Qt Utilities: misc/dbusnotification.h Source File

Qt Utilities 6.20.0 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
18 enum class NotificationIcon { NoIcon, Information, Warning, Critical };
19
20 enum class NotificationCloseReason { Undefined, Expired, Dismissed, Manually, ActionInvoked };
21
22 class 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();
105 void closed(NotificationCloseReason reason);
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
129 inline DBusNotification::Capabilities::Capabilities()
130 : m_valid(false)
131{
132}
133
134 inline 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
144 inline bool DBusNotification::Capabilities::isValid() const
145{
146 return m_valid;
147}
148
149 inline bool DBusNotification::Capabilities::supportsBody() const
150{
151 return contains(QStringLiteral("body"));
152}
153
154 inline bool DBusNotification::Capabilities::supportsLinks() const
155{
156 return contains(QStringLiteral("body-hyperlinks"));
157}
158
159 inline bool DBusNotification::Capabilities::supportsMarkup() const
160{
161 return contains(QStringLiteral("body-markup"));
162}
163
164 inline bool DBusNotification::Capabilities::supportsImages() const
165{
166 return contains(QStringLiteral("body-images"));
167}
168
169 inline bool DBusNotification::Capabilities::supportsIcon() const
170{
171 return contains(QStringLiteral("icon-static")) || supportsAnimatedIcon();
172}
173
174 inline bool DBusNotification::Capabilities::supportsActions() const
175{
176 return contains(QStringLiteral("actions"));
177}
178
179 inline bool DBusNotification::Capabilities::supportsAnimatedIcon() const
180{
181 return contains(QStringLiteral("icon-multi"));
182}
183
184 inline bool DBusNotification::Capabilities::supportsActionIcons() const
185{
186 return contains(QStringLiteral("action-icons"));
187}
188
189 inline bool DBusNotification::Capabilities::supportsSound() const
190{
191 return contains(QStringLiteral("sound"));
192}
193
194 inline bool DBusNotification::Capabilities::supportsPercistence() const
195{
196 return contains(QStringLiteral("persistence"));
197}
198
203 inline const QString &DBusNotification::applicationName() const
204{
205 return m_applicationName;
206}
207
212 inline void DBusNotification::setApplicationName(const QString &applicationName)
213{
214 m_applicationName = applicationName;
215}
216
217 inline const QString &DBusNotification::title() const
218{
219 return m_title;
220}
221
222 inline void DBusNotification::setTitle(const QString &title)
223{
224 m_title = title;
225}
226
227 inline const QString &DBusNotification::message() const
228{
229 return m_msg;
230}
231
232 inline void DBusNotification::setMessage(const QString &message)
233{
234 m_msg = message;
235}
236
241 inline const QString &DBusNotification::icon() const
242{
243 return m_icon;
244}
245
252 inline void DBusNotification::setIcon(const QString &icon)
253{
254 m_icon = icon;
255}
256
260 inline QVariant DBusNotification::hint(const QString &name) const
261{
262 return m_hints[name];
263}
264
268 inline 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
278 inline const QString DBusNotification::imagePath() const
279{
280 return hint(QStringLiteral("image-data"), QStringLiteral("image_path")).toString();
281}
282
288 inline void DBusNotification::setImagePath(const QString &imagePath)
289{
290 m_hints[QStringLiteral("image-path")] = imagePath;
291}
292
293 inline int DBusNotification::timeout() const
294{
295 return m_timeout;
296}
297
298 inline void DBusNotification::setTimeout(int timeout)
299{
300 m_timeout = timeout;
301}
302
306 inline int DBusNotification::urgency() const
307{
308 return m_hints[QStringLiteral("urgency")].toInt();
309}
310
314 inline void DBusNotification::setUrgency(quint8 urgency)
315{
316 m_hints[QStringLiteral("urgency")] = urgency;
317}
318
322 inline bool DBusNotification::isResident() const
323{
324 return m_hints[QStringLiteral("resident")].toBool();
325}
326
330 inline void DBusNotification::setResident(bool resident)
331{
332 m_hints[QStringLiteral("resident")] = resident;
333}
334
339 inline QString DBusNotification::category() const
340{
341 return m_hints[QStringLiteral("category")].toString();
342}
343
348 inline void DBusNotification::setCategory(const QString &category)
349{
350 m_hints[QStringLiteral("category")] = category;
351}
352
363 inline const QStringList &DBusNotification::actions() const
364{
365 return m_actions;
366}
367
372 inline void DBusNotification::setActions(const QStringList &actions)
373{
374 m_actions = actions;
375}
376
377 inline const QVariantMap &DBusNotification::hints() const
378{
379 return m_hints;
380}
381
382 inline QVariantMap &DBusNotification::hints()
383{
384 return m_hints;
385}
386
387 inline bool DBusNotification::isVisible() const
388{
389 return m_id != 0;
390}
391} // namespace QtUtilities
392
393#endif // MISC_UTILS_NOTIFICATION_H
QtUtilities::DBusNotification::Capabilities
Definition dbusnotification.h:35
QtUtilities::DBusNotification::Capabilities::supportsPercistence
bool supportsPercistence() const
Definition dbusnotification.h:194
QtUtilities::DBusNotification::Capabilities::supportsAnimatedIcon
bool supportsAnimatedIcon() const
Definition dbusnotification.h:179
QtUtilities::DBusNotification::Capabilities::supportsImages
bool supportsImages() const
Definition dbusnotification.h:164
QtUtilities::DBusNotification::Capabilities::Capabilities
Capabilities()
Definition dbusnotification.h:129
QtUtilities::DBusNotification::Capabilities::supportsLinks
bool supportsLinks() const
Definition dbusnotification.h:154
QtUtilities::DBusNotification::Capabilities::supportsMarkup
bool supportsMarkup() const
Definition dbusnotification.h:159
QtUtilities::DBusNotification::Capabilities::supportsSound
bool supportsSound() const
Definition dbusnotification.h:189
QtUtilities::DBusNotification::Capabilities::supportsActionIcons
bool supportsActionIcons() const
Definition dbusnotification.h:184
QtUtilities::DBusNotification::Capabilities::supportsIcon
bool supportsIcon() const
Definition dbusnotification.h:169
QtUtilities::DBusNotification::Capabilities::supportsActions
bool supportsActions() const
Definition dbusnotification.h:174
QtUtilities::DBusNotification::Capabilities::supportsBody
bool supportsBody() const
Definition dbusnotification.h:149
QtUtilities::DBusNotification::Capabilities::isValid
bool isValid() const
Definition dbusnotification.h:144
QtUtilities::DBusNotification::deleteOnCloseOrError
void deleteOnCloseOrError()
Makes the notification object delete itself when the notification has been closed or an error occurre...
Definition dbusnotification.cpp:281
QtUtilities::DBusNotification::message
QString message
Returns the assigned message.
Definition dbusnotification.h:26
QtUtilities::DBusNotification::visible
bool visible
Definition dbusnotification.h:30
QtUtilities::DBusNotification::update
bool update(const QString &line)
Updates the message and shows/updates the notification.
Definition dbusnotification.cpp:342
QtUtilities::DBusNotification::shown
void shown()
Emitted when the notification could be shown successful.
QtUtilities::DBusNotification::setUrgency
void setUrgency(quint8 urgency)
Sets the urgency level (0 = low, 1 = normal, 2 = critical).
Definition dbusnotification.h:314
QtUtilities::DBusNotification::isVisible
bool isVisible() const
Returns whether the notification is (still) visible.
Definition dbusnotification.h:387
QtUtilities::DBusNotification::isPending
bool isPending() const
Returns whether the notification is about to be shown after calling show() or update() but has not be...
Definition dbusnotification.cpp:272
QtUtilities::DBusNotification::image
const QImage image() const
Returns the image.
Definition dbusnotification.cpp:248
QtUtilities::DBusNotification::urgency
int urgency() const
Returns the urgency level (0 = low, 1 = normal, 2 = critical).
Definition dbusnotification.h:306
QtUtilities::DBusNotification::pending
bool pending
Definition dbusnotification.h:31
QtUtilities::DBusNotification::setApplicationName
void setApplicationName(const QString &applicationName)
Sets the application name to be used.
Definition dbusnotification.h:212
QtUtilities::DBusNotification::setIcon
void setIcon(const QString &icon)
Sets the icon name.
Definition dbusnotification.h:252
QtUtilities::DBusNotification::queryCapabilities
static bool queryCapabilities(const std::function< void(Capabilities &&capabilities)> &callback)
Definition dbusnotification.cpp:356
QtUtilities::DBusNotification::actionInvoked
void actionInvoked(const QString &action)
Emitted when action has been invoked.
QtUtilities::DBusNotification::icon
QString icon
Definition dbusnotification.h:27
QtUtilities::DBusNotification::setCategory
void setCategory(const QString &category)
Sets the category.
Definition dbusnotification.h:348
QtUtilities::DBusNotification::category
QString category() const
Returns the category.
Definition dbusnotification.h:339
QtUtilities::DBusNotification::actions
QStringList actions
Returns the assigned actions.
Definition dbusnotification.h:29
QtUtilities::DBusNotification::DBusNotification
DBusNotification(const QString &title, NotificationIcon icon=NotificationIcon::Information, int timeout=10000, QObject *parent=nullptr)
Creates a new notification (which is not shown instantly).
Definition dbusnotification.cpp:162
QtUtilities::DBusNotification::imagePath
const QString imagePath() const
Returns the image path.
Definition dbusnotification.h:278
QtUtilities::DBusNotification::setTitle
void setTitle(const QString &title)
Definition dbusnotification.h:222
QtUtilities::DBusNotification::hints
const QVariantMap & hints() const
Definition dbusnotification.h:377
QtUtilities::DBusNotification::isAvailable
static bool isAvailable()
Returns whether the notification D-Bus daemon is running.
Definition dbusnotification.cpp:219
QtUtilities::DBusNotification::isResident
bool isResident() const
Returns whether the notification will remain visible after an action has been clicked.
Definition dbusnotification.h:322
QtUtilities::DBusNotification::setActions
void setActions(const QStringList &actions)
Sets the actions for the notification.
Definition dbusnotification.h:372
QtUtilities::DBusNotification::closed
void closed(NotificationCloseReason reason)
Emitted when the notification has been closed.
QtUtilities::DBusNotification::setImagePath
void setImagePath(const QString &imagePath)
Sets the image path.
Definition dbusnotification.h:288
QtUtilities::DBusNotification::error
void error()
Emitted when the notification couldn't be shown.
QtUtilities::DBusNotification::timeout
int timeout
Returns the number of milliseconds the notification will be visible after calling show().
Definition dbusnotification.h:28
QtUtilities::DBusNotification::setResident
void setResident(bool resident)
Sets whether the notification will remain visible after an action has been clicked.
Definition dbusnotification.h:330
QtUtilities::DBusNotification::applicationName
QString applicationName
Definition dbusnotification.h:24
QtUtilities::DBusNotification::hide
bool hide()
Hides the notification (if still visible).
Definition dbusnotification.cpp:383
QtUtilities::DBusNotification::hint
QVariant hint(const QString &name) const
Returns the hint with the specified name.
Definition dbusnotification.h:260
QtUtilities::DBusNotification::setImage
void setImage(const QImage &image)
Sets the image.
Definition dbusnotification.cpp:259
QtUtilities::DBusNotification::setTimeout
void setTimeout(int timeout)
Sets the number of milliseconds the notification will be visible after calling show().
Definition dbusnotification.h:298
QtUtilities::DBusNotification::setMessage
void setMessage(const QString &message)
Sets the message to be shown.
Definition dbusnotification.h:232
QtUtilities::DBusNotification::title
QString title
Definition dbusnotification.h:25
QtUtilities::DBusNotification::show
bool show()
Shows the notification.
Definition dbusnotification.cpp:296
QtUtilities::DBusNotification::IDType
uint IDType
Definition dbusnotification.h:34
global.h
QT_UTILITIES_EXPORT
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
Definition global.h:14
QtUtilities
!
Definition trylocker.h:8
QtUtilities::NotificationIcon
NotificationIcon
Definition dbusnotification.h:18
QtUtilities::NotificationIcon::Warning
@ Warning
Definition dbusnotification.h:18
QtUtilities::NotificationIcon::Critical
@ Critical
Definition dbusnotification.h:18
QtUtilities::NotificationIcon::NoIcon
@ NoIcon
Definition dbusnotification.h:18
QtUtilities::NotificationIcon::Information
@ Information
Definition dbusnotification.h:18
QtUtilities::NotificationCloseReason
NotificationCloseReason
Definition dbusnotification.h:20
QtUtilities::NotificationCloseReason::Expired
@ Expired
Definition dbusnotification.h:20
QtUtilities::NotificationCloseReason::ActionInvoked
@ ActionInvoked
Definition dbusnotification.h:20
QtUtilities::NotificationCloseReason::Manually
@ Manually
Definition dbusnotification.h:20
QtUtilities::NotificationCloseReason::Dismissed
@ Dismissed
Definition dbusnotification.h:20
QtUtilities::NotificationCloseReason::Undefined
@ Undefined
Definition dbusnotification.h:20