PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/corelib/ipc/localfortuneclient/client.cpp

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