src-common/config.cpp

changeset 46
07578e081ae8
parent 45
f5b526a3423a
child 47
4a0ad0a08ea1
equal deleted inserted replaced
45:f5b526a3423a 46:07578e081ae8
1 /*
2 * ZCinema: Zandronum demo launcher
3 * Copyright (C) 2013-2015 Teemu 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 #include <assert.h>
20 #include <QDir>
21 #include <QTextStream>
22 #include <QSettings>
23 #include "config.h"
24
25 typedef QMap<QString, QVariant> DefaultsMap;
26
27 //
28 // -------------------------------------------------------------------------------------------------
29 //
30
31 static QSettings* getSettingsObject()
32 {
33 return new QSettings;
34 }
35
36 //
37 // -------------------------------------------------------------------------------------------------
38 //
39
40 static DefaultsMap& getDefaults()
41 {
42 static DefaultsMap defaults;
43
44 if (defaults.isEmpty())
45 {
46 // Initialize defaults here.
47 }
48
49 return defaults;
50 }
51
52 //
53 // -------------------------------------------------------------------------------------------------
54 //
55
56 void Config::reset()
57 {
58 DefaultsMap& defaults = getDefaults();
59
60 for (DefaultsMap::iterator it = defaults.begin(); it != defaults.end(); ++it)
61 set (it.key(), it.value());
62 }
63
64 //
65 // -------------------------------------------------------------------------------------------------
66 //
67
68 QVariant Config::get (const QString& name)
69 {
70 QSettings* settings = getSettingsObject();
71 DefaultsMap& defaults = getDefaults();
72 DefaultsMap::iterator it = defaults.find (name);
73 QVariant def = it != defaults.end() ? *it : QVariant();
74 QVariant value = settings->value (name, def);
75 settings->deleteLater();
76 return value;
77 }
78
79 //
80 // -------------------------------------------------------------------------------------------------
81 //
82
83 bool Config::set (const QString& name, const QVariant& value)
84 {
85 QSettings* settings = getSettingsObject();
86 settings->setValue (name, value);
87 settings->deleteLater();
88 return settings->status() == QSettings::NoError;
89 }
90
91 //
92 // -------------------------------------------------------------------------------------------------
93 //
94
95 void Config::sync()
96 {
97 QSettings* settings = getSettingsObject();
98 settings->sync();
99 settings->deleteLater();
100 }

mercurial