- Timestamp:
- May 16, 2014, 9:51:55 AM (11 years ago)
- Location:
- smplayer/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
smplayer/trunk
- Property svn:mergeinfo changed
/smplayer/vendor/current merged: 163
- Property svn:mergeinfo changed
-
smplayer/trunk/src/findsubtitles/filedownloader/filedownloader.cpp
r142 r165 1 1 /* smplayer, GUI front-end for mplayer. 2 Copyright (C) 2006-201 3Ricardo Villalba <rvm@users.sourceforge.net>2 Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net> 3 3 4 4 This program is free software; you can redistribute it and/or modify … … 17 17 */ 18 18 19 /* Based on the Qt network/http example */20 21 19 #include "filedownloader.h" 22 #include <Q Http>23 #include <Q Timer>20 #include <QFile> 21 #include <QMessageBox> 24 22 25 23 FileDownloader::FileDownloader(QWidget *parent) : QProgressDialog(parent) 26 24 { 27 http_get_id = -1; 25 reply = 0; 26 manager = new QNetworkAccessManager(this); 27 connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotResponse(QNetworkReply*))); 28 28 29 setMinimumDuration(0); 30 setRange(0,0); 29 31 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 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 */ 39 38 40 39 setWindowTitle(tr("Downloading...")); … … 42 41 43 42 FileDownloader::~FileDownloader() { 44 //qDebug("FileDownloader::~FileDownloader"); 45 delete http; 43 delete manager; 46 44 } 47 45 48 46 void FileDownloader::setProxy(QNetworkProxy proxy) { 49 http->abort(); 50 http->setProxy(proxy); 47 manager->setProxy(proxy); 51 48 52 49 qDebug("FileDownloader::setProxy: host: '%s' port: %d type: %d", … … 55 52 56 53 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()); 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 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())); 60 setLabelText(tr("Connecting to %1").arg(url.host())); 67 61 } 68 62 69 63 void FileDownloader::cancelDownload() { 70 http_request_aborted = true; 71 http->abort(); 64 if (reply) reply->abort(); 72 65 } 73 66 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) { 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 { 80 81 hide(); 82 emit downloadFailed(reply->errorString()); 81 83 return; 82 84 } 83 85 84 86 hide(); 87 emit downloadFinished(reply->readAll()); 88 } 85 89 86 if (error) { 87 emit downloadFailed(http->errorString()); 88 } else { 89 emit downloadFinished(http->readAll()); 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); 90 95 } 91 96 } 92 97 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 } 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); 101 104 } 102 105 103 void FileDownloader::updateDataReadProgress(int bytes_read, int total_bytes) { 104 if (http_request_aborted) return; 106 void FileDownloader::reportSaveFailed(const QString & file) { 107 hide(); 108 QMessageBox::warning(this, tr("Error"), tr("An error happened writing %1").arg(file)); 109 } 105 110 106 setMaximum(total_bytes); 107 setValue(bytes_read); 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)); 108 114 } 115 */ 109 116 110 117 #include "moc_filedownloader.cpp"
Note:
See TracChangeset
for help on using the changeset viewer.