8 #include <QApplication> 11 #include <QImageWriter> 12 #include <QMessageBox> 15 #include <QSystemTrayIcon> 16 #include <QTemporaryFile> 27 #include <ApplicationServices/ApplicationServices.h> 34 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
40 programName(_programName),
47 if(_trayIcon && _trayIcon->supportsMessages())
52 interface = new QDBusInterface("org.freedesktop.Notifications",
53 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
54 if(interface->isValid())
77 class FreedesktopImage
81 explicit FreedesktopImage(
const QImage &img);
83 static int metaType();
86 static QVariant toVariant(
const QImage &img);
89 int width, height, stride;
95 friend QDBusArgument &operator<<(QDBusArgument &a,
const FreedesktopImage &i);
96 friend const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i);
99 Q_DECLARE_METATYPE(FreedesktopImage);
102 const int CHANNELS = 4;
103 const int BYTES_PER_PIXEL = 4;
104 const int BITS_PER_SAMPLE = 8;
106 FreedesktopImage::FreedesktopImage(
const QImage &img):
108 height(img.height()),
109 stride(img.width() * BYTES_PER_PIXEL),
112 bitsPerSample(BITS_PER_SAMPLE)
115 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
116 const uint32_t *data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
118 unsigned int num_pixels = width * height;
119 image.resize(num_pixels * BYTES_PER_PIXEL);
121 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
123 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
124 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
125 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
126 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
130 QDBusArgument &operator<<(QDBusArgument &a,
const FreedesktopImage &i)
133 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
138 const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i)
141 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
146 int FreedesktopImage::metaType()
148 return qDBusRegisterMetaType<FreedesktopImage>();
151 QVariant FreedesktopImage::toVariant(
const QImage &img)
153 FreedesktopImage fimg(img);
154 return QVariant(FreedesktopImage::metaType(), &fimg);
157 void Notificator::notifyDBus(
Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
161 QList<QVariant> args;
170 args.append(QString());
180 args.append(actions);
189 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
192 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
193 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
194 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
197 tmpicon = QApplication::style()->standardIcon(sicon);
203 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
207 args.append(millisTimeout);
210 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
217 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
220 case Information: sicon = QSystemTrayIcon::Information;
break;
221 case Warning: sicon = QSystemTrayIcon::Warning;
break;
222 case Critical: sicon = QSystemTrayIcon::Critical;
break;
224 trayIcon->showMessage(title, text, sicon, millisTimeout);
229 void Notificator::notifyMacUserNotificationCenter(
Class cls,
const QString &title,
const QString &text,
const QIcon &icon) {
242 notifyDBus(cls, title, text, icon, millisTimeout);
250 notifyMacUserNotificationCenter(cls, title, text, icon);
257 QMessageBox::critical(
parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
bool hasUserNotificationCenterSupport(void)
check if OS can handle UserNotifications
Use DBus org.freedesktop.Notifications.
Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent)
Create a new notificator.
Notify user of potential problem.
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.
static MacNotificationHandler * instance()
QSystemTrayIcon * trayIcon
Use QSystemTray::showMessage.
void notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
void showNotification(const QString &title, const QString &text)
shows a macOS 10.8+ UserNotification in the UserNotificationCenter