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 } |
54 |
54 |
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 print ("config::load: Loading configuration file from %1\n", settings->fileName()); |
65 |
65 |
66 for (Config* cfg : g_configPointers) |
66 for (Config* cfg : g_configPointers) |
67 { |
67 { |
68 if (!cfg) |
68 if (!cfg) |
69 break; |
69 break; |
70 |
70 |
71 QVariant val = settings->value (cfg->getName(), cfg->getDefaultAsVariant()); |
71 QVariant val = settings->value (cfg->name(), cfg->getDefaultAsVariant()); |
72 cfg->loadFromVariant (val); |
72 cfg->loadFromVariant (val); |
73 g_configsByName[cfg->getName()] = cfg; |
73 g_configsByName[cfg->name()] = cfg; |
74 g_configs << cfg; |
74 g_configs << cfg; |
75 } |
75 } |
76 |
76 |
77 settings->deleteLater(); |
77 settings->deleteLater(); |
78 return true; |
78 return true; |
79 } |
79 } |
80 |
80 |
81 // ============================================================================= |
81 // ============================================================================= |
|
82 // |
82 // Save the configuration to disk |
83 // Save the configuration to disk |
83 // ----------------------------------------------------------------------------- |
84 // |
84 bool Config::save() |
85 bool Config::save() |
85 { |
86 { |
86 QSettings* settings = getSettingsObject(); |
87 QSettings* settings = getSettingsObject(); |
87 log ("Saving configuration to %1...\n", settings->fileName()); |
|
88 |
88 |
89 for (Config* cfg : g_configs) |
89 for (Config* cfg : g_configs) |
90 { |
90 { |
91 if (!cfg->isDefault()) |
91 if (!cfg->isDefault()) |
92 settings->setValue (cfg->getName(), cfg->toVariant()); |
92 settings->setValue (cfg->name(), cfg->toVariant()); |
93 else |
93 else |
94 settings->remove (cfg->getName()); |
94 settings->remove (cfg->name()); |
95 } |
95 } |
96 |
96 |
97 settings->sync(); |
97 settings->sync(); |
|
98 print ("Configuration saved to %1.\n", settings->fileName()); |
98 settings->deleteLater(); |
99 settings->deleteLater(); |
99 return true; |
100 return true; |
100 } |
101 } |
101 |
102 |
102 // ============================================================================= |
103 // ============================================================================= |
103 // Reset configuration to defaults. |
104 // Reset configuration to defaults. |
104 // ----------------------------------------------------------------------------- |
105 // ============================================================================= |
105 void Config::reset() |
106 void Config::reset() |
106 { |
107 { |
107 for (Config* cfg : g_configs) |
108 for (Config* cfg : g_configs) |
108 cfg->resetValue(); |
109 cfg->resetValue(); |
109 } |
110 } |
110 |
111 |
111 // ============================================================================= |
112 // ============================================================================= |
112 // Where is the configuration file located at? |
113 // Where is the configuration file located at? |
113 // ----------------------------------------------------------------------------- |
114 // ============================================================================= |
114 QString Config::filepath (QString file) |
115 QString Config::filepath (QString file) |
115 { |
116 { |
116 return Config::dirpath() + DIRSLASH + file; |
117 return Config::dirpath() + DIRSLASH + file; |
117 } |
118 } |
118 |
119 |
119 // ============================================================================= |
120 // ============================================================================= |
120 // Directory of the configuration file. |
121 // Directory of the configuration file. |
121 // ----------------------------------------------------------------------------- |
122 // ============================================================================= |
122 QString Config::dirpath() |
123 QString Config::dirpath() |
123 { |
124 { |
124 QSettings* cfg = getSettingsObject(); |
125 QSettings* cfg = getSettingsObject(); |
125 return dirname (cfg->fileName()); |
126 return dirname (cfg->fileName()); |
126 } |
127 } |
127 |
128 |
128 // ============================================================================= |
129 // ============================================================================= |
129 // We cannot just add config objects to a list or vector because that would rely |
130 // 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 |
131 // 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. |
132 // variables we cannot assume that, therefore we need to use a C-style array here. |
132 // ----------------------------------------------------------------------------- |
133 // ============================================================================= |
133 void Config::addToArray (Config* ptr) |
134 void Config::addToArray (Config* ptr) |
134 { |
135 { |
135 if (g_cfgPointerCursor == 0) |
136 if (g_cfgPointerCursor == 0) |
136 memset (g_configPointers, 0, sizeof g_configPointers); |
137 memset (g_configPointers, 0, sizeof g_configPointers); |
137 |
138 |
138 assert (g_cfgPointerCursor < MAX_CONFIG); |
139 assert (g_cfgPointerCursor < MAX_CONFIG); |
139 g_configPointers[g_cfgPointerCursor++] = ptr; |
140 g_configPointers[g_cfgPointerCursor++] = ptr; |
140 } |
141 } |
141 |
142 |
142 // ============================================================================= |
143 // ============================================================================= |
143 // ----------------------------------------------------------------------------- |
144 // ============================================================================= |
144 template<class T> T* getConfigByName (QString name, Config::Type type) |
145 template<class T> T* getConfigByName (QString name, Config::Type type) |
145 { |
146 { |
146 auto it = g_configsByName.find (name); |
147 auto it = g_configsByName.find (name); |
147 |
148 |
148 if (it == g_configsByName.end()) |
149 if (it == g_configsByName.end()) |