|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 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 "config.h" |
|
31 #include "misc.h" |
|
32 #include "gui.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 |
|
44 // ============================================================================= |
|
45 // Get the QSettings object. A portable build refers to a file in the current |
|
46 // directory, a non-portable build to ~/.config/LDForge or to registry. |
|
47 // ----------------------------------------------------------------------------- |
|
48 static QSettings* getSettingsObject() |
|
49 { |
|
50 #ifdef PORTABLE |
|
51 return new QSettings (str (APPNAME).toLower() + EXTENSION, QSettings::IniFormat); |
|
52 #else |
|
53 return new QSettings; |
|
54 #endif // PORTABLE |
|
55 } |
|
56 |
|
57 Config::Config (const char* name, const char* defstring) : |
|
58 name (name), m_defstring (defstring) {} |
|
59 |
|
60 // ============================================================================= |
|
61 // Load the configuration from file |
|
62 // ----------------------------------------------------------------------------- |
|
63 bool Config::load() |
|
64 { QSettings* settings = getSettingsObject(); |
|
65 log ("config::load: Loading configuration file from %1\n", settings->fileName()); |
|
66 |
|
67 for (Config* cfg : g_configPointers) |
|
68 { if (!cfg) |
|
69 break; |
|
70 |
|
71 QVariant val = settings->value (cfg->name, cfg->defaultVariant()); |
|
72 cfg->loadFromVariant (val); |
|
73 } |
|
74 |
|
75 settings->deleteLater(); |
|
76 return true; |
|
77 } |
|
78 |
|
79 // ============================================================================= |
|
80 // Save the configuration to disk |
|
81 // ----------------------------------------------------------------------------- |
|
82 bool Config::save() |
|
83 { QSettings* settings = getSettingsObject(); |
|
84 log ("Saving configuration to %1...\n", settings->fileName()); |
|
85 |
|
86 for (Config* cfg : g_configPointers) |
|
87 { if (!cfg) |
|
88 break; |
|
89 |
|
90 if (cfg->isDefault()) |
|
91 continue; |
|
92 |
|
93 settings->setValue (cfg->name, cfg->toVariant()); |
|
94 } |
|
95 |
|
96 settings->sync(); |
|
97 settings->deleteLater(); |
|
98 return true; |
|
99 } |
|
100 |
|
101 // ============================================================================= |
|
102 // Reset configuration defaults. |
|
103 // ----------------------------------------------------------------------------- |
|
104 void Config::reset() |
|
105 { for (Config * cfg : g_configPointers) |
|
106 { if (!cfg) |
|
107 break; |
|
108 |
|
109 cfg->resetValue(); |
|
110 } |
|
111 } |
|
112 |
|
113 // ============================================================================= |
|
114 // Where is the configuration file located at? Note that the Windows build uses |
|
115 // the registry so only use this with PORTABLE code. |
|
116 // ----------------------------------------------------------------------------- |
|
117 str Config::filepath (str file) |
|
118 { return Config::dirpath() + DIRSLASH + file; |
|
119 } |
|
120 |
|
121 // ============================================================================= |
|
122 // Directory of the configuration file. PORTABLE code here as well. |
|
123 // ----------------------------------------------------------------------------- |
|
124 str Config::dirpath() |
|
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 { 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 } |