summaryrefslogtreecommitdiffstats
path: root/examples/dbus/chat
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@digia.com>2012-11-27 21:52:13 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-11-28 16:21:57 +0100
commit297bc47ad4c0667e23ac7e2df500a8be5cd89ffa (patch)
tree268ce46c4a3a9185c5ce63aa82a736504d5270ae /examples/dbus/chat
parentb19145125e5989937928034d40b0943203513eee (diff)
dbus-chat => chat
to make the source and install locations consistent Change-Id: Ifde8748eeab843b06bf79f941fa4789b5b68cca9 Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
Diffstat (limited to 'examples/dbus/chat')
-rw-r--r--examples/dbus/chat/chat.cpp163
-rw-r--r--examples/dbus/chat/chat.h81
-rw-r--r--examples/dbus/chat/chat.pro16
-rw-r--r--examples/dbus/chat/chatmainwindow.ui185
-rw-r--r--examples/dbus/chat/chatsetnickname.ui149
-rw-r--r--examples/dbus/chat/org.example.chat.xml15
6 files changed, 609 insertions, 0 deletions
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 <QApplication>
+#include <QMessageBox>
+
+#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 <QStringList>
+
+#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 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>ChatMainWindow</class>
+ <widget class="QMainWindow" name="ChatMainWindow" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>600</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>QtDBus Chat</string>
+ </property>
+ <widget class="QWidget" name="centralwidget" >
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>9</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QTextBrowser" name="chatHistory" >
+ <property name="acceptDrops" >
+ <bool>false</bool>
+ </property>
+ <property name="toolTip" >
+ <string>Messages sent and received from other users</string>
+ </property>
+ <property name="acceptRichText" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QLabel" name="label" >
+ <property name="text" >
+ <string>Message:</string>
+ </property>
+ <property name="buddy" >
+ <cstring>messageLineEdit</cstring>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLineEdit" name="messageLineEdit" />
+ </item>
+ <item>
+ <widget class="QPushButton" name="sendButton" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="toolTip" >
+ <string>Sends a message to other people</string>
+ </property>
+ <property name="whatsThis" >
+ <string/>
+ </property>
+ <property name="text" >
+ <string>Send</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QMenuBar" name="menubar" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>31</height>
+ </rect>
+ </property>
+ <widget class="QMenu" name="menuQuit" >
+ <property name="title" >
+ <string>Help</string>
+ </property>
+ <addaction name="actionAboutQt" />
+ </widget>
+ <widget class="QMenu" name="menuFile" >
+ <property name="title" >
+ <string>File</string>
+ </property>
+ <addaction name="actionChangeNickname" />
+ <addaction name="separator" />
+ <addaction name="actionQuit" />
+ </widget>
+ <addaction name="menuFile" />
+ <addaction name="menuQuit" />
+ </widget>
+ <widget class="QStatusBar" name="statusbar" />
+ <action name="actionQuit" >
+ <property name="text" >
+ <string>Quit</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+Q</string>
+ </property>
+ </action>
+ <action name="actionAboutQt" >
+ <property name="text" >
+ <string>About Qt...</string>
+ </property>
+ </action>
+ <action name="actionChangeNickname" >
+ <property name="text" >
+ <string>Change nickname...</string>
+ </property>
+ <property name="shortcut" >
+ <string>Ctrl+N</string>
+ </property>
+ </action>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <tabstops>
+ <tabstop>chatHistory</tabstop>
+ <tabstop>messageLineEdit</tabstop>
+ <tabstop>sendButton</tabstop>
+ </tabstops>
+ <resources/>
+ <connections>
+ <connection>
+ <sender>messageLineEdit</sender>
+ <signal>returnPressed()</signal>
+ <receiver>sendButton</receiver>
+ <slot>animateClick()</slot>
+ <hints>
+ <hint type="sourcelabel" >
+ <x>299</x>
+ <y>554</y>
+ </hint>
+ <hint type="destinationlabel" >
+ <x>744</x>
+ <y>551</y>
+ </hint>
+ </hints>
+ </connection>
+ <connection>
+ <sender>actionQuit</sender>
+ <signal>triggered(bool)</signal>
+ <receiver>ChatMainWindow</receiver>
+ <slot>close()</slot>
+ <hints>
+ <hint type="sourcelabel" >
+ <x>-1</x>
+ <y>-1</y>
+ </hint>
+ <hint type="destinationlabel" >
+ <x>399</x>
+ <y>299</y>
+ </hint>
+ </hints>
+ </connection>
+ </connections>
+</ui>
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 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>NicknameDialog</class>
+ <widget class="QDialog" name="NicknameDialog" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>396</width>
+ <height>105</height>
+ </rect>
+ </property>
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>1</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="windowTitle" >
+ <string>Set nickname</string>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>9</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QLabel" name="label" >
+ <property name="sizePolicy" >
+ <sizepolicy>
+ <hsizetype>1</hsizetype>
+ <vsizetype>1</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text" >
+ <string>New nickname:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLineEdit" name="nickname" />
+ </item>
+ </layout>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>131</width>
+ <height>31</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QPushButton" name="okButton" >
+ <property name="text" >
+ <string>OK</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="cancelButton" >
+ <property name="text" >
+ <string>Cancel</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <resources/>
+ <connections>
+ <connection>
+ <sender>okButton</sender>
+ <signal>clicked()</signal>
+ <receiver>NicknameDialog</receiver>
+ <slot>accept()</slot>
+ <hints>
+ <hint type="sourcelabel" >
+ <x>278</x>
+ <y>253</y>
+ </hint>
+ <hint type="destinationlabel" >
+ <x>96</x>
+ <y>254</y>
+ </hint>
+ </hints>
+ </connection>
+ <connection>
+ <sender>cancelButton</sender>
+ <signal>clicked()</signal>
+ <receiver>NicknameDialog</receiver>
+ <slot>reject()</slot>
+ <hints>
+ <hint type="sourcelabel" >
+ <x>369</x>
+ <y>253</y>
+ </hint>
+ <hint type="destinationlabel" >
+ <x>179</x>
+ <y>282</y>
+ </hint>
+ </hints>
+ </connection>
+ </connections>
+</ui>
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 @@
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
+<node>
+ <interface name="org.example.chat">
+ <signal name="message">
+ <arg name="nickname" type="s" direction="out"/>
+ <arg name="text" type="s" direction="out"/>
+ </signal>
+ <signal name="action">
+ <arg name="nickname" type="s" direction="out"/>
+ <arg name="text" type="s" direction="out"/>
+ </signal>
+ </interface>
+</node>
+