File: notify.cpp

package info (click to toggle)
cmst 2023.03.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,608 kB
  • sloc: cpp: 6,999; xml: 142; makefile: 12
file content (345 lines) | stat: -rw-r--r-- 11,881 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**************************** notify.cpp ******************************** 

Code for a notify client to interface with a desktop notification 
server.

Copyright (C) 2013-2023
by: Andrew J. Bibb
License: MIT 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"),to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included 
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
***********************************************************************/  

# include <QtCore/QDebug>
# include <QtDBus/QDBusConnection>
# include <QTextDocument>
# include <QIcon>
# include <QPixmap>
# include <QTemporaryFile>
# include <QFile>
# include <QtGlobal>

# include "./notify.h"
                     
#define DBUS_NOTIFY_SERVICE "org.freedesktop.Notifications"
#define DBUS_NOTIFY_PATH "/org/freedesktop/Notifications"
#define DBUS_NOTIFY_INTERFACE "org.freedesktop.Notifications"

//  constructor
NotifyClient::NotifyClient(QObject* parent)
    : QObject(parent)
{ 
  
  // Data members
  s_name.clear();
  s_vendor.clear();
  s_version.clear();
  s_spec_version.clear();
  sl_capabilities.clear();
  b_validconnection = false;
  current_id = 0;
  file_map.clear();
  this->init();

  // Create our client and try to connect to the notify server
  if (! QDBusConnection::sessionBus().isConnected() )
    qCritical("CMST - Cannot connect to the session bus.");
		
  // Signals and slots
  connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(cleanUp()));	
    
  return;   
}


/////////////////////////////////////// PUBLIC FUNCTIONS ////////////////////////////////
//
// Function to connect to a notification server.
void NotifyClient::connectToServer()
{
	// return now if we already have a valid connection
  if (b_validconnection) return;

  notifyclient = new QDBusInterface(DBUS_NOTIFY_SERVICE, DBUS_NOTIFY_PATH, DBUS_NOTIFY_INTERFACE, QDBusConnection::sessionBus(), this); 
  if (notifyclient->isValid() ) {
    b_validconnection = true;
    getServerInformation();
    getCapabilities();  
    QDBusConnection::sessionBus().connect(DBUS_NOTIFY_SERVICE, DBUS_NOTIFY_PATH, DBUS_NOTIFY_INTERFACE, "NotificationClosed", this, SLOT(notificationClosed(quint32, quint32)));
    QDBusConnection::sessionBus().connect(DBUS_NOTIFY_SERVICE, DBUS_NOTIFY_PATH, DBUS_NOTIFY_INTERFACE, "ActionInvoked", this, SLOT(actionInvoked(quint32, QString)));
  } // if connection is valid 
  else {
    notifyclient->deleteLater();
    b_validconnection = false;
  }	// else connection not valid
}
//
// Function to initialize data members that are used to hold information sent to the server
void NotifyClient::init()
{
  s_summary.clear();
  s_app_name.clear();
  s_body.clear();
  s_icon.clear();
  i_urgency = Nc::UrgencyNormal;
  i_expire_timeout = -1;
  b_overwrite = false;
  
  return;
}
  
//
// Function to send a notification to the server.  There is basically a one to one correspondence
// of arguments to the org.freedesktop.Notifications.Notify method.  The arguments are mandatory
// and must be arranged from outside this class. The getxxx functions may be used to obtain server
// information for this purpose.
///////// COMMENTED OUT SINCE WE DON'T USE IT IN CMST /////////////////////
//void NotifyClient::notify (QString app_name, quint32 replaces_id, QString app_icon, QString summary, QString body, QStringList actions, QVariantMap hints, qint32 expire_timeout)
//{
  //// make sure we have a connection we can send the notification to.
  //if (! b_validconnection) return;
  
  //QDBusReply<quint32> reply = notifyclient->call(QLatin1String("Notify"), app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout);
  
  //if (reply.isValid() )
    //current_id = reply.value();
  //else
  	//#if QT_VERSION >= 0x050400 
  	//qCritical("CMST - Error reply received to the Notify method: %s", qUtf8Printable(reply.error().message()) );
		//#else
    //qCritical("CMST - Error reply received to the Notify method: %s", qPrintable(reply.error().message()) );
    //#endif
  
  //return;
//}   

