1 /*
2 * Copyright (c) 2016 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #include "KisRemoteFileFetcher.h"
20
21 #include <QApplication>
22 #include <QMessageBox>
23
24 #include <klocalizedstring.h>
25
KisRemoteFileFetcher(QObject * parent)26 KisRemoteFileFetcher::KisRemoteFileFetcher(QObject *parent)
27 : QObject(parent)
28 , m_request(0)
29 , m_reply(0)
30 {
31 }
32
~KisRemoteFileFetcher()33 KisRemoteFileFetcher::~KisRemoteFileFetcher()
34 {
35 delete m_request;
36 delete m_reply;
37 }
38
fetchFile(const QUrl & remote,QIODevice * io)39 bool KisRemoteFileFetcher::fetchFile(const QUrl &remote, QIODevice *io)
40 {
41 Q_ASSERT(!remote.isLocalFile());
42
43 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
44 m_request = new QNetworkRequest(remote);
45 m_request->setRawHeader("User-Agent", QString("Krita-%1").arg(qApp->applicationVersion()).toUtf8());
46 m_reply = manager->get(*m_request);
47 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
48 connect(m_reply, SIGNAL(finished()), &m_loop, SLOT(quit()));
49
50 // Wait until done
51 m_loop.exec();
52
53 if (m_reply->error() != QNetworkReply::NoError) {
54 QMessageBox::critical(0, i18nc("@title:window", "Krita"), QString("Could not download %1: %2").arg(remote.toDisplayString()).arg(m_reply->errorString()), QMessageBox::Ok);
55 return false;
56 }
57
58 if (!io->isOpen()) {
59 io->open(QIODevice::WriteOnly);
60 }
61 io->write(m_reply->readAll());
62 io->close();
63
64 return true;
65
66 }
67
downloadProgress(qint64,qint64)68 void KisRemoteFileFetcher::downloadProgress(qint64 /*bytesReceived*/, qint64 /*bytesTotal*/)
69 {
70 //qDebug() << "bytesReceived" << bytesReceived << "bytesTotal" << bytesTotal;
71 }
72
error(QNetworkReply::NetworkError error)73 void KisRemoteFileFetcher::error(QNetworkReply::NetworkError error)
74 {
75 Q_UNUSED(error);
76
77 qDebug() << "error" << m_reply->errorString();
78 m_loop.quit();
79 }
80