forked from pipacs/o2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho0settingsstore.cpp
44 lines (36 loc) · 1.39 KB
/
o0settingsstore.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
#include <QCryptographicHash>
#include <QByteArray>
#include "o0settingsstore.h"
static quint64 getHash(const QString &encryptionKey) {
return QCryptographicHash::hash(encryptionKey.toLatin1(), QCryptographicHash::Sha1).toULongLong();
}
O0SettingsStore::O0SettingsStore(const QString &encryptionKey, QObject *parent):
O0AbstractStore(parent), crypt_(getHash(encryptionKey)) {
settings_ = new QSettings(this);
}
O0SettingsStore::O0SettingsStore(QSettings *settings, const QString &encryptionKey, QObject *parent):
O0AbstractStore(parent), crypt_(getHash(encryptionKey)) {
settings_ = settings;
settings_->setParent(this);
}
QString O0SettingsStore::groupKey() const {
return groupKey_;
}
void O0SettingsStore::setGroupKey(const QString &groupKey) {
if (groupKey_ == groupKey) {
return;
}
groupKey_ = groupKey;
Q_EMIT groupKeyChanged();
}
QString O0SettingsStore::value(const QString &key, const QString &defaultValue) {
QString fullKey = groupKey_.isEmpty() ? key : (groupKey_ + '/' + key);
if (!settings_->contains(fullKey)) {
return defaultValue;
}
return crypt_.decryptToString(settings_->value(fullKey).toString());
}
void O0SettingsStore::setValue(const QString &key, const QString &value) {
QString fullKey = groupKey_.isEmpty() ? key : (groupKey_ + '/' + key);
settings_->setValue(fullKey, crypt_.encryptToString(value));
}