source: trunk/examples/network/fortuneclient/client.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 7.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <QtNetwork>
44
45#include "client.h"
46
47#ifdef Q_OS_SYMBIAN
48#include "sym_iap_util.h"
49#endif
50
51//! [0]
52Client::Client(QWidget *parent)
53 : QDialog(parent)
54{
55//! [0]
56 hostLabel = new QLabel(tr("&Server name:"));
57 portLabel = new QLabel(tr("S&erver port:"));
58
59 // find out which IP to connect to
60 QString ipAddress;
61 QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
62 // use the first non-localhost IPv4 address
63 for (int i = 0; i < ipAddressesList.size(); ++i) {
64 if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
65 ipAddressesList.at(i).toIPv4Address()) {
66 ipAddress = ipAddressesList.at(i).toString();
67 break;
68 }
69 }
70 // if we did not find one, use IPv4 localhost
71 if (ipAddress.isEmpty())
72 ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
73
74 hostLineEdit = new QLineEdit(ipAddress);
75 portLineEdit = new QLineEdit;
76 portLineEdit->setValidator(new QIntValidator(1, 65535, this));
77
78 hostLabel->setBuddy(hostLineEdit);
79 portLabel->setBuddy(portLineEdit);
80
81 statusLabel = new QLabel(tr("This examples requires that you run the "
82 "Fortune Server example as well."));
83
84 getFortuneButton = new QPushButton(tr("Get Fortune"));
85 getFortuneButton->setDefault(true);
86 getFortuneButton->setEnabled(false);
87
88 quitButton = new QPushButton(tr("Quit"));
89
90 buttonBox = new QDialogButtonBox;
91 buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
92 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
93
94//! [1]
95 tcpSocket = new QTcpSocket(this);
96//! [1]
97
98 connect(hostLineEdit, SIGNAL(textChanged(QString)),
99 this, SLOT(enableGetFortuneButton()));
100 connect(portLineEdit, SIGNAL(textChanged(QString)),
101 this, SLOT(enableGetFortuneButton()));
102 connect(getFortuneButton, SIGNAL(clicked()),
103 this, SLOT(requestNewFortune()));
104 connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
105//! [2] //! [3]
106 connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
107//! [2] //! [4]
108 connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
109//! [3]
110 this, SLOT(displayError(QAbstractSocket::SocketError)));
111//! [4]
112
113 QGridLayout *mainLayout = new QGridLayout;
114 mainLayout->addWidget(hostLabel, 0, 0);
115 mainLayout->addWidget(hostLineEdit, 0, 1);
116 mainLayout->addWidget(portLabel, 1, 0);
117 mainLayout->addWidget(portLineEdit, 1, 1);
118 mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
119 mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
120 setLayout(mainLayout);
121
122 setWindowTitle(tr("Fortune Client"));
123 portLineEdit->setFocus();
124
125#ifdef Q_OS_SYMBIAN
126 isDefaultIapSet = false;
127#endif
128//! [5]
129}
130//! [5]
131
132//! [6]
133void Client::requestNewFortune()
134{
135 getFortuneButton->setEnabled(false);
136#ifdef Q_OS_SYMBIAN
137 if(!isDefaultIapSet) {
138 qt_SetDefaultIap();
139 isDefaultIapSet = true;
140 }
141#endif
142 blockSize = 0;
143 tcpSocket->abort();
144//! [7]
145 tcpSocket->connectToHost(hostLineEdit->text(),
146 portLineEdit->text().toInt());
147//! [7]
148}
149//! [6]
150
151//! [8]
152void Client::readFortune()
153{
154//! [9]
155 QDataStream in(tcpSocket);
156 in.setVersion(QDataStream::Qt_4_0);
157
158 if (blockSize == 0) {
159 if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
160 return;
161//! [8]
162
163//! [10]
164 in >> blockSize;
165 }
166
167 if (tcpSocket->bytesAvailable() < blockSize)
168 return;
169//! [10] //! [11]
170
171 QString nextFortune;
172 in >> nextFortune;
173
174 if (nextFortune == currentFortune) {
175 QTimer::singleShot(0, this, SLOT(requestNewFortune()));
176 return;
177 }
178//! [11]
179
180//! [12]
181 currentFortune = nextFortune;
182//! [9]
183 statusLabel->setText(currentFortune);
184 getFortuneButton->setEnabled(true);
185}
186//! [12]
187
188//! [13]
189void Client::displayError(QAbstractSocket::SocketError socketError)
190{
191 switch (socketError) {
192 case QAbstractSocket::RemoteHostClosedError:
193 break;
194 case QAbstractSocket::HostNotFoundError:
195 QMessageBox::information(this, tr("Fortune Client"),
196 tr("The host was not found. Please check the "
197 "host name and port settings."));
198 break;
199 case QAbstractSocket::ConnectionRefusedError:
200 QMessageBox::information(this, tr("Fortune Client"),
201 tr("The connection was refused by the peer. "
202 "Make sure the fortune server is running, "
203 "and check that the host name and port "
204 "settings are correct."));
205 break;
206 default:
207 QMessageBox::information(this, tr("Fortune Client"),
208 tr("The following error occurred: %1.")
209 .arg(tcpSocket->errorString()));
210 }
211
212 getFortuneButton->setEnabled(true);
213}
214//! [13]
215
216void Client::enableGetFortuneButton()
217{
218 getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
219 && !portLineEdit->text().isEmpty());
220}
Note: See TracBrowser for help on using the repository browser.