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