1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "transfermodel.h"
22
23 #include <array>
24
25 #include "transfermanager.h"
26
27 namespace {
28 constexpr int colCount{8};
29 }
30
rowCount(const QModelIndex & index) const31 int TransferModel::rowCount(const QModelIndex& index) const
32 {
33 return index.isValid() ? 0 : _transferIds.size();
34 }
35
columnCount(const QModelIndex & index) const36 int TransferModel::columnCount(const QModelIndex& index) const
37 {
38 return index.isValid() ? 0 : colCount;
39 }
40
headerData(int section,Qt::Orientation orientation,int role) const41 QVariant TransferModel::headerData(int section, Qt::Orientation orientation, int role) const
42 {
43 static std::array<QString, colCount> headers = {
44 {tr("Type"), tr("File"), tr("Status"), tr("Progress"), tr("Transferred"), tr("Speed"), tr("Peer"), tr("Peer Address")}};
45
46 if (section < 0 || section >= columnCount() || orientation != Qt::Horizontal)
47 return {};
48
49 switch (role) {
50 case Qt::DisplayRole:
51 return headers[section];
52
53 default:
54 return {};
55 }
56 }
57
data(const QModelIndex & index,int role) const58 QVariant TransferModel::data(const QModelIndex& index, int role) const
59 {
60 if (!_manager)
61 return {};
62 if (index.column() < 0 || index.column() >= columnCount() || index.row() < 0 || index.row() >= rowCount())
63 return {};
64
65 auto t = _manager->transfer(_transferIds.at(index.row()));
66 if (!t) {
67 qWarning() << "Invalid transfer ID stored in TransferModel!";
68 return {};
69 }
70
71 switch (role) {
72 case Qt::DisplayRole:
73 switch (index.column()) {
74 case 0: // Type
75 return t->direction() == Transfer::Direction::Send ? tr("Send") : tr("Receive");
76 case 1: // File
77 return t->fileName();
78 case 2: // Status
79 return t->prettyStatus();
80 case 3: // Progress
81 return (t->transferred() / t->fileSize()) * 100;
82 case 4: // Transferred
83 return t->transferred(); // TODO: use pretty units and show total
84 case 5: // Speed
85 return "n/a"; // TODO: fixme
86 case 6: // Peer
87 return t->nick();
88 case 7: // Peer Address
89 return QString("%1.%2").arg(t->address().toString(), t->port());
90 }
91 break;
92
93 default:
94 return {};
95 }
96
97 return {};
98 }
99
setManager(const TransferManager * manager)100 void TransferModel::setManager(const TransferManager* manager)
101 {
102 if (_manager) {
103 disconnect(_manager, nullptr, this, nullptr);
104 beginResetModel();
105 _transferIds.clear();
106 endResetModel();
107 }
108
109 _manager = manager;
110 if (_manager) {
111 connect(manager, &TransferManager::transferAdded, this, &TransferModel::onTransferAdded);
112 connect(manager, &TransferManager::transferRemoved, this, &TransferModel::onTransferRemoved);
113 for (auto&& transferId : _manager->transferIds()) {
114 onTransferAdded(transferId);
115 }
116 }
117 }
118
onTransferAdded(const QUuid & transferId)119 void TransferModel::onTransferAdded(const QUuid& transferId)
120 {
121 auto transfer = _manager->transfer(transferId);
122 if (!transfer) {
123 qWarning() << "Invalid transfer ID!";
124 return;
125 }
126
127 // TODO Qt5: use new connection syntax to make things much less complicated
128 connect(transfer, &Transfer::statusChanged, this, &TransferModel::onTransferDataChanged);
129 connect(transfer, &Transfer::directionChanged, this, &TransferModel::onTransferDataChanged);
130 connect(transfer, &Transfer::addressChanged, this, &TransferModel::onTransferDataChanged);
131 connect(transfer, &Transfer::portChanged, this, &TransferModel::onTransferDataChanged);
132 connect(transfer, &Transfer::fileNameChanged, this, &TransferModel::onTransferDataChanged);
133 connect(transfer, &Transfer::fileSizeChanged, this, &TransferModel::onTransferDataChanged);
134 connect(transfer, &Transfer::transferredChanged, this, &TransferModel::onTransferDataChanged);
135 connect(transfer, &Transfer::nickChanged, this, &TransferModel::onTransferDataChanged);
136
137 beginInsertRows({}, rowCount(), rowCount());
138 _transferIds.append(transferId);
139 endInsertRows();
140 }
141
onTransferRemoved(const QUuid & transferId)142 void TransferModel::onTransferRemoved(const QUuid& transferId)
143 {
144 // Check if the transfer object still exists, which means we still should disconnect
145 auto transfer = _manager->transfer(transferId);
146 if (transfer)
147 disconnect(transfer, nullptr, this, nullptr);
148
149 for (auto row = 0; row < _transferIds.size(); ++row) {
150 if (_transferIds[row] == transferId) {
151 beginRemoveRows(QModelIndex(), row, row);
152 _transferIds.remove(row);
153 endRemoveRows();
154 break;
155 }
156 }
157 }
158
onTransferDataChanged()159 void TransferModel::onTransferDataChanged()
160 {
161 auto transfer = qobject_cast<Transfer*>(sender());
162 if (!transfer)
163 return;
164
165 const auto& transferId = transfer->uuid();
166 for (auto row = 0; row < _transferIds.size(); ++row) {
167 if (_transferIds[row] == transferId) {
168 // TODO Qt5: use proper column
169 auto topLeft = createIndex(row, 0);
170 auto bottomRight = createIndex(row, columnCount());
171 emit dataChanged(topLeft, bottomRight);
172 break;
173 }
174 }
175 }
176