src/Configuration.cc

changeset 639
851634b85893
parent 632
63e6243b880c
child 642
751a8df42842
equal deleted inserted replaced
638:382226e40865 639:851634b85893
43 static QMap<QString, Config*> g_configsByName; 43 static QMap<QString, Config*> g_configsByName;
44 static QList<Config*> g_configs; 44 static QList<Config*> g_configs;
45 45
46 // ============================================================================= 46 // =============================================================================
47 // Get the QSettings object. 47 // Get the QSettings object.
48 // ----------------------------------------------------------------------------- 48 // =============================================================================
49 static QSettings* getSettingsObject() 49 static QSettings* getSettingsObject()
50 { 50 {
51 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION; 51 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION;
52 return new QSettings (path, QSettings::IniFormat); 52 return new QSettings (path, QSettings::IniFormat);
53 } 53 }
55 Config::Config (QString name) : 55 Config::Config (QString name) :
56 m_Name (name) {} 56 m_Name (name) {}
57 57
58 // ============================================================================= 58 // =============================================================================
59 // Load the configuration from file 59 // Load the configuration from file
60 // ----------------------------------------------------------------------------- 60 // =============================================================================
61 bool Config::load() 61 bool Config::load()
62 { 62 {
63 QSettings* settings = getSettingsObject(); 63 QSettings* settings = getSettingsObject();
64 log ("config::load: Loading configuration file from %1\n", settings->fileName()); 64 log ("config::load: Loading configuration file from %1\n", settings->fileName());
65 65
78 return true; 78 return true;
79 } 79 }
80 80
81 // ============================================================================= 81 // =============================================================================
82 // Save the configuration to disk 82 // Save the configuration to disk
83 // ----------------------------------------------------------------------------- 83 // =============================================================================
84 bool Config::save() 84 bool Config::save()
85 { 85 {
86 QSettings* settings = getSettingsObject(); 86 QSettings* settings = getSettingsObject();
87 log ("Saving configuration to %1...\n", settings->fileName()); 87 log ("Saving configuration to %1...\n", settings->fileName());
88 88
99 return true; 99 return true;
100 } 100 }
101 101
102 // ============================================================================= 102 // =============================================================================
103 // Reset configuration to defaults. 103 // Reset configuration to defaults.
104 // ----------------------------------------------------------------------------- 104 // =============================================================================
105 void Config::reset() 105 void Config::reset()
106 { 106 {
107 for (Config* cfg : g_configs) 107 for (Config* cfg : g_configs)
108 cfg->resetValue(); 108 cfg->resetValue();
109 } 109 }
110 110
111 // ============================================================================= 111 // =============================================================================
112 // Where is the configuration file located at? 112 // Where is the configuration file located at?
113 // ----------------------------------------------------------------------------- 113 // =============================================================================
114 QString Config::filepath (QString file) 114 QString Config::filepath (QString file)
115 { 115 {
116 return Config::dirpath() + DIRSLASH + file; 116 return Config::dirpath() + DIRSLASH + file;
117 } 117 }
118 118
119 // ============================================================================= 119 // =============================================================================
120 // Directory of the configuration file. 120 // Directory of the configuration file.
121 // ----------------------------------------------------------------------------- 121 // =============================================================================
122 QString Config::dirpath() 122 QString Config::dirpath()
123 { 123 {
124 QSettings* cfg = getSettingsObject(); 124 QSettings* cfg = getSettingsObject();
125 return dirname (cfg->fileName()); 125 return dirname (cfg->fileName());
126 } 126 }
127 127
128 // ============================================================================= 128 // =============================================================================
129 // We cannot just add config objects to a list or vector because that would rely 129 // We cannot just add config objects to a list or vector because that would rely
130 // on the vector's c-tor being called before the configs' c-tors. With global 130 // on the vector's c-tor being called before the configs' c-tors. With global
131 // variables we cannot assume that, therefore we need to use a C-style array here. 131 // variables we cannot assume that, therefore we need to use a C-style array here.
132 // ----------------------------------------------------------------------------- 132 // =============================================================================
133 void Config::addToArray (Config* ptr) 133 void Config::addToArray (Config* ptr)
134 { 134 {
135 if (g_cfgPointerCursor == 0) 135 if (g_cfgPointerCursor == 0)
136 memset (g_configPointers, 0, sizeof g_configPointers); 136 memset (g_configPointers, 0, sizeof g_configPointers);
137 137
138 assert (g_cfgPointerCursor < MAX_CONFIG); 138 assert (g_cfgPointerCursor < MAX_CONFIG);
139 g_configPointers[g_cfgPointerCursor++] = ptr; 139 g_configPointers[g_cfgPointerCursor++] = ptr;
140 } 140 }
141 141
142 // ============================================================================= 142 // =============================================================================
143 // ----------------------------------------------------------------------------- 143 // =============================================================================
144 template<class T> T* getConfigByName (QString name, Config::Type type) 144 template<class T> T* getConfigByName (QString name, Config::Type type)
145 { 145 {
146 auto it = g_configsByName.find (name); 146 auto it = g_configsByName.find (name);
147 147
148 if (it == g_configsByName.end()) 148 if (it == g_configsByName.end())
158 158
159 return reinterpret_cast<T*> (cfg); 159 return reinterpret_cast<T*> (cfg);
160 } 160 }
161 161
162 // ============================================================================= 162 // =============================================================================
163 // ----------------------------------------------------------------------------- 163 // =============================================================================
164 #undef IMPLEMENT_CONFIG 164 #undef IMPLEMENT_CONFIG
165 165
166 #define IMPLEMENT_CONFIG(NAME) \ 166 #define IMPLEMENT_CONFIG(NAME) \
167 NAME##Config* NAME##Config::getByName (QString name) \ 167 NAME##Config* NAME##Config::getByName (QString name) \
168 { \ 168 { \

mercurial