util/examples/network/blockingfortuneclient/blockingclient.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include <QtNetwork>
       
    44 
       
    45 #include "blockingclient.h"
       
    46 
       
    47 BlockingClient::BlockingClient(QWidget *parent)
       
    48     : QDialog(parent)
       
    49 {
       
    50     hostLabel = new QLabel(tr("&Server name:"));
       
    51     portLabel = new QLabel(tr("S&erver port:"));
       
    52 
       
    53     // find out which IP to connect to
       
    54     QString ipAddress;
       
    55     QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
       
    56     // use the first non-localhost IPv4 address
       
    57     for (int i = 0; i < ipAddressesList.size(); ++i) {
       
    58         if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
       
    59             ipAddressesList.at(i).toIPv4Address()) {
       
    60             ipAddress = ipAddressesList.at(i).toString();
       
    61             break;
       
    62         }
       
    63     }
       
    64     // if we did not find one, use IPv4 localhost
       
    65     if (ipAddress.isEmpty())
       
    66         ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
       
    67 
       
    68     hostLineEdit = new QLineEdit(ipAddress);
       
    69     portLineEdit = new QLineEdit;
       
    70     portLineEdit->setValidator(new QIntValidator(1, 65535, this));
       
    71 
       
    72     hostLabel->setBuddy(hostLineEdit);
       
    73     portLabel->setBuddy(portLineEdit);
       
    74 
       
    75     statusLabel = new QLabel(tr("This examples requires that you run the "
       
    76                                 "Fortune Server example as well."));
       
    77 
       
    78     getFortuneButton = new QPushButton(tr("Get Fortune"));
       
    79     getFortuneButton->setDefault(true);
       
    80     getFortuneButton->setEnabled(false);
       
    81 
       
    82     quitButton = new QPushButton(tr("Quit"));
       
    83 
       
    84     buttonBox = new QDialogButtonBox;
       
    85     buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
       
    86     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
       
    87 
       
    88     connect(hostLineEdit, SIGNAL(textChanged(QString)),
       
    89             this, SLOT(enableGetFortuneButton()));
       
    90     connect(portLineEdit, SIGNAL(textChanged(QString)),
       
    91             this, SLOT(enableGetFortuneButton()));
       
    92     connect(getFortuneButton, SIGNAL(clicked()),
       
    93             this, SLOT(requestNewFortune()));
       
    94     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
       
    95 //! [0]
       
    96     connect(&thread, SIGNAL(newFortune(QString)),
       
    97             this, SLOT(showFortune(QString)));
       
    98 //! [0] //! [1]
       
    99     connect(&thread, SIGNAL(error(int,QString)),
       
   100             this, SLOT(displayError(int,QString)));
       
   101 //! [1]
       
   102 
       
   103     QGridLayout *mainLayout = new QGridLayout;
       
   104     mainLayout->addWidget(hostLabel, 0, 0);
       
   105     mainLayout->addWidget(hostLineEdit, 0, 1);
       
   106     mainLayout->addWidget(portLabel, 1, 0);
       
   107     mainLayout->addWidget(portLineEdit, 1, 1);
       
   108     mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
       
   109     mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
       
   110     setLayout(mainLayout);
       
   111 
       
   112     setWindowTitle(tr("Blocking Fortune Client"));
       
   113     portLineEdit->setFocus();
       
   114 }
       
   115 
       
   116 //! [2]
       
   117 void BlockingClient::requestNewFortune()
       
   118 {
       
   119     getFortuneButton->setEnabled(false);
       
   120     thread.requestNewFortune(hostLineEdit->text(),
       
   121                              portLineEdit->text().toInt());
       
   122 }
       
   123 //! [2]
       
   124 
       
   125 //! [3]
       
   126 void BlockingClient::showFortune(const QString &nextFortune)
       
   127 {
       
   128     if (nextFortune == currentFortune) {
       
   129         requestNewFortune();
       
   130         return;
       
   131     }
       
   132 //! [3]
       
   133 
       
   134 //! [4]
       
   135     currentFortune = nextFortune;
       
   136     statusLabel->setText(currentFortune);
       
   137     getFortuneButton->setEnabled(true);
       
   138 }
       
   139 //! [4]
       
   140 
       
   141 void BlockingClient::displayError(int socketError, const QString &message)
       
   142 {
       
   143     switch (socketError) {
       
   144     case QAbstractSocket::HostNotFoundError:
       
   145         QMessageBox::information(this, tr("Blocking Fortune Client"),
       
   146                                  tr("The host was not found. Please check the "
       
   147                                     "host and port settings."));
       
   148         break;
       
   149     case QAbstractSocket::ConnectionRefusedError:
       
   150         QMessageBox::information(this, tr("Blocking Fortune Client"),
       
   151                                  tr("The connection was refused by the peer. "
       
   152                                     "Make sure the fortune server is running, "
       
   153                                     "and check that the host name and port "
       
   154                                     "settings are correct."));
       
   155         break;
       
   156     default:
       
   157         QMessageBox::information(this, tr("Blocking Fortune Client"),
       
   158                                  tr("The following error occurred: %1.")
       
   159                                  .arg(message));
       
   160     }
       
   161 
       
   162     getFortuneButton->setEnabled(true);
       
   163 }
       
   164 
       
   165 void BlockingClient::enableGetFortuneButton()
       
   166 {
       
   167     getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
       
   168                                  && !portLineEdit->text().isEmpty());
       
   169 }