1 /*****************************************************************************/
3 // Written by Michael Wilber, OBOS Translation Kit Team
5 // TranslatorSettings.cpp
7 // This class manages (saves/loads/locks/unlocks) the settings
11 // Copyright (c) 2004 OpenBeOS Project
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 /*****************************************************************************/
34 #include <FindDirectory.h>
35 #include <TranslatorFormats.h>
36 // for B_TRANSLATOR_EXT_*
37 #include "TranslatorSettings.h"
39 // ---------------------------------------------------------------
42 // Sets the default settings, location for the settings file
43 // and sets the reference count to 1
52 // ---------------------------------------------------------------
53 TranslatorSettings::TranslatorSettings(const char *settingsFile
,
54 const TranSetting
*defaults
, int32 defCount
)
55 : fLock("TranslatorSettings Lock")
57 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &fSettingsPath
))
58 fSettingsPath
.SetTo("/tmp");
59 fSettingsPath
.Append(settingsFile
);
71 // Add Default Settings
72 // (Used when loading from the settings file or from
74 const TranSetting
*defs
= fDefaults
;
75 for (int32 i
= 0; i
< fDefCount
; i
++) {
76 switch (defs
[i
].dataType
) {
77 case TRAN_SETTING_BOOL
:
78 fSettingsMsg
.AddBool(defs
[i
].name
,
79 static_cast<bool>(defs
[i
].defaultVal
));
82 case TRAN_SETTING_INT32
:
83 fSettingsMsg
.AddInt32(defs
[i
].name
, defs
[i
].defaultVal
);
87 // ASSERT here? Erase the bogus setting entry instead?
93 // ---------------------------------------------------------------
96 // Returns a pointer to the TranslatorSettings and increments
97 // the reference count.
105 // Returns: pointer to this TranslatorSettings object
106 // ---------------------------------------------------------------
108 TranslatorSettings::Acquire()
110 TranslatorSettings
*psettings
= NULL
;
120 // ---------------------------------------------------------------
123 // Decrements the reference count and deletes the
124 // TranslatorSettings if the reference count is zero.
132 // Returns: pointer to this TranslatorSettings object if
133 // the reference count is greater than zero, returns NULL
134 // if the reference count is zero and the TranslatorSettings
135 // object has been deleted
136 // ---------------------------------------------------------------
138 TranslatorSettings::Release()
140 TranslatorSettings
*psettings
= NULL
;
149 // delete this object and
155 // ---------------------------------------------------------------
167 // ---------------------------------------------------------------
168 TranslatorSettings::~TranslatorSettings()
172 // ---------------------------------------------------------------
175 // Loads the settings by reading them from the default
184 // Returns: B_OK if there were no errors or an error code from
185 // BFile::SetTo() or BMessage::Unflatten() if there were errors
186 // ---------------------------------------------------------------
188 TranslatorSettings::LoadSettings()
194 // Don't try to open the settings file if there are
195 // no settings that need to be loaded
198 result
= settingsFile
.SetTo(fSettingsPath
.Path(), B_READ_ONLY
);
199 if (result
== B_OK
) {
201 result
= msg
.Unflatten(&settingsFile
);
203 result
= LoadSettings(&msg
);
213 // ---------------------------------------------------------------
216 // Loads the settings from a BMessage passed to the function.
220 // Parameters: pmsg pointer to BMessage that contains the
225 // Returns: B_BAD_VALUE if pmsg is NULL or invalid options
226 // have been found, B_OK if there were no
227 // errors or an error code from BMessage::FindBool() or
228 // BMessage::ReplaceBool() if there were other errors
229 // ---------------------------------------------------------------
231 TranslatorSettings::LoadSettings(BMessage
*pmsg
)
237 const TranSetting
*defaults
= fDefaults
;
238 for (int32 i
= 0; i
< fDefCount
; i
++) {
239 switch (defaults
[i
].dataType
) {
240 case TRAN_SETTING_BOOL
:
243 if (pmsg
->FindBool(defaults
[i
].name
, &value
) != B_OK
) {
244 if (fSettingsMsg
.HasBool(defaults
[i
].name
))
247 value
= static_cast<bool>(defaults
[i
].defaultVal
);
250 fSettingsMsg
.ReplaceBool(defaults
[i
].name
, value
);
254 case TRAN_SETTING_INT32
:
257 if (pmsg
->FindInt32(defaults
[i
].name
, &value
) != B_OK
) {
258 if (fSettingsMsg
.HasInt32(defaults
[i
].name
))
261 value
= defaults
[i
].defaultVal
;
264 fSettingsMsg
.ReplaceInt32(defaults
[i
].name
, value
);
269 // TODO: ASSERT here? Erase the bogus setting entry instead?
278 // ---------------------------------------------------------------
281 // Saves the settings as a flattened BMessage to the default
290 // Returns: B_OK if no errors or an error code from BFile::SetTo()
291 // or BMessage::Flatten() if there were errors
292 // ---------------------------------------------------------------
294 TranslatorSettings::SaveSettings()
300 // Only write out settings file if there are
301 // actual settings stored by this object
304 result
= settingsFile
.SetTo(fSettingsPath
.Path(),
305 B_WRITE_ONLY
| B_CREATE_FILE
| B_ERASE_FILE
);
307 result
= fSettingsMsg
.Flatten(&settingsFile
);
316 // ---------------------------------------------------------------
317 // GetConfigurationMessage
319 // Saves the current settings to the BMessage passed to the
324 // Parameters: pmsg pointer to BMessage where the settings
329 // Returns: B_OK if there were no errors or an error code from
330 // BMessage::RemoveName() or BMessage::AddBool() if there were
332 // ---------------------------------------------------------------
334 TranslatorSettings::GetConfigurationMessage(BMessage
*pmsg
)
336 status_t result
= B_BAD_VALUE
;
340 for (i
= 0; i
< fDefCount
; i
++) {
341 result
= pmsg
->RemoveName(fDefaults
[i
].name
);
342 if (result
!= B_OK
&& result
!= B_NAME_NOT_FOUND
)
345 if (i
== fDefCount
) {
349 const TranSetting
*defs
= fDefaults
;
350 for (i
= 0; i
< fDefCount
&& result
>= B_OK
; i
++) {
351 switch (defs
[i
].dataType
) {
352 case TRAN_SETTING_BOOL
:
353 result
= pmsg
->AddBool(defs
[i
].name
,
354 SetGetBool(defs
[i
].name
));
357 case TRAN_SETTING_INT32
:
358 result
= pmsg
->AddInt32(defs
[i
].name
,
359 SetGetInt32(defs
[i
].name
));
363 // ASSERT here? Erase the bogus setting entry instead?
375 // ---------------------------------------------------------------
378 // Returns a pointer to the TranSetting with the given name
383 // Parameters: name name of the TranSetting to find
388 // Returns: NULL if the TranSetting cannot be found, or a pointer
389 // to the desired TranSetting if it is found
390 // ---------------------------------------------------------------
392 TranslatorSettings::FindTranSetting(const char *name
)
394 for (int32 i
= 0; i
< fDefCount
; i
++) {
395 if (!strcmp(fDefaults
[i
].name
, name
))
396 return fDefaults
+ i
;
401 // ---------------------------------------------------------------
404 // Sets the state of the bool setting identified by the given name
409 // Parameters: name identifies the setting to set or get
411 // pbool the new value for the bool, or, if null,
412 // it indicates that the caller wants to Get
417 // Returns: the prior value of the setting
418 // ---------------------------------------------------------------
420 TranslatorSettings::SetGetBool(const char *name
, bool *pbool
)
426 const TranSetting
*def
= FindTranSetting(name
);
428 fSettingsMsg
.FindBool(def
->name
, &bprevValue
);
430 fSettingsMsg
.ReplaceBool(def
->name
, *pbool
);
439 // ---------------------------------------------------------------
442 // Sets the state of the int32 setting identified by the given name
447 // Parameters: name identifies the setting to set or get
449 // pint32 the new value for the setting, or, if null,
450 // it indicates that the caller wants to Get
455 // Returns: the prior value of the setting
456 // ---------------------------------------------------------------
458 TranslatorSettings::SetGetInt32(const char *name
, int32
*pint32
)
464 const TranSetting
*def
= FindTranSetting(name
);
466 fSettingsMsg
.FindInt32(def
->name
, &prevValue
);
468 fSettingsMsg
.ReplaceInt32(def
->name
, *pint32
);