[Telepathy-commits] [telepathy-qt4/master] Implement sending of simple (plain text) messages

Simon McVittie simon.mcvittie at collabora.co.uk
Fri Feb 20 11:43:48 PST 2009


---
 TelepathyQt4/Client/message.cpp      |   22 ++++++++++
 TelepathyQt4/Client/message.h        |    2 +
 TelepathyQt4/Client/text-channel.cpp |   78 ++++++++++++++++++++++++++++++++++
 TelepathyQt4/Client/text-channel.h   |   31 ++++++++++++-
 tests/dbus/text-chan.cpp             |   13 ++----
 5 files changed, 134 insertions(+), 12 deletions(-)

diff --git a/TelepathyQt4/Client/message.cpp b/TelepathyQt4/Client/message.cpp
index 6d0b12c..dfed7c9 100644
--- a/TelepathyQt4/Client/message.cpp
+++ b/TelepathyQt4/Client/message.cpp
@@ -167,6 +167,23 @@ Message::Message(uint timestamp, uint type, const QString &text)
 }
 
 /**
+ * Constructor, from the parameters of the old Send method.
+ *
+ * \param type The message type
+ * \param text The text of the message
+ */
+Message::Message(uint type, const QString &text)
+    : mPriv(new Private(MessagePartList() << MessagePart() << MessagePart()))
+{
+    mPriv->parts[0].insert(QString::fromAscii("message-type"),
+            QDBusVariant(type));
+
+    mPriv->parts[1].insert(QString::fromAscii("content-type"),
+            QDBusVariant(QString::fromAscii("text/plain")));
+    mPriv->parts[1].insert(QString::fromAscii("content"), QDBusVariant(text));
+}
+
+/**
  * Copy constructor.
  */
 Message::Message(const Message &other)
@@ -398,6 +415,11 @@ MessagePart Message::part(uint index) const
     return mPriv->parts.at(index);
 }
 
