| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 | #include <QtNetwork>
|
|---|
| 44 |
|
|---|
| 45 | #include "client.h"
|
|---|
| 46 |
|
|---|
| 47 | //! [0]
|
|---|
| 48 | Client::Client(QWidget *parent)
|
|---|
| 49 | : QDialog(parent)
|
|---|
| 50 | {
|
|---|
| 51 | //! [0]
|
|---|
| 52 | hostLabel = new QLabel(tr("&Server name:"));
|
|---|
| 53 | portLabel = new QLabel(tr("S&erver port:"));
|
|---|
| 54 |
|
|---|
| 55 | hostLineEdit = new QLineEdit("Localhost");
|
|---|
| 56 | portLineEdit = new QLineEdit;
|
|---|
| 57 | portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
|---|
| 58 |
|
|---|
| 59 | hostLabel->setBuddy(hostLineEdit);
|
|---|
| 60 | portLabel->setBuddy(portLineEdit);
|
|---|
| 61 |
|
|---|
| 62 | statusLabel = new QLabel(tr("This examples requires that you run the "
|
|---|
| 63 | "Fortune Server example as well."));
|
|---|
| 64 |
|
|---|
| 65 | getFortuneButton = new QPushButton(tr("Get Fortune"));
|
|---|
| 66 | getFortuneButton->setDefault(true);
|
|---|
| 67 | getFortuneButton->setEnabled(false);
|
|---|
| 68 |
|
|---|
| 69 | quitButton = new QPushButton(tr("Quit"));
|
|---|
| 70 |
|
|---|
| 71 | buttonBox = new QDialogButtonBox;
|
|---|
| 72 | buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
|---|
| 73 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
|---|
| 74 |
|
|---|
| 75 | //! [1]
|
|---|
| 76 | tcpSocket = new QTcpSocket(this);
|
|---|
| 77 | //! [1]
|
|---|
| 78 |
|
|---|
| 79 | connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
|
|---|
| 80 | this, SLOT(enableGetFortuneButton()));
|
|---|
| 81 | connect(portLineEdit, SIGNAL(textChanged(const QString &)),
|
|---|
| 82 | this, SLOT(enableGetFortuneButton()));
|
|---|
| 83 | connect(getFortuneButton, SIGNAL(clicked()),
|
|---|
| 84 | this, SLOT(requestNewFortune()));
|
|---|
| 85 | connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
|---|
| 86 | //! [2] //! [3]
|
|---|
| 87 | connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
|
|---|
| 88 | //! [2] //! [4]
|
|---|
| 89 | connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
|
|---|
| 90 | //! [3]
|
|---|
| 91 | this, SLOT(displayError(QAbstractSocket::SocketError)));
|
|---|
| 92 | //! [4]
|
|---|
| 93 |
|
|---|
| 94 | QGridLayout *mainLayout = new QGridLayout;
|
|---|
| 95 | mainLayout->addWidget(hostLabel, 0, 0);
|
|---|
| 96 | mainLayout->addWidget(hostLineEdit, 0, 1);
|
|---|
| 97 | mainLayout->addWidget(portLabel, 1, 0);
|
|---|
| 98 | mainLayout->addWidget(portLineEdit, 1, 1);
|
|---|
| 99 | mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
|
|---|
| 100 | mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
|
|---|
| 101 | setLayout(mainLayout);
|
|---|
| 102 |
|
|---|
| 103 | setWindowTitle(tr("Fortune Client"));
|
|---|
| 104 | portLineEdit->setFocus();
|
|---|
| 105 | //! [5]
|
|---|
| 106 | }
|
|---|
| 107 | //! [5]
|
|---|
| 108 |
|
|---|
| 109 | //! [6]
|
|---|
| 110 | void Client::requestNewFortune()
|
|---|
| 111 | {
|
|---|
| 112 | getFortuneButton->setEnabled(false);
|
|---|
| 113 | blockSize = 0;
|
|---|
| 114 | tcpSocket->abort();
|
|---|
| 115 | //! [7]
|
|---|
| 116 | tcpSocket->connectToHost(hostLineEdit->text(),
|
|---|
| 117 | portLineEdit->text().toInt());
|
|---|
| 118 | //! [7]
|
|---|
| 119 | }
|
|---|
| 120 | //! [6]
|
|---|
| 121 |
|
|---|
| 122 | //! [8]
|
|---|
| 123 | void Client::readFortune()
|
|---|
| 124 | {
|
|---|
| 125 | //! [9]
|
|---|
| 126 | QDataStream in(tcpSocket);
|
|---|
| 127 | in.setVersion(QDataStream::Qt_4_0);
|
|---|
| 128 |
|
|---|
| 129 | if (blockSize == 0) {
|
|---|
| 130 | if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
|
|---|
| 131 | return;
|
|---|
| 132 | //! [8]
|
|---|
| 133 |
|
|---|
| 134 | //! [10]
|
|---|
| 135 | in >> blockSize;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (tcpSocket->bytesAvailable() < blockSize)
|
|---|
| 139 | return;
|
|---|
| 140 | //! [10] //! [11]
|
|---|
| 141 |
|
|---|
| 142 | QString nextFortune;
|
|---|
| 143 | in >> nextFortune;
|
|---|
| 144 |
|
|---|
| 145 | if (nextFortune == currentFortune) {
|
|---|
| 146 | QTimer::singleShot(0, this, SLOT(requestNewFortune()));
|
|---|
| 147 | return;
|
|---|
| 148 | }
|
|---|
| 149 | //! [11]
|
|---|
| 150 |
|
|---|
| 151 | //! [12]
|
|---|
| 152 | currentFortune = nextFortune;
|
|---|
| 153 | //! [9]
|
|---|
| 154 | statusLabel->setText(currentFortune);
|
|---|
| 155 | getFortuneButton->setEnabled(true);
|
|---|
| 156 | }
|
|---|
| 157 | //! [12]
|
|---|
| 158 |
|
|---|
| 159 | //! [13]
|
|---|
| 160 | void Client::displayError(QAbstractSocket::SocketError socketError)
|
|---|
| 161 | {
|
|---|
| 162 | switch (socketError) {
|
|---|
| 163 | case QAbstractSocket::RemoteHostClosedError:
|
|---|
| 164 | break;
|
|---|
| 165 | case QAbstractSocket::HostNotFoundError:
|
|---|
| 166 | QMessageBox::information(this, tr("Fortune Client"),
|
|---|
| 167 | tr("The host was not found. Please check the "
|
|---|
| 168 | "host name and port settings."));
|
|---|
| 169 | break;
|
|---|
| 170 | case QAbstractSocket::ConnectionRefusedError:
|
|---|
| 171 | QMessageBox::information(this, tr("Fortune Client"),
|
|---|
| 172 | tr("The connection was refused by the peer. "
|
|---|
| 173 | "Make sure the fortune server is running, "
|
|---|
| 174 | "and check that the host name and port "
|
|---|
| 175 | "settings are correct."));
|
|---|
| 176 | break;
|
|---|
| 177 | default:
|
|---|
| 178 | QMessageBox::information(this, tr("Fortune Client"),
|
|---|
| 179 | tr("The following error occurred: %1.")
|
|---|
| 180 | .arg(tcpSocket->errorString()));
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | getFortuneButton->setEnabled(true);
|
|---|
| 184 | }
|
|---|
| 185 | //! [13]
|
|---|
| 186 |
|
|---|
| 187 | void Client::enableGetFortuneButton()
|
|---|
| 188 | {
|
|---|
| 189 | getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
|
|---|
| 190 | && !portLineEdit->text().isEmpty());
|
|---|
| 191 | }
|
|---|