8aca28ad8a4cff9554955dd7d6cfe05d2c0702ac
[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 "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 << "online";
125     presenceDetails << "Available";
126
127     SimplePresence presence;
128     presence.type = presenceType;
129     presence.status = "online";
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("com.nokia.Account.Interface.Compat.Profile","vicar");
138
139     QStringList valuesList;
140     valuesList.append("TEL");
141     accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.SecondaryVCardFields",valuesList);
142
143     QDBusPendingReply<QDBusObjectPath> reply = amProxy->CreateAccount("vicar","tel","Vicar",connectionParametersMap,accountPropertiesMap);
144     reply.waitForFinished();
145
146     if (reply.isValid()){
147         QDBusObjectPath account = reply.value();
148         qDebug() << account.path() <<" created successfully.";
149
150         AccountCompatProxy *accountCompatProxy = new AccountCompatProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
151         if (accountCompatProxy->isValid()){
152             QDBusPendingReply<> dbusReply = accountCompatProxy->SetHasBeenOnline();
153             dbusReply.waitForFinished();
154             if (dbusReply.isError()){
155                 qDebug() << "Error occurred while setting HasBeenOnline property "<<dbusReply.error();
156                 return false;
157             }
158         }
159     }
160     else{
161         qDebug() << "Error creating VICaR telepathy account "<<reply.error();
162         return false;
163     }
164
165     return true;
166 }
167
168 //Delete Vicar telepathy account (used during uninstallation)
169 bool TelepathyUtility::deleteAccount(){
170
171     QList<QDBusObjectPath> accountsList = this->getAllAccounts();
172     QDBusObjectPath account;
173     foreach (account,accountsList){
174         if (account.path().contains("vicar/tel/vicar")){
175             AccountProxy *accountProxy = new AccountProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
176             if (accountProxy->isValid()){
177                 QDBusPendingReply<> dbusReply = accountProxy->Remove();
178                 dbusReply.waitForFinished();
179                 if (dbusReply.isError()){
180                     qDebug() << "Error occurred while removing VICaR account "<<dbusReply.error();
181                     return false;
182                 }
183                 else{
184                     qDebug() <<"VICaR account deleted";
185                 }
186             }
187         }
188     }
189
190     return true;
191 }
192
193 bool TelepathyUtility::callNumberWithRing(QString number){
194
195     bool result = false;
196
197     ConnectionInterfaceRequestsProxy *requestsProxy = new ConnectionInterfaceRequestsProxy(RING_CONN_SERVICE,RING_CONN_PATH,QDBusConnection::sessionBus(),this);
198     if (requestsProxy->isValid()){
199         QVariantMap channelRequestDetails;
200         uint targetHandleType(1);
201         channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.TargetHandleType",targetHandleType);
202         channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.TargetID",number);
203         channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.ChannelType","org.freedesktop.Telepathy.Channel.Type.StreamedMedia");
204         QDBusPendingReply<QDBusObjectPath,QVariantMap> dbusReply = requestsProxy->CreateChannel(channelRequestDetails);
205         dbusReply.waitForFinished();
206         if (!dbusReply.isError()){
207             QDBusObjectPath objPath = dbusReply.argumentAt<0>();
208             QVariantMap channelProperties = dbusReply.argumentAt<1>();
209             result = true;
210         }
211         else{
212             qDebug() << "Error occurred calling "<<number<< ". Error is "<< dbusReply.error();
213             result = false;
214         }
215     }
216
217     return result;
218 }
219
220 bool TelepathyUtility::sendDTMFTone(QString tone){
221     bool result = false;
222
223     return result;
224 }
225
226 // Marshall the Presence data into a D-Bus argument
227  QDBusArgument &operator<<(QDBusArgument &argument, const SimplePresence &simplePresence)
228  {
229      argument.beginStructure();
230      argument <<  simplePresence.type << simplePresence.status << simplePresence.statusMessage;
231      argument.endStructure();
232      return argument;
233  }
234
235  // Retrieve the Presence data from the D-Bus argument
236  const QDBusArgument &operator>>(const QDBusArgument &argument, SimplePresence &simplePresence)
237  {
238      argument.beginStructure();
239      argument >> simplePresence.type >> simplePresence.status >> simplePresence.statusMessage;
240      argument.endStructure();
241      return argument;
242  }