notificator.h raw
1 // Copyright (c) 2011-present The Bitcoin Core 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 BITCOIN_QT_NOTIFICATOR_H
6 #define BITCOIN_QT_NOTIFICATOR_H
7
8 #include <bitcoin-build-config.h> // IWYU pragma: keep
9
10 #include <QIcon>
11 #include <QObject>
12
13 QT_BEGIN_NAMESPACE
14 class QSystemTrayIcon;
15
16 #ifdef USE_DBUS
17 class QDBusInterface;
18 #endif
19 QT_END_NAMESPACE
20
21 /** Cross-platform desktop notification client. */
22 class Notificator: public QObject
23 {
24 Q_OBJECT
25
26 public:
27 /** Create a new notificator.
28 @note Ownership of trayIcon is not transferred to this object.
29 */
30 Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent);
31 ~Notificator();
32
33 // Message class
34 enum Class
35 {
36 Information, /**< Informational message */
37 Warning, /**< Notify user of potential problem */
38 Critical /**< An error occurred */
39 };
40
41 public Q_SLOTS:
42 /** Show notification message.
43 @param[in] cls general message class
44 @param[in] title title shown with message
45 @param[in] text message content
46 @param[in] icon optional icon to show with message
47 @param[in] millisTimeout notification timeout in milliseconds (defaults to 10 seconds)
48 @note Platform implementations are free to ignore any of the provided fields except for \a text.
49 */
50 void notify(Class cls, const QString &title, const QString &text,
51 const QIcon &icon = QIcon(), int millisTimeout = 10000);
52
53 private:
54 QWidget *parent;
55 enum Mode {
56 None, /**< Ignore informational notifications, and show a modal pop-up dialog for Critical notifications. */
57 Freedesktop, /**< Use DBus org.freedesktop.Notifications */
58 QSystemTray, /**< Use QSystemTrayIcon::showMessage() */
59 UserNotificationCenter /**< Use the 10.8+ User Notification Center (Mac only) */
60 };
61 QString programName;
62 Mode mode{None};
63 QSystemTrayIcon *trayIcon;
64 #ifdef USE_DBUS
65 QDBusInterface* interface{nullptr};
66
67 void notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout);
68 #endif
69 void notifySystray(Class cls, const QString &title, const QString &text, int millisTimeout);
70 #ifdef Q_OS_MACOS
71 void notifyMacUserNotificationCenter(const QString &title, const QString &text);
72 #endif
73 };
74
75 #endif // BITCOIN_QT_NOTIFICATOR_H
76