qtmobility/tests/bearerex/bearerex.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
    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     // Need to be nulled, because modal dialogs may return after destruction of this object and
   303     delete m_NetworkSession; m_NetworkSession = 0;
   303     // use already released resources.
   304     delete m_dataTransferer; m_dataTransferer = 0;
   304     delete m_NetworkSession;
   305 }
   305     m_NetworkSession = NULL;
   306 
   306     delete m_http;
   307 void SessionTab::on_createQNetworkAccessManagerButton_clicked()
   307     m_http = NULL;
   308 {
   308 }
   309     if (m_dataTransferer) {
   309 
   310         disconnect(m_dataTransferer, 0, 0, 0);
   310 void SessionTab::on_createQHttpButton_clicked()
   311         delete m_dataTransferer;
   311 {
   312         m_dataTransferer = 0;
   312     if (m_httpRequestOngoing) {
   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.");
   313         return;
   324         return;
   314     }
   325     }
   315 
   326     createQNetworkAccessManagerButton->setText("Recreate");
   316     if (m_http) {
   327     connect(m_dataTransferer, SIGNAL(finished(quint32, qint64, QString)), this, SLOT(finished(quint32, qint64, QString)));
   317         disconnect(m_http, 0, 0, 0);
       
   318         delete m_http;
       
   319     }
       
   320     m_http = new QHttp(this);
       
   321     createQHttpButton->setText("Recreate QHttp");
       
   322     connect(m_http, SIGNAL(done(bool)), this, SLOT(done(bool)));    
       
   323 }
   328 }
   324 
   329 
   325 void SessionTab::on_sendRequestButton_clicked()
   330 void SessionTab::on_sendRequestButton_clicked()
   326 {
   331 {
   327     if (m_http) {
   332     if (m_dataTransferer) {
   328         QString urlstring("http://www.google.com");
   333         if (!m_dataTransferer->transferData()) {
   329         QUrl url(urlstring);
   334             QMessageBox msgBox;
   330         m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
   335             msgBox.setStandardButtons(QMessageBox::Close);
   331         m_http->get(urlstring);
   336             msgBox.setText("Data transfer not started. \nVery likely data transfer ongoing.");
   332         m_httpRequestOngoing = true;
   337             msgBox.exec();
       
   338         }
   333     } else {
   339     } else {
   334         QMessageBox msgBox;
   340         QMessageBox msgBox;
   335         msgBox.setStandardButtons(QMessageBox::Close);
   341         msgBox.setStandardButtons(QMessageBox::Close);
   336         msgBox.setText("QHttp not created.\nCreate QHttp First.");
   342         msgBox.setText("Data object not created.\nCreate data object first.");
   337         msgBox.exec();
   343         msgBox.exec();
   338     }
   344     }
   339 }
   345 }
   340 
   346 
   341 void SessionTab::on_openSessionButton_clicked()
   347 void SessionTab::on_openSessionButton_clicked()
   416     font.setBold(true);
   422     font.setBold(true);
   417     listItem->setFont(font);        
   423     listItem->setFont(font);        
   418     listItem->setText(QString("S")+QString::number(m_index)+QString(" - ")+QString("Opened"));
   424     listItem->setText(QString("S")+QString::number(m_index)+QString(" - ")+QString("Opened"));
   419     m_eventListWidget->addItem(listItem);
   425     m_eventListWidget->addItem(listItem);
   420     
   426     
   421     QVariant identifier = m_NetworkSession->property("ActiveConfiguration");
   427     QVariant identifier = m_NetworkSession->sessionProperty("ActiveConfiguration");
   422     if (!identifier.isNull()) {
   428     if (!identifier.isNull()) {
   423         QString configId = identifier.toString();
   429         QString configId = identifier.toString();
   424         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   430         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   425         if (config.isValid()) {
   431         if (config.isValid()) {
   426             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   432             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   427         }
   433         }
   428     }
   434     }
   429 
   435 
   430     if (m_NetworkSession->configuration().type() == QNetworkConfiguration::UserChoice) {
   436     if (m_NetworkSession->configuration().type() == QNetworkConfiguration::UserChoice) {
   431         QVariant identifier = m_NetworkSession->property("UserChoiceConfiguration");
   437         QVariant identifier = m_NetworkSession->sessionProperty("UserChoiceConfiguration");
   432         if (!identifier.isNull()) {
   438         if (!identifier.isNull()) {
   433             QString configId = identifier.toString();
   439             QString configId = identifier.toString();
   434             QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   440             QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   435             if (config.isValid() && (config.type() == QNetworkConfiguration::ServiceNetwork)) {
   441             if (config.isValid() && (config.type() == QNetworkConfiguration::ServiceNetwork)) {
   436                 snapLineEdit->setText(config.name());
   442                 snapLineEdit->setText(config.name());
   477             break;
   483             break;
   478     }
   484     }
   479     return stateString;
   485     return stateString;
   480 }
   486 }
   481 
   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 
   482 void SessionTab::stateChanged(QNetworkSession::State state)    
   500 void SessionTab::stateChanged(QNetworkSession::State state)    
   483 {
   501 {
   484     newState(state);
   502     newState(state);
   485     
   503     
   486     QListWidgetItem* listItem = new QListWidgetItem();
   504     QListWidgetItem* listItem = new QListWidgetItem();
   488     m_eventListWidget->addItem(listItem);
   506     m_eventListWidget->addItem(listItem);
   489 }
   507 }
   490 
   508 
   491 void SessionTab::newState(QNetworkSession::State state)
   509 void SessionTab::newState(QNetworkSession::State state)
   492 {
   510 {
   493     QVariant identifier = m_NetworkSession->property("ActiveConfiguration");
   511     QVariant identifier = m_NetworkSession->sessionProperty("ActiveConfiguration");
   494     if (state == QNetworkSession::Connected && !identifier.isNull()) {
   512     if (state == QNetworkSession::Connected && !identifier.isNull()) {
   495         QString configId = identifier.toString();
   513         QString configId = identifier.toString();
   496         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   514         QNetworkConfiguration config = m_ConfigManager->configurationFromIdentifier(configId);
   497         if (config.isValid()) {
   515         if (config.isValid()) {
   498             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   516             iapLineEdit->setText(config.name()+" ("+config.identifier()+")");
   539     
   557     
   540     msgBox.setText(errorString);
   558     msgBox.setText(errorString);
   541     msgBox.exec();
   559     msgBox.exec();
   542 }
   560 }
   543 
   561 
   544 void SessionTab::done(bool error)
   562 void SessionTab::finished(quint32 errorCode, qint64 dataReceived, QString errorType)
   545 {
   563 {
   546     m_httpRequestOngoing = false;
       
   547 
       
   548     QMessageBox msgBox;
   564     QMessageBox msgBox;
   549     msgBox.setStandardButtons(QMessageBox::Close);
   565     msgBox.setStandardButtons(QMessageBox::Close);
   550     if (error) {
   566     msgBox.setText(QString("Data transfer completed. \nError code: ") + QString::number((int)errorCode) +
   551         msgBox.setText("HTTP request failed.");
   567                    "\nError type: " + errorType +
   552     } else {
   568                    "\nBytes received: " +
   553         QString result(m_http->readAll());
   569                    QString::number(dataReceived));
   554         msgBox.setText(QString("HTTP request finished successfully.\nReceived ")+QString::number(result.length())+QString(" bytes."));
       
   555     }
       
   556     msgBox.exec();
   570     msgBox.exec();
   557     // Check if the networksession still exists - it may have gone after returning from
   571     // Check if the networksession still exists - it may have gone after returning from
   558     // the modal dialog (in the case that app has been closed, and deleting QHttp will
   572     // the modal dialog (in the case that app has been closed, and deleting QHttp will
   559     // trigger the done() invokation).
   573     // trigger the done() invokation).
   560     if (m_NetworkSession) {
   574     if (m_NetworkSession) {