Skip to content

Commit

Permalink
Changed service and file api to use enum flags instead of booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilInTheGaps committed Dec 17, 2023
1 parent a439143 commit 7b54352
Show file tree
Hide file tree
Showing 58 changed files with 884 additions and 647 deletions.
1 change: 1 addition & 0 deletions src/filesystem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(SRC_COMMON
fileaccess.cpp
fileaccessswitcher.h
multigethelper.h
options.h
cache/filecache.h
cache/filecache.cpp
cache/filecacheentry.h
Expand Down
16 changes: 8 additions & 8 deletions src/filesystem/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ void File::init(Services::NextCloud *nc)
s_nc = nc;
}

auto File::getDataAsync(const QString &path, bool allowCache, std::shared_ptr<FileAccess> fileAccess)
auto File::getDataAsync(const QString &path, Options options, std::shared_ptr<FileAccess> fileAccess)
-> QFuture<FileDataResult>
{
auto access = getFileAccess(fileAccess);
return access ? access->getDataAsync(path, allowCache) : QFuture<FileDataResult>();
return access ? access->getDataAsync(path, options) : QFuture<FileDataResult>();
}

auto File::getDataAsync(const QStringList &paths, bool allowCache, std::shared_ptr<FileAccess> fileAccess)
auto File::getDataAsync(const QStringList &paths, Options options, std::shared_ptr<FileAccess> fileAccess)
-> QFuture<std::vector<FileDataResult>>
{
auto access = getFileAccess(fileAccess);
return access ? access->getDataAsync(paths, allowCache) : QFuture<std::vector<FileDataResult>>();
return access ? access->getDataAsync(paths, options) : QFuture<std::vector<FileDataResult>>();
}

auto File::saveAsync(const QString &path, const QByteArray &data, std::shared_ptr<FileAccess> fileAccess)
Expand Down Expand Up @@ -71,18 +71,18 @@ auto File::createDirAsync(const QString &path, std::shared_ptr<FileAccess> fileA
return access ? access->createDirAsync(path) : QFuture<FileResult>();
}

auto File::checkAsync(const QString &path, bool allowCache, std::shared_ptr<FileAccess> fileAccess)
auto File::checkAsync(const QString &path, Options options, std::shared_ptr<FileAccess> fileAccess)
-> QFuture<FileCheckResult>
{
auto access = getFileAccess(fileAccess);
return access ? access->checkAsync(path, allowCache) : QFuture<FileCheckResult>();
return access ? access->checkAsync(path, options) : QFuture<FileCheckResult>();
}

auto File::checkAsync(const QStringList &paths, bool allowCache, std::shared_ptr<FileAccess> fileAccess)
auto File::checkAsync(const QStringList &paths, Options options, std::shared_ptr<FileAccess> fileAccess)
-> QFuture<FileMultiCheckResult>
{
auto access = getFileAccess(fileAccess);
return access ? access->checkAsync(paths, allowCache) : QFuture<FileMultiCheckResult>();
return access ? access->checkAsync(paths, options) : QFuture<FileMultiCheckResult>();
}

void File::updateFileAccess()
Expand Down
9 changes: 5 additions & 4 deletions src/filesystem/file.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "options.h"
#include <QByteArray>
#include <QFuture>
#include <QString>
Expand Down Expand Up @@ -27,10 +28,10 @@ class File
public:
static void init(Services::NextCloud *nc);

