First commit
[hidecallerid] / src / tpsession.cpp
diff --git a/src/tpsession.cpp b/src/tpsession.cpp
new file mode 100644 (file)
index 0000000..5ad4311
--- /dev/null
@@ -0,0 +1,147 @@
+#include "tpsession.h"
+
+#include <TelepathyQt4/Account>
+#include <TelepathyQt4/AccountInterface>
+#include <TelepathyQt4/PendingOperation>
+#include <TelepathyQt4/PendingVoid>
+#include <TelepathyQt4/PendingReady>
+#include <QtDBus/QDBusPendingCallWatcher>
+#include <QtDBus/QDBusPendingReply>
+
+tpSession::tpSession()
+{
+    QDBusConnection bus = QDBusConnection::sessionBus();
+    mAM = AccountManager::create(bus,
+                                 AccountFactory::create(bus, Account::FeatureCore),
+                                 ConnectionFactory::create(bus, Connection::FeatureCore),
+                                 ChannelFactory::create(bus));
+
+    connect(mAM->becomeReady(),
+            SIGNAL(finished(Tp::PendingOperation *)),
+            SLOT(onAMReady(Tp::PendingOperation *)));
+}
+
+void tpSession::onAMReady(Tp::PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "error making AM ready";
+        return;
+    }
+
+    mAccount = mAM->accountForPath("/org/freedesktop/Telepathy/Account/ring/tel/ring");
+    if (!mAccount) {
+        qWarning() << "no account for ring found";
+        return;
+    }
+
+    //connect(mAccount->baseInterface(), SIGNAL(AccountPropertyChanged(const QVariantMap&)), this, SLOT(onPrivacyChanged(const QVariantMap&)));
+    if (!mAccount->haveConnection()) {
+        connect(mAccount.data(),
+                SIGNAL(haveConnectionChanged(bool)),
+                SLOT(onAccountHaveConnectionChanged(bool)));
+    } else {
+        mConnection = mAccount->connection();
+        retrievePrivacy();
+    }
+}
+
+void tpSession::onPropertyChanged(const QVariantMap& properties)
+{
+     qDebug() << "TODO: change state when Privacy has been updated by someone else" << properties;
+}
+
+void tpSession::onAccountHaveConnectionChanged(bool haveConnection)
+{
+    if (haveConnection) {
+        mConnection = mAccount->connection();
+        retrievePrivacy();
+    }
+}
+
+void tpSession::onSetPrivacyFinished(Tp::PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "unable to set com.nokia.Telepathy.Connection.Interface.GSM.Privacy";
+        return;
+    }
+
+    qDebug() << "com.nokia.Telepathy.Connection.Interface.GSM.Privacy property set";
+    emit privacyUpdated(showId);
+}
+
+void tpSession::onGotPrivacy(QDBusPendingCallWatcher *watcher)
+{
+    QDBusPendingReply<QVariant> reply = *watcher;
+
+    if (reply.isError()) {
+        qWarning() << "unable to get com.nokia.Telepathy.Connection.Interface.GSM.Privacy";
+        return;
+    }
+
+    // Don't ask me why, but no-id == showId and id == hide it.
+    if(qdbus_cast<QStringList>(reply.value()) == QVariant("no-id"))
+        showId = true;
+    else
+        showId = false;
+    emit privacyUpdated(showId);
+
+    qDebug() << "com.nokia.Telepathy.Connection.Interface.GSM.Privacy property set";
+}
+
+void tpSession::retrievePrivacy()
+{
+    if (!mConnection) {
+        qWarning() << "Account Manager connection unavailable";
+        return;
+    }
+
+    if (!mConnection->hasInterface("com.nokia.Telepathy.Connection.Interface.GSM")) {
+        qWarning() << "connection does not have interface GSM";
+        return;
+    }
+
+    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(
+                mConnection->propertiesInterface()->Get(
+                    QLatin1String("com.nokia.Telepathy.Connection.Interface.GSM"),
+                    QLatin1String("Privacy")), this);
+
+    connect(watcher,
+            SIGNAL(finished(QDBusPendingCallWatcher *)),
+            SLOT(onGotPrivacy(QDBusPendingCallWatcher *)));
+
+    qDebug() << "com.nokia.Telepathy.Connection.Interface.GSM.Privacy retrieved";
+}
+
+void tpSession::setPrivacy(bool value)
+{
+    showId = value;
+
+    if (!mConnection) {
+        qWarning() << "Account Manager connection unavailable";
+        return;
+    }
+
+    if (!mConnection->hasInterface("com.nokia.Telepathy.Connection.Interface.GSM")) {
+        qWarning() << "connection does not have interface GSM";
+        return;
+    }
+
+    PendingVoid *call;
+    // Don't ask me why, but no-id == showId and id == hide it.
+    if(value)
+        call = new PendingVoid(
+                    mConnection->propertiesInterface()->Set(
+                        QLatin1String("com.nokia.Telepathy.Connection.Interface.GSM"),
+                        QLatin1String("Privacy"), QDBusVariant("no-id")),
+                    this);
+    else
+        call = new PendingVoid(
+                    mConnection->propertiesInterface()->Set(
+                        QLatin1String("com.nokia.Telepathy.Connection.Interface.GSM"),
+                        QLatin1String("Privacy"), QDBusVariant("id")),
+                    this);
+
+    connect(call,
+            SIGNAL(finished(Tp::PendingOperation*)),
+            SLOT(onSetPrivacyFinished(Tp::PendingOperation*)));
+}