1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "transfermodel.h"
25 #include "transfermanager.h"
28 constexpr int colCount{8};
32 int TransferModel::rowCount(const QModelIndex& index) const
34 return index.isValid() ? 0 : _transferIds.size();
38 int TransferModel::columnCount(const QModelIndex& index) const
40 return index.isValid() ? 0 : colCount;
44 QVariant TransferModel::headerData(int section, Qt::Orientation orientation, int role) const
46 static std::array<QString, colCount> headers = {{
47 tr("Type"), tr("File"), tr("Status"), tr("Progress"), tr("Transferred"), tr("Speed"), tr("Peer"), tr("Peer Address")
50 if (section < 0 || section >= columnCount() || orientation != Qt::Horizontal)
55 return headers[section];
63 QVariant TransferModel::data(const QModelIndex& index, int role) const
67 if (index.column() < 0 || index.column() >= columnCount() || index.row() < 0 || index.row() >= rowCount())
70 auto t = _manager->transfer(_transferIds.at(index.row()));
72 qWarning() << "Invalid transfer ID stored in TransferModel!";
78 switch (index.column()) {
80 return t->direction() == Transfer::Direction::Send ? tr("Send") : tr("Receive");
84 return t->prettyStatus();
86 return (t->transferred() / t->fileSize()) * 100;
87 case 4: // Transferred
88 return t->transferred(); // TODO: use pretty units and show total
90 return "n/a"; // TODO: fixme
93 case 7: // Peer Address
94 return QString("%1.%2").arg(t->address().toString(), t->port());
106 void TransferModel::setManager(const TransferManager *manager)
109 disconnect(_manager, 0, this, 0);
111 _transferIds.clear();
117 connect(manager, SIGNAL(transferAdded(QUuid)), SLOT(onTransferAdded(QUuid)));
118 connect(manager, SIGNAL(transferRemoved(QUuid)), SLOT(onTransferRemoved(QUuid)));
119 for (auto &&transferId : _manager->transferIds()) {
120 onTransferAdded(transferId);
126 void TransferModel::onTransferAdded(const QUuid &transferId)
128 auto transfer = _manager->transfer(transferId);
130 qWarning() << "Invalid transfer ID!";
134 // TODO Qt5: use new connection syntax to make things much less complicated
135 connect(transfer, SIGNAL(statusChanged(Transfer::Status)), SLOT(onTransferDataChanged()));
136 connect(transfer, SIGNAL(directionChanged(Transfer::Direction)), SLOT(onTransferDataChanged()));
137 connect(transfer, SIGNAL(addressChanged(QHostAddress)), SLOT(onTransferDataChanged()));
138 connect(transfer, SIGNAL(portChanged(quint16)), SLOT(onTransferDataChanged()));
139 connect(transfer, SIGNAL(fileNameChanged(QString)), SLOT(onTransferDataChanged()));
140 connect(transfer, SIGNAL(fileSizeChanged(quint64)), SLOT(onTransferDataChanged()));
141 connect(transfer, SIGNAL(transferredChanged(quint64)), SLOT(onTransferDataChanged()));
142 connect(transfer, SIGNAL(nickChanged(QString)), SLOT(onTransferDataChanged()));
144 beginInsertRows({}, rowCount(), rowCount());
145 _transferIds.append(transferId);
150 void TransferModel::onTransferRemoved(const QUuid &transferId)
152 // Check if the transfer object still exists, which means we still should disconnect
153 auto transfer = _manager->transfer(transferId);
155 disconnect(transfer, 0, this, 0);
157 for (auto row = 0; row < _transferIds.size(); ++row) {
158 if (_transferIds[row] == transferId) {
159 beginRemoveRows(QModelIndex(), row, row);
160 _transferIds.remove(row);
168 void TransferModel::onTransferDataChanged()
170 auto transfer = qobject_cast<Transfer *>(sender());
174 const auto& transferId = transfer->uuid();
175 for (auto row = 0; row < _transferIds.size(); ++row) {
176 if (_transferIds[row] == transferId) {
177 // TODO Qt5: use proper column
178 auto topLeft = createIndex(row, 0);
179 auto bottomRight = createIndex(row, columnCount());
180 emit dataChanged(topLeft, bottomRight);