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