[telepathy-qt4/master] file-transfer receiver example: Accept multiple incoming file transfers.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Thu Sep 17 12:01:50 PDT 2009


---
 examples/file-transfer/Makefile.am          |    6 +-
 examples/file-transfer/receiver-channel.cpp |  100 +++++++++++++++++++++++++++
 examples/file-transfer/receiver-channel.h   |   60 ++++++++++++++++
 examples/file-transfer/receiver.cpp         |   72 +++----------------
 examples/file-transfer/receiver.h           |    8 --
 5 files changed, 176 insertions(+), 70 deletions(-)
 create mode 100644 examples/file-transfer/receiver-channel.cpp
 create mode 100644 examples/file-transfer/receiver-channel.h

diff --git a/examples/file-transfer/Makefile.am b/examples/file-transfer/Makefile.am
index 5db4efd..9127e88 100644
--- a/examples/file-transfer/Makefile.am
+++ b/examples/file-transfer/Makefile.am
@@ -29,10 +29,12 @@ receiver_LDADD = \
 	$(top_builddir)/TelepathyQt4/libtelepathy-qt4.la
 
 receiver_SOURCES = \
-	receiver.cpp
+	receiver.cpp \
+	receiver-channel.cpp
 
 nodist_receiver_SOURCES = \
-	_gen/receiver.moc.hpp
+	_gen/receiver.moc.hpp \
+	_gen/receiver-channel.moc.hpp
 
 BUILT_SOURCES = \
 	$(nodist_sender_SOURCES) \
