Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Preferences.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * preferences.h
17  * Interface for the Preferences class - this allows storing user preferences /
18  * settings.
19  * Copyright (C) 2005-2006 Simon Newton
20  */
21 
22 #ifndef INCLUDE_OLAD_PREFERENCES_H_
23 #define INCLUDE_OLAD_PREFERENCES_H_
24 
25 #include <ola/Logging.h>
26 #include <ola/thread/Thread.h>
27 #include <ola/io/SelectServer.h>
28 
29 #include <map>
30 #include <vector>
31 #include <set>
32 #include <string>
33 
34 
35 namespace ola {
36 
37 using std::map;
38 using std::multimap;
39 using std::set;
40 using std::string;
41 using std::vector;
42 
43 /*
44  * Checks the value of a variable
45  */
46 class Validator {
47  public:
48  Validator() {}
49  virtual ~Validator() {}
50 
51  virtual bool IsValid(const string &value) const = 0;
52 };
53 
54 
55 /*
56  * Check a value is a non-empty string
57  */
58 class StringValidator: public Validator {
59  public:
60  explicit StringValidator(bool empty_ok = false)
61  : Validator(),
62  m_empty_ok(empty_ok) {
63  }
64  bool IsValid(const string &value) const;
65 
66  private:
67  const bool m_empty_ok;
68 };
69 
70 
71 /*
72  * Check that a value is one of a set of values
73  */
74 class SetValidator: public Validator {
75  public:
76  explicit SetValidator(const set<string> &values):
77  m_values(values) {}
78  bool IsValid(const string &value) const;
79 
80  private:
81  set<string> m_values;
82 };
83 
84 
85 /*
86  * Check that a value is a valid bool
87  */
88 class BoolValidator: public Validator {
89  public:
90  BoolValidator(): Validator() {}
91  bool IsValid(const string &value) const;
92 
93  // On win32 TRUE and FALSE are #define'd. We can #undef them here but that
94  // doesn't fix the case in the calling code. So we use ENABLED and DISABLED
95  // instead.
96  static const char ENABLED[];
97  static const char DISABLED[];
98 };
99 
100 
101 /*
102  * Check that a value falls within a range
103  */
104 class IntValidator: public Validator {
105  public:
106  IntValidator(unsigned int greater_than, unsigned int less_than):
107  m_gt(greater_than),
108  m_lt(less_than) {}
109  bool IsValid(const string &value) const;
110 
111  private:
112  unsigned int m_gt, m_lt;
113 };
114 
115 
116 /*
117  * Check a IPv4 address is valid
118  */
119 class IPv4Validator: public Validator {
120  public:
121  explicit IPv4Validator(bool empty_ok = true):
122  m_empty_ok(empty_ok) {}
123 
124  bool IsValid(const string &value) const;
125  private:
126  bool m_empty_ok;
127 };
128 
129 
130 /*
131  * The abstract Preferences class
132  */
133 class Preferences {
134  public:
135  explicit Preferences(const string name): m_preference_name(name) {}
136  virtual ~Preferences() {}
137 
138  virtual bool Load() = 0;
139  virtual bool Save() const = 0;
140  virtual void Clear() = 0;
141 
142  // The location of where these preferences are stored.
143  virtual string Source() const = 0;
144 
145  virtual void SetValue(const string &key, const string &value) = 0;
146  virtual void SetMultipleValue(const string &key, const string &value) = 0;
147  virtual bool SetDefaultValue(const string &key,
148  const Validator &validator,
149  const string &value) = 0;
150 
151  virtual string GetValue(const string &key) const = 0;
152  virtual vector<string> GetMultipleValue(const string &key) const = 0;
153  virtual bool HasKey(const string &key) const = 0;
154 
155  virtual void RemoveValue(const string &key) = 0;
156 
157  // bool helper methods
158  virtual bool GetValueAsBool(const string &key) const = 0;
159  virtual void SetValueAsBool(const string &key, bool value) = 0;
160 
161  protected:
162  string m_preference_name;
163 
164  private:
165  Preferences(const Preferences&);
166  Preferences& operator=(const Preferences&);
167 };
168 
169 
170 /*
171  * A PreferencesFactory creates preferences objects
172  */
174  public:
175  PreferencesFactory() {}
176  virtual ~PreferencesFactory();
177  virtual Preferences *NewPreference(const string &name);
178  private:
179  virtual Preferences *Create(const string &name) = 0;
180  map<string, Preferences*> m_preferences_map;
181 };
182 
183 
184 /*
185  * MemoryPreferences just stores the preferences in memory. Useful for testing.
186  */
188  public:
189  explicit MemoryPreferences(const string name): Preferences(name) {}
190  virtual ~MemoryPreferences();
191  virtual bool Load() { return true; }
192  virtual bool Save() const { return true; }
193  virtual void Clear();
194 
195  virtual string Source() const { return "Not Saved"; }
196 
197  virtual void SetValue(const string &key, const string &value);
198  virtual void SetMultipleValue(const string &key, const string &value);
199  virtual bool SetDefaultValue(const string &key,
200  const Validator &validator,
201  const string &value);
202 
203  virtual string GetValue(const string &key) const;
204  virtual vector<string> GetMultipleValue(const string &key) const;
205  virtual bool HasKey(const string &key) const;
206 
207  virtual void RemoveValue(const string &key);
208 
209  // bool helper methods
210  virtual bool GetValueAsBool(const string &key) const;
211  virtual void SetValueAsBool(const string &key, bool value);
212 
213  bool operator==(const MemoryPreferences &other) {
214  return m_pref_map == other.m_pref_map;
215  }
216 
217  protected:
218  typedef multimap<string, string> PreferencesMap;
219  PreferencesMap m_pref_map;
220 };
221 
222 
224  private:
225  MemoryPreferences *Create(const string &name) {
226  return new MemoryPreferences(name);
227  }
228 };
229 
230 
235  public:
236  typedef multimap<string, string> PreferencesMap;
238 
239  void SavePreferences(const string &filename,
240  const PreferencesMap &preferences);
241 
242  void *Run();
243  bool Join(void *ptr = NULL);
244  void Syncronize();
245 
246  private:
248 
249  void SaveToFile(const string *filename, const PreferencesMap *preferences);
250  void CompleteSyncronization(ola::thread::ConditionVariable *condition,
251  ola::thread::Mutex *mutex);
252 };
253 
254 
255 /*
256  * FilePreferences uses one file per namespace
257  */
259  public:
260  explicit FileBackedPreferences(const string &directory,
261  const string &name,
262  FilePreferenceSaverThread *saver_thread)
263  : MemoryPreferences(name),
264  m_directory(directory),
265  m_saver_thread(saver_thread) {}
266  virtual bool Load();
267  virtual bool Save() const;
268  bool LoadFromFile(const string &filename);
269 
270  string Source() const { return FileName(); }
271 
272  private:
273  const string m_directory;
274  FilePreferenceSaverThread *m_saver_thread;
275 
276  bool ChangeDir() const;
277  const string FileName() const;
278  static const char OLA_CONFIG_PREFIX[];
279  static const char OLA_CONFIG_SUFFIX[];
280 };
281 
282 
284  public:
285  explicit FileBackedPreferencesFactory(const string &directory)
286  : m_directory(directory) {
287  m_saver_thread.Start();
288  }
289 
291  m_saver_thread.Join();
292  }
293 
294  private:
295  const string m_directory;
296  FilePreferenceSaverThread m_saver_thread;
297 
298  FileBackedPreferences *Create(const string &name) {
299  return new FileBackedPreferences(m_directory, name, &m_saver_thread);
300  }
301 };
302 } // namespace ola
303 #endif // INCLUDE_OLAD_PREFERENCES_H_