Changeset 769 for trunk/examples/network


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:
18 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/examples/network/fortuneclient/fortuneclient.pro

    r561 r769  
    1313    include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
    1414    HEADERS += $$QT_SOURCE_TREE/examples/network/qftp/sym_iap_util.h
    15     LIBS += -lesock
     15    LIBS += -lesock -lcommdb -linsock # For IAP selection
    1616    TARGET.CAPABILITY = "NetworkServices ReadUserData WriteUserData"
    1717    TARGET.EPOCHEAPSIZE = 0x20000 0x2000000
  • trunk/examples/network/fortuneserver/fortuneserver.pro

    r561 r769  
    1414    include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
    1515    HEADERS += $$QT_SOURCE_TREE/examples/network/qftp/sym_iap_util.h
    16     LIBS += -lesock
     16    LIBS += -lesock -lcommdb -linsock # For IAP selection
    1717    TARGET.CAPABILITY = "All -TCB"
    1818    TARGET.EPOCHEAPSIZE = 0x20000 0x2000000
  • trunk/examples/network/googlesuggest/googlesuggest.cpp

    r651 r769  
    233233}
    234234//! [9]
     235
  • trunk/examples/network/googlesuggest/searchbox.cpp

    r651 r769  
    7171}
    7272//! [2]
     73
  • 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}
  • trunk/examples/network/http/httpwindow.h

    r651 r769  
    4444
    4545#include <QDialog>
     46#include <QNetworkAccessManager>
     47#include <QUrl>
    4648
    4749QT_BEGIN_NAMESPACE
    4850class QDialogButtonBox;
    4951class QFile;
    50 class QHttp;
    51 class QHttpResponseHeader;
    5252class QLabel;
    5353class QLineEdit;
     
    5656class QSslError;
    5757class QAuthenticator;
     58class QNetworkReply;
     59
     60
    5861QT_END_NAMESPACE
    5962
     
    6568    HttpWindow(QWidget *parent = 0);
    6669
     70    void startRequest(QUrl url);
     71
    6772private slots:
    6873    void downloadFile();
    6974    void cancelDownload();
    70     void httpRequestFinished(int requestId, bool error);
    71     void readResponseHeader(const QHttpResponseHeader &responseHeader);
    72     void updateDataReadProgress(int bytesRead, int totalBytes);
     75    void httpFinished();
     76    void httpReadyRead();
     77    void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes);
    7378    void enableDownloadButton();
    74     void slotAuthenticationRequired(const QString &, quint16, QAuthenticator *);
     79    void slotAuthenticationRequired(QNetworkReply*,QAuthenticator *);
    7580#ifndef QT_NO_OPENSSL
    76     void sslErrors(const QList<QSslError> &errors);
     81    void sslErrors(QNetworkReply*,const QList<QSslError> &errors);
    7782#endif
    7883
     
    8691    QDialogButtonBox *buttonBox;
    8792
    88     QHttp *http;
     93    QUrl url;
     94    QNetworkAccessManager qnam;
     95    QNetworkReply *reply;
    8996    QFile *file;
    9097    int httpGetId;
  • trunk/examples/network/http/main.cpp

    r651 r769  
    4747{
    4848    QApplication app(argc, argv);
    49     qWarning("The usage of QHttp is not recommended anymore, please use QNetworkAccessManager.");
    5049    HttpWindow httpWin;
    5150    httpWin.show();
  • trunk/examples/network/loopback/dialog.cpp

    r651 r769  
    4545#include "dialog.h"
    4646
    47 #if !defined(Q_OS_WINCE)
     47#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
    4848static const int TotalBytes = 50 * 1024 * 1024;
    4949#else
    5050static const int TotalBytes = 5 * 1024 * 1024;
    5151#endif
    52 static const int PayloadSize = 65536;
     52static const int PayloadSize = 64 * 1024; // 64 KB
    5353
    5454Dialog::Dialog(QWidget *parent)
     
    131131void Dialog::startTransfer()
    132132{
     133    // called when the TCP client connected to the loopback server
    133134    bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@'));
    134135    clientStatusLabel->setText(tr("Connected"));
     
    156157void Dialog::updateClientProgress(qint64 numBytes)
    157158{
     159    // callen when the TCP client has written some bytes
    158160    bytesWritten += (int)numBytes;
    159     if (bytesToWrite > 0)
     161
     162    // only write more if not finished and when the Qt write buffer is below a certain size.
     163    if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4*PayloadSize)
    160164        bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));
    161165
  • trunk/examples/network/network-chat/chatdialog.cpp

    r651 r769  
    8080    cursor.movePosition(QTextCursor::End);
    8181    QTextTable *table = cursor.insertTable(1, 2, tableFormat);
    82     table->cellAt(0, 0).firstCursorPosition().insertText("<" + from + "> ");
     82    table->cellAt(0, 0).firstCursorPosition().insertText('<' + from + "> ");
    8383    table->cellAt(0, 1).firstCursorPosition().insertText(message);
    8484    QScrollBar *bar = textEdit->verticalScrollBar();
  • trunk/examples/network/network-chat/client.cpp

    r651 r769  
    7070QString Client::nickName() const
    7171{
    72     return QString(peerManager->userName()) + "@" + QHostInfo::localHostName()
    73            + ":" + QString::number(server.serverPort());
     72    return QString(peerManager->userName()) + '@' + QHostInfo::localHostName()
     73           + ':' + QString::number(server.serverPort());
    7474}
    7575
  • trunk/examples/network/network-chat/connection.cpp

    r651 r769  
    8484
    8585    QByteArray msg = message.toUtf8();
    86     QByteArray data = "MESSAGE " + QByteArray::number(msg.size()) + " " + msg;
     86    QByteArray data = "MESSAGE " + QByteArray::number(msg.size()) + ' ' + msg;
    8787    return write(data) == data.size();
    8888}
     
    119119        }
    120120
    121         username = QString(buffer) + "@" + peerAddress().toString() + ":"
     121        username = QString(buffer) + '@' + peerAddress().toString() + ':'
    122122                   + QString::number(peerPort());
    123123        currentDataType = Undefined;
     
    163163{
    164164    QByteArray greeting = greetingMessage.toUtf8();
    165     QByteArray data = "GREETING " + QByteArray::number(greeting.size()) + " " + greeting;
     165    QByteArray data = "GREETING " + QByteArray::number(greeting.size()) + ' ' + greeting;
    166166    if (write(data) == data.size())
    167167        isGreetingMessageSent = true;
  • trunk/examples/network/network-chat/network-chat.pro

    r561 r769  
    2222    include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
    2323    HEADERS += $$QT_SOURCE_TREE/examples/network/qftp/sym_iap_util.h
    24     LIBS += -lesock -lconnmon -lcharconv -linsock
     24    LIBS += -lesock -lcommdb -linsock # For IAP selection
     25    LIBS += -lcharconv
    2526    TARGET.CAPABILITY = "NetworkServices ReadUserData WriteUserData"
    2627    TARGET.EPOCHEAPSIZE = 0x20000 0x2000000
  • trunk/examples/network/network-chat/peermanager.cpp

    r651 r769  
    6262        int index = environment.indexOf(QRegExp(string));
    6363        if (index != -1) {
    64             QStringList stringList = environment.at(index).split("=");
     64            QStringList stringList = environment.at(index).split('=');
    6565            if (stringList.size() == 2) {
    6666                username = stringList.at(1).toUtf8();
  • trunk/examples/network/qftp/ftpwindow.cpp

    r651 r769  
    325325        fileList->clear();
    326326        isDirectory.clear();
    327         currentPath += "/" + name;
     327        currentPath += '/';
     328        currentPath += name;
    328329        ftp->cd(name);
    329330        ftp->list();
  • trunk/examples/network/qftp/sym_iap_util.h

    r651 r769  
    6161
    6262_LIT(KIapNameSetting, "IAP\\Name");             // text - mandatory
     63_LIT(KIapTableIdField, "IAP\Id");
    6364_LIT(KIapDialogPref, "IAP\\DialogPref");        // TUnit32 - optional
    6465_LIT(KIapService, "IAP\\IAPService");           // TUnit32 - mandatory
     
    299300    return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
    300301#else
    301     return QString::fromUtf16(aDescriptor.Ptr(), aDescriptor.Length());
     302    return QString((const QChar *)aDescriptor.Ptr(), aDescriptor.Length());
    302303#endif
    303304}
     
    368369
    369370    socketServ.Connect();
     371
     372    TCommDbConnPref prefs;
     373    prefs.SetDialogPreference(ECommDbDialogPrefPrompt);
     374
    370375    connection.Open(socketServ);
    371     connection.Start();
     376    connection.Start(prefs);
    372377
    373378    connection.GetDesSetting(TPtrC(KIapNameSetting), iapName);
    374 
    375379    //connection.Stop();
    376380
     
    379383
    380384    int error = 0;
    381     if(!qt_SetDefaultIapName(strIapName, error)) {
    382         //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
    383         strIapName = QString("");
     385    if(!strIapName.isEmpty()) {
     386        if(!qt_SetDefaultIapName(strIapName, error)) {
     387            //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
     388            strIapName = QString("");
     389        }
    384390    }
    385391
  • trunk/examples/network/securesocketclient/sslclient.cpp

    r651 r769  
    178178{
    179179    QString input = form->sessionInput->text();
    180     appendString(input + "\n");
     180    appendString(input + '\n');
    181181    socket->write(input.toUtf8() + "\r\n");
    182182    form->sessionInput->clear();
  • trunk/examples/network/torrent/trackerclient.cpp

    r651 r769  
    106106    if (fullUrl.contains("?passkey")) {
    107107        passkey = metaInfo.announceUrl().mid(fullUrl.indexOf("?passkey"), -1);
    108         passkey += "&";
     108        passkey += '&';
    109109    }
    110110
Note: See TracChangeset for help on using the changeset viewer.