File indexing completed on 2025-01-05 04:37:27

0001 /*
0002     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef BT_TRACKERMANAGER_H
0008 #define BT_TRACKERMANAGER_H
0009 
0010 #include <QDateTime>
0011 #include <QTimer>
0012 #include <interfaces/trackerslist.h>
0013 #include <ktorrent_export.h>
0014 #include <tracker/tracker.h>
0015 #include <util/constants.h>
0016 #include <util/ptrmap.h>
0017 
0018 namespace bt
0019 {
0020 class TorrentControl;
0021 class WaitJob;
0022 class PeerManager;
0023 
0024 /**
0025  * Manages all trackers
0026  */
0027 class KTORRENT_EXPORT TrackerManager : public QObject, public bt::TrackersList, public TrackerDataSource
0028 {
0029     Q_OBJECT
0030 public:
0031     TrackerManager(TorrentControl *tor, PeerManager *pman);
0032     ~TrackerManager() override;
0033 
0034     TrackerInterface *getCurrentTracker() const override;
0035     void setCurrentTracker(TrackerInterface *t) override;
0036     void setCurrentTracker(const QUrl &url) override;
0037     QList<TrackerInterface *> getTrackers() override;
0038     TrackerInterface *addTracker(const QUrl &url, bool custom = true, int tier = 1) override;
0039     bool removeTracker(TrackerInterface *t) override;
0040     bool removeTracker(const QUrl &url) override;
0041     bool canRemoveTracker(TrackerInterface *t) override;
0042     void restoreDefault() override;
0043     void setTrackerEnabled(const QUrl &url, bool on) override;
0044     bool noTrackersReachable() const override;
0045     TrackersStatusInfo getTrackersStatusInfo() const override;
0046 
0047     /// Get the number of seeders
0048     Uint32 getNumSeeders() const;
0049 
0050     /// Get the number of leechers
0051     Uint32 getNumLeechers() const;
0052 
0053     /**
0054      * Start gathering peers
0055      */
0056     virtual void start();
0057 
0058     /**
0059      * Stop gathering peers
0060      * @param wjob WaitJob to wait at exit for the completion of stopped events to the trackers
0061      */
0062     virtual void stop(WaitJob *wjob = nullptr);
0063 
0064     /**
0065      * Notify peersources and trackrs that the download is complete.
0066      */
0067     virtual void completed();
0068 
0069     /**
0070      * Do a manual update on all peer sources and trackers.
0071      */
0072     virtual void manualUpdate();
0073 
0074     /**
0075      * Do a scrape on the current tracker
0076      * */
0077     virtual void scrape();
0078 
0079 protected:
0080     void saveCustomURLs();
0081     void loadCustomURLs();
0082     void saveTrackerStatus();
0083     void loadTrackerStatus();
0084     void addTracker(Tracker *trk);
0085     void switchTracker(Tracker *trk);
0086     Tracker *selectTracker();
0087 
0088     Uint64 bytesDownloaded() const override;
0089     Uint64 bytesUploaded() const override;
0090     Uint64 bytesLeft() const override;
0091     const SHA1Hash &infoHash() const override;
0092     bool isPartialSeed() const override;
0093 
0094 private Q_SLOTS:
0095     /**
0096      * The an error happened contacting the tracker.
0097      * @param err The error
0098      */
0099     void onTrackerError(const QString &err);
0100 
0101     /**
0102      * Tracker update was OK.
0103      * @param
0104      */
0105     void onTrackerOK();
0106 
0107     /**
0108      * Update the current tracker manually
0109      */
0110     void updateCurrentManually();
0111 
0112 protected:
0113     TorrentControl *tor;
0114     PtrMap<QUrl, Tracker> trackers;
0115     bool no_save_custom_trackers;
0116     PeerManager *pman;
0117     Tracker *curr;
0118     QList<QUrl> custom_trackers;
0119     bool started;
0120 };
0121 
0122 }
0123 
0124 #endif // BT_TRACKERMANAGER_H