qa: Avoid deprecation warnings for QList/QSet conversions
[quassel.git] / src / common / transfer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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(
37     Direction direction, QString nick, QString fileName, const QHostAddress& address, quint16 port, quint64 fileSize, QObject* parent)
38     : SyncableObject(parent)
39     , _status(Status::New)
40     , _direction(direction)
41     , _fileName(std::move(fileName))
42     , _address(address)
43     , _port(port)
44     , _fileSize(fileSize)
45     , _nick(std::move(nick))
46     , _uuid(QUuid::createUuid())
47 {
48     init();
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     setObjectName(QString("Transfer/%1").arg(_uuid.toString()));
63     setAllowClientUpdates(true);
64 }
65
66 QUuid Transfer::uuid() const
67 {
68     return _uuid;
69 }
70
71 Transfer::Status Transfer::status() const
72 {
73     return _status;
74 }
75
76 void Transfer::setStatus(Transfer::Status status)
77 {
78     if (_status != status) {
79         _status = status;
80         SYNC(ARG(status));
81         emit statusChanged(status);
82         if (status == Status::Completed || status == Status::Failed) {
83             cleanUp();
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 Transfer::Direction Transfer::direction() const
113 {
114     return _direction;
115 }
116
117 void Transfer::setDirection(Transfer::Direction direction)
118 {
119     if (_direction != direction) {
120         _direction = direction;
121         SYNC(ARG(direction));
122         emit directionChanged(direction);
123     }
124 }
125
126 QHostAddress Transfer::address() const
127 {
128     return _address;
129 }
130
131 void Transfer::setAddress(const QHostAddress& address)
132 {
133     if (_address != address) {
134         _address = address;
135         SYNC(ARG(address));
136         emit addressChanged(address);
137     }
138 }
139
140 quint16 Transfer::port() const
141 {
142     return _port;
143 }
144
145 void Transfer::setPort(quint16 port)
146 {
147     if (_port != port) {
148         _port = port;
149         SYNC(ARG(port));
150         emit portChanged(port);
151     }
152 }
153
154 QString Transfer::fileName() const
155 {
156     return _fileName;
157 }
158
159 void Transfer::setFileName(const QString& fileName)
160 {
161     if (_fileName != fileName) {
162         _fileName = fileName;
163         SYNC(ARG(fileName));
164         emit fileNameChanged(fileName);
165     }
166 }
167
168 quint64 Transfer::fileSize() const
169 {
170     return _fileSize;
171 }
172
173 void Transfer::setFileSize(quint64 fileSize)
174 {
175     if (_fileSize != fileSize) {
176         _fileSize = fileSize;
177         SYNC(ARG(fileSize));
178         emit fileSizeChanged(fileSize);
179     }
180 }
181
182 QString Transfer::nick() const
183 {
184     return _nick;
185 }
186
187 void Transfer::setNick(const QString& nick)
188 {
189     if (_nick != nick) {
190         _nick = nick;
191         SYNC(ARG(nick));
192         emit nickChanged(nick);
193     }
194 }
195
196 void Transfer::setError(const QString& errorString)
197 {
198     qWarning() << Q_FUNC_INFO << errorString;
199     emit error(errorString);
200     setStatus(Status::Failed);
201 }
202
203 QDataStream& operator<<(QDataStream& out, Transfer::Status state)
204 {
205     out << static_cast<qint8>(state);
206     return out;
207 }
208
209 QDataStream& operator>>(QDataStream& in, Transfer::Status& state)
210 {
211     qint8 s;
212     in >> s;
213     state = static_cast<Transfer::Status>(s);
214     return in;
215 }
216
217 QDataStream& operator<<(QDataStream& out, Transfer::Direction direction)
218 {
219     out << static_cast<qint8>(direction);
220     return out;
221 }
222
223 QDataStream& operator>>(QDataStream& in, Transfer::Direction& direction)
224 {
225     qint8 d;
226     in >> d;
227     direction = static_cast<Transfer::Direction>(d);
228     return in;
229 }