File indexing completed on 2024-04-28 16:54:37

0001 /*
0002     SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QDBusContext>
0010 #include <QObject>
0011 #include <QSet>
0012 #include <QStringList>
0013 #include <memory>
0014 
0015 #include "notification.h"
0016 
0017 class QDBusServiceWatcher;
0018 
0019 struct Inhibition {
0020     QString desktopEntry;
0021     QString applicationName;
0022     // QString applicationIconName;
0023     QString reason;
0024     QVariantMap hints;
0025 };
0026 
0027 namespace NotificationManager
0028 {
0029 class ServerInfo;
0030 
0031 class Q_DECL_HIDDEN ServerPrivate : public QObject, protected QDBusContext
0032 {
0033     Q_OBJECT
0034 
0035     // DBus
0036     // Inhibitions
0037     Q_PROPERTY(bool Inhibited READ inhibited)
0038 
0039 public:
0040     ServerPrivate(QObject *parent);
0041     ~ServerPrivate() override;
0042 
0043     // DBus
0044     uint Notify(const QString &app_name,
0045                 uint replaces_id,
0046                 const QString &app_icon,
0047                 const QString &summary,
0048                 const QString &body,
0049                 const QStringList &actions,
0050                 const QVariantMap &hints,
0051                 int timeout);
0052     void CloseNotification(uint id);
0053     QStringList GetCapabilities() const;
0054     QString GetServerInformation(QString &vendor, QString &version, QString &specVersion) const;
0055 
0056     // Inhibitions
0057     uint Inhibit(const QString &desktop_entry, const QString &reason, const QVariantMap &hints);
0058     void UnInhibit(uint cookie);
0059     bool inhibited() const; // property getter
0060 
0061     // Notifition watcher
0062     void RegisterWatcher();
0063     void UnRegisterWatcher();
0064 
0065     void InvokeAction(uint id, const QString &actionKey);
0066 
0067 Q_SIGNALS:
0068     // DBus
0069     void NotificationClosed(uint id, uint reason);
0070     void ActionInvoked(uint id, const QString &actionKey);
0071     void ActivationToken(uint id, const QString &xdgActivationToken);
0072     // non-standard
0073     // This is manually emitted as targeted signal in sendReplyText()
0074     void NotificationReplied(uint id, const QString &text);
0075 
0076     void validChanged();
0077 
0078     void inhibitedChanged();
0079 
0080     void externalInhibitedChanged();
0081     void externalInhibitionsChanged();
0082 
0083     void serviceOwnershipLost();
0084 
0085 public: // stuff used by public class
0086     friend class ServerInfo;
0087     static QString notificationServiceName();
0088     static QString notificationServicePath();
0089     static QString notificationServiceInterface();
0090 
0091     bool init();
0092     uint add(const Notification &notification);
0093     void sendReplyText(const QString &dbusService, uint notificationId, const QString &text, Notifications::InvokeBehavior behavior);
0094 
0095     ServerInfo *currentOwner() const;
0096 
0097     // Server only handles external application inhibitions but we still want the Inhibited property
0098     // expose the actual inhibition state for applications to check.
0099     void setInhibited(bool inhibited);
0100 
0101     bool externalInhibited() const;
0102     QList<Inhibition> externalInhibitions() const;
0103     void clearExternalInhibitions();
0104 
0105     bool m_valid = false;
0106     uint m_highestNotificationId = 1;
0107 
0108 private Q_SLOTS:
0109     void onBroadcastNotification(const QMap<QString, QVariant> &properties);
0110 
0111 private:
0112     friend class Server;
0113     void onServiceOwnershipLost(const QString &serviceName);
0114     void onInhibitionServiceUnregistered(const QString &serviceName);
0115     void onInhibitedChanged(); // Q_EMIT DBus change signal
0116 
0117     bool m_dbusObjectValid = false;
0118 
0119     mutable std::unique_ptr<ServerInfo> m_currentOwner;
0120 
0121     QDBusServiceWatcher *m_inhibitionWatcher = nullptr;
0122     QDBusServiceWatcher *m_notificationWatchers = nullptr;
0123     uint m_highestInhibitionCookie = 0;
0124     QHash<uint /*cookie*/, Inhibition> m_externalInhibitions;
0125     QHash<uint /*cookie*/, QString> m_inhibitionServices;
0126 
0127     bool m_inhibited = false;
0128 
0129     Notification m_lastNotification;
0130 };
0131 
0132 } // namespace NotificationManager