examples/network/fortuneclient/client.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    41 #include <QtGui>
    41 #include <QtGui>
    42 #include <QtNetwork>
    42 #include <QtNetwork>
    43 
    43 
    44 #include "client.h"
    44 #include "client.h"
    45 
    45 
    46 #ifdef Q_OS_SYMBIAN
       
    47 #include "sym_iap_util.h"
       
    48 #endif
       
    49 
       
    50 //! [0]
    46 //! [0]
    51 Client::Client(QWidget *parent)
    47 Client::Client(QWidget *parent)
    52     : QDialog(parent)
    48 :   QDialog(parent), networkSession(0)
    53 {
    49 {
    54 //! [0]
    50 //! [0]
    55     hostLabel = new QLabel(tr("&Server name:"));
    51     hostLabel = new QLabel(tr("&Server name:"));
    56     portLabel = new QLabel(tr("S&erver port:"));
    52     portLabel = new QLabel(tr("S&erver port:"));
    57 
    53 
   119     setLayout(mainLayout);
   115     setLayout(mainLayout);
   120 
   116 
   121     setWindowTitle(tr("Fortune Client"));
   117     setWindowTitle(tr("Fortune Client"));
   122     portLineEdit->setFocus();
   118     portLineEdit->setFocus();
   123 
   119 
   124 #ifdef Q_OS_SYMBIAN
   120     QNetworkConfigurationManager manager;
   125     isDefaultIapSet = false;
   121     if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
   126 #endif
   122         // Get saved network configuration
       
   123         QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
       
   124         settings.beginGroup(QLatin1String("QtNetwork"));
       
   125         const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
       
   126         settings.endGroup();
       
   127 
       
   128         // If the saved network configuration is not currently discovered use the system default
       
   129         QNetworkConfiguration config = manager.configurationFromIdentifier(id);
       
   130         if ((config.state() & QNetworkConfiguration::Discovered) !=
       
   131             QNetworkConfiguration::Discovered) {
       
   132             config = manager.defaultConfiguration();
       
   133         }
       
   134 
       
   135         networkSession = new QNetworkSession(config, this);
       
   136         connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
       
   137 
       
   138         getFortuneButton->setEnabled(false);
       
   139         statusLabel->setText(tr("Opening network session."));
       
   140         networkSession->open();
       
   141     }
   127 //! [5]
   142 //! [5]
   128 }
   143 }
   129 //! [5]
   144 //! [5]
   130 
   145 
   131 //! [6]
   146 //! [6]
   132 void Client::requestNewFortune()
   147 void Client::requestNewFortune()
   133 {
   148 {
   134     getFortuneButton->setEnabled(false);
   149     getFortuneButton->setEnabled(false);
   135 #ifdef Q_OS_SYMBIAN
       
   136     if(!isDefaultIapSet) {
       
   137         qt_SetDefaultIap();
       
   138         isDefaultIapSet = true;
       
   139     }
       
   140 #endif
       
   141     blockSize = 0;
   150     blockSize = 0;
   142     tcpSocket->abort();
   151     tcpSocket->abort();
   143 //! [7]
   152 //! [7]
   144     tcpSocket->connectToHost(hostLineEdit->text(),
   153     tcpSocket->connectToHost(hostLineEdit->text(),
   145                              portLineEdit->text().toInt());
   154                              portLineEdit->text().toInt());
   212 }
   221 }
   213 //! [13]
   222 //! [13]
   214 
   223 
   215 void Client::enableGetFortuneButton()
   224 void Client::enableGetFortuneButton()
   216 {
   225 {
   217     getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
   226     getFortuneButton->setEnabled((!networkSession || networkSession->isOpen()) &&
   218                                  && !portLineEdit->text().isEmpty());
   227                                  !hostLineEdit->text().isEmpty() &&
   219 }
   228                                  !portLineEdit->text().isEmpty());
       
   229 
       
   230 }
       
   231 
       
   232 void Client::sessionOpened()
       
   233 {
       
   234     // Save the used configuration
       
   235     QNetworkConfiguration config = networkSession->configuration();
       
   236     QString id;
       
   237     if (config.type() == QNetworkConfiguration::UserChoice)
       
   238         id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
       
   239     else
       
   240         id = config.identifier();
       
   241 
       
   242     QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
       
   243     settings.beginGroup(QLatin1String("QtNetwork"));
       
   244     settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
       
   245     settings.endGroup();
       
   246 
       
   247     statusLabel->setText(tr("This examples requires that you run the "
       
   248                             "Fortune Server example as well."));
       
   249 
       
   250     enableGetFortuneButton();
       
   251 }
       
   252