[Telepathy-commits] [telepathy-qt4/master] Add PendingChannel class for friendly channel requests

Olli Salli olli.salli at collabora.co.uk
Mon Nov 3 00:27:40 PST 2008


---
 TelepathyQt4/cli-connection.cpp |  122 +++++++++++++++++++++++++++++++++++++++
 TelepathyQt4/cli-connection.h   |   42 +++++++++++++
 2 files changed, 164 insertions(+), 0 deletions(-)

diff --git a/TelepathyQt4/cli-connection.cpp b/TelepathyQt4/cli-connection.cpp
index 2e17268..c7c60ff 100644
--- a/TelepathyQt4/cli-connection.cpp
+++ b/TelepathyQt4/cli-connection.cpp
@@ -438,5 +438,127 @@ void Connection::gotSimpleStatuses(QDBusPendingCallWatcher* watcher)
     mPriv->continueIntrospection();
 }
 
+PendingChannel* Connection::requestChannel(const QString& channelType, uint handleType, uint handle)
+{
+    debug() << "Requesting a Channel with type" << channelType << "and handle" << handle << "of type" << handleType;
+
+    PendingChannel* channel =
+        new PendingChannel(this, channelType, handleType, handle);
+    QDBusPendingCallWatcher* watcher =
+        new QDBusPendingCallWatcher(RequestChannel(channelType, handleType, handle, true), channel);
+
+    channel->connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
+                              SLOT(onCallFinished(QDBusPendingCallWatcher*)));
+
+    return channel;
+}
+
+struct PendingChannel::Private
+{
+    Connection* connection;
+    QString channelType;
+    uint handleType;
+    uint handle;
+    QDBusObjectPath objectPath;
+    QDBusError error;
+};
+
+PendingChannel::PendingChannel(Connection* connection, const QString& channelType, uint handleType, uint handle)
+    : QObject(connection), mPriv(new Private)
+{
+    mPriv->connection = connection;
+    mPriv->channelType = channelType;
+    mPriv->handleType = handleType;
+    mPriv->handle = handle;
+}
+
+PendingChannel::~PendingChannel()
+{
+    delete mPriv;
+}
+
+Connection* PendingChannel::connection() const
+{
+    return mPriv->connection;
+}
+
+const QString& PendingChannel::channelType() const
+{
+    return mPriv->channelType;
+}
+
+uint PendingChannel::handleType() const
+{
+    return mPriv->handleType;
+}
+
+uint PendingChannel::handle() const
+{
+    return mPriv->handle;
+}
+
+bool PendingChannel::isFinished() const
+{
+    return !mPriv->objectPath.path().isEmpty() || !mPriv->error.message().isEmpty();
+}
+
+bool PendingChannel::isError() const
+{
+    return !mPriv->error.message().isEmpty();
+}
+
+const QDBusError& PendingChannel::error() const
+{
+    if (isValid())
+        warning() << "PendingChannel::error() called when valid";
+    else if (!isFinished())
+        warning() << "PendingChannel::error() called before finished";
+
+    return mPriv->error;
+}
+
+bool PendingChannel::isValid() const
+{
+    return isFinished() && !isError();
+}
+
+Channel* PendingChannel::channel(QObject* parent) const
+{
+    if (!isFinished()) {
+        warning() << "PendingChannel::channel called before finished, returning 0";
+        return 0;
+    } else if (!isValid()) {
+        warning() << "PendingChannel::channel called when not valid, returning 0";
+        return 0;
+    }
+
+    Channel* channel =
+        new Channel(mPriv->connection->connection(),
+                    mPriv->connection->service(),
+                    mPriv->objectPath.path(),
+                    parent);
+    return channel;
+}
+
+void PendingChannel::onCallFinished(QDBusPendingCallWatcher* watcher)
+{
+    QDBusPendingReply<QDBusObjectPath> reply = *watcher;
+
+    debug() << "Received reply to RequestChannel";
+
+    if (!reply.isError()) {
+        debug() << " Success: object path" << reply.value().path();
+        mPriv->objectPath = reply.value();
+    } else {
+        debug().nospace() << " Failure: error " << reply.error().name() << ": " << reply.error().message();
+        mPriv->error = reply.error();
+    }
+
+    debug() << " Emitting finished()";
+    emit finished(this);
+
+    watcher->deleteLater();
+}
+
 }
 }
diff --git a/TelepathyQt4/cli-connection.h b/TelepathyQt4/cli-connection.h
index 5180203..de58aac 100644
--- a/TelepathyQt4/cli-connection.h
+++ b/TelepathyQt4/cli-connection.h
@@ -47,6 +47,7 @@
 #include <QStringList>
 
 #include <TelepathyQt4/Constants>
+#include <TelepathyQt4/Client/Channel>
 #include <TelepathyQt4/Client/DBus>
 #include <TelepathyQt4/Client/OptionalInterfaceFactory>
 
@@ -55,6 +56,8 @@ namespace Telepathy
 namespace Client
 {
 
+class PendingChannel;
+
 /**
  * \class Connection
  * \ingroup clientconn
@@ -380,6 +383,8 @@ public:
         return optionalInterface<DBus::PropertiesInterface>(BypassInterfaceCheck);
     }
 
+    PendingChannel* requestChannel(const QString& type, uint handleType, uint handle);
+
 Q_SIGNALS:
     /**
      * Emitted whenever the readiness of the Connection changes.
@@ -402,6 +407,43 @@ private:
     Private *mPriv;
 };
 
+class PendingChannel : public QObject
+{
+    Q_OBJECT
+
+public:
+    ~PendingChannel();
+
+    Connection* connection() const;
+
+    const QString& type() const;
+    uint handleType() const;
+    uint handle() const;
+
+    bool isFinished() const;
+
+    bool isError() const;
+    const QDBusError& error() const;
+
+    bool isValid() const;
+    Channel* channel(QObject* parent = 0) const;
+
+Q_SIGNALS:
+    void finished(Telepathy::Client::PendingChannel* channel);
+
+private Q_SLOTS:
+    void onCallFinished(QDBusPendingCallWatcher* watcher);
+
+private:
+    friend class Connection;
+
+    PendingChannel(Connection* connection, const QString& type, uint handleType, uint handle);
+
+    struct Private;
+    friend struct Private;
+    Private *mPriv;
+};
+
 }
 }
 
-- 
1.5.6.5




More information about the Telepathy-commits mailing list