Changeset View
Changeset View
Standalone View
Standalone View
src/qt/notificator.cpp
Show All 32 Lines | |||||
#endif | #endif | ||||
Notificator::Notificator(const QString &_programName, | Notificator::Notificator(const QString &_programName, | ||||
QSystemTrayIcon *_trayIcon, QWidget *_parent) | QSystemTrayIcon *_trayIcon, QWidget *_parent) | ||||
: QObject(_parent), parent(_parent), programName(_programName), mode(None), | : QObject(_parent), parent(_parent), programName(_programName), mode(None), | ||||
trayIcon(_trayIcon) | trayIcon(_trayIcon) | ||||
#ifdef USE_DBUS | #ifdef USE_DBUS | ||||
, | , | ||||
interface(0) | interface(nullptr) | ||||
#endif | #endif | ||||
{ | { | ||||
if (_trayIcon && _trayIcon->supportsMessages()) { | if (_trayIcon && _trayIcon->supportsMessages()) { | ||||
mode = QSystemTray; | mode = QSystemTray; | ||||
} | } | ||||
#ifdef USE_DBUS | #ifdef USE_DBUS | ||||
interface = new QDBusInterface("org.freedesktop.Notifications", | interface = new QDBusInterface("org.freedesktop.Notifications", | ||||
"/org/freedesktop/Notifications", | "/org/freedesktop/Notifications", | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | |||||
QVariant FreedesktopImage::toVariant(const QImage &img) { | QVariant FreedesktopImage::toVariant(const QImage &img) { | ||||
FreedesktopImage fimg(img); | FreedesktopImage fimg(img); | ||||
return QVariant(FreedesktopImage::metaType(), &fimg); | return QVariant(FreedesktopImage::metaType(), &fimg); | ||||
} | } | ||||
void Notificator::notifyDBus(Class cls, const QString &title, | void Notificator::notifyDBus(Class cls, const QString &title, | ||||
const QString &text, const QIcon &icon, | const QString &text, const QIcon &icon, | ||||
int millisTimeout) { | int millisTimeout) { | ||||
Q_UNUSED(cls); | // https://developer.gnome.org/notification-spec/ | ||||
// Arguments for DBus call: | // Arguments for DBus call: | ||||
QList<QVariant> args; | QList<QVariant> args; | ||||
// Program Name: | // Program Name: | ||||
args.append(programName); | args.append(programName); | ||||
// Unique ID of this notification type: | // Replaces ID; A value of 0 means that this notification won't replace any | ||||
// existing notifications: | |||||
args.append(0U); | args.append(0U); | ||||
// Application Icon, empty string | // Application Icon, empty string | ||||
args.append(QString()); | args.append(QString()); | ||||
// Summary | // Summary | ||||
args.append(title); | args.append(title); | ||||
Show All 36 Lines | void Notificator::notifyDBus(Class cls, const QString &title, | ||||
args.append(millisTimeout); | args.append(millisTimeout); | ||||
// "Fire and forget" | // "Fire and forget" | ||||
interface->callWithArgumentList(QDBus::NoBlock, "Notify", args); | interface->callWithArgumentList(QDBus::NoBlock, "Notify", args); | ||||
} | } | ||||
#endif | #endif | ||||
void Notificator::notifySystray(Class cls, const QString &title, | void Notificator::notifySystray(Class cls, const QString &title, | ||||
const QString &text, const QIcon &icon, | const QString &text, int millisTimeout) { | ||||
int millisTimeout) { | |||||
Q_UNUSED(icon); | |||||
QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon; | QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon; | ||||
switch (cls) // Set icon based on class | // Set icon based on class | ||||
{ | switch (cls) { | ||||
case Information: | case Information: | ||||
sicon = QSystemTrayIcon::Information; | sicon = QSystemTrayIcon::Information; | ||||
break; | break; | ||||
case Warning: | case Warning: | ||||
sicon = QSystemTrayIcon::Warning; | sicon = QSystemTrayIcon::Warning; | ||||
break; | break; | ||||
case Critical: | case Critical: | ||||
sicon = QSystemTrayIcon::Critical; | sicon = QSystemTrayIcon::Critical; | ||||
break; | break; | ||||
} | } | ||||
trayIcon->showMessage(title, text, sicon, millisTimeout); | trayIcon->showMessage(title, text, sicon, millisTimeout); | ||||
} | } | ||||
// Based on Qt's tray icon implementation | |||||
#ifdef Q_OS_MAC | #ifdef Q_OS_MAC | ||||
void Notificator::notifyMacUserNotificationCenter(Class cls, | void Notificator::notifyMacUserNotificationCenter(const QString &title, | ||||
const QString &title, | const QString &text) { | ||||
const QString &text, | |||||
const QIcon &icon) { | |||||
// icon is not supported by the user notification center yet. OSX will use | // icon is not supported by the user notification center yet. OSX will use | ||||
// the app icon. | // the app icon. | ||||
MacNotificationHandler::instance()->showNotification(title, text); | MacNotificationHandler::instance()->showNotification(title, text); | ||||
} | } | ||||
#endif | #endif | ||||
void Notificator::notify(Class cls, const QString &title, const QString &text, | void Notificator::notify(Class cls, const QString &title, const QString &text, | ||||
const QIcon &icon, int millisTimeout) { | const QIcon &icon, int millisTimeout) { | ||||
switch (mode) { | switch (mode) { | ||||
#ifdef USE_DBUS | #ifdef USE_DBUS | ||||
case Freedesktop: | case Freedesktop: | ||||
notifyDBus(cls, title, text, icon, millisTimeout); | notifyDBus(cls, title, text, icon, millisTimeout); | ||||
break; | break; | ||||
#endif | #endif | ||||
case QSystemTray: | case QSystemTray: | ||||
notifySystray(cls, title, text, icon, millisTimeout); | notifySystray(cls, title, text, millisTimeout); | ||||
break; | break; | ||||
#ifdef Q_OS_MAC | #ifdef Q_OS_MAC | ||||
case UserNotificationCenter: | case UserNotificationCenter: | ||||
notifyMacUserNotificationCenter(cls, title, text, icon); | notifyMacUserNotificationCenter(title, text); | ||||
break; | break; | ||||
#endif | #endif | ||||
default: | default: | ||||
if (cls == Critical) { | if (cls == Critical) { | ||||
// Fall back to old fashioned pop-up dialog if critical and no | // Fall back to old fashioned pop-up dialog if critical and no | ||||
// other notification available | // other notification available | ||||
QMessageBox::critical(parent, title, text, QMessageBox::Ok, | QMessageBox::critical(parent, title, text, QMessageBox::Ok, | ||||
QMessageBox::Ok); | QMessageBox::Ok); | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
} | } |