From 112a9bcfa2158dc5d34ba5c7af918d48b14886d7 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 25 Jun 2018 20:31:32 +0300 Subject: [PATCH] Use new DownloadManager interface --- src/base/bittorrent/session.cpp | 12 +++---- src/base/bittorrent/session.h | 2 +- src/base/net/dnsupdater.cpp | 10 +++--- src/base/net/downloadmanager.cpp | 5 --- src/base/net/downloadmanager.h | 1 - src/base/net/geoipmanager.cpp | 2 +- src/base/rss/rss_feed.cpp | 5 +-- src/base/search/searchpluginmanager.cpp | 4 +-- src/gui/addnewtorrentdialog.cpp | 4 ++- src/gui/mainwindow.cpp | 10 +++--- src/gui/programupdater.cpp | 9 +++--- src/gui/properties/trackersadditiondialog.cpp | 32 ++++++++----------- src/gui/properties/trackersadditiondialog.h | 2 +- src/gui/search/pluginselectdialog.cpp | 3 +- src/gui/transferlistfilterswidget.cpp | 3 +- 15 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 02b7a452c..98c75ffe9 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1844,11 +1844,10 @@ void Session::handleRedirectedToMagnet(const QString &url, const QString &magnet } // Add to BitTorrent session the downloaded torrent file -void Session::handleDownloadFinished(const QString &url, const QString &filePath) +void Session::handleDownloadFinished(const QString &url, const QByteArray &data) { emit downloadFromUrlFinished(url); - addTorrent_impl(m_downloadedTorrents.take(url), MagnetUri(), TorrentInfo::loadFromFile(filePath)); - Utils::Fs::forceRemove(filePath); // remove temporary file + addTorrent_impl(m_downloadedTorrents.take(url), MagnetUri(), TorrentInfo::load(data)); } // Return the torrent handle, given its hash @@ -2077,10 +2076,11 @@ bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) return addTorrent_impl(params, magnetUri); } else if (Utils::Misc::isUrl(source)) { - Logger::instance()->addMessage(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(source)); + LogMsg(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(source)); // Launch downloader - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(source, true, 10485760 /* 10MB */, true); - connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) + Net::DownloadHandler *handler = + Net::DownloadManager::instance()->download(Net::DownloadRequest(source).limit(10485760 /* 10MB */).handleRedirectToMagnet(true)); + connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , this, &Session::handleDownloadFinished); connect(handler, &Net::DownloadHandler::downloadFailed, this, &Session::handleDownloadFailed); connect(handler, &Net::DownloadHandler::redirectedToMagnet, this, &Session::handleRedirectedToMagnet); diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index dec225a73..60c34e46a 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -547,7 +547,7 @@ namespace BitTorrent void generateResumeData(bool final = false); void handleIPFilterParsed(int ruleCount); void handleIPFilterError(); - void handleDownloadFinished(const QString &url, const QString &filePath); + void handleDownloadFinished(const QString &url, const QByteArray &data); void handleDownloadFailed(const QString &url, const QString &reason); void handleRedirectedToMagnet(const QString &url, const QString &magnetUri); diff --git a/src/base/net/dnsupdater.cpp b/src/base/net/dnsupdater.cpp index bcdd5f509..91fd1a377 100644 --- a/src/base/net/dnsupdater.cpp +++ b/src/base/net/dnsupdater.cpp @@ -75,9 +75,8 @@ void DNSUpdater::checkPublicIP() { Q_ASSERT(m_state == OK); - DownloadHandler *handler = DownloadManager::instance()->downloadUrl( - "http://checkip.dyndns.org", false, 0, false, - "qBittorrent/" QBT_VERSION_2); + DownloadHandler *handler = DownloadManager::instance()->download( + DownloadRequest("http://checkip.dyndns.org").userAgent("qBittorrent/" QBT_VERSION_2)); connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , this, &DNSUpdater::ipRequestFinished); connect(handler, &Net::DownloadHandler::downloadFailed, this, &DNSUpdater::ipRequestFailed); @@ -123,9 +122,8 @@ void DNSUpdater::updateDNSService() qDebug() << Q_FUNC_INFO; m_lastIPCheckTime = QDateTime::currentDateTime(); - DownloadHandler *handler = DownloadManager::instance()->downloadUrl( - getUpdateUrl(), false, 0, false, - "qBittorrent/" QBT_VERSION_2); + DownloadHandler *handler = DownloadManager::instance()->download( + DownloadRequest(getUpdateUrl()).userAgent("qBittorrent/" QBT_VERSION_2)); connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , this, &DNSUpdater::ipUpdateFinished); connect(handler, &Net::DownloadHandler::downloadFailed, this, &DNSUpdater::ipUpdateFailed); diff --git a/src/base/net/downloadmanager.cpp b/src/base/net/downloadmanager.cpp index 70a7707be..6e62388fb 100644 --- a/src/base/net/downloadmanager.cpp +++ b/src/base/net/downloadmanager.cpp @@ -155,11 +155,6 @@ Net::DownloadManager *Net::DownloadManager::instance() return m_instance; } -Net::DownloadHandler *Net::DownloadManager::downloadUrl(const QString &url, bool saveToFile, qint64 limit, bool handleRedirectToMagnet, const QString &userAgent) -{ - return download(DownloadRequest(url).saveToFile(saveToFile).limit(limit).handleRedirectToMagnet(handleRedirectToMagnet).userAgent(userAgent)); -} - Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &downloadRequest) { // Process download request diff --git a/src/base/net/downloadmanager.h b/src/base/net/downloadmanager.h index 63eb3fbee..1035ddf27 100644 --- a/src/base/net/downloadmanager.h +++ b/src/base/net/downloadmanager.h @@ -93,7 +93,6 @@ namespace Net static void freeInstance(); static DownloadManager *instance(); - DownloadHandler *downloadUrl(const QString &url, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false, const QString &userAgent = ""); DownloadHandler *download(const DownloadRequest &downloadRequest); void registerSequentialService(const ServiceID &serviceID); diff --git a/src/base/net/geoipmanager.cpp b/src/base/net/geoipmanager.cpp index 20db4eea7..ace840e98 100644 --- a/src/base/net/geoipmanager.cpp +++ b/src/base/net/geoipmanager.cpp @@ -118,7 +118,7 @@ void GeoIPManager::manageDatabaseUpdate() void GeoIPManager::downloadDatabaseFile() { - DownloadHandler *handler = DownloadManager::instance()->downloadUrl(DATABASE_URL); + DownloadHandler *handler = DownloadManager::instance()->download({DATABASE_URL}); connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , this, &GeoIPManager::downloadFinished); connect(handler, &Net::DownloadHandler::downloadFailed, this, &GeoIPManager::downloadFailed); diff --git a/src/base/rss/rss_feed.cpp b/src/base/rss/rss_feed.cpp index c5a450a5f..f094a8e0a 100644 --- a/src/base/rss/rss_feed.cpp +++ b/src/base/rss/rss_feed.cpp @@ -129,7 +129,7 @@ void Feed::refresh() // NOTE: Should we allow manually refreshing for disabled session? - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(m_url); + Net::DownloadHandler *handler = Net::DownloadManager::instance()->download({m_url}); connect(handler , static_cast(&Net::DownloadHandler::downloadFinished) , this, &Feed::handleDownloadFinished); @@ -418,7 +418,8 @@ void Feed::downloadIcon() // XXX: This works for most sites but it is not perfect const QUrl url(m_url); auto iconUrl = QString("%1://%2/favicon.ico").arg(url.scheme(), url.host()); - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(iconUrl, true); + Net::DownloadHandler *handler = Net::DownloadManager::instance()->download( + Net::DownloadRequest(iconUrl).saveToFile(true)); connect(handler , static_cast(&Net::DownloadHandler::downloadFinished) , this, &Feed::handleIconDownloadFinished); diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index de0a4b577..00e0b75c2 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -170,7 +170,7 @@ void SearchPluginManager::installPlugin(const QString &source) if (Utils::Misc::isUrl(source)) { using namespace Net; - DownloadHandler *handler = DownloadManager::instance()->downloadUrl(source, true); + DownloadHandler *handler = DownloadManager::instance()->download(DownloadRequest(source).saveToFile(true)); connect(handler, static_cast(&DownloadHandler::downloadFinished) , this, &SearchPluginManager::pluginDownloaded); connect(handler, &DownloadHandler::downloadFailed, this, &SearchPluginManager::pluginDownloadFailed); @@ -274,7 +274,7 @@ void SearchPluginManager::checkForUpdates() { // Download version file from update server using namespace Net; - DownloadHandler *handler = DownloadManager::instance()->downloadUrl(m_updateUrl + "versions.txt"); + DownloadHandler *handler = DownloadManager::instance()->download({m_updateUrl + "versions.txt"}); connect(handler, static_cast(&DownloadHandler::downloadFinished) , this, &SearchPluginManager::versionInfoDownloaded); connect(handler, &DownloadHandler::downloadFailed, this, &SearchPluginManager::versionInfoDownloadFailed); diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 031a9ca44..0a9e900a4 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -230,7 +230,9 @@ void AddNewTorrentDialog::show(QString source, const BitTorrent::AddTorrentParam if (Utils::Misc::isUrl(source)) { // Launch downloader - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(source, true, 10485760 /* 10MB */, true); + // TODO: Don't save loaded torrent to file, just use downloaded data! + Net::DownloadHandler *handler = Net::DownloadManager::instance()->download( + Net::DownloadRequest(source).limit(10485760 /* 10MB */).handleRedirectToMagnet(true).saveToFile(true)); connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , dlg, &AddNewTorrentDialog::handleDownloadFinished); connect(handler, &Net::DownloadHandler::downloadFailed, dlg, &AddNewTorrentDialog::handleDownloadFailed); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index a4263b9fe..83d013482 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -2043,11 +2043,11 @@ void MainWindow::installPython() { setCursor(QCursor(Qt::WaitCursor)); // Download python - Net::DownloadHandler *handler = nullptr; - if (QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA) - handler = Net::DownloadManager::instance()->downloadUrl("https://www.python.org/ftp/python/3.5.2/python-3.5.2.exe", true); - else - handler = Net::DownloadManager::instance()->downloadUrl("https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi", true); + const QString installerURL = ((QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA) + ? "https://www.python.org/ftp/python/3.5.2/python-3.5.2.exe" + : "https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi"); + Net::DownloadHandler *handler = Net::DownloadManager::instance()->download( + Net::DownloadRequest(installerURL).saveToFile(true)); using Func = void (Net::DownloadHandler::*)(const QString &, const QString &); connect(handler, static_cast(&Net::DownloadHandler::downloadFinished), this, &MainWindow::pythonDownloadSuccess); diff --git a/src/gui/programupdater.cpp b/src/gui/programupdater.cpp index 0e0f69560..7e5cc124a 100644 --- a/src/gui/programupdater.cpp +++ b/src/gui/programupdater.cpp @@ -61,11 +61,10 @@ ProgramUpdater::ProgramUpdater(QObject *parent, bool invokedByUser) void ProgramUpdater::checkForUpdates() { - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl( - RSS_URL, false, 0, false, - // Don't change this User-Agent. In case our updater goes haywire, - // the filehost can identify it and contact us. - "qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"); + // Don't change this User-Agent. In case our updater goes haywire, + // the filehost can identify it and contact us. + Net::DownloadHandler *handler = Net::DownloadManager::instance()->download( + Net::DownloadRequest(RSS_URL).userAgent("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)")); connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , this, &ProgramUpdater::rssDownloadFinished); connect(handler, &Net::DownloadHandler::downloadFailed, this, &ProgramUpdater::rssDownloadFailed); diff --git a/src/gui/properties/trackersadditiondialog.cpp b/src/gui/properties/trackersadditiondialog.cpp index be1fef2a7..ee9d11219 100644 --- a/src/gui/properties/trackersadditiondialog.cpp +++ b/src/gui/properties/trackersadditiondialog.cpp @@ -25,9 +25,10 @@ * but you are not obligated to do so. If you do not wish to do so, delete this * exception statement from your version. */ + #include "trackersadditiondialog.h" -#include +#include #include #include #include @@ -70,25 +71,16 @@ QStringList TrackersAdditionDialog::newTrackers() const void TrackersAdditionDialog::on_uTorrentListButton_clicked() { m_ui->uTorrentListButton->setEnabled(false); - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(m_ui->list_url->text(), true); - connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) + Net::DownloadHandler *handler = Net::DownloadManager::instance()->download({m_ui->list_url->text()}); + connect(handler, static_cast(&Net::DownloadHandler::downloadFinished) , this, &TrackersAdditionDialog::parseUTorrentList); connect(handler, &Net::DownloadHandler::downloadFailed, this, &TrackersAdditionDialog::getTrackerError); // Just to show that it takes times setCursor(Qt::WaitCursor); } -void TrackersAdditionDialog::parseUTorrentList(const QString &, const QString &path) +void TrackersAdditionDialog::parseUTorrentList(const QString &, const QByteArray &data) { - QFile listFile(path); - if (!listFile.open(QFile::ReadOnly)) { - QMessageBox::warning(this, tr("I/O Error"), tr("Error while trying to open the downloaded file."), QMessageBox::Ok); - setCursor(Qt::ArrowCursor); - m_ui->uTorrentListButton->setEnabled(true); - Utils::Fs::forceRemove(path); - return; - } - // Load from torrent handle QList existingTrackers = m_torrent->trackers(); // Load from current user list @@ -103,19 +95,21 @@ void TrackersAdditionDialog::parseUTorrentList(const QString &, const QString &p if (!m_ui->trackers_list->toPlainText().isEmpty() && !m_ui->trackers_list->toPlainText().endsWith('\n')) m_ui->trackers_list->insertPlainText("\n"); int nb = 0; - while (!listFile.atEnd()) { - const QString line = listFile.readLine().trimmed(); + QBuffer buffer; + buffer.setData(data); + buffer.open(QBuffer::ReadOnly); + while (!buffer.atEnd()) { + const QString line = buffer.readLine().trimmed(); if (line.isEmpty()) continue; + BitTorrent::TrackerEntry newTracker(line); if (!existingTrackers.contains(newTracker)) { m_ui->trackers_list->insertPlainText(line + '\n'); ++nb; } } - // Clean up - listFile.close(); - Utils::Fs::forceRemove(path); - //To restore the cursor ... + + // To restore the cursor ... setCursor(Qt::ArrowCursor); m_ui->uTorrentListButton->setEnabled(true); // Display information message if necessary diff --git a/src/gui/properties/trackersadditiondialog.h b/src/gui/properties/trackersadditiondialog.h index 276b4c47d..c6843d340 100644 --- a/src/gui/properties/trackersadditiondialog.h +++ b/src/gui/properties/trackersadditiondialog.h @@ -57,7 +57,7 @@ public: public slots: void on_uTorrentListButton_clicked(); - void parseUTorrentList(const QString &, const QString &path); + void parseUTorrentList(const QString &, const QByteArray &data); void getTrackerError(const QString &, const QString &error); private: diff --git a/src/gui/search/pluginselectdialog.cpp b/src/gui/search/pluginselectdialog.cpp index 11135fe40..e19c17ea9 100644 --- a/src/gui/search/pluginselectdialog.cpp +++ b/src/gui/search/pluginselectdialog.cpp @@ -292,7 +292,8 @@ void PluginSelectDialog::addNewPlugin(QString pluginName) else { // Icon is missing, we must download it using namespace Net; - DownloadHandler *handler = DownloadManager::instance()->downloadUrl(plugin->url + "/favicon.ico", true); + DownloadHandler *handler = DownloadManager::instance()->download( + DownloadRequest(plugin->url + "/favicon.ico").saveToFile(true)); connect(handler, static_cast(&DownloadHandler::downloadFinished) , this, &PluginSelectDialog::iconDownloaded); connect(handler, &DownloadHandler::downloadFailed, this, &PluginSelectDialog::iconDownloadFailed); diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index c8c71983f..843fad8c0 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -404,7 +404,8 @@ void TrackerFiltersList::trackerWarning(const QString &hash, const QString &trac void TrackerFiltersList::downloadFavicon(const QString& url) { if (!m_downloadTrackerFavicon) return; - Net::DownloadHandler *h = Net::DownloadManager::instance()->downloadUrl(url, true); + Net::DownloadHandler *h = Net::DownloadManager::instance()->download( + Net::DownloadRequest(url).saveToFile(true)); using Func = void (Net::DownloadHandler::*)(const QString &, const QString &); connect(h, static_cast(&Net::DownloadHandler::downloadFinished), this , &TrackerFiltersList::handleFavicoDownload);