13#include <QSystemTrayIcon>
14#include <QTemporaryFile>
17#include <QDBusMetaType>
28const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
32 QSystemTrayIcon *_trayIcon, QWidget *_parent)
33 : QObject(_parent), parent(_parent), programName(_programName), mode(
None),
40 if (_trayIcon && _trayIcon->supportsMessages()) {
44 interface = new QDBusInterface("org.freedesktop.Notifications",
45 "/org/freedesktop/Notifications",
46 "org.freedesktop.Notifications");
47 if (interface->isValid()) {
54 ->hasUserNotificationCenterSupport()) {
69class FreedesktopImage {
71 FreedesktopImage() =
default;
72 explicit FreedesktopImage(
const QImage &img);
75 static QVariant toVariant(
const QImage &img);
78 int width, height, stride;
84 friend QDBusArgument &
operator<<(QDBusArgument &a,
85 const FreedesktopImage &i);
86 friend const QDBusArgument &operator>>(
const QDBusArgument &a,
90Q_DECLARE_METATYPE(FreedesktopImage);
93const int CHANNELS = 4;
94const int BYTES_PER_PIXEL = 4;
95const int BITS_PER_SAMPLE = 8;
97FreedesktopImage::FreedesktopImage(
const QImage &img)
98 : width(img.width()), height(img.height()),
99 stride(img.width() * BYTES_PER_PIXEL), hasAlpha(true), channels(CHANNELS),
100 bitsPerSample(BITS_PER_SAMPLE) {
102 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
103 const uint32_t *data =
reinterpret_cast<const uint32_t *
>(tmp.bits());
105 unsigned int num_pixels = width * height;
106 image.resize(num_pixels * BYTES_PER_PIXEL);
108 for (
unsigned int ptr = 0; ptr < num_pixels; ++ptr) {
109 image[ptr * BYTES_PER_PIXEL + 0] = data[ptr] >> 16;
110 image[ptr * BYTES_PER_PIXEL + 1] = data[ptr] >> 8;
111 image[ptr * BYTES_PER_PIXEL + 2] = data[ptr];
112 image[ptr * BYTES_PER_PIXEL + 3] = data[ptr] >> 24;
116QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i) {
118 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample
119 << i.channels << i.image;
124const QDBusArgument &operator>>(
const QDBusArgument &a, FreedesktopImage &i) {
126 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >>
127 i.channels >> i.image;
132QVariant FreedesktopImage::toVariant(
const QImage &img) {
133 FreedesktopImage fimg(img);
134 return QVariant(qDBusRegisterMetaType<FreedesktopImage>(), &fimg);
137void Notificator::notifyDBus(Class cls,
const QString &title,
138 const QString &text,
const QIcon &icon,
142 QList<QVariant> args;
152 args.append(QString());
162 args.append(actions);
170 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
173 sicon = QStyle::SP_MessageBoxInformation;
176 sicon = QStyle::SP_MessageBoxWarning;
179 sicon = QStyle::SP_MessageBoxCritical;
184 tmpicon = QApplication::style()->standardIcon(sicon);
188 hints[
"icon_data"] = FreedesktopImage::toVariant(
189 tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
193 args.append(millisTimeout);
196 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
201 const QString &text,
int millisTimeout) {
202 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
206 sicon = QSystemTrayIcon::Information;
209 sicon = QSystemTrayIcon::Warning;
212 sicon = QSystemTrayIcon::Critical;
215 trayIcon->showMessage(title, text, sicon, millisTimeout);
219void Notificator::notifyMacUserNotificationCenter(
const QString &title,
220 const QString &text) {
228 const QIcon &icon,
int millisTimeout) {
232 notifyDBus(cls, title, text, icon, millisTimeout);
240 notifyMacUserNotificationCenter(title, text);
247 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)