source: trunk/examples/dbus/dbus-chat/chat.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "chat.h"
43#include <QtGui/QApplication>
44#include <QtGui/QMessageBox>
45
46#include "chat_adaptor.h"
47#include "chat_interface.h"
48
49ChatMainWindow::ChatMainWindow()
50 : m_nickname(QLatin1String("nickname"))
51{
52 setupUi(this);
53 sendButton->setEnabled(false);
54
55 connect(messageLineEdit, SIGNAL(textChanged(QString)),
56 this, SLOT(textChangedSlot(QString)));
57 connect(sendButton, SIGNAL(clicked(bool)), this, SLOT(sendClickedSlot()));
58 connect(actionChangeNickname, SIGNAL(triggered(bool)), this, SLOT(changeNickname()));
59 connect(actionAboutQt, SIGNAL(triggered(bool)), this, SLOT(aboutQt()));
60 connect(qApp, SIGNAL(lastWindowClosed()), this, SLOT(exiting()));
61
62 // add our D-Bus interface and connect to D-Bus
63 new ChatAdaptor(this);
64 QDBusConnection::sessionBus().registerObject("/", this);
65
66 com::trolltech::chat *iface;
67 iface = new com::trolltech::chat(QString(), QString(), QDBusConnection::sessionBus(), this);
68 //connect(iface, SIGNAL(message(QString,QString)), this, SLOT(messageSlot(QString,QString)));
69 QDBusConnection::sessionBus().connect(QString(), QString(), "com.trolltech.chat", "message", this, SLOT(messageSlot(QString,QString)));
70 connect(iface, SIGNAL(action(QString,QString)), this, SLOT(actionSlot(QString,QString)));
71
72 NicknameDialog dialog;
73 dialog.cancelButton->setVisible(false);
74 dialog.exec();
75 m_nickname = dialog.nickname->text().trimmed();
76 emit action(m_nickname, QLatin1String("joins the chat"));
77}
78
79ChatMainWindow::~ChatMainWindow()
80{
81}
82
83void ChatMainWindow::rebuildHistory()
84{
85 QString history = m_messages.join( QLatin1String("\n" ) );
86 chatHistory->setPlainText(history);
87}
88
89void ChatMainWindow::messageSlot(const QString &nickname, const QString &text)
90{
91 QString msg( QLatin1String("<%1> %2") );
92 msg = msg.arg(nickname, text);
93 m_messages.append(msg);
94
95 if (m_messages.count() > 100)
96 m_messages.removeFirst();
97 rebuildHistory();
98}
99
100void ChatMainWindow::actionSlot(const QString &nickname, const QString &text)
101{
102 QString msg( QLatin1String("* %1 %2") );
103 msg = msg.arg(nickname, text);
104 m_messages.append(msg);
105
106 if (m_messages.count() > 100)
107 m_messages.removeFirst();
108 rebuildHistory();
109}
110
111void ChatMainWindow::textChangedSlot(const QString &newText)
112{
113 sendButton->setEnabled(!newText.isEmpty());
114}
115
116void ChatMainWindow::sendClickedSlot()
117{
118 //emit message(m_nickname, messageLineEdit->text());
119 QDBusMessage msg = QDBusMessage::createSignal("/", "com.trolltech.chat", "message");
120 msg << m_nickname << messageLineEdit->text();
121 QDBusConnection::sessionBus().send(msg);
122 messageLineEdit->setText(QString());
123}
124
125void ChatMainWindow::changeNickname()
126{
127 NicknameDialog dialog(this);
128 if (dialog.exec() == QDialog::Accepted) {
129 QString old = m_nickname;
130 m_nickname = dialog.nickname->text().trimmed();
131 emit action(old, QString("is now known as %1").arg(m_nickname));
132 }
133}
134
135void ChatMainWindow::aboutQt()
136{
137 QMessageBox::aboutQt(this);
138}
139
140void ChatMainWindow::exiting()
141{
142 emit action(m_nickname, QLatin1String("leaves the chat"));
143}
144
145NicknameDialog::NicknameDialog(QWidget *parent)
146 : QDialog(parent)
147{
148 setupUi(this);
149}
150
151int main(int argc, char **argv)
152{
153 QApplication app(argc, argv);
154
155 if (!QDBusConnection::sessionBus().isConnected()) {
156 qWarning("Cannot connect to the D-Bus session bus.\n"
157 "Please check your system settings and try again.\n");
158 return 1;
159 }
160
161 ChatMainWindow chat;
162 chat.show();
163 return app.exec();
164}
Note: See TracBrowser for help on using the repository browser.