Sayonara Player
Setting.h
1 /* Setting.h */
2 
3 /* Copyright (C) 2011-2016 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 
23 #ifndef SETTING_H
24 #define SETTING_H
25 
26 #include "Helper/Settings/SettingKey.h"
27 #include "Helper/Settings/SettingConverter.h"
28 #include "Helper/Settings/SettingNotifier.h"
29 #include "Database/DatabaseSettings.h"
30 #include "Helper/Logger/Logger.h"
31 
32 
40 
41  protected:
42  SK::SettingKey _key;
43  QString _db_key;
44 
45  AbstrSetting();
46  AbstrSetting(const AbstrSetting&);
47  AbstrSetting(SK::SettingKey key, const char* db_key);
48 
49  public:
50  virtual ~AbstrSetting();
51 
52  QString get_db_key() const;
53  SK::SettingKey get_key() const;
54 
55  /* Pure virtual function for DB load/save */
56  virtual void load_db(DatabaseSettings* db)=0;
57  virtual void store_db(DatabaseSettings* db)=0;
58 };
59 
60 
61 template< typename T,
62  template <typename Arg> class SC = SettingConverter >
68 class Setting : public AbstrSetting
69 {
70 
71  private:
72  Setting();
73  Setting(const Setting&);
74 
75  T _val;
76  T _default_val;
77  bool _db_setting;
78 
79 
80  public:
81 
82  /* Constructor */
83  template<typename SK::SettingKey S>
84  Setting(const SettingKey<T, S>& key, const char* db_key, T def) :
85  AbstrSetting(S, db_key)
86  {
87  Q_UNUSED(key);
88  _default_val = def;
89  _val = def;
90  _db_setting = true;
91  }
92 
93  template<typename SK::SettingKey S>
94  Setting(const SettingKey<T, S>& key, T def) :
95  AbstrSetting(S, "")
96  {
97  Q_UNUSED(key);
98  _default_val = def;
99  _val = def;
100  _db_setting = false;
101  }
102 
103  /* Destructor */
104  virtual ~Setting(){
105 
106  }
107 
108 
109  /* Load setting from DB */
110  virtual void load_db(DatabaseSettings* db){
111 
112  if(!_db_setting) return;
113 
114  QString s;
115  bool success = db->load_setting(_db_key, s);
116 
117  if(!success){
118  sp_log(Log::Warning) << "Setting " << _db_key << ": Not found. Use default value...";
119  _val = _default_val;
120  sp_log(Log::Info) << "Load Setting " << _db_key << ": " << SC<T>::cvt_to_string(_val);
121  return;
122  }
123 
124  success = SC<T>::cvt_from_string(s, _val);
125  if(!success){
126  sp_log(Log::Warning) << "Setting " << _db_key << ": Cannot convert. Use default value...";
127  _val = _default_val;
128  }
129 
130  //sp_log(Log::Info) << "Load Setting " << _db_key << ": " << SC<T>::cvt_to_string(_val);
131  }
132 
133  /* Save setting to DB */
134  virtual void store_db(DatabaseSettings* db){
135 
136  if(!_db_setting) return;
137 
138  QString s = SC<T>::cvt_to_string(_val);
139  db->store_setting(_db_key, s);
140  //sp_log(Log::Info) << "Store Setting " << _db_key << ": " << s;
141  }
142 
143  /* ... */
144  const T& getValue() const {
145  return _val;
146  }
147 
148  /* ... */
149  const T& getDefaultValue() const {
150  return _default_val;
151  }
152 
153  /* ... */
154  bool setValue(const T& val){
155 
156  if( _val == val ){
157  return false;
158  }
159 
160  _val = val;
161  return true;
162  }
163 };
164 
165 #endif // SETTING_H
The Setting class T is the pure value type e.g. QString.
Definition: Setting.h:68
Definition: SettingKey.h:151
Definition: DatabaseSettings.h:29
The AbstrSetting class Every setting needs a key and a value The SK::SettingKey is only used inside t...
Definition: Setting.h:39
The SettingConverter class.
Definition: SettingConverter.h:37