File indexing completed on 2025-01-19 03:55:38

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