Ignore:
Timestamp:
Aug 2, 2010, 9:27:30 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/examples/network/http/httpwindow.cpp

    r651 r769  
    5050{
    5151#ifndef QT_NO_OPENSSL
    52     urlLineEdit = new QLineEdit("https://");
     52    urlLineEdit = new QLineEdit("https://qt.nokia.com/");
    5353#else
    54     urlLineEdit = new QLineEdit("http://");
     54    urlLineEdit = new QLineEdit("http://qt.nokia.com/");
    5555#endif
    5656
     
    7171    progressDialog = new QProgressDialog(this);
    7272
    73     http = new QHttp(this);
    74 
    7573    connect(urlLineEdit, SIGNAL(textChanged(QString)),
    7674            this, SLOT(enableDownloadButton()));
    77     connect(http, SIGNAL(requestFinished(int,bool)),
    78             this, SLOT(httpRequestFinished(int,bool)));
    79     connect(http, SIGNAL(dataReadProgress(int,int)),
    80             this, SLOT(updateDataReadProgress(int,int)));
    81     connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)),
    82             this, SLOT(readResponseHeader(QHttpResponseHeader)));
    83     connect(http, SIGNAL(authenticationRequired(QString,quint16,QAuthenticator*)),
    84             this, SLOT(slotAuthenticationRequired(QString,quint16,QAuthenticator*)));
     75
     76    connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
     77            this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
    8578#ifndef QT_NO_OPENSSL
    86     connect(http, SIGNAL(sslErrors(QList<QSslError>)),
    87             this, SLOT(sslErrors(QList<QSslError>)));
     79    connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
     80            this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
    8881#endif
    8982    connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
     
    10598}
    10699
     100void HttpWindow::startRequest(QUrl url)
     101{
     102    reply = qnam.get(QNetworkRequest(url));
     103    connect(reply, SIGNAL(finished()),
     104            this, SLOT(httpFinished()));
     105    connect(reply, SIGNAL(readyRead()),
     106            this, SLOT(httpReadyRead()));
     107    connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
     108            this, SLOT(updateDataReadProgress(qint64,qint64)));
     109}
     110
    107111void HttpWindow::downloadFile()
    108112{
    109     QUrl url(urlLineEdit->text());
     113    url = urlLineEdit->text();
     114
    110115    QFileInfo fileInfo(url.path());
    111116    QString fileName = fileInfo.fileName();
     
    133138    }
    134139
    135     QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
    136     http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
    137    
    138     if (!url.userName().isEmpty())
    139         http->setUser(url.userName(), url.password());
    140 
    141     httpRequestAborted = false;
    142     QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
    143     if (path.isEmpty())
    144         path = "/";
    145     httpGetId = http->get(path, file);
    146140
    147141    progressDialog->setWindowTitle(tr("HTTP"));
    148142    progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
    149143    downloadButton->setEnabled(false);
     144
     145    // schedule the request
     146    httpRequestAborted = false;
     147    startRequest(url);
    150148}
    151149
     
    154152    statusLabel->setText(tr("Download canceled."));
    155153    httpRequestAborted = true;
    156     http->abort();
     154    reply->abort();
    157155    downloadButton->setEnabled(true);
    158156}
    159157
    160 void HttpWindow::httpRequestFinished(int requestId, bool error)
    161 {
    162     if (requestId != httpGetId)
    163         return;
     158void HttpWindow::httpFinished()
     159{
    164160    if (httpRequestAborted) {
    165161        if (file) {
     
    169165            file = 0;
    170166        }
    171 
     167        reply->deleteLater();
    172168        progressDialog->hide();
    173169        return;
    174170    }
    175171
    176     if (requestId != httpGetId)
    177         return;
    178 
    179172    progressDialog->hide();
     173    file->flush();
    180174    file->close();
    181175
    182     if (error) {
     176
     177    QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
     178    if (reply->error()) {
    183179        file->remove();
    184180        QMessageBox::information(this, tr("HTTP"),
    185181                                 tr("Download failed: %1.")
    186                                  .arg(http->errorString()));
     182                                 .arg(reply->errorString()));
     183        downloadButton->setEnabled(true);
     184    } else if (!redirectionTarget.isNull()) {       
     185        QUrl newUrl = url.resolved(redirectionTarget.toUrl());
     186        if (QMessageBox::question(this, tr("HTTP"),
     187                                  tr("Redirect to %1 ?").arg(newUrl.toString()),
     188                                  QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
     189            url = newUrl;
     190            reply->deleteLater();
     191            file->open(QIODevice::WriteOnly);
     192            file->resize(0);
     193            startRequest(url);
     194            return;
     195        }
    187196    } else {
    188197        QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
    189198        statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));
    190     }
    191 
    192     downloadButton->setEnabled(true);
     199        downloadButton->setEnabled(true);
     200    }
     201
     202    reply->deleteLater();
     203    reply = 0;
    193204    delete file;
    194205    file = 0;
    195206}
    196207
    197 void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader)
    198 {
    199     switch (responseHeader.statusCode()) {
    200     case 200:                   // Ok
    201     case 301:                   // Moved Permanently
    202     case 302:                   // Found
    203     case 303:                   // See Other
    204     case 307:                   // Temporary Redirect
    205         // these are not error conditions
    206         break;
    207 
    208     default:
    209         QMessageBox::information(this, tr("HTTP"),
    210                                  tr("Download failed: %1.")
    211                                  .arg(responseHeader.reasonPhrase()));
    212         httpRequestAborted = true;
    213         progressDialog->hide();
    214         http->abort();
    215     }
    216 }
    217 
    218 void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes)
     208void HttpWindow::httpReadyRead()
     209{
     210    // this slot gets called everytime the QNetworkReply has new data.
     211    // We read all of its new data and write it into the file.
     212    // That way we use less RAM than when reading it at the finished()
     213    // signal of the QNetworkReply
     214    if (file)
     215        file->write(reply->readAll());
     216}
     217
     218void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
    219219{
    220220    if (httpRequestAborted)
     
    230230}
    231231
    232 void HttpWindow::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)
     232void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator)
    233233{
    234234    QDialog dlg;
     
    236236    ui.setupUi(&dlg);
    237237    dlg.adjustSize();
    238     ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(hostName));
    239    
     238    ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host()));
     239
     240    // Did the URL have information? Fill the UI
     241    // This is only relevant if the URL-supplied credentials were wrong
     242    ui.userEdit->setText(url.userName());
     243    ui.passwordEdit->setText(url.password());
     244
    240245    if (dlg.exec() == QDialog::Accepted) {
    241246        authenticator->setUser(ui.userEdit->text());
     
    245250
    246251#ifndef QT_NO_OPENSSL
    247 void HttpWindow::sslErrors(const QList<QSslError> &errors)
     252void HttpWindow::sslErrors(QNetworkReply*,const QList<QSslError> &errors)
    248253{
    249254    QString errorString;
     
    254259    }
    255260   
    256     if (QMessageBox::warning(this, tr("HTTP Example"),
     261    if (QMessageBox::warning(this, tr("HTTP"),
    257262                             tr("One or more SSL errors has occurred: %1").arg(errorString),
    258263                             QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {
    259         http->ignoreSslErrors();
     264        reply->ignoreSslErrors();
    260265    }
    261266}
Note: See TracChangeset for help on using the changeset viewer.