File indexing completed on 2024-04-21 03:56:29

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, const KNotifyConfig &notifyConfig) override;
0034     void close(KNotification *notification) override;
0035     void update(KNotification *notification, const KNotifyConfig &notifyConfig) 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     /**
0047      * Sends notification to DBus "org.freedesktop.notifications" interface.
0048      * @param id knotify-sid identifier of notification
0049      * @param config notification data
0050      * @param update If true, will request the DBus service to update
0051                      the notification with new data from \c notification
0052      *               Otherwise will put new notification on screen
0053      * @return true for success or false if there was an error.
0054      */
0055     bool sendNotificationToServer(KNotification *notification, const KNotifyConfig &config, bool update = false);
0056 
0057     /**
0058      * Find the caption and the icon name of the application
0059      */
0060     void getAppCaptionAndIconName(const KNotifyConfig &config, QString *appCaption, QString *iconName);
0061     /*
0062      * Query the dbus server for notification capabilities
0063      */
0064     void queryPopupServerCapabilities();
0065 
0066     /**
0067      * DBus notification daemon capabilities cache.
0068      * Do not use this variable. Use #popupServerCapabilities() instead.
0069      * @see popupServerCapabilities
0070      */
0071     QStringList m_popupServerCapabilities;
0072 
0073     /**
0074      * In case we still don't know notification server capabilities,
0075      * we need to query those first. That's done in an async way
0076      * so we queue all notifications while waiting for the capabilities
0077      * to return, then process them from this queue
0078      */
0079     QList<QPair<KNotification *, KNotifyConfig>> m_notificationQueue;
0080     /**
0081      * Whether the DBus notification daemon capability cache is up-to-date.
0082      */
0083     bool m_dbusServiceCapCacheDirty;
0084 
0085     /*
0086      * As we communicate with the notification server over dbus
0087      * we use only ids, this is for fast KNotifications lookup
0088      */
0089     QHash<uint, QPointer<KNotification>> m_notifications;
0090 
0091     org::freedesktop::Notifications m_dbusInterface;
0092 
0093     Q_DISABLE_COPY_MOVE(NotifyByPopup)
0094 };
0095 
0096 #endif