Drop WebUI default credentials
[qBittorrent.git] / src / gui / guiaddtorrentmanager.cpp
blob57ecd713b29fa41de7eddb77ba8dc5dfcff228c8
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "guiaddtorrentmanager.h"
32 #include "base/bittorrent/session.h"
33 #include "base/bittorrent/torrentdescriptor.h"
34 #include "base/logger.h"
35 #include "base/net/downloadmanager.h"
36 #include "base/preferences.h"
37 #include "base/torrentfileguard.h"
38 #include "addnewtorrentdialog.h"
39 #include "interfaces/iguiapplication.h"
40 #include "mainwindow.h"
41 #include "raisedmessagebox.h"
43 GUIAddTorrentManager::GUIAddTorrentManager(IGUIApplication *app, BitTorrent::Session *session, QObject *parent)
44 : GUIApplicationComponent(app, session, parent)
46 connect(btSession(), &BitTorrent::Session::metadataDownloaded, this, &GUIAddTorrentManager::onMetadataDownloaded);
49 bool GUIAddTorrentManager::addTorrent(const QString &source, const BitTorrent::AddTorrentParams &params, const AddTorrentOption option)
51 // `source`: .torrent file path, magnet URI or URL
53 if (source.isEmpty())
54 return false;
56 const auto *pref = Preferences::instance();
58 if ((option == AddTorrentOption::SkipDialog)
59 || ((option == AddTorrentOption::Default) && !pref->isAddNewTorrentDialogEnabled()))
61 return AddTorrentManager::addTorrent(source, params);
64 if (Net::DownloadManager::hasSupportedScheme(source))
66 LogMsg(tr("Downloading torrent... Source: \"%1\"").arg(source));
67 // Launch downloader
68 Net::DownloadManager::instance()->download(Net::DownloadRequest(source).limit(pref->getTorrentFileSizeLimit())
69 , pref->useProxyForGeneralPurposes(), this, &GUIAddTorrentManager::onDownloadFinished);
70 m_downloadedTorrents[source] = params;
72 return true;
75 if (const auto parseResult = BitTorrent::TorrentDescriptor::parse(source))
77 return processTorrent(source, parseResult.value(), params);
79 else if (source.startsWith(u"magnet:", Qt::CaseInsensitive))
81 handleAddTorrentFailed(source, parseResult.error());
82 return false;
85 const Path decodedPath {source.startsWith(u"file://", Qt::CaseInsensitive)
86 ? QUrl::fromEncoded(source.toLocal8Bit()).toLocalFile() : source};
87 auto torrentFileGuard = std::make_shared<TorrentFileGuard>(decodedPath);
88 if (const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(decodedPath))
90 const BitTorrent::TorrentDescriptor &torrentDescriptor = loadResult.value();
91 const bool isProcessing = processTorrent(source, torrentDescriptor, params);
92 if (isProcessing)
93 setTorrentFileGuard(source, torrentFileGuard);
94 return isProcessing;
96 else
98 handleAddTorrentFailed(decodedPath.toString(), loadResult.error());
99 return false;
102 return false;
105 void GUIAddTorrentManager::onDownloadFinished(const Net::DownloadResult &result)
107 const QString &source = result.url;
108 const BitTorrent::AddTorrentParams addTorrentParams = m_downloadedTorrents.take(source);
110 switch (result.status)
112 case Net::DownloadStatus::Success:
113 if (const auto loadResult = BitTorrent::TorrentDescriptor::load(result.data))
114 processTorrent(source, loadResult.value(), addTorrentParams);
115 else
116 handleAddTorrentFailed(source, loadResult.error());
117 break;
118 case Net::DownloadStatus::RedirectedToMagnet:
119 if (const auto parseResult = BitTorrent::TorrentDescriptor::parse(result.magnetURI))
120 processTorrent(source, parseResult.value(), addTorrentParams);
121 else
122 handleAddTorrentFailed(source, parseResult.error());
123 break;
124 default:
125 handleAddTorrentFailed(source, result.errorString);
129 void GUIAddTorrentManager::onMetadataDownloaded(const BitTorrent::TorrentInfo &metadata)
131 Q_ASSERT(metadata.isValid());
132 if (!metadata.isValid()) [[unlikely]]
133 return;
135 for (const auto &[infoHash, dialog] : m_dialogs.asKeyValueRange())
137 if (metadata.matchesInfoHash(infoHash))
138 dialog->updateMetadata(metadata);
142 bool GUIAddTorrentManager::processTorrent(const QString &source, const BitTorrent::TorrentDescriptor &torrentDescr, const BitTorrent::AddTorrentParams &params)
144 const bool hasMetadata = torrentDescr.info().has_value();
145 const BitTorrent::InfoHash infoHash = torrentDescr.infoHash();
147 // Prevent showing the dialog if download is already present
148 if (BitTorrent::Torrent *torrent = btSession()->findTorrent(infoHash))
150 if (hasMetadata)
152 // Trying to set metadata to existing torrent in case if it has none
153 torrent->setMetadata(*torrentDescr.info());
156 if (torrent->isPrivate() || (hasMetadata && torrentDescr.info()->isPrivate()))
158 handleDuplicateTorrent(source, torrent, tr("Trackers cannot be merged because it is a private torrent"));
160 else
162 bool mergeTrackers = btSession()->isMergeTrackersEnabled();
163 if (Preferences::instance()->confirmMergeTrackers())
165 const QMessageBox::StandardButton btn = RaisedMessageBox::question(app()->mainWindow(), tr("Torrent is already present")
166 , tr("Torrent '%1' is already in the transfer list. Do you want to merge trackers from new source?").arg(torrent->name())
167 , (QMessageBox::Yes | QMessageBox::No), QMessageBox::Yes);
168 mergeTrackers = (btn == QMessageBox::Yes);
171 if (mergeTrackers)
173 torrent->addTrackers(torrentDescr.trackers());
174 torrent->addUrlSeeds(torrentDescr.urlSeeds());
178 return false;
181 if (!hasMetadata)
182 btSession()->downloadMetadata(torrentDescr);
184 // By not setting a parent to the "AddNewTorrentDialog", all those dialogs
185 // will be displayed on top and will not overlap with the main window.
186 auto *dlg = new AddNewTorrentDialog(torrentDescr, params, nullptr);
187 // Qt::Window is required to avoid showing only two dialog on top (see #12852).
188 // Also improves the general convenience of adding multiple torrents.
189 dlg->setWindowFlags(Qt::Window);
191 dlg->setAttribute(Qt::WA_DeleteOnClose);
192 m_dialogs[infoHash] = dlg;
193 connect(dlg, &QDialog::finished, this, [this, source, infoHash, dlg](int result)
195 if (dlg->isDoNotDeleteTorrentChecked())
196 releaseTorrentFileGuard(source);
198 if (result == QDialog::Accepted)
199 addTorrentToSession(source, dlg->torrentDescriptor(), dlg->addTorrentParams());
201 m_dialogs.remove(infoHash);
203 dlg->show();
205 return true;