notificator.h raw
1 // Copyright (c) 2011-2022 The Limenka developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #ifndef LIMENKA_QT_NOTIFICATOR_H
6 #define LIMENKA_QT_NOTIFICATOR_H
7
8 #include <limenka-build-config.h> // IWYU pragma: keep
9
10 #include <QIcon>
11 #include <QObject>
12 #include <QThread>
13
14 #include <atomic>
15
16 QT_BEGIN_NAMESPACE
17 class QSystemTrayIcon;
18
19 #ifdef USE_DBUS
20 class QDBusInterface;
21 #endif
22 QT_END_NAMESPACE
23
24 class Notificator;
25
26 #ifdef USE_DBUS
27 class DBusInitThread : public QThread
28 {
29 Q_OBJECT
30
31 Notificator& m_notificator;
32
33 public:
34 DBusInitThread(Notificator& notificator) : m_notificator(notificator) {};
35
36 protected:
37 void run() override;
38 };
39 #endif
40
41 /** Cross-platform desktop notification client. */
42 class Notificator: public QObject
43 {
44 Q_OBJECT
45
46 public:
47 /** Create a new notificator.
48 @note Ownership of trayIcon is not transferred to this object.
49 */
50 Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent);
51 ~Notificator();
52
53 // Message class
54 enum Class
55 {
56 Information, /**< Informational message */
57 Warning, /**< Notify user of potential problem */
58 Critical /**< An error occurred */
59 };
60
61 public Q_SLOTS:
62 /** Show notification message.
63 @param[in] cls general message class
64 @param[in] title title shown with message
65 @param[in] text message content
66 @param[in] icon optional icon to show with message
67 @param[in] millisTimeout notification timeout in milliseconds (defaults to 10 seconds)
68 @note Platform implementations are free to ignore any of the provided fields except for \a text.
69 */
70 void notify(Class cls, const QString &title, const QString &text,
71 const QIcon &icon = QIcon(), int millisTimeout = 10000);
72
73 private:
74 QWidget *parent;
75 enum Mode {
76 None, /**< Ignore informational notifications, and show a modal pop-up dialog for Critical notifications. */
77 Freedesktop, /**< Use DBus org.freedesktop.Notifications */
78 QSystemTray, /**< Use QSystemTrayIcon::showMessage() */
79 UserNotificationCenter /**< Use the 10.8+ User Notification Center (Mac only) */
80 };
81 QString programName;
82 std::atomic<Mode> mode{None};
83 QSystemTrayIcon *trayIcon;
84 #ifdef USE_DBUS
85 QThread *m_dbus_init_thread{nullptr};
86 protected:
87 QDBusInterface* interface{nullptr};
88 friend class DBusInitThread;
89
90 private:
91 void notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout);
92 #endif
93 void notifySystray(Class cls, const QString &title, const QString &text, int millisTimeout);
94 #ifdef Q_OS_MACOS
95 void notifyMacUserNotificationCenter(const QString &title, const QString &text);
96 #endif
97 };
98
99 #endif // LIMENKA_QT_NOTIFICATOR_H
100