1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QtNetwork>
|
---|
43 |
|
---|
44 | #include "client.h"
|
---|
45 |
|
---|
46 | //! [0]
|
---|
47 | Client::Client(QWidget *parent)
|
---|
48 | : QDialog(parent), networkSession(0)
|
---|
49 | {
|
---|
50 | //! [0]
|
---|
51 | hostLabel = new QLabel(tr("&Server name:"));
|
---|
52 | portLabel = new QLabel(tr("S&erver port:"));
|
---|
53 |
|
---|
54 | // find out which IP to connect to
|
---|
55 | QString ipAddress;
|
---|
56 | QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
---|
57 | // use the first non-localhost IPv4 address
|
---|
58 | for (int i = 0; i < ipAddressesList.size(); ++i) {
|
---|
59 | if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
|
---|
60 | ipAddressesList.at(i).toIPv4Address()) {
|
---|
61 | ipAddress = ipAddressesList.at(i).toString();
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | // if we did not find one, use IPv4 localhost
|
---|
66 | if (ipAddress.isEmpty())
|
---|
67 | ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
---|
68 |
|
---|
69 | hostLineEdit = new QLineEdit(ipAddress);
|
---|
70 | portLineEdit = new QLineEdit;
|
---|
71 | portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
---|
72 |
|
---|
73 | hostLabel->setBuddy(hostLineEdit);
|
---|
74 | portLabel->setBuddy(portLineEdit);
|
---|
75 |
|
---|
76 | statusLabel = new QLabel(tr("This examples requires that you run the "
|
---|
77 | "Fortune Server example as well."));
|
---|
78 |
|
---|
79 | getFortuneButton = new QPushButton(tr("Get Fortune"));
|
---|
80 | getFortuneButton->setDefault(true);
|
---|
81 | getFortuneButton->setEnabled(false);
|
---|
82 |
|
---|
83 | quitButton = new QPushButton(tr("Quit"));
|
---|
84 |
|
---|
85 | buttonBox = new QDialogButtonBox;
|
---|
86 | buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
---|
87 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
---|
88 |
|
---|
89 | //! [1]
|
---|
90 | tcpSocket = new QTcpSocket(this);
|
---|
91 | //! [1]
|
---|
92 |
|
---|
93 | connect(hostLineEdit, SIGNAL(textChanged(QString)),
|
---|
94 | this, SLOT(enableGetFortuneButton()));
|
---|
95 | connect(portLineEdit, SIGNAL(textChanged(QString)),
|
---|
96 | this, SLOT(enableGetFortuneButton()));
|
---|
97 | connect(getFortuneButton, SIGNAL(clicked()),
|
---|
98 | this, SLOT(requestNewFortune()));
|
---|
99 | connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
---|
100 | //! [2] //! [3]
|
---|
101 | connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
|
---|
102 | //! [2] //! [4]
|
---|
103 | connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
|
---|
104 | //! [3]
|
---|
105 | this, SLOT(displayError(QAbstractSocket::SocketError)));
|
---|
106 | //! [4]
|
---|
107 |
|
---|
108 | QGridLayout *mainLayout = new QGridLayout;
|
---|
109 | mainLayout->addWidget(hostLabel, 0, 0);
|
---|
110 | mainLayout->addWidget(hostLineEdit, 0, 1);
|
---|
111 | mainLayout->addWidget(portLabel, 1, 0);
|
---|
112 | mainLayout->addWidget(portLineEdit, 1, 1);
|
---|
113 | mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
|
---|
114 | mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
|
---|
115 | setLayout(mainLayout);
|
---|
116 |
|
---|
117 | setWindowTitle(tr("Fortune Client"));
|
---|
118 | portLineEdit->setFocus();
|
---|
119 |
|
---|
120 | QNetworkConfigurationManager manager;
|
---|
121 | if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
|
---|
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 | }
|
---|
142 | //! [5]
|
---|
143 | }
|
---|
144 | //! [5]
|
---|
145 |
|
---|
146 | //! [6]
|
---|
147 | void Client::requestNewFortune()
|
---|
148 | {
|
---|
149 | getFortuneButton->setEnabled(false);
|
---|
150 | blockSize = 0;
|
---|
151 | tcpSocket->abort();
|
---|
152 | //! [7]
|
---|
153 | tcpSocket->connectToHost(hostLineEdit->text(),
|
---|
154 | portLineEdit->text().toInt());
|
---|
155 | //! [7]
|
---|
156 | }
|
---|
157 | //! [6]
|
---|
158 |
|
---|
159 | //! [8]
|
---|
160 | void Client::readFortune()
|
---|
161 | {
|
---|
162 | //! [9]
|
---|
163 | QDataStream in(tcpSocket);
|
---|
164 | in.setVersion(QDataStream::Qt_4_0);
|
---|
165 |
|
---|
166 | if (blockSize == 0) {
|
---|
167 | if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
|
---|
168 | return;
|
---|
169 | //! [8]
|
---|
170 |
|
---|
171 | //! [10]
|
---|
172 | in >> blockSize;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (tcpSocket->bytesAvailable() < blockSize)
|
---|
176 | return;
|
---|
177 | //! [10] //! [11]
|
---|
178 |
|
---|
179 | QString nextFortune;
|
---|
180 | in >> nextFortune;
|
---|
181 |
|
---|
182 | if (nextFortune == currentFortune) {
|
---|
183 | QTimer::singleShot(0, this, SLOT(requestNewFortune()));
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | //! [11]
|
---|
187 |
|
---|
188 | //! [12]
|
---|
189 | currentFortune = nextFortune;
|
---|
190 | //! [9]
|
---|
191 | statusLabel->setText(currentFortune);
|
---|
192 | getFortuneButton->setEnabled(true);
|
---|
193 | }
|
---|
194 | //! [12]
|
---|
195 |
|
---|
196 | //! [13]
|
---|
197 | void Client::displayError(QAbstractSocket::SocketError socketError)
|
---|
198 | {
|
---|
199 | switch (socketError) {
|
---|
200 | case QAbstractSocket::RemoteHostClosedError:
|
---|
201 | break;
|
---|
202 | case QAbstractSocket::HostNotFoundError:
|
---|
203 | QMessageBox::information(this, tr("Fortune Client"),
|
---|
204 | tr("The host was not found. Please check the "
|
---|
205 | "host name and port settings."));
|
---|
206 | break;
|
---|
207 | case QAbstractSocket::ConnectionRefusedError:
|
---|
208 | QMessageBox::information(this, tr("Fortune Client"),
|
---|
209 | tr("The connection was refused by the peer. "
|
---|
210 | "Make sure the fortune server is running, "
|
---|
211 | "and check that the host name and port "
|
---|
212 | "settings are correct."));
|
---|
213 | break;
|
---|
214 | default:
|
---|
215 | QMessageBox::information(this, tr("Fortune Client"),
|
---|
216 | tr("The following error occurred: %1.")
|
---|
217 | .arg(tcpSocket->errorString()));
|
---|
218 | }
|
---|
219 |
|
---|
220 | getFortuneButton->setEnabled(true);
|
---|
221 | }
|
---|
222 | //! [13]
|
---|
223 |
|
---|
224 | void Client::enableGetFortuneButton()
|
---|
225 | {
|
---|
226 | getFortuneButton->setEnabled((!networkSession || networkSession->isOpen()) &&
|
---|
227 | !hostLineEdit->text().isEmpty() &&
|
---|
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 |
|
---|