13#include <QSystemTrayIcon>
14#include <QTemporaryFile>
27const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
31 QSystemTrayIcon *_trayIcon, QWidget *_parent)
32 : QObject(_parent), parent(_parent), programName(_programName), mode(
None),
39 if (_trayIcon && _trayIcon->supportsMessages()) {
43 interface = new QDBusInterface("org.freedesktop.Notifications",
44 "/org/freedesktop/Notifications",
45 "org.freedesktop.Notifications");
46 if (interface->isValid()) {
53 ->hasUserNotificationCenterSupport()) {
68class FreedesktopImage {
71 explicit FreedesktopImage(
const QImage &img);
73 static int metaType();
76 static QVariant toVariant(
const QImage &img);
79 int width, height, stride;
85 friend QDBusArgument &
operator<<(QDBusArgument &a,
86 const FreedesktopImage &i);
87 friend const QDBusArgument &operator>>(
const QDBusArgument &a,
91Q_DECLARE_METATYPE(FreedesktopImage);
94const int CHANNELS = 4;
95const int BYTES_PER_PIXEL = 4;
96const int BITS_PER_SAMPLE = 8;
98FreedesktopImage::FreedesktopImage(
const QImage &img)
99 : width(img.width()), height(img.height()),
100 stride(img.width() * BYTES_PER_PIXEL), hasAlpha(true), channels(CHANNELS),
101 bitsPerSample(BITS_PER_SAMPLE) {
103 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
104 const uint32_t *data =
reinterpret_cast<const uint32_t *
>(tmp.bits());
106 unsigned int num_pixels = width * height;
107 image.resize(num_pixels * BYTES_PER_PIXEL);
109 for (
unsigned int ptr = 0; ptr < num_pixels; ++ptr) {
110 image[ptr * BYTES_PER_PIXEL + 0] = data[ptr] >> 16;
111 image[ptr * BYTES_PER_PIXEL + 1] = data[ptr] >> 8;
112 image[ptr * BYTES_PER_PIXEL + 2] = data[ptr];
113 image[ptr * BYTES_PER_PIXEL + 3] = data[ptr] >> 24;
117QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i) {
119 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample
120 << i.channels << i.image;
125const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i) {
127 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >>
128 i.channels >> i.image;
133int FreedesktopImage::metaType() {
134 return qDBusRegisterMetaType<FreedesktopImage>();
137QVariant FreedesktopImage::toVariant(
const QImage &img) {
138 FreedesktopImage fimg(img);
139 return QVariant(FreedesktopImage::metaType(), &fimg);
142void Notificator::notifyDBus(Class cls,
const QString &title,
143 const QString &text,
const QIcon &icon,
147 QList<QVariant> args;
157 args.append(QString());
167 args.append(actions);
175 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
178 sicon = QStyle::SP_MessageBoxInformation;
181 sicon = QStyle::SP_MessageBoxWarning;
184 sicon = QStyle::SP_MessageBoxCritical;
189 tmpicon = QApplication::style()->standardIcon(sicon);
193 hints[
"icon_data"] = FreedesktopImage::toVariant(
194 tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
198 args.append(millisTimeout);
201 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
206 const QString &text,
int millisTimeout) {
207 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
211 sicon = QSystemTrayIcon::Information;
214 sicon = QSystemTrayIcon::Warning;
217 sicon = QSystemTrayIcon::Critical;
220 trayIcon->showMessage(title, text, sicon, millisTimeout);
224void Notificator::notifyMacUserNotificationCenter(
const QString &title,
225 const QString &text) {
233 const QIcon &icon,
int millisTimeout) {
237 notifyDBus(cls, title, text, icon, millisTimeout);
245 notifyMacUserNotificationCenter(title, text);
252 QMessageBox::critical(
parent, title, text, QMessageBox::Ok,
static MacNotificationHandler * instance()
void showNotification(const QString &title, const QString &text)
shows a macOS 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 QSystemTrayIcon::showMessage()
@ Freedesktop
Use DBus org.freedesktop.Notifications.
void notifySystray(Class cls, const QString &title, const QString &text, 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.
std::ostream & operator<<(std::ostream &os, BigO const &bigO)