dcc: Protect DCC file transfer support by a feature flag
[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     }
120 }
121
122
123 void TransferModel::onTransferAdded(const QUuid &transferId)
124 {
125     auto transfer = _manager->transfer(transferId);
126     if (!transfer) {
127         qWarning() << "Invalid transfer ID!";
128         return;
129     }
130
131     // TODO Qt5: use new connection syntax to make things much less complicated
132     connect(transfer, SIGNAL(statusChanged(Transfer::Status)), SLOT(onTransferDataChanged()));
133     connect(transfer, SIGNAL(directionChanged(Transfer::Direction)), SLOT(onTransferDataChanged()));
134     connect(transfer, SIGNAL(addressChanged(QHostAddress)), SLOT(onTransferDataChanged()));
135     connect(transfer, SIGNAL(portChanged(quint16)), SLOT(onTransferDataChanged()));
136     connect(transfer, SIGNAL(fileNameChanged(QString)), SLOT(onTransferDataChanged()));
137     connect(transfer, SIGNAL(fileSizeChanged(quint64)), SLOT(onTransferDataChanged()));
138     connect(transfer, SIGNAL(transferredChanged(quint64)), SLOT(onTransferDataChanged()));
139     connect(transfer, SIGNAL(nickChanged(QString)), SLOT(onTransferDataChanged()));
140
141     beginInsertRows({}, rowCount(), rowCount());
142     _transferIds.append(transferId);
143     endInsertRows();
144 }
145
146
147 void TransferModel::onTransferRemoved(const QUuid &transferId)
148 {
149     // Check if the transfer object still exists, which means we still should disconnect
150     auto transfer = _manager->transfer(transferId);
151     if (transfer)
152         disconnect(transfer, 0, this, 0);
153
154     for (auto row = 0; row < _transferIds.size(); ++row) {
155         if (_transferIds[row] == transferId) {
156             beginRemoveRows(QModelIndex(), row, row);
157             _transferIds.remove(row);
158             endRemoveRows();
159             break;
160         }
161     }
162 }
163
164
165 void TransferModel::onTransferDataChanged()
166 {
167     auto transfer = qobject_cast<Transfer *>(sender());
168     if (!transfer)
169         return;
170
171     const auto& transferId = transfer->uuid();
172     for (auto row = 0; row < _transferIds.size(); ++row) {
173         if (_transferIds[row] == transferId) {
174             // TODO Qt5: use proper column
175             auto topLeft = createIndex(row, 0);
176             auto bottomRight = createIndex(row, columnCount());
177             emit dataChanged(topLeft, bottomRight);
178             break;
179         }
180     }
181 }