21 QtOhos::QObjectThreadSafeRef contextWindowRef, QString filePath,
22 QOhosConsumer<
bool> resultCallback)
24 auto sharedResultCallback = QtOhos::moveToSharedPtr(std::move(resultCallback));
26 QOhosJsThreadGateway::invoke(
27 [contextWindowRef, filePath, sharedResultCallback](QOhosJsState &jsState) {
28 auto optWindowQAbility = jsState.tryGetQAbilityByQWindow(contextWindowRef);
29 auto optQAbility = optWindowQAbility ? optWindowQAbility : jsState.defaultQAbility();
31 qOhosPrintfError(
"%s: no ability available for the file dialog", Q_FUNC_INFO);
32 QtOhos::invokeInQtThread(
33 [sharedResultCallback]() {
34 (*sharedResultCallback)(
false);
39 auto documentSelectOptions = QNapi::makeObject(
42 {
"defaultFilePathUri", tryMapPathToOhosFileUri(filePath.toStdString()).value_or(
"")},
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);
62 "Called DocumentViewPicker.select() callback with result: %s",
63 QNapi::toJsonString(actionResult).c_str());
65 bool authorized = actionResult.Length() > 0;
66 QtOhos::invokeInQtThread(
67 [sharedResultCallback, authorized]() {
68 (*sharedResultCallback)(authorized);
71 [sharedResultCallback]() {
72 qOhosPrintfError(
"DocumentViewPicker.select() call failed");
73 QtOhos::invokeInQtThread(
74 [sharedResultCallback]() {
75 (*sharedResultCallback)(
false);
87 auto eventLoop = std::make_shared<QEventLoop>();
88 auto filePathAuthorized =
std::make_shared<
bool>(
false);
90 showFileDialogAuthorization(
91 QtOhos::QObjectThreadSafeRef(parentWindow), filePath,
92 [filePathAuthorized, eventLoop](
bool result) {
93 *filePathAuthorized = result;
99 return *filePathAuthorized;