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("&Server name:"));
60 portLabel = new QLabel(tr("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("This examples requires that you run the "
86 "Fortune Server example as well."));
87 statusLabel->setWordWrap(true);
88
89 getFortuneButton = new QPushButton(tr("Get Fortune"));
90 getFortuneButton->setDefault(true);
91 getFortuneButton->setEnabled(false);
92
93 quitButton = new QPushButton(tr("Quit"));
94
95 buttonBox = new QDialogButtonBox;
96 buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
97 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
98
99 connect(getFortuneButton, &QPushButton::clicked,
100 this, &BlockingClient::requestNewFortune);
101 connect(quitButton, &QPushButton::clicked,
102 this, &BlockingClient::close);
103
104 connect(hostLineEdit, &QLineEdit::textChanged,
105 this, &BlockingClient::enableGetFortuneButton);
106 connect(portLineEdit, &QLineEdit::textChanged,
107 this, &BlockingClient::enableGetFortuneButton);
108//! [0]
109 connect(&thread, &FortuneThread::newFortune,
110 this, &BlockingClient::showFortune);
111 connect(&thread, &FortuneThread::error,
112 this, &BlockingClient::displayError);
113//! [0]
114
115 QGridLayout *mainLayout = new QGridLayout;
116 mainLayout->addWidget(hostLabel, 0, 0);
117 mainLayout->addWidget(hostLineEdit, 0, 1);
118 mainLayout->addWidget(portLabel, 1, 0);
119 mainLayout->addWidget(portLineEdit, 1, 1);
120 mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
121 mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
122 setLayout(mainLayout);
123
124 setWindowTitle(tr("Blocking Fortune Client"));
125 portLineEdit->setFocus();
126}
127
128//! [1]
129void BlockingClient::requestNewFortune()
130{
131 getFortuneButton->setEnabled(false);
132 thread.requestNewFortune(hostLineEdit->text(),
133 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(this, tr("Blocking Fortune Client"),
158 tr("The host was not found. Please check the "
159 "host and port settings."));
160 break;
161 case QAbstractSocket::ConnectionRefusedError:
162 QMessageBox::information(this, tr("Blocking Fortune Client"),
163 tr("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(this, tr("Blocking Fortune Client"),
170 tr("The following error occurred: %1.")
171 .arg(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