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/qfileinfo.h>
9#include <QtCore/qscopeguard.h>
10#include <QtGui/QColor>
11#include <cstdlib>
12#include <filemanagement/file_uri/oh_file_uri.h>
13#include <qohosplugincore.h>
14#include <qohosqpafunctions_p.h>
15
17
22
23namespace QtOhos {
24
25template<>
27{
28 static constexpr const char *fullTypeName = "@ohos.app.ability.wantConstant.Flags";
29 static constexpr std::array<std::pair<QOhosWantConstantFlags, const char *>, 2> enumeratorsNames = {{
30 {QOhosWantConstantFlags::FLAG_AUTH_READ_URI_PERMISSION, "FLAG_AUTH_READ_URI_PERMISSION"},
31 {QOhosWantConstantFlags::FLAG_AUTH_WRITE_URI_PERMISSION, "FLAG_AUTH_WRITE_URI_PERMISSION"},
32 }};
33};
34
35}
36
37namespace {
38
39void callStartAbility(QNapi::Object baseQAbility, QNapi::Object want, QOhosConsumer<bool> resultConsumer)
40{
41 qOhosPrintfDebug("Calling startAbility() with Want '%s'", QNapi::toJsonString(want).c_str());
42
43 baseQAbility.call<QNapi::Promise>("context.startAbility", {want})
44 .withContext(std::move(resultConsumer))
45 .onThenWithContext(
46 [](const QtOhos::CallbackInfo &, auto &resultConsumer) {
47 qOhosPrintfDebug("Got success from startAbility()");
48 resultConsumer(true);
49 })
50 .onCatchWithContext(
51 [](const QtOhos::CallbackInfo &cbInfo, auto &resultConsumer) {
52 QtOhos::logJsCallbackError(cbInfo, "Got error from startAbility()");
53 resultConsumer(false);
54 });
55}
56
57std::string callOhFileUriConversionFunc(
58 FileManagement_ErrCode (*convFunc)(const char *, unsigned int, char **),
59 const char *convFuncName, const std::string &input)
60{
61 std::string outputString;
62
63 char *outputPtr = nullptr;
64 auto outputPtrGuard = qScopeGuard(std::bind(::free, outputPtr));
65 auto convFuncRetVal = convFunc(input.c_str(), input.size(), &outputPtr);
66
67 if (convFuncRetVal == FileManagement_ErrCode::ERR_OK && outputPtr != nullptr) {
68 outputString = outputPtr;
69 } else {
70 qWarning(
71 "OH FileUri conversion function '%s' failed for input '%s', retval: %d",
72 convFuncName, input.c_str(), static_cast<int>(convFuncRetVal));
73 }
74
75 return outputString;
76}
77
78} // namespace
79
81{
82public:
85 m_ohosColorPickingHandler.reset();
86 }
87
89 {
90 m_ohosColorPickingHandler =
91 QtOhos::getQOhosQpaFunctions().startPickingColorFromScreenWithConsumer(
92 [this](QOhosOptional<quint32> rgbaColor) {
93 if (rgbaColor.hasValue())
94 emit this->colorPicked(QColor::fromRgba(rgbaColor.value()));
95 });
96 }
97private:
98 std::shared_ptr<void> m_ohosColorPickingHandler;
99};
100
102
104{
105 Q_UNUSED(parent);
106 return new QOhosColorPicker;
107}
108
109bool QOhosPlatformServices::hasCapability(Capability capability) const
110{
111 switch (capability) {
112 case ColorPicking:
113 return true;
114 default:
115 return false;
116 }
117}
118
119bool QOhosPlatformServices::openUrl(const QUrl &url)
120{
121 return QtOhos::evalInJsThreadWithConsumer<bool>(
122 [&](QtOhos::JsState &jsState, QOhosConsumer<bool> resultConsumer) {
123 auto mainUiAbility = jsState.defaultQAbilityPeer()->qAbility();
124 if (mainUiAbility.IsEmpty()) {
125 resultConsumer(false);
126 return;
127 }
128
129 auto want =
130 !url.isLocalFile()
131 ? QNapi::makeObject(
132 jsState.env(),
133 {
134 {"action", "ohos.want.action.viewData"},
135 {"entities", QNapi::makeArray(jsState.env(), {"entity.system.browsable"})},
136 {"uri", url.toString().toStdString()},
137 })
138 : QFileInfo(url.path()).isDir()
139 ? QNapi::makeObject(
140 jsState.env(),
141 {
142 {"abilityName", "MainAbility"},
143 {"bundleName", "com.huawei.hmos.filemanager"},
144 {
145 "parameters",
146 QNapi::makeObject(
147 jsState.env(),
148 {
149 {"fileUri", mapPathToOhosUriInJsThread(url.path().toStdString())},
150 })
151 },
152 })
153 : QNapi::makeObject(
154 jsState.env(),
155 {
156 {"action", "ohos.want.action.viewData"},
157 {"uri", mapPathToOhosUriInJsThread(url.path().toStdString())},
158 {
159 "flags",
160 jsState.mapOhosEnumToJs(QOhosWantConstantFlags::FLAG_AUTH_READ_URI_PERMISSION).Int32Value()
161 | jsState.mapOhosEnumToJs(QOhosWantConstantFlags::FLAG_AUTH_WRITE_URI_PERMISSION).Int32Value()
162 },
163 });
164 callStartAbility(mainUiAbility, want, std::move(resultConsumer));
165 });
166}
167
169{
170 return openUrl(url);
171}
172
174{
175 return QByteArray("Ohos");
176}
177
179{
180 return callOhFileUriConversionFunc(OH_FileUri_GetUriFromPath, "OH_FileUri_GetUriFromPath", path);
181}
182
184{
185 return callOhFileUriConversionFunc(OH_FileUri_GetPathFromUri, "OH_FileUri_GetPathFromUri", ohosFileUri);
186}
187
188QT_END_NAMESPACE
static std::string mapPathToOhosUriInJsThread(const std::string &path)
QPlatformServiceColorPicker * colorPicker(QWindow *parent) override
QByteArray desktopEnvironment() const override
QPlatformServices::desktopEnvironment returns the active desktop environment.
static std::string mapOhosFileUriToPathInJsThread(const std::string &ohosFileUri)
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.