src/Configuration.cc

changeset 629
b75c6cce02e2
child 632
63e6243b880c
equal deleted inserted replaced
628:6b13e4c2e97b 629:b75c6cce02e2
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.cpp: 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 log ("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->getName(), cfg->getDefaultAsVariant());
72 cfg->loadFromVariant (val);
73 g_configsByName[cfg->getName()] = cfg;
74 g_configs << cfg;
75 }
76
77 settings->deleteLater();
78 return true;
79 }
80
81 // =============================================================================
82 // Save the configuration to disk
83 // -----------------------------------------------------------------------------
84 bool Config::save()
85 {
86 QSettings* settings = getSettingsObject();
87 log ("Saving configuration to %1...\n", settings->fileName());
88
89 for (Config* cfg : g_configs)
90 {
91 if (!cfg->isDefault())
92 settings->setValue (cfg->getName(), cfg->toVariant());
93 else
94 settings->remove (cfg->getName());
95 }
96
97 settings->sync();
98 settings->deleteLater();
99 return true;
100 }
101
102 // =============================================================================
103 // Reset configuration to defaults.
104 // -----------------------------------------------------------------------------
105 void Config::reset()
106 {
107 for (Config* cfg : g_configs)
108 cfg->resetValue();
109 }
110
111 // =============================================================================
112 // Where is the configuration file located at?
113 // -----------------------------------------------------------------------------
114 QString Config::filepath (QString file)
115 {
116 return Config::dirpath() + DIRSLASH + file;
117 }
118
119 // =============================================================================
120 // Directory of the configuration file.
121 // -----------------------------------------------------------------------------
122 QString Config::dirpath()
123 {
124 QSettings* cfg = getSettingsObject();
125 return dirname (cfg->fileName());
126 }
127
128 // =============================================================================
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
131 // variables we cannot assume that, therefore we need to use a C-style array here.
132 // -----------------------------------------------------------------------------
133 void Config::addToArray (Config* ptr)
134 {
135 if (g_cfgPointerCursor == 0)
136 memset (g_configPointers, 0, sizeof g_configPointers);
137
138 assert (g_cfgPointerCursor < MAX_CONFIG);
139 g_configPointers[g_cfgPointerCursor++] = ptr;
140 }
141
142 // =============================================================================
143 // -----------------------------------------------------------------------------
144 template<class T> T* getConfigByName (QString name, Config::Type type)
145 {
146 auto it = g_configsByName.find (name);
147
148 if (it == g_configsByName.end())
149 return null;
150
151 Config* cfg = it.value();
152
153 if (cfg->getType() != type)
154 {
155 fprint (stderr, "type of %1 is %2, not %3\n", name, cfg->getType(), type);
156 abort();
157 }
158
159 return reinterpret_cast<T*> (cfg);
160 }
161
162 // =============================================================================
163 // -----------------------------------------------------------------------------
164 #undef IMPLEMENT_CONFIG
165
166 #define IMPLEMENT_CONFIG(NAME) \
167 NAME##Config* NAME##Config::getByName (QString name) \
168 { \
169 return getConfigByName<NAME##Config> (name, E##NAME##Type); \
170 }
171
172 IMPLEMENT_CONFIG (Int)
173 IMPLEMENT_CONFIG (String)
174 IMPLEMENT_CONFIG (Bool)
175 IMPLEMENT_CONFIG (Float)
176 IMPLEMENT_CONFIG (List)
177 IMPLEMENT_CONFIG (KeySequence)
178 IMPLEMENT_CONFIG (Vertex)

mercurial