examples/network/fortuneserver/server.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    44 #include <stdlib.h>
    44 #include <stdlib.h>
    45 
    45 
    46 #include "server.h"
    46 #include "server.h"
    47 
    47 
    48 Server::Server(QWidget *parent)
    48 Server::Server(QWidget *parent)
    49     : QDialog(parent)
    49 :   QDialog(parent), tcpServer(0), networkSession(0)
    50 {
    50 {
    51     statusLabel = new QLabel;
    51     statusLabel = new QLabel;
    52     quitButton = new QPushButton(tr("Quit"));
    52     quitButton = new QPushButton(tr("Quit"));
    53     quitButton->setAutoDefault(false);
    53     quitButton->setAutoDefault(false);
       
    54 
       
    55     QNetworkConfigurationManager manager;
       
    56     if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
       
    57         // Get saved network configuration
       
    58         QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
       
    59         settings.beginGroup(QLatin1String("QtNetwork"));
       
    60         const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
       
    61         settings.endGroup();
       
    62 
       
    63         // If the saved network configuration is not currently discovered use the system default
       
    64         QNetworkConfiguration config = manager.configurationFromIdentifier(id);
       
    65         if ((config.state() & QNetworkConfiguration::Discovered) !=
       
    66             QNetworkConfiguration::Discovered) {
       
    67             config = manager.defaultConfiguration();
       
    68         }
       
    69 
       
    70         networkSession = new QNetworkSession(config, this);
       
    71         connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
       
    72 
       
    73         statusLabel->setText(tr("Opening network session."));
       
    74         networkSession->open();
       
    75     } else {
       
    76         sessionOpened();
       
    77     }
       
    78 
       
    79     //! [2]
       
    80         fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
       
    81                  << tr("You've got to think about tomorrow.")
       
    82                  << tr("You will be surprised by a loud noise.")
       
    83                  << tr("You will feel hungry again in another hour.")
       
    84                  << tr("You might have mail.")
       
    85                  << tr("You cannot kill time without injuring eternity.")
       
    86                  << tr("Computers are not intelligent. They only think they are.");
       
    87     //! [2]
       
    88 
       
    89         connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
       
    90     //! [3]
       
    91         connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
       
    92     //! [3]
       
    93 
       
    94         QHBoxLayout *buttonLayout = new QHBoxLayout;
       
    95         buttonLayout->addStretch(1);
       
    96         buttonLayout->addWidget(quitButton);
       
    97         buttonLayout->addStretch(1);
       
    98 
       
    99         QVBoxLayout *mainLayout = new QVBoxLayout;
       
   100         mainLayout->addWidget(statusLabel);
       
   101         mainLayout->addLayout(buttonLayout);
       
   102         setLayout(mainLayout);
       
   103 
       
   104         setWindowTitle(tr("Fortune Server"));
       
   105 }
       
   106 
       
   107 void Server::sessionOpened()
       
   108 {
       
   109     // Save the used configuration
       
   110     QNetworkConfiguration config = networkSession->configuration();
       
   111     QString id;
       
   112     if (config.type() == QNetworkConfiguration::UserChoice)
       
   113         id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
       
   114     else
       
   115         id = config.identifier();
       
   116 
       
   117     QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
       
   118     settings.beginGroup(QLatin1String("QtNetwork"));
       
   119     settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
       
   120     settings.endGroup();
    54 
   121 
    55 //! [0] //! [1]
   122 //! [0] //! [1]
    56     tcpServer = new QTcpServer(this);
   123     tcpServer = new QTcpServer(this);
    57     if (!tcpServer->listen()) {
   124     if (!tcpServer->listen()) {
    58         QMessageBox::critical(this, tr("Fortune Server"),
   125         QMessageBox::critical(this, tr("Fortune Server"),
    77         ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
   144         ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    78     statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
   145     statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
    79                             "Run the Fortune Client example now.")
   146                             "Run the Fortune Client example now.")
    80                          .arg(ipAddress).arg(tcpServer->serverPort()));
   147                          .arg(ipAddress).arg(tcpServer->serverPort()));
    81 //! [1]
   148 //! [1]
    82 
       
    83 //! [2]
       
    84     fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
       
    85              << tr("You've got to think about tomorrow.")
       
    86              << tr("You will be surprised by a loud noise.")
       
    87              << tr("You will feel hungry again in another hour.")
       
    88              << tr("You might have mail.")
       
    89              << tr("You cannot kill time without injuring eternity.")
       
    90              << tr("Computers are not intelligent. They only think they are.");
       
    91 //! [2]
       
    92 
       
    93     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
       
    94 //! [3]
       
    95     connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
       
    96 //! [3]
       
    97 
       
    98     QHBoxLayout *buttonLayout = new QHBoxLayout;
       
    99     buttonLayout->addStretch(1);
       
   100     buttonLayout->addWidget(quitButton);
       
   101     buttonLayout->addStretch(1);
       
   102 
       
   103     QVBoxLayout *mainLayout = new QVBoxLayout;
       
   104     mainLayout->addWidget(statusLabel);
       
   105     mainLayout->addLayout(buttonLayout);
       
   106     setLayout(mainLayout);
       
   107 
       
   108     setWindowTitle(tr("Fortune Server"));
       
   109 }
   149 }
   110 
   150 
   111 //! [4]
   151 //! [4]
   112 void Server::sendFortune()
   152 void Server::sendFortune()
   113 {
   153 {