File: Torrent.h

package info (click to toggle)
transmission 3.00-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 33,160 kB
  • sloc: ansic: 81,055; objc: 18,449; cpp: 16,303; sh: 4,470; javascript: 4,281; makefile: 1,081; xml: 139
file content (622 lines) | stat: -rw-r--r-- 11,945 bytes parent folder | download | duplicates (2)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * This file Copyright (C) 2009-2015 Mnemosyne LLC
 *
 * It may be used under the GNU GPL versions 2 or 3
 * or any future license endorsed by Mnemosyne LLC.
 *
 */

#pragma once

#include <ctime> // time_t

#include <QObject>
#include <QIcon>
#include <QMetaType>
#include <QString>
#include <QStringList>
#include <QList>
#include <QVariant>

#include <libtransmission/transmission.h>
#include <libtransmission/quark.h>

#include "CustomVariantType.h"
#include "Speed.h"

#ifdef ERROR
#undef ERROR
#endif

class QPixmap;

class Prefs;

extern "C"
{
struct tr_variant;
}

struct Peer
{
    bool clientIsChoked;
    bool clientIsInterested;
    bool isDownloadingFrom;
    bool isEncrypted;
    bool isIncoming;
    bool isUploadingTo;
    bool peerIsChoked;
    bool peerIsInterested;
    QString address;
    QString clientName;
    QString flagStr;
    int port;
    Speed rateToClient;
    Speed rateToPeer;
    double progress;
};

Q_DECLARE_METATYPE(Peer)

typedef QList<Peer> PeerList;
Q_DECLARE_METATYPE(PeerList)

struct TrackerStat
{
    QPixmap getFavicon() const;

    bool hasAnnounced;
    bool hasScraped;
    bool isBackup;
    bool lastAnnounceSucceeded;
    bool lastAnnounceTimedOut;
    bool lastScrapeSucceeded;
    bool lastScrapeTimedOut;
    int announceState;
    int downloadCount;
    int id;
    int lastAnnouncePeerCount;
    int lastAnnounceStartTime;
    int lastAnnounceTime;
    int lastScrapeStartTime;
    int lastScrapeTime;
    int leecherCount;
    int nextAnnounceTime;
    int nextScrapeTime;
    int scrapeState;
    int seederCount;
    int tier;
    QString announce;
    QString host;
    QString lastAnnounceResult;
    QString lastScrapeResult;
};

Q_DECLARE_METATYPE(TrackerStat)

typedef QList<TrackerStat> TrackerStatsList;
Q_DECLARE_METATYPE(TrackerStatsList)

struct TorrentFile
{
    TorrentFile() :
        wanted(true),
        index(-1),
        priority(0),
        size(0),
        have(0)
    {
    }

    bool wanted;
    int index;
    int priority;
    QString filename;
    uint64_t size;
    uint64_t have;
};

Q_DECLARE_METATYPE(TorrentFile)

typedef QList<TorrentFile> FileList;
Q_DECLARE_METATYPE(FileList)

class Torrent : public QObject
{
    Q_OBJECT

public:
    enum
    {
        UPLOAD_SPEED,
        DOWNLOAD_SPEED,
        DOWNLOAD_DIR,
        ACTIVITY,
        NAME,
        ERROR,
        ERROR_STRING,
        SIZE_WHEN_DONE,
        LEFT_UNTIL_DONE,
        HAVE_UNCHECKED,
        HAVE_VERIFIED,
        DESIRED_AVAILABLE,
        TOTAL_SIZE,
        PIECE_SIZE,
        PIECE_COUNT,
        PEERS_GETTING_FROM_US,
        PEERS_SENDING_TO_US,
        WEBSEEDS_SENDING_TO_US,
        PERCENT_DONE,
        METADATA_PERCENT_DONE,
        PERCENT_VERIFIED,
        DATE_ACTIVITY,
        DATE_ADDED,
        DATE_STARTED,
        DATE_CREATED,
        PEERS_CONNECTED,
        ETA,
        DOWNLOADED_EVER,
        UPLOADED_EVER,
        FAILED_EVER,
        TRACKERSTATS,
        MIME_ICON,
        SEED_RATIO_LIMIT,
        SEED_RATIO_MODE,
        SEED_IDLE_LIMIT,
        SEED_IDLE_MODE,
        DOWN_LIMIT,
        DOWN_LIMITED,
        UP_LIMIT,
        UP_LIMITED,
        HONORS_SESSION_LIMITS,
        PEER_LIMIT,
        HASH_STRING,
        IS_FINISHED,
        IS_PRIVATE,
        IS_STALLED,
        COMMENT,
        CREATOR,
        MANUAL_ANNOUNCE_TIME,
        PEERS,
        BANDWIDTH_PRIORITY,
        QUEUE_POSITION,
        EDIT_DATE,
        //
        PROPERTY_COUNT
    };

public:
    Torrent(Prefs const&, int id);

