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