dcc: Introduce TransferModel for DCC file transfers
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 8 Jun 2016 20:18:14 +0000 (22:18 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 28 Sep 2016 21:06:00 +0000 (23:06 +0200)
This model serves as input for a yet-to-come widget for displaying
the current list of transfers.

src/client/CMakeLists.txt
src/client/client.cpp
src/client/client.h
src/client/transfermodel.cpp [new file with mode: 0644]
src/client/transfermodel.h [new file with mode: 0644]
src/common/transfermanager.cpp

index 4eef7d5..f0733f0 100644 (file)
@@ -31,6 +31,7 @@ set(SOURCES
     messagemodel.cpp
     networkmodel.cpp
     selectionmodelsynchronizer.cpp
+    transfermodel.cpp
     treemodel.cpp
 
     # needed for automoc
index 988aa02..9449fbb 100644 (file)
@@ -47,6 +47,7 @@
 #include "networkmodel.h"
 #include "quassel.h"
 #include "signalproxy.h"
+#include "transfermodel.h"
 #include "util.h"
 #include "clientauthhandler.h"
 
@@ -105,6 +106,7 @@ Client::Client(QObject *parent)
     _networkConfig(0),
     _ignoreListManager(0),
     _transferManager(0),
+    _transferModel(new TransferModel(this)),
     _messageModel(0),
     _messageProcessor(0),
     _coreAccountModel(new CoreAccountModel(this)),
@@ -414,6 +416,7 @@ void Client::setSyncedToCore()
 
     Q_ASSERT(!_transferManager);
     _transferManager = new ClientTransferManager(this);
+    _transferModel->setManager(_transferManager);
     p->synchronize(transferManager());
 
     // trigger backlog request once all active bufferviews are initialized
@@ -487,8 +490,9 @@ void Client::setDisconnectedFromCore()
     }
 
     if (_transferManager) {
+        _transferModel->setManager(nullptr);
         _transferManager->deleteLater();
-        _transferManager = 0;
+        _transferManager = nullptr;
     }
 
     // we probably don't want to save pending input for reconnect
index fc39e96..9815352 100644 (file)
@@ -57,6 +57,7 @@ class IrcUser;
 class IrcChannel;
 class NetworkConfig;
 class SignalProxy;
+class TransferModel;
 
 struct NetworkInfo;
 
@@ -120,6 +121,7 @@ public:
     static inline NetworkConfig *networkConfig() { return instance()->_networkConfig; }
     static inline ClientIgnoreListManager *ignoreListManager() { return instance()->_ignoreListManager; }
     static inline ClientTransferManager *transferManager() { return instance()->_transferManager; }
+    static inline TransferModel *transferModel() { return instance()->_transferModel; }
 
     static inline CoreAccountModel *coreAccountModel() { return instance()->_coreAccountModel; }
     static inline CoreConnection *coreConnection() { return instance()->_coreConnection; }
@@ -250,6 +252,7 @@ private:
     NetworkConfig *_networkConfig;
     ClientIgnoreListManager *_ignoreListManager;
     ClientTransferManager *_transferManager;
+    TransferModel *_transferModel;
 
     MessageModel *_messageModel;
     AbstractMessageProcessor *_messageProcessor;
diff --git a/src/client/transfermodel.cpp b/src/client/transfermodel.cpp
new file mode 100644 (file)
index 0000000..4661512
--- /dev/null
@@ -0,0 +1,179 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program 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 General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#include "transfermodel.h"
+
+#include <array>
+
+#include "transfermanager.h"
+
+namespace {
+    constexpr int colCount{8};
+}
+
+
+int TransferModel::rowCount(const QModelIndex& index) const
+{
+    return index.isValid() ? 0 : _transferIds.size();
+}
+
+
+int TransferModel::columnCount(const QModelIndex& index) const
+{
+    return index.isValid() ? 0 : colCount;
+}
+
+
+QVariant TransferModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+    static std::array<QString, colCount> headers = {{
+        tr("Type"), tr("File"), tr("Status"), tr("Progress"), tr("Transferred"), tr("Speed"), tr("Peer"), tr("Peer Address")
+    }};
+
+    if (section < 0 || section >= columnCount() || orientation != Qt::Horizontal)
+        return {};
+
+    switch (role) {
+    case Qt::DisplayRole:
+        return headers[section];
+
+    default:
+        return {};
+    }
+}
+
+
+QVariant TransferModel::data(const QModelIndex& index, int role) const
+{
+    if (!_manager)
+        return {};
+    if (index.column() < 0 || index.column() >= columnCount() || index.row() < 0 || index.row() >= rowCount())
+        return {};
+
+    auto t = _manager->transfer(_transferIds.at(index.row()));
+    if (!t) {
+        qWarning() << "Invalid transfer ID stored in TransferModel!";
+        return {};
+    }
+
+    switch (role) {
+    case Qt::DisplayRole:
+        switch (index.column()) {
+        case 0: // Type
+            return t->direction() == Transfer::Direction::Send ? tr("Send") : tr("Receive");
+        case 1: // File
+            return t->fileName();
+        case 2: // Status
+            return t->prettyStatus();
+        case 3: // Progress
+            return (t->transferred() / t->fileSize()) * 100;
+        case 4: // Transferred
+            return t->transferred(); // TODO: use pretty units and show total
+        case 5: // Speed
+            return "n/a"; // TODO: fixme
+        case 6: // Peer
+            return t->nick();
+        case 7: // Peer Address
+            return QString("%1.%2").arg(t->address().toString(), t->port());
+        }
+        break;
+
+    default:
+        return {};
+    }
+
+    return {};
+}
+
+
+void TransferModel::setManager(const TransferManager *manager)
+{
+    if (_manager) {
+        disconnect(_manager, 0, this, 0);
+        beginResetModel();
+        _transferIds.clear();
+        endResetModel();
+    }
+
+    _manager = manager;
+    connect(manager, SIGNAL(transferAdded(QUuid)), SLOT(onTransferAdded(QUuid)));
+    connect(manager, SIGNAL(transferRemoved(QUuid)), SLOT(onTransferRemoved(QUuid)));
+}
+
+
+void TransferModel::onTransferAdded(const QUuid &transferId)
+{
+    auto transfer = _manager->transfer(transferId);
+    if (!transfer) {
+        qWarning() << "Invalid transfer ID!";
+        return;
+    }
+
+    // TODO Qt5: use new connection syntax to make things much less complicated
+    connect(transfer, SIGNAL(statusChanged(Transfer::Status)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(directionChanged(Transfer::Direction)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(addressChanged(QHostAddress)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(portChanged(quint16)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(fileNameChanged(QString)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(fileSizeChanged(quint64)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(transferredChanged(quint64)), SLOT(onTransferDataChanged()));
+    connect(transfer, SIGNAL(nickChanged(QString)), SLOT(onTransferDataChanged()));
+
+    beginInsertRows({}, rowCount(), rowCount());
+    _transferIds.append(transferId);
+    endInsertRows();
+}
+
+
+void TransferModel::onTransferRemoved(const QUuid &transferId)
+{
+    // Check if the transfer object still exists, which means we still should disconnect
+    auto transfer = _manager->transfer(transferId);
+    if (transfer)
+        disconnect(transfer, 0, this, 0);
+
+    for (auto row = 0; row < _transferIds.size(); ++row) {
+        if (_transferIds[row] == transferId) {
+            beginRemoveRows(QModelIndex(), row, row);
+            _transferIds.remove(row);
+            endRemoveRows();
+            break;
+        }
+    }
+}
+
+
+void TransferModel::onTransferDataChanged()
+{
+    auto transfer = qobject_cast<Transfer *>(sender());
+    if (!transfer)
+        return;
+
+    const auto& transferId = transfer->uuid();
+    for (auto row = 0; row < _transferIds.size(); ++row) {
+        if (_transferIds[row] == transferId) {
+            // TODO Qt5: use proper column
+            auto topLeft = createIndex(row, 0);
+            auto bottomRight = createIndex(row, columnCount());
+            emit dataChanged(topLeft, bottomRight);
+            break;
+        }
+    }
+}
diff --git a/src/client/transfermodel.h b/src/client/transfermodel.h
new file mode 100644 (file)
index 0000000..1f9821a
--- /dev/null
@@ -0,0 +1,89 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program 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 General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#pragma once
+
+#include <QAbstractTableModel>
+#include <QString>
+#include <QVector>
+#include <QUuid>
+
+#include "transfer.h"
+
+class TransferManager;
+
+/**
+ * Model that holds the current list of transfers.
+ */
+class TransferModel : public QAbstractTableModel
+{
+    Q_OBJECT
+
+public:
+    using QAbstractTableModel::QAbstractTableModel;
+
+    // see base class docs
+    int rowCount(const QModelIndex &index = {}) const override;
+    int columnCount(const QModelIndex &index = {}) const override;
+    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
+
+    /**
+     * Access the transfer for the given ID
+     *
+     * @param[in] transferId Transfer ID
+     * @returns A pointer to the transfer with the given ID, or nullptr for an unknown ID
+     */
+    Transfer *transfer(const QUuid &transferId) const;
+
+    /**
+     * Sets the TransferManager associated with this model.
+     *
+     * The model receives data from the given TransferManager instance. If set to nullptr, the model
+     * is cleared.
+     *
+     * @param[in] manager Pointer to the TransferManager instance, or nullptr for clearing the model
+     */
+    void setManager(const TransferManager *manager);
+
+private slots:
+    /**
+     * Slot to be called when a transfer is added.
+     *
+     * @param[in] transferId The transfer's ID
+     */
+    void onTransferAdded(const QUuid &transferId);
+
+    /**
+     * Slot to be called when a transfer is removed.
+     *
+     * @param[in] transferId The transfer's ID
+     */
+    void onTransferRemoved(const QUuid &transferId);
+
+    /**
+     * Slot to be called when a transfer's data changes.
+     */
+    void onTransferDataChanged();
+
+private:
+    const TransferManager *_manager{nullptr}; ///< Pointer to the active TransferManager instance, if any
+    QVector<QUuid> _transferIds;              ///< List of transfer IDs
+};
index e2506f4..099d3d3 100644 (file)
@@ -53,7 +53,7 @@ void TransferManager::addTransfer(Transfer *transfer)
 }
 
 
-void TransferManager::removeTransfer(const QUuiduuid)
+void TransferManager::removeTransfer(const QUuid &uuid)
 {
     if (!_transfers.contains(uuid)) {
         qWarning() << "Can not find transfer" << uuid << "to remove!";