This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
torrent.h
231 lines (182 loc) · 7.46 KB
/
torrent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-FileCopyrightText: 2015-2023 Alexey Rochev
//
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef LIBTREMOTESF_TORRENT_H
#define LIBTREMOTESF_TORRENT_H
#include <optional>
#include <span>
#include <vector>
#include <QDateTime>
#include <QJsonArray>
#include <QObject>
#include "formatters.h"
#include "peer.h"
#include "torrentfile.h"
#include "tracker.h"
class QJsonObject;
namespace libtremotesf {
class Rpc;
struct TorrentData {
Q_GADGET
public:
enum class Status {
Paused,
QueuedForChecking,
Checking,
QueuedForDownloading,
Downloading,
QueuedForSeeding,
Seeding
};
Q_ENUM(Status)
enum class Error { None, TrackerWarning, TrackerError, LocalError };
Q_ENUM(Error)
enum class Priority { Low, Normal, High };
Q_ENUM(Priority)
static int priorityToInt(Priority value);
enum class RatioLimitMode { Global, Single, Unlimited };
Q_ENUM(RatioLimitMode)
enum class IdleSeedingLimitMode { Global, Single, Unlimited };
Q_ENUM(IdleSeedingLimitMode)
[[nodiscard]] bool update(const QJsonObject& object, bool firstTime, const Rpc* rpc);
enum class UpdateKey;
[[nodiscard]] bool
update(std::span<const std::optional<UpdateKey>> keys, const QJsonArray& values, bool firstTime, const Rpc* rpc);
int id{};
QString hashString{};
QString name{};
QString magnetLink{};
Status status{};
Error error{};
QString errorString{};
int queuePosition{};
qint64 totalSize{};
qint64 completedSize{};
qint64 leftUntilDone{};
qint64 sizeWhenDone{};
double percentDone{};
double recheckProgress{};
int eta{};
bool metadataComplete{};
qint64 downloadSpeed{};
qint64 uploadSpeed{};
bool downloadSpeedLimited{};
int downloadSpeedLimit{}; // kB/s
bool uploadSpeedLimited{};
int uploadSpeedLimit{}; // kB/s
qint64 totalDownloaded{};
qint64 totalUploaded{};
double ratio{};
double ratioLimit{};
RatioLimitMode ratioLimitMode{};
int totalSeedersFromTrackersCount{};
int peersSendingToUsCount{};
std::vector<QString> webSeeders{};
int webSeedersSendingToUsCount{};
int totalLeechersFromTrackersCount{};
int peersGettingFromUsCount{};
int peersLimit{};
QDateTime addedDate{{}, {}, Qt::UTC};
QDateTime activityDate{{}, {}, Qt::UTC};
QDateTime doneDate{{}, {}, Qt::UTC};
IdleSeedingLimitMode idleSeedingLimitMode{};
int idleSeedingLimit{};
QString downloadDirectory{};
QString comment{};
QString creator{};
QDateTime creationDate{{}, {}, Qt::UTC};
Priority bandwidthPriority{};
bool honorSessionLimits;
bool singleFile = true;
std::vector<Tracker> trackers{};
[[nodiscard]] bool hasError() const { return error != Error::None; }
[[nodiscard]] bool isFinished() const { return leftUntilDone == 0; }
[[nodiscard]] bool isDownloadingStalled() const {
return (peersSendingToUsCount == 0 && webSeedersSendingToUsCount == 0);
}
[[nodiscard]] bool isSeedingStalled() const { return peersGettingFromUsCount == 0; }
private:
void updateProperty(
TorrentData::UpdateKey key, const QJsonValue& value, bool& changed, bool firstTime, const Rpc* rpc
);
};
class Torrent final : public QObject {
Q_OBJECT
public:
explicit Torrent(int id, const QJsonObject& object, Rpc* rpc, QObject* parent = nullptr);
explicit Torrent(
int id,
std::span<const std::optional<TorrentData::UpdateKey>> keys,
const QJsonArray& values,
Rpc* rpc,
QObject* parent = nullptr
);
// For testing only
explicit Torrent() = default;
[[nodiscard]] static QJsonArray updateFields();
[[nodiscard]] static std::optional<int> idFromJson(const QJsonObject& object);
[[nodiscard]] static std::optional<QJsonArray::size_type>
idKeyIndex(std::span<const std::optional<TorrentData::UpdateKey>> keys);
[[nodiscard]] static std::vector<std::optional<TorrentData::UpdateKey>>
mapUpdateKeys(const QJsonArray& stringKeys);
void setDownloadSpeedLimited(bool limited);
void setDownloadSpeedLimit(int limit);
void setUploadSpeedLimited(bool limited);
void setUploadSpeedLimit(int limit);
void setRatioLimitMode(TorrentData::RatioLimitMode mode);
void setRatioLimit(double limit);
void setPeersLimit(int limit);
void setHonorSessionLimits(bool honor);
void setBandwidthPriority(TorrentData::Priority priority);
void setIdleSeedingLimitMode(TorrentData::IdleSeedingLimitMode mode);
void setIdleSeedingLimit(int limit);
void addTrackers(const QStringList& announceUrls);
void setTracker(int trackerId, const QString& announce);
void removeTrackers(std::span<const int> trackerIds);
[[nodiscard]] const TorrentData& data() const { return mData; };
[[nodiscard]] bool isFilesEnabled() const { return mFilesEnabled; };
void setFilesEnabled(bool enabled);
[[nodiscard]] const std::vector<TorrentFile>& files() const { return mFiles; };
void setFilesWanted(std::span<const int> fileIds, bool wanted);
void setFilesPriority(std::span<const int> fileIds, TorrentFile::Priority priority);
void renameFile(const QString& path, const QString& newName);
[[nodiscard]] bool isPeersEnabled() const { return mPeersEnabled; };
void setPeersEnabled(bool enabled);
[[nodiscard]] const std::vector<Peer>& peers() const { return mPeers; };
[[nodiscard]] bool update(const QJsonObject& object);
[[nodiscard]] bool
update(std::span<const std::optional<TorrentData::UpdateKey>> keys, const QJsonArray& values);
void updateFiles(const QJsonObject& torrentMap);
void updatePeers(const QJsonObject& torrentMap);
void checkSingleFile(const QJsonObject& torrentMap);
private:
Rpc* mRpc{};
TorrentData mData{};
std::vector<TorrentFile> mFiles{};
bool mFilesEnabled{};
std::vector<Peer> mPeers{};
bool mPeersEnabled{};
signals:
void updated();
void changed();
void filesUpdated(const std::vector<int>& changedIndexes);
void peersUpdated(
const std::vector<std::pair<int, int>>& removedIndexRanges,
const std::vector<std::pair<int, int>>& changedIndexRanges,
int addedCount
);
void fileRenamed(const QString& filePath, const QString& newName);
};
}
SPECIALIZE_FORMATTER_FOR_Q_ENUM(libtremotesf::TorrentData::Status)
SPECIALIZE_FORMATTER_FOR_Q_ENUM(libtremotesf::TorrentData::Error)
SPECIALIZE_FORMATTER_FOR_Q_ENUM(libtremotesf::TorrentData::Priority)
SPECIALIZE_FORMATTER_FOR_Q_ENUM(libtremotesf::TorrentData::RatioLimitMode)
SPECIALIZE_FORMATTER_FOR_Q_ENUM(libtremotesf::TorrentData::IdleSeedingLimitMode)
namespace fmt {
template<>
struct formatter<libtremotesf::Torrent> : libtremotesf::SimpleFormatter {
format_context::iterator format(const libtremotesf::Torrent& torrent, format_context& ctx) FORMAT_CONST;
};
}
#endif // LIBTREMOTESF_TORRENT_H