1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtWidgets>
52#include <QtNetwork>
53
54#include "blockingclient.h"
55
56BlockingClient::BlockingClient(QWidget *parent)
57 : QWidget(parent)
58{
59 hostLabel = new QLabel(tr(s: "&Server name:"));
60 portLabel = new QLabel(tr(s: "S&erver port:"));
61
62 // find out which IP to connect to
63 QString ipAddress;
64 QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
65 // use the first non-localhost IPv4 address
66 for (int i = 0; i < ipAddressesList.size(); ++i) {
67 if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
68 ipAddressesList.at(i).toIPv4Address()) {
69 ipAddress = ipAddressesList.at(i).toString();
70 break;
71 }
72 }
73 // if we did not find one, use IPv4 localhost
74 if (ipAddress.isEmpty())
75 ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
76
77 hostLineEdit = new QLineEdit(ipAddress);
78 portLineEdit = new QLineEdit;
79 portLineEdit->setValidator(new QIntValidator(1, 65535, this));
80
81
82 hostLabel->setBuddy(hostLineEdit);
83 portLabel->setBuddy(portLineEdit);
84
85 statusLabel = new QLabel(tr(s: "This examples requires that you run the "
86 "Fortune Server example as well."));
87 statusLabel->setWordWrap(true);
88
89 getFortuneButton = new QPushButton(tr(s: "Get Fortune"));
90 getFortuneButton->setDefault(true);
91 getFortuneButton->setEnabled(false);
92
93 quitButton = new QPushButton(tr(s: "Quit"));
94
95 buttonBox = new QDialogButtonBox;
96 buttonBox->addButton(button: getFortuneButton, role: QDialogButtonBox::ActionRole);
97 buttonBox->addButton(button: quitButton, role: QDialogButtonBox::RejectRole);
98
99 connect(sender: getFortuneButton, signal: &QPushButton::clicked,
100 receiver: this, slot: &BlockingClient::requestNewFortune);
101 connect(sender: quitButton, signal: &QPushButton::clicked,
102 receiver: this, slot: &BlockingClient::close);
103
104 connect(sender: hostLineEdit, signal: &QLineEdit::textChanged,
105 receiver: this, slot: &BlockingClient::enableGetFortuneButton);
106 connect(sender: portLineEdit, signal: &QLineEdit::textChanged,
107 receiver: this, slot: &BlockingClient::enableGetFortuneButton);
108//! [0]
109 connect(sender: &thread, signal: &FortuneThread::newFortune,
110 receiver: this, slot: &BlockingClient::showFortune);
111 connect(sender: &thread, signal: &FortuneThread::error,
112 receiver: this, slot: &BlockingClient::displayError);
113//! [0]
114
115 QGridLayout *mainLayout = new QGridLayout;
116 mainLayout->addWidget(hostLabel, row: 0, column: 0);
117 mainLayout->addWidget(hostLineEdit, row: 0, column: 1);
118 mainLayout->addWidget(portLabel, row: 1, column: 0);
119 mainLayout->addWidget(portLineEdit, row: 1, column: 1);
120 mainLayout->addWidget(statusLabel, row: 2, column: 0, rowSpan: 1, columnSpan: 2);
121 mainLayout->addWidget(buttonBox, row: 3, column: 0, rowSpan: 1, columnSpan: 2);
122 setLayout(mainLayout);
123
124 setWindowTitle(tr(s: "Blocking Fortune Client"));
125 portLineEdit->setFocus();
126}
127
128//! [1]
129void BlockingClient::requestNewFortune()
130{
131 getFortuneButton->setEnabled(false);
132 thread.requestNewFortune(hostName: hostLineEdit->text(),
133 port: portLineEdit->text().toInt());
134}
135//! [1]
136
137//! [2]
138void BlockingClient::showFortune(const QString &nextFortune)
139{
140 if (nextFortune == currentFortune) {
141 requestNewFortune();
142 return;
143 }
144//! [2]
145
146//! [3]
147 currentFortune = nextFortune;
148 statusLabel->setText(currentFortune);
149 getFortuneButton->setEnabled(true);
150}
151//! [3]
152
153void BlockingClient::displayError(int socketError, const QString &message)
154{
155 switch (socketError) {
156 case QAbstractSocket::HostNotFoundError:
157 QMessageBox::information(parent: this, title: tr(s: "Blocking Fortune Client"),
158 text: tr(s: "The host was not found. Please check the "
159 "host and port settings."));
160 break;
161 case QAbstractSocket::ConnectionRefusedError:
162 QMessageBox::information(parent: this, title: tr(s: "Blocking Fortune Client"),
163 text: tr(s: "The connection was refused by the peer. "
164 "Make sure the fortune server is running, "
165 "and check that the host name and port "
166 "settings are correct."));
167 break;
168 default:
169 QMessageBox::information(parent: this, title: tr(s: "Blocking Fortune Client"),
170 text: tr(s: "The following error occurred: %1.")
171 .arg(a: message));
172 }
173
174 getFortuneButton->setEnabled(true);
175}
176
177void BlockingClient::enableGetFortuneButton()
178{
179 bool enable(!hostLineEdit->text().isEmpty() && !portLineEdit->text().isEmpty());
180 getFortuneButton->setEnabled(enable);
181}
182

source code of qtbase/examples/network/blockingfortuneclient/blockingclient.cpp