1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2009 Ricardo Villalba <rvm@escomposlinux.org>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
/* Based on the Qt network/http example */
21
#include "filedownloader.h"
25
FileDownloader::FileDownloader(QWidget *parent) : QProgressDialog(parent)
27
setMinimumDuration(0);
29
http = new QHttp(this);
31
connect(http, SIGNAL(requestFinished(int, bool)),
32
this, SLOT(httpRequestFinished(int, bool)));
33
connect(http, SIGNAL(dataReadProgress(int, int)),
34
this, SLOT(updateDataReadProgress(int, int)));
35
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
36
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
37
connect(this, SIGNAL(canceled()), this, SLOT(cancelDownload()));
39
setWindowTitle(tr("Downloading..."));
42
FileDownloader::~FileDownloader() {
43
//qDebug("FileDownloader::~FileDownloader");
47
void FileDownloader::setProxy(QNetworkProxy proxy) {
49
http->setProxy(proxy);
51
qDebug("FileDownloader::setProxy: host: '%s' port: %d type: %d",
52
proxy.hostName().toUtf8().constData(), proxy.port(), proxy.type());
55
void FileDownloader::download(QUrl url) {
56
QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
57
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
59
if (!url.userName().isEmpty())
60
http->setUser(url.userName(), url.password());
62
http_request_aborted = false;
63
http_get_id = http->get(url.path(), &buffer);
65
setLabelText(tr("Downloading %1").arg(url.toString()));
68
void FileDownloader::cancelDownload() {
69
http_request_aborted = true;
73
void FileDownloader::httpRequestFinished(int request_id, bool error) {
74
qDebug("FileDownloader::httpRequestFinished: request_id %d, error %d", request_id, error);
76
if (request_id != http_get_id) return;
78
if (http_request_aborted) {
86
emit downloadFailed(http->errorString());
88
emit downloadFinished(buffer.data());
92
void FileDownloader::readResponseHeader(const QHttpResponseHeader &responseHeader) {
93
if (responseHeader.statusCode() != 200) {
94
emit downloadFailed(responseHeader.reasonPhrase());
95
http_request_aborted = true;
102
void FileDownloader::updateDataReadProgress(int bytes_read, int total_bytes) {
103
if (http_request_aborted) return;
105
setMaximum(total_bytes);
106
setValue(bytes_read);
109
#include "moc_filedownloader.cpp"