Qt Utilities 6.14.3
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
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 <QSet>
8#include <QVariantMap>
9
10#include <functional>
11
12QT_FORWARD_DECLARE_CLASS(QDBusPendingCallWatcher)
13
14class OrgFreedesktopNotificationsInterface;
15
16namespace QtUtilities {
17
19
21
22class QT_UTILITIES_EXPORT DBusNotification : public QObject {
23 Q_OBJECT
24 Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName)
25 Q_PROPERTY(QString title READ title WRITE setTitle)
26 Q_PROPERTY(QString message READ message WRITE setMessage)
27 Q_PROPERTY(QString icon READ icon WRITE setIcon)
28 Q_PROPERTY(int timeout READ timeout WRITE setTimeout)
29 Q_PROPERTY(QStringList actions READ actions WRITE setActions)
30 Q_PROPERTY(bool visible READ isVisible)
31 Q_PROPERTY(bool pending READ isPending)
32
33public:
34 using IDType = uint;
35 class QT_UTILITIES_EXPORT Capabilities : public QSet<QString> {
36 public:
37 explicit Capabilities();
38 explicit Capabilities(const QStringList &capabilities);
39 bool isValid() const;
40 bool supportsBody() const;
41 bool supportsLinks() const;
42 bool supportsMarkup() const;
43 bool supportsImages() const;
44 bool supportsIcon() const;
45 bool supportsActions() const;
46 bool supportsAnimatedIcon() const;
47 bool supportsActionIcons() const;
48 bool supportsSound() const;
49 bool supportsPercistence() const;
50
51 private:
52 bool m_valid;
53 };
54
55 explicit DBusNotification(
56 const QString &title, NotificationIcon icon = NotificationIcon::Information, int timeout = 10000, QObject *parent = nullptr);
57 explicit DBusNotification(const QString &title, const QString &icon, int timeout = 10000, QObject *parent = nullptr);
58 ~DBusNotification() override;
59
60 static bool isAvailable();
61 const QString &applicationName() const;
62 void setApplicationName(const QString &applicationName);
63 const QString &title() const;
64 void setTitle(const QString &title);
65 const QString &message() const;
66 void setMessage(const QString &message);
67 const QString &icon() const;
68 void setIcon(const QString &icon);
69 void setIcon(NotificationIcon icon);
70 const QImage image() const;
71 void setImage(const QImage &image);
72 const QString imagePath() const;
73 void setImagePath(const QString &imagePath);
74 int timeout() const;
75 void setTimeout(int timeout);
76 int urgency() const;
77 void setUrgency(quint8 urgency);
78 bool isResident() const;
79 void setResident(bool resident);
80 QString category() const;
81 void setCategory(const QString &category);
82 const QStringList &actions() const;
83 void setActions(const QStringList &actions);
84 const QVariantMap &hints() const;
85 QVariantMap &hints();
86 QVariant hint(const QString &name) const;
87 QVariant hint(const QString &name, const QString &fallbackNames...) const;
88 bool isVisible() const;
89 bool isPending() const;
90 void deleteOnCloseOrError();
91 static bool queryCapabilities(const std::function<void(Capabilities &&capabilities)> &callback);
92
93public Q_SLOTS:
94 bool show();
95 bool show(const QString &message);
96 bool update(const QString &line);
97 bool hide();
98
99Q_SIGNALS:
101 void shown();
103 void error();
107 void actionInvoked(const QString &action);
108
109private Q_SLOTS:
110 void handleNotifyResult(QDBusPendingCallWatcher *);
111 static void handleNotificationClosed(IDType id, uint reason);
112 static void handleActionInvoked(IDType id, const QString &action);
113
114private:
115 static void initInterface();
116
117 IDType m_id;
118 QDBusPendingCallWatcher *m_watcher;
119 QString m_applicationName;
120 QString m_title;
121 QString m_msg;
122 QString m_icon;
123 int m_timeout;
124 QStringList m_actions;
125 QVariantMap m_hints;
126 static OrgFreedesktopNotificationsInterface *s_dbusInterface;
127};
128
130 : m_valid(false)
131{
132}
133
134inline DBusNotification::Capabilities::Capabilities(const QStringList &capabilities)
135#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
136 : QSet<QString>(capabilities.toSet())
137#else
138 : QSet<QString>(capabilities.begin(), capabilities.end())
139#endif
140 , m_valid(true)
141{
142}
143
145{
146 return m_valid;
147}
148
150{
151 return contains(QStringLiteral("body"));
152}
153
155{
156 return contains(QStringLiteral("body-hyperlinks"));
157}
158
160{
161 return contains(QStringLiteral("body-markup"));
162}
163
165{
166 return contains(QStringLiteral("body-images"));
167}
168
170{
171 return contains(QStringLiteral("icon-static")) || supportsAnimatedIcon();
172}
173
175{
176 return contains(QStringLiteral("actions"));
177}
178
180{
181 return contains(QStringLiteral("icon-multi"));
182}
183
185{
186 return contains(QStringLiteral("action-icons"));
187}
188
190{
191 return contains(QStringLiteral("sound"));
192}
193
195{
196 return contains(QStringLiteral("persistence"));
197}
198
203inline const QString &DBusNotification::applicationName() const
204{
205 return m_applicationName;
206}
207
213{
214 m_applicationName = applicationName;
215}
216
217inline const QString &DBusNotification::title() const
218{
219 return m_title;
220}
221
222inline void DBusNotification::setTitle(const QString &title)
223{
224 m_title = title;
225}
226
227inline const QString &DBusNotification::message() const
228{
229 return m_msg;
230}
231
232inline void DBusNotification::setMessage(const QString &message)
233{
234 m_msg = message;
235}
236
241inline const QString &DBusNotification::icon() const
242{
243 return m_icon;
244}
245
252inline void DBusNotification::setIcon(const QString &icon)
253{
254 m_icon = icon;
255}
256
260inline QVariant DBusNotification::hint(const QString &name) const
261{
262 return m_hints[name];
263}
264
268inline QVariant DBusNotification::hint(const QString &name, const QString &fallbackNames...) const
269{
270 const auto variant(m_hints[name]);
271 return variant.isNull() ? this->hint(fallbackNames) : variant;
272}
273
278inline const QString DBusNotification::imagePath() const
279{
280 return hint(QStringLiteral("image-data"), QStringLiteral("image_path")).toString();
281}
282
288inline void DBusNotification::setImagePath(const QString &imagePath)
289{
290 m_hints[QStringLiteral("image-path")] = imagePath;
291}
292
294{
295 return m_timeout;
296}
297
299{
300 m_timeout = timeout;
301}
302
307{
308 return m_hints[QStringLiteral("urgency")].toInt();
309}
310
315{
316 m_hints[QStringLiteral("urgency")] = urgency;
317}
318
323{
324 return m_hints[QStringLiteral("resident")].toBool();
325}
326
330inline void DBusNotification::setResident(bool resident)
331{
332 m_hints[QStringLiteral("resident")] = resident;
333}
334
339inline QString DBusNotification::category() const
340{
341 return m_hints[QStringLiteral("category")].toString();
342}
343
348inline void DBusNotification::setCategory(const QString &category)
349{
350 m_hints[QStringLiteral("category")] = category;
351}
352
363inline const QStringList &DBusNotification::actions() const
364{
365 return m_actions;
366}
367
372inline void DBusNotification::setActions(const QStringList &actions)
373{
374 m_actions = actions;
375}
376
377inline const QVariantMap &DBusNotification::hints() const
378{
379 return m_hints;
380}
381
382inline QVariantMap &DBusNotification::hints()
383{
384 return m_hints;
385}
386
388{
389 return m_id != 0;
390}
391} // namespace QtUtilities
392
393#endif // MISC_UTILS_NOTIFICATION_H
The DBusNotification class emits D-Bus notifications.
QString message
Returns the assigned message.
void shown()
Emitted when the notification could be shown successful.
void setUrgency(quint8 urgency)
Sets the urgency level (0 = low, 1 = normal, 2 = critical).
bool isVisible() const
Returns whether the notification is (still) visible.
int urgency() const
Returns the urgency level (0 = low, 1 = normal, 2 = critical).
void setApplicationName(const QString &applicationName)
Sets the application name to be used.
void setIcon(const QString &icon)
Sets the icon name.
void actionInvoked(const QString &action)
Emitted when action has been invoked.
void setCategory(const QString &category)
Sets the category.
QString category() const
Returns the category.
QStringList actions
Returns the assigned actions.
const QString imagePath() const
Returns the image path.
void setTitle(const QString &title)
const QVariantMap & hints() const
bool isResident() const
Returns whether the notification will remain visible after an action has been clicked.
void setActions(const QStringList &actions)
Sets the actions for the notification.
void closed(NotificationCloseReason reason)
Emitted when the notification has been closed.
void setImagePath(const QString &imagePath)
Sets the image path.
void error()
Emitted when the notification couldn't be shown.
int timeout
Returns the number of milliseconds the notification will be visible after calling show().
void setResident(bool resident)
Sets whether the notification will remain visible after an action has been clicked.
QVariant hint(const QString &name) const
Returns the hint with the specified name.
void setTimeout(int timeout)
Sets the number of milliseconds the notification will be visible after calling show().
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.
Definition global.h:14