1*34d0fe82SMatthew Wilber /*****************************************************************************/ 2*34d0fe82SMatthew Wilber // LiveSettings 3*34d0fe82SMatthew Wilber // Written by Michael Wilber 4*34d0fe82SMatthew Wilber // 5*34d0fe82SMatthew Wilber // LiveSettings.h 6*34d0fe82SMatthew Wilber // 7*34d0fe82SMatthew Wilber // This class manages (saves/loads/locks/unlocks) a collection of BMessage 8*34d0fe82SMatthew Wilber // based settings. This class allows you to share settings between different 9*34d0fe82SMatthew Wilber // classes in different threads and receive notifications when the settings 10*34d0fe82SMatthew Wilber // change. This class makes it easy to share settings between a Translator 11*34d0fe82SMatthew Wilber // and its config panel or a Screen Saver and its config panel. 12*34d0fe82SMatthew Wilber // 13*34d0fe82SMatthew Wilber // 14*34d0fe82SMatthew Wilber // Copyright (C) Haiku 15*34d0fe82SMatthew Wilber // 16*34d0fe82SMatthew Wilber // Permission is hereby granted, free of charge, to any person obtaining a 17*34d0fe82SMatthew Wilber // copy of this software and associated documentation files (the "Software"), 18*34d0fe82SMatthew Wilber // to deal in the Software without restriction, including without limitation 19*34d0fe82SMatthew Wilber // the rights to use, copy, modify, merge, publish, distribute, sublicense, 20*34d0fe82SMatthew Wilber // and/or sell copies of the Software, and to permit persons to whom the 21*34d0fe82SMatthew Wilber // Software is furnished to do so, subject to the following conditions: 22*34d0fe82SMatthew Wilber // 23*34d0fe82SMatthew Wilber // The above copyright notice and this permission notice shall be included 24*34d0fe82SMatthew Wilber // in all copies or substantial portions of the Software. 25*34d0fe82SMatthew Wilber // 26*34d0fe82SMatthew Wilber // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 27*34d0fe82SMatthew Wilber // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28*34d0fe82SMatthew Wilber // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 29*34d0fe82SMatthew Wilber // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30*34d0fe82SMatthew Wilber // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 31*34d0fe82SMatthew Wilber // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 32*34d0fe82SMatthew Wilber // DEALINGS IN THE SOFTWARE. 33*34d0fe82SMatthew Wilber /*****************************************************************************/ 34*34d0fe82SMatthew Wilber 35*34d0fe82SMatthew Wilber #ifndef LIVE_SETTINGS_H 36*34d0fe82SMatthew Wilber #define LIVE_SETTINGS_H 37*34d0fe82SMatthew Wilber 38*34d0fe82SMatthew Wilber #include <Locker.h> 39*34d0fe82SMatthew Wilber #include <Path.h> 40*34d0fe82SMatthew Wilber #include <Message.h> 41*34d0fe82SMatthew Wilber #include <String.h> 42*34d0fe82SMatthew Wilber #include <vector> 43*34d0fe82SMatthew Wilber #include "LiveSettingsObserver.h" 44*34d0fe82SMatthew Wilber #include "LiveSetting.h" 45*34d0fe82SMatthew Wilber 46*34d0fe82SMatthew Wilber class LiveSettings { 47*34d0fe82SMatthew Wilber public: 48*34d0fe82SMatthew Wilber LiveSettings(const char *settingsFile, LiveSetting *defaults, 49*34d0fe82SMatthew Wilber int32 defCount); 50*34d0fe82SMatthew Wilber 51*34d0fe82SMatthew Wilber LiveSettings *Acquire(); 52*34d0fe82SMatthew Wilber // increments the reference count, returns this 53*34d0fe82SMatthew Wilber LiveSettings *Release(); 54*34d0fe82SMatthew Wilber // decrements the reference count, deletes this 55*34d0fe82SMatthew Wilber // when count reaches zero, returns this when 56*34d0fe82SMatthew Wilber // ref count is greater than zero, NULL when 57*34d0fe82SMatthew Wilber // ref count is zero 58*34d0fe82SMatthew Wilber 59*34d0fe82SMatthew Wilber bool AddObserver(LiveSettingsObserver *observer); 60*34d0fe82SMatthew Wilber // returns true if observer was added sucessfully, 61*34d0fe82SMatthew Wilber // false if observer already in the list or error 62*34d0fe82SMatthew Wilber bool RemoveObserver(LiveSettingsObserver *observer); 63*34d0fe82SMatthew Wilber // returns true if observer was removed successfully, 64*34d0fe82SMatthew Wilber // false if observer not found or error 65*34d0fe82SMatthew Wilber 66*34d0fe82SMatthew Wilber status_t LoadSettings(); 67*34d0fe82SMatthew Wilber status_t LoadSettings(BMessage *pmsg); 68*34d0fe82SMatthew Wilber status_t SaveSettings(); 69*34d0fe82SMatthew Wilber status_t GetConfigurationMessage(BMessage *pmsg); 70*34d0fe82SMatthew Wilber 71*34d0fe82SMatthew Wilber bool SetGetBool(const char *name, bool *pVal = NULL); 72*34d0fe82SMatthew Wilber int32 SetGetInt32(const char *name, int32 *pVal = NULL); 73*34d0fe82SMatthew Wilber 74*34d0fe82SMatthew Wilber void SetString(const char *name, const BString &str); 75*34d0fe82SMatthew Wilber void GetString(const char *name, BString &str); 76*34d0fe82SMatthew Wilber 77*34d0fe82SMatthew Wilber private: 78*34d0fe82SMatthew Wilber const LiveSetting *FindLiveSetting(const char *name); 79*34d0fe82SMatthew Wilber ~LiveSettings(); 80*34d0fe82SMatthew Wilber // private so that Release() must be used 81*34d0fe82SMatthew Wilber // to delete the object 82*34d0fe82SMatthew Wilber 83*34d0fe82SMatthew Wilber void NotifySettingChanged(uint32 setting); 84*34d0fe82SMatthew Wilber 85*34d0fe82SMatthew Wilber template <class T> 86*34d0fe82SMatthew Wilber bool GetValue(const char *name, T &val); 87*34d0fe82SMatthew Wilber 88*34d0fe82SMatthew Wilber template <class T> 89*34d0fe82SMatthew Wilber bool SetValue(const char *name, const T &val); 90*34d0fe82SMatthew Wilber 91*34d0fe82SMatthew Wilber BLocker fLock; 92*34d0fe82SMatthew Wilber int32 fRefCount; 93*34d0fe82SMatthew Wilber BPath fSettingsPath; 94*34d0fe82SMatthew Wilber // where the settings file will be loaded from / 95*34d0fe82SMatthew Wilber // saved to 96*34d0fe82SMatthew Wilber 97*34d0fe82SMatthew Wilber BMessage fSettingsMsg; 98*34d0fe82SMatthew Wilber // the actual settings 99*34d0fe82SMatthew Wilber 100*34d0fe82SMatthew Wilber const LiveSetting *fDefaults; 101*34d0fe82SMatthew Wilber int32 fDefCount; 102*34d0fe82SMatthew Wilber 103*34d0fe82SMatthew Wilber typedef std::vector<LiveSettingsObserver *> ObserverList; 104*34d0fe82SMatthew Wilber ObserverList fObservers; 105*34d0fe82SMatthew Wilber }; 106*34d0fe82SMatthew Wilber 107*34d0fe82SMatthew Wilber #endif // #ifndef LIVE_SETTTINGS_H 108*34d0fe82SMatthew Wilber 109