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 "server.h" |
52 | |
53 | #include <QtWidgets> |
54 | #include <QtNetwork> |
55 | |
56 | Server::Server(QWidget *parent) |
57 | : QDialog(parent) |
58 | { |
59 | setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
60 | |
61 | server = new QLocalServer(this); |
62 | if (!server->listen(name: "fortune" )) { |
63 | QMessageBox::critical(parent: this, title: tr(s: "Local Fortune Server" ), |
64 | text: tr(s: "Unable to start the server: %1." ) |
65 | .arg(a: server->errorString())); |
66 | close(); |
67 | return; |
68 | } |
69 | |
70 | QLabel *statusLabel = new QLabel; |
71 | statusLabel->setWordWrap(true); |
72 | statusLabel->setText(tr(s: "The server is running.\n" |
73 | "Run the Local Fortune Client example now." )); |
74 | |
75 | fortunes << tr(s: "You've been leading a dog's life. Stay off the furniture." ) |
76 | << tr(s: "You've got to think about tomorrow." ) |
77 | << tr(s: "You will be surprised by a loud noise." ) |
78 | << tr(s: "You will feel hungry again in another hour." ) |
79 | << tr(s: "You might have mail." ) |
80 | << tr(s: "You cannot kill time without injuring eternity." ) |
81 | << tr(s: "Computers are not intelligent. They only think they are." ); |
82 | |
83 | QPushButton *quitButton = new QPushButton(tr(s: "Quit" )); |
84 | quitButton->setAutoDefault(false); |
85 | connect(sender: quitButton, signal: &QPushButton::clicked, receiver: this, slot: &Server::close); |
86 | connect(sender: server, signal: &QLocalServer::newConnection, receiver: this, slot: &Server::sendFortune); |
87 | |
88 | QHBoxLayout *buttonLayout = new QHBoxLayout; |
89 | buttonLayout->addStretch(stretch: 1); |
90 | buttonLayout->addWidget(quitButton); |
91 | buttonLayout->addStretch(stretch: 1); |
92 | |
93 | QVBoxLayout *mainLayout = new QVBoxLayout(this); |
94 | mainLayout->addWidget(statusLabel); |
95 | mainLayout->addLayout(layout: buttonLayout); |
96 | |
97 | setWindowTitle(QGuiApplication::applicationDisplayName()); |
98 | } |
99 | |
100 | void Server::sendFortune() |
101 | { |
102 | QByteArray block; |
103 | QDataStream out(&block, QIODevice::WriteOnly); |
104 | out.setVersion(QDataStream::Qt_5_10); |
105 | const int fortuneIndex = QRandomGenerator::global()->bounded(lowest: 0, highest: fortunes.size()); |
106 | const QString &message = fortunes.at(i: fortuneIndex); |
107 | out << quint32(message.size()); |
108 | out << message; |
109 | |
110 | QLocalSocket *clientConnection = server->nextPendingConnection(); |
111 | connect(sender: clientConnection, signal: &QLocalSocket::disconnected, |
112 | receiver: clientConnection, slot: &QLocalSocket::deleteLater); |
113 | |
114 | clientConnection->write(data: block); |
115 | clientConnection->flush(); |
116 | clientConnection->disconnectFromServer(); |
117 | } |
118 | |