| 1 | /* |
| 2 | * This file is part of OnlineAccountsDaemon |
| 3 | * |
| 4 | * Copyright (C) 2015 Canonical Ltd. |
| 5 | * |
| 6 | * Contact: Alberto Mardegan <[email protected]> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License version 3, as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "state_saver.h" |
| 22 | |
| 23 | #include <QDebug> |
| 24 | #include <QDir> |
| 25 | #include <QFile> |
| 26 | #include <QJsonArray> |
| 27 | #include <QJsonDocument> |
| 28 | #include <QJsonObject> |
| 29 | #include <QJsonValue> |
| 30 | #include <QStandardPaths> |
| 31 | |
| 32 | using namespace OnlineAccountsDaemon; |
| 33 | |
| 34 | namespace OnlineAccountsDaemon { |
| 35 | |
| 36 | class StateSaverPrivate { |
| 37 | public: |
| 38 | StateSaverPrivate(); |
| 39 | ~StateSaverPrivate(); |
| 40 | |
| 41 | static QList<AccountInfo> accountsFromJson(const QJsonValue &value); |
| 42 | static QJsonValue accountsToJson(const QList<AccountInfo> &accounts); |
| 43 | |
| 44 | static QList<Client> clientsFromJson(const QJsonValue &value); |
| 45 | static QJsonValue clientsToJson(const QList<Client> &clients); |
| 46 | |
| 47 | void load(); |
| 48 | void save(); |
| 49 | |
| 50 | private: |
| 51 | friend class StateSaver; |
| 52 | QString m_cacheFile; |
| 53 | QList<Client> m_clients; |
| 54 | QList<AccountInfo> m_accounts; |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | StateSaverPrivate::StateSaverPrivate() |
| 60 | { |
| 61 | QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); |
| 62 | if (!cacheDir.exists()) { |
| 63 | cacheDir.mkpath("."); |
| 64 | } |
| 65 | m_cacheFile = cacheDir.filePath("client_account_refs.json"); |
| 66 | |
| 67 | load(); |
| 68 | } |
| 69 | |
| 70 | StateSaverPrivate::~StateSaverPrivate() |
| 71 | { |
| 72 | save(); |
| 73 | } |
| 74 | |
| 75 | QList<AccountInfo> |
| 76 | StateSaverPrivate::accountsFromJson(const QJsonValue &value) |
| 77 | { |
| 78 | QList<AccountInfo> accounts; |
| 79 | QJsonArray jsonAccounts = value.toArray(); |
| 80 | |
| 81 | Q_FOREACH(const QJsonValue &jsonAccount, jsonAccounts) { |
| 82 | QJsonObject jsonObject = jsonAccount.toObject(); |
| 83 | int accountId = jsonObject.value("accountId").toInt(); |
| 84 | QVariantMap details = |
| 85 | jsonObject.value("details").toObject().toVariantMap(); |
| 86 | accounts.append(AccountInfo(uint(accountId), details)); |
| 87 | } |
| 88 | return accounts; |
| 89 | } |
| 90 | |
| 91 | QJsonValue |
| 92 | StateSaverPrivate::accountsToJson(const QList<AccountInfo> &accounts) |
| 93 | { |
| 94 | Q_UNUSED(accounts); |
| 95 | return QJsonValue(); // TODO |
| 96 | } |
| 97 | |
| 98 | QList<Client> StateSaverPrivate::clientsFromJson(const QJsonValue &value) |
| 99 | { |
| 100 | QList<Client> clients; |
| 101 | Q_FOREACH(const QJsonValue &jsonClient, value.toArray()) { |
| 102 | QJsonObject jsonObject = jsonClient.toObject(); |
| 103 | QString busName = jsonObject.value("busName").toString(); |
| 104 | QString applicationId = jsonObject.value("applicationId").toString(); |
| 105 | clients.append(Client(busName, applicationId)); |
| 106 | } |
| 107 | return clients; |
| 108 | } |
| 109 | |
| 110 | QJsonValue StateSaverPrivate::clientsToJson(const QList<Client> &clients) |
| 111 | { |
| 112 | QJsonArray clientArray; |
| 113 | Q_FOREACH(const Client &client, clients) { |
| 114 | QJsonObject clientObject; |
| 115 | clientObject.insert("busName", client.first); |
| 116 | clientObject.insert("applicationId", client.second); |
| 117 | clientArray.append(clientObject); |
| 118 | } |
| 119 | return QJsonValue(clientArray); |
| 120 | } |
| 121 | |
| 122 | void StateSaverPrivate::save() |
| 123 | { |
| 124 | QFile file(m_cacheFile); |
| 125 | if (Q_UNLIKELY(!file.open(QIODevice::WriteOnly | QIODevice::Text))) { |
| 126 | qWarning() << "Couldn't save state to" << m_cacheFile; |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | QJsonObject jsonObject; |
| 131 | jsonObject.insert("accounts", accountsToJson(m_accounts)); |
| 132 | jsonObject.insert("clients", clientsToJson(m_clients)); |
| 133 | |
| 134 | QJsonDocument doc(jsonObject); |
| 135 | file.write(doc.toJson()); |
| 136 | } |
| 137 | |
| 138 | void StateSaverPrivate::load() |
| 139 | { |
| 140 | QFile file(m_cacheFile); |
| 141 | if (Q_UNLIKELY(!file.open(QIODevice::ReadOnly | QIODevice::Text))) { |
| 142 | qWarning() << "Cannot open file" << m_cacheFile; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | QByteArray contents = file.readAll(); |
| 147 | QJsonDocument doc = QJsonDocument::fromJson(contents); |
| 148 | if (doc.isEmpty() || !doc.isObject()) return; |
| 149 | |
| 150 | QJsonObject jsonObject = doc.object(); |
| 151 | m_accounts = accountsFromJson(jsonObject.value("accounts")); |
| 152 | m_clients = clientsFromJson(jsonObject.value("clients")); |
| 153 | } |
| 154 | |
| 155 | StateSaver::StateSaver(QObject *parent): |
| 156 | QObject(parent), |
| 157 | d_ptr(new StateSaverPrivate) |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | StateSaver::~StateSaver() |
| 162 | { |
| 163 | delete d_ptr; |
| 164 | } |
| 165 | |
| 166 | void StateSaver::setAccounts(const QList<AccountInfo> &accounts) |
| 167 | { |
| 168 | Q_D(StateSaver); |
| 169 | d->m_accounts = accounts; |
| 170 | } |
| 171 | |
| 172 | QList<AccountInfo> StateSaver::accounts() const |
| 173 | { |
| 174 | Q_D(const StateSaver); |
| 175 | return d->m_accounts; |
| 176 | } |
| 177 | |
| 178 | void StateSaver::setClients(const QList<Client> &clients) |
| 179 | { |
| 180 | Q_D(StateSaver); |
| 181 | d->m_clients = clients; |
| 182 | } |
| 183 | |
| 184 | QList<Client> StateSaver::clients() const |
| 185 | { |
| 186 | Q_D(const StateSaver); |
| 187 | return d->m_clients; |
| 188 | } |