Updated version number in files
[vicar] / src / vicar-lib / cpp / dbusutility.cpp
1 /*
2 @version: 0.7
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5 */
6
7 #include "dbusutility.h"
8 #include <QDBusMessage>
9 #include <QDebug>
10
11
12 DbusUtility::DbusUtility(QObject *parent):
13     QObject(parent),
14     connection(QDBusConnection::systemBus()){
15 }
16
17 //Destructor for Dbus Utility object.
18 DbusUtility::~DbusUtility(){
19     //this->connection.disconnectFromBus(this->connection.baseService());
20     //qDebug() << "Disconnected from system bus";
21 }
22
23 QDBusConnection DbusUtility::getConnection(bool systemBus){
24     if (systemBus){
25         if (!this->connection.isConnected()){
26             qDebug() << "Not connected to Dbus";
27         }
28         return this->connection;
29     }
30     else{
31         return QDBusConnection::sessionBus();
32     }
33 }
34
35 void DbusUtility::setConnection(QDBusConnection connection){
36     this->connection = connection;
37 }
38
39 //Utility method to send a dbus signal.
40 bool DbusUtility::sendSignal(QString strPath,QString strInterface,QString strName,bool systemBus){
41     QDBusMessage dbusSignal =  QDBusMessage::createSignal(strPath,strInterface,strName);
42     QDBusConnection dbusConnection = getConnection(systemBus);
43     bool status = dbusConnection.send(dbusSignal);
44     return status;
45 }
46
47 //Utility method to call a dbus method with parameters
48 bool DbusUtility::sendMethodCall(QString strService,QString strPath,
49                                  QString strInterface,QString strMethodName,
50                                  QList<QVariant> & arguments,
51                                  bool systemBus){
52     QDBusMessage dbusMethodCall = QDBusMessage::createMethodCall(strService,strPath,strInterface,strMethodName);
53     dbusMethodCall.setArguments(arguments);
54     QDBusConnection dbusConnection = getConnection(systemBus);
55     bool status = dbusConnection.send(dbusMethodCall);
56     return status;
57 }
58
59 //Utility method to display a notification (Orange sliding banner in Maemo) with custom message
60 bool DbusUtility::displayNotification(QString strMessage){
61     QList <QVariant> arguments;
62     arguments.append(strMessage);
63
64     bool status = this->sendMethodCall(NOTIFICATION_SERVICE,
65                                           NOTIFICATION_PATH,
66                                           NOTIFICATION_INTERFACE,
67                                           QString("SystemNoteInfoprint"),
68                                           arguments);
69     return status;
70 }
71
72
73 //Utility method to retrieve the last dbus error
74 QString DbusUtility::getErrorMessage(){
75     QString strErrorMessage;
76     QDBusError dbusError = this->connection.lastError();
77     if (dbusError.isValid()){
78         strErrorMessage = qPrintable(dbusError.message());
79     }
80     return strErrorMessage;
81 }