Provide asynchronous results via QFuture

Makes asynchronous logic to look more straightforward.
Allows caller to choose blocking or non-blocking way of obtaining asynchronous results via the same interface.

PR #22598.
This commit is contained in:
Vladimir Golovnev 2025-04-27 16:24:07 +03:00 committed by GitHub
parent 33aaa867b5
commit 732b2bcbdb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 165 additions and 223 deletions

View file

@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2020 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2020-2025 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -27,13 +27,14 @@
*/
#include "filesearcher.h"
#include "base/bittorrent/common.h"
#include "base/bittorrent/infohash.h"
void FileSearcher::search(const BitTorrent::TorrentID &id, const PathList &originalFileNames
, const Path &savePath, const Path &downloadPath, const bool forceAppendExt)
#include <QPromise>
#include "base/bittorrent/common.h"
namespace
{
const auto findInDir = [](const Path &dirPath, PathList &fileNames, const bool forceAppendExt) -> bool
bool findInDir(const Path &dirPath, PathList &fileNames, const bool forceAppendExt)
{
bool found = false;
for (Path &fileName : fileNames)
@ -58,7 +59,13 @@ void FileSearcher::search(const BitTorrent::TorrentID &id, const PathList &origi
}
return found;
};
}
}
void FileSearcher::search(const PathList &originalFileNames, const Path &savePath
, const Path &downloadPath, const bool forceAppendExt, QPromise<FileSearchResult> promise)
{
promise.start();
Path usedPath = savePath;
PathList adjustedFileNames = originalFileNames;
@ -69,5 +76,6 @@ void FileSearcher::search(const BitTorrent::TorrentID &id, const PathList &origi
findInDir(usedPath, adjustedFileNames, forceAppendExt);
}
emit searchFinished(id, usedPath, adjustedFileNames);
promise.addResult(FileSearchResult {.savePath = usedPath, .fileNames = adjustedFileNames});
promise.finish();
}