(k)ubuntu 10.04+ notification support (based on @zwierzak his code)
[novacoin.git] / src / qt / notificator.cpp
1 #include "notificator.h"
2
3 #include <QMetaType>
4 #include <QVariant>
5 #include <QIcon>
6 #include <QApplication>
7 #include <QStyle>
8 #include <QByteArray>
9 #include <QSystemTrayIcon>
10 #include <QMessageBox>
11
12 #ifdef QT_DBUS
13 #include <QtDBus/QtDBus>
14 #include <stdint.h>
15 #endif
16
17 // https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
18 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
19
20 Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent):
21     QObject(parent),
22     parent(parent),
23     programName(programName),
24     mode(None),
25     trayIcon(trayicon)
26 #ifdef QT_DBUS
27     ,interface(0)
28 #endif
29 {
30     if(trayicon && trayicon->supportsMessages())
31     {
32         mode = QSystemTray;
33     }
34 #ifdef QT_DBUS
35     interface = new QDBusInterface("org.freedesktop.Notifications",
36           "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
37     if(interface->isValid())
38     {
39         mode = Freedesktop;
40     }
41 #endif
42 }
43
44 Notificator::~Notificator()
45 {
46 #ifdef QT_DBUS
47     delete interface;
48 #endif
49 }
50
51 #ifdef QT_DBUS
52
53 // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html
54 class FreedesktopImage
55 {
56 public:
57     FreedesktopImage() {}
58     FreedesktopImage(const QImage &img);
59
60     static int metaType();
61
62     // Image to variant that can be marshaled over DBus
63     static QVariant toVariant(const QImage &img);
64
65 private:
66     int width, height, stride;
67     bool hasAlpha;
68     int channels;
69     int bitsPerSample;
70     QByteArray image;
71
72     friend QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i);
73     friend const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i);
74 };
75
76 Q_DECLARE_METATYPE(FreedesktopImage);
77
78 // Image configuration settings
79 const int CHANNELS = 4;
80 const int BYTES_PER_PIXEL = 4;
81 const int BITS_PER_SAMPLE = 8;
82
83 FreedesktopImage::FreedesktopImage(const QImage &img):
84     width(img.width()),
85     height(img.height()),
86     stride(img.width() * BYTES_PER_PIXEL),
87     hasAlpha(true),
88     channels(CHANNELS),
89     bitsPerSample(BITS_PER_SAMPLE)
90 {
91     // Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
92     QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
93     const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.constBits());
94
95     unsigned int num_pixels = width * height;
96     image.resize(num_pixels * BYTES_PER_PIXEL);
97
98     for(unsigned int ptr = 0; ptr < num_pixels; ++ptr)
99     {
100         image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16; // R
101         image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;  // G
102         image[ptr*BYTES_PER_PIXEL+2] = data[ptr];       // B
103         image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24; // A
104     }
105 }
106
107 QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i)
108 {
109     a.beginStructure();
110     a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
111     a.endStructure();
112     return a;
113 }
114
115 const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i)
116 {
117     a.beginStructure();
118     a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
119     a.endStructure();
120     return a;
121 }
122
123 int FreedesktopImage::metaType()
124 {
125     return qDBusRegisterMetaType<FreedesktopImage>();
126 }
127
128 QVariant FreedesktopImage::toVariant(const QImage &img)
129 {
130     FreedesktopImage fimg(img);
131     return QVariant(FreedesktopImage::metaType(), &fimg);
132 }
133
134 void Notificator::notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
135 {
136     Q_UNUSED(cls);
137     // Arguments for DBus call:
138     QList<QVariant> args;
139
140     // Program Name:
141     args.append(programName);
142
143     // Unique ID of this notification type:
144     args.append(0U);
145
146     // Application Icon, empty string
147     args.append(QString());
148
149     // Summary
150     args.append(title);
151
152     // Body
153     args.append(text);
154
155     // Actions (none, actions are deprecated)
156     QStringList actions;
157     args.append(actions);
158
159     // Hints
160     QVariantMap hints;
161
162     // If no icon specified, set icon based on class
163     QIcon tmpicon;
164     if(icon.isNull())
165     {
166         QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
167         switch(cls)
168         {
169         case Information: sicon = QStyle::SP_MessageBoxInformation; break;
170         case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
171         case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
172         default: break;
173         }
174         tmpicon = QApplication::style()->standardIcon(sicon);
175     }
176     else
177     {
178         tmpicon = icon;
179     }
180     hints["icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
181     args.append(hints);
182
183     // Timeout (in msec)
184     args.append(millisTimeout);
185
186     // "Fire and forget"
187     interface->callWithArgumentList(QDBus::NoBlock, "Notify", args);
188 }
189 #endif
190
191 void Notificator::notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
192 {
193     Q_UNUSED(icon);
194     QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
195     switch(cls) // Set icon based on class
196     {
197     case Information: sicon = QSystemTrayIcon::Information; break;
198     case Warning: sicon = QSystemTrayIcon::Warning; break;
199     case Critical: sicon = QSystemTrayIcon::Critical; break;
200     }
201     trayIcon->showMessage(title, text, sicon, millisTimeout);
202 }
203
204 void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
205 {
206     switch(mode)
207     {
208 #ifdef QT_DBUS
209     case Freedesktop:
210         notifyDBus(cls, title, text, icon, millisTimeout);
211         break;
212 #endif
213     case QSystemTray:
214         notifySystray(cls, title, text, icon, millisTimeout);
215         break;
216     default:
217         if(cls == Critical)
218         {
219             // Fall back to old fashioned popup dialog if critical and no other notification available
220             QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
221         }
222         break;
223     }
224 }