1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef KNOTIFICATIONQMLPLUGIN_H
8#define KNOTIFICATIONQMLPLUGIN_H
9
10#include <QQmlEngine>
11
12#include <KNotification>
13#include <KNotificationPermission>
14#include <KNotificationReplyAction>
15
16struct NotificationActionForeign {
17 Q_GADGET
18 QML_NAMED_ELEMENT(NotificationAction)
19 QML_FOREIGN(KNotificationAction);
20};
21
22struct NotificationReplyActionForeign {
23 Q_GADGET
24 QML_NAMED_ELEMENT(NotificationReplyAction)
25 QML_UNCREATABLE("")
26 QML_FOREIGN(KNotificationReplyAction);
27};
28
29class NotificationWrapper : public KNotification
30{
31 Q_OBJECT
32 QML_NAMED_ELEMENT(Notification)
33 Q_PROPERTY(KNotificationReplyAction *replyAction READ replyActionFactory CONSTANT)
34 Q_PROPERTY(QQmlListProperty<KNotificationAction> actions READ actionsProperty NOTIFY actionsChanged)
35 Q_PROPERTY(KNotificationAction *defaultAction READ defaultAction WRITE setDefaultActionQml NOTIFY defaultActionChanged)
36public:
37 explicit NotificationWrapper(QObject *parent = nullptr);
38
39 KNotificationReplyAction *replyActionFactory();
40
41 int actionCount() const;
42
43 KNotificationAction *actionAt(qsizetype index);
44
45 QQmlListProperty<KNotificationAction> actionsProperty() const;
46
47 static qsizetype actionsCount(QQmlListProperty<KNotificationAction> *list);
48
49 static void appendAction(QQmlListProperty<KNotificationAction> *list, KNotificationAction *value);
50
51 static KNotificationAction *actionAt(QQmlListProperty<KNotificationAction> *list, qsizetype index);
52
53 static void clearActions(QQmlListProperty<KNotificationAction> *list);
54
55private:
56 QQmlListProperty<KNotificationAction> m_actionsProperty;
57};
58
59class NotificationPermissionWrapper : public QObject
60{
61 Q_OBJECT
62 QML_NAMED_ELEMENT(NotificationPermission)
63 QML_SINGLETON
64public:
65 Q_INVOKABLE bool checkPermission()
66 {
67 return KNotificationPermission::checkPermission() == Qt::PermissionStatus::Granted;
68 }
69
70 Q_INVOKABLE void requestPermission(const QJSValue &callback)
71 {
72 KNotificationPermission::requestPermission(context: this, callback: [&callback](Qt::PermissionStatus status) {
73 callback.call(args: {status == Qt::PermissionStatus::Granted});
74 });
75 }
76};
77
78#endif // KNOTIFICATIONQMLPLUGIN_H
79

source code of knotifications/src/qml/knotificationqmlplugin.h