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 10077 2010-02-02 05:34:26Z charles $ |
---|
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 TrFile |
---|
62 | { |
---|
63 | TrFile(): index(-1), priority(0), wanted(true), size(0), have(0) { } |
---|
64 | int index; |
---|
65 | int priority; |
---|
66 | bool wanted; |
---|
67 | uint64_t size; |
---|
68 | uint64_t have; |
---|
69 | QString filename; |
---|
70 | }; |
---|
71 | |
---|
72 | typedef QList<TrFile> FileList; |
---|
73 | Q_DECLARE_METATYPE(TrFile) |
---|
74 | Q_DECLARE_METATYPE(FileList) |
---|
75 | |
---|
76 | |
---|
77 | class Torrent: public QObject |
---|
78 | { |
---|
79 | Q_OBJECT; |
---|
80 | |
---|
81 | public: |
---|
82 | |
---|
83 | enum |
---|
84 | { |
---|
85 | ID, |
---|
86 | UPLOAD_SPEED, |
---|
87 | DOWNLOAD_SPEED, |
---|
88 | DOWNLOAD_DIR, |
---|
89 | ACTIVITY, |
---|
90 | NAME, |
---|
91 | ERROR, |
---|
92 | ERROR_STRING, |
---|
93 | SIZE_WHEN_DONE, |
---|
94 | LEFT_UNTIL_DONE, |
---|
95 | HAVE_UNCHECKED, |
---|
96 | HAVE_VERIFIED, |
---|
97 | DESIRED_AVAILABLE, |
---|
98 | TOTAL_SIZE, |
---|
99 | PIECE_SIZE, |
---|
100 | PIECE_COUNT, |
---|
101 | PEERS_GETTING_FROM_US, |
---|
102 | PEERS_SENDING_TO_US, |
---|
103 | WEBSEEDS_SENDING_TO_US, |
---|
104 | PERCENT_DONE, |
---|
105 | METADATA_PERCENT_DONE, |
---|
106 | PERCENT_VERIFIED, |
---|
107 | DATE_ACTIVITY, |
---|
108 | DATE_ADDED, |
---|
109 | DATE_STARTED, |
---|
110 | DATE_CREATED, |
---|
111 | PEERS_CONNECTED, |
---|
112 | ETA, |
---|
113 | RATIO, |
---|
114 | DOWNLOADED_EVER, |
---|
115 | UPLOADED_EVER, |
---|
116 | FAILED_EVER, |
---|
117 | TRACKERS, |
---|
118 | MIME_ICON, |
---|
119 | SEED_RATIO_LIMIT, |
---|
120 | SEED_RATIO_MODE, |
---|
121 | DOWN_LIMIT, |
---|
122 | DOWN_LIMITED, |
---|
123 | UP_LIMIT, |
---|
124 | UP_LIMITED, |
---|
125 | HONORS_SESSION_LIMITS, |
---|
126 | PEER_LIMIT, |
---|
127 | HASH_STRING, |
---|
128 | IS_PRIVATE, |
---|
129 | COMMENT, |
---|
130 | CREATOR, |
---|
131 | LAST_ANNOUNCE_TIME, |
---|
132 | LAST_SCRAPE_TIME, |
---|
133 | MANUAL_ANNOUNCE_TIME, |
---|
134 | NEXT_ANNOUNCE_TIME, |
---|
135 | NEXT_SCRAPE_TIME, |
---|
136 | SCRAPE_RESPONSE, |
---|
137 | ANNOUNCE_RESPONSE, |
---|
138 | ANNOUNCE_URL, |
---|
139 | PEERS, |
---|
140 | TORRENT_FILE, |
---|
141 | BANDWIDTH_PRIORITY, |
---|
142 | |
---|
143 | PROPERTY_COUNT |
---|
144 | }; |
---|
145 | |
---|
146 | public: |
---|
147 | Torrent( Prefs&, int id ); |
---|
148 | virtual ~Torrent( ); |
---|
149 | |
---|
150 | signals: |
---|
151 | void torrentChanged( int id ); |
---|
152 | |
---|
153 | private: |
---|
154 | |
---|
155 | enum Group |
---|
156 | { |
---|
157 | INFO, // info fields that only need to be loaded once |
---|
158 | STAT, // commonly-used stats that should be refreshed often |
---|
159 | STAT_EXTRA, // rarely used; only refresh if details dialog is open |
---|
160 | DERIVED // doesn't come from RPC |
---|
161 | }; |
---|
162 | |
---|
163 | struct Property |
---|
164 | { |
---|
165 | int id; |
---|
166 | const char * key; |
---|
167 | int type; |
---|
168 | int group; |
---|
169 | }; |
---|
170 | |
---|
171 | static Property myProperties[]; |
---|
172 | |
---|
173 | public: |
---|
174 | typedef QList<const char*> KeyList; |
---|
175 | static const KeyList& getInfoKeys( ); |
---|
176 | static const KeyList& getStatKeys( ); |
---|
177 | static const KeyList& getExtraStatKeys( ); |
---|
178 | |
---|
179 | private: |
---|
180 | static KeyList buildKeyList( Group group ); |
---|
181 | |
---|
182 | private: |
---|
183 | QVariant myValues[PROPERTY_COUNT]; |
---|
184 | |
---|
185 | int getInt ( int key ) const; |
---|
186 | bool getBool ( int key ) const; |
---|
187 | QTime getTime ( int key ) const; |
---|
188 | QIcon getIcon ( int key ) const; |
---|
189 | double getDouble ( int key ) const; |
---|
190 | qulonglong getSize ( int key ) const; |
---|
191 | QString getString ( int key ) const; |
---|
192 | QDateTime getDateTime ( int key ) const; |
---|
193 | |
---|
194 | bool setInt ( int key, int value ); |
---|
195 | bool setBool ( int key, bool value ); |
---|
196 | bool setIcon ( int key, const QIcon& ); |
---|
197 | bool setDouble ( int key, double ); |
---|
198 | bool setString ( int key, const char * ); |
---|
199 | bool setSize ( int key, qulonglong ); |
---|
200 | bool setDateTime ( int key, const QDateTime& ); |
---|
201 | |
---|
202 | public: |
---|
203 | int getBandwidthPriority( ) const { return getInt( BANDWIDTH_PRIORITY ); } |
---|
204 | int id( ) const { return getInt( ID ); } |
---|
205 | QString name( ) const { return getString( NAME ); } |
---|
206 | QString creator( ) const { return getString( CREATOR ); } |
---|
207 | QString comment( ) const { return getString( COMMENT ); } |
---|
208 | QString getPath( ) const { return getString( DOWNLOAD_DIR ); } |
---|
209 | QString getError( ) const; |
---|
210 | QString hashString( ) const { return getString( HASH_STRING ); } |
---|
211 | QString scrapeResponse( ) const { return getString( SCRAPE_RESPONSE ); } |
---|
212 | QString announceResponse( ) const { return getString( ANNOUNCE_RESPONSE ); } |
---|
213 | QString announceUrl( ) const { return getString( ANNOUNCE_URL ); } |
---|
214 | QString torrentFile( ) const { return getString( TORRENT_FILE ); } |
---|
215 | bool hasError( ) const { return !getError( ).isEmpty( ); } |
---|
216 | bool isDone( ) const { return getSize( LEFT_UNTIL_DONE ) == 0; } |
---|
217 | bool isSeed( ) const { return haveVerified() >= totalSize(); } |
---|
218 | bool isPrivate( ) const { return getBool( IS_PRIVATE ); } |
---|
219 | bool getSeedRatio( double& setme ) const; |
---|
220 | uint64_t haveVerified( ) const { return getSize( HAVE_VERIFIED ); } |
---|
221 | uint64_t haveUnverified( ) const { return getSize( HAVE_UNCHECKED ); } |
---|
222 | uint64_t desiredAvailable( ) const { return getSize( DESIRED_AVAILABLE ); } |
---|
223 | uint64_t haveTotal( ) const { return haveVerified( ) + haveUnverified(); } |
---|
224 | uint64_t totalSize( ) const { return getSize( TOTAL_SIZE ); } |
---|
225 | uint64_t sizeWhenDone( ) const { return getSize( SIZE_WHEN_DONE ); } |
---|
226 | uint64_t leftUntilDone( ) const { return getSize( LEFT_UNTIL_DONE ); } |
---|
227 | uint64_t pieceSize( ) const { return getSize( PIECE_SIZE ); } |
---|
228 | bool hasMetadata( ) const { return getDouble( METADATA_PERCENT_DONE ) >= 1.0; } |
---|
229 | int pieceCount( ) const { return getInt( PIECE_COUNT ); } |
---|
230 | double ratio( ) const { return getDouble( RATIO ); } |
---|
231 | double percentDone( ) const { return getDouble( PERCENT_DONE ); } |
---|
232 | double metadataPercentDone( ) const { return getDouble( METADATA_PERCENT_DONE ); } |
---|
233 | uint64_t downloadedEver( ) const { return getSize( DOWNLOADED_EVER ); } |
---|
234 | uint64_t uploadedEver( ) const { return getSize( UPLOADED_EVER ); } |
---|
235 | uint64_t failedEver( ) const { return getSize( FAILED_EVER ); } |
---|
236 | int compareTracker( const Torrent& ) const; |
---|
237 | int compareRatio( const Torrent& ) const; |
---|
238 | int compareETA( const Torrent& ) const; |
---|
239 | bool hasETA( ) const { return getETA( ) >= 0; } |
---|
240 | int getETA( ) const { return getInt( ETA ); } |
---|
241 | QDateTime lastActivity( ) const { return getDateTime( DATE_ACTIVITY ); } |
---|
242 | QDateTime lastStarted( ) const { return getDateTime( DATE_STARTED ); } |
---|
243 | QDateTime dateAdded( ) const { return getDateTime( DATE_ADDED ); } |
---|
244 | QDateTime dateCreated( ) const { return getDateTime( DATE_CREATED ); } |
---|
245 | QDateTime lastAnnounceTime( ) const { return getDateTime( LAST_ANNOUNCE_TIME ); } |
---|
246 | QDateTime lastScrapeTime( ) const { return getDateTime( LAST_SCRAPE_TIME ); } |
---|
247 | QDateTime manualAnnounceTime( ) const { return getDateTime( MANUAL_ANNOUNCE_TIME ); } |
---|
248 | QDateTime nextAnnounceTime( ) const { return getDateTime( NEXT_ANNOUNCE_TIME ); } |
---|
249 | QDateTime nextScrapeTime( ) const { return getDateTime( NEXT_SCRAPE_TIME ); } |
---|
250 | bool canManualAnnounce( ) const { return isReadyToTransfer() && (manualAnnounceTime()<=QDateTime::currentDateTime()); } |
---|
251 | int peersWeAreDownloadingFrom( ) const { return getInt( PEERS_SENDING_TO_US ) + getInt( WEBSEEDS_SENDING_TO_US ); } |
---|
252 | int peersWeAreUploadingTo( ) const { return getInt( PEERS_GETTING_FROM_US ); } |
---|
253 | bool isUploading( ) const { return peersWeAreUploadingTo( ) > 0; } |
---|
254 | int connectedPeers( ) const { return getInt( PEERS_CONNECTED ); } |
---|
255 | int connectedPeersAndWebseeds( ) const { return connectedPeers( ) + getInt( WEBSEEDS_SENDING_TO_US ); } |
---|
256 | Speed downloadSpeed( ) const { return Speed::fromBps( getInt( DOWNLOAD_SPEED ) ); } |
---|
257 | Speed uploadSpeed( ) const { return Speed::fromBps( getInt( UPLOAD_SPEED ) ); } |
---|
258 | double getVerifyProgress( ) const { return getDouble( PERCENT_VERIFIED ); } |
---|
259 | bool hasFileSubstring( const QString& substr ) const; |
---|
260 | bool hasTrackerSubstring( const QString& substr ) const; |
---|
261 | Speed uploadLimit( ) const { return Speed::fromKbps( getInt( UP_LIMIT ) ); } |
---|
262 | Speed downloadLimit( ) const { return Speed::fromKbps( getInt( DOWN_LIMIT ) ); } |
---|
263 | bool uploadIsLimited( ) const { return getBool( UP_LIMITED ); } |
---|
264 | bool downloadIsLimited( ) const { return getBool( DOWN_LIMITED ); } |
---|
265 | bool honorsSessionLimits( ) const { return getBool( HONORS_SESSION_LIMITS ); } |
---|
266 | int peerLimit( ) const { return getInt( PEER_LIMIT ); } |
---|
267 | double seedRatioLimit( ) const { return getDouble( SEED_RATIO_LIMIT ); } |
---|
268 | tr_ratiolimit seedRatioMode( ) const { return (tr_ratiolimit) getInt( SEED_RATIO_MODE ); } |
---|
269 | PeerList peers( ) const{ return myValues[PEERS].value<PeerList>(); } |
---|
270 | const FileList& files( ) const { return myFiles; } |
---|
271 | |
---|
272 | public: |
---|
273 | QString activityString( ) const; |
---|
274 | tr_torrent_activity getActivity( ) const { return (tr_torrent_activity) getInt( ACTIVITY ); } |
---|
275 | bool isPaused( ) const { return getActivity( ) == TR_STATUS_STOPPED; } |
---|
276 | bool isVerifying( ) const { return getActivity( ) == TR_STATUS_CHECK; } |
---|
277 | bool isDownloading( ) const { return getActivity( ) == TR_STATUS_DOWNLOAD; } |
---|
278 | bool isReadyToTransfer( ) const { return getActivity()==TR_STATUS_DOWNLOAD || getActivity()==TR_STATUS_SEED; } |
---|
279 | void notifyComplete( ) const; |
---|
280 | |
---|
281 | public: |
---|
282 | void update( tr_benc * dict ); |
---|
283 | |
---|
284 | private: |
---|
285 | const char * getMimeTypeString( ) const; |
---|
286 | void updateMimeIcon( ); |
---|
287 | |
---|
288 | public: |
---|
289 | QIcon getMimeTypeIcon( ) const { return getIcon( MIME_ICON ); } |
---|
290 | |
---|
291 | private: |
---|
292 | Prefs& myPrefs; |
---|
293 | FileList myFiles; |
---|
294 | }; |
---|
295 | |
---|
296 | Q_DECLARE_METATYPE(const Torrent*) |
---|
297 | |
---|
298 | #endif |
---|
299 | |
---|