[telepathy-qt4/master] file-transfer receiver example: Added example for a file transfer receiver.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Mon Sep 28 19:03:34 PDT 2009


---
 examples/file-transfer/receiver.cpp |  188 +++++++++++++++++++++++++++++++++++
 examples/file-transfer/receiver.h   |   67 +++++++++++++
 2 files changed, 255 insertions(+), 0 deletions(-)
 create mode 100644 examples/file-transfer/receiver.cpp
 create mode 100644 examples/file-transfer/receiver.h

diff --git a/examples/file-transfer/receiver.cpp b/examples/file-transfer/receiver.cpp
new file mode 100644
index 0000000..9e7d84e
--- /dev/null
+++ b/examples/file-transfer/receiver.cpp
@@ -0,0 +1,188 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2009 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "receiver.h"
+#include "_gen/receiver.moc.hpp"
+
+#include <TelepathyQt4/Debug>
+#include <TelepathyQt4/Connection>
+#include <TelepathyQt4/ConnectionManager>
+#include <TelepathyQt4/Constants>
+#include <TelepathyQt4/ContactManager>
+#include <TelepathyQt4/FileTransferChannel>
+#include <TelepathyQt4/PendingChannel>
+#include <TelepathyQt4/PendingConnection>
+#include <TelepathyQt4/PendingContacts>
+#include <TelepathyQt4/PendingOperation>
+#include <TelepathyQt4/PendingReady>
+
+#include <QDebug>
+#include <QFile>
+#include <QFileInfo>
+
+Receiver::Receiver(const QString &username, const QString &password)
+    : mUsername(username),
+      mPassword(password),
+      mTransferStarted(false),
+      mCompleted(false)
+{
+    mCM = ConnectionManager::create("gabble");
+    connect(mCM->becomeReady(),
+            SIGNAL(finished(Tp::PendingOperation *)),
+            SLOT(onCMReady(Tp::PendingOperation *)));
+}
+
+Receiver::~Receiver()
+{
+    mFile.close();
+}
+
+void Receiver::onCMReady(PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "CM cannot become ready -" <<
+            op->errorName() << ": " << op->errorMessage();
+        return;
+    }
+
+    qDebug() << "CM ready!";
+
+    qDebug() << "Creating connection...";
+    QVariantMap params;
+    params.insert("account", QVariant(mUsername));
+    params.insert("password", QVariant(mPassword));
+    PendingConnection *pconn = mCM->requestConnection("jabber", params);
+    connect(pconn,
+            SIGNAL(finished(Tp::PendingOperation *)),
+            SLOT(onConnectionCreated(Tp::PendingOperation *)));
+}
+
+void Receiver::onConnectionCreated(PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "Unable to create connection -" <<
+            op->errorName() << ": " << op->errorMessage();
+        return;
+    }
+
+    qDebug() << "Connection ready!";
+
+    qDebug() << "Connecting...";
+    PendingConnection *pconn =
+        qobject_cast<PendingConnection *>(op);
+    mConn = pconn->connection();
+    connect(mConn->requestConnect(),
+            SIGNAL(finished(Tp::PendingOperation *)),
+            SLOT(onConnectionConnected(Tp::PendingOperation *)));
+    connect(mConn.data(),
+            SIGNAL(invalidated(Tp::DBusProxy *, const QString &, const QString &)),
+            SLOT(onInvalidated()));
+}
+
+void Receiver::onConnectionConnected(PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "Connection cannot become connected -" <<
+            op->errorName() << ": " << op->errorMessage();
+        return;
+    }
+
+    qDebug() << "Connected!";
+
+    connect(mConn->requestsInterface(),
+            SIGNAL(NewChannels(const Tp::ChannelDetailsList&)),
+            SLOT(onNewChannels(const Tp::ChannelDetailsList&)));
+}
+
+void Receiver::onNewChannels(const Tp::ChannelDetailsList &channels)
+{
+    foreach (const Tp::ChannelDetails &details, channels) {
+        QString channelType = details.properties.value(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType")).toString();
+        bool requested = details.properties.value(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".Requested")).toBool();
+        qDebug() << " channelType:" << channelType;
+        qDebug() << " requested  :" << requested;
+
+        if (channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_FILE_TRANSFER &&
+            !requested && !mTransferStarted) {
+            mTransferStarted = true;
+            mChan = FileTransferChannel::create(mConn,
+                        details.channel.path(),
+                        details.properties);
+            connect(mChan.data(),
+                    SIGNAL(invalidated(Tp::DBusProxy *, const QString &, const QString &)),
+                    SLOT(onInvalidated()));
+            connect(mChan->becomeReady(FileTransferChannel::FeatureCore),
+                    SIGNAL(finished(Tp::PendingOperation *)),
+                    SLOT(onFileTransferChannelReady(Tp::PendingOperation *)));
+        }
+    }
+}
+
+void Receiver::onFileTransferChannelReady(PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "Unable to make file transfer channel ready -" <<
+            op->errorName() << ": " << op->errorMessage();
+        return;
+    }
+
+    qDebug() << "File transfer channel ready!";
+    connect(mChan.data(),
+            SIGNAL(stateChanged(Tp::FileTransferState, Tp::FileTransferStateChangeReason)),
+            SLOT(onFileTransferChannelStateChanged(Tp::FileTransferState, Tp::FileTransferStateChangeReason)));
+    qDebug() << "Saving file as" << mChan->fileName();
+    mFile.setFileName(mChan->fileName());
+    mChan->acceptFile(0, &mFile);
+}
+
+void Receiver::onFileTransferChannelStateChanged(Tp::FileTransferState state,
+    Tp::FileTransferStateChangeReason stateReason)
+{
+    qDebug() << "File transfer channel state changed to" << state <<
+        "with reason" << stateReason;
+    mCompleted = (state == FileTransferStateCompleted);
+    if (mCompleted) {
+        qDebug() << "Transfer completed!";
+        QCoreApplication::exit(0);
+    }
+}
+
+void Receiver::onInvalidated()
+{
+    QCoreApplication::exit(!mCompleted);
+}
+
+int main(int argc, char **argv)
+{
+    QCoreApplication app(argc, argv);
+
+    if (argc < 3) {
+        qDebug() << "usage: receiver username password";
+        return 1;
+    }
+
+    Tp::registerTypes();
+    Tp::enableDebug(true);
+
+    new Receiver(argv[1], argv[2]);
+
+    return app.exec();
+}
diff --git a/examples/file-transfer/receiver.h b/examples/file-transfer/receiver.h
new file mode 100644
index 0000000..63797b8
--- /dev/null
+++ b/examples/file-transfer/receiver.h
@@ -0,0 +1,67 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2009 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _TelepathyQt4_examples_file_transfer_receiver_h_HEADER_GUARD_
+#define _TelepathyQt4_examples_file_transfer_receiver_h_HEADER_GUARD_
+
+#include <TelepathyQt4/Constants>
+#include <TelepathyQt4/Contact>
+#include <TelepathyQt4/Types>
+
+using namespace Tp;
+
+namespace Tp
+{
+class PendingOperation;
+}
+
+class Receiver : public QObject
+{
+    Q_OBJECT
+
+public:
+   Receiver(const QString &username, const QString &password);
+   ~Receiver();
+
+private Q_SLOTS:
+    void onCMReady(Tp::PendingOperation *op);
+    void onConnectionCreated(Tp::PendingOperation *op);
+    void onConnectionConnected(Tp::PendingOperation *op);
+    void onNewChannels(const Tp::ChannelDetailsList &channels);
+    void onFileTransferChannelReady(Tp::PendingOperation *op);
+    void onFileTransferChannelStateChanged(Tp::FileTransferState state,
+            Tp::FileTransferStateChangeReason stateReason);
+    void onInvalidated();
+
+private:
+    QString mUsername;
+    QString mPassword;
+    QFile mFile;
+
+    ConnectionManagerPtr mCM;
+    ConnectionPtr mConn;
+    FileTransferChannelPtr mChan;
+
+    bool mTransferStarted;
+    bool mCompleted;
+};
+
+#endif
-- 
1.5.6.5




More information about the telepathy-commits mailing list