    int getBandwidthPriority() const
    {
        return getInt(BANDWIDTH_PRIORITY);
    }

    int id() const
    {
        return myId;
    }

    QString name() const
    {
        return getString(NAME);
    }

    bool hasName() const
    {
        return !myValues[NAME].isNull();
    }

    QString creator() const
    {
        return getString(CREATOR);
    }

    QString comment() const
    {
        return getString(COMMENT);
    }

    QString getPath() const
    {
        return getString(DOWNLOAD_DIR);
    }

    QString getError() const;

    QString hashString() const
    {
        return getString(HASH_STRING);
    }

    bool hasError() const
    {
        return getInt(ERROR) != TR_STAT_OK;
    }

    bool isDone() const
    {
        return getSize(LEFT_UNTIL_DONE) == 0;
    }

    bool isSeed() const
    {
        return haveVerified() >= totalSize();
    }

    bool isPrivate() const
    {
        return getBool(IS_PRIVATE);
    }

    bool getSeedRatio(double& setme) const;

    uint64_t haveVerified() const
    {
        return getSize(HAVE_VERIFIED);
    }

    uint64_t haveUnverified() const
    {
        return getSize(HAVE_UNCHECKED);
    }

    uint64_t desiredAvailable() const
    {
        return getSize(DESIRED_AVAILABLE);
    }

    uint64_t haveTotal() const
    {
        return haveVerified() + haveUnverified();
    }

    uint64_t totalSize() const
    {
        return getSize(TOTAL_SIZE);
    }

    uint64_t sizeWhenDone() const
    {
        return getSize(SIZE_WHEN_DONE);
    }

    uint64_t leftUntilDone() const
    {
        return getSize(LEFT_UNTIL_DONE);
    }

    uint64_t pieceSize() const
    {
        return getSize(PIECE_SIZE);
    }

    bool hasMetadata() const
    {
        return getDouble(METADATA_PERCENT_DONE) >= 1.0;
    }

    int pieceCount() const
    {
        return getInt(PIECE_COUNT);
    }

    double ratio() const
    {
        auto const u = uploadedEver();
        auto const d = downloadedEver();
        auto const t = totalSize();
        return double(u) / (d ? d : t);
    }

    double percentComplete() const
    {
        return totalSize() != 0 ? haveTotal() / static_cast<double>(totalSize()) : 0;
    }

    double percentDone() const
    {
        auto const l = leftUntilDone();
        auto const s = sizeWhenDone();
        return s ? double(s - l) / s : 0.0;
    }

    double metadataPercentDone() const
    {
        return getDouble(METADATA_PERCENT_DONE);
    }

    uint64_t downloadedEver() const
    {
        return getSize(DOWNLOADED_EVER);
    }

    uint64_t uploadedEver() const
    {
        return getSize(UPLOADED_EVER);
    }

    uint64_t failedEver() const
    {
        return getSize(FAILED_EVER);
    }

    int compareSeedRatio(Torrent const&) const;
    int compareRatio(Torrent const&) const;
    int compareETA(Torrent const&) const;

    bool hasETA() const
    {
        return getETA() >= 0;
    }

    int getETA() const
    {
        return getInt(ETA);
    }

    time_t lastActivity() const
    {
        return getTime(DATE_ACTIVITY);
    }

    time_t lastStarted() const
    {
        return getTime(DATE_STARTED);
    }

    time_t dateAdded() const
    {
        return getTime(DATE_ADDED);
    }

    time_t dateCreated() const
    {
        return getTime(DATE_CREATED);
    }

    time_t manualAnnounceTime() const
    {
        return getTime(MANUAL_ANNOUNCE_TIME);
    }

    bool canManualAnnounceAt(time_t t) const
    {
        return isReadyToTransfer() && (manualAnnounceTime() <= t);
    }

    int peersWeAreDownloadingFrom() const
    {
        return getInt(PEERS_SENDING_TO_US);
    }

    int webseedsWeAreDownloadingFrom() const
    {
        return getInt(WEBSEEDS_SENDING_TO_US);
    }

    int peersWeAreUploadingTo() const
    {
        return getInt(PEERS_GETTING_FROM_US);
    }

    bool isUploading() const
    {
        return peersWeAreUploadingTo() > 0;
    }

    int connectedPeers() const
    {
        return getInt(PEERS_CONNECTED);
    }

    int connectedPeersAndWebseeds() const
    {
        return connectedPeers() + getInt(WEBSEEDS_SENDING_TO_US);
    }

