2 * Copyright 2013, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
10 #include <AutoLocker.h>
12 #include "SettingsDescription.h"
15 // #pragma mark - Settings
18 Settings::Settings(SettingsDescription
* description
)
21 fDescription(description
)
23 fDescription
->AcquireReference();
29 fDescription
->ReleaseReference();
36 return fLock
.InitCheck();
41 Settings::Value(Setting
* setting
) const
43 AutoLocker
<BLocker
> locker(fLock
);
46 return value
.SetFromMessage(fValues
, setting
->ID()) == B_OK
47 ? value
: setting
->DefaultValue();
52 Settings::Value(const char* settingID
) const
54 AutoLocker
<BLocker
> locker(fLock
);
57 if (value
.SetFromMessage(fValues
, settingID
) == B_OK
)
60 Setting
* setting
= fDescription
->SettingByID(settingID
);
61 return setting
!= NULL
? setting
->DefaultValue() : value
;
66 Settings::SetValue(Setting
* setting
, const BVariant
& value
)
68 AutoLocker
<BLocker
> locker(fLock
);
70 // remove the message field and re-add it with the new value
71 const char* fieldName
= setting
->ID();
72 fValues
.RemoveName(fieldName
);
74 bool success
= value
.AddToMessage(fValues
, fieldName
) == B_OK
;
76 // notify the listeners
77 int32 count
= fListeners
.CountItems();
78 for (int32 i
= count
- 1; i
>= 0; i
--)
79 fListeners
.ItemAt(i
)->SettingValueChanged(setting
);
86 Settings::RestoreValues(const BMessage
& message
)
88 AutoLocker
<BLocker
> locker(fLock
);
90 for (int32 i
= 0; i
< fDescription
->CountSettings(); i
++) {
91 Setting
* setting
= fDescription
->SettingAt(i
);
93 if (value
.SetFromMessage(message
, setting
->ID()) == B_OK
) {
94 if (!SetValue(setting
, value
))
104 Settings::OptionValue(OptionsSetting
* setting
) const
106 BVariant value
= Value(setting
);
107 return value
.Type() == B_STRING_TYPE
108 ? setting
->OptionByID(value
.ToString())
109 : setting
->DefaultOption();
114 Settings::AddListener(Listener
* listener
)
116 AutoLocker
<BLocker
> locker(fLock
);
117 return fListeners
.AddItem(listener
);
122 Settings::RemoveListener(Listener
* listener
)
124 AutoLocker
<BLocker
> locker(fLock
);
125 fListeners
.RemoveItem(listener
);
129 // #pragma mark - Listener
132 Settings::Listener::~Listener()