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
qohoswindowmanager.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
5#include <QtCore/private/qcore_ohos_p.h>
6#include <QtCore/qcoreapplication.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/qurl.h>
12#include <algorithm>
13#include <iterator>
14#include <vector>
16
18
19namespace {
20
26
28 QOhosJsState &jsState, QtOhos::QObjectThreadSafeRef qWindowRef)
29{
30 auto optQAbility = jsState.tryGetQAbilityByQWindow(qWindowRef);
31 return optQAbility ? optQAbility : jsState.defaultQAbility();
32}
33
35 QOhosJsState &jsState, QNapi::Object qAbility,
36 QtOhos::QObjectThreadSafeRef contextWindowRef)
37{
38 auto optContextJsWindow = jsState.tryGetJsWindowByQWindow(contextWindowRef);
39
40 std::vector<QNapi::ValueWrapper> constructorParams = {qAbility.get("context")};
41 if (optContextJsWindow)
42 constructorParams.push_back(optContextJsWindow.value());
43
44 return jsState.eval<QNapi::Object>(
45 "@ohos.file.picker.DocumentViewPicker<new>(*)", constructorParams);
46}
47
49 QOhosJsState &jsState, QtOhos::QObjectThreadSafeRef contextWindowRef,
50 const std::string &pickerActionName, QNapi::Object pickerActionOptions,
51 QOhosConsumer<std::optional<FilePickerResult>> resultConsumer)
52{
53 auto sharedResultConsumer = QtOhos::moveToSharedPtr(std::move(resultConsumer));
54
55 auto optQAbility = tryGetQAbilityForQWindow(jsState, contextWindowRef);
56 if (!optQAbility) {
57 qOhosPrintfError(
58 "Cannot call DocumentViewPicker.%s(): no UIAbility to use", pickerActionName.c_str());
59 (*sharedResultConsumer)(std::nullopt);
60 return;
61 }
62
63 qOhosPrintfDebug(
64 "Calling DocumentViewPicker.%s() with options: %s",
65 pickerActionName.c_str(), QNapi::toJsonString(pickerActionOptions).c_str());
66 auto documentViewPicker = QtOhos::moveToSharedPtr(
67 QNapi::Reference<>::makePersistentFrom(
68 makeDocumentViewPicker(jsState, optQAbility.value(), contextWindowRef)));
69 documentViewPicker->evalToPromiseOrRejectOnThrow(pickerActionName + "(*)", {pickerActionOptions}).onThen(
70 [documentViewPicker, pickerActionName, sharedResultConsumer](const QOhosCallbackInfo &cbInfo) {
71 auto actionResult = cbInfo.getFirstArg<QNapi::Array>(Q_FUNC_INFO);
72 auto resultOhosUris = QNapi::getArrayElements<std::vector<std::string>, QNapi::String>(actionResult);
73
74 qOhosPrintfDebug(
75 "Called DocumentViewPicker.%s() callback with result: %s",
76 pickerActionName.c_str(), QNapi::toJsonString(actionResult).c_str());
77
78 std::vector<std::string> resultPaths;
79 std::transform(
80 resultOhosUris.cbegin(), resultOhosUris.cend(), std::back_inserter(resultPaths),
81 [&](const auto &uri) {
82 return tryMapOhosFileUriToPath(uri).value_or("");
83 });
84
85 (*sharedResultConsumer)(
86 std::optional<FilePickerResult>(
87 {
88 .resultPaths = std::move(resultPaths),
89 .selectedIndex = documentViewPicker->eval<QNapi::Number>("getSelectedIndex()"),
90 }));
91 },
92 [pickerActionName, sharedResultConsumer]() {
93 qOhosPrintfError("DocumentViewPicker.%s() call failed", pickerActionName.c_str());
94 (*sharedResultConsumer)({});
95 });
96}
97
98QStringList mapFilePathsToQtUrls(const std::vector<std::string> &filePaths)
99{
100 QStringList qtUrls;
101 std::transform(
102 filePaths.cbegin(), filePaths.cend(), std::back_inserter(qtUrls),
103 [&](const auto &path) {
104 return QUrl::fromLocalFile(QString::fromStdString(path)).toString();
105 });
106 return qtUrls;
107}
108
109}
110
111namespace QOhosWindowManager {
112
114 QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList filters, QString defaultPath,
115 DocumentSelectMode documentSelectMode, ResultMultiplicity resultMultiplicity,
116 QOhosConsumer<std::optional<OpenResult>> resultCallback)
117{
118 auto sharedResultCallback = QtOhos::moveToSharedPtr(std::move(resultCallback));
119
120 QOhosJsThreadGateway::invoke(
121 [contextWindowRef, filters, defaultPath, documentSelectMode, resultMultiplicity, sharedResultCallback](QOhosJsState &jsState) {
122 constexpr auto ohosMaxValueForMaxSelectNumber = 500;
123
124 auto *env = jsState.env();
125
126 auto documentSelectOptions = QNapi::makeObject(
127 env,
128 {
129 {"maxSelectNumber", resultMultiplicity == ResultMultiplicity::SINGLE ? 1 : ohosMaxValueForMaxSelectNumber},
130 {"fileSuffixFilters", QNapi::makeArray(env, filters, std::mem_fn(&QString::toStdString))},
131 {"defaultFilePathUri", tryMapPathToOhosFileUri(defaultPath.toStdString()).value_or("")},
132 {"selectMode", jsState.mapOhosEnumToJs(documentSelectMode)},
133 });
134
135 startOhosFilePicker(
136 jsState, contextWindowRef, "select", documentSelectOptions,
137 [sharedResultCallback](auto optResult) {
138 auto optQtOpenResult = qTransform(
139 optResult,
140 [](const auto &result) {
141 return OpenResult{
142 .selectedUrls = mapFilePathsToQtUrls(result.resultPaths),
143 };
144 });
145 QtOhos::invokeInQtThread(
146 [sharedResultCallback, optQtOpenResult]() {
147 (*sharedResultCallback)(optQtOpenResult);
148 });
149 });
150 });
151}
152
154 QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList newFileNames,
155 QString defaultFilePath, QStringList fileSuffixChoices,
156 QOhosConsumer<std::optional<SaveResult>> resultCallback)
157{
158 auto sharedResultCallback = QtOhos::moveToSharedPtr(std::move(resultCallback));
159
160 QOhosJsThreadGateway::invoke(
161 [contextWindowRef, newFileNames, defaultFilePath, fileSuffixChoices, sharedResultCallback](QOhosJsState &jsState) {
162 auto *env = jsState.env();
163 auto documentSaveOptions = QNapi::Object::New(env);
164 if (!newFileNames.isEmpty())
165 documentSaveOptions.Set("newFileNames", QNapi::makeArray(env, newFileNames, std::mem_fn(&QString::toStdString)));
166 if (!defaultFilePath.isEmpty())
167 documentSaveOptions.Set("defaultFilePathUri", tryMapPathToOhosFileUri(defaultFilePath.toStdString()).value_or(""));
168 if (!fileSuffixChoices.isEmpty())
169 documentSaveOptions.Set("fileSuffixChoices", QNapi::makeArray(env, fileSuffixChoices, std::mem_fn(&QString::toStdString)));
170 documentSaveOptions.Set("autoCreateEmptyFile", false);
171
172 startOhosFilePicker(
173 jsState, contextWindowRef, "save", documentSaveOptions,
174 [sharedResultCallback](auto optResult) {
175 auto optQtSaveResult = qTransform(
176 optResult,
177 [](const auto &result) {
178 return SaveResult{
179 .savedUrls = mapFilePathsToQtUrls(result.resultPaths),
180 .selectedFileSuffixChoiceIndex = result.selectedIndex,
181 };
182 });
183 QtOhos::invokeInQtThread(
184 [sharedResultCallback, optQtSaveResult]() {
185 (*sharedResultCallback)(optQtSaveResult);
186 });
187 });
188 });
189}
190
191}
192
193QT_END_NAMESPACE
void showFileDialogOpen(QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList filters, QString defaultPath, DocumentSelectMode documentSelectMode, ResultMultiplicity resultMultiplicity, QOhosConsumer< std::optional< OpenResult > > resultCallback)
QtOhosQpa::enums::ohos::file::picker::DocumentSelectMode DocumentSelectMode
void showFileDialogSave(QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList newFileNames, QString defaultFilePath, QStringList fileSuffixChoices, QOhosConsumer< std::optional< SaveResult > > resultCallback)
Combined button and popup list for selecting options.
QNapi::Object makeDocumentViewPicker(QOhosJsState &jsState, QNapi::Object qAbility, QtOhos::QObjectThreadSafeRef contextWindowRef)
std::optional< QNapi::Object > tryGetQAbilityForQWindow(QOhosJsState &jsState, QtOhos::QObjectThreadSafeRef qWindowRef)
void startOhosFilePicker(QOhosJsState &jsState, QtOhos::QObjectThreadSafeRef contextWindowRef, const std::string &pickerActionName, QNapi::Object pickerActionOptions, QOhosConsumer< std::optional< FilePickerResult > > resultConsumer)
QStringList mapFilePathsToQtUrls(const std::vector< std::string > &filePaths)