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::evalWithConsumer<bool>(
25 [&](QOhosJsState &jsState, QOhosConsumer<bool> resultConsumer) {
26 auto optFileOhosUri = tryMapPathToOhosFileUri(filePath.toStdString());
27 if (!optFileOhosUri.has_value()) {
28 resultConsumer(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 QNapi::checkedCast<QNapi::Promise>(deletePromiseOrValue)
45 .withContext(std::move(resultConsumer))
46 .onThenWithContext(
47 [](const QOhosCallbackInfo &cbInfo, auto &resultConsumer) {
48 std::string deletedPath = cbInfo.getFirstArg<QNapi::String>(Q_FUNC_INFO);
49 resultConsumer(!deletedPath.empty());
50 })
51 .onCatchWithContext(
52 [](const QOhosCallbackInfo &cbInfo, auto &resultConsumer) {
53 QtOhos::logJsCallbackError(cbInfo, "Got error from deleteToTrash()");
54 resultConsumer(false);
55 });
56 } else {
57 qOhosPrintfWarning("Got non-Promise from deleteToTrash()");
58 resultConsumer(false);
59 }
60 });
61}
62
63}
64
65bool QFileSystemEngine::supportsMoveFileToTrash()
66{
67 return true;
68}
69
70bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
71 QFileSystemEntry &, QSystemError &error)
72{
73 bool movedToTrash = tryDeleteToTrash(source.filePath());
74 if (!movedToTrash)
75 error = QSystemError(ENOSYS, QSystemError::StandardLibraryError);
76 return movedToTrash;
77}
78
79QT_END_NAMESPACE
bool tryDeleteToTrash(const QString &filePath)