Commit c7b86a28 authored by Volker Krause's avatar Volker Krause
Browse files

Add permission request API usable from QML

parent 55c6e35c
Pipeline #190999 passed with stage
in 2 minutes and 38 seconds
......@@ -91,6 +91,7 @@ target_sources(itinerary-app PRIVATE
mapdownloadmanager.cpp
notificationconfigcontroller.cpp
notifyrc.qrc
permissionmanager.cpp
settings.cpp
tickettokenmodel.cpp
transferdelegatecontroller.cpp
......
......@@ -27,6 +27,7 @@
#include "passmanager.h"
#include "pkpassmanager.h"
#include "timelinemodel.h"
#include "permissionmanager.h"
#include "pkpassimageprovider.h"
#include "publictransport.h"
#include "reservationmanager.h"
......@@ -122,6 +123,7 @@ void registerApplicationTypes()
qRegisterMetaType<Transfer::Alignment>();
qRegisterMetaType<TripGroupManager*>();
qRegisterMetaType<WeatherForecast>();
qRegisterMetaType<Permission::Permission>();
qmlRegisterUncreatableType<LocationInformation>("org.kde.itinerary", 1, 0, "LocationInformation", {});
qmlRegisterUncreatableType<StatisticsItem>("org.kde.itinerary", 1, 0, "StatisticsItem", {});
......@@ -129,6 +131,8 @@ void registerApplicationTypes()
qmlRegisterUncreatableType<TimelineModel>("org.kde.itinerary", 1, 0, "TimelineModel", {});
qmlRegisterUncreatableType<Transfer>("org.kde.itinerary", 1, 0, "Transfer", {});
qmlRegisterUncreatableMetaObject(Permission::staticMetaObject, "org.kde.itinerary", 1, 0, "Permission", {});
qmlRegisterType<CountrySubdivisionModel>("org.kde.itinerary", 1, 0, "CountrySubdivisionModel");
qmlRegisterType<DocumentsModel>("org.kde.itinerary", 1, 0, "DocumentsModel");
qmlRegisterType<LocationInformationDelegateController>("org.kde.itinerary", 1, 0, "LocationInformationDelegateController");
......@@ -196,6 +200,7 @@ void registerApplicationSingletons()
REGISTER_SINGLETON_GADGET_FACTORY(Localizer)
REGISTER_SINGLETON_GADGET_FACTORY(NavigationController)
REGISTER_SINGLETON_GADGET_FACTORY(NotificationConfigController)
REGISTER_SINGLETON_GADGET_FACTORY(PermissionManager)
REGISTER_SINGLETON_GADGET_FACTORY(PublicTransport)
REGISTER_SINGLETON_GADGET_FACTORY(Util)
}
......
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "permissionmanager.h"
#include <QDebug>
#ifdef Q_OS_ANDROID
#include <KAndroidExtras/ManifestPermission>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QtAndroid>
#else
#include <private/qandroidextras_p.h>
#endif
#endif
#ifdef Q_OS_ANDROID
static QString permissionName(Permission::Permission p)
{
using namespace KAndroidExtras;
switch (p) {
case Permission::ReadCalendar:
return ManifestPermission::READ_CALENDAR;
case Permission::WriteCalendar:
return ManifestPermission::WRITE_CALENDAR;
}
}
#endif
void PermissionManager::requestPermission(Permission::Permission permission, QJSValue callback)
{
qDebug() << permission;
#ifdef Q_OS_ANDROID
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (QtAndroid::checkPermission(permissionName(permission)) == QtAndroid::PermissionResult::Granted) {
callback.call();
return;
}
QtAndroid::requestPermissions({permissionName(permission)}, [permission, callback] (const QtAndroid::PermissionResultMap &result) {
if (result[permissionName(permission)] == QtAndroid::PermissionResult::Granted) {
auto cb = callback;
cb.call();
}
});
#else
// TODO make this properly async
if (QtAndroidPrivate::checkPermission(permissionName(permission)).result() != QtAndroidPrivate::PermissionResult::Authorized) {
if (QtAndroidPrivate::requestPermission(permissionName(permission)).result() != QtAndroidPrivate::PermissionResult::Authorized) {
return;
}
}
callback.call();
#endif
#else
// non-Android
callback.call();
#endif
}
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef PERMISSIONMANAGER_H
#define PERMISSIONMANAGER_H
#include <QJSValue>
/** Permission enum for use in QML. */
namespace Permission
{
Q_NAMESPACE
enum Permission {
ReadCalendar,
WriteCalendar,
};
Q_ENUM_NS(Permission)
}
/** Requesting platform permissions for access to controlled resources (calendar, location, etc). */
class PermissionManager
{
Q_GADGET
public:
Q_INVOKABLE static void requestPermission(Permission::Permission permission, QJSValue callback);
};
#endif // PERMISSIONMANAGER_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment