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"
34 #include "base/bittorrent/session.h"
35 #include "base/bittorrent/torrentdescriptor.h"
36 #include "base/logger.h"
37 #include "base/net/downloadmanager.h"
38 #include "base/preferences.h"
39 #include "base/torrentfileguard.h"
40 #include "addnewtorrentdialog.h"
41 #include "interfaces/iguiapplication.h"
42 #include "mainwindow.h"
43 #include "raisedmessagebox.h"
47 void adjustDialogGeometry(QWidget
*dialog
, const QWidget
*parentWindow
)
49 // It is preferable to place the dialog in the center of the parent window.
50 // However, if it goes beyond the current screen, then move it so that it fits there
51 // (or, if the dialog is larger than the current screen, at least make sure that
52 // the upper/left coordinates of the dialog are inside it).
54 QRect dialogGeometry
= dialog
->geometry();
56 dialogGeometry
.moveCenter(parentWindow
->geometry().center());
58 const QRect screenGeometry
= parentWindow
->screen()->availableGeometry();
60 QPoint delta
= screenGeometry
.bottomRight() - dialogGeometry
.bottomRight();
65 dialogGeometry
.translate(delta
);
67 delta
= screenGeometry
.topLeft() - dialogGeometry
.topLeft();
72 dialogGeometry
.translate(delta
);
74 dialog
->setGeometry(dialogGeometry
);
78 GUIAddTorrentManager::GUIAddTorrentManager(IGUIApplication
*app
, BitTorrent::Session
*session
, QObject
*parent
)
79 : GUIApplicationComponent(app
, session
, parent
)
81 connect(btSession(), &BitTorrent::Session::metadataDownloaded
, this, &GUIAddTorrentManager::onMetadataDownloaded
);
84 bool GUIAddTorrentManager::addTorrent(const QString
&source
, const BitTorrent::AddTorrentParams
¶ms
, const AddTorrentOption option
)
86 // `source`: .torrent file path, magnet URI or URL
91 const auto *pref
= Preferences::instance();
93 if ((option
== AddTorrentOption::SkipDialog
)
94 || ((option
== AddTorrentOption::Default
) && !pref
->isAddNewTorrentDialogEnabled()))
96 return AddTorrentManager::addTorrent(source
, params
);
99 if (Net::DownloadManager::hasSupportedScheme(source
))
101 LogMsg(tr("Downloading torrent... Source: \"%1\"").arg(source
));
103 Net::DownloadManager::instance()->download(Net::DownloadRequest(source
).limit(pref
->getTorrentFileSizeLimit())
104 , pref
->useProxyForGeneralPurposes(), this, &GUIAddTorrentManager::onDownloadFinished
);
105 m_downloadedTorrents
[source
] = params
;
110 if (const auto parseResult
= BitTorrent::TorrentDescriptor::parse(source
))
112 return processTorrent(source
, parseResult
.value(), params
);
114 else if (source
.startsWith(u
"magnet:", Qt::CaseInsensitive
))
116 handleAddTorrentFailed(source
, parseResult
.error());
120 const Path decodedPath
{source
.startsWith(u
"file://", Qt::CaseInsensitive
)
121 ? QUrl::fromEncoded(source
.toLocal8Bit()).toLocalFile() : source
};
122 auto torrentFileGuard
= std::make_shared
<TorrentFileGuard
>(decodedPath
);
123 if (const auto loadResult
= BitTorrent::TorrentDescriptor::loadFromFile(decodedPath
))
125 const BitTorrent::TorrentDescriptor
&torrentDescriptor
= loadResult
.value();
126 const bool isProcessing
= processTorrent(source
, torrentDescriptor
, params
);
128 setTorrentFileGuard(source
, torrentFileGuard
);
133 handleAddTorrentFailed(decodedPath
.toString(), loadResult
.error());
140 void GUIAddTorrentManager::onDownloadFinished(const Net::DownloadResult
&result
)
142 const QString
&source
= result
.url
;
143 const BitTorrent::AddTorrentParams addTorrentParams
= m_downloadedTorrents
.take(source
);
145 switch (result
.status
)
147 case Net::DownloadStatus::Success
:
148 if (const auto loadResult
= BitTorrent::TorrentDescriptor::load(result
.data
))
149 processTorrent(source
, loadResult
.value(), addTorrentParams
);
151 handleAddTorrentFailed(source
, loadResult
.error());
153 case Net::DownloadStatus::RedirectedToMagnet
:
154 if (const auto parseResult
= BitTorrent::TorrentDescriptor::parse(result
.magnetURI
))
155 processTorrent(source
, parseResult
.value(), addTorrentParams
);
157 handleAddTorrentFailed(source
, parseResult
.error());
160 handleAddTorrentFailed(source
, result
.errorString
);
164 void GUIAddTorrentManager::onMetadataDownloaded(const BitTorrent::TorrentInfo
&metadata
)
166 Q_ASSERT(metadata
.isValid());
167 if (!metadata
.isValid()) [[unlikely
]]
170 for (const auto &[infoHash
, dialog
] : m_dialogs
.asKeyValueRange())
172 if (metadata
.matchesInfoHash(infoHash
))
173 dialog
->updateMetadata(metadata
);
177 bool GUIAddTorrentManager::processTorrent(const QString
&source
, const BitTorrent::TorrentDescriptor
&torrentDescr
, const BitTorrent::AddTorrentParams
¶ms
)
179 const bool hasMetadata
= torrentDescr
.info().has_value();
180 const BitTorrent::InfoHash infoHash
= torrentDescr
.infoHash();
182 // Prevent showing the dialog if download is already present
183 if (BitTorrent::Torrent
*torrent
= btSession()->findTorrent(infoHash
))
187 // Trying to set metadata to existing torrent in case if it has none
188 torrent
->setMetadata(*torrentDescr
.info());
191 if (torrent
->isPrivate() || (hasMetadata
&& torrentDescr
.info()->isPrivate()))
193 handleDuplicateTorrent(source
, torrent
, tr("Trackers cannot be merged because it is a private torrent"));
197 bool mergeTrackers
= btSession()->isMergeTrackersEnabled();
198 if (Preferences::instance()->confirmMergeTrackers())
200 const QMessageBox::StandardButton btn
= RaisedMessageBox::question(app()->mainWindow(), tr("Torrent is already present")
201 , tr("Torrent '%1' is already in the transfer list. Do you want to merge trackers from new source?").arg(torrent
->name())
202 , (QMessageBox::Yes
| QMessageBox::No
), QMessageBox::Yes
);
203 mergeTrackers
= (btn
== QMessageBox::Yes
);
208 torrent
->addTrackers(torrentDescr
.trackers());
209 torrent
->addUrlSeeds(torrentDescr
.urlSeeds());
217 btSession()->downloadMetadata(torrentDescr
);
219 // By not setting a parent to the "AddNewTorrentDialog", all those dialogs
220 // will be displayed on top and will not overlap with the main window.
221 auto *dlg
= new AddNewTorrentDialog(torrentDescr
, params
, nullptr);
222 // Qt::Window is required to avoid showing only two dialog on top (see #12852).
223 // Also improves the general convenience of adding multiple torrents.
224 dlg
->setWindowFlags(Qt::Window
);
226 dlg
->setAttribute(Qt::WA_DeleteOnClose
);
227 m_dialogs
[infoHash
] = dlg
;
228 connect(dlg
, &QDialog::finished
, this, [this, source
, infoHash
, dlg
](int result
)
230 if (dlg
->isDoNotDeleteTorrentChecked())
231 releaseTorrentFileGuard(source
);
233 if (result
== QDialog::Accepted
)
234 addTorrentToSession(source
, dlg
->torrentDescriptor(), dlg
->addTorrentParams());
236 m_dialogs
.remove(infoHash
);
239 adjustDialogGeometry(dlg
, app()->mainWindow());