src/config.cpp

Sat, 03 Aug 2013 00:56:02 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 03 Aug 2013 00:56:02 +0300
changeset 430
8458cf2719d1
parent 421
7d26db0be944
child 436
4268a5507725
permissions
-rw-r--r--

added config option, refined logic and regexps. This behaves coherently now. :)

183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 Santeri Piippo
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
4 *
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
9 *
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
14 *
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <errno.h>
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <time.h>
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include <QDir>
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
22 #include <QTextStream>
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "common.h"
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 #include "config.h"
198
f246725199dc Split some stuff into separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents: 189
diff changeset
25 #include "misc.h"
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
26 #include "gui.h"
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
28 config* g_configPointers[MAX_CONFIG];
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
29 static ushort g_cfgPointerCursor = 0;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 // Load the configuration from file
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
33 bool config::load() {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
34 print ("config::load: Loading configuration file...\n");
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
35 print ("config::load: Path to configuration is %1\n", filepath());
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
36
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 // Locale must be disabled for atof
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
38 setlocale (LC_NUMERIC, "C");
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
39
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
40 File f (filepath(), File::Read);
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
41 int ln = 0;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
42
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
43 if (!f)
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
44 return false;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
45
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 // Read the values.
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
47 for (str line : f) {
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 ln++;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
49
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
50 if (line.isEmpty() || line[0] == '#')
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 continue; // Empty line or comment.
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
52
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 // Find the equals sign.
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
54 int equals = line.indexOf ('=');
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
55
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
56 if (equals == -1) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
57 fprint (stderr, "couldn't find `=` sign in entry `%1`\n", line);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 continue;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
61 str entry = line.left (equals);
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
62 str valstring = line.right (line.length() - equals - 1);
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
63
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 // Find the config entry for this.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 config* cfg = null;
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
66
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
67 for (config* i : g_configPointers) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
68 if (!i)
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
69 break;
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
70
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
71 if (entry == i->name)
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 cfg = i;
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
73 }
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
75 if (!cfg) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
76 fprint (stderr, "unknown config `%1`\n", entry);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 continue;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
80 switch (cfg->getType()) {
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
81 case Type_int:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
82 static_cast<intconfig*> (cfg)->value = valstring.toInt();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
85 case Type_str:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
86 static_cast<strconfig*> (cfg)->value = valstring;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
89 case Type_float:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
90 static_cast<floatconfig*> (cfg)->value = valstring.toFloat();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
93 case Type_bool: {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
94 bool& val = static_cast<boolconfig*> (cfg)->value;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
96 if (valstring.toUpper() == "TRUE" || valstring == "1")
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 val = true;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
98 elif (valstring.toUpper() == "FALSE" || valstring == "0")
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 val = false;
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
100
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
104 case Type_keyseq:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
105 static_cast<keyseqconfig*> (cfg)->value = keyseq::fromString (valstring);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 default:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
113 f.close();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 return true;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
117 extern_cfg (str, io_ldpath);
300
0c7e00cc3d44 Added support for launching external programs with Wine
Santeri Piippo <crimsondusk64@gmail.com>
parents: 299
diff changeset
118
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 // Save the configuration to disk
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
121 bool config::save() {
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 // The function will write floats, disable the locale now so that they
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 // are written properly.
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
124 setlocale (LC_NUMERIC, "C");
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 // If the directory doesn't exist, create it now.
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
127 if (QDir (dirpath()).exists() == false) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
128 fprint (stderr, "Creating config path %1...\n", dirpath());
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
129
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
130 if (!QDir().mkpath (dirpath())) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
131 critical ("Failed to create the configuration directory. Configuration cannot be saved!\n");
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
132 return false;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
136 File f (filepath(), File::Write);
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
137 print ("writing cfg to %1\n", filepath());
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
139 if (!f) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
140 critical (fmt (QObject::tr ("Cannot save configuration, cannot open %1 for writing: %2\n"),
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
141 filepath(), strerror (errno)));
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 return false;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
145 fprint (f, "# Configuration file for " APPNAME "\n");
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
146 str valstring;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
148 for (config* cfg : g_configPointers) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
149 if (!cfg)
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
150 break;
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
151
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
152 if (cfg->isDefault())
300
0c7e00cc3d44 Added support for launching external programs with Wine
Santeri Piippo <crimsondusk64@gmail.com>
parents: 299
diff changeset
153 continue;
0c7e00cc3d44 Added support for launching external programs with Wine
Santeri Piippo <crimsondusk64@gmail.com>
parents: 299
diff changeset
154
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
155 switch (cfg->getType()) {
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
156 case Type_int:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
157 valstring = fmt ("%1", static_cast<intconfig*> (cfg)->value);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
159
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
160 case Type_str:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
161 valstring = static_cast<strconfig*> (cfg)->value;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
163
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
164 case Type_float:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
165 valstring = fmt ("%1", static_cast<floatconfig*> (cfg)->value);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
167
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
168 case Type_bool:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
169 valstring = (static_cast<boolconfig*> (cfg)->value) ? "true" : "false";
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
171
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
172 case Type_keyseq:
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
173 valstring = static_cast<keyseqconfig*> (cfg)->value.toString();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
175
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 default:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 // Write the entry now.
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
181 fprint (f, "%1=%2\n", cfg->name, valstring);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
184 f.close();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 return true;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 // =============================================================================
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
189 void config::reset() {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
190 for (config* cfg : g_configPointers) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
191 if (!cfg)
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
192 break;
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
193
359
bcdfc801658b more restyle/refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
194 cfg->resetValue();
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
195 }
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 // =============================================================================
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
199 str config::filepath() {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
200 str path = fmt ("%1%2.cfg", dirpath(), str (APPNAME).toLower());
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 return path;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 // =============================================================================
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
205 str config::dirpath() {
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
206 #ifndef _WIN32
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
207 return fmt ("%1" DIRSLASH ".%2" DIRSLASH,
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
208 QDir::homePath(), str (APPNAME).toLower());
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
209 #else
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
210 return fmt ("%1" DIRSLASH APPNAME DIRSLASH, QDir::homePath());
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
211 #endif // _WIN32
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
212 }
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
213
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
214 void addConfig (config* ptr) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
215 if (g_cfgPointerCursor == 0)
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
216 memset (g_configPointers, 0, sizeof g_configPointers);
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
217
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
218 assert (g_cfgPointerCursor < MAX_CONFIG);
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
219 g_configPointers[g_cfgPointerCursor++] = ptr;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 359
diff changeset
220 }

mercurial