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
qohosplatformservices.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/QUrl>
6#include <QtCore/private/qnapi_p.h>
7#include <QtCore/private/qohoscommon_p.h>
8#include <QtCore/private/qohospathutils_p.h>
9#include <QtCore/qfileinfo.h>
10#include <QtGui/QColor>
11#include <qohosenums.h>
12#include <qohosplugincore.h>
13#include <utility>
14
16
17using QOhosWantConstantFlags = QtOhos::enums::ohos::app::ability::wantConstant::Flags;
18
19namespace {
20
21void callStartAbility(QNapi::Object baseQAbility, QNapi::Object want, QOhosConsumer<bool> resultConsumer)
22{
23 qOhosPrintfDebug("Calling startAbility() with Want '%s'", QNapi::toJsonString(want).c_str());
24
25 baseQAbility.evalToPromiseOrRejectOnThrow("context.startAbility(*)", {want})
26 .withContext(std::move(resultConsumer))
27 .onThenWithContext(
28 [](const QtOhos::CallbackInfo &, auto &resultConsumer) {
29 qOhosPrintfDebug("Got success from startAbility()");
30 resultConsumer(true);
31 })
32 .onCatchWithContext(
33 [](const QtOhos::CallbackInfo &cbInfo, auto &resultConsumer) {
34 QtOhos::logJsCallbackError(cbInfo, "Got error from startAbility()");
35 resultConsumer(false);
36 });
37}
38
39class QOhosColorPicker : public QPlatformServiceColorPicker
40{
41public:
42 QOhosColorPicker();
43
44 void pickColor() override;
45};
46
47QOhosColorPicker::QOhosColorPicker() = default;
48
49void QOhosColorPicker::pickColor()
50{
51 auto selfRef = QtOhos::makeQThreadSafeRef(this);
53 [selfRef](QtOhos::JsState &jsState) {
54 jsState.evalToPromiseOrRejectOnThrow("@kit.Penkit.imageFeaturePicker.pickForResult()")
55 .onThen(
56 [selfRef](const QtOhos::CallbackInfo &cbInfo) {
57 auto pickedColorInfo = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
58 auto color = pickedColorInfo.get<QNapi::Object>("color");
59 QColor qColor(
60 color.get<QNapi::Number>("red"),
61 color.get<QNapi::Number>("green"),
62 color.get<QNapi::Number>("blue"),
63 color.get<QNapi::Number>("alpha"));
64 selfRef.visitInQtThreadIfAlive(
65 [qColor](QOhosColorPicker &self) {
66 Q_EMIT self.colorPicked(qColor);
67 });
68 })
69 .onCatch(
70 [](const QtOhos::CallbackInfo &cbInfo) {
71 QtOhos::logJsCallbackError(cbInfo, "@kit.Penkit.imageFeaturePicker.pickForResult() failed");
72 });
73 });
74}
75} // namespace
76
77QOhosPlatformServices::QOhosPlatformServices() = default;
78
79QPlatformServiceColorPicker *QOhosPlatformServices::colorPicker(QWindow *parent)
80{
81 Q_UNUSED(parent);
82 return new QOhosColorPicker;
83}
84
85bool QOhosPlatformServices::hasCapability(Capability capability) const
86{
87 switch (capability) {
88 case ColorPicking:
89 return true;
90 default:
91 return false;
92 }
93}
94
95bool QOhosPlatformServices::openUrl(const QUrl &url)
96{
97 return QtOhos::evalInJsThreadWithPromise<bool>(
98 [&](QtOhos::JsState &jsState, QOhosTaskPromise<bool> evalPromise) {
99 auto mainUiAbility = jsState.defaultQAbilityPeer()->qAbility();
100 if (mainUiAbility.IsEmpty()) {
101 evalPromise(false);
102 return;
103 }
104
105 auto want =
106 !url.isLocalFile()
107 ? QNapi::makeObject(
108 jsState.env(),
109 {
110 {"action", "ohos.want.action.viewData"},
111 {"entities", QNapi::makeArray(jsState.env(), {"entity.system.browsable"})},
112 {"uri", url.toString().toStdString()},
113 })
114 : QFileInfo(url.path()).isDir()
115 ? QNapi::makeObject(
116 jsState.env(),
117 {
118 {"abilityName", "MainAbility"},
119 {"bundleName", "com.huawei.hmos.filemanager"},
120 {
121 "parameters",
122 QNapi::makeObject(
123 jsState.env(),
124 {
125 {"fileUri", tryMapPathToOhosFileUri(url.path().toStdString()).value_or("")},
126 })
127 },
128 })
129 : QNapi::makeObject(
130 jsState.env(),
131 {
132 {"action", "ohos.want.action.viewData"},
133 {"uri", tryMapPathToOhosFileUri(url.path().toStdString()).value_or("")},
134 {
135 "flags",
136 jsState.mapOhosEnumToJs(QOhosWantConstantFlags::FLAG_AUTH_READ_URI_PERMISSION).Int32Value()
137 | jsState.mapOhosEnumToJs(QOhosWantConstantFlags::FLAG_AUTH_WRITE_URI_PERMISSION).Int32Value()
138 },
139 });
140 callStartAbility(mainUiAbility, want, std::move(evalPromise));
141 },
142 Q_FUNC_INFO);
143}
144
145bool QOhosPlatformServices::openDocument(const QUrl &url)
146{
147 return openUrl(url);
148}
149
150QByteArray QOhosPlatformServices::desktopEnvironment() const
151{
152 return QByteArray("Ohos");
153}
154
155QT_END_NAMESPACE
Combined button and popup list for selecting options.
void invokeInJsThread(std::function< void(JsState &)> task)