diff --git a/examples/file-transfer/receiver-channel.cpp b/examples/file-transfer/receiver-channel.cpp
new file mode 100644
index 0000000..6564ff1
--- /dev/null
+++ b/examples/file-transfer/receiver-channel.cpp
@@ -0,0 +1,100 @@
+/*
+ * 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-channel.h"
+#include "_gen/receiver-channel.moc.hpp"
+
+#include <TelepathyQt4/Connection>
+#include <TelepathyQt4/Constants>
+#include <TelepathyQt4/FileTransferChannel>
+#include <TelepathyQt4/IncomingFileTransferChannel>
+#include <TelepathyQt4/PendingOperation>
+#include <TelepathyQt4/PendingReady>
+
+#include <QDebug>
+#include <QFile>
+#include <QFileInfo>
+
+ReceiverChannel::ReceiverChannel(const ConnectionPtr &connection, const QString &objectPath,
+        const QVariantMap &immutableProperties)
+    : mCompleted(false)
+{
+    mChan = IncomingFileTransferChannel::create(connection,
+            objectPath, immutableProperties);
+    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 *)));
+}
+
+ReceiverChannel::~ReceiverChannel()
+{
+    mFile.close();
+}
+
+void ReceiverChannel::onFileTransferChannelReady(PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "Unable to make file transfer channel ready -" <<
+            op->errorName() << ": " << op->errorMessage();
+        emit finished();
+        return;
+    }
+
+    qDebug() << "File transfer channel ready!";
+    connect(mChan.data(),
+            SIGNAL(stateChanged(Tp::FileTransferState, Tp::FileTransferStateChangeReason)),
+            SLOT(onFileTransferChannelStateChanged(Tp::FileTransferState, Tp::FileTransferStateChangeReason)));
+    connect(mChan.data(),
+            SIGNAL(transferredBytesChanged(qulonglong)),
+            SLOT(onFileTransferChannelTransferredBytesChanged(qulonglong)));
+    QString fileName(QLatin1String("TelepathyQt4FTReceiverExample_") + mChan->fileName());
+    fileName.replace("/", "_");
+    mFile.setFileName(fileName);
+    qDebug() << "Saving file as" << mFile.fileName();
+    mChan->acceptFile(0, &mFile);
+}
+
+void ReceiverChannel::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, file saved at" << mFile.fileName();
+        emit finished();
+    }
+}
+
+void ReceiverChannel::onFileTransferChannelTransferredBytesChanged(qulonglong count)
+{
+    qDebug().nospace() << "Receiving " << mChan->fileName() <<
+        " - transferred bytes=" << count << " (" <<
+        ((int) (((double) count / mChan->size()) * 100)) << "% done)";
+}
+
+void ReceiverChannel::onInvalidated()
+{
+    emit finished();
+}
diff --git a/examples/file-transfer/receiver-channel.h b/examples/file-transfer/receiver-channel.h
new file mode 100644
index 0000000..b5a5b3f
--- /dev/null
+++ b/examples/file-transfer/receiver-channel.h
@@ -0,0 +1,60 @@
+/*
+ * 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_channel_h_HEADER_GUARD_
+#define _TelepathyQt4_examples_file_transfer_receiver_channel_h_HEADER_GUARD_
+
+#include <TelepathyQt4/Constants>
+#include <TelepathyQt4/Types>
+
+using namespace Tp;
+
+namespace Tp
+{
+class PendingOperation;
+}
+
+class ReceiverChannel : public QObject
+{
+    Q_OBJECT
+
+public:
+   ReceiverChannel(const ConnectionPtr &connection,
+           const QString &objectPath, const QVariantMap &immutableProperties);
+   ~ReceiverChannel();
+
+Q_SIGNALS:
+    void finished();
+
+private Q_SLOTS:
+    void onFileTransferChannelReady(Tp::PendingOperation *op);
+    void onFileTransferChannelStateChanged(Tp::FileTransferState state,
+            Tp::FileTransferStateChangeReason stateReason);
+    void onFileTransferChannelTransferredBytesChanged(qulonglong count);
+    void onInvalidated();
+
+private:
+    IncomingFileTransferChannelPtr mChan;
+    QFile mFile;
+    bool mCompleted;
+};
+
+#endif
diff --git a/examples/file-transfer/receiver.cpp b/examples/file-transfer/receiver.cpp
index bce17bc..375eef9 100644
--- a/examples/file-transfer/receiver.cpp
+++ b/examples/file-transfer/receiver.cpp
@@ -22,28 +22,23 @@
 #include "receiver.h"
 #include "_gen/receiver.moc.hpp"
 
+#include "receiver-channel.h"
+
 #include <TelepathyQt4/Debug>
 #include <TelepathyQt4/Connection>
 #include <TelepathyQt4/ConnectionManager>
 #include <TelepathyQt4/Constants>
-#include <TelepathyQt4/ContactManager>
 #include <TelepathyQt4/FileTransferChannel>
 #include <TelepathyQt4/IncomingFileTransferChannel>
-#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)
+      mPassword(password)
 {
     mCM = ConnectionManager::create("gabble");
     connect(mCM->becomeReady(),
@@ -122,64 +117,21 @@ void Receiver::onNewChannels(const Tp::ChannelDetailsList &channels)
         qDebug() << " requested  :" << requested;
 
         if (channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_FILE_TRANSFER &&
-            !requested && !mTransferStarted) {
-            mTransferStarted = true;
-            mChan = IncomingFileTransferChannel::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 *)));
+            !requested) {
+            ReceiverChannel *channel = new ReceiverChannel(mConn,
+                    details.channel.path(),
+                    details.properties);
+            connect(channel,
+                    SIGNAL(finished()),
+                    channel,
+                    SLOT(deleteLater()));
         }
     }
 }
 
-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)));
-    connect(mChan.data(),
-            SIGNAL(transferredBytesChanged(qulonglong)),
-            SLOT(onFileTransferChannelTransferredBytesChanged(qulonglong)));
-    QString fileName(QLatin1String("TelepathyQt4FTReceiverExample_") + mChan->fileName());
-    fileName.replace("/", "_");
-    mFile.setFileName(fileName);
-    qDebug() << "Saving file as" << mFile.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::onFileTransferChannelTransferredBytesChanged(qulonglong count)
-{
-    qDebug().nospace() << "Tranferred bytes " << count << " - " <<
-        ((int) (((double) count / mChan->size()) * 100)) << "% done";
-}
-
 void Receiver::onInvalidated()
 {
-    QCoreApplication::exit(!mCompleted);
+    QCoreApplication::exit(1);
 }
 
 int main(int argc, char **argv)
diff --git a/examples/file-transfer/receiver.h b/examples/file-transfer/receiver.h
index 4dc0f69..83a38ad 100644
--- a/examples/file-transfer/receiver.h
+++ b/examples/file-transfer/receiver.h
@@ -46,10 +46,6 @@ private Q_SLOTS:
     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 onFileTransferChannelTransferredBytesChanged(qulonglong count);
     void onInvalidated();
 
 private:
@@ -59,10 +55,6 @@ private:
 
     ConnectionManagerPtr mCM;
     ConnectionPtr mConn;
-    IncomingFileTransferChannelPtr mChan;
-
-    bool mTransferStarted;
-    bool mCompleted;
 };
 
 #endif
-- 
1.5.6.5




More information about the telepathy-commits mailing list