Handle invalid handshake data properly in the core
[quassel.git] / src / common / transfer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 "transfer.h"
22
23 INIT_SYNCABLE_OBJECT(Transfer)
24 Transfer::Transfer(const QUuid &uuid, QObject *parent)
25     : SyncableObject(parent),
26     _status(Status::New),
27     _direction(Direction::Receive),
28     _port(0),
29     _fileSize(0),
30     _uuid(uuid)
31 {
32     init();
33 }
34
35 Transfer::Transfer(Direction direction, const QString &nick, const QString &fileName, const QHostAddress &address, quint16 port, quint64 fileSize, QObject *parent)
36     : SyncableObject(parent),
37     _status(Status::New),
38     _direction(direction),
39     _fileName(fileName),
40     _address(address),
41     _port(port),
42     _fileSize(fileSize),
43     _nick(nick),
44     _uuid(QUuid::createUuid())
45 {
46     init();
47 }
48
49
50 void Transfer::init()
51 {
52     static auto regTypes = []() -> bool {
53         qRegisterMetaType<Status>("Transfer::Status");
54         qRegisterMetaType<Direction>("Transfer::Direction");
55         qRegisterMetaTypeStreamOperators<Status>("Transfer::Status");
56         qRegisterMetaTypeStreamOperators<Direction>("Transfer::Direction");
57         return true;
58     }();
59     Q_UNUSED(regTypes);
60
61     renameObject(QString("Transfer/%1").arg(_uuid.toString()));
62     setAllowClientUpdates(true);
63 }
64
65
66 QUuid Transfer::uuid() const
67 {
68     return _uuid;
69 }
70
71
72 Transfer::Status Transfer::status() const
73 {
74     return _status;
75 }
76
77
78 void Transfer::setStatus(Transfer::Status status)
79 {
80     if (_status != status) {
81         _status = status;
82         SYNC(ARG(status));
83         emit statusChanged(status);
84     }
85 }
86
87
88 QString Transfer::prettyStatus() const
89 {
90     switch(status()) {
91         case Status::New:
92             return tr("New");
93         case Status::Pending:
94             return tr("Pending");
95         case Status::Connecting:
96             return tr("Connecting");
97         case Status::Transferring:
98             return tr("Transferring");
99         case Status::Paused:
100             return tr("Paused");
101         case Status::Completed:
102             return tr("Completed");
103         case Status::Failed:
104             return tr("Failed");
105         case Status::Rejected:
106             return tr("Rejected");
107     }
108
109     return QString();
110 }
111
112
113
114 Transfer::Direction Transfer::direction() const
115 {
116     return _direction;
117 }
118
119
120 void Transfer::setDirection(Transfer::Direction direction)
121 {
122     if (_direction != direction) {
123         _direction = direction;
124         SYNC(ARG(direction));
125         emit directionChanged(direction);
126     }
127 }
128
129
130 QHostAddress Transfer::address() const
131 {
132     return _address;
133 }
134
135
136 void Transfer::setAddress(const QHostAddress &address)
137 {
138     if (_address != address) {
139         _address = address;
140         SYNC(ARG(address));
141         emit addressChanged(address);
142     }
143 }
144
145
146 quint16 Transfer::port() const
147 {
148     return _port;
149 }
150
151
152 void Transfer::setPort(quint16 port)
153 {
154     if (_port != port) {
155         _port = port;
156         SYNC(ARG(port));
157         emit portChanged(port);
158     }
159 }
160
161
162 QString Transfer::fileName() const
163 {
164     return _fileName;
165 }
166
167
168 void Transfer::setFileName(const QString &fileName)
169 {
170     if (_fileName != fileName) {
171         _fileName = fileName;
172         SYNC(ARG(fileName));
173         emit fileNameChanged(fileName);
174     }
175 }
176
177
178 quint64 Transfer::fileSize() const
179 {
180     return _fileSize;
181 }
182
183
184 void Transfer::setFileSize(quint64 fileSize)
185 {
186     if (_fileSize != fileSize) {
187         _fileSize = fileSize;
188         SYNC(ARG(fileSize));
189         emit fileSizeChanged(fileSize);
190     }
191 }
192
193
194 QString Transfer::nick() const
195 {
196     return _nick;
197 }
198
199
200 void Transfer::setNick(const QString &nick)
201 {
202     if (_nick != nick) {
203         _nick = nick;
204         SYNC(ARG(nick));
205         emit nickChanged(nick);
206     }
207 }
208
209
210 void Transfer::setError(const QString &errorString)
211 {
212     qWarning() << Q_FUNC_INFO << errorString;
213     emit error(errorString);
214     setStatus(Status::Failed);
215     cleanUp();
216 }
217
218
219 QDataStream &operator<<(QDataStream &out, Transfer::Status state) {
220     out << static_cast<qint8>(state);
221     return out;
222 }
223
224 QDataStream &operator>>(QDataStream &in, Transfer::Status &state) {
225     qint8 s;
226     in >> s;
227     state = static_cast<Transfer::Status>(s);
228     return in;
229 }
230
231 QDataStream &operator<<(QDataStream &out, Transfer::Direction direction) {
232     out << static_cast<qint8>(direction);
233     return out;
234 }
235
236 QDataStream &operator>>(QDataStream &in, Transfer::Direction &direction) {
237     qint8 d;
238     in >> d;
239     direction = static_cast<Transfer::Direction>(d);
240     return in;
241 }