diff options
author | Qt Continuous Integration System <qt-info@nokia.com> | 2010-02-26 15:04:38 (GMT) |
---|---|---|
committer | Qt Continuous Integration System <qt-info@nokia.com> | 2010-02-26 15:04:38 (GMT) |
commit | ca82ee4ee55e52c75326949148455af1095df014 (patch) | |
tree | 8266295822f50094ff49fd60dc5ac70f075925f2 /examples | |
parent | 27e403d9c6185c606980bb7881e39a2c88138a13 (diff) | |
parent | b78012c855a8b00fa0d100cd0cf6c8de024dfa69 (diff) | |
download | Qt-ca82ee4ee55e52c75326949148455af1095df014.zip Qt-ca82ee4ee55e52c75326949148455af1095df014.tar.gz Qt-ca82ee4ee55e52c75326949148455af1095df014.tar.bz2 |
Merge branch 'master' of scm.dev.nokia.troll.no:qt/qt-s60-public into master-integration
* 'master' of scm.dev.nokia.troll.no:qt/qt-s60-public: (81 commits)
Added addMMPRules for adding conditional MMP_RULES
Allow overriding TARGET.EPOCHEAPSIZE with MMP_RULES
Build break fix for commit d8465414e6fd543cfc20e732030dedd8d2bc685f.
Fix for cetest.
Speed up compilation of this test with MSVC.
New benchmark for QDirIterator
Backporting auto-test utility header from master.
Fix a signed/unsigned comparison compiler warning
Removed an export that shouldn't be exported.
Reduced the code and memory footprint of the keymap.
Enabled Qt key events to work also when native key code is missing.
Cleaning of the patch to QTBUG-3168
Patch to QTBUG-3168
Crash in QGraphicsScenePrivate::setFocusItemHelper
Pass the right arguments to QApplication in GraphicsView benchmark.
Improvements to itemview keypad navigation in S60.
Revert changes made in scroll_sys().
Fix SymbianMakefileGenerator::absolutizePath for clean builds
Set the roleNames of proxy models to the roleNames of the source model.
Add benchmarks for accessing meta-object properties via QScriptValue
...
Diffstat (limited to 'examples')
-rw-r--r-- | examples/network/http/httpwindow.cpp | 129 | ||||
-rw-r--r-- | examples/network/http/httpwindow.h | 23 | ||||
-rw-r--r-- | examples/network/http/main.cpp | 1 | ||||
-rw-r--r-- | examples/network/loopback/dialog.cpp | 10 |
4 files changed, 89 insertions, 74 deletions
diff --git a/examples/network/http/httpwindow.cpp b/examples/network/http/httpwindow.cpp index 95fc82f..ec7cd33 100644 --- a/examples/network/http/httpwindow.cpp +++ b/examples/network/http/httpwindow.cpp @@ -49,9 +49,9 @@ HttpWindow::HttpWindow(QWidget *parent) : QDialog(parent) { #ifndef QT_NO_OPENSSL - urlLineEdit = new QLineEdit("https://"); + urlLineEdit = new QLineEdit("https://qt.nokia.com/"); #else - urlLineEdit = new QLineEdit("http://"); + urlLineEdit = new QLineEdit("http://qt.nokia.com/"); #endif urlLabel = new QLabel(tr("&URL:")); @@ -70,21 +70,14 @@ HttpWindow::HttpWindow(QWidget *parent) progressDialog = new QProgressDialog(this); - http = new QHttp(this); - connect(urlLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableDownloadButton())); - connect(http, SIGNAL(requestFinished(int,bool)), - this, SLOT(httpRequestFinished(int,bool))); - connect(http, SIGNAL(dataReadProgress(int,int)), - this, SLOT(updateDataReadProgress(int,int))); - connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), - this, SLOT(readResponseHeader(QHttpResponseHeader))); - connect(http, SIGNAL(authenticationRequired(QString,quint16,QAuthenticator*)), - this, SLOT(slotAuthenticationRequired(QString,quint16,QAuthenticator*))); + + connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*))); #ifndef QT_NO_OPENSSL - connect(http, SIGNAL(sslErrors(QList<QSslError>)), - this, SLOT(sslErrors(QList<QSslError>))); + connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), + this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); #endif connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile())); @@ -104,9 +97,21 @@ HttpWindow::HttpWindow(QWidget *parent) urlLineEdit->setFocus(); } +void HttpWindow::startRequest(QUrl url) +{ + reply = qnam.get(QNetworkRequest(url)); + connect(reply, SIGNAL(finished()), + this, SLOT(httpFinished())); + connect(reply, SIGNAL(readyRead()), + this, SLOT(httpReadyRead())); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), + this, SLOT(updateDataReadProgress(qint64,qint64))); +} + void HttpWindow::downloadFile() { - QUrl url(urlLineEdit->text()); + url = urlLineEdit->text(); + QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (fileName.isEmpty()) @@ -132,35 +137,26 @@ void HttpWindow::downloadFile() return; } - QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp; - http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port()); - - if (!url.userName().isEmpty()) - http->setUser(url.userName(), url.password()); - - httpRequestAborted = false; - QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/"); - if (path.isEmpty()) - path = "/"; - httpGetId = http->get(path, file); progressDialog->setWindowTitle(tr("HTTP")); progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); downloadButton->setEnabled(false); + + // schedule the request + httpRequestAborted = false; + startRequest(url); } void HttpWindow::cancelDownload() { statusLabel->setText(tr("Download canceled.")); httpRequestAborted = true; - http->abort(); + reply->abort(); downloadButton->setEnabled(true); } -void HttpWindow::httpRequestFinished(int requestId, bool error) +void HttpWindow::httpFinished() { - if (requestId != httpGetId) - return; if (httpRequestAborted) { if (file) { file->close(); @@ -168,54 +164,58 @@ void HttpWindow::httpRequestFinished(int requestId, bool error) delete file; file = 0; } - + reply->deleteLater(); progressDialog->hide(); return; } - if (requestId != httpGetId) - return; - progressDialog->hide(); + file->flush(); file->close(); - if (error) { + + QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); + if (reply->error()) { file->remove(); QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.") - .arg(http->errorString())); + .arg(reply->errorString())); + downloadButton->setEnabled(true); + } else if (!redirectionTarget.isNull()) { + QUrl newUrl = url.resolved(redirectionTarget.toUrl()); + if (QMessageBox::question(this, tr("HTTP"), + tr("Redirect to %1 ?").arg(newUrl.toString()), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + url = newUrl; + reply->deleteLater(); + file->open(QIODevice::WriteOnly); + file->resize(0); + startRequest(url); + return; + } } else { QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName(); statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName)); + downloadButton->setEnabled(true); } - downloadButton->setEnabled(true); + reply->deleteLater(); + reply = 0; delete file; file = 0; } -void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader) +void HttpWindow::httpReadyRead() { - switch (responseHeader.statusCode()) { - case 200: // Ok - case 301: // Moved Permanently - case 302: // Found - case 303: // See Other - case 307: // Temporary Redirect - // these are not error conditions - break; - - default: - QMessageBox::information(this, tr("HTTP"), - tr("Download failed: %1.") - .arg(responseHeader.reasonPhrase())); - httpRequestAborted = true; - progressDialog->hide(); - http->abort(); - } + // this slot gets called everytime the QNetworkReply has new data. + // We read all of its new data and write it into the file. + // That way we use less RAM than when reading it at the finished() + // signal of the QNetworkReply + if (file) + file->write(reply->readAll()); } -void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes) +void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes) { if (httpRequestAborted) return; @@ -229,14 +229,19 @@ void HttpWindow::enableDownloadButton() downloadButton->setEnabled(!urlLineEdit->text().isEmpty()); } -void HttpWindow::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator) +void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator) { QDialog dlg; Ui::Dialog ui; ui.setupUi(&dlg); dlg.adjustSize(); - ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(hostName)); - + ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host())); + + // Did the URL have information? Fill the UI + // This is only relevant if the URL-supplied credentials were wrong + ui.userEdit->setText(url.userName()); + ui.passwordEdit->setText(url.password()); + if (dlg.exec() == QDialog::Accepted) { authenticator->setUser(ui.userEdit->text()); authenticator->setPassword(ui.passwordEdit->text()); @@ -244,7 +249,7 @@ void HttpWindow::slotAuthenticationRequired(const QString &hostName, quint16, QA } #ifndef QT_NO_OPENSSL -void HttpWindow::sslErrors(const QList<QSslError> &errors) +void HttpWindow::sslErrors(QNetworkReply*,const QList<QSslError> &errors) { QString errorString; foreach (const QSslError &error, errors) { @@ -253,10 +258,10 @@ void HttpWindow::sslErrors(const QList<QSslError> &errors) errorString += error.errorString(); } - if (QMessageBox::warning(this, tr("HTTP Example"), + if (QMessageBox::warning(this, tr("HTTP"), tr("One or more SSL errors has occurred: %1").arg(errorString), QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) { - http->ignoreSslErrors(); + reply->ignoreSslErrors(); } } #endif diff --git a/examples/network/http/httpwindow.h b/examples/network/http/httpwindow.h index 9dca8a5..83898af 100644 --- a/examples/network/http/httpwindow.h +++ b/examples/network/http/httpwindow.h @@ -43,18 +43,21 @@ #define HTTPWINDOW_H #include <QDialog> +#include <QNetworkAccessManager> +#include <QUrl> QT_BEGIN_NAMESPACE class QDialogButtonBox; class QFile; -class QHttp; -class QHttpResponseHeader; class QLabel; class QLineEdit; class QProgressDialog; class QPushButton; class QSslError; class QAuthenticator; +class QNetworkReply; + + QT_END_NAMESPACE class HttpWindow : public QDialog @@ -64,16 +67,18 @@ class HttpWindow : public QDialog public: HttpWindow(QWidget *parent = 0); + void startRequest(QUrl url); + private slots: void downloadFile(); void cancelDownload(); - void httpRequestFinished(int requestId, bool error); - void readResponseHeader(const QHttpResponseHeader &responseHeader); - void updateDataReadProgress(int bytesRead, int totalBytes); + void httpFinished(); + void httpReadyRead(); + void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes); void enableDownloadButton(); - void slotAuthenticationRequired(const QString &, quint16, QAuthenticator *); + void slotAuthenticationRequired(QNetworkReply*,QAuthenticator *); #ifndef QT_NO_OPENSSL - void sslErrors(const QList<QSslError> &errors); + void sslErrors(QNetworkReply*,const QList<QSslError> &errors); #endif private: @@ -85,7 +90,9 @@ private: QPushButton *quitButton; QDialogButtonBox *buttonBox; - QHttp *http; + QUrl url; + QNetworkAccessManager qnam; + QNetworkReply *reply; QFile *file; int httpGetId; bool httpRequestAborted; diff --git a/examples/network/http/main.cpp b/examples/network/http/main.cpp index ecbe100..817b2be 100644 --- a/examples/network/http/main.cpp +++ b/examples/network/http/main.cpp @@ -46,7 +46,6 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); - qWarning("The usage of QHttp is not recommended anymore, please use QNetworkAccessManager."); HttpWindow httpWin; httpWin.show(); return httpWin.exec(); diff --git a/examples/network/loopback/dialog.cpp b/examples/network/loopback/dialog.cpp index 27cff31..b504e36 100644 --- a/examples/network/loopback/dialog.cpp +++ b/examples/network/loopback/dialog.cpp @@ -44,12 +44,12 @@ #include "dialog.h" -#if !defined(Q_OS_WINCE) +#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) static const int TotalBytes = 50 * 1024 * 1024; #else static const int TotalBytes = 5 * 1024 * 1024; #endif -static const int PayloadSize = 65536; +static const int PayloadSize = 64 * 1024; // 64 KB Dialog::Dialog(QWidget *parent) : QDialog(parent) @@ -130,6 +130,7 @@ void Dialog::acceptConnection() void Dialog::startTransfer() { + // called when the TCP client connected to the loopback server bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@')); clientStatusLabel->setText(tr("Connected")); } @@ -155,8 +156,11 @@ void Dialog::updateServerProgress() void Dialog::updateClientProgress(qint64 numBytes) { + // callen when the TCP client has written some bytes bytesWritten += (int)numBytes; - if (bytesToWrite > 0) + + // only write more if not finished and when the Qt write buffer is below a certain size. + if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4*PayloadSize) bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@')); clientProgressBar->setMaximum(TotalBytes); |