File indexing completed on 2024-05-05 16:16:20

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006 Olivier Goffart <ogoffart at kde.org>
0003     SPDX-FileCopyrightText: 2008 Dmitry Suzdalev <dimsuz@gmail.com>
0004     SPDX-FileCopyrightText: 2014 Martin Klapetek <mklapetek@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #ifndef NOTIFYBYPOPUP_H
0010 #define NOTIFYBYPOPUP_H
0011 
0012 #include "knotificationplugin.h"
0013 
0014 #include "knotifyconfig.h"
0015 #include <QStringList>
0016 
0017 #include "notifications_interface.h"
0018 
0019 class KNotification;
0020 class QDBusPendingCallWatcher;
0021 
0022 class NotifyByPopup : public KNotificationPlugin
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit NotifyByPopup(QObject *parent = nullptr);
0027     ~NotifyByPopup() override;
0028 
0029     QString optionName() override
0030     {
0031         return QStringLiteral("Popup");
0032     }
0033     void notify(KNotification *notification, KNotifyConfig *notifyConfig) override;
0034     void close(KNotification *notification) override;
0035     void update(KNotification *notification, KNotifyConfig *config) override;
0036 
0037 private Q_SLOTS:
0038     // slot which gets called when DBus signals that some notification action was invoked
0039     void onNotificationActionInvoked(uint notificationId, const QString &actionKey);
0040     void onNotificationActionTokenReceived(uint notificationId, const QString &xdgActionToken);
0041     // slot which gets called when DBus signals that some notification was closed
0042     void onNotificationClosed(uint, uint);
0043     void onNotificationReplied(uint notificationId, const QString &text);
0044 
0045 private:
0046     // TODO KF6, replace current public notify/update
0047     void notify(KNotification *notification, const KNotifyConfig &notifyConfig);
0048     void update(KNotification *notification, const KNotifyConfig &notifyConfig);
0049 
0050     /**
0051      * Sends notification to DBus "org.freedesktop.notifications" interface.
0052      * @param id knotify-sid identifier of notification
0053      * @param config notification data
0054      * @param update If true, will request the DBus service to update
0055                      the notification with new data from \c notification
0056      *               Otherwise will put new notification on screen
0057      * @return true for success or false if there was an error.
0058      */
0059     bool sendNotificationToServer(KNotification *notification, const KNotifyConfig &config, bool update = false);
0060 
0061     /**
0062      * Find the caption and the icon name of the application
0063      */
0064     void getAppCaptionAndIconName(const KNotifyConfig &config, QString *appCaption, QString *iconName);
0065     /*
0066      * Query the dbus server for notification capabilities
0067      * If no DBus server is present, use fallback capabilities for KPassivePopup
0068      */
0069     void queryPopupServerCapabilities();
0070 
0071     /**
0072      * DBus notification daemon capabilities cache.
0073      * Do not use this variable. Use #popupServerCapabilities() instead.
0074      * @see popupServerCapabilities
0075      */
0076     QStringList m_popupServerCapabilities;
0077 
0078     /**
0079      * In case we still don't know notification server capabilities,
0080      * we need to query those first. That's done in an async way
0081      * so we queue all notifications while waiting for the capabilities
0082      * to return, then process them from this queue
0083      */
0084     QList<QPair<KNotification *, KNotifyConfig>> m_notificationQueue;
0085     /**
0086      * Whether the DBus notification daemon capability cache is up-to-date.
0087      */
0088     bool m_dbusServiceCapCacheDirty;
0089 
0090     /*
0091      * As we communicate with the notification server over dbus
0092      * we use only ids, this is for fast KNotifications lookup
0093      */
0094     QHash<uint, QPointer<KNotification>> m_notifications;
0095 
0096     org::freedesktop::Notifications m_dbusInterface;
0097 
0098     Q_DISABLE_COPY_MOVE(NotifyByPopup)
0099 };
0100 
0101 #endif