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