| 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 "SettingsFileBase.h" |
| 19 | #include <QDir> |
| 20 | #include <QFileInfo> |
| 21 | #include <QFile> |
| 22 | #include <QSaveFile> |
| 23 | #include <QJsonParseError> |
| 24 | #include <QJsonDocument> |
| 25 | #include <QDebug> |
| 26 | #include <functional> |
| 27 | |
| 28 | SettingsFileBase::SettingsFileBase(QObject *parent) : |
| 29 | QObject(parent), m_rootObject(0) |
| 30 | { |
| 31 | m_rootObject = new SettingsObjectBase(this); |
| 32 | m_syncTimer.setInterval(0); |
| 33 | m_syncTimer.setSingleShot(true); |
| 34 | connect(&m_syncTimer, SIGNAL(timeout()), this, SLOT(sync())); |
| 35 | } |
| 36 | SettingsFileBase::~SettingsFileBase() |
| 37 | { |
| 38 | if (m_syncTimer.isActive()) { |
| 39 | sync(); |
| 40 | } |
| 41 | m_rootObject->deleteLater(); |
| 42 | } |
| 43 | |
| 44 | QString SettingsFileBase::path() const |
| 45 | { |
| 46 | return m_path; |
| 47 | } |
| 48 | |
| 49 | bool SettingsFileBase::setPath(const QString &path) |
| 50 | { |
| 51 | if (m_path == path) { |
| 52 | return false; |
| 53 | } |
| 54 | reset(); |
| 55 | m_path = path; |
| 56 | // Get the path and check it exists if not try and make it |
| 57 | QFileInfo info(m_path); |
| 58 | QDir dir(info.path()); |
| 59 | if (!dir.exists() && !dir.mkpath(QStringLiteral("."))) { |
| 60 | setErrorString(QStringLiteral("Cannot create directory: %1").arg(dir.path())); |
| 61 | return false; |
| 62 | } |
| 63 | checkDirPermissions(info.path()); |
| 64 | emit pathChanged(); |
| 65 | return readFile(); |
| 66 | } |
| 67 | |
| 68 | SettingsObjectBase *SettingsFileBase::object() |
| 69 | { |
| 70 | return m_rootObject; |
| 71 | } |
| 72 | |
| 73 | const SettingsObjectBase *SettingsFileBase::object() const |
| 74 | { |
| 75 | return m_rootObject; |
| 76 | } |
| 77 | |
| 78 | QString SettingsFileBase::errorString() const |
| 79 | { |
| 80 | return m_errorString; |
| 81 | } |
| 82 | |
| 83 | QJsonValue SettingsFileBase::read(const QJsonObject &obj, const QStringList &path) |
| 84 | { |
| 85 | QJsonValue val = obj; |
| 86 | Q_FOREACH(const QString &key, path) { |
| 87 | QJsonObject object = val.toObject(); |
| 88 | if (object.isEmpty() || (val = object.value(key)).isUndefined()) { |
| 89 | return QJsonValue::Undefined; |
| 90 | } |
| 91 | } |
| 92 | return val; |
| 93 | } |
| 94 | |
| 95 | typedef QList<QPair<QStringList, QJsonValue>> ChangedList; |
| 96 | static void findChangedRecursive(ChangedList &list, const QStringList &objPath, const QJsonValue &oldValue, const QJsonValue &newValue) { |
| 97 | if (oldValue.isObject() || newValue.isObject()) { |
| 98 | // If either is a non-object type, this returns an empty object |
| 99 | QJsonObject oldObject = oldValue.toObject(); |
| 100 | QJsonObject newObject = newValue.toObject(); |
| 101 | |
| 102 | // Iterate keys of the original object and compare to new |
| 103 | for (QJsonObject::iterator it = oldObject.begin(); it != oldObject.end(); it++) { |
| 104 | QJsonValue newSubValue = newObject.value(it.key()); |
| 105 | if (*it == newSubValue) |
| 106 | continue; |
| 107 | |
| 108 | if ((*it).isObject() || newSubValue.isObject()) |
| 109 | findChangedRecursive(list, QStringList() << objPath << it.key(), *it, newSubValue); |
| 110 | else |
| 111 | list.append(qMakePair(QStringList() << objPath << it.key(), newSubValue)); |
| 112 | } |
| 113 | |
| 114 | // Iterate keys of the new object that may not be in original |
| 115 | for (QJsonObject::iterator it = newObject.begin(); it != newObject.end(); it++) { |
| 116 | if (oldObject.contains(it.key())) |
| 117 | continue; |
| 118 | |
| 119 | if ((*it).isObject()) |
| 120 | findChangedRecursive(list, QStringList() << objPath << it.key(), QJsonValue::Undefined, it.value()); |
| 121 | else |
| 122 | list.append(qMakePair(QStringList() << objPath << it.key(), it.value())); |
| 123 | } |
| 124 | } else |
| 125 | list.append(qMakePair(objPath, newValue)); |
| 126 | } |
| 127 | |
| 128 | bool SettingsFileBase::write(const QStringList &path, const QJsonValue &value) |
| 129 | { |
| 130 | typedef QVarLengthArray<QPair<QString,QJsonObject> > ObjectArray; |
| 131 | ObjectArray stack; |
| 132 | QJsonValue current = m_jsonObject; |
| 133 | QJsonValue originalValue; |
| 134 | QString currentKey; |
| 135 | |
| 136 | foreach (const QString &key, path) { |
| 137 | const QJsonObject &parent = current.toObject(); |
| 138 | stack.append(qMakePair(currentKey, parent)); |
| 139 | current = parent.value(key); |
| 140 | currentKey = key; |
| 141 | } |
| 142 | |
| 143 | // ObjectArray now contains parent objects starting with the root, and current |
| 144 | // is the old value. Write back changes in reverse. |
| 145 | if (current == value) |
| 146 | return false; |
| 147 | originalValue = current; |
| 148 | current = value; |
| 149 | |
| 150 | ObjectArray::const_iterator it = stack.end(), begin = stack.begin(); |
| 151 | while (it != begin) { |
| 152 | --it; |
| 153 | QJsonObject update = it->second; |
| 154 | update.insert(currentKey, current); |
| 155 | current = update; |
| 156 | currentKey = it->first; |
| 157 | } |
| 158 | |
| 159 | // current is now the updated jsonRoot |
| 160 | m_jsonObject = current.toObject(); |
| 161 | m_syncTimer.start(); |
| 162 | |
| 163 | ChangedList changed; |
| 164 | findChangedRecursive(changed, path, originalValue, value); |
| 165 | |
| 166 | for (ChangedList::iterator it = changed.begin(); it != changed.end(); it++) |
| 167 | emit this->modified(it->first, it->second); |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | void SettingsFileBase::reset() |
| 173 | { |
| 174 | m_errorString.clear(); |
| 175 | m_path.clear(); |
| 176 | m_jsonObject = QJsonObject(); |
| 177 | emit modified(QStringList(), m_jsonObject); |
| 178 | } |
| 179 | |
| 180 | void SettingsFileBase::setErrorString(const QString &msg) |
| 181 | { |
| 182 | m_errorString = msg; |
| 183 | emit error(); |
| 184 | } |
| 185 | |
| 186 | bool SettingsFileBase::checkDirPermissions(const QString &path) |
| 187 | { |
| 188 | static QFile::Permissions desired = QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ExeUser; |
| 189 | static QFile::Permissions ignored = QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner; |
| 190 | |
| 191 | QFile file(path); |
| 192 | if ((file.permissions() & ~ignored) != desired) { |
| 193 | qDebug() << "Correcting permissions on configuration directory"; |
| 194 | if (!file.setPermissions(desired)) { |
| 195 | qWarning() << "Correcting permissions on configuration directory failed"; |
| 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | bool SettingsFileBase::readFile() |
| 204 | { |
| 205 | QFile config(m_path); |
| 206 | if (!config.open(QIODevice::ReadWrite)) { |
| 207 | setErrorString(config.errorString()); |
| 208 | return false; |
| 209 | } |
| 210 | QByteArray contents = config.readAll(); |
| 211 | if (contents.isEmpty()) { |
| 212 | // ok we havn't previously saved anything so let's just set |
| 213 | // a default jsonobject to return |
| 214 | m_jsonObject = QJsonObject(); |
| 215 | return true; |
| 216 | } |
| 217 | QJsonParseError err; |
| 218 | QJsonDocument doc = QJsonDocument::fromJson(contents); |
| 219 | if (doc.isNull()) { |
| 220 | setErrorString(err.errorString()); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // at the moment we only support a rootobject, in the future |
| 225 | // would be good to expand to support an array here aswell. |
| 226 | if (!doc.isObject()) { |
| 227 | setErrorString(QStringLiteral("Unsupported config file type. Expecting an object here.")); |
| 228 | return false; |
| 229 | } |
| 230 | m_jsonObject = doc.object(); |
| 231 | emit modified(QStringList(), m_jsonObject); |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool SettingsFileBase::writeFile() |
| 236 | { |
| 237 | QSaveFile config(m_path); |
| 238 | if (!config.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 239 | setErrorString(config.errorString()); |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | QJsonDocument document(m_jsonObject); |
| 244 | QByteArray data = document.toJson(); |
| 245 | if (data.isEmpty() && !document.isEmpty()) { |
| 246 | setErrorString(QStringLiteral("Encoding failure")); |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | if (config.write(data) < data.size() || !config.commit()) { |
| 251 | setErrorString(config.errorString()); |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | QStringList SettingsFileBase::splitPath(const QString &input, bool &ok) |
| 258 | { |
| 259 | QStringList objectList = input.split(QLatin1Char('.')); |
| 260 | // There should be no empty obj keys in here |
| 261 | Q_FOREACH(const QString &obj, objectList) { |
| 262 | if (obj.isEmpty()) { |
| 263 | ok = false; |
| 264 | return QStringList(); |
| 265 | } |
| 266 | } |
| 267 | ok = true; |
| 268 | return objectList; |
| 269 | } |
| 270 | |
| 271 | void SettingsFileBase::sync() |
| 272 | { |
| 273 | if (m_path.isEmpty()) |
| 274 | return; |
| 275 | m_syncTimer.stop(); |
| 276 | writeFile(); |
| 277 | } |