2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2013-01-02 11:13:29 +00:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
** You may use this file under the terms of the BSD license as follows:
|
|
|
|
**
|
|
|
|
** "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.
|
2012-09-19 12:28:29 +00:00
|
|
|
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
|
|
|
** of its contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
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)
|
|
|
|
: QDialog(parent), networkSession(0)
|
|
|
|
{
|
|
|
|
//! [0]
|
|
|
|
hostLabel = new QLabel(tr("&Server name:"));
|
|
|
|
portLabel = new QLabel(tr("S&erver port:"));
|
|
|
|
|
2012-02-15 20:12:15 +00:00
|
|
|
hostCombo = new QComboBox;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (name != QString("localhost"))
|
|
|
|
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 = new QLineEdit;
|
|
|
|
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
|
|
|
|
2012-02-15 20:12:15 +00:00
|
|
|
hostLabel->setBuddy(hostCombo);
|
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 = new QPushButton(tr("Get Fortune"));
|
|
|
|
getFortuneButton->setDefault(true);
|
|
|
|
getFortuneButton->setEnabled(false);
|
|
|
|
|
|
|
|
quitButton = new QPushButton(tr("Quit"));
|
|
|
|
|
|
|
|
buttonBox = new QDialogButtonBox;
|
|
|
|
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
|
|
|
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
tcpSocket = new QTcpSocket(this);
|
|
|
|
//! [1]
|
|
|
|
|
2012-02-15 20:12:15 +00:00
|
|
|
connect(hostCombo, SIGNAL(editTextChanged(QString)),
|
2011-04-27 10:05:43 +00:00
|
|
|
this, SLOT(enableGetFortuneButton()));
|
|
|
|
connect(portLineEdit, SIGNAL(textChanged(QString)),
|
|
|
|
this, SLOT(enableGetFortuneButton()));
|
|
|
|
connect(getFortuneButton, SIGNAL(clicked()),
|
|
|
|
this, SLOT(requestNewFortune()));
|
|
|
|
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
|
|
|
//! [2] //! [3]
|
|
|
|
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
|
|
|
|
//! [2] //! [4]
|
|
|
|
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
|
|
|
|
//! [3]
|
|
|
|
this, SLOT(displayError(QAbstractSocket::SocketError)));
|
|
|
|
//! [4]
|
|
|
|
|
|
|
|
QGridLayout *mainLayout = new QGridLayout;
|
|
|
|
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);
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
|
|
|
setWindowTitle(tr("Fortune Client"));
|
|
|
|
portLineEdit->setFocus();
|
|
|
|
|
|
|
|
QNetworkConfigurationManager manager;
|
|
|
|
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
|
|
|
|
// Get saved network configuration
|
2012-10-16 08:23:43 +00:00
|
|
|
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
|
2011-04-27 10:05:43 +00:00
|
|
|
settings.beginGroup(QLatin1String("QtNetwork"));
|
|
|
|
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
// If the saved network configuration is not currently discovered use the system default
|
|
|
|
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
|
|
|
|
if ((config.state() & QNetworkConfiguration::Discovered) !=
|
|
|
|
QNetworkConfiguration::Discovered) {
|
|
|
|
config = manager.defaultConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
networkSession = new QNetworkSession(config, this);
|
|
|
|
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
|
|
|
|
|
|
|
|
getFortuneButton->setEnabled(false);
|
|
|
|
statusLabel->setText(tr("Opening network session."));
|
|
|
|
networkSession->open();
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|
|
|
|
//! [6]
|
|
|
|
void Client::requestNewFortune()
|
|
|
|
{
|
|
|
|
getFortuneButton->setEnabled(false);
|
|
|
|
blockSize = 0;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
//! [9]
|
|
|
|
QDataStream in(tcpSocket);
|
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
|
|
|
if (blockSize == 0) {
|
|
|
|
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
|
|
|
|
return;
|
|
|
|
//! [8]
|
|
|
|
|
|
|
|
//! [10]
|
|
|
|
in >> blockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcpSocket->bytesAvailable() < blockSize)
|
|
|
|
return;
|
|
|
|
//! [10] //! [11]
|
|
|
|
|
|
|
|
QString nextFortune;
|
|
|
|
in >> nextFortune;
|
|
|
|
|
|
|
|
if (nextFortune == currentFortune) {
|
|
|
|
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//! [11]
|
|
|
|
|
|
|
|
//! [12]
|
|
|
|
currentFortune = nextFortune;
|
|
|
|
//! [9]
|
|
|
|
statusLabel->setText(currentFortune);
|
|
|
|
getFortuneButton->setEnabled(true);
|
|
|
|
}
|
|
|
|
//! [12]
|
|
|
|
|
|
|
|
//! [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()
|
|
|
|
{
|
|
|
|
getFortuneButton->setEnabled((!networkSession || networkSession->isOpen()) &&
|
2012-02-15 20:12:15 +00:00
|
|
|
!hostCombo->currentText().isEmpty() &&
|
2011-04-27 10:05:43 +00:00
|
|
|
!portLineEdit->text().isEmpty());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::sessionOpened()
|
|
|
|
{
|
|
|
|
// Save the used configuration
|
|
|
|
QNetworkConfiguration config = networkSession->configuration();
|
|
|
|
QString id;
|
|
|
|
if (config.type() == QNetworkConfiguration::UserChoice)
|
|
|
|
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
|
|
|
|
else
|
|
|
|
id = config.identifier();
|
|
|
|
|
2012-10-16 08:23:43 +00:00
|
|
|
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
|
2011-04-27 10:05:43 +00:00
|
|
|
settings.beginGroup(QLatin1String("QtNetwork"));
|
|
|
|
settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
statusLabel->setText(tr("This examples requires that you run the "
|
|
|
|
"Fortune Server example as well."));
|
|
|
|
|
|
|
|
enableGetFortuneButton();
|
|
|
|
}
|
|
|
|
|