Raven Core  3.0.0
P2P Digital Currency
notificator.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "notificator.h"
7 
8 #include <QApplication>
9 #include <QByteArray>
10 #include <QIcon>
11 #include <QImageWriter>
12 #include <QMessageBox>
13 #include <QMetaType>
14 #include <QStyle>
15 #include <QSystemTrayIcon>
16 #include <QTemporaryFile>
17 #include <QVariant>
18 #ifdef USE_DBUS
19 #include <stdint.h>
20 #include <QtDBus>
21 #endif
22 // Include ApplicationServices.h after QtDbus to avoid redefinition of check().
23 // This affects at least OSX 10.6. See /usr/include/AssertMacros.h for details.
24 // Note: This could also be worked around using:
25 // #define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
26 #ifdef Q_OS_MAC
27 #include <ApplicationServices/ApplicationServices.h>
28 #include "macnotificationhandler.h"
29 #endif
30 
31 
32 #ifdef USE_DBUS
33 // https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
34 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
35 #endif
36 
37 Notificator::Notificator(const QString &_programName, QSystemTrayIcon *_trayIcon, QWidget *_parent) :
38  QObject(_parent),
39  parent(_parent),
40  programName(_programName),
41  mode(None),
42  trayIcon(_trayIcon)
43 #ifdef USE_DBUS
44  ,interface(0)
45 #endif
46 {
47  if(_trayIcon && _trayIcon->supportsMessages())
48  {
49  mode = QSystemTray;
50  }
51 #ifdef USE_DBUS
52  interface = new QDBusInterface("org.freedesktop.Notifications",
53  "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
54  if(interface->isValid())
55  {
56  mode = Freedesktop;
57  }
58 #endif
59 #ifdef Q_OS_MAC
60  // check if users OS has support for NSUserNotification
63  }
64 #endif
65 }
66 
68 {
69 #ifdef USE_DBUS
70  delete interface;
71 #endif
72 }
73 
74 #ifdef USE_DBUS
75 
76 // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html
77 class FreedesktopImage
78 {
79 public:
80  FreedesktopImage() {}
81  explicit FreedesktopImage(const QImage &img);
82 
83  static int metaType();
84 
85  // Image to variant that can be marshalled over DBus
86  static QVariant toVariant(const QImage &img);
87 
88 private:
89  int width, height, stride;
90  bool hasAlpha;
91  int channels;
92  int bitsPerSample;
93  QByteArray image;
94 
95  friend QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i);
96  friend const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i);
97 };
98 
99 Q_DECLARE_METATYPE(FreedesktopImage);
100 
101 // Image configuration settings
102 const int CHANNELS = 4;
103 const int BYTES_PER_PIXEL = 4;
104 const int BITS_PER_SAMPLE = 8;
105 
106 FreedesktopImage::FreedesktopImage(const QImage &img):
107  width(img.width()),
108  height(img.height()),
109  stride(img.width() * BYTES_PER_PIXEL),
110  hasAlpha(true),
111  channels(CHANNELS),
112  bitsPerSample(BITS_PER_SAMPLE)
113 {
114  // Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
115  QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
116  const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.bits());
117 
118  unsigned int num_pixels = width * height;
119  image.resize(num_pixels * BYTES_PER_PIXEL);
120 
121  for(unsigned int ptr = 0; ptr < num_pixels; ++ptr)
122  {
123  image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16; // R
124  image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8; // G
125  image[ptr*BYTES_PER_PIXEL+2] = data[ptr]; // B
126  image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24; // A
127  }
128 }
129 
130 QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i)
131 {
132  a.beginStructure();
133  a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
134  a.endStructure();
135  return a;
136 }
137 
138 const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i)
139 {
140  a.beginStructure();
141  a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
142  a.endStructure();
143  return a;
144 }
145 
146 int FreedesktopImage::metaType()
147 {
148  return qDBusRegisterMetaType<FreedesktopImage>();
149 }
150 
151 QVariant FreedesktopImage::toVariant(const QImage &img)
152 {
153  FreedesktopImage fimg(img);
154  return QVariant(FreedesktopImage::metaType(), &fimg);
155 }
156 
157 void Notificator::notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
158 {
159  Q_UNUSED(cls);
160  // Arguments for DBus call:
161  QList<QVariant> args;
162 
163  // Program Name:
164  args.append(programName);
165 
166  // Unique ID of this notification type:
167  args.append(0U);
168 
169  // Application Icon, empty string
170  args.append(QString());
171 
172  // Summary
173  args.append(title);
174 
175  // Body
176  args.append(text);
177 
178  // Actions (none, actions are deprecated)
179  QStringList actions;
180  args.append(actions);
181 
182  // Hints
183  QVariantMap hints;
184 
185  // If no icon specified, set icon based on class
186  QIcon tmpicon;
187  if(icon.isNull())
188  {
189  QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
190  switch(cls)
191  {
192  case Information: sicon = QStyle::SP_MessageBoxInformation; break;
193  case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
194  case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
195  default: break;
196  }
197  tmpicon = QApplication::style()->standardIcon(sicon);
198  }
199  else
200  {
201  tmpicon = icon;
202  }
203  hints["icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
204  args.append(hints);
205 
206  // Timeout (in msec)
207  args.append(millisTimeout);
208 
209  // "Fire and forget"
210  interface->callWithArgumentList(QDBus::NoBlock, "Notify", args);
211 }
212 #endif
213 
214 void Notificator::notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
215 {
216  Q_UNUSED(icon);
217  QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
218  switch(cls) // Set icon based on class
219  {
220  case Information: sicon = QSystemTrayIcon::Information; break;
221  case Warning: sicon = QSystemTrayIcon::Warning; break;
222  case Critical: sicon = QSystemTrayIcon::Critical; break;
223  }
224  trayIcon->showMessage(title, text, sicon, millisTimeout);
225 }
226 
227 // Based on Qt's tray icon implementation
228 #ifdef Q_OS_MAC
229 void Notificator::notifyMacUserNotificationCenter(Class cls, const QString &title, const QString &text, const QIcon &icon) {
230  // icon is not supported by the user notification center yet. OSX will use the app icon.
232 }
233 
234 #endif
235 
236 void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
237 {
238  switch(mode)
239  {
240 #ifdef USE_DBUS
241  case Freedesktop:
242  notifyDBus(cls, title, text, icon, millisTimeout);
243  break;
244 #endif
245  case QSystemTray:
246  notifySystray(cls, title, text, icon, millisTimeout);
247  break;
248 #ifdef Q_OS_MAC
250  notifyMacUserNotificationCenter(cls, title, text, icon);
251  break;
252 #endif
253  default:
254  if(cls == Critical)
255  {
256  // Fall back to old fashioned pop-up dialog if critical and no other notification available
257  QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
258  }
259  break;
260  }
261 }
QString programName
Definition: notificator.h:64
bool hasUserNotificationCenterSupport(void)
check if OS can handle UserNotifications
Use DBus org.freedesktop.Notifications.
Definition: notificator.h:60
QWidget * parent
Definition: notificator.h:57
Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent)
Create a new notificator.
Definition: notificator.cpp:37
Notify user of potential problem.
Definition: notificator.h:40
Use the 10.8+ User Notification Center (Mac only)
Definition: notificator.h:62
void notify(Class cls, const QString &title, const QString &text, const QIcon &icon=QIcon(), int millisTimeout=10000)
Show notification message.
#define USE_DBUS
Definition: raven-config.h:396
Informational message.
Definition: notificator.h:39
static MacNotificationHandler * instance()
An error occurred.
Definition: notificator.h:41
QSystemTrayIcon * trayIcon
Definition: notificator.h:66
Use QSystemTray::showMessage.
Definition: notificator.h:61
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