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

Last change on this file since 175 was 175, checked in by Silvan Scherrer, 10 years ago

smplayer: update vendor to version 16.4

File size: 3.8 KB
Line 
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
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 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
42FileDownloader::~FileDownloader() {
43 delete manager;
44}
45
46void 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
53void 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
63void FileDownloader::cancelDownload() {
64 if (reply) reply->abort();
65}
66
67void 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
90void 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/*
99void 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
106void FileDownloader::reportSaveFailed(const QString & file) {
107 hide();
108 QMessageBox::warning(this, tr("Error"), tr("An error happened writing %1").arg(file));
109}
110
111void 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
Note: See TracBrowser for help on using the repository browser.