Qt Utilities  5.8.1
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  int timeout() const;
43  void setTimeout(int timeout);
44  const QStringList &actions() const;
45  void setActions(const QStringList &actions);
46  const QVariantMap &hints() const;
47  QVariantMap &hints();
48  bool isVisible() const;
49  void deleteOnCloseOrError();
50 
51 public Q_SLOTS:
52  bool show();
53  bool show(const QString &message);
54  bool update(const QString &line);
55  void hide();
56 
57 Q_SIGNALS:
59  void shown();
61  void error();
63  void closed(NotificationCloseReason reason);
65  void actionInvoked(const QString &action);
66 
67 private Q_SLOTS:
68  void handleNotifyResult(QDBusPendingCallWatcher *);
69  static void handleNotificationClosed(uint id, uint reason);
70  static void handleActionInvoked(uint id, const QString &action);
71 
72 private:
73  static void initInterface();
74 
75  uint m_id;
76  QDBusPendingCallWatcher *m_watcher;
77  QString m_title;
78  QString m_msg;
79  QString m_icon;
80  int m_timeout;
81  QStringList m_actions;
82  QVariantMap m_hints;
83  static OrgFreedesktopNotificationsInterface *m_dbusInterface;
84 };
85 
86 inline const QString &DBusNotification::title() const
87 {
88  return m_title;
89 }
90 
91 inline void DBusNotification::setTitle(const QString &title)
92 {
93  m_title = title;
94 }
95 
96 inline const QString &DBusNotification::message() const
97 {
98  return m_msg;
99 }
100 
101 inline void DBusNotification::setMessage(const QString &message)
102 {
103  m_msg = message;
104 }
105 
110 inline const QString &DBusNotification::icon() const
111 {
112  return m_icon;
113 }
114 
121 inline void DBusNotification::setIcon(const QString &icon)
122 {
123  m_icon = icon;
124 }
125 
126 inline int DBusNotification::timeout() const
127 {
128  return m_timeout;
129 }
130 
131 inline void DBusNotification::setTimeout(int timeout)
132 {
133  m_timeout = timeout;
134 }
135 
146 inline const QStringList &DBusNotification::actions() const
147 {
148  return m_actions;
149 }
150 
155 inline void DBusNotification::setActions(const QStringList &actions)
156 {
157  m_actions = actions;
158 }
159 
160 inline const QVariantMap &DBusNotification::hints() const
161 {
162  return m_hints;
163 }
164 
165 inline QVariantMap &DBusNotification::hints()
166 {
167  return m_hints;
168 }
169 
170 inline bool DBusNotification::isVisible() const
171 {
172  return m_id != 0;
173 }
174 } // namespace MiscUtils
175 
176 #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.
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