| 1 | /* Copyright (C) 2014-2016 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 "SettingsObjectBase.h" |
| 19 | #include <QWeakPointer> |
| 20 | #include <QSharedPointer> |
| 21 | #include <QMap> |
| 22 | #include <Paths.h> |
| 23 | #include <QDebug> |
| 24 | |
| 25 | typedef QMap<QString, QWeakPointer<SettingsFileBase> > SettingsObjectMap; |
| 26 | static SettingsObjectMap objectMap; |
| 27 | |
| 28 | SettingsObjectBase::SettingsObjectBase(QObject *parent) : |
| 29 | QObject(parent), m_file(0), m_invalid(true) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void SettingsObjectBase::setSettingsKey(const QString &key) |
| 34 | { |
| 35 | m_settingsKey = key; |
| 36 | const QString lockKey = QString("%1.json.lock").arg(m_settingsKey); |
| 37 | const QString fileKey = QString("%1.json").arg(m_settingsKey); |
| 38 | if (!objectMap.contains(m_settingsKey)) { |
| 39 | QString error; |
| 40 | QLockFile *lock = 0; |
| 41 | QString settingsLockPath = Paths::configLocationForFile(lockKey); |
| 42 | if (Paths::checkForStaleLockFile(&lock, settingsLockPath, error)) { |
| 43 | qCritical() << error; |
| 44 | qApp->quit(); |
| 45 | } |
| 46 | QSharedPointer<SettingsFileBase> file = QSharedPointer<SettingsFileBase>(new SettingsFileBase); |
| 47 | objectMap.insert(m_settingsKey, file); |
| 48 | setFile(file); |
| 49 | m_lock = new QScopedPointer<QLockFile>(lock); |
| 50 | } else { |
| 51 | QSharedPointer<SettingsFileBase> file = objectMap.value(m_settingsKey).toStrongRef(); |
| 52 | if (file.isNull()) { |
| 53 | qCritical() << "Failed to get strong ref for settings file object"; |
| 54 | qApp->quit(); |
| 55 | } |
| 56 | setFile(file); |
| 57 | } |
| 58 | if (!m_file.isNull()) { |
| 59 | m_file->setPath(Paths::configLocationForFile(fileKey)); |
| 60 | setPath(QStringLiteral("default")); |
| 61 | createDefaultsIfNotExist(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | QJsonObject SettingsObjectBase::data() const |
| 66 | { |
| 67 | return m_object; |
| 68 | } |
| 69 | |
| 70 | void SettingsObjectBase::setData(const QJsonObject &data) |
| 71 | { |
| 72 | if (m_invalid || m_object == data) { |
| 73 | return; |
| 74 | } |
| 75 | m_object = data; |
| 76 | m_file->write(m_path, m_object); |
| 77 | } |
| 78 | |
| 79 | QString SettingsObjectBase::path() const |
| 80 | { |
| 81 | return m_path.join(QLatin1Char('.')); |
| 82 | } |
| 83 | |
| 84 | void SettingsObjectBase::setPath(const QString &path) |
| 85 | { |
| 86 | bool ok = false; |
| 87 | // Split this new path |
| 88 | QStringList p = SettingsFileBase::splitPath(path, ok); |
| 89 | if (!ok) { |
| 90 | qDebug() << "Failed splitting path"; |
| 91 | // set invalid and return and empty object |
| 92 | m_invalid = true; |
| 93 | m_path.clear(); |
| 94 | m_object = QJsonObject(); |
| 95 | emit dataChanged(); |
| 96 | emit pathChanged(); |
| 97 | return; |
| 98 | } |
| 99 | if (m_path == p && m_invalid) { |
| 100 | return; |
| 101 | } |
| 102 | m_path = p; |
| 103 | if (m_file) { |
| 104 | m_invalid = false; |
| 105 | m_object = m_file->read(m_file->m_jsonObject, m_path).toObject(); |
| 106 | emit dataChanged(); |
| 107 | } |
| 108 | emit pathChanged(); |
| 109 | } |
| 110 | |
| 111 | bool SettingsObjectBase::isValid() const |
| 112 | { |
| 113 | return !m_invalid; |
| 114 | } |
| 115 | |
| 116 | QJsonValue SettingsObjectBase::read(const QString &key, const QJsonValue &defaultValue) const |
| 117 | { |
| 118 | bool ok = false; |
| 119 | QStringList splitKey = SettingsFileBase::splitPath(key, ok); |
| 120 | if (m_invalid || !ok || splitKey.isEmpty()) { |
| 121 | qDebug() << "Invalid settings for path: " << key; |
| 122 | return defaultValue; |
| 123 | } |
| 124 | |
| 125 | QJsonValue ret = m_file->read(m_object, splitKey); |
| 126 | if (ret.isUndefined()) |
| 127 | ret = defaultValue; |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | void SettingsObjectBase::write(const QString &key, const QJsonValue &value) |
| 132 | { |
| 133 | bool ok = false; |
| 134 | QStringList splitKey = SettingsFileBase::splitPath(key, ok); |
| 135 | if (m_invalid || !ok || splitKey.isEmpty()) { |
| 136 | qDebug() << "Invalid settings, can't write to path: " << key; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | splitKey = m_path + splitKey; |
| 141 | m_file->write(splitKey, value); |
| 142 | } |
| 143 | |
| 144 | void SettingsObjectBase::undefine() |
| 145 | { |
| 146 | if (m_invalid) |
| 147 | return; |
| 148 | |
| 149 | m_object = QJsonObject(); |
| 150 | m_file->write(m_path, QJsonValue::Undefined); |
| 151 | } |
| 152 | |
| 153 | void SettingsObjectBase::modified(const QStringList &path, const QJsonValue &value) |
| 154 | { |
| 155 | if (path.size() < m_path.size()) { |
| 156 | return; |
| 157 | } |
| 158 | // Check the paths actually match |
| 159 | for (int var = 0; var < m_path.size(); ++var) { |
| 160 | if (m_path[var] != path[var]) { |
| 161 | return; |
| 162 | } |
| 163 | } |
| 164 | m_object = m_file->read(m_file->m_jsonObject, m_path).toObject(); |
| 165 | emit modified(QStringList(path.mid(m_path.size())).join(QLatin1Char('.')), value); |
| 166 | emit dataChanged(); |
| 167 | } |
| 168 | |
| 169 | void SettingsObjectBase::createDefaultsIfNotExist() |
| 170 | { |
| 171 | qDebug() << __func__ << "Not implemented here"; |
| 172 | } |
| 173 | |
| 174 | void SettingsObjectBase::setFile(QSharedPointer<SettingsFileBase> file) |
| 175 | { |
| 176 | if (m_file == file) { |
| 177 | return; |
| 178 | } |
| 179 | if (!m_file.isNull()) { |
| 180 | disconnect(m_file.data(), 0, this, 0); |
| 181 | } |
| 182 | m_file = file; |
| 183 | if (!m_file.isNull()) { |
| 184 | connect(m_file.data(), SIGNAL(modified(QStringList,QJsonValue)), this, SLOT(modified(QStringList,QJsonValue))); |
| 185 | } |
| 186 | } |