diff options
Diffstat (limited to 'examples/dbus/chat/chat.cpp')
-rw-r--r-- | examples/dbus/chat/chat.cpp | 170 |
1 files changed, 44 insertions, 126 deletions
diff --git a/examples/dbus/chat/chat.cpp b/examples/dbus/chat/chat.cpp index 7855457a82..cced911955 100644 --- a/examples/dbus/chat/chat.cpp +++ b/examples/dbus/chat/chat.cpp @@ -1,54 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** 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." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include <QApplication> +#include <QInputDialog> #include <QMessageBox> #include "chat.h" @@ -56,105 +10,69 @@ #include "chat_interface.h" ChatMainWindow::ChatMainWindow() - : m_nickname(QLatin1String("nickname")) { setupUi(this); - sendButton->setEnabled(false); - connect(messageLineEdit, SIGNAL(textChanged(QString)), - this, SLOT(textChangedSlot(QString))); - connect(sendButton, SIGNAL(clicked(bool)), this, SLOT(sendClickedSlot())); - connect(actionChangeNickname, SIGNAL(triggered(bool)), this, SLOT(changeNickname())); - connect(actionAboutQt, SIGNAL(triggered(bool)), this, SLOT(aboutQt())); - connect(qApp, SIGNAL(lastWindowClosed()), this, SLOT(exiting())); + connect(messageLineEdit, &QLineEdit::textChanged, this, + [this](const QString &newText) { sendButton->setEnabled(!newText.isEmpty()); }); + connect(sendButton, &QPushButton::clicked, this, [this]() { + emit message(m_nickname, messageLineEdit->text()); + messageLineEdit->clear(); + }); + connect(actionChangeNickname, &QAction::triggered, + this, &ChatMainWindow::changeNickname); + connect(actionAboutQt, &QAction::triggered, this, [this]() { QMessageBox::aboutQt(this); }); + connect(qApp, &QApplication::lastWindowClosed, this, + [this]() { emit action(m_nickname, tr("leaves the chat")); }); // add our D-Bus interface and connect to D-Bus new ChatAdaptor(this); - QDBusConnection::sessionBus().registerObject("/", this); - - org::example::chat *iface; - iface = new org::example::chat(QString(), QString(), QDBusConnection::sessionBus(), this); - //connect(iface, SIGNAL(message(QString,QString)), this, SLOT(messageSlot(QString,QString))); - QDBusConnection::sessionBus().connect(QString(), QString(), "org.example.chat", "message", this, SLOT(messageSlot(QString,QString))); - connect(iface, SIGNAL(action(QString,QString)), this, SLOT(actionSlot(QString,QString))); - - NicknameDialog dialog; - dialog.cancelButton->setVisible(false); - dialog.exec(); - m_nickname = dialog.nickname->text().trimmed(); - emit action(m_nickname, QLatin1String("joins the chat")); -} -ChatMainWindow::~ChatMainWindow() -{ -} + auto connection = QDBusConnection::sessionBus(); + connection.registerObject("/", this); -void ChatMainWindow::rebuildHistory() -{ - QString history = m_messages.join( QLatin1String("\n" ) ); - chatHistory->setPlainText(history); -} + using org::example::chat; -void ChatMainWindow::messageSlot(const QString &nickname, const QString &text) -{ - QString msg( QLatin1String("<%1> %2") ); - msg = msg.arg(nickname, text); - m_messages.append(msg); + auto *iface = new chat({}, {}, connection, this); + connect(iface, &chat::message, this, [this](const QString &nickname, const QString &text) { + displayMessage(tr("<%1> %2").arg(nickname, text)); + }); + connect(iface, &chat::action, this, [this](const QString &nickname, const QString &text) { + displayMessage(tr("* %1 %2").arg(nickname, text)); + }); - if (m_messages.count() > 100) - m_messages.removeFirst(); - rebuildHistory(); + if (!changeNickname(true)) + QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection); } -void ChatMainWindow::actionSlot(const QString &nickname, const QString &text) +void ChatMainWindow::displayMessage(const QString &message) { - QString msg( QLatin1String("* %1 %2") ); - msg = msg.arg(nickname, text); - m_messages.append(msg); + m_messages.append(message); if (m_messages.count() > 100) m_messages.removeFirst(); - rebuildHistory(); -} - -void ChatMainWindow::textChangedSlot(const QString &newText) -{ - sendButton->setEnabled(!newText.isEmpty()); -} -void ChatMainWindow::sendClickedSlot() -{ - //emit message(m_nickname, messageLineEdit->text()); - QDBusMessage msg = QDBusMessage::createSignal("/", "org.example.chat", "message"); - msg << m_nickname << messageLineEdit->text(); - QDBusConnection::sessionBus().send(msg); - messageLineEdit->setText(QString()); + auto history = m_messages.join(QLatin1String("\n")); + chatHistory->setPlainText(history); } -void ChatMainWindow::changeNickname() +bool ChatMainWindow::changeNickname(bool initial) { - NicknameDialog dialog(this); - if (dialog.exec() == QDialog::Accepted) { - QString old = m_nickname; - m_nickname = dialog.nickname->text().trimmed(); - emit action(old, QString("is now known as %1").arg(m_nickname)); + auto newNickname = QInputDialog::getText(this, tr("Set nickname"), tr("New nickname:")); + newNickname = newNickname.trimmed(); + + if (!newNickname.isEmpty()) { + auto old = m_nickname; + m_nickname = newNickname; + + if (initial) + emit action(m_nickname, tr("joins the chat")); + else + emit action(old, tr("is now known as %1").arg(m_nickname)); + return true; } -} - -void ChatMainWindow::aboutQt() -{ - QMessageBox::aboutQt(this); -} - -void ChatMainWindow::exiting() -{ - emit action(m_nickname, QLatin1String("leaves the chat")); -} -NicknameDialog::NicknameDialog(QWidget *parent) - : QDialog(parent) -{ - setupUi(this); + return false; } int main(int argc, char **argv) |