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