KNotifications

knotificationqmlplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "knotificationqmlplugin.h"
8
9#include <KNotification>
10#include <KNotificationPermission>
11#include <KNotificationReplyAction>
12
13class NotificationWrapper : public KNotification
14{
16 Q_PROPERTY(KNotificationReplyAction *replyAction READ replyActionFactory CONSTANT)
17 Q_PROPERTY(QQmlListProperty<KNotificationAction> actions READ actionsProperty NOTIFY actionsChanged)
18 Q_PROPERTY(KNotificationAction *defaultAction READ defaultAction WRITE setDefaultActionQml NOTIFY defaultActionChanged)
19public:
20 explicit NotificationWrapper(QObject *parent = nullptr)
22 {
23 setAutoDelete(false);
24
25 m_actionsProperty = QQmlListProperty<KNotificationAction>(this,
26 nullptr,
27 &NotificationWrapper::appendAction,
28 &NotificationWrapper::actionsCount,
29 &NotificationWrapper::actionAt,
31 }
32
33 KNotificationReplyAction *replyActionFactory()
34 {
35 if (!replyAction()) {
36 setReplyAction(std::make_unique<KNotificationReplyAction>(QString()));
37 }
38 return replyAction();
39 }
40
41 int actionCount() const
42 {
43 return actions().count();
44 }
45
46 KNotificationAction *actionAt(qsizetype index)
47 {
48 return actions().at(index);
49 }
50
51 QQmlListProperty<KNotificationAction> actionsProperty() const
52 {
53 return m_actionsProperty;
54 }
55
56 static qsizetype actionsCount(QQmlListProperty<KNotificationAction> *list)
57 {
58 return static_cast<NotificationWrapper *>(list->object)->actionCount();
59 }
60
61 static void appendAction(QQmlListProperty<KNotificationAction> *list, KNotificationAction *value)
62 {
63 auto notification = static_cast<NotificationWrapper *>(list->object);
64 auto actions = notification->actions();
65 actions << value;
66 notification->setActionsQml(actions);
67 }
68
69 static KNotificationAction *actionAt(QQmlListProperty<KNotificationAction> *list, qsizetype index)
70 {
71 return static_cast<NotificationWrapper *>(list->object)->actionAt(index);
72 }
73
75 {
76 auto notification = static_cast<KNotification *>(list->object);
77 notification->clearActions();
78 }
79
80private:
82};
83
84class NotificationPermissionWrapper
85{
86 Q_GADGET
87public:
88 Q_INVOKABLE bool checkPermission()
89 {
90 return KNotificationPermission::checkPermission() == Qt::PermissionStatus::Granted;
91 }
92
93 Q_INVOKABLE void requestPermission(const QJSValue &callback)
94 {
96 callback.call({status == Qt::PermissionStatus::Granted});
97 });
98 }
99
100 QQmlEngine *m_engine = nullptr;
101};
102
103void KNotificationQmlPlugin::registerTypes(const char *uri)
104{
105 Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.notification"));
106 qmlRegisterType<NotificationWrapper>(uri, 1, 0, "Notification");
107 qmlRegisterType<KNotificationAction>(uri, 1, 0, "NotificationAction");
108 qmlRegisterUncreatableType<KNotificationReplyAction>(uri, 1, 0, "NotificationReplyAction", {});
109 qmlRegisterSingletonType("org.kde.notification", 1, 0, "NotificationPermission", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
110 return engine->toScriptValue(NotificationPermissionWrapper{engine});
111 });
112}
113
114#include "knotificationqmlplugin.moc"
115#include "moc_knotificationqmlplugin.cpp"
This class represents a notification.
KNotification is the main class for creating notifications.
void defaultActionChanged()
Emitted when defaultAction changed.
void clearActions()
Removes all actions previously added by addAction() from the notification.
void setAutoDelete(bool autoDelete)
Sets whether this notification object will be automatically deleted after closing.
void actionsChanged()
Emitted when actions changed.
@ CloseOnTimeout
The notification will be automatically closed after a timeout.
void setReplyAction(std::unique_ptr< KNotificationReplyAction > replyAction)
Add an inline reply action to the notification.
Q_SCRIPTABLE CaptureState status()
KNOTIFICATIONS_EXPORT void requestPermission(QObject *context, const std::function< void(Qt::PermissionStatus)> &callback)
Request notification permissions.
KNOTIFICATIONS_EXPORT Qt::PermissionStatus checkPermission()
Check if the current application has permissions to show notifications.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QJSValue toScriptValue(const T &value)
QJSValue call(const QJSValueList &args) const const
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
QObject * parent() const const
PermissionStatus
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.