source: smplayer/vendor/current/src/findsubtitles/filedownloader/filedownloader.cpp

Last change on this file was 186, checked in by Silvan Scherrer, 9 years ago

SMPlayer: update vendor to 17.1.0

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