Show "last activity" value under all circumstances
[qBittorrent.git] / src / webui / api / serialize / serialize_torrent.cpp
blobbd356050e040b6b63325adb10cddc194339003e3
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 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 <QSet>
33 #include <QVector>
35 #include "base/bittorrent/infohash.h"
36 #include "base/bittorrent/torrent.h"
37 #include "base/bittorrent/trackerentry.h"
38 #include "base/utils/fs.h"
40 namespace
42 QString torrentStateToString(const BitTorrent::TorrentState state)
44 switch (state)
46 case BitTorrent::TorrentState::Error:
47 return QLatin1String("error");
48 case BitTorrent::TorrentState::MissingFiles:
49 return QLatin1String("missingFiles");
50 case BitTorrent::TorrentState::Uploading:
51 return QLatin1String("uploading");
52 case BitTorrent::TorrentState::PausedUploading:
53 return QLatin1String("pausedUP");
54 case BitTorrent::TorrentState::QueuedUploading:
55 return QLatin1String("queuedUP");
56 case BitTorrent::TorrentState::StalledUploading:
57 return QLatin1String("stalledUP");
58 case BitTorrent::TorrentState::CheckingUploading:
59 return QLatin1String("checkingUP");
60 case BitTorrent::TorrentState::ForcedUploading:
61 return QLatin1String("forcedUP");
62 case BitTorrent::TorrentState::Downloading:
63 return QLatin1String("downloading");
64 case BitTorrent::TorrentState::DownloadingMetadata:
65 return QLatin1String("metaDL");
66 case BitTorrent::TorrentState::PausedDownloading:
67 return QLatin1String("pausedDL");
68 case BitTorrent::TorrentState::QueuedDownloading:
69 return QLatin1String("queuedDL");
70 case BitTorrent::TorrentState::StalledDownloading:
71 return QLatin1String("stalledDL");
72 case BitTorrent::TorrentState::CheckingDownloading:
73 return QLatin1String("checkingDL");
74 case BitTorrent::TorrentState::ForcedDownloading:
75 return QLatin1String("forcedDL");
76 case BitTorrent::TorrentState::CheckingResumeData:
77 return QLatin1String("checkingResumeData");
78 case BitTorrent::TorrentState::Moving:
79 return QLatin1String("moving");
80 default:
81 return QLatin1String("unknown");
86 QVariantMap serialize(const BitTorrent::Torrent &torrent)
88 const auto adjustQueuePosition = [](const int position) -> int
90 return (position < 0) ? 0 : (position + 1);
93 const auto adjustRatio = [](const qreal ratio) -> qreal
95 return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio;
98 return {
99 // TODO: Add fields for real SHA1 and SHA256 hashes
100 {KEY_TORRENT_ID, QString(torrent.id().toString())},
101 {KEY_TORRENT_NAME, torrent.name()},
102 {KEY_TORRENT_MAGNET_URI, torrent.createMagnetURI()},
103 {KEY_TORRENT_SIZE, torrent.wantedSize()},
104 {KEY_TORRENT_PROGRESS, torrent.progress()},
105 {KEY_TORRENT_DLSPEED, torrent.downloadPayloadRate()},
106 {KEY_TORRENT_UPSPEED, torrent.uploadPayloadRate()},
107 {KEY_TORRENT_QUEUE_POSITION, adjustQueuePosition(torrent.queuePosition())},
108 {KEY_TORRENT_SEEDS, torrent.seedsCount()},
109 {KEY_TORRENT_NUM_COMPLETE, torrent.totalSeedsCount()},
110 {KEY_TORRENT_LEECHS, torrent.leechsCount()},
111 {KEY_TORRENT_NUM_INCOMPLETE, torrent.totalLeechersCount()},
113 {KEY_TORRENT_STATE, torrentStateToString(torrent.state())},
114 {KEY_TORRENT_ETA, torrent.eta()},
115 {KEY_TORRENT_SEQUENTIAL_DOWNLOAD, torrent.isSequentialDownload()},
116 {KEY_TORRENT_FIRST_LAST_PIECE_PRIO, torrent.hasFirstLastPiecePriority()},
118 {KEY_TORRENT_CATEGORY, torrent.category()},
119 {KEY_TORRENT_TAGS, torrent.tags().values().join(", ")},
120 {KEY_TORRENT_SUPER_SEEDING, torrent.superSeeding()},
121 {KEY_TORRENT_FORCE_START, torrent.isForced()},
122 {KEY_TORRENT_SAVE_PATH, Utils::Fs::toNativePath(torrent.savePath())},
123 {KEY_TORRENT_CONTENT_PATH, Utils::Fs::toNativePath(torrent.contentPath())},
124 {KEY_TORRENT_ADDED_ON, torrent.addedTime().toSecsSinceEpoch()},
125 {KEY_TORRENT_COMPLETION_ON, torrent.completedTime().toSecsSinceEpoch()},
126 {KEY_TORRENT_TRACKER, torrent.currentTracker()},
127 {KEY_TORRENT_TRACKERS_COUNT, torrent.trackers().size()},
128 {KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()},
129 {KEY_TORRENT_UP_LIMIT, torrent.uploadLimit()},
130 {KEY_TORRENT_AMOUNT_DOWNLOADED, torrent.totalDownload()},
131 {KEY_TORRENT_AMOUNT_UPLOADED, torrent.totalUpload()},
132 {KEY_TORRENT_AMOUNT_DOWNLOADED_SESSION, torrent.totalPayloadDownload()},
133 {KEY_TORRENT_AMOUNT_UPLOADED_SESSION, torrent.totalPayloadUpload()},
134 {KEY_TORRENT_AMOUNT_LEFT, torrent.remainingSize()},
135 {KEY_TORRENT_AMOUNT_COMPLETED, torrent.completedSize()},
136 {KEY_TORRENT_MAX_RATIO, torrent.maxRatio()},
137 {KEY_TORRENT_MAX_SEEDING_TIME, torrent.maxSeedingTime()},
138 {KEY_TORRENT_RATIO, adjustRatio(torrent.realRatio())},
139 {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()},
140 {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()},
141 {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, torrent.lastSeenComplete().toSecsSinceEpoch()},
142 {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()},
143 {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()},
144 {KEY_TORRENT_SEEDING_TIME, torrent.seedingTime()},
145 {KEY_TORRENT_LAST_ACTIVITY_TIME, (QDateTime::currentDateTime().toSecsSinceEpoch() - torrent.timeSinceActivity())},
146 {KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()},
148 {KEY_TORRENT_TOTAL_SIZE, torrent.totalSize()}