WebUI: disallow unnecessary quotes in property name
[qBittorrent.git] / src / base / bittorrent / addtorrentparams.cpp
blobd284b36d36eaaba4e3cc9b9ca2a65808a938e302
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
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.
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.
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.
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.
29 #include "addtorrentparams.h"
31 #include <QJsonArray>
32 #include <QJsonObject>
33 #include <QJsonValue>
35 #include "base/utils/sslkey.h"
36 #include "base/utils/string.h"
38 const QString PARAM_CATEGORY = u"category"_s;
39 const QString PARAM_TAGS = u"tags"_s;
40 const QString PARAM_SAVEPATH = u"save_path"_s;
41 const QString PARAM_USEDOWNLOADPATH = u"use_download_path"_s;
42 const QString PARAM_DOWNLOADPATH = u"download_path"_s;
43 const QString PARAM_OPERATINGMODE = u"operating_mode"_s;
44 const QString PARAM_QUEUETOP = u"add_to_top_of_queue"_s;
45 const QString PARAM_STOPPED = u"stopped"_s;
46 const QString PARAM_STOPCONDITION = u"stop_condition"_s;
47 const QString PARAM_SKIPCHECKING = u"skip_checking"_s;
48 const QString PARAM_CONTENTLAYOUT = u"content_layout"_s;
49 const QString PARAM_AUTOTMM = u"use_auto_tmm"_s;
50 const QString PARAM_UPLOADLIMIT = u"upload_limit"_s;
51 const QString PARAM_DOWNLOADLIMIT = u"download_limit"_s;
52 const QString PARAM_SEEDINGTIMELIMIT = u"seeding_time_limit"_s;
53 const QString PARAM_INACTIVESEEDINGTIMELIMIT = u"inactive_seeding_time_limit"_s;
54 const QString PARAM_SHARELIMITACTION = u"share_limit_action"_s;
55 const QString PARAM_RATIOLIMIT = u"ratio_limit"_s;
56 const QString PARAM_SSL_CERTIFICATE = u"ssl_certificate"_s;
57 const QString PARAM_SSL_PRIVATEKEY = u"ssl_private_key"_s;
58 const QString PARAM_SSL_DHPARAMS = u"ssl_dh_params"_s;
60 namespace
62 TagSet parseTagSet(const QJsonArray &jsonArr)
64 TagSet tags;
65 for (const QJsonValue &jsonVal : jsonArr)
66 tags.insert(Tag(jsonVal.toString()));
68 return tags;
71 QJsonArray serializeTagSet(const TagSet &tags)
73 QJsonArray arr;
74 for (const Tag &tag : tags)
75 arr.append(tag.toString());
77 return arr;
80 std::optional<bool> getOptionalBool(const QJsonObject &jsonObj, const QString &key)
82 const QJsonValue jsonVal = jsonObj.value(key);
83 if (jsonVal.isUndefined() || jsonVal.isNull())
84 return std::nullopt;
86 return jsonVal.toBool();
89 template <typename Enum>
90 std::optional<Enum> getOptionalEnum(const QJsonObject &jsonObj, const QString &key)
92 const QJsonValue jsonVal = jsonObj.value(key);
93 if (jsonVal.isUndefined() || jsonVal.isNull())
94 return std::nullopt;
96 return Utils::String::toEnum<Enum>(jsonVal.toString(), {});
99 template <typename Enum>
100 Enum getEnum(const QJsonObject &jsonObj, const QString &key, const Enum defaultValue = {})
102 const QJsonValue jsonVal = jsonObj.value(key);
103 return Utils::String::toEnum<Enum>(jsonVal.toString(), defaultValue);
107 BitTorrent::AddTorrentParams BitTorrent::parseAddTorrentParams(const QJsonObject &jsonObj)
109 const AddTorrentParams params
111 .name = {},
112 .category = jsonObj.value(PARAM_CATEGORY).toString(),
113 .tags = parseTagSet(jsonObj.value(PARAM_TAGS).toArray()),
114 .savePath = Path(jsonObj.value(PARAM_SAVEPATH).toString()),
115 .useDownloadPath = getOptionalBool(jsonObj, PARAM_USEDOWNLOADPATH),
116 .downloadPath = Path(jsonObj.value(PARAM_DOWNLOADPATH).toString()),
117 .addForced = (getEnum<TorrentOperatingMode>(jsonObj, PARAM_OPERATINGMODE) == TorrentOperatingMode::Forced),
118 .addToQueueTop = getOptionalBool(jsonObj, PARAM_QUEUETOP),
119 .addStopped = getOptionalBool(jsonObj, PARAM_STOPPED),
120 .stopCondition = getOptionalEnum<Torrent::StopCondition>(jsonObj, PARAM_STOPCONDITION),
121 .filePaths = {},
122 .filePriorities = {},
123 .skipChecking = jsonObj.value(PARAM_SKIPCHECKING).toBool(),
124 .contentLayout = getOptionalEnum<TorrentContentLayout>(jsonObj, PARAM_CONTENTLAYOUT),
125 .useAutoTMM = getOptionalBool(jsonObj, PARAM_AUTOTMM),
126 .uploadLimit = jsonObj.value(PARAM_UPLOADLIMIT).toInt(-1),
127 .downloadLimit = jsonObj.value(PARAM_DOWNLOADLIMIT).toInt(-1),
128 .seedingTimeLimit = jsonObj.value(PARAM_SEEDINGTIMELIMIT).toInt(Torrent::USE_GLOBAL_SEEDING_TIME),
129 .inactiveSeedingTimeLimit = jsonObj.value(PARAM_INACTIVESEEDINGTIMELIMIT).toInt(Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME),
130 .ratioLimit = jsonObj.value(PARAM_RATIOLIMIT).toDouble(Torrent::USE_GLOBAL_RATIO),
131 .shareLimitAction = getEnum<ShareLimitAction>(jsonObj, PARAM_SHARELIMITACTION, ShareLimitAction::Default),
132 .sslParameters =
134 .certificate = QSslCertificate(jsonObj.value(PARAM_SSL_CERTIFICATE).toString().toLatin1()),
135 .privateKey = Utils::SSLKey::load(jsonObj.value(PARAM_SSL_PRIVATEKEY).toString().toLatin1()),
136 .dhParams = jsonObj.value(PARAM_SSL_DHPARAMS).toString().toLatin1()
139 return params;
142 QJsonObject BitTorrent::serializeAddTorrentParams(const AddTorrentParams &params)
144 QJsonObject jsonObj
146 {PARAM_CATEGORY, params.category},
147 {PARAM_TAGS, serializeTagSet(params.tags)},
148 {PARAM_SAVEPATH, params.savePath.data()},
149 {PARAM_DOWNLOADPATH, params.downloadPath.data()},
150 {PARAM_OPERATINGMODE, Utils::String::fromEnum(params.addForced
151 ? TorrentOperatingMode::Forced : TorrentOperatingMode::AutoManaged)},
152 {PARAM_SKIPCHECKING, params.skipChecking},
153 {PARAM_UPLOADLIMIT, params.uploadLimit},
154 {PARAM_DOWNLOADLIMIT, params.downloadLimit},
155 {PARAM_SEEDINGTIMELIMIT, params.seedingTimeLimit},
156 {PARAM_INACTIVESEEDINGTIMELIMIT, params.inactiveSeedingTimeLimit},
157 {PARAM_SHARELIMITACTION, Utils::String::fromEnum(params.shareLimitAction)},
158 {PARAM_RATIOLIMIT, params.ratioLimit},
159 {PARAM_SSL_CERTIFICATE, QString::fromLatin1(params.sslParameters.certificate.toPem())},
160 {PARAM_SSL_PRIVATEKEY, QString::fromLatin1(params.sslParameters.privateKey.toPem())},
161 {PARAM_SSL_DHPARAMS, QString::fromLatin1(params.sslParameters.dhParams)}
164 if (params.addToQueueTop)
165 jsonObj[PARAM_QUEUETOP] = *params.addToQueueTop;
166 if (params.addStopped)
167 jsonObj[PARAM_STOPPED] = *params.addStopped;
168 if (params.stopCondition)
169 jsonObj[PARAM_STOPCONDITION] = Utils::String::fromEnum(*params.stopCondition);
170 if (params.contentLayout)
171 jsonObj[PARAM_CONTENTLAYOUT] = Utils::String::fromEnum(*params.contentLayout);
172 if (params.useAutoTMM)
173 jsonObj[PARAM_AUTOTMM] = *params.useAutoTMM;
174 if (params.useDownloadPath)
175 jsonObj[PARAM_USEDOWNLOADPATH] = *params.useDownloadPath;
177 return jsonObj;