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 "client.h"
55
56Client::Client(QWidget *parent)
57 : QDialog(parent),
58 hostLineEdit(new QLineEdit("fortune")),
59 getFortuneButton(new QPushButton(tr("Get Fortune"))),
60 statusLabel(new QLabel(tr("This examples requires that you run the "
61 "Local Fortune Server example as well."))),
62 socket(new QLocalSocket(this))
63{
64 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
65 QLabel *hostLabel = new QLabel(tr("&Server name:"));
66 hostLabel->setBuddy(hostLineEdit);
67
68 statusLabel->setWordWrap(true);
69
70 getFortuneButton->setDefault(true);
71 QPushButton *quitButton = new QPushButton(tr("Quit"));
72
73 QDialogButtonBox *buttonBox = new QDialogButtonBox;
74 buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
75 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
76
77 in.setDevice(socket);
78 in.setVersion(QDataStream::Qt_5_10);
79
80 connect(hostLineEdit, &QLineEdit::textChanged,
81 this, &Client::enableGetFortuneButton);
82 connect(getFortuneButton, &QPushButton::clicked,
83 this, &Client::requestNewFortune);
84 connect(quitButton, &QPushButton::clicked, this, &Client::close);
85 connect(socket, &QLocalSocket::readyRead, this, &Client::readFortune);
86 connect(socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error),
87 this, &Client::displayError);
88
89 QGridLayout *mainLayout = new QGridLayout(this);
90 mainLayout->addWidget(hostLabel, 0, 0);
91 mainLayout->addWidget(hostLineEdit, 0, 1);
92 mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
93 mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
94
95 setWindowTitle(QGuiApplication::applicationDisplayName());
96 hostLineEdit->setFocus();
97}
98
99void Client::requestNewFortune()
100{
101 getFortuneButton->setEnabled(false);
102 blockSize = 0;
103 socket->abort();
104 socket->connectToServer(hostLineEdit->text());
105}
106
107void Client::readFortune()
108{
109 if (blockSize == 0) {
110 // Relies on the fact that QDataStream serializes a quint32 into
111 // sizeof(quint32) bytes
112 if (socket->bytesAvailable() < (int)sizeof(quint32))
113 return;
114 in >> blockSize;
115 }
116
117 if (socket->bytesAvailable() < blockSize || in.atEnd())
118 return;
119
120 QString nextFortune;
121 in >> nextFortune;
122
123 if (nextFortune == currentFortune) {
124 QTimer::singleShot(0, this, &Client::requestNewFortune);
125 return;
126 }
127
128 currentFortune = nextFortune;
129 statusLabel->setText(currentFortune);
130 getFortuneButton->setEnabled(true);
131}
132
133void Client::displayError(QLocalSocket::LocalSocketError socketError)
134{
135 switch (socketError) {
136 case QLocalSocket::ServerNotFoundError:
137 QMessageBox::information(this, tr("Local Fortune Client"),
138 tr("The host was not found. Please make sure "
139 "that the server is running and that the "
140 "server name is correct."));
141 break;
142 case QLocalSocket::ConnectionRefusedError:
143 QMessageBox::information(this, tr("Local Fortune Client"),
144 tr("The connection was refused by the peer. "
145 "Make sure the fortune server is running, "
146 "and check that the server name "
147 "is correct."));
148 break;
149 case QLocalSocket::PeerClosedError:
150 break;
151 default:
152 QMessageBox::information(this, tr("Local Fortune Client"),
153 tr("The following error occurred: %1.")
154 .arg(socket->errorString()));
155 }
156
157 getFortuneButton->setEnabled(true);
158}
159
160void Client::enableGetFortuneButton()
161{
162 getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty());
163}
164