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

source code of qtbase/examples/corelib/ipc/localfortuneclient/client.cpp