Add support for compiling with QCA on Qt5
[quassel.git] / src / client / clienttransfer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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 <QFile>
22
23 #include "clienttransfer.h"
24
25 INIT_SYNCABLE_OBJECT(ClientTransfer)
26 ClientTransfer::ClientTransfer(const QUuid &uuid, QObject *parent)
27     : Transfer(uuid, parent),
28     _file(0)
29 {
30     connect(this, SIGNAL(stateChanged(State)), SLOT(onStateChanged(State)));
31 }
32
33
34 void ClientTransfer::cleanUp()
35 {
36     if (_file) {
37         _file->close();
38         _file->deleteLater();
39         _file = 0;
40     }
41 }
42
43
44 QString ClientTransfer::savePath() const
45 {
46     return _savePath;
47 }
48
49
50 void ClientTransfer::accept(const QString &savePath) const
51 {
52     _savePath = savePath;
53     PeerPtr ptr = 0;
54     REQUEST_OTHER(requestAccepted, ARG(ptr));
55     emit accepted();
56 }
57
58
59 void ClientTransfer::reject() const
60 {
61     PeerPtr ptr = 0;
62     REQUEST_OTHER(requestRejected, ARG(ptr));
63     emit rejected();
64 }
65
66
67 void ClientTransfer::dataReceived(PeerPtr, const QByteArray &data)
68 {
69     // TODO: proper error handling (relay to core)
70     if (!_file) {
71         _file = new QFile(_savePath, this);
72         if (!_file->open(QFile::WriteOnly|QFile::Truncate)) {
73             qWarning() << Q_FUNC_INFO << "Could not open file:" << _file->errorString();
74             return;
75         }
76     }
77
78     if (!_file->isOpen())
79         return;
80
81     if (_file->write(data) < 0) {
82         qWarning() << Q_FUNC_INFO << "Could not write to file:" << _file->errorString();
83         return;
84     }
85 }
86
87
88 void ClientTransfer::onStateChanged(Transfer::State state)
89 {
90     switch(state) {
91         case Completed:
92             if (_file)
93                 _file->close();
94             break;
95         case Failed:
96             if (_file)
97                 _file->remove();
98             break;
99         default:
100             ;
101     }
102 }