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 <QDBusInterface>
12 #include <QDBusConnection>
13 #include <QStringList>
14 #include <QDBusPendingReply>
15 #include <QDBusObjectPath>
18 using namespace org::freedesktop::Telepathy;
20 TelepathyUtility::TelepathyUtility(QObject *parent) :
25 TelepathyUtility::~TelepathyUtility(){
29 //Get a list of all Telepathy accounts
30 QList<QDBusObjectPath> TelepathyUtility::getAllAccounts(){
32 QList<QDBusObjectPath> objPathList;
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");
38 if (reply.isValid()){;
39 QDBusVariant validAccounts = reply.value();
40 const QVariant var = validAccounts.variant();
41 const QDBusArgument arg = var.value<QDBusArgument>();
45 QDBusObjectPath opath;
47 if (opath.path().contains("tel")){
48 qDebug() << opath.path();
50 objPathList.append(opath);
55 qDebug() << "Error occurred while fetching accounts list "<<reply.error();
59 qDebug() << "Error occurred while connecting to DBus interface";
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;
78 return vicarAccountExists;
81 //Get telepathy account status
82 QString TelepathyUtility::getAccountStatus(){
84 QString status = "Not Available";
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
99 status = "Connecting";
102 status = "Disconnected";
112 //Create Vicar telepathy account (used installation)
113 bool TelepathyUtility::createAccount(){
115 AccountManagerProxy *amProxy = new AccountManagerProxy(AM_SERVICE,AM_OBJ_PATH,QDBusConnection::sessionBus(),this);
117 QMap<QString,QVariant> connectionParametersMap;
118 connectionParametersMap.insert("account","vicar");
120 QList<QVariant> presenceDetails;
121 uint presenceType(2); //Available = 2
122 presenceDetails << presenceType;
123 presenceDetails << "online";
124 presenceDetails << "Available";
126 SimplePresence presence;
127 presence.type = presenceType;
128 presence.status = "online";
129 presence.statusMessage = "Available";
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");
138 QStringList valuesList;
139 valuesList.append("TEL");
140 accountPropertiesMap.insert("com.nokia.Account.Interface.Compat.SecondaryVCardFields",valuesList);
142 QDBusPendingReply<QDBusObjectPath> reply = amProxy->CreateAccount("vicar","tel","Vicar",connectionParametersMap,accountPropertiesMap);
143 reply.waitForFinished();
145 if (reply.isValid()){
146 QDBusObjectPath account = reply.value();
147 qDebug() << account.path() <<" created successfully.";
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();
160 qDebug() << "Error creating VICaR telepathy account "<<reply.error();
167 //Delete Vicar telepathy account (used during uninstallation)
168 bool TelepathyUtility::deleteAccount(){
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();
183 qDebug() <<"VICaR account deleted";
192 // Marshall the Presence data into a D-Bus argument
193 QDBusArgument &operator<<(QDBusArgument &argument, const SimplePresence &simplePresence)
195 argument.beginStructure();
196 argument << simplePresence.type << simplePresence.status << simplePresence.statusMessage;
197 argument.endStructure();
201 // Retrieve the Presence data from the D-Bus argument
202 const QDBusArgument &operator>>(const QDBusArgument &argument, SimplePresence &simplePresence)
204 argument.beginStructure();
205 argument >> simplePresence.type >> simplePresence.status >> simplePresence.statusMessage;
206 argument.endStructure();