Browse Source

Store libtorrent native file index in QHash

adaptive-webui-19844
Vladimir Golovnev (Glassez) 5 years ago
parent
commit
4021a0c7ce
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 30
      src/base/bittorrent/torrenthandle.cpp
  2. 8
      src/base/bittorrent/torrenthandle.h

30
src/base/bittorrent/torrenthandle.cpp

@ -74,16 +74,28 @@ const QString QB_EXT {QStringLiteral(".!qB")}; @@ -74,16 +74,28 @@ const QString QB_EXT {QStringLiteral(".!qB")};
using namespace BitTorrent;
#if (LIBTORRENT_VERSION_NUM >= 10200)
namespace libtorrent
{
namespace aux
{
template <typename T, typename Tag>
uint qHash(const strong_typedef<T, Tag> &key, const uint seed)
{
return static_cast<uint>((std::hash<strong_typedef<T, Tag>> {})(key) ^ seed);
}
}
}
#endif
namespace
{
#if (LIBTORRENT_VERSION_NUM < 10200)
using LTDownloadPriority = int;
using LTFileIndex = int;
using LTPieceIndex = int;
using LTQueuePosition = int;
#else
using LTDownloadPriority = lt::download_priority_t;
using LTFileIndex = lt::file_index_t;
using LTPieceIndex = lt::piece_index_t;
using LTQueuePosition = lt::queue_position_t;
#endif
@ -1513,7 +1525,7 @@ void TorrentHandle::renameFile(const int index, const QString &name) @@ -1513,7 +1525,7 @@ void TorrentHandle::renameFile(const int index, const QString &name)
{
if (m_startupState != Started) return;
m_oldPath[index].push_back(filePath(index));
m_oldPath[LTFileIndex {index}].push_back(filePath(index));
++m_renameCount;
m_nativeHandle.rename_file(LTFileIndex {index}, Utils::Fs::toNativePath(name).toStdString());
}
@ -1813,11 +1825,11 @@ void TorrentHandle::handleFileRenamedAlert(const lt::file_renamed_alert *p) @@ -1813,11 +1825,11 @@ void TorrentHandle::handleFileRenamedAlert(const lt::file_renamed_alert *p)
// remove empty leftover folders
// for example renaming "a/b/c" to "d/b/c", then folders "a/b" and "a" will
// be removed if they are empty
const QString oldFilePath = m_oldPath[LTUnderlyingType<LTFileIndex> {p->index}].takeFirst();
const QString oldFilePath = m_oldPath[p->index].takeFirst();
const QString newFilePath = Utils::Fs::toUniformPath(p->new_name());
if (m_oldPath[LTUnderlyingType<LTFileIndex> {p->index}].isEmpty())
m_oldPath.remove(LTUnderlyingType<LTFileIndex> {p->index});
if (m_oldPath[p->index].isEmpty())
m_oldPath.remove(p->index);
QVector<QStringRef> oldPathParts = oldFilePath.splitRef('/', QString::SkipEmptyParts);
oldPathParts.removeLast(); // drop file name part
@ -1856,9 +1868,9 @@ void TorrentHandle::handleFileRenameFailedAlert(const lt::file_rename_failed_ale @@ -1856,9 +1868,9 @@ void TorrentHandle::handleFileRenameFailedAlert(const lt::file_rename_failed_ale
.arg(name(), filePath(LTUnderlyingType<LTFileIndex> {p->index})
, QString::fromStdString(p->error.message())), Log::WARNING);
m_oldPath[LTUnderlyingType<LTFileIndex> {p->index}].removeFirst();
if (m_oldPath[LTUnderlyingType<LTFileIndex> {p->index}].isEmpty())
m_oldPath.remove(LTUnderlyingType<LTFileIndex> {p->index});
m_oldPath[p->index].removeFirst();
if (m_oldPath[p->index].isEmpty())
m_oldPath.remove(p->index);
--m_renameCount;
while (!isMoveInProgress() && (m_renameCount == 0) && !m_moveFinishedTriggers.isEmpty())

8
src/base/bittorrent/torrenthandle.h

@ -355,6 +355,12 @@ namespace BitTorrent @@ -355,6 +355,12 @@ namespace BitTorrent
private:
typedef std::function<void ()> EventTrigger;
#if (LIBTORRENT_VERSION_NUM < 10200)
using LTFileIndex = int;
#else
using LTFileIndex = lt::file_index_t;
#endif
void updateStatus();
void updateStatus(const lt::torrent_status &nativeStatus);
void updateState();
@ -419,7 +425,7 @@ namespace BitTorrent @@ -419,7 +425,7 @@ namespace BitTorrent
// Until libtorrent provide an "old_name" field in `file_renamed_alert`
// we will rely on this workaround to remove empty leftover folders
QHash<int, QVector<QString>> m_oldPath;
QHash<LTFileIndex, QVector<QString>> m_oldPath;
bool m_useAutoTMM;

Loading…
Cancel
Save