9 #include <QApplication> 11 #include <QImageWriter> 12 #include <QMessageBox> 15 #include <QSystemTrayIcon> 16 #include <QTemporaryFile> 19 #include <QDBusMetaType> 30 const 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())
69 class FreedesktopImage
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);
89 Q_DECLARE_METATYPE(FreedesktopImage);
92 const int CHANNELS = 4;
93 const int BYTES_PER_PIXEL = 4;
94 const int BITS_PER_SAMPLE = 8;
96 FreedesktopImage::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);
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 QVariant FreedesktopImage::toVariant(
const QImage &img)
138 FreedesktopImage fimg(img);
139 return QVariant(qDBusRegisterMetaType<FreedesktopImage>(), &fimg);
142 void 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);
212 void 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);
Use DBus org.freedesktop.Notifications.
bool hasUserNotificationCenterSupport()
check if OS can handle UserNotifications
Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent)
Create a new notificator.
Notify user of potential problem.
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
QDataStream & operator>>(QDataStream &in, BitcoinUnit &unit)
Use the 10.8+ User Notification Center (Mac only)
void notify(Class cls, const QString &title, const QString &text, const QIcon &icon=QIcon(), int millisTimeout=10000)
Show notification message.
void notifySystray(Class cls, const QString &title, const QString &text, int millisTimeout)
static MacNotificationHandler * instance()
QSystemTrayIcon * trayIcon
Use QSystemTrayIcon::showMessage()
void showNotification(const QString &title, const QString &text)
shows a macOS 10.8+ UserNotification in the UserNotificationCenter