259ad0cd916e16366c69707d3a4f5dc96c724a4a
[vicar] / src / vicar-lib / src / telepathyutility.cpp
1 /*
2 @version: 0.5
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5 */
6
7 #include "telepathyutility.h"
8 #include "accountmanagerproxy.h"
9 #include "accountproxy.h"
10 #include "accountcompatproxy.h"
11 #include <QDBusInterface>
12 #include <QDBusConnection>
13 #include <QStringList>
14 #include <QDBusPendingReply>
15 #include <QDBusObjectPath>
16 #include <QDebug>
17
18 using namespace org::freedesktop::Telepathy;
19
20 TelepathyUtility::TelepathyUtility(QObject *parent) :
21     QObject(parent)
22 {
23 }
24
25 TelepathyUtility::~TelepathyUtility(){
26
27 }
28
29 //Get a list of all Telepathy accounts
30 QList<QDBusObjectPath> TelepathyUtility::getAllAccounts(){
31
32     QList<QDBusObjectPath> objPathList;
33
34     QDBusInterface *iface = new QDBusInterface(AM_SERVICE,AM_OBJ_PATH,DBUS_PROPS_IFACE,QDBusConnection::sessionBus(),this);
35     if (iface->isValid()){
36         QDBusReply<QDBusVariant> reply = iface->call(QDBus::AutoDetect,"Get",AM_INTERFACE,"ValidAccounts");
37
38         if (reply.isValid()){;
39             QDBusVariant validAccounts = reply.value();
40             const QVariant var = validAccounts.variant();
41             const QDBusArgument arg = var.value<QDBusArgument>();
42
43             arg.beginArray();
44             while (!arg.atEnd()){
45                 QDBusObjectPath opath;
46                 arg >> opath;
47                 if (opath.path().contains("tel")){
48                     qDebug() << opath.path();
49                 }
50                 objPathList.append(opath);
51             }
52             arg.endArray();
53         }
54         else{
55             qDebug() << "Error occurred while fetching accounts list "<<reply.error();
56         }
57     }
58     else{
59         qDebug() << "Error occurred while connecting to DBus interface";
60     }
61
62     return objPathList;
63
64 }
65
66 //Check if Vicar telepathy account exists
67 bool TelepathyUtility::accountExists(){
68     bool vicarAccountExists = false;
69     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
70     QDBusObjectPath account;
71     foreach (account,accountsList){
72         if (account.path().contains("vicar/tel/vicar")){
73             vicarAccountExists = true;
74             break;
75         }
76     }
77
78     return vicarAccountExists;
79 }
80
81 //Get telepathy account status
82 QString TelepathyUtility::getAccountStatus(){
83
84     QString status = "Not Available";
85
86     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
87     QDBusObjectPath account;
88     foreach (account,accountsList){
89         if (account.path().contains("vicar/tel/vicar")){
90             AccountProxy *accountProxy = new AccountProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
91             if (accountProxy->isValid()){
92                 uint intStatus = accountProxy->property("ConnectionStatus").toUInt();
93                 //Based on http://telepathy.freedesktop.org/spec/Connection.html#Connection_Status
94                 switch(intStatus){
95                 case 0:
96                     status = "Connected";
97                     break;
98                 case 1:
99                     status = "Connecting";
100                     break;
101                 case 2:
102                     status = "Disconnected";
103                     break;
104                 }
105             }
106         }
107     }
108
109     return status;
110 }
111
112 //Create Vicar telepathy account (used installation)
113 bool TelepathyUtility::createAccount(){
114
115     AccountManagerProxy *amProxy = new AccountManagerProxy(AM_SERVICE,AM_OBJ_PATH,QDBusConnection::sessionBus(),this);
116
117     QMap<QString,QVariant> connectionParametersMap;
118     connectionParametersMap.insert("account","vicar");
119
120     QList<QVariant> presenceDetails;
121     uint presenceType(2); //Available = 2
122     presenceDetails << presenceType;
123     presenceDetails << "online";
124     presenceDetails << "Available";
125
126     SimplePresence presence;
127     presence.type = presenceType;
128     presence.status = "online";
129     presence.statusMessage = "Available";
130
131     QMap<QString,QVariant> accountPropertiesMap;
132     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.AutomaticPresence",QVariant::fromValue(presence));
133     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.Enabled",true);
134     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.ConnectAutomatically",true);
135     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.RequestedPresence",QVariant::fromValue(presence));
136     accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.Profile","vicar");
137
138     QStringList valuesList;
139     valuesList.append("TEL");
140     accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.SecondaryVCardFields",valuesList);
141
142     QDBusPendingReply<QDBusObjectPath> reply = amProxy->CreateAccount("vicar","tel","Vicar",connectionParametersMap,accountPropertiesMap);
143     reply.waitForFinished();
144
145     if (reply.isValid()){
146         QDBusObjectPath account = reply.value();
147         qDebug() << account.path() <<" created successfully.";
148
149         AccountCompatProxy *accountCompatProxy = new AccountCompatProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
150         if (accountCompatProxy->isValid()){
151             QDBusPendingReply<> dbusReply = accountCompatProxy->SetHasBeenOnline();
152             dbusReply.waitForFinished();
153             if (dbusReply.isError()){
154                 qDebug() << "Error occurred while setting HasBeenOnline property "<<dbusReply.error();
155                 return false;
156             }
157         }
158     }
159     else{
160         qDebug() << "Error creating VICaR telepathy account "<<reply.error();
161         return false;
162     }
163
164     return true;
165 }
166
167 //Delete Vicar telepathy account (used during uninstallation)
168 bool TelepathyUtility::deleteAccount(){
169
170     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
171     QDBusObjectPath account;
172     foreach (account,accountsList){
173         if (account.path().contains("vicar/tel/vicar")){
174             AccountProxy *accountProxy = new AccountProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
175             if (accountProxy->isValid()){
176                 QDBusPendingReply<> dbusReply = accountProxy->Remove();
177                 dbusReply.waitForFinished();
178                 if (dbusReply.isError()){
179                     qDebug() << "Error occurred while removing VICaR account "<<dbusReply.error();
180                     return false;
181                 }
182                 else{
183                     qDebug() <<"VICaR account deleted";
184                 }
185             }
186         }
187     }
188
189     return true;
190 }
191
192 // Marshall the Presence data into a D-Bus argument
193  QDBusArgument &operator<<(QDBusArgument &argument, const SimplePresence &simplePresence)
194  {
195      argument.beginStructure();
196      argument <<  simplePresence.type << simplePresence.status << simplePresence.statusMessage;
197      argument.endStructure();
198      return argument;
199  }
200
201  // Retrieve the Presence data from the D-Bus argument
202  const QDBusArgument &operator>>(const QDBusArgument &argument, SimplePresence &simplePresence)
203  {
204      argument.beginStructure();
205      argument >> simplePresence.type >> simplePresence.status >> simplePresence.statusMessage;
206      argument.endStructure();
207      return argument;
208  }