7 #include <QApplication>
9 #include <QImageWriter>
10 #include <QMessageBox>
13 #include <QSystemTrayIcon>
14 #include <QTemporaryFile>
17 #include <QDBusMetaType>
28 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
34 programName(_programName),
37 if(_trayIcon && _trayIcon->supportsMessages())
42 interface = new QDBusInterface("org.freedesktop.Notifications",
43 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
44 if(interface->isValid())
67 class FreedesktopImage
70 FreedesktopImage() =
default;
71 explicit FreedesktopImage(
const QImage &img);
74 static QVariant toVariant(
const QImage &img);
77 int width, height, stride;
83 friend QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i);
84 friend const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i);
87 Q_DECLARE_METATYPE(FreedesktopImage);
90 const int CHANNELS = 4;
91 const int BYTES_PER_PIXEL = 4;
92 const int BITS_PER_SAMPLE = 8;
94 FreedesktopImage::FreedesktopImage(
const QImage &img):
97 stride(img.width() * BYTES_PER_PIXEL),
100 bitsPerSample(BITS_PER_SAMPLE)
103 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
104 const uint32_t *data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
106 unsigned int num_pixels = width * height;
107 image.resize(num_pixels * BYTES_PER_PIXEL);
109 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
111 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
112 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
113 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
114 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
118 QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i)
121 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
126 const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i)
129 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
134 QVariant FreedesktopImage::toVariant(
const QImage &img)
136 FreedesktopImage fimg(img);
137 return QVariant(qDBusRegisterMetaType<FreedesktopImage>(), &fimg);
140 void Notificator::notifyDBus(Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
144 QList<QVariant>
args;
153 args.append(QString());
163 args.append(actions);
172 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
175 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
176 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
177 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
180 tmpicon = QApplication::style()->standardIcon(sicon);
186 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
190 args.append(millisTimeout);
193 interface->callWithArgumentList(QDBus::NoBlock,
"Notify",
args);
199 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
202 case Information: sicon = QSystemTrayIcon::Information;
break;
203 case Warning: sicon = QSystemTrayIcon::Warning;
break;
204 case Critical: sicon = QSystemTrayIcon::Critical;
break;
206 trayIcon->showMessage(title, text, sicon, millisTimeout);
210 void Notificator::notifyMacUserNotificationCenter(
const QString &title,
const QString &text)
223 notifyDBus(cls, title, text, icon, millisTimeout);
231 notifyMacUserNotificationCenter(title, text);
238 QMessageBox::critical(
parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
QDataStream & operator>>(QDataStream &in, BitcoinUnit &unit)
static MacNotificationHandler * instance()
void showNotification(const QString &title, const QString &text)
shows a macOS 10.8+ UserNotification in the UserNotificationCenter
@ Information
Informational message.
@ Critical
An error occurred.
@ Warning
Notify user of potential problem.
@ UserNotificationCenter
Use the 10.8+ User Notification Center (Mac only)
@ QSystemTray
Use QSystemTrayIcon::showMessage()
@ Freedesktop
Use DBus org.freedesktop.Notifications.
void notifySystray(Class cls, const QString &title, const QString &text, int millisTimeout)
Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent)
Create a new notificator.
QSystemTrayIcon * trayIcon
void notify(Class cls, const QString &title, const QString &text, const QIcon &icon=QIcon(), int millisTimeout=10000)
Show notification message.
std::ostream & operator<<(std::ostream &os, BigO const &bigO)