Version 0.7-0
[vicar] / src / vicar-lib / cpp / telepathyutility.cpp
1 /*
2 @version: 0.6
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 "connifacereqproxy.h"
12 #include <QDBusInterface>
13 #include <QDBusConnection>
14 #include <QStringList>
15 #include <QDBusPendingReply>
16 #include <QDBusObjectPath>
17 #include <QDebug>
18
19 using namespace org::freedesktop::Telepathy;
20
21 TelepathyUtility::TelepathyUtility(QObject *parent) :
22     QObject(parent)
23 {
24 }
25
26 TelepathyUtility::~TelepathyUtility(){
27
28 }
29
30 //Get a list of all Telepathy accounts
31 QList<QDBusObjectPath> TelepathyUtility::getAllAccounts(){
32
33     QList<QDBusObjectPath> objPathList;
34
35     QDBusInterface *iface = new QDBusInterface(AM_SERVICE,AM_OBJ_PATH,DBUS_PROPS_IFACE,QDBusConnection::sessionBus(),this);
36     if (iface->isValid()){
37         QDBusReply<QDBusVariant> reply = iface->call(QDBus::AutoDetect,"Get",AM_INTERFACE,"ValidAccounts");
38
39         if (reply.isValid()){;
40             QDBusVariant validAccounts = reply.value();
41             const QVariant var = validAccounts.variant();
42             const QDBusArgument arg = var.value<QDBusArgument>();
43
44             arg.beginArray();
45             while (!arg.atEnd()){
46                 QDBusObjectPath opath;
47                 arg >> opath;
48                 if (opath.path().contains("tel")){
49                     qDebug() << opath.path();
50                 }
51                 objPathList.append(opath);
52             }
53             arg.endArray();
54         }
55         else{
56             qDebug() << "Error occurred while fetching accounts list "<<reply.error();
57         }
58     }
59     else{
60         qDebug() << "Error occurred while connecting to DBus interface";
61     }
62
63     return objPathList;
64
65 }
66
67 //Check if Vicar telepathy account exists
68 bool TelepathyUtility::accountExists(){
69     bool vicarAccountExists = false;
70     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
71     QDBusObjectPath account;
72     foreach (account,accountsList){
73         if (account.path().contains("vicar/tel/vicar")){
74             vicarAccountExists = true;
75             break;
76         }
77     }
78
79     return vicarAccountExists;
80 }
81
82 //Get telepathy account status
83 QString TelepathyUtility::getAccountStatus(){
84
85     QString status = "Not Available";
86
87     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
88     QDBusObjectPath account;
89     foreach (account,accountsList){
90         if (account.path().contains("vicar/tel/vicar")){
91             AccountProxy *accountProxy = new AccountProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
92             if (accountProxy->isValid()){
93                 uint intStatus = accountProxy->property("ConnectionStatus").toUInt();
94                 //Based on http://telepathy.freedesktop.org/spec/Connection.html#Connection_Status
95                 switch(intStatus){
96                 case 0:
97                     status = "Connected";
98                     break;
99                 case 1:
100                     status = "Connecting";
101                     break;
102                 case 2:
103                     status = "Disconnected";
104                     break;
105                 }
106             }
107         }
108     }
109
110     return status;
111 }
112
113 //Create Vicar telepathy account (used during installation)
114 bool TelepathyUtility::createAccount(){
115
116     AccountManagerProxy *amProxy = new AccountManagerProxy(AM_SERVICE,AM_OBJ_PATH,QDBusConnection::sessionBus(),this);
117
118     QMap<QString,QVariant> connectionParametersMap;
119     connectionParametersMap.insert("account","vicar");
120
121     QList<QVariant> presenceDetails;
122     uint presenceType(2); //Available = 2
123     presenceDetails << presenceType;
124     presenceDetails << "available";
125     presenceDetails << "Available";
126
127     SimplePresence presence;
128     presence.type = presenceType;
129     presence.status = "available";
130     presence.statusMessage = "Available";
131
132     QMap<QString,QVariant> accountPropertiesMap;
133     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.AutomaticPresence",QVariant::fromValue(presence));
134     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.Enabled",true);
135     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.ConnectAutomatically",true);
136     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.RequestedPresence",QVariant::fromValue(presence));
137     accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.Service","vicar"); //Required when service can't be determined by the protocol name
138     accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.Profile","vicar");
139
140     QStringList valuesList;
141     valuesList.append("TEL");
142     accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.SecondaryVCardFields",valuesList);
143
144     //QStringList uriSchemeList;
145     //uriSchemeList.append("tel");
146     //accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.Interface.Addressing.DRAFT.URISchemes",uriSchemeList);
147
148     QDBusPendingReply<QDBusObjectPath> reply = amProxy->CreateAccount("vicar","tel","Vicar",connectionParametersMap,accountPropertiesMap);
149     reply.waitForFinished();
150
151     if (reply.isValid()){
152         QDBusObjectPath account = reply.value();
153         qDebug() << account.path() <<" created successfully.";
154
155         AccountCompatProxy *accountCompatProxy = new AccountCompatProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
156         if (accountCompatProxy->isValid()){
157             QDBusPendingReply<> dbusReply = accountCompatProxy->SetHasBeenOnline();
158             dbusReply.waitForFinished();
159             if (dbusReply.isError()){
160                 qDebug() << "Error occurred while setting HasBeenOnline property "<<dbusReply.error();
161                 return false;
162             }
163         }
164     }
165     else{
166         qDebug() << "Error creating VICaR telepathy account "<<reply.error();
167         return false;
168     }
169
170     return true;
171 }
172
173 //Delete Vicar telepathy account (used during uninstallation)
174 bool TelepathyUtility::deleteAccount(){
175
176     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
177     QDBusObjectPath account;
178     foreach (account,accountsList){
179         if (account.path().contains("vicar/tel/vicar")){
180             AccountProxy *accountProxy = new AccountProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
181             if (accountProxy->isValid()){
182                 QDBusPendingReply<> dbusReply = accountProxy->Remove();
183                 dbusReply.waitForFinished();
184                 if (dbusReply.isError()){
185                     qDebug() << "Error occurred while removing VICaR account "<<dbusReply.error();
186                     return false;
187                 }
188                 else{
189                     qDebug() <<"VICaR account deleted";
190                 }
191             }
192         }
193     }
194
195     return true;
196 }
197
198 bool TelepathyUtility::callNumberWithRing(QString number){
199
200     bool result = false;
201
202     ConnectionInterfaceRequestsProxy *requestsProxy = new ConnectionInterfaceRequestsProxy(RING_CONN_SERVICE,RING_CONN_PATH,QDBusConnection::sessionBus(),this);
203     if (requestsProxy->isValid()){
204         QVariantMap channelRequestDetails;
205         uint targetHandleType(1);
206         channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.TargetHandleType",targetHandleType);
207         channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.TargetID",number);
208         channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.ChannelType","org.freedesktop.Telepathy.Channel.Type.StreamedMedia");
209         QDBusPendingReply<QDBusObjectPath,QVariantMap> dbusReply = requestsProxy->CreateChannel(channelRequestDetails);
210         dbusReply.waitForFinished();
211         if (!dbusReply.isError()){
212             QDBusObjectPath objPath = dbusReply.argumentAt<0>();
213             QVariantMap channelProperties = dbusReply.argumentAt<1>();
214             result = true;
215         }
216         else{
217             qDebug() << "Error occurred calling "<<number<< ". Error is "<< dbusReply.error();
218             result = false;
219         }
220     }
221
222     return result;
223 }
224
225 bool TelepathyUtility::sendDTMFTone(QString tone){
226     bool result = false;
227
228     return result;
229 }
230
231 // Marshall the Presence data into a D-Bus argument
232  QDBusArgument &operator<<(QDBusArgument &argument, const SimplePresence &simplePresence)
233  {
234      argument.beginStructure();
235      argument <<  simplePresence.type << simplePresence.status << simplePresence.statusMessage;
236      argument.endStructure();
237      return argument;
238  }
239
240  // Retrieve the Presence data from the D-Bus argument
241  const QDBusArgument &operator>>(const QDBusArgument &argument, SimplePresence &simplePresence)
242  {
243      argument.beginStructure();
244      argument >> simplePresence.type >> simplePresence.status >> simplePresence.statusMessage;
245      argument.endStructure();
246      return argument;
247  }