headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / debugger / settings / generic / Settings.cpp
blobdfc176d3a74b8f1b011c792ebba974840e80d66f
1 /*
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.
5 */
8 #include "Settings.h"
10 #include <AutoLocker.h>
12 #include "SettingsDescription.h"
15 // #pragma mark - Settings
18 Settings::Settings(SettingsDescription* description)
20 fLock("settings"),
21 fDescription(description)
23 fDescription->AcquireReference();
27 Settings::~Settings()
29 fDescription->ReleaseReference();
33 status_t
34 Settings::Init()
36 return fLock.InitCheck();
40 BVariant
41 Settings::Value(Setting* setting) const
43 AutoLocker<BLocker> locker(fLock);
45 BVariant value;
46 return value.SetFromMessage(fValues, setting->ID()) == B_OK
47 ? value : setting->DefaultValue();
51 BVariant
52 Settings::Value(const char* settingID) const
54 AutoLocker<BLocker> locker(fLock);
56 BVariant value;
57 if (value.SetFromMessage(fValues, settingID) == B_OK)
58 return value;
60 Setting* setting = fDescription->SettingByID(settingID);
61 return setting != NULL ? setting->DefaultValue() : value;
65 bool
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);
81 return success;
85 bool
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);
92 BVariant value;
93 if (value.SetFromMessage(message, setting->ID()) == B_OK) {
94 if (!SetValue(setting, value))
95 return false;
99 return true;
103 SettingsOption*
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();
113 bool
114 Settings::AddListener(Listener* listener)
116 AutoLocker<BLocker> locker(fLock);
117 return fListeners.AddItem(listener);
121 void
122 Settings::RemoveListener(Listener* listener)
124 AutoLocker<BLocker> locker(fLock);
125 fListeners.RemoveItem(listener);
129 // #pragma mark - Listener
132 Settings::Listener::~Listener()