File indexing completed on 2024-05-05 05:38:31

0001 /*
0002     SPDX-FileCopyrightText: 2018 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 <QObject>
0010 #include <QQmlEngine>
0011 
0012 #include "notificationmanager_export.h"
0013 #include "notifications.h"
0014 
0015 #include <qqmlregistration.h>
0016 
0017 namespace NotificationManager
0018 {
0019 class Notification;
0020 
0021 class ServerInfo;
0022 class ServerPrivate;
0023 
0024 /**
0025  * @short A notification DBus server
0026  *
0027  * @author Kai Uwe Broulik <kde@privat.broulik.de>
0028  **/
0029 class NOTIFICATIONMANAGER_EXPORT Server : public QObject
0030 {
0031     Q_OBJECT
0032     QML_ELEMENT
0033     QML_SINGLETON
0034 
0035     /**
0036      * Whether the notification service could be registered.
0037      * Call @c init() to register.
0038      */
0039     Q_PROPERTY(bool valid READ isValid NOTIFY validChanged)
0040 
0041     /**
0042      * Information about the current owner of the Notification service.
0043      *
0044      * This can be used to tell the user which application is currently
0045      * owning the service in case service registration failed.
0046      *
0047      * This is never null, even if there is no notification service running.
0048      *
0049      * @since 5.18
0050      */
0051     Q_PROPERTY(NotificationManager::ServerInfo *currentOwner READ currentOwner CONSTANT)
0052 
0053     /**
0054      * Whether notifications are currently inhibited.
0055      *
0056      * This is what is announced to other applications on the bus.
0057      *
0058      * @note This does not keep track of inhibitions on its own,
0059      * you need to calculate this yourself and update the property accordingly.
0060      */
0061     Q_PROPERTY(bool inhibited READ inhibited WRITE setInhibited NOTIFY inhibitedChanged)
0062 
0063 public:
0064     ~Server() override;
0065 
0066     /**
0067      * The reason a notification was closed
0068      */
0069     enum class CloseReason {
0070         Expired = 1, ///< The notification timed out
0071         DismissedByUser = 2, ///< The user explicitly closed or acknowledged the notification
0072         Revoked = 3, ///< The notification was revoked by the issuing app because it is no longer relevant
0073     };
0074     Q_ENUM(CloseReason)
0075 
0076     static Server &self();
0077 
0078     static Server *create(QQmlEngine *, QJSEngine *);
0079 
0080     /**
0081      * Registers the Notification Service on DBus.
0082      *
0083      * @return true if it succeeded, false otherwise.
0084      */
0085     bool init();
0086 
0087     /**
0088      * Whether the notification service could be registered
0089      */
0090     bool isValid() const;
0091 
0092     /**
0093      * Information about the current owner of the Notification service.
0094      * @since 5.18
0095      */
0096     ServerInfo *currentOwner() const;
0097 
0098     /**
0099      * Whether notifications are currently inhibited.
0100      * @since 5.17
0101      */
0102     bool inhibited() const;
0103 
0104     /**
0105      * Whether notifications are currently effectively inhibited.
0106      *
0107      * @note You need to keep track of inhibitions and call this
0108      * yourself when appropriate.
0109      * @since 5.17
0110      */
0111     void setInhibited(bool inhibited);
0112 
0113     /**
0114      * Whether an application requested to inhibit notifications.
0115      */
0116     bool inhibitedByApplication() const;
0117 
0118     // should we return a struct or pair or something?
0119     QStringList inhibitionApplications() const;
0120     QStringList inhibitionReasons() const;
0121 
0122     /**
0123      * Remove all inhibitions.
0124      *
0125      * @note The applications are not explicitly informed about this.
0126      */
0127     void clearInhibitions();
0128 
0129     /**
0130      * Sends a notification closed event
0131      *
0132      * @param id The notification ID
0133      * @param reason The reason why it was closed
0134      */
0135     void closeNotification(uint id, CloseReason reason);
0136     /**
0137      * Sends an action invocation request
0138      *
0139      * @param id The notification ID
0140      * @param actionName The name of the action, e.g. "Action 1", or "default"
0141      * @param xdgActivationToken The token the application needs to send to raise itself.
0142      * @param window the window that invokes the action
0143      */
0144     void invokeAction(uint id, const QString &actionName, const QString &xdgActivationToken, Notifications::InvokeBehavior behavior, QWindow *window);
0145 
0146     /**
0147      * Convenience call to maintain ABI
0148      *
0149      * @deprecated
0150      */
0151     void invokeAction(uint id, const QString &actionName, const QString &xdgActivationToken, Notifications::InvokeBehavior behavior)
0152     {
0153         invokeAction(id, actionName, xdgActivationToken, behavior, nullptr);
0154     }
0155 
0156     /**
0157      * Sends a notification reply text
0158      *
0159      * @param dbusService The bus name of the receiving application
0160      * @param id The notification ID
0161      * @param text The reply message text
0162      * @since 5.18
0163      */
0164     void reply(const QString &dbusService, uint id, const QString &text, Notifications::InvokeBehavior behavior);
0165 
0166     /**
0167      * Adds a notification
0168      *
0169      * @note The notification isn't actually broadcast
0170      * but just emitted locally.
0171      *
0172      * @return the ID of the notification
0173      */
0174     uint add(const Notification &notification);
0175 
0176 Q_SIGNALS:
0177     /**
0178      * Emitted when the notification service validity changes,
0179      * because it successfully registered the service or lost
0180      * ownership of it.
0181      * @since 5.18
0182      */
0183     void validChanged();
0184 
0185     /**
0186      * Emitted when a notification was added.
0187      * This is emitted regardless of any filtering rules or user settings.
0188      * @param notification The notification
0189      */
0190     void notificationAdded(const Notification &notification);
0191     /**
0192      * Emitted when a notification is supposed to be updated
0193      * This is emitted regardless of any filtering rules or user settings.
0194      * @param replacedId The ID of the notification it replaces
0195      * @param notification The new notification to use instead
0196      */
0197     void notificationReplaced(uint replacedId, const Notification &notification);
0198     /**
0199      * Emitted when a notification got removed (closed)
0200      * @param id The notification ID
0201      * @param reason The reason why it was closed
0202      */
0203     void notificationRemoved(uint id, CloseReason reason);
0204 
0205     /**
0206      * Emitted when the inhibited state changed.
0207      */
0208     void inhibitedChanged(bool inhibited);
0209 
0210     /**
0211      * Emitted when inhibitions by application have been changed.
0212      * Becomes true as soon as there is one inhibition and becomes
0213      * false again when all inhibitions have been lifted.
0214      * @since 5.17
0215      */
0216     void inhibitedByApplicationChanged(bool inhibited);
0217 
0218     /**
0219      * Emitted when the list of applications holding a notification
0220      * inhibition changes.
0221      * Normally you would only want to listen do @c inhibitedChanged
0222      */
0223     void inhibitionApplicationsChanged();
0224 
0225     /**
0226      * Emitted when the ownership of the Notification DBus Service is lost.
0227      */
0228     void serviceOwnershipLost();
0229 
0230 private:
0231     explicit Server(QObject *parent = nullptr);
0232     Q_DISABLE_COPY(Server)
0233     // FIXME we also need to disable move and other stuff?
0234 
0235     std::unique_ptr<ServerPrivate> d;
0236 };
0237 
0238 } // namespace NotificationManager