37 QtOhos::QObjectThreadSafeRef contextWindowRef)
39 auto optContextJsWindow = jsState.tryGetJsWindowByQWindow(contextWindowRef);
41 std::vector<QNapi::ValueWrapper> constructorParams = {qAbility.get(
"context")};
42 if (optContextJsWindow)
43 constructorParams.push_back(optContextJsWindow.value());
45 return jsState.eval<QNapi::Object>(
46 "@ohos.file.picker.DocumentViewPicker<new>(*)", constructorParams);
50 QtOhos::
JsState &jsState, QtOhos::QObjectThreadSafeRef contextWindowRef,
51 const std::string &pickerActionName, QNapi::Object pickerActionOptions,
52 QOhosConsumer<std::optional<FilePickerResult>> resultConsumer)
54 auto sharedResultConsumer = QtOhos::moveToSharedPtr(std::move(resultConsumer));
56 auto optQAbility = tryGetQAbilityForQWindow(jsState, contextWindowRef);
59 "Cannot call DocumentViewPicker.%s(): no UIAbility to use", pickerActionName.c_str());
60 (*sharedResultConsumer)(
std::nullopt);
65 "Calling DocumentViewPicker.%s() with options: %s",
66 pickerActionName.c_str(), QNapi::toJsonString(pickerActionOptions).c_str());
67 auto documentViewPicker = QtOhos::moveToSharedPtr(
68 QNapi::Reference<>::makePersistentFrom(
69 makeDocumentViewPicker(jsState, optQAbility.value(), contextWindowRef)));
70 documentViewPicker->evalToPromiseOrRejectOnThrow(pickerActionName +
"(*)", {pickerActionOptions}).onThen(
71 [documentViewPicker, pickerActionName, sharedResultConsumer](
const QtOhos::
CallbackInfo &cbInfo) {
72 auto actionResult = cbInfo.getFirstArg<QNapi::Array>(Q_FUNC_INFO);
73 auto resultOhosUris = QNapi::getArrayElements<std::vector<std::string>, QNapi::String>(actionResult);
76 "Called DocumentViewPicker.%s() callback with result: %s",
77 pickerActionName.c_str(), QNapi::toJsonString(actionResult).c_str());
79 std::vector<std::string> resultPaths;
81 resultOhosUris.cbegin(), resultOhosUris.cend(),
std::back_inserter(resultPaths),
82 [&](
const auto &uri) {
83 return tryMapOhosFileUriToPath(uri).value_or(
"");
86 (*sharedResultConsumer)(
87 std::optional<FilePickerResult>(
89 .resultPaths = std::move(resultPaths),
90 .selectedIndex = documentViewPicker->eval<QNapi::Number>(
"getSelectedIndex()"),
93 [pickerActionName, sharedResultConsumer]() {
94 qOhosPrintfError(
"DocumentViewPicker.%s() call failed", pickerActionName.c_str());
95 (*sharedResultConsumer)({});
115 QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList filters, QString defaultPath,
117 QOhosConsumer<std::optional<OpenResult>> resultCallback)
119 auto sharedResultCallback = QtOhos::moveToSharedPtr(std::move(resultCallback));
122 [contextWindowRef, filters, defaultPath, documentSelectMode, resultMultiplicity, sharedResultCallback](
QtOhos::
JsState &jsState) {
123 constexpr auto ohosMaxValueForMaxSelectNumber = 500;
125 auto *env = jsState.env();
127 auto documentSelectOptions = QNapi::makeObject(
130 {
"maxSelectNumber", resultMultiplicity == ResultMultiplicity::SINGLE ? 1 : ohosMaxValueForMaxSelectNumber},
131 {
"fileSuffixFilters", QNapi::makeArray(env, filters, std::mem_fn(&QString::toStdString))},
132 {
"defaultFilePathUri", tryMapPathToOhosFileUri(defaultPath.toStdString()).value_or(
"")},
133 {
"selectMode", jsState.mapOhosEnumToJs(documentSelectMode)},
137 jsState, contextWindowRef,
"select", documentSelectOptions,
138 [sharedResultCallback](
auto optResult) {
139 auto optQtOpenResult = qTransform(
141 [](
const auto &result) {
143 .selectedUrls = mapFilePathsToQtUrls(result.resultPaths),
146 QtOhos::invokeInQtThread(
147 [sharedResultCallback, optQtOpenResult]() {
148 (*sharedResultCallback)(optQtOpenResult);
155 QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList newFileNames,
156 QString defaultFilePath, QStringList fileSuffixChoices,
157 QOhosConsumer<std::optional<SaveResult>> resultCallback)
159 auto sharedResultCallback = QtOhos::moveToSharedPtr(std::move(resultCallback));
162 [contextWindowRef, newFileNames, defaultFilePath, fileSuffixChoices, sharedResultCallback](
QtOhos::
JsState &jsState) {
163 auto *env = jsState.env();
164 auto documentSaveOptions = QNapi::Object::New(env);
165 if (!newFileNames.isEmpty())
166 documentSaveOptions.Set(
"newFileNames", QNapi::makeArray(env, newFileNames, std::mem_fn(&QString::toStdString)));
167 if (!defaultFilePath.isEmpty())
168 documentSaveOptions.Set(
"defaultFilePathUri", tryMapPathToOhosFileUri(defaultFilePath.toStdString()).value_or(
""));
169 if (!fileSuffixChoices.isEmpty())
170 documentSaveOptions.Set(
"fileSuffixChoices", QNapi::makeArray(env, fileSuffixChoices, std::mem_fn(&QString::toStdString)));
171 documentSaveOptions.Set(
"autoCreateEmptyFile",
false);
174 jsState, contextWindowRef,
"save", documentSaveOptions,
175 [sharedResultCallback](
auto optResult) {
176 auto optQtSaveResult = qTransform(
178 [](
const auto &result) {
180 .savedUrls = mapFilePathsToQtUrls(result.resultPaths),
181 .selectedFileSuffixChoiceIndex = result.selectedIndex,
184 QtOhos::invokeInQtThread(
185 [sharedResultCallback, optQtSaveResult]() {
186 (*sharedResultCallback)(optQtSaveResult);
void showFileDialogOpen(QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList filters, QString defaultPath, DocumentSelectMode documentSelectMode, ResultMultiplicity resultMultiplicity, QOhosConsumer< std::optional< OpenResult > > resultCallback)
void showFileDialogSave(QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList newFileNames, QString defaultFilePath, QStringList fileSuffixChoices, QOhosConsumer< std::optional< SaveResult > > resultCallback)
void startOhosFilePicker(QtOhos::JsState &jsState, QtOhos::QObjectThreadSafeRef contextWindowRef, const std::string &pickerActionName, QNapi::Object pickerActionOptions, QOhosConsumer< std::optional< FilePickerResult > > resultConsumer)