telepathy-qt: IODevice added to TelepathyQt-Service.

Alexandr Akulich kaffeine at kemper.freedesktop.org
Sat Apr 23 08:33:21 UTC 2016


Module: telepathy-qt
Branch: master
Commit: 30cd89c2e962a85f0f9cc6d19168ae675658124d
URL:    http://cgit.freedesktop.org/telepathy/telepathy-qt/commit/?id=30cd89c2e962a85f0f9cc6d19168ae675658124d

Author: Alexandr Akulich <akulichalexander at gmail.com>
Date:   Mon Feb 15 21:50:58 2016 +0500

IODevice added to TelepathyQt-Service.

This class is interesting for all CMs that use a library that
accepts a QIODevice for file transfers.

Commit-by: Niels Ole Salscheider <niels_ole at salscheider-online.de>
Commit-by: Alexandr Akulich <akulichalexander at gmail.com>

---

 TelepathyQt/CMakeLists.txt |   4 ++
 TelepathyQt/IODevice       |  13 ++++++
 TelepathyQt/io-device.cpp  | 102 +++++++++++++++++++++++++++++++++++++++++++++
 TelepathyQt/io-device.h    |  57 +++++++++++++++++++++++++
 4 files changed, 176 insertions(+)

diff --git a/TelepathyQt/CMakeLists.txt b/TelepathyQt/CMakeLists.txt
index c827c08..096cc64 100644
--- a/TelepathyQt/CMakeLists.txt
+++ b/TelepathyQt/CMakeLists.txt
@@ -865,6 +865,7 @@ if(ENABLE_SERVICE_SUPPORT)
         dbus-error.cpp
         dbus-object.cpp
         dbus-service.cpp
+        io-device.cpp
         abstract-adaptor.cpp)
 
     set(telepathy_qt_service_HEADERS
@@ -891,6 +892,8 @@ if(ENABLE_SERVICE_SUPPORT)
         dbus-object.h
         DBusService
         dbus-service.h
+        IODevice
+        io-device.h
         ServiceTypes
         service-types.h)
 
@@ -915,6 +918,7 @@ if(ENABLE_SERVICE_SUPPORT)
         base-protocol.h
         base-protocol-internal.h
         dbus-object.h
+        io-device.h
         dbus-service.h)
 
     add_custom_target(all-generated-service-sources)
diff --git a/TelepathyQt/IODevice b/TelepathyQt/IODevice
new file mode 100644
index 0000000..7a6b566
--- /dev/null
+++ b/TelepathyQt/IODevice
@@ -0,0 +1,13 @@
+#ifndef _TelepathyQt_IODevice_HEADER_GUARD_
+#define _TelepathyQt_IODevice_HEADER_GUARD_
+
+#ifndef IN_TP_QT_HEADER
+#define IN_TP_QT_HEADER
+#endif
+
+#include <TelepathyQt/io-device.h>
+
+#undef IN_TP_QT_HEADER
+
+#endif // _TelepathyQt_IODevice_HEADER_GUARD_
+// vim:set ft=cpp:
diff --git a/TelepathyQt/io-device.cpp b/TelepathyQt/io-device.cpp
new file mode 100644
index 0000000..120dcbf
--- /dev/null
+++ b/TelepathyQt/io-device.cpp
@@ -0,0 +1,102 @@
+/**
+ * This file is part of TelepathyQt
+ *
+ * @copyright Copyright (C) 2016 Niels Ole Salscheider <niels_ole at salscheider-online.de>
+ * @copyright Copyright (C) 2016 Alexandr Akulich <akulichalexander at gmail.com>
+ * @license LGPL 2.1
+ *
+ * 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 <TelepathyQt/IODevice>
+
+#include "TelepathyQt/_gen/io-device.moc.hpp"
+
+namespace Tp
+{
+
+struct TP_QT_NO_EXPORT IODevice::Private
+{
+    QByteArray data;
+};
+
+/**
+ * \class IODevice
+ * \ingroup utils
+ * \headerfile <TelepathyQt/IODevice>
+ *
+ * \brief The IODevice class represents a buffer with independent read-write.
+ *
+ * QBuffer has one position pointer, so when we write data, the position
+ * pointer points to the end of the buffer and no bytes can be read.
+ *
+ * This class is interesting for all CMs that use a library that accepts a
+ * QIODevice for file transfers.
+ *
+ * Note: This class belongs to the service library.
+ */
+
+IODevice::IODevice(QObject *parent) :
+    QIODevice(parent),
+    mPriv(new Private)
+{
+}
+
+qint64 IODevice::bytesAvailable() const
+{
+    return QIODevice::bytesAvailable() + mPriv->data.size();
+}
+
+/**
+ * Returns the number of bytes that are available for reading.
+ *
+ * \return the number of bytes that are available for reading.
+ */
+bool IODevice::isSequential() const
+{
+    return true;
+}
+
+qint64 IODevice::readData(char *data, qint64 maxSize)
+{
+    qint64 size = qMin<qint64>(mPriv->data.size(), maxSize);
+    memcpy(data, mPriv->data.constData(), size);
+    mPriv->data.remove(0, size);
+    return size;
+}
+
+/**
+ * Writes the data to the buffer.
+ *
+ * Writes up to \a maxSize bytes from \a data to the buffer.
+ * If maxSize is not a zero, emits readyRead() and bytesWritten() signals.
+ *
+ * \param data The data to write.
+ * \param maxSize The number for bytes to write.
+ * \return The number of bytes that were written.
+ */
+qint64 IODevice::writeData(const char *data, qint64 maxSize)
+{
+    if (maxSize <= 0) {
+        return 0;
+    }
+
+    mPriv->data.append(data, maxSize);
+    Q_EMIT bytesWritten(maxSize);
+    Q_EMIT readyRead();
+    return maxSize;
+}
+
+}
diff --git a/TelepathyQt/io-device.h b/TelepathyQt/io-device.h
new file mode 100644
index 0000000..5231b77
--- /dev/null
+++ b/TelepathyQt/io-device.h
@@ -0,0 +1,57 @@
+/**
+ * This file is part of TelepathyQt
+ *
+ * @copyright Copyright (C) 2016 Niels Ole Salscheider <niels_ole at salscheider-online.de>
+ * @copyright Copyright (C) 2016 Alexandr Akulich <akulichalexander at gmail.com>
+ * @license LGPL 2.1
+ *
+ * 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 _TelepathyQt_io_device_h_HEADER_GUARD_
+#define _TelepathyQt_io_device_h_HEADER_GUARD_
+
+#ifndef IN_TP_QT_HEADER
+#error IN_TP_QT_HEADER
+#endif
+
+#include <TelepathyQt/Global>
+
+#include <QIODevice>
+
+namespace Tp
+{
+
+class TP_QT_EXPORT IODevice : public QIODevice
+{
+    Q_OBJECT
+public:
+    explicit IODevice(QObject *parent = 0);
+    bool isSequential() const Q_DECL_OVERRIDE;
+    qint64 bytesAvailable() const Q_DECL_OVERRIDE;
+
+protected:
+    qint64 readData(char *data, qint64 maxSize) Q_DECL_OVERRIDE;
+    qint64 writeData(const char *data, qint64 maxSize) Q_DECL_OVERRIDE;
+
+private:
+    class Private;
+    friend class Private;
+    Private *mPriv;
+};
+
+} // namespace Tp
+
+#endif // _TelepathyQt_io_device_h_HEADER_GUARD_



More information about the telepathy-commits mailing list