Browse Source

Code optimization

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
79cdad47f1
  1. 39
      src/qtlibtorrent/qbtsession.cpp
  2. 46
      src/qtlibtorrent/qbtsession.h

39
src/qtlibtorrent/qbtsession.cpp

@ -617,7 +617,7 @@ void QBtSession::useAlternativeSpeedsLimit(bool alternative) {
} }
// Return the torrent handle, given its hash // 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))); 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 // Delete a torrent from the session, given its hash
// permanent = true means that the torrent will be removed from the hard-drive too // 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)); qDebug("Deleting torrent with hash: %s", qPrintable(hash));
const QTorrentHandle h = getTorrentHandle(hash); const QTorrentHandle h = getTorrentHandle(hash);
if(!h.is_valid()) { 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); QTorrentHandle h = getTorrentHandle(hash);
if(!h.is_paused()) { if(!h.is_paused()) {
h.pause(); 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); QTorrentHandle h = getTorrentHandle(hash);
if(h.is_paused()) { if(h.is_paused()) {
h.resume(); h.resume();
@ -758,7 +758,7 @@ void QBtSession::resumeTorrent(QString hash) {
} }
} }
bool QBtSession::loadFastResumeData(QString hash, std::vector<char> &buf) { bool QBtSession::loadFastResumeData(const QString &hash, std::vector<char> &buf) {
const QString fastresume_path = QDir(misc::BTBackupLocation()).absoluteFilePath(hash+QString(".fastresume")); const QString fastresume_path = QDir(misc::BTBackupLocation()).absoluteFilePath(hash+QString(".fastresume"));
qDebug("Trying to load fastresume data: %s", qPrintable(fastresume_path)); qDebug("Trying to load fastresume data: %s", qPrintable(fastresume_path));
QFile fastresume_file(fastresume_path); QFile fastresume_file(fastresume_path);
@ -770,7 +770,7 @@ bool QBtSession::loadFastResumeData(QString hash, std::vector<char> &buf) {
return true; return true;
} }
void QBtSession::loadTorrentSettings(QTorrentHandle h) { void QBtSession::loadTorrentSettings(QTorrentHandle& h) {
Preferences pref; Preferences pref;
// Connections limit per torrent // Connections limit per torrent
h.set_max_connections(pref.getMaxConnecsPerTorrent()); 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 // Check if the torrent contains trackers or url seeds we don't know about
// and add them // and add them
mergeTorrents(getTorrentHandle(hash), t); QTorrentHandle h_ex = getTorrentHandle(hash);
mergeTorrents(h_ex, t);
// Delete file if temporary // Delete file if temporary
if(!from_url.isNull() || fromScanDir) misc::safeRemove(path); if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
@ -1094,7 +1095,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
return h; return h;
} }
void QBtSession::exportTorrentFile(QTorrentHandle h) { void QBtSession::exportTorrentFile(const QTorrentHandle &h) {
Q_ASSERT(torrentExport); Q_ASSERT(torrentExport);
QString torrent_path = QDir(misc::BTBackupLocation()).absoluteFilePath(h.hash()+".torrent"); QString torrent_path = QDir(misc::BTBackupLocation()).absoluteFilePath(h.hash()+".torrent");
QDir exportPath(Preferences().getExportDir()); 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; add_torrent_params p;
// Seeding mode // Seeding mode
@ -1137,7 +1138,7 @@ add_torrent_params QBtSession::initializeAddTorrentParams(QString hash) {
return p; return p;
} }
void QBtSession::loadTorrentTempData(QTorrentHandle h, QString savePath, bool magnet) { void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool magnet) {
qDebug("loadTorrentTempdata() - ENTER"); qDebug("loadTorrentTempdata() - ENTER");
const QString hash = h.hash(); const QString hash = h.hash();
// Sequential download // Sequential download
@ -1189,7 +1190,7 @@ void QBtSession::loadTorrentTempData(QTorrentHandle h, QString savePath, bool ma
TorrentPersistentData::saveTorrentPersistentData(h, savePath, magnet); TorrentPersistentData::saveTorrentPersistentData(h, savePath, magnet);
} }
void QBtSession::mergeTorrents(QTorrentHandle h_ex, boost::intrusive_ptr<torrent_info> t) { void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torrent_info> t) {
// Check if the torrent contains trackers or url seeds we don't know about // Check if the torrent contains trackers or url seeds we don't know about
// and add them // and add them
if(!h_ex.is_valid()) return; if(!h_ex.is_valid()) return;
@ -1432,7 +1433,7 @@ bool QBtSession::enableDHT(bool b) {
return true; return true;
} }
qreal QBtSession::getRealRatio(QString hash) const{ qreal QBtSession::getRealRatio(const QString &hash) const{
QTorrentHandle h = getTorrentHandle(hash); QTorrentHandle h = getTorrentHandle(hash);
if(!h.is_valid()) { if(!h.is_valid()) {
return 0.; return 0.;
@ -1562,7 +1563,7 @@ void QBtSession::addPeerBanMessage(QString ip, bool from_ipfilter) {
emit newBanMessage(msg); 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 // See if there are supported files in the torrent
const QTorrentHandle h = getTorrentHandle(hash); const QTorrentHandle h = getTorrentHandle(hash);
if(!h.is_valid() || !h.has_metadata()) { if(!h.is_valid() || !h.has_metadata()) {
@ -1916,7 +1917,7 @@ void QBtSession::cleanUpAutoRunProcess(int) {
sender()->deleteLater(); sender()->deleteLater();
} }
void QBtSession::autoRunExternalProgram(QTorrentHandle h, bool async) { void QBtSession::autoRunExternalProgram(const QTorrentHandle &h, bool async) {
if(!h.is_valid()) return; if(!h.is_valid()) return;
QString program = Preferences().getAutoRunProgram().trimmed(); QString program = Preferences().getAutoRunProgram().trimmed();
if(program.isEmpty()) return; 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 // Prepare mail content
QString content = tr("Torrent name: %1").arg(h.name()) + "\n"; QString content = tr("Torrent name: %1").arg(h.name()) + "\n";
content += tr("Torrent size: %1").arg(misc::friendlyUnit(h.actual_size())) + "\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); QTorrentHandle h = getTorrentHandle(hash);
if(h.is_valid() && h.has_metadata()) { if(h.is_valid() && h.has_metadata()) {
if(h.is_paused()) { if(h.is_paused()) {
@ -2388,7 +2389,7 @@ void QBtSession::recheckTorrent(QString hash) {
} }
} }
QHash<QString, TrackerInfos> QBtSession::getTrackersInfo(QString hash) const{ QHash<QString, TrackerInfos> QBtSession::getTrackersInfo(const QString &hash) const{
return trackersInfos.value(hash, QHash<QString, TrackerInfos>()); return trackersInfos.value(hash, QHash<QString, TrackerInfos>());
} }
@ -2401,7 +2402,7 @@ session_status QBtSession::getSessionStatus() const{
return s->status(); 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; QString savePath;
if(TorrentTempData::hasTempData(hash)) { if(TorrentTempData::hasTempData(hash)) {
savePath = TorrentTempData::getSavePath(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, // Take an url string to a torrent file,
// download the torrent file to a tmp location, then // download the torrent file to a tmp location, then
// add it to download list // 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) addConsoleMessage(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(url)
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
, QPalette::WindowText , QPalette::WindowText

46
src/qtlibtorrent/qbtsession.h

@ -31,7 +31,6 @@
#define __BITTORRENT_H__ #define __BITTORRENT_H__
#include <QHash> #include <QHash>
#include <QMap>
#include <QUrl> #include <QUrl>
#include <QStringList> #include <QStringList>
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
@ -76,20 +75,19 @@ public:
static QBtSession* instance(); static QBtSession* instance();
static void drop(); static void drop();
~QBtSession(); ~QBtSession();
QTorrentHandle getTorrentHandle(QString hash) const; QTorrentHandle getTorrentHandle(const QString &hash) const;
std::vector<libtorrent::torrent_handle> getTorrents() const; std::vector<libtorrent::torrent_handle> getTorrents() const;
bool isFilePreviewPossible(QString fileHash) const; bool isFilePreviewPossible(const QString& hash) const;
qreal getPayloadDownloadRate() const; qreal getPayloadDownloadRate() const;
qreal getPayloadUploadRate() const; qreal getPayloadUploadRate() const;
libtorrent::session_status getSessionStatus() const; libtorrent::session_status getSessionStatus() const;
int getListenPort() const; int getListenPort() const;
qreal getRealRatio(QString hash) const; qreal getRealRatio(const QString& hash) const;
QHash<QString, TrackerInfos> getTrackersInfo(QString hash) const; QHash<QString, TrackerInfos> getTrackersInfo(const QString &hash) const;
bool hasActiveTorrents() const; bool hasActiveTorrents() const;
bool hasDownloadingTorrents() const; bool hasDownloadingTorrents() const;
//int getMaximumActiveDownloads() const; //int getMaximumActiveDownloads() const;
//int getMaximumActiveTorrents() const; //int getMaximumActiveTorrents() const;
int loadTorrentPriority(QString hash);
inline QStringList getConsoleMessages() const { return consoleMessages; } inline QStringList getConsoleMessages() const { return consoleMessages; }
inline QStringList getPeerBanMessages() const { return peerBanMessages; } inline QStringList getPeerBanMessages() const { return peerBanMessages; }
inline libtorrent::session* getSession() const { return s; } inline libtorrent::session* getSession() const { return s; }
@ -106,16 +104,16 @@ public slots:
QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false);
void loadSessionState(); void loadSessionState();
void saveSessionState(); void saveSessionState();
void downloadFromUrl(QString url); void downloadFromUrl(const QString &url);
void deleteTorrent(QString hash, bool delete_local_files = false); void deleteTorrent(const QString &hash, bool delete_local_files = false);
void startUpTorrents(); void startUpTorrents();
void recheckTorrent(QString hash); void recheckTorrent(const QString &hash);
void useAlternativeSpeedsLimit(bool alternative); void useAlternativeSpeedsLimit(bool alternative);
qlonglong getETA(const QString& hash) const; qlonglong getETA(const QString& hash) const;
/* Needed by Web UI */ /* Needed by Web UI */
void pauseAllTorrents(); void pauseAllTorrents();
void pauseTorrent(QString hash); void pauseTorrent(const QString &hash);
void resumeTorrent(QString hash); void resumeTorrent(const QString &hash);
void resumeAllTorrents(); void resumeAllTorrents();
/* End Web UI */ /* End Web UI */
void preAllocateAllFiles(bool b); void preAllocateAllFiles(bool b);
@ -165,11 +163,11 @@ public slots:
void recursiveTorrentDownload(const QTorrentHandle &h); void recursiveTorrentDownload(const QTorrentHandle &h);
private: private:
QString getSavePath(QString hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null); QString getSavePath(const QString &hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null);
bool loadFastResumeData(QString hash, std::vector<char> &buf); bool loadFastResumeData(const QString &hash, std::vector<char> &buf);
void loadTorrentSettings(QTorrentHandle h); void loadTorrentSettings(QTorrentHandle &h);
void loadTorrentTempData(QTorrentHandle h, QString savePath, bool magnet); void loadTorrentTempData(QTorrentHandle &h, QString savePath, bool magnet);
libtorrent::add_torrent_params initializeAddTorrentParams(QString hash); libtorrent::add_torrent_params initializeAddTorrentParams(const QString &hash);
libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr<libtorrent::torrent_info> &t, const std::vector<int> &fp); libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr<libtorrent::torrent_info> &t, const std::vector<int> &fp);
private slots: private slots:
@ -178,27 +176,27 @@ private slots:
void processBigRatios(); void processBigRatios();
void exportTorrentFiles(QString path); void exportTorrentFiles(QString path);
void saveTempFastResumeData(); void saveTempFastResumeData();
void sendNotificationEmail(QTorrentHandle h); void sendNotificationEmail(const QTorrentHandle &h);
void autoRunExternalProgram(QTorrentHandle h, bool async=true); void autoRunExternalProgram(const QTorrentHandle &h, bool async=true);
void cleanUpAutoRunProcess(int); void cleanUpAutoRunProcess(int);
void mergeTorrents(QTorrentHandle h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t); void mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t);
void exportTorrentFile(QTorrentHandle h); void exportTorrentFile(const QTorrentHandle &h);
void initWebUi(); void initWebUi();
void handleIPFilterParsed(int ruleCount); void handleIPFilterParsed(int ruleCount);
void handleIPFilterError(); void handleIPFilterError();
signals: signals:
void addedTorrent(const QTorrentHandle& h); void addedTorrent(const QTorrentHandle& h);
void deletedTorrent(QString hash); void deletedTorrent(const QString &hash);
void torrentAboutToBeRemoved(const QTorrentHandle &h); void torrentAboutToBeRemoved(const QTorrentHandle &h);
void pausedTorrent(const QTorrentHandle& h); void pausedTorrent(const QTorrentHandle& h);
void resumedTorrent(const QTorrentHandle& h); void resumedTorrent(const QTorrentHandle& h);
void finishedTorrent(const QTorrentHandle& h); void finishedTorrent(const QTorrentHandle& h);
void fullDiskError(const QTorrentHandle& h, QString msg); 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 trackerAuthenticationRequired(const QTorrentHandle& h);
void newDownloadedTorrent(QString path, QString url); void newDownloadedTorrent(QString path, QString url);
void updateFileSize(QString hash); void updateFileSize(const QString &hash);
void downloadFromUrlFailure(QString url, QString reason); void downloadFromUrlFailure(QString url, QString reason);
void torrentFinishedChecking(const QTorrentHandle& h); void torrentFinishedChecking(const QTorrentHandle& h);
void metadataReceived(const QTorrentHandle &h); void metadataReceived(const QTorrentHandle &h);
@ -219,7 +217,7 @@ private:
libtorrent::session *s; libtorrent::session *s;
QPointer<QTimer> timerAlerts; QPointer<QTimer> timerAlerts;
QPointer<BandwidthScheduler> bd_scheduler; QPointer<BandwidthScheduler> bd_scheduler;
QMap<QUrl, QPair<QString, QString> > savepathLabel_fromurl; QHash<QUrl, QPair<QString, QString> > savepathLabel_fromurl;
QHash<QString, QHash<QString, TrackerInfos> > trackersInfos; QHash<QString, QHash<QString, TrackerInfos> > trackersInfos;
QHash<QString, QString> savePathsToRemove; QHash<QString, QString> savePathsToRemove;
QStringList torrentsToPausedAfterChecking; QStringList torrentsToPausedAfterChecking;

Loading…
Cancel
Save