tests/manual/bearerex/bearerex.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "bearerex.h"
    42 #include "bearerex.h"
       
    43 #include "datatransferer.h"
    43 
    44 
    44 #include <QtNetwork>
    45 #include <QtNetwork>
    45 
    46 
    46 Q_DECLARE_METATYPE(QNetworkConfiguration)
    47 Q_DECLARE_METATYPE(QNetworkConfiguration)
    47 
    48 
   259 SessionTab::SessionTab(QNetworkConfiguration* apNetworkConfiguration,
   260 SessionTab::SessionTab(QNetworkConfiguration* apNetworkConfiguration,
   260                        QNetworkConfigurationManager* configManager,
   261                        QNetworkConfigurationManager* configManager,
   261                        QListWidget* eventListWidget,
   262                        QListWidget* eventListWidget,
   262                        int index,
   263                        int index,
   263                        BearerEx * parent)
   264                        BearerEx * parent)
   264     : QWidget(parent), m_http(0), m_eventListWidget(eventListWidget),
   265     : QWidget(parent), m_dataTransferer(0), m_eventListWidget(eventListWidget),
   265      m_index(index), m_httpRequestOngoing(false), m_alrEnabled (false)
   266      m_index(index), m_alrEnabled (false)
   266 {
   267 {
   267     setupUi(this);
   268     setupUi(this);
   268 
   269 
   269     m_ConfigManager = configManager;
   270     m_ConfigManager = configManager;
   270     m_NetworkSession = new QNetworkSession(*apNetworkConfiguration);
   271     m_NetworkSession = new QNetworkSession(*apNetworkConfiguration);
   298     stateLineEdit->setFocusPolicy(Qt::NoFocus);
   299     stateLineEdit->setFocusPolicy(Qt::NoFocus);
   299 }
   300 }
   300 
   301 
   301 SessionTab::~SessionTab()
   302 SessionTab::~SessionTab()
   302 {
   303 {
   303     // Need to be nulled, because modal dialogs may return after destruction of this object and
   304     delete m_NetworkSession; m_NetworkSession = 0;
   304     // use already released resources.
   305     delete m_dataTransferer; m_dataTransferer = 0;
   305     delete m_NetworkSession;
   306 }
   306     m_NetworkSession = NULL;
   307 
   307     delete m_http;
   308 void SessionTab::on_createQNetworkAccessManagerButton_clicked()
   308     m_http = NULL;
   309 {
   309 }
   310     if (m_dataTransferer) {
   310 
   311         disconnect(m_dataTransferer, 0, 0, 0);
   311 void SessionTab::on_createQHttpButton_clicked()
   312         delete m_dataTransferer;
   312 {
   313         m_dataTransferer = 0;
   313     if (m_httpRequestOngoing) {
   314     }
       
   315     // Create new object according to current selection
       
   316     QString type(comboBox->currentText());
       
   317     if (type == "QNAM") {
       
   318         m_dataTransferer = new DataTransfererQNam(this);
       
   319     } else if (type == "QTcpSocket") {
       
   320         m_dataTransferer = new DataTransfererQTcp(this);
       
   321     } else if (type == "QHttp") {
       
   322         m_dataTransferer = new DataTransfererQHttp(this);
       
   323     } else {
       
   324         qDebug("BearerEx Warning, unknown data transfer object requested, not creating anything.");
   314         return;
   325         return;
   315     }
   326     }
   316 
   327     createQNetworkAccessManagerButton->setText("Recreate");
   317     if (m_http) {
   328     connect(m_dataTransferer, SIGNAL(finished(quint32, qint64, QString)), this, SLOT(finished(quint32, qint64, QString)));
   318         disconnect(m_http, 0, 0, 0);
       
   319         delete m_http;
       
   320     }
       
   321     m_http = new QHttp(this);
       
   322     createQHttpButton->setText("Recreate QHttp");
       
   323     connect(m_http, SIGNAL(done(bool)), this, SLOT(done(bool)));    
       
   324 }
   329 }
   325 
   330 
   326 void SessionTab::on_sendRequestButton_clicked()
   331 void SessionTab::on_sendRequestButton_clicked()
   327 {
   332 {
   328     if (m_http) {
   333     if (m_dataTransferer) {
   329         QString urlstring("http://www.google.com");
   334         if (!m_dataTransferer->transferData()) {
   330         QUrl url(urlstring);
   335             QMessageBox msgBox;
   331         m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
   336             msgBox.setStandardButtons(QMessageBox::Close);
   332         m_http->get(urlstring);
   337             msgBox.setText("Data transfer not started. \nVery likely data transfer ongoing.");
   333         m_httpRequestOngoing = true;
   338             msgBox.exec();
       
   339         }
   334     } else {
   340     } else {
   335         QMessageBox msgBox;
   341         QMessageBox msgBox;
   336         msgBox.setStandardButtons(QMessageBox::Close);
   342         msgBox.setStandardButtons(QMessageBox::Close);
   337         msgBox.setText("QHttp not created.\nCreate QHttp First.");
   343         msgBox.setText("Data object not created.\nCreate data object first.");
   338         msgBox.exec();
   344         msgBox.exec();
   339     }
   345     }
   340 }
   346 }
   341 
   347 
   342 void SessionTab::on_openSessionButton_clicked()
   348 void SessionTab::on_openSessionButton_clicked()
   417     font.setBold(true);
   423     font.setBold(true);
   418     listItem->setFont(font);        
   424     listItem->setFont(font);        
   419     listItem->setText(QString("S")+QString::number(m_index)+QString(" - ")+QString("Opened"));
   425     listItem->setText(QString("S")+QString::number(m_index)+QString(" - ")+QString("Opened"));
   420     m_eventListWidget->addItem(listItem);
   426     m_eventListWidget->addItem(listItem);
   421     
   427     
   422     QVariant identifier = m_NetworkSession->property("ActiveConfiguration");
   428     QVariant identifier = m_NetworkSession->sessionProperty("ActiveConfiguration");
   423     if (!identifier.isNull()) {
   429     if (!identifier.isNull()) {
   424         QString configId = identifier.toString();
   430         QString configId = identifier.toString();
   425         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   431         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   426         if (config.isValid()) {
   432         if (config.isValid()) {
   427             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   433             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   428         }
   434         }
   429     }
   435     }
   430 
   436 
   431     if (m_NetworkSession->configuration().type() == QNetworkConfiguration::UserChoice) {
   437     if (m_NetworkSession->configuration().type() == QNetworkConfiguration::UserChoice) {
   432         QVariant identifier = m_NetworkSession->property("UserChoiceConfiguration");
   438         QVariant identifier = m_NetworkSession->sessionProperty("UserChoiceConfiguration");
   433         if (!identifier.isNull()) {
   439         if (!identifier.isNull()) {
   434             QString configId = identifier.toString();
   440             QString configId = identifier.toString();
   435             QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   441             QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   436             if (config.isValid() && (config.type() == QNetworkConfiguration::ServiceNetwork)) {
   442             if (config.isValid() && (config.type() == QNetworkConfiguration::ServiceNetwork)) {
   437                 snapLineEdit->setText(config.name());
   443                 snapLineEdit->setText(config.name());
   478             break;
   484             break;
   479     }
   485     }
   480     return stateString;
   486     return stateString;
   481 }
   487 }
   482 
   488 
       
   489 void SessionTab::on_dataObjectChanged(const QString &newObjectType)
       
   490 {
       
   491     qDebug() << "BearerEx SessionTab dataObjectChanged to: " << newObjectType;
       
   492     if (m_dataTransferer) {
       
   493         disconnect(m_dataTransferer, 0, 0, 0);
       
   494         delete m_dataTransferer; m_dataTransferer = 0;
       
   495         qDebug() << "BearerEx SessionTab, previous data object deleted.";
       
   496     }
       
   497     createQNetworkAccessManagerButton->setText("Create");
       
   498 }
       
   499 
       
   500 
   483 void SessionTab::stateChanged(QNetworkSession::State state)    
   501 void SessionTab::stateChanged(QNetworkSession::State state)    
   484 {
   502 {
   485     newState(state);
   503     newState(state);
   486     
   504     
   487     QListWidgetItem* listItem = new QListWidgetItem();
   505     QListWidgetItem* listItem = new QListWidgetItem();
   489     m_eventListWidget->addItem(listItem);
   507     m_eventListWidget->addItem(listItem);
   490 }
   508 }
   491 
   509 
   492 void SessionTab::newState(QNetworkSession::State state)
   510 void SessionTab::newState(QNetworkSession::State state)
   493 {
   511 {
   494     QVariant identifier = m_NetworkSession->property("ActiveConfiguration");
   512     QVariant identifier = m_NetworkSession->sessionProperty("ActiveConfiguration");
   495     if (state == QNetworkSession::Connected && !identifier.isNull()) {
   513     if (state == QNetworkSession::Connected && !identifier.isNull()) {
   496         QString configId = identifier.toString();
   514         QString configId = identifier.toString();
   497         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   515         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   498         if (config.isValid()) {
   516         if (config.isValid()) {
   499             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   517             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   540     
   558     
   541     msgBox.setText(errorString);
   559     msgBox.setText(errorString);
   542     msgBox.exec();
   560     msgBox.exec();
   543 }
   561 }
   544 
   562 
   545 void SessionTab::done(bool error)
   563 void SessionTab::finished(quint32 errorCode, qint64 dataReceived, QString errorType)
   546 {
   564 {
   547     m_httpRequestOngoing = false;
       
   548 
       
   549     QMessageBox msgBox;
   565     QMessageBox msgBox;
   550     msgBox.setStandardButtons(QMessageBox::Close);
   566     msgBox.setStandardButtons(QMessageBox::Close);
   551     if (error) {
   567     msgBox.setText(QString("Data transfer completed. \nError code: ") +
   552         msgBox.setText("HTTP request failed.");
   568                    QString::number(int(errorCode)) +
   553     } else {
   569                    "\nError type: " + errorType +
   554         QString result(m_http->readAll());
   570                    "\nBytes received: " +
   555         msgBox.setText(QString("HTTP request finished successfully.\nReceived ")+QString::number(result.length())+QString(" bytes."));
   571                    QString::number(dataReceived));
   556     }
       
   557     msgBox.exec();
   572     msgBox.exec();
   558     // Check if the networksession still exists - it may have gone after returning from
   573     // Check if the networksession still exists - it may have gone after returning from
   559     // the modal dialog (in the case that app has been closed, and deleting QHttp will
   574     // the modal dialog (in the case that app has been closed, and deleting QHttp will
   560     // trigger the done() invokation).
   575     // trigger the done() invokation).
   561     if (m_NetworkSession) {
   576     if (m_NetworkSession) {