Qt Utilities  5.6.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
dbusnotification.h
Go to the documentation of this file.
1 #ifndef MISC_UTILS_NOTIFICATION_H
2 #define MISC_UTILS_NOTIFICATION_H
3 
4 #include "../global.h"
5 
6 #include <QObject>
7 #include <QVariantMap>
8 
9 QT_FORWARD_DECLARE_CLASS(QDBusPendingCallWatcher)
10 
11 class OrgFreedesktopNotificationsInterface;
12 
13 namespace MiscUtils {
14 
15 enum class NotificationIcon
16 {
17  NoIcon,
19  Warning,
20  Critical
21 };
22 
24 {
25  Undefined,
26  Expired,
27  Dismissed,
28  Manually,
30 };
31 
32 class QT_UTILITIES_EXPORT DBusNotification : public QObject
33 {
34  Q_OBJECT
35  Q_PROPERTY(QString title READ title WRITE setTitle)
36  Q_PROPERTY(QString message READ message WRITE setMessage)
37  Q_PROPERTY(QString icon READ icon WRITE setIcon)
38  Q_PROPERTY(int timeout READ timeout WRITE setTimeout)
39  Q_PROPERTY(QStringList actions READ actions WRITE setActions)
40  Q_PROPERTY(bool visible READ isVisible)
41 
42 public:
43  explicit DBusNotification(const QString &title, NotificationIcon icon = NotificationIcon::Information, int timeout = 10000, QObject *parent = nullptr);
44  explicit DBusNotification(const QString &title, const QString &icon, int timeout = 10000, QObject *parent = nullptr);
46 
47  static bool isAvailable();
48  const QString &title() const;
49  void setTitle(const QString &title);
50  const QString &message() const;
51  void setMessage(const QString &message);
52  const QString &icon() const;
53  void setIcon(const QString &icon);
54  void setIcon(NotificationIcon icon);
55  int timeout() const;
56  void setTimeout(int timeout);
57  const QStringList &actions() const;
58  void setActions(const QStringList &actions);
59  const QVariantMap &hints() const;
60  QVariantMap &hints();
61  bool isVisible() const;
62  void deleteOnCloseOrError();
63 
64 public Q_SLOTS:
65  bool show();
66  bool show(const QString &message);
67  bool update(const QString &line);
68  void hide();
69 
70 Q_SIGNALS:
72  void shown();
74  void error();
76  void closed(NotificationCloseReason reason);
78  void actionInvoked(const QString &action);
79 
80 private Q_SLOTS:
81  void handleNotifyResult(QDBusPendingCallWatcher *);
82  static void handleNotificationClosed(uint id, uint reason);
83  static void handleActionInvoked(uint id, const QString &action);
84 
85 private:
86  static void initInterface();
87 
88  uint m_id;
89  QDBusPendingCallWatcher *m_watcher;
90  QString m_title;
91  QString m_msg;
92  QString m_icon;
93  int m_timeout;
94  QStringList m_actions;
95  QVariantMap m_hints;
96  static OrgFreedesktopNotificationsInterface *m_dbusInterface;
97 };
98 
99 inline const QString &DBusNotification::title() const
100 {
101  return m_title;
102 }
103 
104 inline void DBusNotification::setTitle(const QString &title)
105 {
106  m_title = title;
107 }
108 
109 inline const QString &DBusNotification::message() const
110 {
111  return m_msg;
112 }
113 
114 inline void DBusNotification::setMessage(const QString &message)
115 {
116  m_msg = message;
117 }
118 
123 inline const QString &DBusNotification::icon() const
124 {
125  return m_icon;
126 }
127 
134 inline void DBusNotification::setIcon(const QString &icon)
135 {
136  m_icon = icon;
137 }
138 
139 inline int DBusNotification::timeout() const
140 {
141  return m_timeout;
142 }
143 
144 inline void DBusNotification::setTimeout(int timeout)
145 {
146  m_timeout = timeout;
147 }
148 
149 inline const QStringList &DBusNotification::actions() const
150 {
151  return m_actions;
152 }
153 
154 inline void DBusNotification::setActions(const QStringList &actions)
155 {
156  m_actions = actions;
157 }
158 
159 inline const QVariantMap &DBusNotification::hints() const
160 {
161  return m_hints;
162 }
163 
164 inline QVariantMap &DBusNotification::hints()
165 {
166  return m_hints;
167 }
168 
169 inline bool DBusNotification::isVisible() const
170 {
171  return m_id != 0;
172 }
173 
174 }
175 
176 #endif // MISC_UTILS_NOTIFICATION_H
void setIcon(const QString &icon)
Sets the icon name.
void setActions(const QStringList &actions)
Sets the list of available actions.
void setMessage(const QString &message)
Sets the message to be shown.
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
bool isVisible() const
Returns whether the notification is (still) visible.
void setTitle(const QString &title)
void setTimeout(int timeout)
Sets the number of milliseconds the notification will be visible after calling show().
const QStringList & actions() const
const QVariantMap & hints() const
The DBusNotification class emits D-Bus notifications.
const QString & icon() const
const QString & message() const