1 /*
2 * QtDownload.cpp
3 *
4 *
5 * Copyright 2013 Scott Wilson.
6 *
7 * This file is part of SuperCollider.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24 #include "Common.h"
25 #include "QtDownload.h"
26 #include "QcWidgetFactory.h"
27
28 #include <QCoreApplication>
29 #include <QUrl>
30 #include <QNetworkRequest>
31 #include <QFile>
32 #include <QDebug>
33
34 QC_DECLARE_QOBJECT_FACTORY(QtDownload);
35
QtDownload()36 QtDownload::QtDownload(): QObject(0), started(false) {}
37
~QtDownload()38 QtDownload::~QtDownload() {}
39
40
setSource(const QString & t)41 void QtDownload::setSource(const QString& t) { m_target = t; }
42
setDestination(const QString & l)43 void QtDownload::setDestination(const QString& l) { m_local = l; }
44
downloadFinished()45 void QtDownload::downloadFinished() {
46 if (m_reply->error() == QNetworkReply::NoError) { // only write if no error
47 QFile localFile(this->m_local);
48 if (!localFile.open(QIODevice::WriteOnly))
49 return;
50 const QByteArray sdata = m_reply->readAll();
51 localFile.write(sdata);
52 qDebug() << sdata;
53 localFile.close();
54
55 // call action
56 Q_EMIT(doFinished());
57 }
58
59 m_reply->deleteLater();
60 m_manager->deleteLater();
61 }
62
download()63 void QtDownload::download() {
64 if (!started) {
65 started = true;
66 m_manager = new QNetworkAccessManager(this);
67 QUrl url = QUrl::fromEncoded(this->m_target.toLocal8Bit());
68 QNetworkRequest request;
69 request.setUrl(url);
70 m_reply = m_manager->get(request);
71 QObject::connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), this,
72 SLOT(downloadProgress(qint64, qint64)));
73 QObject::connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
74 SLOT(replyError(QNetworkReply::NetworkError)));
75 bool fin = QObject::connect(m_reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
76 if (!fin) {
77 qWarning("Download could not connect");
78 }
79 }
80 }
81
cancel()82 void QtDownload::cancel() {
83 if (m_reply) {
84 m_reply->disconnect();
85 m_reply->abort();
86 }
87 }
88
replyError(QNetworkReply::NetworkError errorCode)89 void QtDownload::replyError(QNetworkReply::NetworkError errorCode) {
90 qWarning() << m_reply->errorString();
91
92 // call action
93 Q_EMIT(doError());
94 }
95
downloadProgress(qint64 received,qint64 total)96 void QtDownload::downloadProgress(qint64 received, qint64 total) {
97 qDebug() << received << total;
98
99 // call action
100 Q_EMIT(doProgress(static_cast<int>(received), static_cast<int>(total)));
101 }