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