| 1 | /* Copyright (C) 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 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 | #include "MazDBSettings.h" |
| 19 | #include <SnapStandardPaths.h> |
| 20 | #include <QCoreApplication> |
| 21 | #include "MazDB.h" |
| 22 | |
| 23 | MazDBSettings::MazDBSettings(QObject *parent) : QObject(parent), |
| 24 | m_db(Q_NULLPTR) |
| 25 | { |
| 26 | m_db = new MazDB(this); |
| 27 | connect(m_db, &MazDB::keyValueChanged, this, &MazDBSettings::handleKeyValueChanged); |
| 28 | connect(m_db, &MazDB::filenameChanged, this, &MazDBSettings::init); |
| 29 | // connect(m_db, &MazDB::keyValueRemoved, this, &MazDBSettings::handleKeyValueRemoved); |
| 30 | } |
| 31 | |
| 32 | QStringList MazDBSettings::allKeys() |
| 33 | { |
| 34 | QStringList keys; |
| 35 | m_db->readStream([&keys](QString key, QVariant value) { |
| 36 | Q_UNUSED(value); |
| 37 | keys << key; |
| 38 | return true; |
| 39 | }); |
| 40 | return keys; |
| 41 | } |
| 42 | |
| 43 | bool MazDBSettings::contains(const QString &key) |
| 44 | { |
| 45 | return allKeys().contains(key); |
| 46 | } |
| 47 | |
| 48 | QString MazDBSettings::fileName() const |
| 49 | { |
| 50 | return m_db->filename(); |
| 51 | } |
| 52 | |
| 53 | void MazDBSettings::remove(const QString &key) |
| 54 | { |
| 55 | m_db->del(key); |
| 56 | } |
| 57 | |
| 58 | void MazDBSettings::setValue(const QString &key, const QVariant &value) |
| 59 | { |
| 60 | if (key.isEmpty()) { |
| 61 | return; |
| 62 | } |
| 63 | m_db->put(key, value); |
| 64 | } |
| 65 | |
| 66 | QVariant MazDBSettings::value(const QString &key, const QVariant &defaultValue) |
| 67 | { |
| 68 | return m_db->get(key, defaultValue); |
| 69 | } |
| 70 | |
| 71 | void MazDBSettings::close() |
| 72 | { |
| 73 | m_db->close(); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void MazDBSettings::classBegin() |
| 78 | { |
| 79 | |
| 80 | } |
| 81 | |
| 82 | void MazDBSettings::componentComplete() |
| 83 | { |
| 84 | QString path; |
| 85 | if (m_category.isEmpty()) { |
| 86 | path = SnapStandardPaths::writableLocation(SnapStandardPaths::AppConfigLocation) + QStringLiteral("/mazdb/settings.db"); |
| 87 | } else { |
| 88 | path = SnapStandardPaths::writableLocation(SnapStandardPaths::AppConfigLocation) + QStringLiteral("/mazdb/categories/%1.db").arg(m_category); |
| 89 | } |
| 90 | m_db->setFilename(path); |
| 91 | init(); |
| 92 | } |
| 93 | |
| 94 | void MazDBSettings::handlePropertyChanged() |
| 95 | { |
| 96 | if (!m_db->opened()) |
| 97 | return; |
| 98 | QMetaProperty property = m_propertyHash[senderSignalIndex()]; |
| 99 | QString propertyName = QString::fromLocal8Bit(property.name()); |
| 100 | QVariant value = property.read(this); |
| 101 | m_db->put(propertyName, value); |
| 102 | } |
| 103 | |
| 104 | void MazDBSettings::handleKeyValueChanged(const QString &key, const QVariant &value) |
| 105 | { |
| 106 | const QMetaObject *ob = metaObject(); |
| 107 | int i = ob->indexOfProperty(key.toLocal8Bit().constData()); |
| 108 | QMetaProperty prop = ob->property(i); |
| 109 | prop.write(this, value); |
| 110 | } |
| 111 | |
| 112 | void MazDBSettings::init() |
| 113 | { |
| 114 | if (!m_connections.isEmpty()) { |
| 115 | for(auto c : m_connections) { |
| 116 | QObject::disconnect(c); |
| 117 | } |
| 118 | m_connections.clear(); |
| 119 | m_propertyHash.clear(); |
| 120 | } |
| 121 | const QMetaObject *m = metaObject(); |
| 122 | for (int i = m->propertyOffset(); i < m->propertyCount(); ++i) { |
| 123 | auto metaProp = m->property(i); |
| 124 | if (!metaProp.isWritable()) { |
| 125 | continue; |
| 126 | } |
| 127 | const QVariant storedValue = m_db->get(QString::fromLocal8Bit(metaProp.name())); |
| 128 | const QVariant oldPropValue = metaProp.read(this); |
| 129 | if (storedValue.isValid() && storedValue != oldPropValue) { |
| 130 | metaProp.write(this, storedValue); |
| 131 | } else if (oldPropValue.isValid()) { |
| 132 | m_db->put(QString::fromLocal8Bit(metaProp.name()), oldPropValue); |
| 133 | } |
| 134 | if (metaProp.hasNotifySignal()) { |
| 135 | m_propertyHash.insert(metaProp.notifySignalIndex(), metaProp); |
| 136 | m_connections.append(QMetaObject::connect(this, metaProp.notifySignalIndex(), this, m->indexOfSlot("handlePropertyChanged()"))); |
| 137 | } |
| 138 | } |
| 139 | } |