1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: torrent.h 10771 2010-06-16 03:02:17Z Longinus00 $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef QTR_TORRENT_H |
---|
14 | #define QTR_TORRENT_H |
---|
15 | |
---|
16 | #include <QObject> |
---|
17 | #include <QIcon> |
---|
18 | #include <QMetaType> |
---|
19 | #include <QDateTime> |
---|
20 | #include <QString> |
---|
21 | #include <QStringList> |
---|
22 | #include <QList> |
---|
23 | #include <QTemporaryFile> |
---|
24 | #include <QVariant> |
---|
25 | |
---|
26 | #include <libtransmission/transmission.h> |
---|
27 | |
---|
28 | #include "speed.h" |
---|
29 | #include "types.h" |
---|
30 | |
---|
31 | extern "C" |
---|
32 | { |
---|
33 | struct tr_benc; |
---|
34 | } |
---|
35 | |
---|
36 | class Prefs; |
---|
37 | class QStyle; |
---|
38 | |
---|
39 | struct Peer |
---|
40 | { |
---|
41 | QString address; |
---|
42 | QString clientName; |
---|
43 | bool clientIsChoked; |
---|
44 | bool clientIsInterested; |
---|
45 | bool isDownloadingFrom; |
---|
46 | bool isEncrypted; |
---|
47 | bool isIncoming; |
---|
48 | bool isUploadingTo; |
---|
49 | bool peerIsChoked; |
---|
50 | bool peerIsInterested; |
---|
51 | int port; |
---|
52 | double progress; |
---|
53 | Speed rateToClient; |
---|
54 | Speed rateToPeer; |
---|
55 | }; |
---|
56 | |
---|
57 | typedef QList<Peer> PeerList; |
---|
58 | Q_DECLARE_METATYPE(Peer) |
---|
59 | Q_DECLARE_METATYPE(PeerList) |
---|
60 | |
---|
61 | struct TrackerStat |
---|
62 | { |
---|
63 | QString announce; |
---|
64 | int announceState; |
---|
65 | int downloadCount; |
---|
66 | bool hasAnnounced; |
---|
67 | bool hasScraped; |
---|
68 | QString host; |
---|
69 | int id; |
---|
70 | bool isBackup; |
---|
71 | int lastAnnouncePeerCount; |
---|
72 | int lastAnnounceResult; |
---|
73 | int lastAnnounceStartTime; |
---|
74 | bool lastAnnounceSucceeded; |
---|
75 | int lastAnnounceTime; |
---|
76 | bool lastAnnounceTimedOut; |
---|
77 | QString lastScrapeResult; |
---|
78 | int lastScrapeStartTime; |
---|
79 | bool lastScrapeSucceeded; |
---|
80 | int lastScrapeTime; |
---|
81 | bool lastScrapeTimedOut; |
---|
82 | int leecherCount; |
---|
83 | int nextAnnounceTime; |
---|
84 | int nextScrapeTime; |
---|
85 | int scrapeState; |
---|
86 | int seederCount; |
---|
87 | int tier; |
---|
88 | }; |
---|
89 | |
---|
90 | typedef QList<TrackerStat> TrackerStatsList; |
---|
91 | Q_DECLARE_METATYPE(TrackerStat) |
---|
92 | Q_DECLARE_METATYPE(TrackerStatsList) |
---|
93 | |
---|
94 | struct TrFile |
---|
95 | { |
---|
96 | TrFile(): index(-1), priority(0), wanted(true), size(0), have(0) { } |
---|
97 | int index; |
---|
98 | int priority; |
---|
99 | bool wanted; |
---|
100 | uint64_t size; |
---|
101 | uint64_t have; |
---|
102 | QString filename; |
---|
103 | }; |
---|
104 | |
---|
105 | typedef QList<TrFile> FileList; |
---|
106 | Q_DECLARE_METATYPE(TrFile) |
---|
107 | Q_DECLARE_METATYPE(FileList) |
---|
108 | |
---|
109 | |
---|
110 | class Torrent: public QObject |
---|
111 | { |
---|
112 | Q_OBJECT; |
---|
113 | |
---|
114 | public: |
---|
115 | |
---|
116 | enum |
---|
117 | { |
---|
118 | ID, |
---|
119 | UPLOAD_SPEED, |
---|
120 | DOWNLOAD_SPEED, |
---|
121 | DOWNLOAD_DIR, |
---|
122 | ACTIVITY, |
---|
123 | NAME, |
---|
124 | ERROR, |
---|
125 | ERROR_STRING, |
---|
126 | SIZE_WHEN_DONE, |
---|
127 | LEFT_UNTIL_DONE, |
---|
128 | HAVE_UNCHECKED, |
---|
129 | HAVE_VERIFIED, |
---|
130 | DESIRED_AVAILABLE, |
---|
131 | TOTAL_SIZE, |
---|
132 | PIECE_SIZE, |
---|
133 | PIECE_COUNT, |
---|
134 | PEERS_GETTING_FROM_US, |
---|
135 | PEERS_SENDING_TO_US, |
---|
136 | WEBSEEDS_SENDING_TO_US, |
---|
137 | PERCENT_DONE, |
---|
138 | METADATA_PERCENT_DONE, |
---|
139 | PERCENT_VERIFIED, |
---|
140 | DATE_ACTIVITY, |
---|
141 | DATE_ADDED, |
---|
142 | DATE_STARTED, |
---|
143 | DATE_CREATED, |
---|
144 | PEERS_CONNECTED, |
---|
145 | ETA, |
---|
146 | RATIO, |
---|
147 | DOWNLOADED_EVER, |
---|
148 | UPLOADED_EVER, |
---|
149 | FAILED_EVER, |
---|
150 | TRACKERS, |
---|
151 | TRACKERSTATS, |
---|
152 | MIME_ICON, |
---|
153 | SEED_RATIO_LIMIT, |
---|
154 | SEED_RATIO_MODE, |
---|
155 | DOWN_LIMIT, |
---|
156 | DOWN_LIMITED, |
---|
157 | UP_LIMIT, |
---|
158 | UP_LIMITED, |
---|
159 | HONORS_SESSION_LIMITS, |
---|
160 | PEER_LIMIT, |
---|
161 | HASH_STRING, |
---|
162 | IS_FINISHED, |
---|
163 | IS_PRIVATE, |
---|
164 | COMMENT, |
---|
165 | CREATOR, |
---|
166 | MANUAL_ANNOUNCE_TIME, |
---|
167 | PEERS, |
---|
168 | TORRENT_FILE, |
---|
169 | BANDWIDTH_PRIORITY, |
---|
170 | |
---|
171 | PROPERTY_COUNT |
---|
172 | }; |
---|
173 | |
---|
174 | public: |
---|
175 | Torrent( Prefs&, int id ); |
---|
176 | virtual ~Torrent( ); |
---|
177 | |
---|
178 | signals: |
---|
179 | void torrentChanged( int id ); |
---|
180 | |
---|
181 | private: |
---|
182 | |
---|
183 | enum Group |
---|
184 | { |
---|
185 | INFO, // info fields that only need to be loaded once |
---|
186 | STAT, // commonly-used stats that should be refreshed often |
---|
187 | STAT_EXTRA, // rarely used; only refresh if details dialog is open |
---|
188 | DERIVED // doesn't come from RPC |
---|
189 | }; |
---|
190 | |
---|
191 | struct Property |
---|
192 | { |
---|
193 | int id; |
---|
194 | const char * key; |
---|
195 | int type; |
---|
196 | int group; |
---|
197 | }; |
---|
198 | |
---|
199 | static Property myProperties[]; |
---|
200 | |
---|
201 | bool magnetTorrent; |
---|
202 | |
---|
203 | public: |
---|
204 | typedef QList<const char*> KeyList; |
---|
205 | static const KeyList& getInfoKeys( ); |
---|
206 | static const KeyList& getStatKeys( ); |
---|
207 | static const KeyList& getExtraStatKeys( ); |
---|
208 | |
---|
209 | private: |
---|
210 | static KeyList buildKeyList( Group group ); |
---|
211 | |
---|
212 | private: |
---|
213 | QVariant myValues[PROPERTY_COUNT]; |
---|
214 | |
---|
215 | int getInt ( int key ) const; |
---|
216 | bool getBool ( int key ) const; |
---|
217 | QTime getTime ( int key ) const; |
---|
218 | QIcon getIcon ( int key ) const; |
---|
219 | double getDouble ( int key ) const; |
---|
220 | qulonglong getSize ( int key ) const; |
---|
221 | QString getString ( int key ) const; |
---|
222 | QDateTime getDateTime ( int key ) const; |
---|
223 | |
---|
224 | bool setInt ( int key, int value ); |
---|
225 | bool setBool ( int key, bool value ); |
---|
226 | bool setIcon ( int key, const QIcon& ); |
---|
227 | bool setDouble ( int key, double ); |
---|
228 | bool setString ( int key, const char * ); |
---|
229 | bool setSize ( int key, qulonglong ); |
---|
230 | bool setDateTime ( int key, const QDateTime& ); |
---|
231 | |
---|
232 | public: |
---|
233 | int getBandwidthPriority( ) const { return getInt( BANDWIDTH_PRIORITY ); } |
---|
234 | int id( ) const { return getInt( ID ); } |
---|
235 | QString name( ) const { return getString( NAME ); } |
---|
236 | QString creator( ) const { return getString( CREATOR ); } |
---|
237 | QString comment( ) const { return getString( COMMENT ); } |
---|
238 | QString getPath( ) const { return getString( DOWNLOAD_DIR ); } |
---|
239 | QString getError( ) const; |
---|
240 | QString hashString( ) const { return getString( HASH_STRING ); } |
---|
241 | QString torrentFile( ) const { return getString( TORRENT_FILE ); } |
---|
242 | bool hasError( ) const { return !getError( ).isEmpty( ); } |
---|
243 | bool isDone( ) const { return getSize( LEFT_UNTIL_DONE ) == 0; } |
---|
244 | bool isSeed( ) const { return haveVerified() >= totalSize(); } |
---|
245 | bool isPrivate( ) const { return getBool( IS_PRIVATE ); } |
---|
246 | bool getSeedRatio( double& setme ) const; |
---|
247 | uint64_t haveVerified( ) const { return getSize( HAVE_VERIFIED ); } |
---|
248 | uint64_t haveUnverified( ) const { return getSize( HAVE_UNCHECKED ); } |
---|
249 | uint64_t desiredAvailable( ) const { return getSize( DESIRED_AVAILABLE ); } |
---|
250 | uint64_t haveTotal( ) const { return haveVerified( ) + haveUnverified(); } |
---|
251 | uint64_t totalSize( ) const { return getSize( TOTAL_SIZE ); } |
---|
252 | uint64_t sizeWhenDone( ) const { return getSize( SIZE_WHEN_DONE ); } |
---|
253 | uint64_t leftUntilDone( ) const { return getSize( LEFT_UNTIL_DONE ); } |
---|
254 | uint64_t pieceSize( ) const { return getSize( PIECE_SIZE ); } |
---|
255 | bool hasMetadata( ) const { return getDouble( METADATA_PERCENT_DONE ) >= 1.0; } |
---|
256 | bool isMagnet( ) const { return magnetTorrent; } |
---|
257 | int pieceCount( ) const { return getInt( PIECE_COUNT ); } |
---|
258 | double ratio( ) const { return getDouble( RATIO ); } |
---|
259 | double percentComplete( ) const { return haveTotal() / (double)totalSize(); } |
---|
260 | double percentDone( ) const { return getDouble( PERCENT_DONE ); } |
---|
261 | double metadataPercentDone( ) const { return getDouble( METADATA_PERCENT_DONE ); } |
---|
262 | uint64_t downloadedEver( ) const { return getSize( DOWNLOADED_EVER ); } |
---|
263 | uint64_t uploadedEver( ) const { return getSize( UPLOADED_EVER ); } |
---|
264 | uint64_t failedEver( ) const { return getSize( FAILED_EVER ); } |
---|
265 | int compareTracker( const Torrent& ) const; |
---|
266 | int compareRatio( const Torrent& ) const; |
---|
267 | int compareETA( const Torrent& ) const; |
---|
268 | bool hasETA( ) const { return getETA( ) >= 0; } |
---|
269 | int getETA( ) const { return getInt( ETA ); } |
---|
270 | QDateTime lastActivity( ) const { return getDateTime( DATE_ACTIVITY ); } |
---|
271 | QDateTime lastStarted( ) const { return getDateTime( DATE_STARTED ); } |
---|
272 | QDateTime dateAdded( ) const { return getDateTime( DATE_ADDED ); } |
---|
273 | QDateTime dateCreated( ) const { return getDateTime( DATE_CREATED ); } |
---|
274 | QDateTime manualAnnounceTime( ) const { return getDateTime( MANUAL_ANNOUNCE_TIME ); } |
---|
275 | bool canManualAnnounce( ) const { return isReadyToTransfer() && (manualAnnounceTime()<=QDateTime::currentDateTime()); } |
---|
276 | int peersWeAreDownloadingFrom( ) const { return getInt( PEERS_SENDING_TO_US ) + getInt( WEBSEEDS_SENDING_TO_US ); } |
---|
277 | int peersWeAreUploadingTo( ) const { return getInt( PEERS_GETTING_FROM_US ); } |
---|
278 | bool isUploading( ) const { return peersWeAreUploadingTo( ) > 0; } |
---|
279 | int connectedPeers( ) const { return getInt( PEERS_CONNECTED ); } |
---|
280 | int connectedPeersAndWebseeds( ) const { return connectedPeers( ) + getInt( WEBSEEDS_SENDING_TO_US ); } |
---|
281 | Speed downloadSpeed( ) const { return Speed::fromBps( getInt( DOWNLOAD_SPEED ) ); } |
---|
282 | Speed uploadSpeed( ) const { return Speed::fromBps( getInt( UPLOAD_SPEED ) ); } |
---|
283 | double getVerifyProgress( ) const { return getDouble( PERCENT_VERIFIED ); } |
---|
284 | bool hasFileSubstring( const QString& substr ) const; |
---|
285 | bool hasTrackerSubstring( const QString& substr ) const; |
---|
286 | Speed uploadLimit( ) const { return Speed::fromKbps( getInt( UP_LIMIT ) ); } |
---|
287 | Speed downloadLimit( ) const { return Speed::fromKbps( getInt( DOWN_LIMIT ) ); } |
---|
288 | bool uploadIsLimited( ) const { return getBool( UP_LIMITED ); } |
---|
289 | bool downloadIsLimited( ) const { return getBool( DOWN_LIMITED ); } |
---|
290 | bool honorsSessionLimits( ) const { return getBool( HONORS_SESSION_LIMITS ); } |
---|
291 | int peerLimit( ) const { return getInt( PEER_LIMIT ); } |
---|
292 | double seedRatioLimit( ) const { return getDouble( SEED_RATIO_LIMIT ); } |
---|
293 | tr_ratiolimit seedRatioMode( ) const { return (tr_ratiolimit) getInt( SEED_RATIO_MODE ); } |
---|
294 | TrackerStatsList trackerStats( ) const{ return myValues[TRACKERSTATS].value<TrackerStatsList>(); } |
---|
295 | PeerList peers( ) const{ return myValues[PEERS].value<PeerList>(); } |
---|
296 | const FileList& files( ) const { return myFiles; } |
---|
297 | |
---|
298 | public: |
---|
299 | QString activityString( ) const; |
---|
300 | tr_torrent_activity getActivity( ) const { return (tr_torrent_activity) getInt( ACTIVITY ); } |
---|
301 | bool isFinished( ) const { return getBool( IS_FINISHED ); } |
---|
302 | bool isPaused( ) const { return getActivity( ) == TR_STATUS_STOPPED; } |
---|
303 | bool isVerifying( ) const { return getActivity( ) == TR_STATUS_CHECK; } |
---|
304 | bool isDownloading( ) const { return getActivity( ) == TR_STATUS_DOWNLOAD; } |
---|
305 | bool isSeeding( ) const { return getActivity( ) == TR_STATUS_SEED; } |
---|
306 | bool isReadyToTransfer( ) const { return getActivity()==TR_STATUS_DOWNLOAD || getActivity()==TR_STATUS_SEED; } |
---|
307 | void notifyComplete( ) const; |
---|
308 | |
---|
309 | public: |
---|
310 | void update( tr_benc * dict ); |
---|
311 | void setMagnet( bool magnet ) { magnetTorrent = magnet; } |
---|
312 | |
---|
313 | private: |
---|
314 | const char * getMimeTypeString( ) const; |
---|
315 | void updateMimeIcon( ); |
---|
316 | |
---|
317 | public: |
---|
318 | QIcon getMimeTypeIcon( ) const { return getIcon( MIME_ICON ); } |
---|
319 | |
---|
320 | private: |
---|
321 | Prefs& myPrefs; |
---|
322 | FileList myFiles; |
---|
323 | }; |
---|
324 | |
---|
325 | Q_DECLARE_METATYPE(const Torrent*) |
---|
326 | |
---|
327 | #endif |
---|
328 | |
---|