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