| 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 |
|
| 12 |
QT_FORWARD_DECLARE_CLASS(QDBusPendingCallWatcher) |
| 13 |
|
| 14 |
class OrgFreedesktopNotificationsInterface; |
| 15 |
|
| 16 |
namespace 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 |
|
| 33 |
public: |
| 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 |
|
| 93 |
public Q_SLOTS: |
| 94 |
bool show(); |
| 95 |
bool show(const QString &message); |
| 96 |
bool update(const QString &line); |
| 97 |
bool hide(); |
| 98 |
|
| 99 |
Q_SIGNALS: |
| 100 |
|
| 101 |
void shown(); |
| 102 |
|
| 103 |
void error(); |
| 104 |
|
| 105 |
void closed(NotificationCloseReason reason); |
| 106 |
|
| 107 |
void actionInvoked(const QString &action); |
| 108 |
|
| 109 |
private Q_SLOTS: |
| 110 |
void handleNotifyResult(QDBusPendingCallWatcher *); |
| 111 |
static void handleNotificationClosed(IDType id, uint reason); |
| 112 |
static void handleActionInvoked(IDType id, const QString &action); |
| 113 |
|
| 114 |
private: |
| 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 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
inline const QString &DBusNotification::applicationName() const |
| 204 |
{ |
| 205 |
return m_applicationName; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 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 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
inline const QString &DBusNotification::icon() const |
| 242 |
{ |
| 243 |
return m_icon; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
inline void DBusNotification::setIcon(const QString &icon) |
| 253 |
{ |
| 254 |
m_icon = icon; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
inline QVariant DBusNotification::hint(const QString &name) const |
| 261 |
{ |
| 262 |
return m_hints[name]; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 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 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
inline const QString DBusNotification::imagePath() const |
| 279 |
{ |
| 280 |
return hint(QStringLiteral("image-data"), QStringLiteral("image_path")).toString(); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 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 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
inline int DBusNotification::urgency() const |
| 307 |
{ |
| 308 |
return m_hints[QStringLiteral("urgency")].toInt(); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
inline void DBusNotification::setUrgency(quint8 urgency) |
| 315 |
{ |
| 316 |
m_hints[QStringLiteral("urgency")] = urgency; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
inline bool DBusNotification::isResident() const |
| 323 |
{ |
| 324 |
return m_hints[QStringLiteral("resident")].toBool(); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
inline void DBusNotification::setResident(bool resident) |
| 331 |
{ |
| 332 |
m_hints[QStringLiteral("resident")] = resident; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
inline QString DBusNotification::category() const |
| 340 |
{ |
| 341 |
return m_hints[QStringLiteral("category")].toString(); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
inline void DBusNotification::setCategory(const QString &category) |
| 349 |
{ |
| 350 |
m_hints[QStringLiteral("category")] = category; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
inline const QStringList &DBusNotification::actions() const |
| 364 |
{ |
| 365 |
return m_actions; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 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 |
} |
| 392 |
|
| 393 |
#endif |