From d40495a4bebeba561f4dcd04b01adf157d66b763 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Fri, 25 Sep 2015 10:52:25 +0300 Subject: [PATCH] Simplify Session/TorrentHandle classes. --- src/core/bittorrent/private/sessionprivate.h | 81 ------------------- .../bittorrent/private/torrenthandleprivate.h | 49 ----------- src/core/bittorrent/session.cpp | 10 +-- src/core/bittorrent/session.h | 60 +++++++------- src/core/bittorrent/torrenthandle.cpp | 2 +- src/core/bittorrent/torrenthandle.h | 21 +++-- src/core/core.pri | 2 - 7 files changed, 47 insertions(+), 178 deletions(-) delete mode 100644 src/core/bittorrent/private/sessionprivate.h delete mode 100644 src/core/bittorrent/private/torrenthandleprivate.h diff --git a/src/core/bittorrent/private/sessionprivate.h b/src/core/bittorrent/private/sessionprivate.h deleted file mode 100644 index 9f77beafa..000000000 --- a/src/core/bittorrent/private/sessionprivate.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev - * - * 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. - */ - -#ifndef SESSIONPRIVATE_H -#define SESSIONPRIVATE_H - -class QString; -class QUrl; -template class QList; - -namespace libtorrent -{ - class entry; -} - -namespace BitTorrent -{ - class TorrentHandle; - class TrackerEntry; -} - -struct SessionPrivate -{ - virtual bool isQueueingEnabled() const = 0; - virtual bool isTempPathEnabled() const = 0; - virtual bool isAppendExtensionEnabled() const = 0; - virtual bool useAppendLabelToSavePath() const = 0; - virtual QString defaultSavePath() const = 0; - virtual QString tempPath() const = 0; - virtual qreal globalMaxRatio() const = 0; - - virtual void handleTorrentRatioLimitChanged(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentSavePathChanged(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentLabelChanged(BitTorrent::TorrentHandle *const torrent, const QString &oldLabel) = 0; - virtual void handleTorrentMetadataReceived(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentPaused(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentResumed(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentChecked(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentFinished(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentTrackersAdded(BitTorrent::TorrentHandle *const torrent, const QList &newTrackers) = 0; - virtual void handleTorrentTrackersRemoved(BitTorrent::TorrentHandle *const torrent, const QList &deletedTrackers) = 0; - virtual void handleTorrentTrackersChanged(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentUrlSeedsAdded(BitTorrent::TorrentHandle *const torrent, const QList &newUrlSeeds) = 0; - virtual void handleTorrentUrlSeedsRemoved(BitTorrent::TorrentHandle *const torrent, const QList &urlSeeds) = 0; - virtual void handleTorrentResumeDataReady(BitTorrent::TorrentHandle *const torrent, const libtorrent::entry &data) = 0; - virtual void handleTorrentResumeDataFailed(BitTorrent::TorrentHandle *const torrent) = 0; - virtual void handleTorrentTrackerReply(BitTorrent::TorrentHandle *const torrent, const QString &trackerUrl) = 0; - virtual void handleTorrentTrackerWarning(BitTorrent::TorrentHandle *const torrent, const QString &trackerUrl) = 0; - virtual void handleTorrentTrackerError(BitTorrent::TorrentHandle *const torrent, const QString &trackerUrl) = 0; - virtual void handleTorrentTrackerAuthenticationRequired(BitTorrent::TorrentHandle *const torrent, const QString &trackerUrl) = 0; - -protected: - ~SessionPrivate() {} -}; - -#endif // SESSIONPRIVATE_H diff --git a/src/core/bittorrent/private/torrenthandleprivate.h b/src/core/bittorrent/private/torrenthandleprivate.h deleted file mode 100644 index 907a28a23..000000000 --- a/src/core/bittorrent/private/torrenthandleprivate.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev - * - * 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. - */ - -#ifndef TORRENTHANDLEPRIVATE_H -#define TORRENTHANDLEPRIVATE_H - -namespace libtorrent -{ - class alert; - struct torrent_status; -} - -struct TorrentHandlePrivate -{ - virtual void handleAlert(libtorrent::alert *) = 0; - virtual void handleStateUpdate(const libtorrent::torrent_status &) = 0; - virtual void handleTempPathChanged() = 0; - virtual void handleAppendExtensionToggled() = 0; - -protected: - ~TorrentHandlePrivate() {} -}; - -#endif // TORRENTHANDLEPRIVATE_H diff --git a/src/core/bittorrent/session.cpp b/src/core/bittorrent/session.cpp index a025b92d3..5a490d145 100644 --- a/src/core/bittorrent/session.cpp +++ b/src/core/bittorrent/session.cpp @@ -1397,7 +1397,7 @@ void Session::saveResumeData() switch (a->type()) { case libt::save_resume_data_failed_alert::alert_type: case libt::save_resume_data_alert::alert_type: - TorrentHandlePrivate *torrent = m_torrents.take(static_cast(a)->handle.info_hash()); + TorrentHandle *torrent = m_torrents.take(static_cast(a)->handle.info_hash()); if (torrent) torrent->handleAlert(a); break; @@ -1429,7 +1429,7 @@ void Session::setDefaultTempPath(const QString &path) if (m_tempPath != tempPath) { m_tempPath = tempPath; - foreach (TorrentHandlePrivate *const torrent, m_torrents) + foreach (TorrentHandle *const torrent, m_torrents) torrent->handleTempPathChanged(); } } @@ -1460,7 +1460,7 @@ void Session::setAppendExtension(bool append) if (m_appendExtension != append) { m_appendExtension = append; // append or remove .!qB extension for incomplete files - foreach (TorrentHandlePrivate *const torrent, m_torrents) + foreach (TorrentHandle *const torrent, m_torrents) torrent->handleAppendExtensionToggled(); } } @@ -2098,7 +2098,7 @@ void Session::handleAlert(libt::alert *a) void Session::dispatchTorrentAlert(libt::alert *a) { - TorrentHandlePrivate *const torrent = m_torrents.value(static_cast(a)->handle.info_hash()); + TorrentHandle *const torrent = m_torrents.value(static_cast(a)->handle.info_hash()); if (torrent) torrent->handleAlert(a); } @@ -2321,7 +2321,7 @@ void Session::handleStateUpdateAlert(libt::state_update_alert *p) foreach (const libt::torrent_status &status, p->status) { TorrentHandle *const torrent = m_torrents.value(status.info_hash); if (torrent) { - static_cast(torrent)->handleStateUpdate(status); + torrent->handleStateUpdate(status); emit torrentStatusUpdated(torrent); } } diff --git a/src/core/bittorrent/session.h b/src/core/bittorrent/session.h index ab96af405..b0d046a80 100644 --- a/src/core/bittorrent/session.h +++ b/src/core/bittorrent/session.h @@ -40,12 +40,12 @@ #include "core/tristatebool.h" #include "core/types.h" -#include "private/sessionprivate.h" #include "torrentinfo.h" namespace libtorrent { class session; + class entry; struct add_torrent_params; struct pe_settings; struct proxy_settings; @@ -85,10 +85,11 @@ namespace libtorrent struct external_ip_alert; } -QT_BEGIN_NAMESPACE class QTimer; class QStringList; -QT_END_NAMESPACE +class QString; +class QUrl; +template class QList; class FilterParserThread; class BandwidthScheduler; @@ -136,7 +137,7 @@ namespace BitTorrent TorrentStatusReport(); }; - class Session : public QObject, public SessionPrivate + class Session : public QObject { Q_OBJECT Q_DISABLE_COPY(Session) @@ -151,6 +152,11 @@ namespace BitTorrent bool isPexEnabled() const; bool isQueueingEnabled() const; qreal globalMaxRatio() const; + bool isTempPathEnabled() const; + bool isAppendExtensionEnabled() const; + bool useAppendLabelToSavePath() const; + QString defaultSavePath() const; + QString tempPath() const; TorrentHandle *findTorrent(const InfoHash &hash) const; QHash torrents() const; @@ -185,6 +191,27 @@ namespace BitTorrent void topTorrentsPriority(const QStringList &hashes); void bottomTorrentsPriority(const QStringList &hashes); + // TorrentHandle interface + void handleTorrentRatioLimitChanged(TorrentHandle *const torrent); + void handleTorrentSavePathChanged(TorrentHandle *const torrent); + void handleTorrentLabelChanged(TorrentHandle *const torrent, const QString &oldLabel); + void handleTorrentMetadataReceived(TorrentHandle *const torrent); + void handleTorrentPaused(TorrentHandle *const torrent); + void handleTorrentResumed(TorrentHandle *const torrent); + void handleTorrentChecked(TorrentHandle *const torrent); + void handleTorrentFinished(TorrentHandle *const torrent); + void handleTorrentTrackersAdded(TorrentHandle *const torrent, const QList &newTrackers); + void handleTorrentTrackersRemoved(TorrentHandle *const torrent, const QList &deletedTrackers); + void handleTorrentTrackersChanged(TorrentHandle *const torrent); + void handleTorrentUrlSeedsAdded(TorrentHandle *const torrent, const QList &newUrlSeeds); + void handleTorrentUrlSeedsRemoved(TorrentHandle *const torrent, const QList &urlSeeds); + void handleTorrentResumeDataReady(TorrentHandle *const torrent, const libtorrent::entry &data); + void handleTorrentResumeDataFailed(TorrentHandle *const torrent); + void handleTorrentTrackerReply(TorrentHandle *const torrent, const QString &trackerUrl); + void handleTorrentTrackerWarning(TorrentHandle *const torrent, const QString &trackerUrl); + void handleTorrentTrackerError(TorrentHandle *const torrent, const QString &trackerUrl); + void handleTorrentTrackerAuthenticationRequired(TorrentHandle *const torrent, const QString &trackerUrl); + signals: void torrentsUpdated(const BitTorrent::TorrentStatusReport &torrentStatusReport = BitTorrent::TorrentStatusReport()); void addTorrentFailed(const QString &error); @@ -287,31 +314,6 @@ namespace BitTorrent void handleListenFailedAlert(libtorrent::listen_failed_alert *p); void handleExternalIPAlert(libtorrent::external_ip_alert *p); - bool isTempPathEnabled() const; - bool isAppendExtensionEnabled() const; - bool useAppendLabelToSavePath() const; - QString defaultSavePath() const; - QString tempPath() const; - void handleTorrentRatioLimitChanged(TorrentHandle *const torrent); - void handleTorrentSavePathChanged(TorrentHandle *const torrent); - void handleTorrentLabelChanged(TorrentHandle *const torrent, const QString &oldLabel); - void handleTorrentMetadataReceived(TorrentHandle *const torrent); - void handleTorrentPaused(TorrentHandle *const torrent); - void handleTorrentResumed(TorrentHandle *const torrent); - void handleTorrentChecked(TorrentHandle *const torrent); - void handleTorrentFinished(TorrentHandle *const torrent); - void handleTorrentTrackersAdded(TorrentHandle *const torrent, const QList &newTrackers); - void handleTorrentTrackersRemoved(TorrentHandle *const torrent, const QList &deletedTrackers); - void handleTorrentTrackersChanged(TorrentHandle *const torrent); - void handleTorrentUrlSeedsAdded(TorrentHandle *const torrent, const QList &newUrlSeeds); - void handleTorrentUrlSeedsRemoved(TorrentHandle *const torrent, const QList &urlSeeds); - void handleTorrentResumeDataReady(TorrentHandle *const torrent, const libtorrent::entry &data); - void handleTorrentResumeDataFailed(TorrentHandle *const torrent); - void handleTorrentTrackerReply(TorrentHandle *const torrent, const QString &trackerUrl); - void handleTorrentTrackerWarning(TorrentHandle *const torrent, const QString &trackerUrl); - void handleTorrentTrackerError(TorrentHandle *const torrent, const QString &trackerUrl); - void handleTorrentTrackerAuthenticationRequired(TorrentHandle *const torrent, const QString &trackerUrl); - void saveResumeData(); bool writeResumeDataFile(TorrentHandle *const torrent, const libtorrent::entry &data); diff --git a/src/core/bittorrent/torrenthandle.cpp b/src/core/bittorrent/torrenthandle.cpp index 3179603bc..e741bd39e 100644 --- a/src/core/bittorrent/torrenthandle.cpp +++ b/src/core/bittorrent/torrenthandle.cpp @@ -1399,7 +1399,7 @@ void TorrentHandle::handleTorrentFinishedAlert(libtorrent::torrent_finished_aler forceRecheck(); if (isMoveInProgress() || m_renameCount > 0) - m_moveFinishedTriggers.append(boost::bind(&SessionPrivate::handleTorrentFinished, m_session, this)); + m_moveFinishedTriggers.append(boost::bind(&Session::handleTorrentFinished, m_session, this)); else m_session->handleTorrentFinished(this); } diff --git a/src/core/bittorrent/torrenthandle.h b/src/core/bittorrent/torrenthandle.h index 7548aee6e..4f14e92f2 100644 --- a/src/core/bittorrent/torrenthandle.h +++ b/src/core/bittorrent/torrenthandle.h @@ -42,7 +42,6 @@ #include "core/tristatebool.h" #include "private/speedmonitor.h" -#include "private/torrenthandleprivate.h" #include "infohash.h" #include "torrentinfo.h" @@ -70,10 +69,9 @@ namespace libtorrent struct tracker_reply_alert; struct tracker_warning_alert; struct fastresume_rejected_alert; + struct torrent_status; } -struct SessionPrivate; - namespace BitTorrent { struct PeerAddress; @@ -153,7 +151,7 @@ namespace BitTorrent int m_value; }; - class TorrentHandle : public QObject, public TorrentHandlePrivate + class TorrentHandle : public QObject { Q_DISABLE_COPY(TorrentHandle) @@ -293,10 +291,16 @@ namespace BitTorrent QString toMagnetUri() const; bool needSaveResumeData() const; - void saveResumeData(); + // Session interface libtorrent::torrent_handle nativeHandle() const; + void handleAlert(libtorrent::alert *a); + void handleStateUpdate(const libtorrent::torrent_status &nativeStatus); + void handleTempPathChanged(); + void handleAppendExtensionToggled(); + void saveResumeData(); + private: typedef boost::function EventTrigger; @@ -306,11 +310,6 @@ namespace BitTorrent void updateState(); void updateTorrentInfo(); - void handleAlert(libtorrent::alert *a); - void handleStateUpdate(const libtorrent::torrent_status &nativeStatus); - void handleTempPathChanged(); - void handleAppendExtensionToggled(); - void handleStorageMovedAlert(libtorrent::storage_moved_alert *p); void handleStorageMovedFailedAlert(libtorrent::storage_moved_failed_alert *p); void handleTrackerReplyAlert(libtorrent::tracker_reply_alert *p); @@ -342,7 +341,7 @@ namespace BitTorrent bool addUrlSeed(const QUrl &urlSeed); bool removeUrlSeed(const QUrl &urlSeed); - SessionPrivate *const m_session; + Session *const m_session; libtorrent::torrent_handle m_nativeHandle; libtorrent::torrent_status m_nativeStatus; TorrentState m_state; diff --git a/src/core/core.pri b/src/core/core.pri index 2852c1236..bb4643dd9 100644 --- a/src/core/core.pri +++ b/src/core/core.pri @@ -32,8 +32,6 @@ HEADERS += \ $$PWD/bittorrent/trackerentry.h \ $$PWD/bittorrent/tracker.h \ $$PWD/bittorrent/torrentcreatorthread.h \ - $$PWD/bittorrent/private/sessionprivate.h \ - $$PWD/bittorrent/private/torrenthandleprivate.h \ $$PWD/bittorrent/private/speedmonitor.h \ $$PWD/bittorrent/private/bandwidthscheduler.h \ $$PWD/bittorrent/private/filterparserthread.h \