5#include <bitcoin-build-config.h>
11#include <QImageWriter>
15#include <QSystemTrayIcon>
16#include <QTemporaryFile>
19#include <QDBusMetaType>
30const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
36 programName(_programName),
39 if(_trayIcon && _trayIcon->supportsMessages())
44 interface = new QDBusInterface("org.freedesktop.Notifications",
45 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
46 if(interface->isValid())
72 FreedesktopImage() =
default;
73 explicit FreedesktopImage(
const QImage &img);
76 static QVariant toVariant(
const QImage &img);
79 int width, height, stride;
85 friend QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i);
86 friend const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i);
89Q_DECLARE_METATYPE(FreedesktopImage);
92const int CHANNELS = 4;
93const int BYTES_PER_PIXEL = 4;
94const int BITS_PER_SAMPLE = 8;
96FreedesktopImage::FreedesktopImage(
const QImage &img):
99 stride(img.width() * BYTES_PER_PIXEL),
102 bitsPerSample(BITS_PER_SAMPLE)
105 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
106 const uint32_t *
data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
108 unsigned int num_pixels = width * height;
109 image.resize(num_pixels * BYTES_PER_PIXEL);
111 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
113 image[ptr * BYTES_PER_PIXEL + 0] = char(
data[ptr] >> 16);
114 image[ptr * BYTES_PER_PIXEL + 1] = char(
data[ptr] >> 8);
115 image[ptr * BYTES_PER_PIXEL + 2] = char(
data[ptr]);
116 image[ptr * BYTES_PER_PIXEL + 3] = char(
data[ptr] >> 24);
120QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i)
123 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
128const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i)
131 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
136QVariant FreedesktopImage::toVariant(
const QImage &img)
138 FreedesktopImage fimg(img);
139 return QVariant(qDBusRegisterMetaType<FreedesktopImage>(), &fimg);
142void Notificator::notifyDBus(Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
146 QList<QVariant>
args;
155 args.append(QString());
165 args.append(actions);
174 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
177 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
178 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
179 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
182 tmpicon = QApplication::style()->standardIcon(sicon);
188 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
192 args.append(millisTimeout);
195 interface->callWithArgumentList(QDBus::NoBlock,
"Notify",
args);
201 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
204 case Information: sicon = QSystemTrayIcon::Information;
break;
206 case Critical: sicon = QSystemTrayIcon::Critical;
break;
208 trayIcon->showMessage(title, text, sicon, millisTimeout);
212void Notificator::notifyMacUserNotificationCenter(
const QString &title,
const QString &text)
225 notifyDBus(cls, title, text, icon, millisTimeout);
233 notifyMacUserNotificationCenter(title, text);
240 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)