    Speed downloadSpeed() const
    {
        return Speed::fromBps(getSize(DOWNLOAD_SPEED));
    }

    Speed uploadSpeed() const
    {
        return Speed::fromBps(getSize(UPLOAD_SPEED));
    }

    double getVerifyProgress() const
    {
        return getDouble(PERCENT_VERIFIED);
    }

    bool hasTrackerSubstring(QString const& substr) const;

    Speed uploadLimit() const
    {
        return Speed::fromKBps(getInt(UP_LIMIT));
    }

    Speed downloadLimit() const
    {
        return Speed::fromKBps(getInt(DOWN_LIMIT));
    }

    bool uploadIsLimited() const
    {
        return getBool(UP_LIMITED);
    }

    bool downloadIsLimited() const
    {
        return getBool(DOWN_LIMITED);
    }

    bool honorsSessionLimits() const
    {
        return getBool(HONORS_SESSION_LIMITS);
    }

    int peerLimit() const
    {
        return getInt(PEER_LIMIT);
    }

    double seedRatioLimit() const
    {
        return getDouble(SEED_RATIO_LIMIT);
    }

    tr_ratiolimit seedRatioMode() const
    {
        return static_cast<tr_ratiolimit>(getInt(SEED_RATIO_MODE));
    }

    int seedIdleLimit() const
    {
        return getInt(SEED_IDLE_LIMIT);
    }

    tr_idlelimit seedIdleMode() const
    {
        return static_cast<tr_idlelimit>(getInt(SEED_IDLE_MODE));
    }

    TrackerStatsList trackerStats() const
    {
        return myValues[TRACKERSTATS].value<TrackerStatsList>();
    }

    QStringList const& trackers() const
    {
        return trackers_;
    }

    QStringList const& trackerDisplayNames() const
    {
        return trackerDisplayNames_;
    }

    PeerList peers() const
    {
        return myValues[PEERS].value<PeerList>();
    }

    FileList const& files() const
    {
        return myFiles;
    }

    int queuePosition() const
    {
        return getInt(QUEUE_POSITION);
    }

    bool isStalled() const
    {
        return getBool(IS_STALLED);
    }

    QString activityString() const;

    tr_torrent_activity getActivity() const
    {
        return static_cast<tr_torrent_activity>(getInt(ACTIVITY));
    }

    bool isFinished() const
    {
        return getBool(IS_FINISHED);
    }

    bool isPaused() const
    {
        return getActivity() == TR_STATUS_STOPPED;
    }

    bool isWaitingToVerify() const
    {
        return getActivity() == TR_STATUS_CHECK_WAIT;
    }

    bool isVerifying() const
    {
        return getActivity() == TR_STATUS_CHECK;
    }

    bool isDownloading() const
    {
        return getActivity() == TR_STATUS_DOWNLOAD;
    }

    bool isWaitingToDownload() const
    {
        return getActivity() == TR_STATUS_DOWNLOAD_WAIT;
    }

    bool isSeeding() const
    {
        return getActivity() == TR_STATUS_SEED;
    }

    bool isWaitingToSeed() const
    {
        return getActivity() == TR_STATUS_SEED_WAIT;
    }

    bool isReadyToTransfer() const
    {
        return getActivity() == TR_STATUS_DOWNLOAD || getActivity() == TR_STATUS_SEED;
    }

    bool isQueued() const
    {
        return isWaitingToDownload() || isWaitingToSeed();
    }

    bool update(tr_quark const* keys, tr_variant** values, size_t n);

    QIcon getMimeTypeIcon() const
    {
        return getIcon(MIME_ICON);
    }

    using KeyList = QSet<tr_quark>;
    static KeyList const allMainKeys;
    static KeyList const detailInfoKeys;
    static KeyList const detailStatKeys;
    static KeyList const mainInfoKeys;
    static KeyList const mainStatKeys;

private:
    int getInt(int key) const;
    bool getBool(int key) const;
    QIcon getIcon(int key) const;
    double getDouble(int key) const;
    qulonglong getSize(int key) const;
    QString getString(int key) const;
    time_t getTime(int key) const;

    bool setInt(int key, int value);
    bool setBool(int key, bool value);
    bool setIcon(int key, QIcon const&);
    bool setDouble(int key, double);
    bool setString(int key, char const*, size_t len);
    bool setSize(int key, qulonglong);
    bool setTime(int key, time_t);

    QStringList trackers_;
    QStringList trackerDisplayNames_;

    char const* getMimeTypeString() const;
    void updateMimeIcon();

private:
    int const myId;
    Prefs const& myPrefs;
    QVariant myValues[PROPERTY_COUNT];
    FileList myFiles;
};

Q_DECLARE_METATYPE(Torrent const*)