File indexing completed on 2024-04-21 04:57:13

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2007 - 2010 Lukas Appelhans <l.appelhans@gmx.de>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 
0011 #ifndef BTTRANSFER_H
0012 #define BTTRANSFER_H
0013 
0014 #include "core/transfer.h"
0015 #include "torrent/torrentcontrol.h"
0016 
0017 #include <interfaces/monitorinterface.h>
0018 
0019 #include <QTimer>
0020 
0021 class FileModel;
0022 
0023 namespace bt
0024 {
0025 class ChunkDownloadInterface;
0026 class TorrentFileInterface;
0027 class PeerInterface;
0028 }
0029 
0030 class BTTransfer : public Transfer, public bt::MonitorInterface
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     /**
0036      * Here we define the Bittorrent specific flags.
0037      */
0038     enum BTTransferChange {
0039         Tc_ChunksTotal = 0x00010000,
0040         Tc_ChunksDownloaded = 0x00020000,
0041         Tc_ChunksExcluded = 0x00040000,
0042         Tc_ChunksLeft = 0x00080000,
0043         Tc_SeedsConnected = 0x00100000,
0044         Tc_SeedsDisconnected = 0x00200000,
0045         Tc_LeechesConnected = 0x00400000,
0046         Tc_LeechesDisconnected = 0x00800000,
0047         Tc_SessionBytesDownloaded = 0x04000000,
0048         Tc_SessionBytesUploaded = 0x08000000,
0049         Tc_TrackersList = 0x10000000
0050     };
0051 
0052     enum ErrorId {
0053         TorrentFileNotFoundError = 1,
0054     };
0055 
0056     BTTransfer(TransferGroup *parent, TransferFactory *factory, Scheduler *scheduler, const QUrl &src, const QUrl &dest, const QDomElement *e = nullptr);
0057     ~BTTransfer() override;
0058 
0059     void deinit(Transfer::DeleteOptions options) override;
0060 
0061     // Job virtual functions
0062     void start() override;
0063     void stop() override;
0064     int elapsedTime() const override;
0065     int remainingTime() const override;
0066     bool isStalled() const override;
0067     bool isWorking() const override;
0068 
0069     /**
0070      * Open a file dialog to select a new torrent file
0071      */
0072     void resolveError(int errorId) override;
0073 
0074     /**
0075      * @returns the directory the Transfer will be stored to
0076      */
0077     QUrl directory() const override
0078     {
0079         return m_directory;
0080     }
0081 
0082     /**
0083      * Move the download to the new destination
0084      * @param newDirectory is a directory where the download should be stored
0085      * @returns true if newDestination can be used
0086      */
0087     bool setDirectory(const QUrl &newDirectory) override;
0088 
0089     QList<QUrl> files() const override;
0090 
0091     FileModel *fileModel() override;
0092 
0093     // Bittorrent specific functions (connected with TransferFlags
0094     int chunksTotal() const;
0095     int chunksDownloaded() const;
0096     int chunksExcluded() const;
0097     int chunksLeft() const;
0098     int seedsConnected() const;
0099     int seedsDisconnected() const;
0100     int leechesConnected() const;
0101     int leechesDisconnected() const;
0102     int sessionBytesDownloaded() const;
0103     int sessionBytesUploaded() const;
0104     QList<QUrl> trackersList() const;
0105     bt::TorrentControl *torrentControl();
0106 
0107     // More Bittorrent-Functions
0108     void setPort(int port);
0109     void addTracker(const QString &url);
0110     // void save(const QDomElement &element);
0111 
0112     bool ready();
0113 
0114 protected:
0115     void load(const QDomElement *element) override;
0116     void setSpeedLimits(int ulLimit, int dlLimit) override;
0117 
0118 private Q_SLOTS:
0119     void btTransferInit(const QUrl &src = QUrl(), const QByteArray &data = QByteArray());
0120     void update();
0121     void slotStoppedByError(const bt::TorrentInterface *&error, const QString &errormsg);
0122     void slotDownloadFinished(bt::TorrentInterface *ti);
0123     void newDestResult();
0124     void filesSelected();
0125 
0126 private:
0127     void startTorrent();
0128     void stopTorrent();
0129     void updateTorrent();
0130     void updateFilesStatus();
0131 
0132     // bt::MonitorInterface functions
0133     void downloadRemoved(bt::ChunkDownloadInterface *cd) override;
0134     void downloadStarted(bt::ChunkDownloadInterface *cd) override;
0135     void peerAdded(bt::PeerInterface *peer) override;
0136     void peerRemoved(bt::PeerInterface *peer) override;
0137     void stopped() override;
0138     void destroyed() override;
0139     void filePercentageChanged(bt::TorrentFileInterface *, float) override
0140     {
0141     }
0142     void filePreviewChanged(bt::TorrentFileInterface *, bool) override
0143     {
0144     }
0145 
0146     bt::TorrentControl *torrent;
0147     QUrl m_directory;
0148     QString m_tmp;
0149     QString m_tmpTorrentFile;
0150     float m_ratio;
0151     QTimer timer;
0152     bool m_ready;
0153     bool m_downloadFinished;
0154     bool m_movingFile;
0155     FileModel *m_fileModel;
0156     QHash<QUrl, bt::TorrentFileInterface *> m_files;
0157     int m_updateCounter;
0158 };
0159 
0160 #endif