Implement core-side highlights
[quassel.git] / src / client / transfermodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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
31
32 int TransferModel::rowCount(const QModelIndex& index) const
33 {
34     return index.isValid() ? 0 : _transferIds.size();
35 }
36
37
38 int TransferModel::columnCount(const QModelIndex& index) const
39 {
40     return index.isValid() ? 0 : colCount;
41 }
42
43
44 QVariant TransferModel::headerData(int section, Qt::Orientation orientation, int role) const
45 {
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")
48     }};
49
50     if (section < 0 || section >= columnCount() || orientation != Qt::Horizontal)
51         return {};
52
53     switch (role) {
54     case Qt::DisplayRole:
55         return headers[section];
56
57     default:
58         return {};
59     }
60 }
61
62
63 QVariant TransferModel::data(const QModelIndex& index, int role) const
64 {
65     if (!_manager)
66         return {};
67     if (index.column() < 0 || index.column() >= columnCount() || index.row() < 0 || index.row() >= rowCount())
68         return {};
69
70     auto t = _manager->transfer(_transferIds.at(index.row()));
71     if (!t) {
72         qWarning() << "Invalid transfer ID stored in TransferModel!";
73         return {};
74     }
75
76     switch (role) {
77     case Qt::DisplayRole:
78         switch (index.column()) {
79         case 0: // Type
80             return t->direction() == Transfer::Direction::Send ? tr("Send") : tr("Receive");
81         case 1: // File
82             return t->fileName();
83         case 2: // Status
84             return t->prettyStatus();
85         case 3: // Progress
86             return (t->transferred() / t->fileSize()) * 100;
87         case 4: // Transferred
88             return t->transferred(); // TODO: use pretty units and show total
89         case 5: // Speed
90             return "n/a"; // TODO: fixme
91         case 6: // Peer
92             return t->nick();
93         case 7: // Peer Address
94             return QString("%1.%2").arg(t->address().toString(), t->port());
95         }
96         break;
97
98     default:
99         return {};
100     }
101
102     return {};
103 }
104
105
106 void TransferModel::setManager(const TransferManager *manager)
107 {
108     if (_manager) {
109         disconnect(_manager, 0, this, 0);
110         beginResetModel();
111         _transferIds.clear();
112         endResetModel();
113     }
114
115     _manager = manager;
116     if (_manager) {
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);
121         }
122     }
123 }
124
125
126 void TransferModel::onTransferAdded(const QUuid &transferId)
127 {
128     auto transfer = _manager->transfer(transferId);
129     if (!transfer) {
130         qWarning() << "Invalid transfer ID!";
131         return;
132     }
133
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()));
143
144     beginInsertRows({}, rowCount(), rowCount());
145     _transferIds.append(transferId);
146     endInsertRows();
147 }
148
149
150 void TransferModel::onTransferRemoved(const QUuid &transferId)
151 {
152     // Check if the transfer object still exists, which means we still should disconnect
153     auto transfer = _manager->transfer(transferId);
154     if (transfer)
155         disconnect(transfer, 0, this, 0);
156
157     for (auto row = 0; row < _transferIds.size(); ++row) {
158         if (_transferIds[row] == transferId) {
159             beginRemoveRows(QModelIndex(), row, row);
160             _transferIds.remove(row);
161             endRemoveRows();
162             break;
163         }
164     }
165 }
166
167
168 void TransferModel::onTransferDataChanged()
169 {
170     auto transfer = qobject_cast<Transfer *>(sender());
171     if (!transfer)
172         return;
173
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);
181             break;
182         }
183     }
184 }