00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pent_include.h"
00020
00021 #include "SettingManager.h"
00022 #include "ConfigFileManager.h"
00023
00024 using Pentagram::istring;
00025
00026 SettingManager* SettingManager::settingmanager = 0;
00027
00028 SettingManager::SettingManager()
00029 {
00030 con.Print(MM_INFO, "Creating SettingManager...\n");
00031
00032 assert(settingmanager == 0);
00033 settingmanager = this;
00034
00035 domains.resize(DOM_GAME+1);
00036
00037 conffileman = ConfigFileManager::get_instance();
00038
00039 conffileman->readConfigString("", "defaultsettings", false);
00040 }
00041
00042 SettingManager::~SettingManager()
00043 {
00044 con.Print(MM_INFO, "Destroying SettingManager...\n");
00045
00046 settingmanager = 0;
00047 }
00048
00049 bool SettingManager::readConfigFile(std::string fname, bool readonly)
00050 {
00051 return conffileman->readConfigFile(fname, "settings", readonly);
00052 }
00053
00054 void SettingManager::write()
00055 {
00056 conffileman->write("settings");
00057 }
00058
00059 bool SettingManager::exists(istring key, Domain dom)
00060 {
00061 Domain temp;
00062
00063 return findKeyDomain(key, dom, temp);
00064 }
00065
00066 bool SettingManager::get(istring key, std::string &ret, Domain dom)
00067 {
00068 Domain keydom;
00069 bool found = findKeyDomain(key, dom, keydom);
00070 if (!found) return false;
00071
00072 conffileman->get(getConfigKey(key,keydom), ret);
00073
00074 return true;
00075 }
00076
00077 bool SettingManager::get(istring key, int &ret, Domain dom)
00078 {
00079 Domain keydom;
00080 bool found = findKeyDomain(key, dom, keydom);
00081 if (!found) return false;
00082
00083 conffileman->get(getConfigKey(key,keydom), ret);
00084
00085 return true;
00086 }
00087
00088 bool SettingManager::get(istring key, bool &ret, Domain dom)
00089 {
00090 Domain keydom;
00091 bool found = findKeyDomain(key, dom, keydom);
00092 if (!found) return false;
00093
00094 conffileman->get(getConfigKey(key,keydom), ret);
00095
00096 return true;
00097 }
00098
00099
00100 void SettingManager::set(istring key, std::string value, Domain dom)
00101 {
00102 conffileman->set(getConfigKey(key,dom), value);
00103
00104 callCallbacks(key);
00105 }
00106
00107 void SettingManager::set(istring key, const char* value, Domain dom)
00108 {
00109 conffileman->set(getConfigKey(key,dom), value);
00110
00111 callCallbacks(key);
00112 }
00113
00114 void SettingManager::set(istring key, int value, Domain dom)
00115 {
00116 conffileman->set(getConfigKey(key,dom), value);
00117
00118 callCallbacks(key);
00119 }
00120
00121 void SettingManager::set(istring key, bool value, Domain dom)
00122 {
00123 conffileman->set(getConfigKey(key,dom), value);
00124
00125 callCallbacks(key);
00126 }
00127
00128 void SettingManager::unset(istring key, Domain dom)
00129 {
00130 conffileman->unset(getConfigKey(key,dom));
00131
00132 callCallbacks(key);
00133 }
00134
00135
00136
00137
00138 void SettingManager::setDefault(istring key, std::string value)
00139 {
00140 set(key, value, DOM_DEFAULTS);
00141 }
00142
00143 void SettingManager::setDefault(istring key, const char* value)
00144 {
00145 set(key, value, DOM_DEFAULTS);
00146 }
00147
00148 void SettingManager::setDefault(istring key, int value)
00149 {
00150 set(key, value, DOM_DEFAULTS);
00151 }
00152
00153 void SettingManager::setDefault(istring key, bool value)
00154 {
00155 set(key, value, DOM_DEFAULTS);
00156 }
00157
00158
00159
00160 void SettingManager::setCurrentDomain(Domain dom)
00161 {
00162 currentDomain = dom;
00163 }
00164
00165 void SettingManager::setDomainName(Domain dom, istring section)
00166 {
00167 unsigned int d = static_cast<unsigned int>(dom);
00168
00169 if (domains.size() <= d) domains.resize(d+1);
00170 domains[d] = section;
00171 }
00172
00173 void SettingManager::registerCallback(istring key, ConfigCallback callback)
00174 {
00175 callbacks[key].push_back(callback);
00176 }
00177
00178 void SettingManager::unregisterCallback(istring key, ConfigCallback callback)
00179 {
00180 std::map<istring, std::vector<ConfigCallback> >::iterator i;
00181 i = callbacks.find(key);
00182
00183 if (i == callbacks.end()) return;
00184
00185 std::vector<ConfigCallback>& cb = (*i).second;
00186 std::vector<ConfigCallback>::iterator iter;
00187 for (iter = cb.begin(); iter != cb.end(); ++iter) {
00188 if (*iter == callback) {
00189 cb.erase(iter);
00190 return;
00191 }
00192 }
00193 }
00194
00195 std::vector<istring> SettingManager::listGames()
00196 {
00197
00198
00199 std::vector<istring> sections, games;
00200 sections = conffileman->listSections("settings", false);
00201
00202 games.push_back("pentagram");
00203
00204 for (unsigned int i = 0; i < sections.size(); ++i) {
00205 istring name = sections[i];
00206
00207 istring pathkey = "settings/" + name + "/path";
00208 if (name.find(':') == istring::npos && conffileman->exists(pathkey) && name != "pentagram")
00209 games.push_back(name);
00210 }
00211
00212 return games;
00213 }
00214
00215 std::vector<istring> SettingManager::listDataKeys(istring section)
00216 {
00217 istring csection = "settings/" + domains[DOM_GAME] + ":" + section;
00218
00219 return conffileman->listKeys(csection, false);
00220 }
00221
00222 std::map<istring,std::string> SettingManager::listDataValues(istring section)
00223 {
00224 istring csection = "settings/" + domains[DOM_GAME] + ":" + section;
00225
00226 return conffileman->listKeyValues(csection, false);
00227 }
00228
00229 bool SettingManager::findKeyDomain(istring key, Domain dom, Domain& keydom)
00230 {
00231
00232
00233
00234
00235 if (dom == DOM_CURRENT) {
00236 int d = static_cast<int>(currentDomain);
00237 for (; d >= 0; --d) {
00238 if (conffileman->exists(getConfigKey(key,static_cast<Domain>(d)))){
00239 keydom = static_cast<Domain>(d);
00240 return true;
00241 }
00242 }
00243 return false;
00244 } else {
00245 keydom = dom;
00246 return conffileman->exists(getConfigKey(key,dom));
00247 }
00248 }
00249
00250 istring SettingManager::getConfigKey(istring key, Domain dom)
00251 {
00252 istring ckey;
00253
00254 if (dom == DOM_CURRENT) dom = currentDomain;
00255
00256 if (dom == DOM_DEFAULTS) {
00257 ckey = "defaultsettings/";
00258 } else {
00259 ckey = "settings/" + domains[dom];
00260 }
00261
00262 istring::size_type pos = key.find('/');
00263
00264 if (pos != istring::npos) {
00265 ckey += ":" + key;
00266 } else {
00267 ckey += "/" + key;
00268 }
00269
00270 return ckey;
00271 }
00272
00273 void SettingManager::callCallbacks(istring key)
00274 {
00275 std::map<istring, std::vector<ConfigCallback> >::iterator i;
00276 i = callbacks.find(key);
00277
00278 if (i == callbacks.end()) return;
00279
00280 std::vector<ConfigCallback>& cb = (*i).second;
00281 std::vector<ConfigCallback>::iterator iter;
00282 for (iter = cb.begin(); iter != cb.end(); ++iter) {
00283 (*iter)(key);
00284 }
00285 }