|
| 1 | +/* |
| 2 | + * Bittorrent Client using Qt and libtorrent. |
| 3 | + * Copyright (C) 2025 Thomas Piccirello <[email protected]> |
| 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 |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 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, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + * |
| 19 | + * In addition, as a special exception, the copyright holders give permission to |
| 20 | + * link this program with the OpenSSL project's "OpenSSL" library (or with |
| 21 | + * modified versions of it that use the same license as the "OpenSSL" library), |
| 22 | + * and distribute the linked executables. You must obey the GNU General Public |
| 23 | + * License in all respects for all of the code used other than "OpenSSL". If you |
| 24 | + * modify file(s), you may extend this exception to your version of the file(s), |
| 25 | + * but you are not obligated to do so. If you do not wish to do so, delete this |
| 26 | + * exception statement from your version. |
| 27 | + */ |
| 28 | + |
| 29 | +#include "clientdatastorage.h" |
| 30 | + |
| 31 | +#include <QJsonDocument> |
| 32 | +#include <QJsonObject> |
| 33 | + |
| 34 | +#include "base/global.h" |
| 35 | +#include "base/logger.h" |
| 36 | +#include "base/path.h" |
| 37 | +#include "base/profile.h" |
| 38 | +#include "base/utils/io.h" |
| 39 | + |
| 40 | +const int CLIENT_DATA_FILE_MAX_SIZE = 1024 * 1024; // 1 MiB |
| 41 | +const QString CLIENT_DATA_FILE_NAME = u"web_clientdata.json"_s; |
| 42 | + |
| 43 | +ClientDataStorage::ClientDataStorage(QObject *parent) |
| 44 | + : QObject(parent) |
| 45 | + , m_clientDataFilePath(specialFolderLocation(SpecialFolder::Data) / Path(CLIENT_DATA_FILE_NAME)) |
| 46 | +{ |
| 47 | + if (!m_clientDataFilePath.exists()) |
| 48 | + return; |
| 49 | + |
| 50 | + const auto readResult = Utils::IO::readFile(m_clientDataFilePath, CLIENT_DATA_FILE_MAX_SIZE); |
| 51 | + if (!readResult) |
| 52 | + { |
| 53 | + LogMsg(tr("Failed to load web client data. %1").arg(readResult.error().message), Log::WARNING); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + QJsonParseError jsonError; |
| 58 | + const QJsonDocument jsonDoc = QJsonDocument::fromJson(readResult.value(), &jsonError); |
| 59 | + if (jsonError.error != QJsonParseError::NoError) |
| 60 | + { |
| 61 | + LogMsg(tr("Failed to parse web client data. File: \"%1\". Error: \"%2\"") |
| 62 | + .arg(m_clientDataFilePath.toString(), jsonError.errorString()), Log::WARNING); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (!jsonDoc.isObject()) |
| 67 | + { |
| 68 | + LogMsg(tr("Failed to load web client data. File: \"%1\". Error: \"Invalid data format\"") |
| 69 | + .arg(m_clientDataFilePath.toString()), Log::WARNING); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + m_clientData = jsonDoc.object(); |
| 74 | +} |
| 75 | + |
| 76 | +nonstd::expected<void, QString> ClientDataStorage::storeData(const QJsonObject &object) |
| 77 | +{ |
| 78 | + QJsonObject clientData = m_clientData; |
| 79 | + bool dataModified = false; |
| 80 | + for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) |
| 81 | + { |
| 82 | + const QString &key = it.key(); |
| 83 | + const QJsonValue &value = it.value(); |
| 84 | + |
| 85 | + if (value.isNull()) |
| 86 | + { |
| 87 | + if (auto it = clientData.find(key); it != clientData.end()) |
| 88 | + { |
| 89 | + clientData.erase(it); |
| 90 | + dataModified = true; |
| 91 | + } |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + const auto &existingValue = clientData.find(key); |
| 96 | + if (existingValue == clientData.end()) |
| 97 | + { |
| 98 | + clientData.insert(key, value); |
| 99 | + dataModified = true; |
| 100 | + } |
| 101 | + else if (existingValue.value() != value) |
| 102 | + { |
| 103 | + existingValue.value() = value; |
| 104 | + dataModified = true; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (dataModified) |
| 110 | + { |
| 111 | + const QByteArray json = QJsonDocument(clientData).toJson(QJsonDocument::Compact); |
| 112 | + if (json.size() > CLIENT_DATA_FILE_MAX_SIZE) |
| 113 | + return nonstd::make_unexpected(tr("Total web client data must not be larger than %1 bytes").arg(CLIENT_DATA_FILE_MAX_SIZE)); |
| 114 | + const nonstd::expected<void, QString> result = Utils::IO::saveToFile(m_clientDataFilePath, json); |
| 115 | + if (!result) |
| 116 | + return nonstd::make_unexpected(tr("Failed to save web client data. Error: \"%1\"").arg(result.error())); |
| 117 | + |
| 118 | + m_clientData = clientData; |
| 119 | + } |
| 120 | + |
| 121 | + return {}; |
| 122 | +} |
| 123 | + |
| 124 | +QJsonObject ClientDataStorage::loadData() const |
| 125 | +{ |
| 126 | + return m_clientData; |
| 127 | +} |
| 128 | + |
| 129 | +QJsonObject ClientDataStorage::loadData(const QStringList &keys) const |
| 130 | +{ |
| 131 | + QJsonObject clientData; |
| 132 | + for (const QString &key : keys) |
| 133 | + { |
| 134 | + if (const auto iter = m_clientData.constFind(key); iter != m_clientData.constEnd()) |
| 135 | + clientData.insert(key, iter.value()); |
| 136 | + } |
| 137 | + return clientData; |
| 138 | +} |
0 commit comments