Skip to content

Commit

Permalink
Revamp deletion of source .torrent file
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Dec 27, 2024
1 parent e370c64 commit ffc0982
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ namespace BitTorrent
SQLite
};
Q_ENUM_NS(ResumeDataStorageType)

enum class DeleteTorrentFileOption : int
{
// Do not change these names: they are stored in config file
Never,
IfAdded,
Always
};
Q_ENUM_NS(DeleteTorrentFileOption)
}

class Session : public QObject
Expand Down Expand Up @@ -452,6 +461,8 @@ namespace BitTorrent
virtual void setStartPaused(bool value) = 0;
virtual TorrentContentRemoveOption torrentContentRemoveOption() const = 0;
virtual void setTorrentContentRemoveOption(TorrentContentRemoveOption option) = 0;
virtual DeleteTorrentFileOption deleteTorrentFileOption() const = 0;
virtual void setDeleteTorrentFileOption(DeleteTorrentFileOption option) = 0;

virtual bool isRestored() const = 0;

Expand Down
42 changes: 36 additions & 6 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ SessionImpl::SessionImpl(QObject *parent)
, m_I2PInboundLength {BITTORRENT_SESSION_KEY(u"I2P/InboundLength"_s), 3}
, m_I2POutboundLength {BITTORRENT_SESSION_KEY(u"I2P/OutboundLength"_s), 3}
, m_torrentContentRemoveOption {BITTORRENT_SESSION_KEY(u"TorrentContentRemoveOption"_s), TorrentContentRemoveOption::Delete}
, m_deleteTorrentFileOption {u"Core/AutoDeleteAddedTorrentFile"_s, DeleteTorrentFileOption::Never}
, m_startPaused {BITTORRENT_SESSION_KEY(u"StartPaused"_s)}
, m_seedingLimitTimer {new QTimer(this)}
, m_resumeDataTimer {new QTimer(this)}
Expand Down Expand Up @@ -4108,6 +4109,16 @@ void SessionImpl::setTorrentContentRemoveOption(const TorrentContentRemoveOption
m_torrentContentRemoveOption = option;
}

DeleteTorrentFileOption SessionImpl::deleteTorrentFileOption() const
{
return m_deleteTorrentFileOption;
}

void SessionImpl::setDeleteTorrentFileOption(const DeleteTorrentFileOption option)
{
m_deleteTorrentFileOption = option;
}

QStringList SessionImpl::bannedIPs() const
{
return m_bannedIPs;
Expand Down Expand Up @@ -5671,14 +5682,18 @@ void SessionImpl::handleAddTorrentAlert(const lt::add_torrent_alert *alert)
#else
const InfoHash infoHash {(hasMetadata ? params.ti->info_hash() : params.info_hash)};
#endif
if (const auto loadingTorrentsIter = m_loadingTorrents.find(TorrentID::fromInfoHash(infoHash))
const auto torrentID = TorrentID::fromInfoHash(infoHash);
if (const auto loadingTorrentsIter = m_loadingTorrents.find(torrentID)
; loadingTorrentsIter != m_loadingTorrents.end())
{
emit addTorrentFailed(infoHash, msg);
m_loadingTorrents.erase(loadingTorrentsIter);
m_addingTorrents.remove(torrentID);
// TODO: Delete .torrent file

emit addTorrentFailed(infoHash, msg);
}
else if (const auto downloadedMetadataIter = m_downloadedMetadata.find(TorrentID::fromInfoHash(infoHash))
; downloadedMetadataIter != m_downloadedMetadata.end())
else if (const auto downloadedMetadataIter = m_downloadedMetadata.find(torrentID)
; downloadedMetadataIter != m_downloadedMetadata.end())
{
m_downloadedMetadata.erase(downloadedMetadataIter);
if (infoHash.isHybrid())
Expand Down Expand Up @@ -5865,21 +5880,34 @@ void SessionImpl::dispatchTorrentAlert(const lt::torrent_alert *alert)
}
}

template <template <typename, typename> typename Dict, typename Key, typename Value>
std::optional<Value> take(Dict<Key, Value> &dict, const Key &key)
{
if (const auto it = dict.find(key); (it != std::end(dict)))
{
const Value value = std::move(it.value());
dict.erase(it);
return value;
}

return std::nullopt;
}

TorrentImpl *SessionImpl::createTorrent(const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params)
{
auto *const torrent = new TorrentImpl(this, m_nativeSession, nativeHandle, params);
m_torrents.insert(torrent->id(), torrent);
if (const InfoHash infoHash = torrent->infoHash(); infoHash.isHybrid())
m_hybridTorrentsByAltID.insert(TorrentID::fromSHA1Hash(infoHash.v1()), torrent);

if (isRestored())
if (const std::optional<TorrentDescriptor> optTorrentDescr = take(m_addingTorrents, torrent->id()))
{
if (params.addToQueueTop)
nativeHandle.queue_position_top();

torrent->requestResumeData(lt::torrent_handle::save_info_dict);

const TorrentDescriptor torrentDescr = m_addingTorrents.take(torrent->id());
const TorrentDescriptor torrentDescr = optTorrentDescr.value();
if (isStoreTorrentFileEnabled())
{
if (torrentDescr.hasCompleteMetadata())
Expand Down Expand Up @@ -5928,6 +5956,8 @@ TorrentImpl *SessionImpl::createTorrent(const lt::torrent_handle &nativeHandle,
torrent->setStoredTorrentInfo(std::get<QString>(torrentDescr.source()));
}
}

// TODO: Delete .torrent file
}

if (((torrent->ratioLimit() >= 0) || (torrent->seedingTimeLimit() >= 0))
Expand Down
3 changes: 3 additions & 0 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ namespace BitTorrent
void setStartPaused(bool value) override;
TorrentContentRemoveOption torrentContentRemoveOption() const override;
void setTorrentContentRemoveOption(TorrentContentRemoveOption option) override;
DeleteTorrentFileOption deleteTorrentFileOption() const override;
void setDeleteTorrentFileOption(DeleteTorrentFileOption option) override;

bool isRestored() const override;

Expand Down Expand Up @@ -747,6 +749,7 @@ namespace BitTorrent
CachedSettingValue<int> m_I2PInboundLength;
CachedSettingValue<int> m_I2POutboundLength;
CachedSettingValue<TorrentContentRemoveOption> m_torrentContentRemoveOption;
CachedSettingValue<DeleteTorrentFileOption> m_deleteTorrentFileOption;
SettingValue<bool> m_startPaused;

lt::session *m_nativeSession = nullptr;
Expand Down

0 comments on commit ffc0982

Please sign in to comment.