From 297bc47ad4c0667e23ac7e2df500a8be5cd89ffa Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Tue, 27 Nov 2012 21:52:13 +0100 Subject: dbus-chat => chat to make the source and install locations consistent Change-Id: Ifde8748eeab843b06bf79f941fa4789b5b68cca9 Reviewed-by: Jerome Pasion --- examples/dbus/chat/chat.cpp | 163 ++++++++++++++++++++++++++++ examples/dbus/chat/chat.h | 81 ++++++++++++++ examples/dbus/chat/chat.pro | 16 +++ examples/dbus/chat/chatmainwindow.ui | 185 ++++++++++++++++++++++++++++++++ examples/dbus/chat/chatsetnickname.ui | 149 +++++++++++++++++++++++++ examples/dbus/chat/org.example.chat.xml | 15 +++ 6 files changed, 609 insertions(+) create mode 100644 examples/dbus/chat/chat.cpp create mode 100644 examples/dbus/chat/chat.h create mode 100644 examples/dbus/chat/chat.pro create mode 100644 examples/dbus/chat/chatmainwindow.ui create mode 100644 examples/dbus/chat/chatsetnickname.ui create mode 100644 examples/dbus/chat/org.example.chat.xml (limited to 'examples/dbus/chat') diff --git a/examples/dbus/chat/chat.cpp b/examples/dbus/chat/chat.cpp new file mode 100644 index 0000000000..febb1da90c --- /dev/null +++ b/examples/dbus/chat/chat.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 Digia Plc and its Subsidiary(-ies) 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$ +** +****************************************************************************/ + +#include +#include + +#include "chat.h" +#include "chat_adaptor.h" +#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())); + + // 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() +{ +} + +void ChatMainWindow::rebuildHistory() +{ + QString history = m_messages.join( QLatin1String("\n" ) ); + chatHistory->setPlainText(history); +} + +void ChatMainWindow::messageSlot(const QString &nickname, const QString &text) +{ + QString msg( QLatin1String("<%1> %2") ); + msg = msg.arg(nickname, text); + m_messages.append(msg); + + if (m_messages.count() > 100) + m_messages.removeFirst(); + rebuildHistory(); +} + +void ChatMainWindow::actionSlot(const QString &nickname, const QString &text) +{ + QString msg( QLatin1String("* %1 %2") ); + msg = msg.arg(nickname, text); + m_messages.append(msg); + + 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()); +} + +void ChatMainWindow::changeNickname() +{ + 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)); + } +} + +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); +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + if (!QDBusConnection::sessionBus().isConnected()) { + qWarning("Cannot connect to the D-Bus session bus.\n" + "Please check your system settings and try again.\n"); + return 1; + } + + ChatMainWindow chat; + chat.show(); + return app.exec(); +} diff --git a/examples/dbus/chat/chat.h b/examples/dbus/chat/chat.h new file mode 100644 index 0000000000..fd73d6d227 --- /dev/null +++ b/examples/dbus/chat/chat.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 Digia Plc and its Subsidiary(-ies) 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$ +** +****************************************************************************/ + +#ifndef CHAT_H +#define CHAT_H + +#include + +#include "ui_chatmainwindow.h" +#include "ui_chatsetnickname.h" + +class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow +{ + Q_OBJECT + QString m_nickname; + QStringList m_messages; +public: + ChatMainWindow(); + ~ChatMainWindow(); + + void rebuildHistory(); + +signals: + void message(const QString &nickname, const QString &text); + void action(const QString &nickname, const QString &text); + +private slots: + void messageSlot(const QString &nickname, const QString &text); + void actionSlot(const QString &nickname, const QString &text); + void textChangedSlot(const QString &newText); + void sendClickedSlot(); + void changeNickname(); + void aboutQt(); + void exiting(); +}; + +class NicknameDialog: public QDialog, public Ui::NicknameDialog +{ + Q_OBJECT +public: + NicknameDialog(QWidget *parent = 0); +}; + +#endif // CHAT_H diff --git a/examples/dbus/chat/chat.pro b/examples/dbus/chat/chat.pro new file mode 100644 index 0000000000..5c78405804 --- /dev/null +++ b/examples/dbus/chat/chat.pro @@ -0,0 +1,16 @@ +QT += dbus widgets + +HEADERS += chat.h +SOURCES += chat.cpp +FORMS += chatmainwindow.ui chatsetnickname.ui + +DBUS_ADAPTORS += org.example.chat.xml +DBUS_INTERFACES += org.example.chat.xml + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/dbus/chat +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS *.pro *.xml +sources.path = $$[QT_INSTALL_EXAMPLES]/dbus/dbus-chat +INSTALLS += target sources + +simulator: warning(This example does not work on Simulator platform) diff --git a/examples/dbus/chat/chatmainwindow.ui b/examples/dbus/chat/chatmainwindow.ui new file mode 100644 index 0000000000..0616dcb137 --- /dev/null +++ b/examples/dbus/chat/chatmainwindow.ui @@ -0,0 +1,185 @@ + + + + + ChatMainWindow + + + + 0 + 0 + 800 + 600 + + + + QtDBus Chat + + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + false + + + Messages sent and received from other users + + + true + + + + + + + 0 + + + 6 + + + + + Message: + + + messageLineEdit + + + + + + + + + + + 1 + 0 + 0 + 0 + + + + Sends a message to other people + + + + + + Send + + + + + + + + + + + + + 0 + 0 + 800 + 31 + + + + + Help + + + + + + File + + + + + + + + + + + + Quit + + + Ctrl+Q + + + + + About Qt... + + + + + Change nickname... + + + Ctrl+N + + + + + + chatHistory + messageLineEdit + sendButton + + + + + messageLineEdit + returnPressed() + sendButton + animateClick() + + + 299 + 554 + + + 744 + 551 + + + + + actionQuit + triggered(bool) + ChatMainWindow + close() + + + -1 + -1 + + + 399 + 299 + + + + + diff --git a/examples/dbus/chat/chatsetnickname.ui b/examples/dbus/chat/chatsetnickname.ui new file mode 100644 index 0000000000..fb9894e09f --- /dev/null +++ b/examples/dbus/chat/chatsetnickname.ui @@ -0,0 +1,149 @@ + + + + + NicknameDialog + + + + 0 + 0 + 396 + 105 + + + + + 1 + 1 + 0 + 0 + + + + Set nickname + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + + 1 + 1 + 0 + 0 + + + + New nickname: + + + + + + + + + + + + 0 + + + 6 + + + + + Qt::Horizontal + + + + 131 + 31 + + + + + + + + OK + + + + + + + Cancel + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + okButton + clicked() + NicknameDialog + accept() + + + 278 + 253 + + + 96 + 254 + + + + + cancelButton + clicked() + NicknameDialog + reject() + + + 369 + 253 + + + 179 + 282 + + + + + diff --git a/examples/dbus/chat/org.example.chat.xml b/examples/dbus/chat/org.example.chat.xml new file mode 100644 index 0000000000..acb6dc1d57 --- /dev/null +++ b/examples/dbus/chat/org.example.chat.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + -- cgit v1.2.3