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