File indexing completed on 2024-04-28 03:56:31

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "knotificationqmlplugin.h"
0008 
0009 #include <KNotification>
0010 #include <KNotificationPermission>
0011 #include <KNotificationReplyAction>
0012 
0013 class NotificationWrapper : public KNotification
0014 {
0015     Q_OBJECT
0016     Q_PROPERTY(KNotificationReplyAction *replyAction READ replyActionFactory CONSTANT)
0017     Q_PROPERTY(QQmlListProperty<KNotificationAction> actions READ actionsProperty NOTIFY actionsChanged)
0018     Q_PROPERTY(KNotificationAction *defaultAction READ defaultAction WRITE setDefaultActionQml NOTIFY defaultActionChanged)
0019 public:
0020     explicit NotificationWrapper(QObject *parent = nullptr)
0021         : KNotification(QString(), KNotification::CloseOnTimeout, parent)
0022     {
0023         setAutoDelete(false);
0024 
0025         m_actionsProperty = QQmlListProperty<KNotificationAction>(this,
0026                                                                   nullptr,
0027                                                                   &NotificationWrapper::appendAction,
0028                                                                   &NotificationWrapper::actionsCount,
0029                                                                   &NotificationWrapper::actionAt,
0030                                                                   &NotificationWrapper::clearActions);
0031     }
0032 
0033     KNotificationReplyAction *replyActionFactory()
0034     {
0035         if (!replyAction()) {
0036             setReplyAction(std::make_unique<KNotificationReplyAction>(QString()));
0037         }
0038         return replyAction();
0039     }
0040 
0041     int actionCount() const
0042     {
0043         return actions().count();
0044     }
0045 
0046     KNotificationAction *actionAt(qsizetype index)
0047     {
0048         return actions().at(index);
0049     }
0050 
0051     QQmlListProperty<KNotificationAction> actionsProperty() const
0052     {
0053         return m_actionsProperty;
0054     }
0055 
0056     static qsizetype actionsCount(QQmlListProperty<KNotificationAction> *list)
0057     {
0058         return static_cast<NotificationWrapper *>(list->object)->actionCount();
0059     }
0060 
0061     static void appendAction(QQmlListProperty<KNotificationAction> *list, KNotificationAction *value)
0062     {
0063         auto notification = static_cast<NotificationWrapper *>(list->object);
0064         auto actions = notification->actions();
0065         actions << value;
0066         notification->setActionsQml(actions);
0067     }
0068 
0069     static KNotificationAction *actionAt(QQmlListProperty<KNotificationAction> *list, qsizetype index)
0070     {
0071         return static_cast<NotificationWrapper *>(list->object)->actionAt(index);
0072     }
0073 
0074     static void clearActions(QQmlListProperty<KNotificationAction> *list)
0075     {
0076         auto notification = static_cast<KNotification *>(list->object);
0077         notification->clearActions();
0078     }
0079 
0080 private:
0081     QQmlListProperty<KNotificationAction> m_actionsProperty;
0082 };
0083 
0084 class NotificationPermissionWrapper
0085 {
0086     Q_GADGET
0087 public:
0088     Q_INVOKABLE bool checkPermission()
0089     {
0090         return KNotificationPermission::checkPermission() == Qt::PermissionStatus::Granted;
0091     }
0092 
0093     Q_INVOKABLE void requestPermission(const QJSValue &callback)
0094     {
0095         KNotificationPermission::requestPermission(m_engine, [&callback](Qt::PermissionStatus status) {
0096             callback.call({status == Qt::PermissionStatus::Granted});
0097         });
0098     }
0099 
0100     QQmlEngine *m_engine = nullptr;
0101 };
0102 
0103 void KNotificationQmlPlugin::registerTypes(const char *uri)
0104 {
0105     Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.notification"));
0106     qmlRegisterType<NotificationWrapper>(uri, 1, 0, "Notification");
0107     qmlRegisterType<KNotificationAction>(uri, 1, 0, "NotificationAction");
0108     qmlRegisterUncreatableType<KNotificationReplyAction>(uri, 1, 0, "NotificationReplyAction", {});
0109     qmlRegisterSingletonType("org.kde.notification", 1, 0, "NotificationPermission", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
0110         return engine->toScriptValue(NotificationPermissionWrapper{engine});
0111     });
0112 }
0113 
0114 #include "knotificationqmlplugin.moc"
0115 #include "moc_knotificationqmlplugin.cpp"