WebUI: Provide 'Merge trackers to existing torrent' option
[qBittorrent.git] / src / webui / api / serialize / serialize_torrent.cpp
blob177c44f5b3eeeb274d4a028e65c390cf5760a983
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018-2023 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 "serialize_torrent.h"
31 #include <QDateTime>
32 #include <QList>
34 #include "base/bittorrent/infohash.h"
35 #include "base/bittorrent/torrent.h"
36 #include "base/bittorrent/trackerentrystatus.h"
37 #include "base/path.h"
38 #include "base/tagset.h"
39 #include "base/utils/datetime.h"
40 #include "base/utils/string.h"
42 namespace
44 QString torrentStateToString(const BitTorrent::TorrentState state)
46 switch (state)
48 case BitTorrent::TorrentState::Error:
49 return u"error"_s;
50 case BitTorrent::TorrentState::MissingFiles:
51 return u"missingFiles"_s;
52 case BitTorrent::TorrentState::Uploading:
53 return u"uploading"_s;
54 case BitTorrent::TorrentState::StoppedUploading:
55 return u"stoppedUP"_s;
56 case BitTorrent::TorrentState::QueuedUploading:
57 return u"queuedUP"_s;
58 case BitTorrent::TorrentState::StalledUploading:
59 return u"stalledUP"_s;
60 case BitTorrent::TorrentState::CheckingUploading:
61 return u"checkingUP"_s;
62 case BitTorrent::TorrentState::ForcedUploading:
63 return u"forcedUP"_s;
64 case BitTorrent::TorrentState::Downloading:
65 return u"downloading"_s;
66 case BitTorrent::TorrentState::DownloadingMetadata:
67 return u"metaDL"_s;
68 case BitTorrent::TorrentState::ForcedDownloadingMetadata:
69 return u"forcedMetaDL"_s;
70 case BitTorrent::TorrentState::StoppedDownloading:
71 return u"stoppedDL"_s;
72 case BitTorrent::TorrentState::QueuedDownloading:
73 return u"queuedDL"_s;
74 case BitTorrent::TorrentState::StalledDownloading:
75 return u"stalledDL"_s;
76 case BitTorrent::TorrentState::CheckingDownloading:
77 return u"checkingDL"_s;
78 case BitTorrent::TorrentState::ForcedDownloading:
79 return u"forcedDL"_s;
80 case BitTorrent::TorrentState::CheckingResumeData:
81 return u"checkingResumeData"_s;
82 case BitTorrent::TorrentState::Moving:
83 return u"moving"_s;
84 default:
85 return u"unknown"_s;
90 QVariantMap serialize(const BitTorrent::Torrent &torrent)
92 const auto adjustQueuePosition = [](const int position) -> int
94 return (position < 0) ? 0 : (position + 1);
97 const auto adjustRatio = [](const qreal ratio) -> qreal
99 return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio;
102 const auto getLastActivityTime = [&torrent]() -> qlonglong
104 const qlonglong timeSinceActivity = torrent.timeSinceActivity();
105 return (timeSinceActivity < 0)
106 ? Utils::DateTime::toSecsSinceEpoch(torrent.addedTime())
107 : (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity);
110 return {
111 {KEY_TORRENT_ID, torrent.id().toString()},
112 {KEY_TORRENT_INFOHASHV1, torrent.infoHash().v1().toString()},
113 {KEY_TORRENT_INFOHASHV2, torrent.infoHash().v2().toString()},
114 {KEY_TORRENT_NAME, torrent.name()},
115 {KEY_TORRENT_MAGNET_URI, torrent.createMagnetURI()},
116 {KEY_TORRENT_SIZE, torrent.wantedSize()},
117 {KEY_TORRENT_PROGRESS, torrent.progress()},
118 {KEY_TORRENT_DLSPEED, torrent.downloadPayloadRate()},
119 {KEY_TORRENT_UPSPEED, torrent.uploadPayloadRate()},
120 {KEY_TORRENT_QUEUE_POSITION, adjustQueuePosition(torrent.queuePosition())},
121 {KEY_TORRENT_SEEDS, torrent.seedsCount()},
122 {KEY_TORRENT_NUM_COMPLETE, torrent.totalSeedsCount()},
123 {KEY_TORRENT_LEECHS, torrent.leechsCount()},
124 {KEY_TORRENT_NUM_INCOMPLETE, torrent.totalLeechersCount()},
126 {KEY_TORRENT_STATE, torrentStateToString(torrent.state())},
127 {KEY_TORRENT_ETA, torrent.eta()},
128 {KEY_TORRENT_SEQUENTIAL_DOWNLOAD, torrent.isSequentialDownload()},
129 {KEY_TORRENT_FIRST_LAST_PIECE_PRIO, torrent.hasFirstLastPiecePriority()},
131 {KEY_TORRENT_CATEGORY, torrent.category()},
132 {KEY_TORRENT_TAGS, Utils::String::joinIntoString(torrent.tags(), u", "_s)},
133 {KEY_TORRENT_SUPER_SEEDING, torrent.superSeeding()},
134 {KEY_TORRENT_FORCE_START, torrent.isForced()},
135 {KEY_TORRENT_SAVE_PATH, torrent.savePath().toString()},
136 {KEY_TORRENT_DOWNLOAD_PATH, torrent.downloadPath().toString()},
137 {KEY_TORRENT_CONTENT_PATH, torrent.contentPath().toString()},
138 {KEY_TORRENT_ROOT_PATH, torrent.rootPath().toString()},
139 {KEY_TORRENT_ADDED_ON, Utils::DateTime::toSecsSinceEpoch(torrent.addedTime())},
140 {KEY_TORRENT_COMPLETION_ON, Utils::DateTime::toSecsSinceEpoch(torrent.completedTime())},
141 {KEY_TORRENT_TRACKER, torrent.currentTracker()},
142 {KEY_TORRENT_TRACKERS_COUNT, torrent.trackers().size()},
143 {KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()},
144 {KEY_TORRENT_UP_LIMIT, torrent.uploadLimit()},
145 {KEY_TORRENT_AMOUNT_DOWNLOADED, torrent.totalDownload()},
146 {KEY_TORRENT_AMOUNT_UPLOADED, torrent.totalUpload()},
147 {KEY_TORRENT_AMOUNT_DOWNLOADED_SESSION, torrent.totalPayloadDownload()},
148 {KEY_TORRENT_AMOUNT_UPLOADED_SESSION, torrent.totalPayloadUpload()},
149 {KEY_TORRENT_AMOUNT_LEFT, torrent.remainingSize()},
150 {KEY_TORRENT_AMOUNT_COMPLETED, torrent.completedSize()},
151 {KEY_TORRENT_MAX_RATIO, torrent.maxRatio()},
152 {KEY_TORRENT_MAX_SEEDING_TIME, torrent.maxSeedingTime()},
153 {KEY_TORRENT_MAX_INACTIVE_SEEDING_TIME, torrent.maxInactiveSeedingTime()},
154 {KEY_TORRENT_RATIO, adjustRatio(torrent.realRatio())},
155 {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()},
156 {KEY_TORRENT_POPULARITY, torrent.popularity()},
157 {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()},
158 {KEY_TORRENT_INACTIVE_SEEDING_TIME_LIMIT, torrent.inactiveSeedingTimeLimit()},
159 {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, Utils::DateTime::toSecsSinceEpoch(torrent.lastSeenComplete())},
160 {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()},
161 {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()},
162 {KEY_TORRENT_SEEDING_TIME, torrent.finishedTime()},
163 {KEY_TORRENT_LAST_ACTIVITY_TIME, getLastActivityTime()},
164 {KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()},
165 {KEY_TORRENT_REANNOUNCE, torrent.nextAnnounce()},
166 {KEY_TORRENT_COMMENT, torrent.comment()},
167 {KEY_TORRENT_PRIVATE, (torrent.hasMetadata() ? torrent.isPrivate() : QVariant())},
168 {KEY_TORRENT_TOTAL_SIZE, torrent.totalSize()},
169 {KEY_TORRENT_HAS_METADATA, torrent.hasMetadata()}