qBittorrent/src/qtlibtorrent/qbtsession.h

277 lines
10 KiB
C
Raw Normal View History

/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#ifndef __BITTORRENT_H__
#define __BITTORRENT_H__
2007-06-29 19:23:15 +04:00
#include <QHash>
#include <QUrl>
2007-07-23 16:46:36 +04:00
#include <QStringList>
2010-01-03 01:20:37 +03:00
#ifdef DISABLE_GUI
#include <QCoreApplication>
2010-01-03 01:20:37 +03:00
#else
#include <QApplication>
#include <QPalette>
2010-01-03 01:20:37 +03:00
#endif
2008-09-13 11:33:41 +04:00
#include <QPointer>
2010-03-21 00:21:42 +03:00
#include <QTimer>
2007-06-29 19:23:15 +04:00
#include <libtorrent/version.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/ip_filter.hpp>
2010-10-09 18:32:00 +04:00
#include "qtracker.h"
#include "qtorrenthandle.h"
2010-10-09 18:32:00 +04:00
#include "trackerinfos.h"
#define MAX_SAMPLES 20
2011-01-24 20:58:57 +03:00
class DownloadThread;
class FilterParserThread;
2009-11-18 20:46:59 +03:00
class HttpServer;
class BandwidthScheduler;
class ScanFoldersModel;
2010-12-18 18:34:38 +03:00
class TorrentSpeedMonitor;
2010-10-17 18:46:01 +04:00
class QBtSession : public QObject {
Q_OBJECT
Q_DISABLE_COPY(QBtSession)
2011-02-24 20:36:20 +03:00
public:
static const qreal MAX_RATIO;
2011-02-24 20:36:20 +03:00
private:
explicit QBtSession();
static QBtSession* m_instance;
enum shutDownAction { NO_SHUTDOWN, SHUTDOWN_COMPUTER, SUSPEND_COMPUTER };
2009-11-19 11:14:04 +03:00
public:
static QBtSession* instance();
static void drop();
2010-10-17 18:46:01 +04:00
~QBtSession();
2011-02-26 22:56:15 +03:00
QTorrentHandle getTorrentHandle(const QString &hash) const;
std::vector<libtorrent::torrent_handle> getTorrents() const;
2011-02-26 22:56:15 +03:00
bool isFilePreviewPossible(const QString& hash) const;
2011-01-25 20:04:55 +03:00
qreal getPayloadDownloadRate() const;
qreal getPayloadUploadRate() const;
libtorrent::session_status getSessionStatus() const;
2009-11-19 11:14:04 +03:00
int getListenPort() const;
2011-02-26 22:56:15 +03:00
qreal getRealRatio(const QString& hash) const;
QHash<QString, TrackerInfos> getTrackersInfo(const QString &hash) const;
2009-11-19 11:14:04 +03:00
bool hasActiveTorrents() const;
bool hasDownloadingTorrents() const;
//int getMaximumActiveDownloads() const;
//int getMaximumActiveTorrents() const;
inline QStringList getConsoleMessages() const { return consoleMessages; }
inline QStringList getPeerBanMessages() const { return peerBanMessages; }
inline libtorrent::session* getSession() const { return s; }
inline bool useTemporaryFolder() const { return !defaultTempPath.isEmpty(); }
inline QString getDefaultSavePath() const { return defaultSavePath; }
inline ScanFoldersModel* getScanFoldersModel() const { return m_scanFolders; }
inline bool isDHTEnabled() const { return DHTEnabled; }
inline bool isLSDEnabled() const { return LSDEnabled; }
inline bool isPexEnabled() const { return PeXEnabled; }
inline bool isQueueingEnabled() const { return queueingEnabled; }
2009-11-19 11:14:04 +03:00
public slots:
QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false);
QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false);
void loadSessionState();
void saveSessionState();
2011-02-26 22:56:15 +03:00
void downloadFromUrl(const QString &url);
void deleteTorrent(const QString &hash, bool delete_local_files = false);
2009-11-19 11:14:04 +03:00
void startUpTorrents();
2011-02-26 22:56:15 +03:00
void recheckTorrent(const QString &hash);
void useAlternativeSpeedsLimit(bool alternative);
2010-12-18 18:34:38 +03:00
qlonglong getETA(const QString& hash) const;
2009-11-19 11:14:04 +03:00
/* Needed by Web UI */
void pauseAllTorrents();
2011-02-26 22:56:15 +03:00
void pauseTorrent(const QString &hash);
void resumeTorrent(const QString &hash);
2009-11-19 11:14:04 +03:00
void resumeAllTorrents();
/* End Web UI */
void preAllocateAllFiles(bool b);
void saveFastResumeData();
void enableIPFilter(const QString &filter_path, bool force=false);
2009-11-19 11:14:04 +03:00
void disableIPFilter();
void setQueueingEnabled(bool enable);
void handleDownloadFailure(QString url, QString reason);
2010-11-22 22:07:04 +03:00
void downloadUrlAndSkipDialog(QString url, QString save_path=QString(), QString label=QString());
2009-11-19 11:14:04 +03:00
// Session configuration - Setters
void setListeningPort(int port);
void setMaxConnections(int maxConnec);
void setMaxConnectionsPerTorrent(int max);
void setMaxUploadsPerTorrent(int max);
void setDownloadRateLimit(long rate);
void setUploadRateLimit(long rate);
void setGlobalMaxRatio(qreal ratio);
qreal getGlobalMaxRatio() const { return global_ratio_limit; }
void setMaxRatioPerTorrent(const QString &hash, qreal ratio);
qreal getMaxRatioPerTorrent(const QString &hash, bool *usesGlobalRatio) const;
void removeRatioPerTorrent(const QString &hash);
2009-11-19 11:14:04 +03:00
void setDHTPort(int dht_port);
void setProxySettings(const libtorrent::proxy_settings &proxySettings);
void setSessionSettings(const libtorrent::session_settings &sessionSettings);
2009-11-19 11:14:04 +03:00
void startTorrentsInPause(bool b);
void setDefaultTempPath(QString temppath);
void setAppendLabelToSavePath(bool append);
2010-11-24 23:31:14 +03:00
void appendLabelToTorrentSavePath(const QTorrentHandle &h);
void changeLabelInTorrentSavePath(const QTorrentHandle &h, QString old_label, QString new_label);
#if LIBTORRENT_VERSION_MINOR > 14
2010-11-24 23:31:14 +03:00
void appendqBextensionToTorrent(const QTorrentHandle &h, bool append);
void setAppendqBExtension(bool append);
#endif
void applyEncryptionSettings(libtorrent::pe_settings se);
2009-11-19 11:14:04 +03:00
void setDownloadLimit(QString hash, long val);
void setUploadLimit(QString hash, long val);
void enableUPnP(bool b);
void enableLSD(bool b);
bool enableDHT(bool b);
2010-01-03 01:20:37 +03:00
#ifdef DISABLE_GUI
void addConsoleMessage(QString msg, QString color=QString::null);
#else
2009-11-19 11:14:04 +03:00
void addConsoleMessage(QString msg, QColor color=QApplication::palette().color(QPalette::WindowText));
2010-01-03 01:20:37 +03:00
#endif
2009-11-19 11:14:04 +03:00
void addPeerBanMessage(QString msg, bool from_ipfilter);
void processDownloadedFile(QString, QString);
void addMagnetSkipAddDlg(QString uri);
void downloadFromURLList(const QStringList& urls);
void configureSession();
void banIP(QString ip);
void recursiveTorrentDownload(const QTorrentHandle &h);
private:
2011-02-26 22:56:15 +03:00
QString getSavePath(const QString &hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null);
bool loadFastResumeData(const QString &hash, std::vector<char> &buf);
void loadTorrentSettings(QTorrentHandle &h);
void loadTorrentTempData(QTorrentHandle &h, QString savePath, bool magnet);
libtorrent::add_torrent_params initializeAddTorrentParams(const QString &hash);
libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr<libtorrent::torrent_info> &t, const std::vector<int> &fp);
void updateRatioTimer();
2010-03-03 22:04:34 +03:00
private slots:
2009-11-19 11:14:04 +03:00
void addTorrentsFromScanFolder(QStringList&);
void readAlerts();
void processBigRatios();
void exportTorrentFiles(QString path);
2010-03-21 00:21:42 +03:00
void saveTempFastResumeData();
2011-02-26 22:56:15 +03:00
void sendNotificationEmail(const QTorrentHandle &h);
void autoRunExternalProgram(const QTorrentHandle &h, bool async=true);
void cleanUpAutoRunProcess(int);
2011-02-26 22:56:15 +03:00
void mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t);
void exportTorrentFile(const QTorrentHandle &h);
void initWebUi();
void handleIPFilterParsed(int ruleCount);
void handleIPFilterError();
2009-11-19 11:14:04 +03:00
signals:
void addedTorrent(const QTorrentHandle& h);
2011-02-26 22:56:15 +03:00
void deletedTorrent(const QString &hash);
void torrentAboutToBeRemoved(const QTorrentHandle &h);
2010-11-24 23:31:14 +03:00
void pausedTorrent(const QTorrentHandle& h);
void resumedTorrent(const QTorrentHandle& h);
void finishedTorrent(const QTorrentHandle& h);
void fullDiskError(const QTorrentHandle& h, QString msg);
2011-02-26 22:56:15 +03:00
void trackerError(const QString &hash, QString time, QString msg);
2010-11-24 23:31:14 +03:00
void trackerAuthenticationRequired(const QTorrentHandle& h);
2009-11-19 11:14:04 +03:00
void newDownloadedTorrent(QString path, QString url);
2011-02-26 22:56:15 +03:00
void updateFileSize(const QString &hash);
2009-11-19 11:14:04 +03:00
void downloadFromUrlFailure(QString url, QString reason);
2010-11-24 23:31:14 +03:00
void torrentFinishedChecking(const QTorrentHandle& h);
void metadataReceived(const QTorrentHandle &h);
void savePathChanged(const QTorrentHandle &h);
void newConsoleMessage(const QString &msg);
void newBanMessage(const QString &msg);
void alternativeSpeedsModeChanged(bool alternative);
2010-11-24 23:31:14 +03:00
void recursiveTorrentDownloadPossible(const QTorrentHandle &h);
void ipFilterParsed(bool error, int ruleCount);
2010-03-03 22:04:34 +03:00
2010-11-15 20:15:50 +03:00
private:
#if LIBTORRENT_VERSION_MINOR < 15
void saveDHTEntry();
#endif
2010-03-03 22:04:34 +03:00
private:
// Bittorrent
libtorrent::session *s;
2010-03-03 22:04:34 +03:00
QPointer<QTimer> timerAlerts;
QPointer<BandwidthScheduler> bd_scheduler;
2011-03-03 22:20:49 +03:00
QMap<QUrl, QPair<QString, QString> > savepathLabel_fromurl; // Use QMap for compatibility with Qt < 4.7: qHash(QUrl)
2010-03-03 22:04:34 +03:00
QHash<QString, QHash<QString, TrackerInfos> > trackersInfos;
QHash<QString, QString> savePathsToRemove;
2010-03-03 22:04:34 +03:00
QStringList torrentsToPausedAfterChecking;
2010-03-21 00:21:42 +03:00
QTimer resumeDataTimer;
2010-03-03 22:04:34 +03:00
// Ratio
QPointer<QTimer> BigRatioTimer;
// HTTP
2011-01-24 20:58:57 +03:00
DownloadThread* downloader;
2010-03-03 22:04:34 +03:00
// File System
ScanFoldersModel *m_scanFolders;
// Console / Log
QStringList consoleMessages;
QStringList peerBanMessages;
// Settings
bool preAllocateAll;
bool addInPause;
qreal global_ratio_limit;
int high_ratio_action;
2010-03-03 22:04:34 +03:00
bool LSDEnabled;
bool DHTEnabled;
int current_dht_port;
bool PeXEnabled;
bool queueingEnabled;
bool appendLabelToSavePath;
bool torrentExport;
#if LIBTORRENT_VERSION_MINOR > 14
2010-03-03 22:04:34 +03:00
bool appendqBExtension;
#endif
QString defaultSavePath;
QString defaultTempPath;
// IP filtering
QPointer<FilterParserThread> filterParser;
QString filterPath;
// Web UI
QPointer<HttpServer> httpServer;
QList<QUrl> url_skippingDlg;
// GeoIP
#ifndef DISABLE_GUI
bool geoipDBLoaded;
bool resolve_countries;
#endif
// Tracker
QPointer<QTracker> m_tracker;
2010-12-18 18:34:38 +03:00
TorrentSpeedMonitor *m_speedMonitor;
shutDownAction m_shutdownAct;
// Port forwarding
libtorrent::upnp *m_upnp;
libtorrent::natpmp *m_natpmp;
};
#endif