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