+MessagePartList Message::parts() const
+{
+    return mPriv->parts;
+}
+
 /**
  * \class ReceivedMessage
  * \ingroup clientchannel
diff --git a/TelepathyQt4/Client/message.h b/TelepathyQt4/Client/message.h
index a535840..e0ac335 100644
--- a/TelepathyQt4/Client/message.h
+++ b/TelepathyQt4/Client/message.h
@@ -72,6 +72,7 @@ public:
 
     int size() const;
     MessagePart part(uint index) const;
+    MessagePartList parts() const;
 
 private:
     friend class TextChannel;
@@ -79,6 +80,7 @@ private:
 
     Message();
     Message(const MessagePartList &parts);
+    Message(uint, const QString &);
     Message(uint, uint, const QString &);
 
     class Private;
diff --git a/TelepathyQt4/Client/text-channel.cpp b/TelepathyQt4/Client/text-channel.cpp
index 4ec020c..913054e 100644
--- a/TelepathyQt4/Client/text-channel.cpp
+++ b/TelepathyQt4/Client/text-channel.cpp
@@ -39,6 +39,63 @@ namespace Telepathy
 namespace Client
 {
 
+struct PendingSendMessage::Private
+{
+    inline Private(const Message &message);
+    QString token;
+    Message message;
+};
+
+inline PendingSendMessage::Private::Private(const Message &message)
+    : token(QString::fromAscii("")), message(message)
+{
+}
+
+PendingSendMessage::PendingSendMessage(const Message &message, QObject *parent)
+    : PendingOperation(parent), mPriv(new Private(message))
+{
+}
+
+PendingSendMessage::~PendingSendMessage()
+{
+    delete mPriv;
+}
+
+QString PendingSendMessage::sentMessageToken() const
+{
+    return mPriv->token;
+}
+
+Message PendingSendMessage::message() const
+{
+    return mPriv->message;
+}
+
+void PendingSendMessage::onTextSent(QDBusPendingCallWatcher *watcher)
+{
+    QDBusPendingReply<> reply = *watcher;
+
+    if (reply.isError()) {
+        setFinishedWithError(reply.error());
+    } else {
+        setFinished();
+    }
+    watcher->deleteLater();
+}
+
+void PendingSendMessage::onMessageSent(QDBusPendingCallWatcher *watcher)
+{
+    QDBusPendingReply<QString> reply = *watcher;
+
+    if (reply.isError()) {
+        setFinishedWithError(reply.error());
+    } else {
+        mPriv->token = reply.value();
+        setFinished();
+    }
+    watcher->deleteLater();
+}
+
 struct TextChannel::Private
 {
     inline Private();
@@ -402,6 +459,27 @@ void TextChannel::forget(const QList<ReceivedMessage> &messages)
     }
 }
 
+PendingSendMessage *TextChannel::send(const QString &text,
+        ChannelTextMessageType type)
+{
+    Message m(type, text);
+    PendingSendMessage *op = new PendingSendMessage(m, this);
+
+    if (hasMessagesInterface()) {
+        connect(new QDBusPendingCallWatcher(
+                    messagesInterface()->SendMessage(m.parts(), 0)),
+                SIGNAL(finished(QDBusPendingCallWatcher *)),
+                op,
+                SLOT(onMessageSent(QDBusPendingCallWatcher *)));
+    } else {
+        connect(new QDBusPendingCallWatcher(textInterface()->Send(type, text)),
+                SIGNAL(finished(QDBusPendingCallWatcher *)),
+                op,
+                SLOT(onTextSent(QDBusPendingCallWatcher *)));
+    }
+    return op;
+}
+
 /**
  * Return whether the desired features are ready for use.
  *
diff --git a/TelepathyQt4/Client/text-channel.h b/TelepathyQt4/Client/text-channel.h
index 91f9c57..e836b48 100644
--- a/TelepathyQt4/Client/text-channel.h
+++ b/TelepathyQt4/Client/text-channel.h
@@ -26,6 +26,7 @@
 #endif
 
 #include <TelepathyQt4/Client/Channel>
+#include <TelepathyQt4/Client/PendingOperation>
 
 namespace Telepathy
 {
@@ -35,6 +36,31 @@ namespace Client
 class PendingReadyChannel;
 class Message;
 class ReceivedMessage;
+class TextChannel;
+
+class PendingSendMessage : public PendingOperation
+{
+    Q_OBJECT
+    Q_DISABLE_COPY(PendingSendMessage)
+
+public:
+    PendingSendMessage(const Message &message, QObject *parent = 0);
+    ~PendingSendMessage();
+
+    QString sentMessageToken() const;
+    Message message() const;
+
+private Q_SLOTS:
+    void onTextSent(QDBusPendingCallWatcher *);
+    void onMessageSent(QDBusPendingCallWatcher *);
+
+private:
+    friend class TextChannel;
+
+    struct Private;
+    friend struct Private;
+    Private *mPriv;
+};
 
 class TextChannel : public Channel
 {
@@ -80,11 +106,10 @@ public Q_SLOTS:
 
     void forget(const QList<ReceivedMessage> &messages);
 
-#if 0
-    // Returns a sent-message token (as in messageSent), or "".
-    QString send(const QString &text,
+    PendingSendMessage *send(const QString &text,
             ChannelTextMessageType type = ChannelTextMessageTypeNormal);
 
+#if 0
     // For advanced users.
     //
     // Returns a sent-message token (as in messageSent), or "".
diff --git a/tests/dbus/text-chan.cpp b/tests/dbus/text-chan.cpp
index 5f89b67..a056c06 100644
--- a/tests/dbus/text-chan.cpp
+++ b/tests/dbus/text-chan.cpp
@@ -127,16 +127,11 @@ void TestTextChan::expectConnReady(uint newStatus, uint newStatusReason)
 
 void TestTextChan::sendText(const char *text)
 {
-    // FIXME: there's no high-level API for Send() yet, so...
-    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(
-            mChan->textInterface()->Send(
-                Telepathy::ChannelTextMessageTypeNormal,
-                QLatin1String(text)));
-    QVERIFY(connect(watcher,
-                SIGNAL(finished(QDBusPendingCallWatcher *)),
-                SLOT(expectSuccessfulCall(QDBusPendingCallWatcher *))));
+    QVERIFY(connect(mChan->send(QLatin1String(text),
+                    Telepathy::ChannelTextMessageTypeNormal),
+                SIGNAL(finished(Telepathy::Client::PendingOperation *)),
+                SLOT(expectSuccessfulCall(Telepathy::Client::PendingOperation *))));
     QCOMPARE(mLoop->exec(), 0);
-    delete watcher;
 }
 
 void TestTextChan::initTestCase()
-- 
1.5.6.5




More information about the telepathy-commits mailing list