// Convienence function to send notifications.  This function does some processing
// of the arguments.  In these functions:
//    expire_timeout: The amount of time in milliseconds the message is shown.
//                    A value of -1 means timeout is based on server's settings.
//    overwrite     : Will overwrite the previous message sent from this function.
//                    It will not overwrite notifications sent by other programs. 
//
//
// Show notification with summary, app_name, and body text
void NotifyClient::sendNotification ()
{
  // make sure we have a connection we can send the notification to.
  if (! b_validconnection) return;  
  
  // variables
  QString app_name = s_app_name;  
  quint32 replaces_id = 0;
  QString app_icon = "";
  QString body = ""; 
  QString summary = s_summary;
  QStringList actions = QStringList();
  QVariantMap hints;
  int expire_timeout = i_expire_timeout;
  
  // set replaces_id
  if (b_overwrite) replaces_id = current_id;
  
  // assemble the hints
  hints.clear();
  hints.insert("urgency", QVariant::fromValue(static_cast<uchar>(i_urgency)) );
  //if (! app_icon.isEmpty() ) hints.insert("image-path", QVariant::fromValue(app_icon));
  
  // make sure we can display the text on this server
  if (sl_capabilities.contains("body", Qt::CaseInsensitive) ) {
    body = s_body;
    if (! sl_capabilities.contains ("body-markup", Qt::CaseInsensitive) ) {
      QTextDocument td;
      td.setHtml(body);
      body = td.toPlainText();
    } // if server cannot display markup
  } // if capabilities contains body
  
  // process the icon, if we are using a fallback icon create a temporary file to hold it
    QTemporaryFile*  tempfileicon = NULL; 
    if (! s_icon.isEmpty() ) {   
      if (QFile::exists(s_icon) ) {
	tempfileicon = new QTemporaryFile(this);
	tempfileicon->setAutoRemove(false);
	if (tempfileicon->open() ) {
	  QPixmap px = QPixmap(s_icon);
	  px.save(tempfileicon->fileName(),"PNG");
	  app_icon =  tempfileicon->fileName().prepend("file://");
	} // if tempfileicon could be opened
      } // if s_icon exists as a disk file

      // assume s_icon exists as a theme icon, don't check it here.  That
      // check needs to be done in the calling program.
      else app_icon = s_icon;
    } // if s_icon is not empty
  
  QDBusReply<quint32> reply = notifyclient->call(QLatin1String("Notify"), app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout);
  if (reply.isValid() ) {
    current_id = reply.value();
    if (file_map.contains(current_id) && tempfileicon != NULL) {
      if (b_overwrite) {
	file_map.value(current_id)->remove();
	delete file_map.value(current_id);
	file_map.remove(current_id);				
      } // if
      else {
	tempfileicon->remove();
	delete tempfileicon;
	tempfileicon = NULL;
      } // else
    } // if contains current_id and not NULL
    if (tempfileicon != NULL) file_map[current_id] = tempfileicon;
  } // if reply is valid
  
  else {
  #if QT_VERSION >= 0x050400 
    qCritical("CMST - Error reply received to the Notify method: %s", qUtf8Printable(reply.error().message()) );
  #else
    qCritical("CMST - Error reply received to the Notify method: %s", qPrintable(reply.error().message()) );
  #endif
 } 

  return;
} 
  
/////////////////////////////////////// PRIVATE FUNCTIONS////////////////////////////////
//
//  Function to get information about the server and write results to data members
void NotifyClient::getServerInformation()
{
  // return if we don't have valid connection
  if (! b_validconnection) return; 
  
  // get the server information
  QDBusMessage reply = notifyclient->call(QLatin1String("GetServerInformation"));
  
  if (reply.type() == QDBusMessage::ReplyMessage) {
    QList<QVariant> outargs = reply.arguments();
    s_name = outargs.at(0).toString();
    s_vendor = outargs.at(1).toString();
    s_version = outargs.at(2).toString();
    s_spec_version = outargs.at(3).toString();
  }
  
  else {
    if (reply.type() == QDBusMessage::InvalidMessage)
      qCritical("CMST - Invalid reply received to GetServerInformation method.");
    
    else if (reply.type() == QDBusMessage::ErrorMessage) 
    #if QT_VERSION >= 0x050400 
			qCritical("CMST - Error reply received to GetServerInforation method: %s", qUtf8Printable(reply.errorMessage()) );
    #else
      qCritical("CMST - Error reply received to GetServerInforation method: %s", qPrintable(reply.errorMessage()) );
    #endif
  } // else some error occured
  

  return;
}

//
// Function to get the capabilities of the server and write to a qstringlist data member
void NotifyClient::getCapabilities()
{
  // return if we don't have valid connection
  if (! b_validconnection) return;  
  
  // get the server capabilities
  QDBusReply<QStringList> reply = notifyclient->call(QLatin1String("GetCapabilities") );

  if (reply.isValid()) 
    sl_capabilities = reply.value();
  else
  #if QT_VERSION >= 0x050400 
		qCritical("CMST - Error reply received to GetCapabilities method: %s", qUtf8Printable(reply.error().message()) );
  #else
    qCritical("CMST - Error reply received to GetCapabilities method: %s", qPrintable(reply.error().message()) );
  #endif
  
  return;
}

//
//  Function to force a close of a notification
void NotifyClient::closeNotification(quint32 id)
{
  // return if we don't have valid connection
  if (! b_validconnection) return; 
  
  QDBusMessage reply = notifyclient->call(QLatin1String("CloseNotification"), id);
  
  if (reply.type() == QDBusMessage::InvalidMessage)
    qCritical("CMST - Invalid reply received to CloseNotification method.");
  
  else if (reply.type() == QDBusMessage::ErrorMessage) 
  #if QT_VERSION >= 0x050400 
		qCritical("CMST - Error reply received to CloseNotification method: %s", qUtf8Printable(reply.errorMessage()) );
  #else
    qCritical("CMST - Error reply received to CloseNotification method: %s", qPrintable(reply.errorMessage()) );
  #endif
  
  return;
}

/////////////////////////////// PRIVATE SLOTS /////////////////////////////////////
//
// Slot called when a notification was closed
void NotifyClient::notificationClosed(quint32 id, quint32 reason)
{
	(void) reason;
	
	if (file_map.contains(id) ) {
		file_map.value(id)->remove();
		delete file_map.value(id);
		file_map.remove(id);
	}	
  
  return;
}

//
// Slot called when some action from the notification is invoked
// RIght now we don't do anything with the information
void NotifyClient::actionInvoked(quint32 id, QString action_key)
{
 
  (void) id;
	(void) action_key;
  //qDebug() << "Action invoked signal received" << id << action_key;
  
  return;
}

//
// Slot to tidy up things, mainly the temp files created if we made icons
// Called when qApp issues an aboutToQuit signal
void NotifyClient::cleanUp()
{
	
	QMapIterator<quint32, QTemporaryFile*> itr(file_map);
	while (itr.hasNext()) {
    itr.next();
		file_map.value(itr.key())->remove();
		delete file_map.value(itr.key() );
		file_map.remove(itr.key() );    
}
	
	return;
}