8 #include <QApplication>
10 #include <QImageWriter>
11 #include <QMessageBox>
14 #include <QSystemTrayIcon>
15 #include <QTemporaryFile>
28 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
33 programName(programName),
41 if (trayicon && trayicon->supportsMessages()) {
45 interface = new QDBusInterface("org.freedesktop.Notifications",
46 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
47 if (interface->isValid()) {
69 class FreedesktopImage
73 explicit FreedesktopImage(
const QImage& img);
75 static int metaType();
78 static QVariant toVariant(
const QImage& img);
81 int width{0}, height{0}, stride{0};
87 friend QDBusArgument&
operator<<(QDBusArgument& a,
const FreedesktopImage& i);
88 friend const QDBusArgument&
operator>>(
const QDBusArgument& a, FreedesktopImage& i);
94 const int CHANNELS = 4;
95 const int BYTES_PER_PIXEL = 4;
96 const int BITS_PER_SAMPLE = 8;
98 FreedesktopImage::FreedesktopImage(
const QImage& img) : width(img.width()),
100 stride(img.width() * BYTES_PER_PIXEL),
103 bitsPerSample(BITS_PER_SAMPLE)
106 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
107 const uint32_t* data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
109 unsigned int num_pixels = width * height;
110 image.resize(num_pixels * BYTES_PER_PIXEL);
112 for (
unsigned int ptr = 0; ptr < num_pixels; ++ptr) {
113 image[ptr * BYTES_PER_PIXEL + 0] = data[ptr] >> 16;
114 image[ptr * BYTES_PER_PIXEL + 1] = data[ptr] >> 8;
115 image[ptr * BYTES_PER_PIXEL + 2] = data[ptr];
116 image[ptr * BYTES_PER_PIXEL + 3] = data[ptr] >> 24;
120 QDBusArgument&
operator<<(QDBusArgument& a,
const FreedesktopImage& i)
123 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
128 const QDBusArgument&
operator>>(
const QDBusArgument& a, FreedesktopImage& i)
131 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
136 int FreedesktopImage::metaType()
138 return qDBusRegisterMetaType<FreedesktopImage>();
141 QVariant FreedesktopImage::toVariant(
const QImage& img)
143 FreedesktopImage fimg(img);
144 return QVariant(FreedesktopImage::metaType(), &fimg);
147 void Notificator::notifyDBus(Class cls,
const QString& title,
const QString& text,
const QIcon& icon,
int millisTimeout)
151 QList<QVariant> args;
160 args.append(QString());
170 args.append(actions);
178 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
181 sicon = QStyle::SP_MessageBoxInformation;
184 sicon = QStyle::SP_MessageBoxWarning;
187 sicon = QStyle::SP_MessageBoxCritical;
192 tmpicon = QApplication::style()->standardIcon(sicon);
196 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
200 args.append(millisTimeout);
203 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
210 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
214 sicon = QSystemTrayIcon::Information;
217 sicon = QSystemTrayIcon::Warning;
220 sicon = QSystemTrayIcon::Critical;
223 trayIcon->showMessage(title, text, sicon, millisTimeout);
228 void Notificator::notifyMacUserNotificationCenter(Class cls,
const QString& title,
const QString& text,
const QIcon& icon)
241 notifyDBus(cls, title, text, icon, millisTimeout);
249 notifyMacUserNotificationCenter(cls, title, text, icon);
255 QMessageBox::critical(
parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
const CBigNum operator>>(const CBigNum &a, unsigned int shift)
const CBigNum operator<<(const CBigNum &a, unsigned int shift)
static MacNotificationHandler * instance()
void showNotification(const QString &title, const QString &text)
shows a 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 QSystemTray::showMessage.
@ Freedesktop
Use DBus org.freedesktop.Notifications.
void notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, 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.
Q_DECLARE_METATYPE(interfaces::WalletBalances)