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/qscopeguard.h>
15#include <cstdlib>
16#include <filemanagement/file_uri/oh_file_uri.h>
17#include <optional>
18#include <string>
19
20QT_BEGIN_NAMESPACE
21
22namespace {
23
24std::optional<std::string> tryMapPathToOhosUri(const std::string &inputPath)
25{
26 char *outputPtr = nullptr;
27 auto outputPtrGuard = qScopeGuard(
28 [&]() {
29 ::free(outputPtr);
30 });
31 auto getUriResult = ::OH_FileUri_GetUriFromPath(inputPath.c_str(), inputPath.size(), &outputPtr);
32
33 std::optional<std::string> outputOhosUri;
34 if (getUriResult == FileManagement_ErrCode::ERR_OK && outputPtr != nullptr) {
35 outputOhosUri.emplace(outputPtr);
36 } else {
37 qOhosPrintfWarning(
38 "%s: path to OHOS URI conversion failed for input path '%s', retval: %d",
39 Q_FUNC_INFO, inputPath.c_str(), static_cast<int>(getUriResult));
40 }
41
42 return outputOhosUri;
43}
44
45bool tryDeleteToTrash(const QString &filePath)
46{
47 return QOhosJsThreadGateway::evalWithConsumer<bool>(
48 [&](QOhosJsState &jsState, QOhosConsumer<bool> resultConsumer) {
49 auto optFileOhosUri = tryMapPathToOhosUri(filePath.toStdString());
50 if (!optFileOhosUri.has_value()) {
51 resultConsumer(false);
52 return;
53 }
54
55 QNapi::Value deletePromiseOrValue;
56 try {
57 deletePromiseOrValue = jsState.eval(
58 "@kit.FileManagerServiceKit.fileManagerService.deleteToTrash(*)",
59 {optFileOhosUri.value()});
60 } catch (const Napi::Error &error) {
61 qOhosPrintfError(
62 "deleteToTrash('%s') failed with error: %s",
63 optFileOhosUri.value().c_str(), error.what());
64 }
65
66 if (deletePromiseOrValue.IsPromise()) {
67 QNapi::checkedCast<QNapi::Promise>(deletePromiseOrValue)
68 .withContext(std::move(resultConsumer))
69 .onThenWithContext(
70 [](const QOhosCallbackInfo &cbInfo, auto &resultConsumer) {
71 std::string deletedPath = cbInfo.getFirstArg<QNapi::String>(Q_FUNC_INFO);
72 resultConsumer(!deletedPath.empty());
73 })
74 .onCatchWithContext(
75 [](const QOhosCallbackInfo &cbInfo, auto &resultConsumer) {
76 QtOhos::logJsCallbackError(cbInfo, "Got error from deleteToTrash()");
77 resultConsumer(false);
78 });
79 } else {
80 qOhosPrintfWarning("Got non-Promise from deleteToTrash()");
81 resultConsumer(false);
82 }
83 });
84}
85
86}
87
88bool QFileSystemEngine::supportsMoveFileToTrash()
89{
90 return true;
91}
92
93bool QFileSystemEngine::moveFileToTrash(const QFileSystemEntry &source,
94 QFileSystemEntry &, QSystemError &error)
95{
96 bool movedToTrash = tryDeleteToTrash(source.filePath());
97 if (!movedToTrash)
98 error = QSystemError(ENOSYS, QSystemError::StandardLibraryError);
99 return movedToTrash;
100}
101
102QT_END_NAMESPACE
std::optional< std::string > tryMapPathToOhosUri(const std::string &inputPath)
bool tryDeleteToTrash(const QString &filePath)