From 6759446639d6be0f159371390168c27a739919a6 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 28 Dec 2018 13:38:08 +0800 Subject: [PATCH] Cleanup code Also remove redundant code, it is already handled correctly in subsequent function calls. --- src/base/bittorrent/session.cpp | 11 ++++++----- src/base/bittorrent/session.h | 2 +- src/gui/addnewtorrentdialog.cpp | 34 ++++++++++++++++----------------- src/gui/addnewtorrentdialog.h | 4 ++-- src/gui/mainwindow.cpp | 7 +------ 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index c9e0d442c..fd3278db3 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -2090,12 +2090,9 @@ TorrentStatusReport Session::torrentStatusReport() const return m_torrentStatusReport; } -// source - .torrent file path/url or magnet uri -bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) +bool Session::addTorrent(const QString &source, const AddTorrentParams ¶ms) { - MagnetUri magnetUri(source); - if (magnetUri.isValid()) - return addTorrent_impl(CreateTorrentParams(params), magnetUri); + // `source`: .torrent file path/url or magnet uri if (Utils::Misc::isUrl(source)) { LogMsg(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(source)); @@ -2110,6 +2107,10 @@ bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) return true; } + const MagnetUri magnetUri {source}; + if (magnetUri.isValid()) + return addTorrent_impl(CreateTorrentParams(params), magnetUri); + TorrentFileGuard guard(source); if (addTorrent_impl(CreateTorrentParams(params) , MagnetUri(), TorrentInfo::loadFromFile(source))) { diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 3b863b737..873421d26 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -469,7 +469,7 @@ namespace BitTorrent void banIP(const QString &ip); bool isKnownTorrent(const InfoHash &hash) const; - bool addTorrent(QString source, const AddTorrentParams ¶ms = AddTorrentParams()); + bool addTorrent(const QString &source, const AddTorrentParams ¶ms = AddTorrentParams()); bool addTorrent(const TorrentInfo &torrentInfo, const AddTorrentParams ¶ms = AddTorrentParams()); bool deleteTorrent(const QString &hash, bool deleteLocalFiles = false); bool loadMetadata(const MagnetUri &magnetUri); diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 132207714..784cb953c 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -227,7 +227,7 @@ void AddNewTorrentDialog::saveState() settings()->storeValue(KEY_EXPANDED, m_ui->toolButtonAdvanced->isChecked()); } -void AddNewTorrentDialog::show(QString source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent) +void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent) { AddNewTorrentDialog *dlg = new AddNewTorrentDialog(inParams, parent); @@ -240,27 +240,27 @@ void AddNewTorrentDialog::show(QString source, const BitTorrent::AddTorrentParam , dlg, &AddNewTorrentDialog::handleDownloadFinished); connect(handler, &Net::DownloadHandler::downloadFailed, dlg, &AddNewTorrentDialog::handleDownloadFailed); connect(handler, &Net::DownloadHandler::redirectedToMagnet, dlg, &AddNewTorrentDialog::handleRedirectedToMagnet); + return; + } + + const BitTorrent::MagnetUri magnetUri(source); + const bool isLoaded = magnetUri.isValid() + ? dlg->loadMagnet(magnetUri) + : dlg->loadTorrent(source); + + if (isLoaded) { +#ifdef Q_OS_MAC + dlg->exec(); +#else + dlg->open(); +#endif } else { - bool ok = false; - BitTorrent::MagnetUri magnetUri(source); - if (magnetUri.isValid()) - ok = dlg->loadMagnet(magnetUri); - else - ok = dlg->loadTorrent(source); - - if (ok) -#ifdef Q_OS_MAC - dlg->exec(); -#else - dlg->open(); -#endif - else - delete dlg; + delete dlg; } } -void AddNewTorrentDialog::show(QString source, QWidget *parent) +void AddNewTorrentDialog::show(const QString &source, QWidget *parent) { show(source, BitTorrent::AddTorrentParams(), parent); } diff --git a/src/gui/addnewtorrentdialog.h b/src/gui/addnewtorrentdialog.h index e2636dfe1..6a9e80434 100644 --- a/src/gui/addnewtorrentdialog.h +++ b/src/gui/addnewtorrentdialog.h @@ -68,8 +68,8 @@ public: static int savePathHistoryLength(); static void setSavePathHistoryLength(int value); - static void show(QString source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent); - static void show(QString source, QWidget *parent); + static void show(const QString &source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent); + static void show(const QString &source, QWidget *parent); private slots: void showAdvancedSettings(bool show); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index bf70bc83b..64b03877e 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -1628,11 +1627,7 @@ void MainWindow::showNotificationBaloon(QString title, QString msg) const void MainWindow::downloadFromURLList(const QStringList &urlList) { const bool useTorrentAdditionDialog = AddNewTorrentDialog::isEnabled(); - for (QString url : urlList) { - if (((url.size() == 40) && !url.contains(QRegularExpression("[^0-9A-Fa-f]"))) - || ((url.size() == 32) && !url.contains(QRegularExpression("[^2-7A-Za-z]")))) - url = "magnet:?xt=urn:btih:" + url; - + for (const QString &url : urlList) { if (useTorrentAdditionDialog) AddNewTorrentDialog::show(url, this); else