7 #include <QApplication> 9 #include <QImageWriter> 10 #include <QMessageBox> 13 #include <QSystemTrayIcon> 14 #include <QTemporaryFile> 27 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
33 programName(_programName),
40 if(_trayIcon && _trayIcon->supportsMessages())
45 interface = new QDBusInterface("org.freedesktop.Notifications",
46 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
47 if(interface->isValid())
70 class FreedesktopImage
74 explicit FreedesktopImage(
const QImage &img);
76 static int metaType();
79 static QVariant toVariant(
const QImage &img);
82 int width, height, stride;
88 friend QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i);
89 friend const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i);
92 Q_DECLARE_METATYPE(FreedesktopImage);
95 const int CHANNELS = 4;
96 const int BYTES_PER_PIXEL = 4;
97 const int BITS_PER_SAMPLE = 8;
99 FreedesktopImage::FreedesktopImage(
const QImage &img):
101 height(img.height()),
102 stride(img.width() * BYTES_PER_PIXEL),
105 bitsPerSample(BITS_PER_SAMPLE)
108 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
109 const uint32_t *data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
111 unsigned int num_pixels = width * height;
112 image.resize(num_pixels * BYTES_PER_PIXEL);
114 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
116 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
117 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
118 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
119 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
123 QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i)
126 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
131 const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i)
134 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
139 int FreedesktopImage::metaType()
141 return qDBusRegisterMetaType<FreedesktopImage>();
144 QVariant FreedesktopImage::toVariant(
const QImage &img)
146 FreedesktopImage fimg(img);
147 return QVariant(FreedesktopImage::metaType(), &fimg);
150 void Notificator::notifyDBus(
Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
154 QList<QVariant> args;
163 args.append(QString());
173 args.append(actions);
182 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
185 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
186 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
187 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
190 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);
209 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
212 case Information: sicon = QSystemTrayIcon::Information;
break;
213 case Warning: sicon = QSystemTrayIcon::Warning;
break;
214 case Critical: sicon = QSystemTrayIcon::Critical;
break;
216 trayIcon->showMessage(title, text, sicon, millisTimeout);
220 void Notificator::notifyMacUserNotificationCenter(
const QString &title,
const QString &text)
233 notifyDBus(cls, title, text, icon, millisTimeout);
241 notifyMacUserNotificationCenter(title, text);
248 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)
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