Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfilesystemengine_ohos.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qplatformdefs.h"
7#include "qfile.h"
8#include "qurl.h"
9
10#include <QtCore/private/qcore_ohos_p.h>
11#include <QtCore/private/qnapi_p.h>
12#include <QtCore/private/qohoscommon_p.h>
13#include <QtCore/private/qohoslogger_p.h>
14#include <QtCore/private/qohospathutils_p.h>
15#include <optional>
16#include <string>
17
18QT_BEGIN_NAMESPACE
19
20namespace {
21
22bool tryDeleteToTrash(const QString &filePath)
23{
24 return QOhosJsThreadGateway::evalWithPromise<bool>(
25 [&](QOhosJsState &jsState, QOhosTaskPromise<bool> resultPromise) {
26 auto optFileOhosUri = tryMapPathToOhosFileUri(filePath.toStdString());
27 if (!optFileOhosUri.has_value()) {
28 resultPromise(false);
29 return;
30 }
31
32 QNapi::Value deletePromiseOrValue;
33 try {
34 deletePromiseOrValue = jsState.eval(
35 "@kit.FileManagerServiceKit.fileManagerService.deleteToTrash(*)",
36 {optFileOhosUri.value()});
37 } catch (const Napi::Error &error) {
38 qOhosPrintfError(
39 "deleteToTrash('%s') failed with error: %s",
40 optFileOhosUri.value().c_str(), error.what());
41 }
42
43 if (deletePromiseOrValue.IsPromise()) {
44 auto thenCatchPromises = std::move(resultPromise).makeThenCatchBranches(Q_FUNC_INFO);
45 QNapi::checkedCast<QNapi::Promise>(deletePromiseOrValue)
46 .onThen(
47 [thenPromise = std::move(thenCatchPromises.first)](const QOhosCallbackInfo &cbInfo) {
48 std::string deletedPath = cbInfo.getFirstArg<QNapi::String>(Q_FUNC_INFO);
49 thenPromise(!deletedPath.empty());
50 })
51 .onCatch(
52 [catchPromise = std::move(thenCatchPromises.second)](const QOhosCallbackInfo &cbInfo) {
53 QtOhos::logJsCallbackError(cbInfo, "Got error from deleteToTrash()");
54 catchPromise(false);
55 });
56 } else {
57 qOhosPrintfWarning("Got non-Promise from deleteToTrash()");
58 resultPromise(false);
59 }
60 },
61 Q_FUNC_INFO);
62}
63
64}
65
66bool QFileSystemEngine::supportsMoveFileToTrash()
67{
68 return true;
69}
70
71bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
72 QFileSystemEntry &, QSystemError &error)
73{
74 bool movedToTrash = tryDeleteToTrash(source.filePath());
75 if (!movedToTrash)
76 error = QSystemError(ENOSYS, QSystemError::StandardLibraryError);
77 return movedToTrash;
78}
79
80QT_END_NAMESPACE
bool tryDeleteToTrash(const QString &filePath)