dcc: Always clean up if a transfer completed or failed
[quassel.git] / src / common / transfer.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 "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         if (status == Status::Completed || status == Status::Failed) {
85             cleanUp();
86         }
87     }
88 }
89
90
91 QString Transfer::prettyStatus() const
92 {
93     switch(status()) {
94         case Status::New:
95             return tr("New");
96         case Status::Pending:
97             return tr("Pending");
98         case Status::Connecting:
99             return tr("Connecting");
100         case Status::Transferring:
101             return tr("Transferring");
102         case Status::Paused:
103             return tr("Paused");
104         case Status::Completed:
105             return tr("Completed");
106         case Status::Failed:
107             return tr("Failed");
108         case Status::Rejected:
109             return tr("Rejected");
110     }
111
112     return QString();
113 }
114
115
116
117 Transfer::Direction Transfer::direction() const
118 {
119     return _direction;
120 }
121
122
123 void Transfer::setDirection(Transfer::Direction direction)
124 {
125     if (_direction != direction) {
126         _direction = direction;
127         SYNC(ARG(direction));
128         emit directionChanged(direction);
129     }
130 }
131
132
133 QHostAddress Transfer::address() const
134 {
135     return _address;
136 }
137
138
139 void Transfer::setAddress(const QHostAddress &address)
140 {
141     if (_address != address) {
142         _address = address;
143         SYNC(ARG(address));
144         emit addressChanged(address);
145     }
146 }
147
148
149 quint16 Transfer::port() const
150 {
151     return _port;
152 }
153
154
155 void Transfer::setPort(quint16 port)
156 {
157     if (_port != port) {
158         _port = port;
159         SYNC(ARG(port));
160         emit portChanged(port);
161     }
162 }
163
164
165 QString Transfer::fileName() const
166 {
167     return _fileName;
168 }
169
170
171 void Transfer::setFileName(const QString &fileName)
172 {
173     if (_fileName != fileName) {
174         _fileName = fileName;
175         SYNC(ARG(fileName));
176         emit fileNameChanged(fileName);
177     }
178 }
179
180
181 quint64 Transfer::fileSize() const
182 {
183     return _fileSize;
184 }
185
186
187 void Transfer::setFileSize(quint64 fileSize)
188 {
189     if (_fileSize != fileSize) {
190         _fileSize = fileSize;
191         SYNC(ARG(fileSize));
192         emit fileSizeChanged(fileSize);
193     }
194 }
195
196
197 QString Transfer::nick() const
198 {
199     return _nick;
200 }
201
202
203 void Transfer::setNick(const QString &nick)
204 {
205     if (_nick != nick) {
206         _nick = nick;
207         SYNC(ARG(nick));
208         emit nickChanged(nick);
209     }
210 }
211
212
213 void Transfer::setError(const QString &errorString)
214 {
215     qWarning() << Q_FUNC_INFO << errorString;
216     emit error(errorString);
217     setStatus(Status::Failed);
218 }
219
220
221 QDataStream &operator<<(QDataStream &out, Transfer::Status state) {
222     out << static_cast<qint8>(state);
223     return out;
224 }
225
226 QDataStream &operator>>(QDataStream &in, Transfer::Status &state) {
227     qint8 s;
228     in >> s;
229     state = static_cast<Transfer::Status>(s);
230     return in;
231 }
232
233 QDataStream &operator<<(QDataStream &out, Transfer::Direction direction) {
234     out << static_cast<qint8>(direction);
235     return out;
236 }
237
238 QDataStream &operator>>(QDataStream &in, Transfer::Direction &direction) {
239     qint8 d;
240     in >> d;
241     direction = static_cast<Transfer::Direction>(d);
242     return in;
243 }