Qt Utilities  5.9.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 
16 
18 
19 class QT_UTILITIES_EXPORT DBusNotification : public QObject {
20  Q_OBJECT
21  Q_PROPERTY(QString title READ title WRITE setTitle)
22  Q_PROPERTY(QString message READ message WRITE setMessage)
23  Q_PROPERTY(QString icon READ icon WRITE setIcon)
24  Q_PROPERTY(int timeout READ timeout WRITE setTimeout)
25  Q_PROPERTY(QStringList actions READ actions WRITE setActions)
26  Q_PROPERTY(bool visible READ isVisible)
27 
28 public:
29  explicit DBusNotification(
30  const QString &title, NotificationIcon icon = NotificationIcon::Information, int timeout = 10000, QObject *parent = nullptr);
31  explicit DBusNotification(const QString &title, const QString &icon, int timeout = 10000, QObject *parent = nullptr);
33 
34  static bool isAvailable();
35  const QString &title() const;
36  void setTitle(const QString &title);
37  const QString &message() const;
38  void setMessage(const QString &message);
39  const QString &icon() const;
40  void setIcon(const QString &icon);
41  void setIcon(NotificationIcon icon);
42  const QImage image() const;
43  void setImage(const QImage &image);
44  const QString imagePath() const;
45  void setImagePath(const QString &imagePath);
46  int timeout() const;
47  void setTimeout(int timeout);
48  const QStringList &actions() const;
49  void setActions(const QStringList &actions);
50  const QVariantMap &hints() const;
51  QVariantMap &hints();
52  QVariant hint(const QString &name) const;
53  QVariant hint(const QString &name, const QString &fallbackNames...) const;
54  bool isVisible() const;
55  void deleteOnCloseOrError();
56 
57 public Q_SLOTS:
58  bool show();
59  bool show(const QString &message);
60  bool update(const QString &line);
61  void hide();
62 
63 Q_SIGNALS:
65  void shown();
67  void error();
69  void closed(NotificationCloseReason reason);
71  void actionInvoked(const QString &action);
72 
73 private Q_SLOTS:
74  void handleNotifyResult(QDBusPendingCallWatcher *);
75  static void handleNotificationClosed(uint id, uint reason);
76  static void handleActionInvoked(uint id, const QString &action);
77 
78 private:
79  static void initInterface();
80 
81  uint m_id;
82  QDBusPendingCallWatcher *m_watcher;
83  QString m_title;
84  QString m_msg;
85  QString m_icon;
86  int m_timeout;
87  QStringList m_actions;
88  QVariantMap m_hints;
89  static OrgFreedesktopNotificationsInterface *m_dbusInterface;
90 };
91 
92 inline const QString &DBusNotification::title() const
93 {
94  return m_title;
95 }
96 
97 inline void DBusNotification::setTitle(const QString &title)
98 {
99  m_title = title;
100 }
101 
102 inline const QString &DBusNotification::message() const
103 {
104  return m_msg;
105 }
106 
107 inline void DBusNotification::setMessage(const QString &message)
108 {
109  m_msg = message;
110 }
111 
116 inline const QString &DBusNotification::icon() const
117 {
118  return m_icon;
119 }
120 
127 inline void DBusNotification::setIcon(const QString &icon)
128 {
129  m_icon = icon;
130 }
131 
135 inline QVariant DBusNotification::hint(const QString &name) const
136 {
137  return m_hints[name];
138 }
139 
143 inline QVariant DBusNotification::hint(const QString &name, const QString &fallbackNames...) const
144 {
145  const auto variant(m_hints[name]);
146  return variant.isNull() ? this->hint(fallbackNames) : variant;
147 }
148 
153 inline const QString DBusNotification::imagePath() const
154 {
155  return hint(QStringLiteral("image-data"), QStringLiteral("image_path")).toString();
156 }
157 
163 inline void DBusNotification::setImagePath(const QString &imagePath)
164 {
165  m_hints[QStringLiteral("image-path")] = imagePath;
166 }
167 
168 inline int DBusNotification::timeout() const
169 {
170  return m_timeout;
171 }
172 
173 inline void DBusNotification::setTimeout(int timeout)
174 {
175  m_timeout = timeout;
176 }
177 
188 inline const QStringList &DBusNotification::actions() const
189 {
190  return m_actions;
191 }
192 
197 inline void DBusNotification::setActions(const QStringList &actions)
198 {
199  m_actions = actions;
200 }
201 
202 inline const QVariantMap &DBusNotification::hints() const
203 {
204  return m_hints;
205 }
206 
207 inline QVariantMap &DBusNotification::hints()
208 {
209  return m_hints;
210 }
211 
212 inline bool DBusNotification::isVisible() const
213 {
214  return m_id != 0;
215 }
216 } // namespace MiscUtils
217 
218 #endif // MISC_UTILS_NOTIFICATION_H
void setIcon(const QString &icon)
Sets the icon name.
void setActions(const QStringList &actions)
Sets the actions for the notification.
const QString imagePath() const
Returns the image path.
void setMessage(const QString &message)
Sets the message to be shown.
QVariant hint(const QString &name) const
Returns the hint with the specified name.
#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.
void setImagePath(const QString &imagePath)
Sets the image path.
const QString & icon() const
const QString & message() const