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 "blockingclient.h"
|
---|
45 |
|
---|
46 | BlockingClient::BlockingClient(QWidget *parent)
|
---|
47 | : QDialog(parent)
|
---|
48 | {
|
---|
49 | hostLabel = new QLabel(tr("&Server name:"));
|
---|
50 | portLabel = new QLabel(tr("S&erver port:"));
|
---|
51 |
|
---|
52 | // find out which IP to connect to
|
---|
53 | QString ipAddress;
|
---|
54 | QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
---|
55 | // use the first non-localhost IPv4 address
|
---|
56 | for (int i = 0; i < ipAddressesList.size(); ++i) {
|
---|
57 | if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
|
---|
58 | ipAddressesList.at(i).toIPv4Address()) {
|
---|
59 | ipAddress = ipAddressesList.at(i).toString();
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | // if we did not find one, use IPv4 localhost
|
---|
64 | if (ipAddress.isEmpty())
|
---|
65 | ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
---|
66 |
|
---|
67 | hostLineEdit = new QLineEdit(ipAddress);
|
---|
68 | portLineEdit = new QLineEdit;
|
---|
69 | portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
---|
70 |
|
---|
71 | hostLabel->setBuddy(hostLineEdit);
|
---|
72 | portLabel->setBuddy(portLineEdit);
|
---|
73 |
|
---|
74 | statusLabel = new QLabel(tr("This examples requires that you run the "
|
---|
75 | "Fortune Server example as well."));
|
---|
76 |
|
---|
77 | getFortuneButton = new QPushButton(tr("Get Fortune"));
|
---|
78 | getFortuneButton->setDefault(true);
|
---|
79 | getFortuneButton->setEnabled(false);
|
---|
80 |
|
---|
81 | quitButton = new QPushButton(tr("Quit"));
|
---|
82 |
|
---|
83 | buttonBox = new QDialogButtonBox;
|
---|
84 | buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
---|
85 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
---|
86 |
|
---|
87 | connect(hostLineEdit, SIGNAL(textChanged(QString)),
|
---|
88 | this, SLOT(enableGetFortuneButton()));
|
---|
89 | connect(portLineEdit, SIGNAL(textChanged(QString)),
|
---|
90 | this, SLOT(enableGetFortuneButton()));
|
---|
91 | connect(getFortuneButton, SIGNAL(clicked()),
|
---|
92 | this, SLOT(requestNewFortune()));
|
---|
93 | connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
---|
94 | //! [0]
|
---|
95 | connect(&thread, SIGNAL(newFortune(QString)),
|
---|
96 | this, SLOT(showFortune(QString)));
|
---|
97 | //! [0] //! [1]
|
---|
98 | connect(&thread, SIGNAL(error(int,QString)),
|
---|
99 | this, SLOT(displayError(int,QString)));
|
---|
100 | //! [1]
|
---|
101 |
|
---|
102 | QGridLayout *mainLayout = new QGridLayout;
|
---|
103 | mainLayout->addWidget(hostLabel, 0, 0);
|
---|
104 | mainLayout->addWidget(hostLineEdit, 0, 1);
|
---|
105 | mainLayout->addWidget(portLabel, 1, 0);
|
---|
106 | mainLayout->addWidget(portLineEdit, 1, 1);
|
---|
107 | mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
|
---|
108 | mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
|
---|
109 | setLayout(mainLayout);
|
---|
110 |
|
---|
111 | setWindowTitle(tr("Blocking Fortune Client"));
|
---|
112 | portLineEdit->setFocus();
|
---|
113 | }
|
---|
114 |
|
---|
115 | //! [2]
|
---|
116 | void BlockingClient::requestNewFortune()
|
---|
117 | {
|
---|
118 | getFortuneButton->setEnabled(false);
|
---|
119 | thread.requestNewFortune(hostLineEdit->text(),
|
---|
120 | portLineEdit->text().toInt());
|
---|
121 | }
|
---|
122 | //! [2]
|
---|
123 |
|
---|
124 | //! [3]
|
---|
125 | void BlockingClient::showFortune(const QString &nextFortune)
|
---|
126 | {
|
---|
127 | if (nextFortune == currentFortune) {
|
---|
128 | requestNewFortune();
|
---|
129 | return;
|
---|
130 | }
|
---|
131 | //! [3]
|
---|
132 |
|
---|
133 | //! [4]
|
---|
134 | currentFortune = nextFortune;
|
---|
135 | statusLabel->setText(currentFortune);
|
---|
136 | getFortuneButton->setEnabled(true);
|
---|
137 | }
|
---|
138 | //! [4]
|
---|
139 |
|
---|
140 | void BlockingClient::displayError(int socketError, const QString &message)
|
---|
141 | {
|
---|
142 | switch (socketError) {
|
---|
143 | case QAbstractSocket::HostNotFoundError:
|
---|
144 | QMessageBox::information(this, tr("Blocking Fortune Client"),
|
---|
145 | tr("The host was not found. Please check the "
|
---|
146 | "host and port settings."));
|
---|
147 | break;
|
---|
148 | case QAbstractSocket::ConnectionRefusedError:
|
---|
149 | QMessageBox::information(this, tr("Blocking Fortune Client"),
|
---|
150 | tr("The connection was refused by the peer. "
|
---|
151 | "Make sure the fortune server is running, "
|
---|
152 | "and check that the host name and port "
|
---|
153 | "settings are correct."));
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | QMessageBox::information(this, tr("Blocking Fortune Client"),
|
---|
157 | tr("The following error occurred: %1.")
|
---|
158 | .arg(message));
|
---|
159 | }
|
---|
160 |
|
---|
161 | getFortuneButton->setEnabled(true);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void BlockingClient::enableGetFortuneButton()
|
---|
165 | {
|
---|
166 | getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
|
---|
167 | && !portLineEdit->text().isEmpty());
|
---|
168 | }
|
---|