7 #include <QApplication> 9 #include <QImageWriter> 10 #include <QMessageBox> 13 #include <QSystemTrayIcon> 14 #include <QTemporaryFile> 25 #include <ApplicationServices/ApplicationServices.h> 32 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
38 programName(_programName),
45 if(_trayIcon && _trayIcon->supportsMessages())
50 interface = new QDBusInterface("
org.freedesktop.Notifications",
51 "/
org/freedesktop/Notifications", "
org.freedesktop.Notifications");
52 if(interface->isValid())
75 class FreedesktopImage
79 explicit FreedesktopImage(
const QImage &img);
81 static int metaType();
84 static QVariant toVariant(
const QImage &img);
87 int width, height, stride;
93 friend QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i);
94 friend const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i);
97 Q_DECLARE_METATYPE(FreedesktopImage);
100 const int CHANNELS = 4;
101 const int BYTES_PER_PIXEL = 4;
102 const int BITS_PER_SAMPLE = 8;
104 FreedesktopImage::FreedesktopImage(
const QImage &img):
106 height(img.height()),
107 stride(img.width() * BYTES_PER_PIXEL),
110 bitsPerSample(BITS_PER_SAMPLE)
113 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
114 const uint32_t *data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
116 unsigned int num_pixels = width * height;
117 image.resize(num_pixels * BYTES_PER_PIXEL);
119 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
121 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
122 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
123 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
124 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
128 QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i)
131 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
136 const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i)
139 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
144 int FreedesktopImage::metaType()
146 return qDBusRegisterMetaType<FreedesktopImage>();
149 QVariant FreedesktopImage::toVariant(
const QImage &img)
151 FreedesktopImage fimg(img);
152 return QVariant(FreedesktopImage::metaType(), &fimg);
155 void Notificator::notifyDBus(Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
159 QList<QVariant> args;
168 args.append(QString());
178 args.append(actions);
187 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
190 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
191 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
192 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
195 tmpicon = QApplication::style()->standardIcon(sicon);
201 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
205 args.append(millisTimeout);
208 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
215 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
218 case Information: sicon = QSystemTrayIcon::Information;
break;
219 case Warning: sicon = QSystemTrayIcon::Warning;
break;
220 case Critical: sicon = QSystemTrayIcon::Critical;
break;
222 trayIcon->showMessage(title, text, sicon, millisTimeout);
227 void Notificator::notifyMacUserNotificationCenter(Class cls,
const QString &title,
const QString &text,
const QIcon &icon) {
240 notifyDBus(cls, title, text, icon, millisTimeout);
248 notifyMacUserNotificationCenter(cls, title, text, icon);
255 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.
std::ostream & operator<<(std::ostream &os, governance_exception_type_enum_t eType)
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