12 #undef B_TRANSLATION_CONTEXT
13 #define B_TRANSLATION_CONTEXT "Settings"
16 Settings
* settings
= NULL
;
19 StringValueSetting::StringValueSetting(const char* name
,
20 const char* defaultValue
, const char* valueExpectedErrorString
,
21 const char* wrongValueErrorString
)
23 SettingsArgvDispatcher(name
),
24 fDefaultValue(defaultValue
),
25 fValueExpectedErrorString(valueExpectedErrorString
),
26 fWrongValueErrorString(wrongValueErrorString
),
27 fValue(strdup(defaultValue
))
32 StringValueSetting::~StringValueSetting()
39 StringValueSetting::ValueChanged(const char* newValue
)
41 if (newValue
== fValue
)
42 // guard against self assingment
46 fValue
= strdup(newValue
);
51 StringValueSetting::Value() const
58 StringValueSetting::SaveSettingValue(Settings
* settings
)
60 printf("-----StringValueSetting::SaveSettingValue %s %s\n", Name(), fValue
);
61 settings
->Write("\"%s\"", fValue
);
66 StringValueSetting::NeedsSaving() const
68 // needs saving if different than default
69 return strcmp(fValue
, fDefaultValue
) != 0;
74 StringValueSetting::Handle(const char *const *argv
)
77 return fValueExpectedErrorString
;
87 EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name
,
88 const char* defaultValue
, StringEnumerator enumerator
,
89 const char* valueExpectedErrorString
,
90 const char* wrongValueErrorString
)
92 StringValueSetting(name
, defaultValue
, valueExpectedErrorString
,
93 wrongValueErrorString
),
94 fEnumerator(enumerator
)
100 EnumeratedStringValueSetting::ValueChanged(const char* newValue
)
103 // must be one of the enumerated values
104 ASSERT(_ValidateString(newValue
));
106 StringValueSetting::ValueChanged(newValue
);
111 EnumeratedStringValueSetting::Handle(const char *const *argv
)
114 return fValueExpectedErrorString
;
116 printf("---EnumeratedStringValueSetting::Handle %s %s\n", *(argv
-1), *argv
);
117 if (!_ValidateString(*argv
))
118 return fWrongValueErrorString
;
126 EnumeratedStringValueSetting::_ValidateString(const char* string
)
128 for (int32 i
= 0;; i
++) {
129 const char* enumString
= fEnumerator(i
);
132 if (strcmp(enumString
, string
) == 0)
142 ScalarValueSetting::ScalarValueSetting(const char* name
, int32 defaultValue
,
143 const char* valueExpectedErrorString
, const char* wrongValueErrorString
,
144 int32 min
, int32 max
)
145 : SettingsArgvDispatcher(name
),
146 fDefaultValue(defaultValue
),
147 fValue(defaultValue
),
150 fValueExpectedErrorString(valueExpectedErrorString
),
151 fWrongValueErrorString(wrongValueErrorString
)
156 ScalarValueSetting::~ScalarValueSetting()
162 ScalarValueSetting::ValueChanged(int32 newValue
)
164 ASSERT(newValue
> fMin
);
165 ASSERT(newValue
< fMax
);
171 ScalarValueSetting::Value() const
178 ScalarValueSetting::GetValueAsString(char* buffer
) const
180 sprintf(buffer
, "%" B_PRId32
, fValue
);
185 ScalarValueSetting::Handle(const char *const *argv
)
188 return fValueExpectedErrorString
;
190 int32 newValue
= atoi(*argv
);
191 if (newValue
< fMin
|| newValue
> fMax
)
192 return fWrongValueErrorString
;
200 ScalarValueSetting::SaveSettingValue(Settings
* settings
)
202 settings
->Write("%d", fValue
);
207 ScalarValueSetting::NeedsSaving() const
209 return fValue
!= fDefaultValue
;
216 BooleanValueSetting::BooleanValueSetting(const char* name
, bool defaultValue
)
217 : ScalarValueSetting(name
, defaultValue
, 0, 0)
222 BooleanValueSetting::~BooleanValueSetting()
228 BooleanValueSetting::Value() const
235 BooleanValueSetting::Handle(const char *const *argv
)
238 return B_TRANSLATE_COMMENT("on or off expected","Do not translate "
242 if (strcmp(*argv
, "on") == 0)
244 else if (strcmp(*argv
, "off") == 0)
247 return B_TRANSLATE_COMMENT("on or off expected", "Do not translate "
256 BooleanValueSetting::SaveSettingValue(Settings
* settings
)
258 settings
->Write(fValue
? "on" : "off");