src/Configuration.cc

changeset 655
b376645315ab
parent 654
a74f2ff353b8
child 656
2a1c204df14d
child 706
d79083b9f74d
equal deleted inserted replaced
654:a74f2ff353b8 655:b376645315ab
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * =====================================================================
18 *
19 * config.cxx: Configuration management. I don't like how unsafe QSettings
20 * is so this implements a type-safer and identifer-safer wrapping system of
21 * configuration variables. QSettings is used underlyingly, this is a matter
22 * of interface.
23 */
24
25 #include <errno.h>
26 #include <QDir>
27 #include <QTextStream>
28 #include <QSettings>
29 #include "Main.h"
30 #include "Configuration.h"
31 #include "Misc.h"
32 #include "MainWindow.h"
33 #include "Document.h"
34
35 #ifdef _WIN32
36 # define EXTENSION ".ini"
37 #else
38 # define EXTENSION ".cfg"
39 #endif // _WIN32
40
41 Config* g_configPointers[MAX_CONFIG];
42 static int g_cfgPointerCursor = 0;
43 static QMap<QString, Config*> g_configsByName;
44 static QList<Config*> g_configs;
45
46 // =============================================================================
47 // Get the QSettings object.
48 // =============================================================================
49 static QSettings* getSettingsObject()
50 {
51 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION;
52 return new QSettings (path, QSettings::IniFormat);
53 }
54
55 Config::Config (QString name) :
56 m_name (name) {}
57
58 // =============================================================================
59 // Load the configuration from file
60 // =============================================================================
61 bool Config::load()
62 {
63 QSettings* settings = getSettingsObject();
64 print ("config::load: Loading configuration file from %1\n", settings->fileName());
65
66 for (Config* cfg : g_configPointers)
67 {
68 if (!cfg)
69 break;
70
71 QVariant val = settings->value (cfg->name(), cfg->getDefaultAsVariant());
72 cfg->loadFromVariant (val);
73 g_configsByName[cfg->name()] = cfg;
74 g_configs << cfg;
75 }
76
77 settings->deleteLater();
78 return true;
79 }
80
81 // =============================================================================
82 //
83 // Save the configuration to disk
84 //
85 bool Config::save()
86 {
87 QSettings* settings = getSettingsObject();
88
89 for (Config* cfg : g_configs)
90 {
91 if (!cfg->isDefault())
92 settings->setValue (cfg->name(), cfg->toVariant());
93 else
94 settings->remove (cfg->name());
95 }
96
97 settings->sync();
98 print ("Configuration saved to %1.\n", settings->fileName());
99 settings->deleteLater();
100 return true;
101 }
102
103 // =============================================================================
104 // Reset configuration to defaults.
105 // =============================================================================
106 void Config::reset()
107 {
108 for (Config* cfg : g_configs)
109 cfg->resetValue();
110 }
111
112 // =============================================================================
113 // Where is the configuration file located at?
114 // =============================================================================
115 QString Config::filepath (QString file)
116 {
117 return Config::dirpath() + DIRSLASH + file;
118 }
119
120 // =============================================================================
121 // Directory of the configuration file.
122 // =============================================================================
123 QString Config::dirpath()
124 {
125 QSettings* cfg = getSettingsObject();
126 return dirname (cfg->fileName());
127 }
128
129 // =============================================================================
130 // We cannot just add config objects to a list or vector because that would rely
131 // on the vector's c-tor being called before the configs' c-tors. With global
132 // variables we cannot assume that, therefore we need to use a C-style array here.
133 // =============================================================================
134 void Config::addToArray (Config* ptr)
135 {
136 if (g_cfgPointerCursor == 0)
137 memset (g_configPointers, 0, sizeof g_configPointers);
138
139 assert (g_cfgPointerCursor < MAX_CONFIG);
140 g_configPointers[g_cfgPointerCursor++] = ptr;
141 }
142
143 // =============================================================================
144 // =============================================================================
145 template<class T> T* getConfigByName (QString name, Config::Type type)
146 {
147 auto it = g_configsByName.find (name);
148
149 if (it == g_configsByName.end())
150 return null;
151
152 Config* cfg = it.value();
153
154 if (cfg->getType() != type)
155 {
156 fprint (stderr, "type of %1 is %2, not %3\n", name, cfg->getType(), type);
157 abort();
158 }
159
160 return reinterpret_cast<T*> (cfg);
161 }
162
163 // =============================================================================
164 // =============================================================================
165 #undef IMPLEMENT_CONFIG
166
167 #define IMPLEMENT_CONFIG(NAME) \
168 NAME##Config* NAME##Config::getByName (QString name) \
169 { \
170 return getConfigByName<NAME##Config> (name, E##NAME##Type); \
171 }
172
173 IMPLEMENT_CONFIG (Int)
174 IMPLEMENT_CONFIG (String)
175 IMPLEMENT_CONFIG (Bool)
176 IMPLEMENT_CONFIG (Float)
177 IMPLEMENT_CONFIG (List)
178 IMPLEMENT_CONFIG (KeySequence)
179 IMPLEMENT_CONFIG (Vertex)

mercurial