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