| 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 | |
| 16 | /*! |
| 17 | * \qmltype NotificationAction |
| 18 | * \inqmlmodule org.kde.notifications |
| 19 | * \nativetype KNotificationAction |
| 20 | */ |
| 21 | struct NotificationActionForeign { |
| 22 | Q_GADGET |
| 23 | QML_NAMED_ELEMENT(NotificationAction) |
| 24 | QML_FOREIGN(KNotificationAction); |
| 25 | }; |
| 26 | |
| 27 | /*! |
| 28 | * \qmltype NotificationReplyAction |
| 29 | * \inqmlmodule org.kde.notifications |
| 30 | * \nativetype KNotificationReplyAction |
| 31 | */ |
| 32 | struct NotificationReplyActionForeign { |
| 33 | Q_GADGET |
| 34 | QML_NAMED_ELEMENT(NotificationReplyAction) |
| 35 | QML_UNCREATABLE("" ) |
| 36 | QML_FOREIGN(KNotificationReplyAction); |
| 37 | }; |
| 38 | |
| 39 | /*! |
| 40 | * \qmltype Notification |
| 41 | * \inqmlmodule org.kde.notifications |
| 42 | * \nativetype KNotification |
| 43 | */ |
| 44 | class NotificationWrapper : public KNotification |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | QML_NAMED_ELEMENT(Notification) |
| 48 | |
| 49 | /*! |
| 50 | * \qmlproperty NotificationReplyAction Notification::replyAction |
| 51 | */ |
| 52 | Q_PROPERTY(KNotificationReplyAction *replyAction READ replyActionFactory CONSTANT) |
| 53 | |
| 54 | /*! |
| 55 | * \qmlproperty list<NotificationAction> Notification::actions |
| 56 | */ |
| 57 | Q_PROPERTY(QQmlListProperty<KNotificationAction> actions READ actionsProperty NOTIFY actionsChanged) |
| 58 | |
| 59 | /*! |
| 60 | * \qmlproperty NotificationAction Notification::defaultAction |
| 61 | */ |
| 62 | Q_PROPERTY(KNotificationAction *defaultAction READ defaultAction WRITE setDefaultActionQml NOTIFY defaultActionChanged) |
| 63 | public: |
| 64 | explicit NotificationWrapper(QObject *parent = nullptr); |
| 65 | |
| 66 | KNotificationReplyAction *replyActionFactory(); |
| 67 | |
| 68 | int actionCount() const; |
| 69 | |
| 70 | KNotificationAction *actionAt(qsizetype index); |
| 71 | |
| 72 | QQmlListProperty<KNotificationAction> actionsProperty() const; |
| 73 | |
| 74 | static qsizetype actionsCount(QQmlListProperty<KNotificationAction> *list); |
| 75 | |
| 76 | static void appendAction(QQmlListProperty<KNotificationAction> *list, KNotificationAction *value); |
| 77 | |
| 78 | static KNotificationAction *actionAt(QQmlListProperty<KNotificationAction> *list, qsizetype index); |
| 79 | |
| 80 | static void clearActions(QQmlListProperty<KNotificationAction> *list); |
| 81 | |
| 82 | private: |
| 83 | QQmlListProperty<KNotificationAction> m_actionsProperty; |
| 84 | }; |
| 85 | |
| 86 | /*! |
| 87 | * \qmltype NotificationPermission |
| 88 | * \inqmlmodule org.kde.notifications |
| 89 | * |
| 90 | * \brief Check or request permissions to show notifications on platforms where |
| 91 | * that is necessary. |
| 92 | */ |
| 93 | class NotificationPermissionWrapper : public QObject |
| 94 | { |
| 95 | Q_OBJECT |
| 96 | QML_NAMED_ELEMENT(NotificationPermission) |
| 97 | QML_SINGLETON |
| 98 | public: |
| 99 | /*! |
| 100 | * \qmlmethod bool NotificationPermission::checkPermission() |
| 101 | * |
| 102 | * Check if the current application has permissions to show notifications. |
| 103 | */ |
| 104 | Q_INVOKABLE bool checkPermission() |
| 105 | { |
| 106 | return KNotificationPermission::checkPermission() == Qt::PermissionStatus::Granted; |
| 107 | } |
| 108 | |
| 109 | /*! |
| 110 | * \qmlmethod void NotificationPermission::requestPermission(var callback) |
| 111 | * |
| 112 | * Request notification permissions. |
| 113 | */ |
| 114 | Q_INVOKABLE void requestPermission(const QJSValue &callback) |
| 115 | { |
| 116 | KNotificationPermission::requestPermission(context: this, callback: [callback](Qt::PermissionStatus status) { |
| 117 | callback.call(args: {status == Qt::PermissionStatus::Granted}); |
| 118 | }); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | #endif // KNOTIFICATIONQMLPLUGIN_H |
| 123 | |