File indexing completed on 2024-12-22 04:57:52

0001 /*
0002     SPDX-License-Identifier: BSD-2-Clause
0003 */
0004 
0005 #include <QCryptographicHash>
0006 
0007 #include "o2/o0settingsstore.h"
0008 
0009 static quint64 getHash(const QString &encryptionKey)
0010 {
0011     return QCryptographicHash::hash(encryptionKey.toLatin1(), QCryptographicHash::Sha1).toULongLong();
0012 }
0013 
0014 O0SettingsStore::O0SettingsStore(const QString &encryptionKey, QObject *parent)
0015     : O0AbstractStore(parent)
0016     , crypt_(getHash(encryptionKey))
0017 {
0018     settings_ = new QSettings(this);
0019 }
0020 
0021 O0SettingsStore::O0SettingsStore(QSettings *settings, const QString &encryptionKey, QObject *parent)
0022     : O0AbstractStore(parent)
0023     , crypt_(getHash(encryptionKey))
0024 {
0025     settings_ = settings;
0026     settings_->setParent(this);
0027 }
0028 
0029 QString O0SettingsStore::groupKey() const
0030 {
0031     return groupKey_;
0032 }
0033 
0034 void O0SettingsStore::setGroupKey(const QString &groupKey)
0035 {
0036     if (groupKey_ == groupKey) {
0037         return;
0038     }
0039     groupKey_ = groupKey;
0040     Q_EMIT groupKeyChanged();
0041 }
0042 
0043 QString O0SettingsStore::value(const QString &key, const QString &defaultValue)
0044 {
0045     QString fullKey = groupKey_.isEmpty() ? key : (groupKey_ + QLatin1Char('/') + key);
0046     if (!settings_->contains(fullKey)) {
0047         return defaultValue;
0048     }
0049     return crypt_.decryptToString(settings_->value(fullKey).toString());
0050 }
0051 
0052 void O0SettingsStore::setValue(const QString &key, const QString &value)
0053 {
0054     QString fullKey = groupKey_.isEmpty() ? key : (groupKey_ + QLatin1Char('/') + key);
0055     settings_->setValue(fullKey, crypt_.encryptToString(value));
0056 }
0057 
0058 #include "moc_o0settingsstore.cpp"