/
Settings.cpp
253 lines (204 loc) · 6.7 KB
/
Settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "Settings.h"
#include <QDir>
#include <QStandardPaths>
#include "SimpleCrypt.h"
#include "Log.h"
#include "ToolBase.h"
#include "Exceptions.h"
#include "Helper.h"
bool Settings::settingsApplicationUserExists()
{
QStringList default_paths = QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);
if(default_paths.isEmpty()) return false;
QString filename = default_paths[0] + QDir::separator() + QCoreApplication::applicationName() + "_local.ini";
if (!QFile::exists(filename)) return false;
return true;
}
QSettings& Settings::settingsApplicationUser()
{
static QSharedPointer<QSettings> settings;
if(settings.isNull())
{
QStringList default_paths = QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);
if(default_paths.isEmpty()) THROW(Exception, "No local application data path was found!");
QString path = default_paths[0];
if (!QDir().mkpath(path)) THROW(Exception, "Could not create application data path '" + path + "'!");
//set log file
QString filename = path + QDir::separator() + QCoreApplication::applicationName() + "_local.ini";
settings.reset(new QSettings(filename, QSettings::IniFormat));
if (!settings->isWritable()) THROW(Exception, "Settings file '" + filename + "' is not writable!");
}
return *(settings.data());
}
const QSettings& Settings::settingsApplication()
{
static QSettings* settings = nullptr;
if(settings==nullptr)
{
QString filename = QCoreApplication::applicationDirPath() + QDir::separator() + QCoreApplication::applicationName() + ".ini";
settings = new QSettings(filename, QSettings::IniFormat);
}
return *settings;
}
const QSettings& Settings::settingsGeneral()
{
static QSettings* settings = nullptr;
if(settings==nullptr)
{
QString filename = QCoreApplication::applicationDirPath() + QDir::separator() + "settings.ini";
settings = new QSettings(filename, QSettings::IniFormat);
}
return *settings;
}
int Settings::integer(QString key)
{
return valueWithFallback(key).toInt();
}
void Settings::setInteger(QString key, int value)
{
QSettings& settings = settingsApplicationUser();
settings.setValue(key, value);
settings.sync(); //sync, so that the file is created, which is checked by settingsApplicationUserExists
}
QString Settings::string(QString key, bool optional)
{
if (optional && !contains(key)) return "";
QString value = valueWithFallback(key).toString();
//decrypt if encrypted
QString crypt_prefix = "encrypted:";
if (value.startsWith(crypt_prefix))
{
//remove prefix
value = value.mid(crypt_prefix.count()).trimmed();
//decrypt
qulonglong crypt_key = ToolBase::encryptionKey("setting entry '" + key + "'");
value = SimpleCrypt(crypt_key).decryptToString(value);
}
return value;
}
void Settings::setString(QString key, QString value)
{
QSettings& settings = settingsApplicationUser();
settings.setValue(key, value);
settings.sync(); //sync, so that the file is created, which is checked by settingsApplicationUserExists
}
QStringList Settings::stringList(QString key, bool optional)
{
if (optional && !contains(key)) return QStringList();
return valueWithFallback(key).toStringList();
}
void Settings::setStringList(QString key, QStringList value)
{
QSettings& settings = settingsApplicationUser();
settings.setValue(key, value);
settings.sync(); //sync, so that the file is created, which is checked by settingsApplicationUserExists
}
bool Settings::boolean(QString key, bool optional)
{
if (optional && !contains(key)) return false;
return valueWithFallback(key).toBool();
}
void Settings::setBoolean(QString key, bool value)
{
QSettings& settings = settingsApplicationUser();
settings.setValue(key, value);
settings.sync(); //sync, so that the file is created, which is checked by settingsApplicationUserExists
}
QMap<QString, QVariant> Settings::map(QString key, bool optional)
{
if (optional && !contains(key)) QMap<QString, QVariant>();
return valueWithFallback(key).toMap();
}
void Settings::setMap(QString key, QMap<QString, QVariant> value)
{
QSettings& settings = settingsApplicationUser();
settings.setValue(key, value);
settings.sync(); //sync, so that the file is created, which is checked by settingsApplicationUserExists
}
QString Settings::path(QString key, bool optional)
{
if (optional && !contains(key)) return "";
QString path = string(key).trimmed();
//convert separators
path = Helper::canonicalPath(path);
//add separator at the end if missing from path
if (QFile::exists(path) && QFileInfo(path).isDir() && !path.endsWith(QDir::separator()))
{
path += QDir::separator();
}
return path;
}
void Settings::setPath(QString key, QString path)
{
QFileInfo info(path);
if (info.isFile())
{
path = info.absolutePath();
}
if (QDir(path).exists())
{
setString(key, Helper::canonicalPath(path));
}
}
void Settings::clear()
{
settingsApplicationUser().clear();
}
void Settings::remove(QString key)
{
settingsApplicationUser().remove(key);
}
QStringList Settings::allKeys()
{
QStringList output;
if (settingsApplicationUserExists())
{
output << settingsApplicationUser().allKeys();
}
output << settingsApplication().allKeys();
output << settingsGeneral().allKeys();
std::sort(output.begin(), output.end());
output.removeDuplicates();
return output;
}
bool Settings::contains(QString key)
{
//check if key exists
bool key_exists = (settingsApplicationUserExists() && settingsApplicationUser().contains(key)) || settingsApplication().contains(key) || settingsGeneral().contains(key);
if (!key_exists) return false;
//if the key exists, check that the value is not empty
QVariant var = valueWithFallback(key);
if (var.type()==QVariant::StringList) return var.toStringList().join("").trimmed()!=""; //special handling for QStringList
if (var.type()==QVariant::Map) return var.toMap().keys().join("").trimmed()!=""; //special handling for QMap
return var.toString().trimmed()!="";
}
QStringList Settings::files()
{
QStringList output;
if (settingsApplicationUserExists())
{
QString filename = settingsApplicationUser().fileName();
if (QFile::exists(filename)) output << settingsApplicationUser().fileName();
}
QString filename = settingsApplication().fileName();
if (QFile::exists(filename)) output << filename;
filename = settingsGeneral().fileName();
if (QFile::exists(filename)) output << filename;
return output;
}
QVariant Settings::valueWithFallback(QString key)
{
if (settingsApplicationUserExists() && settingsApplicationUser().contains(key))
{
return settingsApplicationUser().value(key);
}
if (settingsApplication().contains(key))
{
return settingsApplication().value(key);
}
if (settingsGeneral().contains(key))
{
return settingsGeneral().value(key);
}
THROW(ProgrammingException, "Requested key '" + key + "' not found in settings!");
}