| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2016 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 | #include "filedownloader.h"
|
|---|
| 20 | #include <QFile>
|
|---|
| 21 | #include <QMessageBox>
|
|---|
| 22 |
|
|---|
| 23 | FileDownloader::FileDownloader(QWidget *parent) : QProgressDialog(parent)
|
|---|
| 24 | {
|
|---|
| 25 | reply = 0;
|
|---|
| 26 | manager = new QNetworkAccessManager(this);
|
|---|
| 27 | connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotResponse(QNetworkReply*)));
|
|---|
| 28 |
|
|---|
| 29 | setMinimumDuration(0);
|
|---|
| 30 | setRange(0,0);
|
|---|
| 31 |
|
|---|
| 32 | connect(this, SIGNAL(canceled()), this, SLOT(cancelDownload()));
|
|---|
| 33 | /*
|
|---|
| 34 | connect(this, SIGNAL(fileSaved(const QString &, const QString &)), this, SLOT(reportFileSaved(const QString &,const QString &)));
|
|---|
| 35 | connect(this, SIGNAL(saveFailed(const QString &)), this, SLOT(reportSaveFailed(const QString &)));
|
|---|
| 36 | connect(this, SIGNAL(errorOcurred(int,QString)), this, SLOT(reportError(int,QString)));
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | setWindowTitle(tr("Downloading..."));
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | FileDownloader::~FileDownloader() {
|
|---|
| 43 | delete manager;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | void FileDownloader::setProxy(QNetworkProxy proxy) {
|
|---|
| 47 | manager->setProxy(proxy);
|
|---|
| 48 |
|
|---|
| 49 | qDebug("FileDownloader::setProxy: host: '%s' port: %d type: %d",
|
|---|
| 50 | proxy.hostName().toUtf8().constData(), proxy.port(), proxy.type());
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void FileDownloader::download(QUrl url) {
|
|---|
| 54 | QNetworkRequest req(url);
|
|---|
| 55 | req.setRawHeader("User-Agent", "SMPlayer");
|
|---|
| 56 | reply = manager->get(req);
|
|---|
| 57 | connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
|---|
| 58 | this, SLOT(updateDataReadProgress(qint64, qint64)));
|
|---|
| 59 |
|
|---|
| 60 | setLabelText(tr("Connecting to %1").arg(url.host()));
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void FileDownloader::cancelDownload() {
|
|---|
| 64 | if (reply) reply->abort();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void FileDownloader::gotResponse(QNetworkReply* reply) {
|
|---|
| 68 | if (reply->error() == QNetworkReply::NoError) {
|
|---|
| 69 | int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|---|
| 70 | qDebug("FileDownloader::gotResponse: status: %d", status);
|
|---|
| 71 | switch (status) {
|
|---|
| 72 | case 301:
|
|---|
| 73 | case 302:
|
|---|
| 74 | case 307:
|
|---|
| 75 | QString r_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString();
|
|---|
| 76 | qDebug("FileDownloader::gotResponse: redirected: %s", r_url.toLatin1().constData());
|
|---|
| 77 | download(r_url);
|
|---|
| 78 | return;
|
|---|
| 79 | }
|
|---|
| 80 | } else {
|
|---|
| 81 | hide();
|
|---|
| 82 | emit downloadFailed(reply->errorString());
|
|---|
| 83 | return;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | hide();
|
|---|
| 87 | emit downloadFinished(reply->readAll());
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void FileDownloader::updateDataReadProgress(qint64 bytes_read, qint64 total_bytes) {
|
|---|
| 91 | qDebug() << "FileDownloader::updateDataReadProgress: " << bytes_read << " " << total_bytes;
|
|---|
| 92 | if (total_bytes > -1) {
|
|---|
| 93 | setMaximum(total_bytes);
|
|---|
| 94 | setValue(bytes_read);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /*
|
|---|
| 99 | void FileDownloader::reportFileSaved(const QString &, const QString & version) {
|
|---|
| 100 | hide();
|
|---|
| 101 | QString t = tr("The Youtube code has been updated successfully.");
|
|---|
| 102 | if (!version.isEmpty()) t += "<br>"+ tr("Installed version: %1").arg(version);
|
|---|
| 103 | QMessageBox::information(this, tr("Success"),t);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void FileDownloader::reportSaveFailed(const QString & file) {
|
|---|
| 107 | hide();
|
|---|
| 108 | QMessageBox::warning(this, tr("Error"), tr("An error happened writing %1").arg(file));
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | void FileDownloader::reportError(int, QString error_str) {
|
|---|
| 112 | hide();
|
|---|
| 113 | QMessageBox::warning(this, tr("Error"), tr("An error happened while downloading the file:<br>%1").arg(error_str));
|
|---|
| 114 | }
|
|---|
| 115 | */
|
|---|
| 116 |
|
|---|
| 117 | #include "moc_filedownloader.cpp"
|
|---|
| 118 |
|
|---|