Transfers also should notify on changes to transferred()
[quassel.git] / src / client / clienttransfer.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 <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(statusChanged(Transfer::Status)), SLOT(onStatusChanged(Transfer::Status)));
31 }
32
33
34 quint64 ClientTransfer::transferred() const
35 {
36     if (status() == Status::Completed)
37         return fileSize();
38
39     return _file ? _file->size() : 0;
40 }
41
42
43 void ClientTransfer::cleanUp()
44 {
45     if (_file) {
46         _file->close();
47         _file->deleteLater();
48         _file = 0;
49     }
50 }
51
52
53 QString ClientTransfer::savePath() const
54 {
55     return _savePath;
56 }
57
58
59 void ClientTransfer::accept(const QString &savePath) const
60 {
61     _savePath = savePath;
62     PeerPtr ptr = 0;
63     REQUEST_OTHER(requestAccepted, ARG(ptr));
64     emit accepted();
65 }
66
67
68 void ClientTransfer::reject() const
69 {
70     PeerPtr ptr = 0;
71     REQUEST_OTHER(requestRejected, ARG(ptr));
72     emit rejected();
73 }
74
75
76 void ClientTransfer::dataReceived(PeerPtr, const QByteArray &data)
77 {
78     // TODO: proper error handling (relay to core)
79     if (!_file) {
80         _file = new QFile(_savePath, this);
81         if (!_file->open(QFile::WriteOnly|QFile::Truncate)) {
82             qWarning() << Q_FUNC_INFO << "Could not open file:" << _file->errorString();
83             return;
84         }
85     }
86
87     if (!_file->isOpen())
88         return;
89
90     if (_file->write(data) < 0) {
91         qWarning() << Q_FUNC_INFO << "Could not write to file:" << _file->errorString();
92         return;
93     }
94
95     emit transferredChanged(transferred());
96 }
97
98
99 void ClientTransfer::onStatusChanged(Transfer::Status status)
100 {
101     switch(status) {
102         case Status::Completed:
103             if (_file)
104                 _file->close();
105             break;
106         case Status::Failed:
107             if (_file)
108                 _file->remove();
109             break;
110         default:
111             ;
112     }
113 }