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