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>
12#include <filemanagement/file_uri/oh_file_uri.h>
14#include <qohosenums.h>
15#include <qohosplugincore.h>
20using QOhosWantConstantFlags = QtOhos::enums::ohos::app::ability::wantConstant::Flags;
24void callStartAbility(QNapi::Object baseQAbility, QNapi::Object want, QOhosConsumer<
bool> resultConsumer)
26 qOhosPrintfDebug(
"Calling startAbility() with Want '%s'", QNapi::toJsonString(want).c_str());
28 baseQAbility.evalToPromiseOrRejectOnThrow(
"context.startAbility(*)", {want})
29 .withContext(
std::move(resultConsumer))
32 qOhosPrintfDebug(
"Got success from startAbility()");
37 QtOhos::logJsCallbackError(cbInfo,
"Got error from startAbility()");
38 resultConsumer(
false);
42std::string callOhFileUriConversionFunc(
43 FileManagement_ErrCode (*convFunc)(
const char *,
unsigned int,
char **),
44 const char *convFuncName,
const std::string &input)
46 std::string outputString;
48 char *outputPtr =
nullptr;
49 auto outputPtrGuard = qScopeGuard(std::bind(::free, outputPtr));
50 auto convFuncRetVal = convFunc(input.c_str(), input.size(), &outputPtr);
52 if (convFuncRetVal == FileManagement_ErrCode::ERR_OK && outputPtr !=
nullptr) {
53 outputString = outputPtr;
56 "OH FileUri conversion function '%s' failed for input '%s', retval: %d",
57 convFuncName, input.c_str(),
static_cast<
int>(convFuncRetVal));
63class QOhosColorPicker :
public QPlatformServiceColorPicker
68 void pickColor() override;
71 std::shared_ptr<
void> m_ohosColorPickingHandle;
74QOhosColorPicker::QOhosColorPicker() =
default;
76void QOhosColorPicker::pickColor()
80 std::unique_ptr<QObject> colorConsumerQtContext;
81 QOhosConsumer<QOhosOptional<quint32>> colorConsumer;
84 auto sharedContext = QtOhos::moveToSharedPtr(
86 .colorConsumerQtContext = std::make_unique<QObject>(),
88 [
this](QOhosOptional<quint32> rgbaColor) {
89 if (rgbaColor.has_value())
90 Q_EMIT colorPicked(QColor::fromRgba(rgbaColor.value()));
94 auto colorConsumerProxy = QtOhos::moveToSharedPtr(
95 [weakContext = QtOhos::makeWeakPtr(sharedContext)](QOhosOptional<quint32> rgbaColor) {
96 auto sharedContext = weakContext.lock();
98 QMetaObject::invokeMethod(
99 sharedContext->colorConsumerQtContext.get(),
100 [weakContext, rgbaColor]() {
101 auto sharedContext = weakContext.lock();
103 sharedContext->colorConsumer(rgbaColor);
105 Qt::QueuedConnection);
111 jsState.evalToPromiseOrRejectOnThrow(
"@kit.Penkit.imageFeaturePicker.pickForResult()")
114 auto pickedColorInfo = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
115 auto color = pickedColorInfo.get<QNapi::Object>(
"color");
117 color.get<QNapi::Number>(
"red"),
118 color.get<QNapi::Number>(
"green"),
119 color.get<QNapi::Number>(
"blue"),
120 color.get<QNapi::Number>(
"alpha"));
121 (*colorConsumerProxy)(makeQOhosOptional(qColor.rgba()));
125 QtOhos::logJsCallbackError(cbInfo,
"@kit.Penkit.imageFeaturePicker.pickForResult() failed");
130 m_ohosColorPickingHandle = std::move(sharedContext);
139 return new QOhosColorPicker;
144 switch (capability) {
154 return QtOhos::evalInJsThreadWithPromise<
bool>(
155 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<
bool> evalPromise) {
156 auto mainUiAbility = jsState.defaultQAbilityPeer()->qAbility();
157 if (mainUiAbility.IsEmpty()) {
167 {
"action",
"ohos.want.action.viewData"},
168 {
"entities", QNapi::makeArray(jsState.env(), {
"entity.system.browsable"})},
169 {
"uri", url.toString().toStdString()},
171 : QFileInfo(url.path()).isDir()
175 {
"abilityName",
"MainAbility"},
176 {
"bundleName",
"com.huawei.hmos.filemanager"},
182 {
"fileUri", mapPathToOhosUriInJsThread(url.path().toStdString())},
189 {
"action",
"ohos.want.action.viewData"},
190 {
"uri", mapPathToOhosUriInJsThread(url.path().toStdString())},
193 jsState.mapOhosEnumToJs(QOhosWantConstantFlags::FLAG_AUTH_READ_URI_PERMISSION).Int32Value()
194 | jsState.mapOhosEnumToJs(QOhosWantConstantFlags::FLAG_AUTH_WRITE_URI_PERMISSION).Int32Value()
197 callStartAbility(mainUiAbility, want,
std::move(evalPromise));
209 return QByteArray(
"Ohos");
214 return callOhFileUriConversionFunc(OH_FileUri_GetUriFromPath,
"OH_FileUri_GetUriFromPath", path);
219 return callOhFileUriConversionFunc(OH_FileUri_GetPathFromUri,
"OH_FileUri_GetPathFromUri", ohosFileUri);
Combined button and popup list for selecting options.
void invokeInJsThread(std::function< void(JsState &)> task)
std::nullopt_t makeEmptyQOhosOptional()