[154] | 1 | /* smplayer, GUI front-end for mplayer.
|
---|
[176] | 2 | Copyright (C) 2006-2013 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
[154] | 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 "codedownloader.h"
|
---|
| 20 | #include <QFile>
|
---|
| 21 | #include <QMessageBox>
|
---|
| 22 |
|
---|
| 23 | CodeDownloader::CodeDownloader(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 | connect(this, SIGNAL(fileSaved(const QString &, const QString &)), this, SLOT(reportFileSaved(const QString &,const QString &)));
|
---|
| 34 | connect(this, SIGNAL(saveFailed(const QString &)), this, SLOT(reportSaveFailed(const QString &)));
|
---|
| 35 | connect(this, SIGNAL(errorOcurred(int,QString)), this, SLOT(reportError(int,QString)));
|
---|
| 36 |
|
---|
| 37 | setWindowTitle(tr("Downloading..."));
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | CodeDownloader::~CodeDownloader() {
|
---|
| 41 | delete manager;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | void CodeDownloader::setProxy(QNetworkProxy proxy) {
|
---|
| 45 | manager->setProxy(proxy);
|
---|
| 46 |
|
---|
| 47 | qDebug("CodeDownloader::setProxy: host: '%s' port: %d type: %d",
|
---|
| 48 | proxy.hostName().toUtf8().constData(), proxy.port(), proxy.type());
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void CodeDownloader::download(QUrl url) {
|
---|
| 52 | QNetworkRequest req(url);
|
---|
| 53 | req.setRawHeader("User-Agent", "SMPlayer");
|
---|
| 54 | reply = manager->get(req);
|
---|
| 55 | connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
---|
| 56 | this, SLOT(updateDataReadProgress(qint64, qint64)));
|
---|
| 57 |
|
---|
| 58 | setLabelText(tr("Connecting to %1").arg(url.host()));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void CodeDownloader::cancelDownload() {
|
---|
| 62 | if (reply) reply->abort();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void CodeDownloader::gotResponse(QNetworkReply* reply) {
|
---|
| 66 | if (reply->error() == QNetworkReply::NoError) {
|
---|
| 67 | int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
---|
| 68 | qDebug("CodeDownloader::gotResponse: status: %d", status);
|
---|
| 69 | switch (status) {
|
---|
| 70 | case 301:
|
---|
| 71 | case 302:
|
---|
| 72 | case 307:
|
---|
| 73 | QString r_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString();
|
---|
| 74 | qDebug("CodeDownloader::gotResponse: redirected: %s", r_url.toLatin1().constData());
|
---|
| 75 | download(r_url);
|
---|
| 76 | return;
|
---|
| 77 | }
|
---|
| 78 | } else {
|
---|
| 79 | emit errorOcurred((int)reply->error(), reply->errorString());
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | emit downloadFinished();
|
---|
| 84 | save(reply->readAll());
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void CodeDownloader::save(QByteArray bytes) {
|
---|
| 88 | if (output_filename.isEmpty()) {
|
---|
| 89 | qWarning("CodeDownloader::save: output filename is empty");
|
---|
| 90 | emit saveFailed(output_filename);
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | QFile file(output_filename);
|
---|
| 95 | if (!file.open(QIODevice::WriteOnly)) {
|
---|
| 96 | qWarning("CodeDownloader::save: could not open %s for writing", output_filename.toUtf8().constData());
|
---|
| 97 | emit saveFailed(output_filename);
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | file.write(bytes);
|
---|
| 102 | file.close();
|
---|
| 103 |
|
---|
| 104 | QString version;
|
---|
| 105 | QRegExp rx("Version: ([\\d,-]+)");
|
---|
| 106 | if (rx.indexIn(bytes)) {
|
---|
| 107 | version = rx.cap(1);
|
---|
| 108 | qDebug("CodeDownloader::save: version: %s", version.toLatin1().constData());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | emit fileSaved(output_filename, version);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void CodeDownloader::updateDataReadProgress(qint64 bytes_read, qint64 total_bytes) {
|
---|
[176] | 115 | #ifndef QT_NO_DEBUG_OUTPUT
|
---|
[154] | 116 | qDebug() << "CodeDownloader::updateDataReadProgress: " << bytes_read << " " << total_bytes;
|
---|
[176] | 117 | #endif
|
---|
[154] | 118 | if (total_bytes > -1) {
|
---|
| 119 | setMaximum(total_bytes);
|
---|
| 120 | setValue(bytes_read);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | void CodeDownloader::reportFileSaved(const QString &, const QString & version) {
|
---|
| 125 | hide();
|
---|
| 126 | QString t = tr("The Youtube code has been updated successfully.");
|
---|
| 127 | if (!version.isEmpty()) t += "<br>"+ tr("Installed version: %1").arg(version);
|
---|
| 128 | QMessageBox::information(this, tr("Success"),t);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void CodeDownloader::reportSaveFailed(const QString & file) {
|
---|
| 132 | hide();
|
---|
| 133 | QMessageBox::warning(this, tr("Error"), tr("An error happened writing %1").arg(file));
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void CodeDownloader::reportError(int, QString error_str) {
|
---|
| 137 | hide();
|
---|
| 138 | QMessageBox::warning(this, tr("Error"), tr("An error happened while downloading the file:<br>%1").arg(error_str));
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | #include "moc_codedownloader.cpp"
|
---|
| 142 |
|
---|