static auto getDataAsync(const QString &path, bool allowCache = true,
static auto getDataAsync(const QString &path, Options options = Option::AllowCache,
std::shared_ptr<FileAccess> fileAccess = nullptr) -> QFuture<FileDataResult>;

static auto getDataAsync(const QStringList &paths, bool allowCache = true,
static auto getDataAsync(const QStringList &paths, Options options = Option::AllowCache,
std::shared_ptr<FileAccess> fileAccess = nullptr) -> QFuture<std::vector<FileDataResult>>;

static auto saveAsync(const QString &path, const QByteArray &data, std::shared_ptr<FileAccess> fileAccess = nullptr)
Expand All @@ -51,10 +52,10 @@ class File
static auto createDirAsync(const QString &path, std::shared_ptr<FileAccess> fileAccess = nullptr)
-> QFuture<FileResult>;

static auto checkAsync(const QString &path, bool allowCache = true,
static auto checkAsync(const QString &path, Options options = Option::AllowCache,
std::shared_ptr<FileAccess> fileAccess = nullptr) -> QFuture<FileCheckResult>;

static auto checkAsync(const QStringList &paths, bool allowCache = true,
static auto checkAsync(const QStringList &paths, Options options = Option::AllowCache,
std::shared_ptr<FileAccess> fileAccess = nullptr) -> QFuture<FileMultiCheckResult>;

static void updateFileAccess();
Expand Down
16 changes: 8 additions & 8 deletions src/filesystem/fileaccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

using namespace Files;

auto FileAccess::multiGetDataAsync(MultiGetHelper<FileDataResult> &&helper, bool allowCache)
auto FileAccess::multiGetDataAsync(MultiGetHelper<FileDataResult> &&helper, Options options)
-> QFuture<std::vector<FileDataResult>>
{
if (helper.isDone())
{
return QtFuture::makeReadyFuture(helper.getResults());
}

auto next = getDataAsync(helper.getNextPath(), allowCache);
auto next = getDataAsync(helper.getNextPath(), options);

return next
.then([this, helper = std::move(helper), allowCache](FileDataResult &&result) mutable {
.then([this, helper = std::move(helper), options](FileDataResult &&result) mutable {
helper.addResult(std::move(result));
return multiGetDataAsync(std::move(helper), allowCache);
return multiGetDataAsync(std::move(helper), options);
})
.unwrap();
}

auto FileAccess::multiCheckAsync(MultiGetHelper<FileCheckResult> &&helper, bool allowCache)
auto FileAccess::multiCheckAsync(MultiGetHelper<FileCheckResult> &&helper, Options options)
-> QFuture<FileMultiCheckResult>
{
if (helper.isDone())
{
return QtFuture::makeReadyFuture(FileMultiCheckResult(helper.getResults()));
}

auto next = checkAsync(helper.getNextPath(), allowCache);
auto next = checkAsync(helper.getNextPath(), options);

return next
.then([this, helper = std::move(helper), allowCache](FileCheckResult &&result) mutable {
.then([this, helper = std::move(helper), options](FileCheckResult &&result) mutable {
helper.addResult(std::move(result));
return multiCheckAsync(std::move(helper), allowCache);
return multiCheckAsync(std::move(helper), options);
})
.unwrap();
}
13 changes: 7 additions & 6 deletions src/filesystem/fileaccess.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "multigethelper.h"
#include "options.h"
#include "results/filecheckresult.h"
#include "results/filedataresult.h"
#include "results/filelistresult.h"
Expand All @@ -20,16 +21,16 @@ class FileAccess
virtual ~FileAccess() = default;
Q_DISABLE_COPY_MOVE(FileAccess);

virtual auto getDataAsync(const QString &path, bool allowCache) -> QFuture<FileDataResult> = 0;
virtual auto getDataAsync(const QStringList &paths, bool allowCache) -> QFuture<std::vector<FileDataResult>> = 0;
virtual auto getDataAsync(const QString &path, Options options) -> QFuture<FileDataResult> = 0;
virtual auto getDataAsync(const QStringList &paths, Options options) -> QFuture<std::vector<FileDataResult>> = 0;
virtual auto saveAsync(const QString &path, const QByteArray &data) -> QFuture<FileResult> = 0;
virtual auto moveAsync(const QString &oldPath, const QString &newPath) -> QFuture<FileResult> = 0;
virtual auto deleteAsync(const QString &path) -> QFuture<FileResult> = 0;
virtual auto copyAsync(const QString &path, const QString &copy) -> QFuture<FileResult> = 0;
virtual auto listAsync(const QString &path, bool files, bool folders) -> QFuture<FileListResult> = 0;
virtual auto createDirAsync(const QString &path) -> QFuture<FileResult> = 0;
virtual auto checkAsync(const QString &path, bool allowCache) -> QFuture<FileCheckResult> = 0;
virtual auto checkAsync(const QStringList &paths, bool allowCache) -> QFuture<FileMultiCheckResult> = 0;
virtual auto checkAsync(const QString &path, Options options) -> QFuture<FileCheckResult> = 0;
virtual auto checkAsync(const QStringList &paths, Options options) -> QFuture<FileMultiCheckResult> = 0;

static auto getInstance() -> std::shared_ptr<FileAccess>
{
Expand All @@ -42,9 +43,9 @@ class FileAccess
}

protected:
auto multiGetDataAsync(MultiGetHelper<FileDataResult> &&helper, bool allowCache)
auto multiGetDataAsync(MultiGetHelper<FileDataResult> &&helper, Options options)
-> QFuture<std::vector<FileDataResult>>;
auto multiCheckAsync(MultiGetHelper<FileCheckResult> &&helper, bool allowCache) -> QFuture<FileMultiCheckResult>;
auto multiCheckAsync(MultiGetHelper<FileCheckResult> &&helper, Options options) -> QFuture<FileMultiCheckResult>;

private:
inline static std::shared_ptr<FileAccess> instance = nullptr;
Expand Down
36 changes: 20 additions & 16 deletions src/filesystem/fileaccesslocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ using namespace Files;
Q_LOGGING_CATEGORY(gmFileAccessLocal, "gm.files.access.local")

/// Read data from one file
auto FileAccessLocal::getData(const QString &path, bool allowCache) -> FileDataResult
auto FileAccessLocal::getData(const QString &path) -> FileDataResult
{
Q_UNUSED(allowCache)

QFile f(path);

if (f.open(QIODevice::ReadOnly))
Expand All @@ -29,27 +27,31 @@ auto FileAccessLocal::getData(const QString &path, bool allowCache) -> FileDataR
}

/// Read data from one file async
auto FileAccessLocal::getDataAsync(const QString &path, bool allowCache) -> QFuture<FileDataResult>
auto FileAccessLocal::getDataAsync(const QString &path, Options options) -> QFuture<FileDataResult>
{
Q_UNUSED(options)

qCDebug(gmFileAccessLocal()) << "Getting data from file:" << path << "...";

return QtConcurrent::run(&FileAccessLocal::getData, path, allowCache).then(&m_context, [](FileDataResult &&result) {
return QtConcurrent::run(&FileAccessLocal::getData, path).then(&m_context, [](FileDataResult &&result) {
return std::move(result);
});
}

/// Read data from multiple files
auto FileAccessLocal::getDataAsync(const QStringList &paths, bool allowCache) -> QFuture<std::vector<FileDataResult>>
auto FileAccessLocal::getDataAsync(const QStringList &paths, Options options) -> QFuture<std::vector<FileDataResult>>
{
Q_UNUSED(options)

qCDebug(gmFileAccessLocal()) << "Getting data from multiple files:" << paths << "...";

return QtConcurrent::run([paths, allowCache]() {
return QtConcurrent::run([paths]() {
std::vector<FileDataResult> results;
results.reserve(paths.size());

foreach (const auto &path, paths)
{
results.push_back(getData(path, allowCache));
results.push_back(getData(path));
}

return results;
Expand Down Expand Up @@ -246,30 +248,32 @@ auto FileAccessLocal::listAsync(const QString &path, bool files, bool folders) -
}

/// Check if a file exists
auto FileAccessLocal::check(const QString &path, bool allowCache) -> FileCheckResult
auto FileAccessLocal::check(const QString &path) -> FileCheckResult
{
Q_UNUSED(allowCache)

QFile const f(path);
return FileCheckResult(path, f.exists());
}

/// Check if a file exists async
auto FileAccessLocal::checkAsync(const QString &path, bool allowCache) -> QFuture<FileCheckResult>
auto FileAccessLocal::checkAsync(const QString &path, Options options) -> QFuture<FileCheckResult>
{
return QtConcurrent::run(&FileAccessLocal::check, path, allowCache).then(&m_context, [](FileCheckResult &&result) {
Q_UNUSED(options)

return QtConcurrent::run(&FileAccessLocal::check, path).then(&m_context, [](FileCheckResult &&result) {
return std::move(result);
});
}

/// Check which files exist
auto FileAccessLocal::checkAsync(const QStringList &paths, bool allowCache) -> QFuture<FileMultiCheckResult>
auto FileAccessLocal::checkAsync(const QStringList &paths, Options options) -> QFuture<FileMultiCheckResult>
{
return QtConcurrent::run([paths, allowCache]() {
Q_UNUSED(options)

return QtConcurrent::run([paths]() {
FileMultiCheckResult result(true);
for (const auto &path : paths)
{
result.add(check(path, allowCache));
result.add(check(path));
}
return result;
})
Expand Down
12 changes: 6 additions & 6 deletions src/filesystem/fileaccesslocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ class FileAccessLocal : public FileAccess
~FileAccessLocal() override = default;
Q_DISABLE_COPY_MOVE(FileAccessLocal)

auto getDataAsync(const QString &path, bool allowCache) -> QFuture<FileDataResult> override;
auto getDataAsync(const QStringList &paths, bool allowCache) -> QFuture<std::vector<FileDataResult>> override;
auto getDataAsync(const QString &path, Options options) -> QFuture<FileDataResult> override;
auto getDataAsync(const QStringList &paths, Options options) -> QFuture<std::vector<FileDataResult>> override;
auto saveAsync(const QString &path, const QByteArray &data) -> QFuture<FileResult> override;
auto moveAsync(const QString &oldPath, const QString &newPath) -> QFuture<FileResult> override;
auto deleteAsync(const QString &path) -> QFuture<FileResult> override;
auto copyAsync(const QString &path, const QString &copy) -> QFuture<FileResult> override;
auto listAsync(const QString &path, bool files, bool folders) -> QFuture<FileListResult> override;
auto createDirAsync(const QString &path) -> QFuture<FileResult> override;
auto checkAsync(const QString &path, bool allowCache) -> QFuture<FileCheckResult> override;
auto checkAsync(const QStringList &paths, bool allowCache) -> QFuture<FileMultiCheckResult> override;
auto checkAsync(const QString &path, Options options) -> QFuture<FileCheckResult> override;
auto checkAsync(const QStringList &paths, Options options) -> QFuture<FileMultiCheckResult> override;

private:
static auto getData(const QString &path, bool allowCache) -> FileDataResult;
static auto getData(const QString &path) -> FileDataResult;
static auto createDir(const QDir &dir) -> FileResult;
static auto save(const QString &path, const QByteArray &data) -> FileResult;
static auto move(const QString &oldPath, const QString &newPath) -> FileResult;
static auto copy(const QString &path, const QString &copy) -> FileResult;
static auto getDirFilter(bool files, bool folders) -> QFlags<QDir::Filter>;
static auto check(const QString &path, bool allowCache) -> FileCheckResult;
static auto check(const QString &path) -> FileCheckResult;

// There is an issue in qt >= 6.6 where futures resolved on the same thread as the context object
// of their continuation become stuck trying to lock a mutex.
Expand Down
Loading

0 comments on commit 7b54352

Please sign in to comment.