Skip to content

Commit

Permalink
Merge pull request #1027 from vktr/feature/fix-duplicate-magnet-links
Browse files Browse the repository at this point in the history
Show notice when some (or all) torrents are already in the session
  • Loading branch information
vktr committed Nov 21, 2020
2 parents efbbd40 + 1279f28 commit 280fe10
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,7 @@
"eta_s_format": "{0}s",
"per_second_format": "{0}/s",
"contents": "Contents",
"tier": "Tier"
"tier": "Tier",
"some_torrents_already_in_session": "Some of the torrents are already added and will be skipped",
"all_torrents_already_in_session": "All torrents are already added and will be skipped"
}
27 changes: 27 additions & 0 deletions src/picotorrent/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ void Session::AddMetadataSearch(std::vector<libtorrent::info_hash_t> const& hash

for (lt::info_hash_t const& hash : hashes)
{
if (m_torrents.find(hash) != m_torrents.end())
{
BOOST_LOG_TRIVIAL(warning) << "Cannot search for torrent - already in session";
continue;
}

if (IsSearching(hash))
{
BOOST_LOG_TRIVIAL(warning) << "Already searching for info hash '" << str(hash) << "'";
continue;
}

lt::add_torrent_params params;
params.flags &= ~lt::torrent_flags::auto_managed;
params.flags &= ~lt::torrent_flags::need_save_resume;
Expand Down Expand Up @@ -304,6 +316,21 @@ void Session::AddTorrent(lt::add_torrent_params const& params)
m_session->async_add_torrent(params);
}

bool Session::HasTorrent(lt::info_hash_t const& hash)
{
if (m_torrents.find(hash) != m_torrents.end())
{
return true;
}

if (IsSearching(hash))
{
return true;
}

return false;
}

void Session::RemoveTorrent(pt::BitTorrent::TorrentHandle* torrent, lt::remove_flags_t flags)
{
m_session->remove_torrent(torrent->WrappedHandle(), flags);
Expand Down
1 change: 1 addition & 0 deletions src/picotorrent/bittorrent/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace BitTorrent

void AddMetadataSearch(std::vector<libtorrent::info_hash_t> const& hashes);
void AddTorrent(libtorrent::add_torrent_params const& params);
bool HasTorrent(libtorrent::info_hash_t const& hash);
void ReloadSettings();
void RemoveTorrent(TorrentHandle* handle, libtorrent::remove_flags_t flags = {});

Expand Down
27 changes: 27 additions & 0 deletions src/picotorrent/ui/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,33 @@ MainFrame::~MainFrame()

void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)
{
bool didRemove = false;

for (auto it = params.begin(); it != params.end();)
{
lt::info_hash_t ih;
if (it->ti) { ih = it->ti->info_hashes(); }
else { ih = it->info_hashes; }

if (m_session->HasTorrent(ih))
{
it = params.erase(it);
didRemove = true;
}
else
{
++it;
}
}

if (didRemove)
{
auto err = i18n("some_torrents_already_in_session");
if (params.empty()) err = i18n("all_torrents_already_in_session");

wxMessageBox(err, "PicoTorrent", wxOK, this);
}

if (params.empty())
{
return;
Expand Down

0 comments on commit 280fe10

Please sign in to comment.