f565a0d2aa6da97379e86417c274d8a9d7c3a9a2
[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     _state(State::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     _state(State::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<State>("Transfer::State");
54         qRegisterMetaType<Direction>("Transfer::Direction");
55         qRegisterMetaTypeStreamOperators<State>("Transfer::State");
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::State Transfer::state() const
73 {
74     return _state;
75 }
76
77
78 void Transfer::setState(Transfer::State state)
79 {
80     if (_state != state) {
81         _state = state;
82         SYNC(ARG(state));
83         emit stateChanged(state);
84     }
85 }
86
87
88
89 Transfer::Direction Transfer::direction() const
90 {
91     return _direction;
92 }
93
94
95 void Transfer::setDirection(Transfer::Direction direction)
96 {
97     if (_direction != direction) {
98         _direction = direction;
99         SYNC(ARG(direction));
100         emit directionChanged(direction);
101     }
102 }
103
104
105 QHostAddress Transfer::address() const
106 {
107     return _address;
108 }
109
110
111 void Transfer::setAddress(const QHostAddress &address)
112 {
113     if (_address != address) {
114         _address = address;
115         SYNC(ARG(address));
116         emit addressChanged(address);
117     }
118 }
119
120
121 quint16 Transfer::port() const
122 {
123     return _port;
124 }
125
126
127 void Transfer::setPort(quint16 port)
128 {
129     if (_port != port) {
130         _port = port;
131         SYNC(ARG(port));
132         emit portChanged(port);
133     }
134 }
135
136
137 QString Transfer::fileName() const
138 {
139     return _fileName;
140 }
141
142
143 void Transfer::setFileName(const QString &fileName)
144 {
145     if (_fileName != fileName) {
146         _fileName = fileName;
147         SYNC(ARG(fileName));
148         emit fileNameChanged(fileName);
149     }
150 }
151
152
153 quint64 Transfer::fileSize() const
154 {
155     return _fileSize;
156 }
157
158
159 void Transfer::setFileSize(quint64 fileSize)
160 {
161     if (_fileSize != fileSize) {
162         _fileSize = fileSize;
163         SYNC(ARG(fileSize));
164         emit fileSizeChanged(fileSize);
165     }
166 }
167
168
169 QString Transfer::nick() const
170 {
171     return _nick;
172 }
173
174
175 void Transfer::setNick(const QString &nick)
176 {
177     if (_nick != nick) {
178         _nick = nick;
179         SYNC(ARG(nick));
180         emit nickChanged(nick);
181     }
182 }
183
184
185 void Transfer::setError(const QString &errorString)
186 {
187     qWarning() << Q_FUNC_INFO << errorString;
188     emit error(errorString);
189     setState(State::Failed);
190     cleanUp();
191 }
192
193
194 QDataStream &operator<<(QDataStream &out, Transfer::State state) {
195     out << static_cast<qint8>(state);
196     return out;
197 }
198
199 QDataStream &operator>>(QDataStream &in, Transfer::State &state) {
200     qint8 s;
201     in >> s;
202     state = static_cast<Transfer::State>(s);
203     return in;
204 }
205
206 QDataStream &operator<<(QDataStream &out, Transfer::Direction direction) {
207     out << static_cast<qint8>(direction);
208     return out;
209 }
210
211 QDataStream &operator>>(QDataStream &in, Transfer::Direction &direction) {
212     qint8 d;
213     in >> d;
214     direction = static_cast<Transfer::Direction>(d);
215     return in;
216 }