2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2017-09-27 09:59:59 +00:00
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
2016-01-22 12:24:00 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-07 20:57:49 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QtNetwork>
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
Client::Client(QWidget *parent)
|
2015-11-26 14:04:33 +00:00
|
|
|
: QDialog(parent)
|
|
|
|
, hostCombo(new QComboBox)
|
|
|
|
, portLineEdit(new QLineEdit)
|
|
|
|
, getFortuneButton(new QPushButton(tr("Get Fortune")))
|
|
|
|
, tcpSocket(new QTcpSocket(this))
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2015-11-26 14:04:33 +00:00
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [0]
|
2012-02-15 20:12:15 +00:00
|
|
|
hostCombo->setEditable(true);
|
|
|
|
// find out name of this machine
|
|
|
|
QString name = QHostInfo::localHostName();
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
hostCombo->addItem(name);
|
|
|
|
QString domain = QHostInfo::localDomainName();
|
|
|
|
if (!domain.isEmpty())
|
|
|
|
hostCombo->addItem(name + QChar('.') + domain);
|
|
|
|
}
|
2015-11-26 14:04:33 +00:00
|
|
|
if (name != QLatin1String("localhost"))
|
2012-02-15 20:12:15 +00:00
|
|
|
hostCombo->addItem(QString("localhost"));
|
|
|
|
// find out IP addresses of this machine
|
2011-04-27 10:05:43 +00:00
|
|
|
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
2012-02-15 20:12:15 +00:00
|
|
|
// add non-localhost addresses
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int i = 0; i < ipAddressesList.size(); ++i) {
|
2012-02-15 20:12:15 +00:00
|
|
|
if (!ipAddressesList.at(i).isLoopback())
|
|
|
|
hostCombo->addItem(ipAddressesList.at(i).toString());
|
|
|
|
}
|
|
|
|
// add localhost addresses
|
|
|
|
for (int i = 0; i < ipAddressesList.size(); ++i) {
|
|
|
|
if (ipAddressesList.at(i).isLoopback())
|
|
|
|
hostCombo->addItem(ipAddressesList.at(i).toString());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
|
|
|
|
2017-09-27 09:59:59 +00:00
|
|
|
auto hostLabel = new QLabel(tr("&Server name:"));
|
2012-02-15 20:12:15 +00:00
|
|
|
hostLabel->setBuddy(hostCombo);
|
2017-09-27 09:59:59 +00:00
|
|
|
auto portLabel = new QLabel(tr("S&erver port:"));
|
2011-04-27 10:05:43 +00:00
|
|
|
portLabel->setBuddy(portLineEdit);
|
|
|
|
|
|
|
|
statusLabel = new QLabel(tr("This examples requires that you run the "
|
|
|
|
"Fortune Server example as well."));
|
|
|
|
|
|
|
|
getFortuneButton->setDefault(true);
|
|
|
|
getFortuneButton->setEnabled(false);
|
|
|
|
|
2017-09-27 09:59:59 +00:00
|
|
|
auto quitButton = new QPushButton(tr("Quit"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-09-27 09:59:59 +00:00
|
|
|
auto buttonBox = new QDialogButtonBox;
|
2011-04-27 10:05:43 +00:00
|
|
|
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
|
|
|
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
|
|
|
|
2015-03-05 08:52:17 +00:00
|
|
|
//! [1]
|
|
|
|
in.setDevice(tcpSocket);
|
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
//! [1]
|
|
|
|
|
2015-11-26 14:04:33 +00:00
|
|
|
connect(hostCombo, &QComboBox::editTextChanged,
|
|
|
|
this, &Client::enableGetFortuneButton);
|
|
|
|
connect(portLineEdit, &QLineEdit::textChanged,
|
|
|
|
this, &Client::enableGetFortuneButton);
|
|
|
|
connect(getFortuneButton, &QAbstractButton::clicked,
|
|
|
|
this, &Client::requestNewFortune);
|
|
|
|
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [2] //! [3]
|
2015-11-26 14:04:33 +00:00
|
|
|
connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune);
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [2] //! [4]
|
2020-02-07 14:29:33 +00:00
|
|
|
connect(tcpSocket, &QAbstractSocket::errorOccurred,
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [3]
|
2015-11-26 14:04:33 +00:00
|
|
|
this, &Client::displayError);
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [4]
|
|
|
|
|
2017-09-27 09:59:59 +00:00
|
|
|
QGridLayout *mainLayout = nullptr;
|
2015-12-08 09:39:57 +00:00
|
|
|
if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) {
|
2017-09-27 09:59:59 +00:00
|
|
|
auto outerVerticalLayout = new QVBoxLayout(this);
|
2015-11-26 14:04:33 +00:00
|
|
|
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
|
2017-09-27 09:59:59 +00:00
|
|
|
auto outerHorizontalLayout = new QHBoxLayout;
|
2015-11-26 14:04:33 +00:00
|
|
|
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
2017-09-27 09:59:59 +00:00
|
|
|
auto groupBox = new QGroupBox(QGuiApplication::applicationDisplayName());
|
2015-11-26 14:04:33 +00:00
|
|
|
mainLayout = new QGridLayout(groupBox);
|
|
|
|
outerHorizontalLayout->addWidget(groupBox);
|
|
|
|
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
|
|
|
outerVerticalLayout->addLayout(outerHorizontalLayout);
|
|
|
|
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
|
|
|
|
} else {
|
|
|
|
mainLayout = new QGridLayout(this);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(hostLabel, 0, 0);
|
2012-02-15 20:12:15 +00:00
|
|
|
mainLayout->addWidget(hostCombo, 0, 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(portLabel, 1, 0);
|
|
|
|
mainLayout->addWidget(portLineEdit, 1, 1);
|
|
|
|
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
|
|
|
|
mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
|
|
|
|
|
2015-11-26 14:04:33 +00:00
|
|
|
setWindowTitle(QGuiApplication::applicationDisplayName());
|
2011-04-27 10:05:43 +00:00
|
|
|
portLineEdit->setFocus();
|
|
|
|
//! [5]
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|
|
|
|
//! [6]
|
|
|
|
void Client::requestNewFortune()
|
|
|
|
{
|
|
|
|
getFortuneButton->setEnabled(false);
|
|
|
|
tcpSocket->abort();
|
|
|
|
//! [7]
|
2012-02-15 20:12:15 +00:00
|
|
|
tcpSocket->connectToHost(hostCombo->currentText(),
|
2011-04-27 10:05:43 +00:00
|
|
|
portLineEdit->text().toInt());
|
|
|
|
//! [7]
|
|
|
|
}
|
|
|
|
//! [6]
|
|
|
|
|
|
|
|
//! [8]
|
|
|
|
void Client::readFortune()
|
|
|
|
{
|
2015-03-05 08:52:17 +00:00
|
|
|
in.startTransaction();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QString nextFortune;
|
|
|
|
in >> nextFortune;
|
|
|
|
|
2015-03-05 08:52:17 +00:00
|
|
|
if (!in.commitTransaction())
|
|
|
|
return;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (nextFortune == currentFortune) {
|
2015-11-26 14:04:33 +00:00
|
|
|
QTimer::singleShot(0, this, &Client::requestNewFortune);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFortune = nextFortune;
|
|
|
|
statusLabel->setText(currentFortune);
|
|
|
|
getFortuneButton->setEnabled(true);
|
|
|
|
}
|
2015-03-05 08:52:17 +00:00
|
|
|
//! [8]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [13]
|
|
|
|
void Client::displayError(QAbstractSocket::SocketError socketError)
|
|
|
|
{
|
|
|
|
switch (socketError) {
|
|
|
|
case QAbstractSocket::RemoteHostClosedError:
|
|
|
|
break;
|
|
|
|
case QAbstractSocket::HostNotFoundError:
|
|
|
|
QMessageBox::information(this, tr("Fortune Client"),
|
|
|
|
tr("The host was not found. Please check the "
|
|
|
|
"host name and port settings."));
|
|
|
|
break;
|
|
|
|
case QAbstractSocket::ConnectionRefusedError:
|
|
|
|
QMessageBox::information(this, tr("Fortune Client"),
|
|
|
|
tr("The connection was refused by the peer. "
|
|
|
|
"Make sure the fortune server is running, "
|
|
|
|
"and check that the host name and port "
|
|
|
|
"settings are correct."));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
QMessageBox::information(this, tr("Fortune Client"),
|
|
|
|
tr("The following error occurred: %1.")
|
|
|
|
.arg(tcpSocket->errorString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
getFortuneButton->setEnabled(true);
|
|
|
|
}
|
|
|
|
//! [13]
|
|
|
|
|
|
|
|
void Client::enableGetFortuneButton()
|
|
|
|
{
|
2020-02-14 15:35:01 +00:00
|
|
|
getFortuneButton->setEnabled(!hostCombo->currentText().isEmpty() &&
|
2011-04-27 10:05:43 +00:00
|
|
|
!portLineEdit->text().isEmpty());
|
|
|
|
|
|
|
|
}
|