3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
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>
20 using namespace org::freedesktop::Telepathy;
22 TelepathyUtility::TelepathyUtility(QObject *parent) :
27 TelepathyUtility::~TelepathyUtility(){
31 //Get a list of all Telepathy accounts
32 QList<QDBusObjectPath> TelepathyUtility::getAllAccounts(){
34 QList<QDBusObjectPath> objPathList;
36 QDBusConnection sessionBus = QDBusConnection::sessionBus();
38 if (sessionBus.isConnected()){
39 QDBusInterface *iface = new QDBusInterface(AM_SERVICE,AM_OBJ_PATH,DBUS_PROPS_IFACE,sessionBus,this);
41 if (iface->isValid()){
42 QDBusReply<QDBusVariant> reply = iface->call(QDBus::AutoDetect,"Get",AM_INTERFACE,"ValidAccounts");
44 if (reply.isValid()){;
45 QDBusVariant validAccounts = reply.value();
46 const QVariant var = validAccounts.variant();
47 const QDBusArgument arg = var.value<QDBusArgument>();
51 QDBusObjectPath opath;
54 if (opath.path().contains("tel")){
55 qDebug() << opath.path();
57 objPathList.append(opath);
62 qDebug() << "Error occurred while fetching accounts list "<<reply.error();
66 QDBusError dbusError = QDBusConnection::sessionBus().lastError();
67 if (dbusError.isValid()) {
68 qDebug () << "DBUS Error - " << dbusError.message();
71 qDebug () << "Debug - Invalid Interface";
76 qDebug() << "Error occurred while connecting to DBus interface";
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;
96 return vicarAccountExists;
99 //Get telepathy account status
100 QString TelepathyUtility::getAccountStatus(){
102 QString status = "Not Available";
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
114 status = "Connected";
117 status = "Connecting";
120 status = "Disconnected";
130 //Create Vicar telepathy account (used during installation)
131 bool TelepathyUtility::createAccount(){
133 AccountManagerProxy *amProxy = new AccountManagerProxy(AM_SERVICE,AM_OBJ_PATH,QDBusConnection::sessionBus(),this);
135 QMap<QString,QVariant> connectionParametersMap;
136 connectionParametersMap.insert("account","vicar");
138 QList<QVariant> presenceDetails;
139 uint presenceType(2); //Available = 2
140 presenceDetails << presenceType;
141 presenceDetails << "available";
142 presenceDetails << "Available";
144 SimplePresence presence;
145 presence.type = presenceType;
146 presence.status = "available";
147 presence.statusMessage = "Available";
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 //accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.Service","vicar"); //Required when service can't be determined by the protocol name
155 accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.Profile","vicar");
157 QStringList valuesList;
158 valuesList.append("TEL");
159 accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.SecondaryVCardFields",valuesList);
161 //QStringList uriSchemeList;
162 //uriSchemeList.append("tel");
163 //accountPropertiesMap.insert("org.freedesktop.Telepathy.Account.Interface.Addressing.DRAFT.URISchemes",uriSchemeList);
165 QDBusPendingReply<QDBusObjectPath> reply = amProxy->CreateAccount("vicar","tel","Vicar",connectionParametersMap,accountPropertiesMap);
166 reply.waitForFinished();
168 if (reply.isValid()){
169 QDBusObjectPath account = reply.value();
170 qDebug() << account.path() <<" created successfully.";
172 AccountCompatProxy *accountCompatProxy = new AccountCompatProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
173 if (accountCompatProxy->isValid()){
174 QDBusPendingReply<> dbusReply = accountCompatProxy->SetHasBeenOnline();
175 dbusReply.waitForFinished();
176 if (dbusReply.isError()){
177 qDebug() << "Error occurred while setting HasBeenOnline property "<<dbusReply.error();
183 qDebug() << "Error creating VICaR telepathy account "<<reply.error();
190 //Delete Vicar telepathy account (used during uninstallation)
191 bool TelepathyUtility::deleteAccount(){
193 QList<QDBusObjectPath> accountsList = this->getAllAccounts();
194 QDBusObjectPath account;
195 foreach (account,accountsList){
196 if (account.path().contains("vicar/tel/vicar")){
197 AccountProxy *accountProxy = new AccountProxy(AM_SERVICE,account.path(),QDBusConnection::sessionBus(),this);
198 if (accountProxy->isValid()){
199 QDBusPendingReply<> dbusReply = accountProxy->Remove();
200 dbusReply.waitForFinished();
201 if (dbusReply.isError()){
202 qDebug() << "Error occurred while removing VICaR account "<<dbusReply.error();
206 qDebug() <<"VICaR account deleted";
215 bool TelepathyUtility::callNumberWithRing(QString number){
219 ConnectionInterfaceRequestsProxy *requestsProxy = new ConnectionInterfaceRequestsProxy(RING_CONN_SERVICE,RING_CONN_PATH,QDBusConnection::sessionBus(),this);
220 if (requestsProxy->isValid()){
221 QVariantMap channelRequestDetails;
222 uint targetHandleType(1);
223 channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.TargetHandleType",targetHandleType);
224 channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.TargetID",number);
225 channelRequestDetails.insert("org.freedesktop.Telepathy.Channel.ChannelType","org.freedesktop.Telepathy.Channel.Type.StreamedMedia");
226 QDBusPendingReply<QDBusObjectPath,QVariantMap> dbusReply = requestsProxy->CreateChannel(channelRequestDetails);
227 dbusReply.waitForFinished();
228 if (!dbusReply.isError()){
229 QDBusObjectPath objPath = dbusReply.argumentAt<0>();
230 QVariantMap channelProperties = dbusReply.argumentAt<1>();
234 qDebug() << "Error occurred calling "<<number<< ". Error is "<< dbusReply.error();
242 bool TelepathyUtility::sendDTMFTone(QString tone){
248 // Marshall the Presence data into a D-Bus argument
249 QDBusArgument &operator<<(QDBusArgument &argument, const SimplePresence &simplePresence)
251 argument.beginStructure();
252 argument << simplePresence.type << simplePresence.status << simplePresence.statusMessage;
253 argument.endStructure();
257 // Retrieve the Presence data from the D-Bus argument
258 const QDBusArgument &operator>>(const QDBusArgument &argument, SimplePresence &simplePresence)
260 argument.beginStructure();
261 argument >> simplePresence.type >> simplePresence.status >> simplePresence.statusMessage;
262 argument.endStructure();