Ignore:
Timestamp:
May 16, 2014, 9:51:55 AM (11 years ago)
Author:
Silvan Scherrer
Message:

SMPlayer: update trunk to latest 0.8.7

Location:
smplayer/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • smplayer/trunk

  • smplayer/trunk/src/findsubtitles/filedownloader/filedownloader.cpp

    r142 r165  
    11/*  smplayer, GUI front-end for mplayer.
    2     Copyright (C) 2006-2013 Ricardo Villalba <rvm@users.sourceforge.net>
     2    Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
    33
    44    This program is free software; you can redistribute it and/or modify
     
    1717*/
    1818
    19 /* Based on the Qt network/http example */
    20 
    2119#include "filedownloader.h"
    22 #include <QHttp>
    23 #include <QTimer>
     20#include <QFile>
     21#include <QMessageBox>
    2422
    2523FileDownloader::FileDownloader(QWidget *parent) : QProgressDialog(parent)
    2624{
    27         http_get_id = -1;
     25        reply = 0;
     26        manager = new QNetworkAccessManager(this);
     27        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotResponse(QNetworkReply*)));
     28
    2829        setMinimumDuration(0);
     30        setRange(0,0);
    2931
    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 &)));
    3832        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        */
    3938
    4039        setWindowTitle(tr("Downloading..."));
     
    4241
    4342FileDownloader::~FileDownloader() {
    44         //qDebug("FileDownloader::~FileDownloader");
    45         delete http;
     43        delete manager;
    4644}
    4745
    4846void FileDownloader::setProxy(QNetworkProxy proxy) {
    49         http->abort();
    50         http->setProxy(proxy);
     47        manager->setProxy(proxy);
    5148
    5249        qDebug("FileDownloader::setProxy: host: '%s' port: %d type: %d",
     
    5552
    5653void 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)));
    5959
    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()));
    6761}
    6862
    6963void FileDownloader::cancelDownload() {
    70         http_request_aborted = true;
    71         http->abort();
     64        if (reply) reply->abort();
    7265}
    7366
    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) {
     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 {
    8081                hide();
     82                emit downloadFailed(reply->errorString());
    8183                return;
    8284        }
    8385
    8486        hide();
     87        emit downloadFinished(reply->readAll());
     88}
    8589
    86         if (error) {
    87                 emit downloadFailed(http->errorString());
    88         } else {
    89                 emit downloadFinished(http->readAll());
     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);
    9095        }
    9196}
    9297
    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/*
     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);
    101104}
    102105
    103 void FileDownloader::updateDataReadProgress(int bytes_read, int total_bytes) {
    104         if (http_request_aborted) return;
     106void FileDownloader::reportSaveFailed(const QString & file) {
     107        hide();
     108        QMessageBox::warning(this, tr("Error"), tr("An error happened writing %1").arg(file));
     109}
    105110
    106         setMaximum(total_bytes);
    107         setValue(bytes_read);
     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));
    108114}
     115*/
    109116
    110117#include "moc_filedownloader.cpp"
Note: See TracChangeset for help on using the changeset viewer.