From 7b8a28df7c0dfab0d5691e75b1932be9f4209c17 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Wed, 8 Jun 2016 22:18:14 +0200 Subject: [PATCH 1/1] dcc: Introduce TransferModel for DCC file transfers This model serves as input for a yet-to-come widget for displaying the current list of transfers. --- src/client/CMakeLists.txt | 1 + src/client/client.cpp | 6 +- src/client/client.h | 3 + src/client/transfermodel.cpp | 179 +++++++++++++++++++++++++++++++++ src/client/transfermodel.h | 89 ++++++++++++++++ src/common/transfermanager.cpp | 2 +- 6 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 src/client/transfermodel.cpp create mode 100644 src/client/transfermodel.h diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 4eef7d54..f0733f01 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -31,6 +31,7 @@ set(SOURCES messagemodel.cpp networkmodel.cpp selectionmodelsynchronizer.cpp + transfermodel.cpp treemodel.cpp # needed for automoc diff --git a/src/client/client.cpp b/src/client/client.cpp index 988aa027..9449fbb4 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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 diff --git a/src/client/client.h b/src/client/client.h index fc39e96d..98153527 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -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 index 00000000..46615126 --- /dev/null +++ b/src/client/transfermodel.cpp @@ -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 + +#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 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(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 index 00000000..1f9821ac --- /dev/null +++ b/src/client/transfermodel.h @@ -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 +#include +#include +#include + +#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 _transferIds; ///< List of transfer IDs +}; diff --git a/src/common/transfermanager.cpp b/src/common/transfermanager.cpp index e2506f49..099d3d38 100644 --- a/src/common/transfermanager.cpp +++ b/src/common/transfermanager.cpp @@ -53,7 +53,7 @@ void TransferManager::addTransfer(Transfer *transfer) } -void TransferManager::removeTransfer(const QUuid& uuid) +void TransferManager::removeTransfer(const QUuid &uuid) { if (!_transfers.contains(uuid)) { qWarning() << "Can not find transfer" << uuid << "to remove!"; -- 2.20.1