Always notify user about duplicate torrent

PR #21480.
Closes #21475.
This commit is contained in:
Vladimir Golovnev 2024-10-14 11:50:20 +03:00 committed by Vladimir Golovnev (Glassez)
parent b579afe1aa
commit a3a8b15828
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
3 changed files with 60 additions and 51 deletions

View file

@ -157,10 +157,36 @@ void AddTorrentManager::handleAddTorrentFailed(const QString &source, const QStr
emit addTorrentFailed(source, reason); emit addTorrentFailed(source, reason);
} }
void AddTorrentManager::handleDuplicateTorrent(const QString &source, BitTorrent::Torrent *torrent, const QString &message) void AddTorrentManager::handleDuplicateTorrent(const QString &source
, const BitTorrent::TorrentDescriptor &torrentDescr, BitTorrent::Torrent *existingTorrent)
{ {
const bool hasMetadata = torrentDescr.info().has_value();
if (hasMetadata)
{
// Trying to set metadata to existing torrent in case if it has none
existingTorrent->setMetadata(*torrentDescr.info());
}
const bool isPrivate = existingTorrent->isPrivate() || (hasMetadata && torrentDescr.info()->isPrivate());
QString message;
if (!btSession()->isMergeTrackersEnabled())
{
message = tr("Merging of trackers is disabled");
}
else if (isPrivate)
{
message = tr("Trackers cannot be merged because it is a private torrent");
}
else
{
// merge trackers and web seeds
existingTorrent->addTrackers(torrentDescr.trackers());
existingTorrent->addUrlSeeds(torrentDescr.urlSeeds());
message = tr("Trackers are merged from new source");
}
LogMsg(tr("Detected an attempt to add a duplicate torrent. Source: %1. Existing torrent: %2. Result: %3") LogMsg(tr("Detected an attempt to add a duplicate torrent. Source: %1. Existing torrent: %2. Result: %3")
.arg(source, torrent->name(), message)); .arg(source, existingTorrent->name(), message));
emit addTorrentFailed(source, message); emit addTorrentFailed(source, message);
} }
@ -184,32 +210,7 @@ bool AddTorrentManager::processTorrent(const QString &source, const BitTorrent::
if (BitTorrent::Torrent *torrent = btSession()->findTorrent(infoHash)) if (BitTorrent::Torrent *torrent = btSession()->findTorrent(infoHash))
{ {
// a duplicate torrent is being added // a duplicate torrent is being added
handleDuplicateTorrent(source, torrentDescr, torrent);
const bool hasMetadata = torrentDescr.info().has_value();
if (hasMetadata)
{
// Trying to set metadata to existing torrent in case if it has none
torrent->setMetadata(*torrentDescr.info());
}
if (!btSession()->isMergeTrackersEnabled())
{
handleDuplicateTorrent(source, torrent, tr("Merging of trackers is disabled"));
return false;
}
const bool isPrivate = torrent->isPrivate() || (hasMetadata && torrentDescr.info()->isPrivate());
if (isPrivate)
{
handleDuplicateTorrent(source, torrent, tr("Trackers cannot be merged because it is a private torrent"));
return false;
}
// merge trackers and web seeds
torrent->addTrackers(torrentDescr.trackers());
torrent->addUrlSeeds(torrentDescr.urlSeeds());
handleDuplicateTorrent(source, torrent, tr("Trackers are merged from new source"));
return false; return false;
} }

View file

@ -72,7 +72,7 @@ protected:
bool addTorrentToSession(const QString &source, const BitTorrent::TorrentDescriptor &torrentDescr bool addTorrentToSession(const QString &source, const BitTorrent::TorrentDescriptor &torrentDescr
, const BitTorrent::AddTorrentParams &addTorrentParams); , const BitTorrent::AddTorrentParams &addTorrentParams);
void handleAddTorrentFailed(const QString &source, const QString &reason); void handleAddTorrentFailed(const QString &source, const QString &reason);
void handleDuplicateTorrent(const QString &source, BitTorrent::Torrent *torrent, const QString &message); void handleDuplicateTorrent(const QString &source, const BitTorrent::TorrentDescriptor &torrentDescr, BitTorrent::Torrent *existingTorrent);
void setTorrentFileGuard(const QString &source, std::shared_ptr<TorrentFileGuard> torrentFileGuard); void setTorrentFileGuard(const QString &source, std::shared_ptr<TorrentFileGuard> torrentFileGuard);
void releaseTorrentFileGuard(const QString &source); void releaseTorrentFileGuard(const QString &source);

View file

@ -175,7 +175,8 @@ void GUIAddTorrentManager::onMetadataDownloaded(const BitTorrent::TorrentInfo &m
} }
} }
bool GUIAddTorrentManager::processTorrent(const QString &source, const BitTorrent::TorrentDescriptor &torrentDescr, const BitTorrent::AddTorrentParams &params) bool GUIAddTorrentManager::processTorrent(const QString &source
, const BitTorrent::TorrentDescriptor &torrentDescr, const BitTorrent::AddTorrentParams &params)
{ {
const bool hasMetadata = torrentDescr.info().has_value(); const bool hasMetadata = torrentDescr.info().has_value();
const BitTorrent::InfoHash infoHash = torrentDescr.infoHash(); const BitTorrent::InfoHash infoHash = torrentDescr.infoHash();
@ -183,32 +184,39 @@ bool GUIAddTorrentManager::processTorrent(const QString &source, const BitTorren
// Prevent showing the dialog if download is already present // Prevent showing the dialog if download is already present
if (BitTorrent::Torrent *torrent = btSession()->findTorrent(infoHash)) if (BitTorrent::Torrent *torrent = btSession()->findTorrent(infoHash))
{ {
if (hasMetadata) if (Preferences::instance()->confirmMergeTrackers())
{ {
// Trying to set metadata to existing torrent in case if it has none if (hasMetadata)
torrent->setMetadata(*torrentDescr.info()); {
} // Trying to set metadata to existing torrent in case if it has none
torrent->setMetadata(*torrentDescr.info());
}
if (torrent->isPrivate() || (hasMetadata && torrentDescr.info()->isPrivate())) const bool isPrivate = torrent->isPrivate() || (hasMetadata && torrentDescr.info()->isPrivate());
{ const QString dialogCaption = tr("Torrent is already present");
handleDuplicateTorrent(source, torrent, tr("Trackers cannot be merged because it is a private torrent")); if (isPrivate)
{
// We cannot merge trackers for private torrent but we still notify user
// about duplicate torrent if confirmation dialog is enabled.
RaisedMessageBox::warning(app()->mainWindow(), dialogCaption
, tr("Trackers cannot be merged because it is a private torrent."));
}
else
{
const bool mergeTrackers = btSession()->isMergeTrackersEnabled();
const QMessageBox::StandardButton btn = RaisedMessageBox::question(app()->mainWindow(), dialogCaption
, tr("Torrent '%1' is already in the transfer list. Do you want to merge trackers from new source?").arg(torrent->name())
, (QMessageBox::Yes | QMessageBox::No), (mergeTrackers ? QMessageBox::Yes : QMessageBox::No));
if (btn == QMessageBox::Yes)
{
torrent->addTrackers(torrentDescr.trackers());
torrent->addUrlSeeds(torrentDescr.urlSeeds());
}
}
} }
else else
{ {
bool mergeTrackers = btSession()->isMergeTrackersEnabled(); handleDuplicateTorrent(source, torrentDescr, torrent);
if (Preferences::instance()->confirmMergeTrackers())
{
const QMessageBox::StandardButton btn = RaisedMessageBox::question(app()->mainWindow(), tr("Torrent is already present")
, tr("Torrent '%1' is already in the transfer list. Do you want to merge trackers from new source?").arg(torrent->name())
, (QMessageBox::Yes | QMessageBox::No), QMessageBox::Yes);
mergeTrackers = (btn == QMessageBox::Yes);
}
if (mergeTrackers)
{
torrent->addTrackers(torrentDescr.trackers());
torrent->addUrlSeeds(torrentDescr.urlSeeds());
}
} }
return false; return false;