From 79cdad47f106b2c84d3b888ff8e4541c452d0e8b Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 26 Feb 2011 19:56:15 +0000 Subject: [PATCH] Code optimization --- src/qtlibtorrent/qbtsession.cpp | 39 ++++++++++++++-------------- src/qtlibtorrent/qbtsession.h | 46 ++++++++++++++++----------------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 1c3f89d7f..202ab8851 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -617,7 +617,7 @@ void QBtSession::useAlternativeSpeedsLimit(bool alternative) { } // Return the torrent handle, given its hash -QTorrentHandle QBtSession::getTorrentHandle(QString hash) const{ +QTorrentHandle QBtSession::getTorrentHandle(const QString &hash) const{ return QTorrentHandle(s->find_torrent(misc::QStringToSha1(hash))); } @@ -654,7 +654,7 @@ void QBtSession::banIP(QString ip) { // Delete a torrent from the session, given its hash // permanent = true means that the torrent will be removed from the hard-drive too -void QBtSession::deleteTorrent(QString hash, bool delete_local_files) { +void QBtSession::deleteTorrent(const QString &hash, bool delete_local_files) { qDebug("Deleting torrent with hash: %s", qPrintable(hash)); const QTorrentHandle h = getTorrentHandle(hash); if(!h.is_valid()) { @@ -742,7 +742,7 @@ void QBtSession::resumeAllTorrents() { } } -void QBtSession::pauseTorrent(QString hash) { +void QBtSession::pauseTorrent(const QString &hash) { QTorrentHandle h = getTorrentHandle(hash); if(!h.is_paused()) { h.pause(); @@ -750,7 +750,7 @@ void QBtSession::pauseTorrent(QString hash) { } } -void QBtSession::resumeTorrent(QString hash) { +void QBtSession::resumeTorrent(const QString &hash) { QTorrentHandle h = getTorrentHandle(hash); if(h.is_paused()) { h.resume(); @@ -758,7 +758,7 @@ void QBtSession::resumeTorrent(QString hash) { } } -bool QBtSession::loadFastResumeData(QString hash, std::vector &buf) { +bool QBtSession::loadFastResumeData(const QString &hash, std::vector &buf) { const QString fastresume_path = QDir(misc::BTBackupLocation()).absoluteFilePath(hash+QString(".fastresume")); qDebug("Trying to load fastresume data: %s", qPrintable(fastresume_path)); QFile fastresume_file(fastresume_path); @@ -770,7 +770,7 @@ bool QBtSession::loadFastResumeData(QString hash, std::vector &buf) { return true; } -void QBtSession::loadTorrentSettings(QTorrentHandle h) { +void QBtSession::loadTorrentSettings(QTorrentHandle& h) { Preferences pref; // Connections limit per torrent h.set_max_connections(pref.getMaxConnecsPerTorrent()); @@ -935,7 +935,8 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr } // Check if the torrent contains trackers or url seeds we don't know about // and add them - mergeTorrents(getTorrentHandle(hash), t); + QTorrentHandle h_ex = getTorrentHandle(hash); + mergeTorrents(h_ex, t); // Delete file if temporary if(!from_url.isNull() || fromScanDir) misc::safeRemove(path); @@ -1094,7 +1095,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr return h; } -void QBtSession::exportTorrentFile(QTorrentHandle h) { +void QBtSession::exportTorrentFile(const QTorrentHandle &h) { Q_ASSERT(torrentExport); QString torrent_path = QDir(misc::BTBackupLocation()).absoluteFilePath(h.hash()+".torrent"); QDir exportPath(Preferences().getExportDir()); @@ -1109,7 +1110,7 @@ void QBtSession::exportTorrentFile(QTorrentHandle h) { } } -add_torrent_params QBtSession::initializeAddTorrentParams(QString hash) { +add_torrent_params QBtSession::initializeAddTorrentParams(const QString &hash) { add_torrent_params p; // Seeding mode @@ -1137,7 +1138,7 @@ add_torrent_params QBtSession::initializeAddTorrentParams(QString hash) { return p; } -void QBtSession::loadTorrentTempData(QTorrentHandle h, QString savePath, bool magnet) { +void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool magnet) { qDebug("loadTorrentTempdata() - ENTER"); const QString hash = h.hash(); // Sequential download @@ -1189,7 +1190,7 @@ void QBtSession::loadTorrentTempData(QTorrentHandle h, QString savePath, bool ma TorrentPersistentData::saveTorrentPersistentData(h, savePath, magnet); } -void QBtSession::mergeTorrents(QTorrentHandle h_ex, boost::intrusive_ptr t) { +void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr t) { // Check if the torrent contains trackers or url seeds we don't know about // and add them if(!h_ex.is_valid()) return; @@ -1432,7 +1433,7 @@ bool QBtSession::enableDHT(bool b) { return true; } -qreal QBtSession::getRealRatio(QString hash) const{ +qreal QBtSession::getRealRatio(const QString &hash) const{ QTorrentHandle h = getTorrentHandle(hash); if(!h.is_valid()) { return 0.; @@ -1562,7 +1563,7 @@ void QBtSession::addPeerBanMessage(QString ip, bool from_ipfilter) { emit newBanMessage(msg); } -bool QBtSession::isFilePreviewPossible(QString hash) const{ +bool QBtSession::isFilePreviewPossible(const QString &hash) const{ // See if there are supported files in the torrent const QTorrentHandle h = getTorrentHandle(hash); if(!h.is_valid() || !h.has_metadata()) { @@ -1916,7 +1917,7 @@ void QBtSession::cleanUpAutoRunProcess(int) { sender()->deleteLater(); } -void QBtSession::autoRunExternalProgram(QTorrentHandle h, bool async) { +void QBtSession::autoRunExternalProgram(const QTorrentHandle &h, bool async) { if(!h.is_valid()) return; QString program = Preferences().getAutoRunProgram().trimmed(); if(program.isEmpty()) return; @@ -1937,7 +1938,7 @@ void QBtSession::autoRunExternalProgram(QTorrentHandle h, bool async) { } } -void QBtSession::sendNotificationEmail(QTorrentHandle h) { +void QBtSession::sendNotificationEmail(const QTorrentHandle &h) { // Prepare mail content QString content = tr("Torrent name: %1").arg(h.name()) + "\n"; content += tr("Torrent size: %1").arg(misc::friendlyUnit(h.actual_size())) + "\n"; @@ -2375,7 +2376,7 @@ void QBtSession::readAlerts() { } } -void QBtSession::recheckTorrent(QString hash) { +void QBtSession::recheckTorrent(const QString &hash) { QTorrentHandle h = getTorrentHandle(hash); if(h.is_valid() && h.has_metadata()) { if(h.is_paused()) { @@ -2388,7 +2389,7 @@ void QBtSession::recheckTorrent(QString hash) { } } -QHash QBtSession::getTrackersInfo(QString hash) const{ +QHash QBtSession::getTrackersInfo(const QString &hash) const{ return trackersInfos.value(hash, QHash()); } @@ -2401,7 +2402,7 @@ session_status QBtSession::getSessionStatus() const{ return s->status(); } -QString QBtSession::getSavePath(QString hash, bool fromScanDir, QString filePath, QString root_folder) { +QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString filePath, QString root_folder) { QString savePath; if(TorrentTempData::hasTempData(hash)) { savePath = TorrentTempData::getSavePath(hash); @@ -2454,7 +2455,7 @@ QString QBtSession::getSavePath(QString hash, bool fromScanDir, QString filePath // Take an url string to a torrent file, // download the torrent file to a tmp location, then // add it to download list -void QBtSession::downloadFromUrl(QString url) { +void QBtSession::downloadFromUrl(const QString &url) { addConsoleMessage(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(url) #ifndef DISABLE_GUI , QPalette::WindowText diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index b36d8e07f..35059efc3 100644 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -31,7 +31,6 @@ #define __BITTORRENT_H__ #include -#include #include #include #ifdef DISABLE_GUI @@ -76,20 +75,19 @@ public: static QBtSession* instance(); static void drop(); ~QBtSession(); - QTorrentHandle getTorrentHandle(QString hash) const; + QTorrentHandle getTorrentHandle(const QString &hash) const; std::vector getTorrents() const; - bool isFilePreviewPossible(QString fileHash) const; + bool isFilePreviewPossible(const QString& hash) const; qreal getPayloadDownloadRate() const; qreal getPayloadUploadRate() const; libtorrent::session_status getSessionStatus() const; int getListenPort() const; - qreal getRealRatio(QString hash) const; - QHash getTrackersInfo(QString hash) const; + qreal getRealRatio(const QString& hash) const; + QHash getTrackersInfo(const QString &hash) const; bool hasActiveTorrents() const; bool hasDownloadingTorrents() const; //int getMaximumActiveDownloads() const; //int getMaximumActiveTorrents() const; - int loadTorrentPriority(QString hash); inline QStringList getConsoleMessages() const { return consoleMessages; } inline QStringList getPeerBanMessages() const { return peerBanMessages; } inline libtorrent::session* getSession() const { return s; } @@ -106,16 +104,16 @@ public slots: QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); void loadSessionState(); void saveSessionState(); - void downloadFromUrl(QString url); - void deleteTorrent(QString hash, bool delete_local_files = false); + void downloadFromUrl(const QString &url); + void deleteTorrent(const QString &hash, bool delete_local_files = false); void startUpTorrents(); - void recheckTorrent(QString hash); + void recheckTorrent(const QString &hash); void useAlternativeSpeedsLimit(bool alternative); qlonglong getETA(const QString& hash) const; /* Needed by Web UI */ void pauseAllTorrents(); - void pauseTorrent(QString hash); - void resumeTorrent(QString hash); + void pauseTorrent(const QString &hash); + void resumeTorrent(const QString &hash); void resumeAllTorrents(); /* End Web UI */ void preAllocateAllFiles(bool b); @@ -165,11 +163,11 @@ public slots: void recursiveTorrentDownload(const QTorrentHandle &h); private: - QString getSavePath(QString hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null); - bool loadFastResumeData(QString hash, std::vector &buf); - void loadTorrentSettings(QTorrentHandle h); - void loadTorrentTempData(QTorrentHandle h, QString savePath, bool magnet); - libtorrent::add_torrent_params initializeAddTorrentParams(QString hash); + QString getSavePath(const QString &hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null); + bool loadFastResumeData(const QString &hash, std::vector &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 &t, const std::vector &fp); private slots: @@ -178,27 +176,27 @@ private slots: void processBigRatios(); void exportTorrentFiles(QString path); void saveTempFastResumeData(); - void sendNotificationEmail(QTorrentHandle h); - void autoRunExternalProgram(QTorrentHandle h, bool async=true); + void sendNotificationEmail(const QTorrentHandle &h); + void autoRunExternalProgram(const QTorrentHandle &h, bool async=true); void cleanUpAutoRunProcess(int); - void mergeTorrents(QTorrentHandle h_ex, boost::intrusive_ptr t); - void exportTorrentFile(QTorrentHandle h); + void mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr t); + void exportTorrentFile(const QTorrentHandle &h); void initWebUi(); void handleIPFilterParsed(int ruleCount); void handleIPFilterError(); signals: void addedTorrent(const QTorrentHandle& h); - void deletedTorrent(QString hash); + void deletedTorrent(const QString &hash); void torrentAboutToBeRemoved(const QTorrentHandle &h); void pausedTorrent(const QTorrentHandle& h); void resumedTorrent(const QTorrentHandle& h); void finishedTorrent(const QTorrentHandle& h); void fullDiskError(const QTorrentHandle& h, QString msg); - void trackerError(QString hash, QString time, QString msg); + void trackerError(const QString &hash, QString time, QString msg); void trackerAuthenticationRequired(const QTorrentHandle& h); void newDownloadedTorrent(QString path, QString url); - void updateFileSize(QString hash); + void updateFileSize(const QString &hash); void downloadFromUrlFailure(QString url, QString reason); void torrentFinishedChecking(const QTorrentHandle& h); void metadataReceived(const QTorrentHandle &h); @@ -219,7 +217,7 @@ private: libtorrent::session *s; QPointer timerAlerts; QPointer bd_scheduler; - QMap > savepathLabel_fromurl; + QHash > savepathLabel_fromurl; QHash > trackersInfos; QHash savePathsToRemove; QStringList torrentsToPausedAfterChecking;