First commit
[hidecallerid] / src / tpsession.cpp
1 #include "tpsession.h"
2
3 #include <TelepathyQt4/Account>
4 #include <TelepathyQt4/AccountInterface>
5 #include <TelepathyQt4/PendingOperation>
6 #include <TelepathyQt4/PendingVoid>
7 #include <TelepathyQt4/PendingReady>
8 #include <QtDBus/QDBusPendingCallWatcher>
9 #include <QtDBus/QDBusPendingReply>
10
11 tpSession::tpSession()
12 {
13     QDBusConnection bus = QDBusConnection::sessionBus();
14     mAM = AccountManager::create(bus,
15                                  AccountFactory::create(bus, Account::FeatureCore),
16                                  ConnectionFactory::create(bus, Connection::FeatureCore),
17                                  ChannelFactory::create(bus));
18
19     connect(mAM->becomeReady(),
20             SIGNAL(finished(Tp::PendingOperation *)),
21             SLOT(onAMReady(Tp::PendingOperation *)));
22 }
23
24 void tpSession::onAMReady(Tp::PendingOperation *op)
25 {
26     if (op->isError()) {
27         qWarning() << "error making AM ready";
28         return;
29     }
30
31     mAccount = mAM->accountForPath("/org/freedesktop/Telepathy/Account/ring/tel/ring");
32     if (!mAccount) {
33         qWarning() << "no account for ring found";
34         return;
35     }
36
37     //connect(mAccount->baseInterface(), SIGNAL(AccountPropertyChanged(const QVariantMap&)), this, SLOT(onPrivacyChanged(const QVariantMap&)));
38     if (!mAccount->haveConnection()) {
39         connect(mAccount.data(),
40                 SIGNAL(haveConnectionChanged(bool)),
41                 SLOT(onAccountHaveConnectionChanged(bool)));
42     } else {
43         mConnection = mAccount->connection();
44         retrievePrivacy();
45     }
46 }
47
48 void tpSession::onPropertyChanged(const QVariantMap& properties)
49 {
50      qDebug() << "TODO: change state when Privacy has been updated by someone else" << properties;
51 }
52
53 void tpSession::onAccountHaveConnectionChanged(bool haveConnection)
54 {
55     if (haveConnection) {
56         mConnection = mAccount->connection();
57         retrievePrivacy();
58     }
59 }
60
61 void tpSession::onSetPrivacyFinished(Tp::PendingOperation *op)
62 {
63     if (op->isError()) {
64         qWarning() << "unable to set com.nokia.Telepathy.Connection.Interface.GSM.Privacy";
65         return;
66     }
67
68     qDebug() << "com.nokia.Telepathy.Connection.Interface.GSM.Privacy property set";
69     emit privacyUpdated(showId);
70 }
71
72 void tpSession::onGotPrivacy(QDBusPendingCallWatcher *watcher)
73 {
74     QDBusPendingReply<QVariant> reply = *watcher;
75
76     if (reply.isError()) {
77         qWarning() << "unable to get com.nokia.Telepathy.Connection.Interface.GSM.Privacy";
78         return;
79     }
80
81     // Don't ask me why, but no-id == showId and id == hide it.
82     if(qdbus_cast<QStringList>(reply.value()) == QVariant("no-id"))
83         showId = true;
84     else
85         showId = false;
86     emit privacyUpdated(showId);
87
88     qDebug() << "com.nokia.Telepathy.Connection.Interface.GSM.Privacy property set";
89 }
90
91 void tpSession::retrievePrivacy()
92 {
93     if (!mConnection) {
94         qWarning() << "Account Manager connection unavailable";
95         return;
96     }
97
98     if (!mConnection->hasInterface("com.nokia.Telepathy.Connection.Interface.GSM")) {
99         qWarning() << "connection does not have interface GSM";
100         return;
101     }
102
103     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(
104                 mConnection->propertiesInterface()->Get(
105                     QLatin1String("com.nokia.Telepathy.Connection.Interface.GSM"),
106                     QLatin1String("Privacy")), this);
107
108     connect(watcher,
109             SIGNAL(finished(QDBusPendingCallWatcher *)),
110             SLOT(onGotPrivacy(QDBusPendingCallWatcher *)));
111
112     qDebug() << "com.nokia.Telepathy.Connection.Interface.GSM.Privacy retrieved";
113 }
114
115 void tpSession::setPrivacy(bool value)
116 {
117     showId = value;
118
119     if (!mConnection) {
120         qWarning() << "Account Manager connection unavailable";
121         return;
122     }
123
124     if (!mConnection->hasInterface("com.nokia.Telepathy.Connection.Interface.GSM")) {
125         qWarning() << "connection does not have interface GSM";
126         return;
127     }
128
129     PendingVoid *call;
130     // Don't ask me why, but no-id == showId and id == hide it.
131     if(value)
132         call = new PendingVoid(
133                     mConnection->propertiesInterface()->Set(
134                         QLatin1String("com.nokia.Telepathy.Connection.Interface.GSM"),
135                         QLatin1String("Privacy"), QDBusVariant("no-id")),
136                     this);
137     else
138         call = new PendingVoid(
139                     mConnection->propertiesInterface()->Set(
140                         QLatin1String("com.nokia.Telepathy.Connection.Interface.GSM"),
141                         QLatin1String("Privacy"), QDBusVariant("id")),
142                     this);
143
144     connect(call,
145             SIGNAL(finished(Tp::PendingOperation*)),
146             SLOT(onSetPrivacyFinished(Tp::PendingOperation*)));
147 }