File indexing completed on 2024-04-28 15:29:09

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006 Olivier Goffart <ogoffart at kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KNOTIFICATIONPLUGIN_H
0008 #define KNOTIFICATIONPLUGIN_H
0009 
0010 #include <QObject>
0011 #include <QTextDocumentFragment>
0012 
0013 #include <KPluginFactory>
0014 
0015 #include "knotifications_export.h"
0016 
0017 #include <memory>
0018 
0019 class KNotification;
0020 class KNotificationPluginPrivate;
0021 class KNotifyConfig;
0022 
0023 /**
0024  * @class KNotificationPlugin knotificationplugin.h KNotificationPlugin
0025  *
0026  * @brief abstract class for KNotification actions
0027  *
0028  * A KNotificationPlugin is responsible of notification presentation.
0029  * You can subclass it to have your own presentation of a notification.
0030  *
0031  * You should reimplement the KNotificationPlugin::notify method to display the notification.
0032  *
0033  * @author Olivier Goffart <ogoffart at kde.org>
0034  */
0035 class KNOTIFICATIONS_EXPORT KNotificationPlugin : public QObject
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040     KNotificationPlugin(QObject *parent = nullptr, const QVariantList &args = QVariantList());
0041     ~KNotificationPlugin() override;
0042 
0043     /**
0044      * @brief return the name of this plugin.
0045      *
0046      * this is the name that should appear in the .notifyrc file,
0047      * in the field Action=... if a notification is set to use this plugin
0048      */
0049     virtual QString optionName() = 0;
0050 
0051     // TODO KF6 make notifyConfig const reference
0052     /**
0053      * This function is called when the notification is sent.
0054      * (or re-sent)
0055      * You should implement this function to display a notification
0056      *
0057      * for each call to this function (even for re-notification), you MUST call finish(KNotification*)
0058      *
0059      * @param notification is the KNotification object
0060      * @param notifyConfig is the configuration of the notification
0061      */
0062     virtual void notify(KNotification *notification, KNotifyConfig *notifyConfig) = 0;
0063 
0064     // TODO KF6 make config const reference
0065     /**
0066      * This function is called when the notification has changed (such as the text or the icon)
0067      */
0068     virtual void update(KNotification *notification, KNotifyConfig *config);
0069 
0070     /**
0071      * This function is called when the notification has been closed
0072      */
0073     virtual void close(KNotification *notification);
0074 
0075 protected:
0076     /**
0077      * emit the finished signal
0078      * you MUST call this function for each call to notify(), even if you do nothing there
0079      *
0080      * call it when the presentation is finished (because the user closed the popup or the sound is finished)
0081      *
0082      * If your presentation is synchronous, you can even call this function from the notify() call itself
0083      */
0084     void finish(KNotification *notification);
0085 
0086     static inline QString stripRichText(const QString &s)
0087     {
0088         return QTextDocumentFragment::fromHtml(s).toPlainText();
0089     }
0090 
0091 Q_SIGNALS:
0092     /**
0093      * the presentation is finished.
0094      */
0095     void finished(KNotification *notification);
0096     /**
0097      * emit this signal if one action was invoked
0098      * @param id is the id of the notification
0099      * @param action is the action number.  zero for the default action
0100      */
0101     void actionInvoked(int id, int action);
0102 
0103     void xdgActivationTokenReceived(int id, const QString &token);
0104 
0105     void replied(int id, const QString &text);
0106 
0107 private:
0108     std::unique_ptr<KNotificationPluginPrivate> const d;
0109 };
0110 
0111 #endif