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
qohosfileutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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
5
6#include <QtCore/private/qcore_ohos_p.h>
7#include <QtCore/private/qnapi_p.h>
8#include <QtCore/private/qohoscommon_p.h>
9#include <QtCore/private/qohoslogger_p.h>
10#include <QtCore/private/qohospathutils_p.h>
11#include <QtCore/qeventloop.h>
12#include <memory>
13#include <utility>
14#include <vector>
15
16QT_BEGIN_NAMESPACE
17
18namespace {
19
21 QtOhos::QObjectThreadSafeRef contextWindowRef, QString filePath,
22 QOhosConsumer<bool> resultCallback)
23{
24 auto sharedResultCallback = QtOhos::moveToSharedPtr(std::move(resultCallback));
25
26 QOhosJsThreadGateway::invoke(
27 [contextWindowRef, filePath, sharedResultCallback](QOhosJsState &jsState) {
28 auto optWindowQAbility = jsState.tryGetQAbilityByQWindow(contextWindowRef);
29 auto optQAbility = optWindowQAbility ? optWindowQAbility : jsState.defaultQAbility();
30 if (!optQAbility) {
31 qOhosPrintfError("%s: no ability available for the file dialog", Q_FUNC_INFO);
32 QtOhos::invokeInQtThread(
33 [sharedResultCallback]() {
34 (*sharedResultCallback)(false);
35 });
36 return;
37 }
38
39 auto documentSelectOptions = QNapi::makeObject(
40 jsState.env(),
41 {
42 {"defaultFilePathUri", tryMapPathToOhosFileUri(filePath.toStdString()).value_or("")},
43 {"authMode", true},
44 });
45
46 qOhosPrintfDebug(
47 "Calling DocumentViewPicker.select() with options: %s",
48 QNapi::toJsonString(documentSelectOptions).c_str());
49 auto optContextJsWindow = jsState.tryGetJsWindowByQWindow(contextWindowRef);
50 std::vector<QNapi::ValueWrapper> constructorParams = {optQAbility.value().get("context")};
51 if (optContextJsWindow)
52 constructorParams.push_back(optContextJsWindow.value());
53 auto documentViewPicker = QtOhos::moveToSharedPtr(
54 QNapi::Reference<>::makePersistentFrom(
55 jsState.eval<QNapi::Object>(
56 "@ohos.file.picker.DocumentViewPicker<new>(*)", constructorParams)));
57 documentViewPicker->evalToPromiseOrRejectOnThrow("select(*)", {documentSelectOptions}).onThen(
58 [documentViewPicker, sharedResultCallback](const QOhosCallbackInfo &cbInfo) {
59 auto actionResult = cbInfo.getFirstArg<QNapi::Array>(Q_FUNC_INFO);
60
61 qOhosPrintfDebug(
62 "Called DocumentViewPicker.select() callback with result: %s",
63 QNapi::toJsonString(actionResult).c_str());
64
65 bool authorized = actionResult.Length() > 0;
66 QtOhos::invokeInQtThread(
67 [sharedResultCallback, authorized]() {
68 (*sharedResultCallback)(authorized);
69 });
70 },
71 [sharedResultCallback]() {
72 qOhosPrintfError("DocumentViewPicker.select() call failed");
73 QtOhos::invokeInQtThread(
74 [sharedResultCallback]() {
75 (*sharedResultCallback)(false);
76 });
77 });
78 });
79}
80
81}
82
83namespace QtOhosAppKit {
84
85bool authorizeFilePath(QWindow *parentWindow, const QString &filePath)
86{
87 auto eventLoop = std::make_shared<QEventLoop>();
88 auto filePathAuthorized = std::make_shared<bool>(false);
89
90 showFileDialogAuthorization(
91 QtOhos::QObjectThreadSafeRef(parentWindow), filePath,
92 [filePathAuthorized, eventLoop](bool result) {
93 *filePathAuthorized = result;
94 eventLoop->quit();
95 });
96
97 eventLoop->exec();
98
99 return *filePathAuthorized;
100}
101
102}
103
104QT_END_NAMESPACE
void showFileDialogAuthorization(QtOhos::QObjectThreadSafeRef contextWindowRef, QString filePath, QOhosConsumer< bool > resultCallback)
bool authorizeFilePath(QWindow *parentWindow, const QString &filePath)