| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2012 Ricardo Villalba <[email protected]>
|
|---|
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | /* Based on the Qt network/http example */
|
|---|
| 20 |
|
|---|
| 21 | #include "filedownloader.h"
|
|---|
| 22 | #include <QHttp>
|
|---|
| 23 | #include <QTimer>
|
|---|
| 24 |
|
|---|
| 25 | FileDownloader::FileDownloader(QWidget *parent) : QProgressDialog(parent)
|
|---|
| 26 | {
|
|---|
| 27 | http_get_id = -1;
|
|---|
| 28 | setMinimumDuration(0);
|
|---|
| 29 |
|
|---|
| 30 | http = new QHttp(this);
|
|---|
| 31 |
|
|---|
| 32 | connect(http, SIGNAL(requestFinished(int, bool)),
|
|---|
| 33 | this, SLOT(httpRequestFinished(int, bool)));
|
|---|
| 34 | connect(http, SIGNAL(dataReadProgress(int, int)),
|
|---|
| 35 | this, SLOT(updateDataReadProgress(int, int)));
|
|---|
| 36 | connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
|
|---|
| 37 | this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
|
|---|
| 38 | connect(this, SIGNAL(canceled()), this, SLOT(cancelDownload()));
|
|---|
| 39 |
|
|---|
| 40 | setWindowTitle(tr("Downloading..."));
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | FileDownloader::~FileDownloader() {
|
|---|
| 44 | //qDebug("FileDownloader::~FileDownloader");
|
|---|
| 45 | delete http;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void FileDownloader::setProxy(QNetworkProxy proxy) {
|
|---|
| 49 | http->abort();
|
|---|
| 50 | http->setProxy(proxy);
|
|---|
| 51 |
|
|---|
| 52 | qDebug("FileDownloader::setProxy: host: '%s' port: %d type: %d",
|
|---|
| 53 | proxy.hostName().toUtf8().constData(), proxy.port(), proxy.type());
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void FileDownloader::download(QUrl url) {
|
|---|
| 57 | QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
|
|---|
| 58 | http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
|
|---|
| 59 |
|
|---|
| 60 | if (!url.userName().isEmpty())
|
|---|
| 61 | http->setUser(url.userName(), url.password());
|
|---|
| 62 |
|
|---|
| 63 | http_request_aborted = false;
|
|---|
| 64 | http_get_id = http->get(url.path());
|
|---|
| 65 |
|
|---|
| 66 | setLabelText(tr("Downloading %1").arg(url.toString()));
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | void FileDownloader::cancelDownload() {
|
|---|
| 70 | http_request_aborted = true;
|
|---|
| 71 | http->abort();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void FileDownloader::httpRequestFinished(int request_id, bool error) {
|
|---|
| 75 | qDebug("FileDownloader::httpRequestFinished: request_id %d, error %d", request_id, error);
|
|---|
| 76 |
|
|---|
| 77 | if (request_id != http_get_id) return;
|
|---|
| 78 |
|
|---|
| 79 | if (http_request_aborted) {
|
|---|
| 80 | hide();
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | hide();
|
|---|
| 85 |
|
|---|
| 86 | if (error) {
|
|---|
| 87 | emit downloadFailed(http->errorString());
|
|---|
| 88 | } else {
|
|---|
| 89 | emit downloadFinished(http->readAll());
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void FileDownloader::readResponseHeader(const QHttpResponseHeader &responseHeader) {
|
|---|
| 94 | if (responseHeader.statusCode() != 200) {
|
|---|
| 95 | emit downloadFailed(responseHeader.reasonPhrase());
|
|---|
| 96 | http_request_aborted = true;
|
|---|
| 97 | hide();
|
|---|
| 98 | http->abort();
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void FileDownloader::updateDataReadProgress(int bytes_read, int total_bytes) {
|
|---|
| 104 | if (http_request_aborted) return;
|
|---|
| 105 |
|
|---|
| 106 | setMaximum(total_bytes);
|
|---|
| 107 | setValue(bytes_read);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | #include "moc_filedownloader.cpp"
|
|---|
| 111 |
|
|---|