| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include "chat.h"
|
|---|
| 42 | #include <QtGui/QApplication>
|
|---|
| 43 | #include <QtGui/QMessageBox>
|
|---|
| 44 |
|
|---|
| 45 | #include "chat_adaptor.h"
|
|---|
| 46 | #include "chat_interface.h"
|
|---|
| 47 |
|
|---|
| 48 | ChatMainWindow::ChatMainWindow()
|
|---|
| 49 | : m_nickname(QLatin1String("nickname"))
|
|---|
| 50 | {
|
|---|
| 51 | setupUi(this);
|
|---|
| 52 | sendButton->setEnabled(false);
|
|---|
| 53 |
|
|---|
| 54 | connect(messageLineEdit, SIGNAL(textChanged(QString)),
|
|---|
| 55 | this, SLOT(textChangedSlot(QString)));
|
|---|
| 56 | connect(sendButton, SIGNAL(clicked(bool)), this, SLOT(sendClickedSlot()));
|
|---|
| 57 | connect(actionChangeNickname, SIGNAL(triggered(bool)), this, SLOT(changeNickname()));
|
|---|
| 58 | connect(actionAboutQt, SIGNAL(triggered(bool)), this, SLOT(aboutQt()));
|
|---|
| 59 | connect(qApp, SIGNAL(lastWindowClosed()), this, SLOT(exiting()));
|
|---|
| 60 |
|
|---|
| 61 | // add our D-Bus interface and connect to D-Bus
|
|---|
| 62 | new ChatAdaptor(this);
|
|---|
| 63 | QDBusConnection::sessionBus().registerObject("/", this);
|
|---|
| 64 |
|
|---|
| 65 | com::trolltech::chat *iface;
|
|---|
| 66 | iface = new com::trolltech::chat(QString(), QString(), QDBusConnection::sessionBus(), this);
|
|---|
| 67 | //connect(iface, SIGNAL(message(QString,QString)), this, SLOT(messageSlot(QString,QString)));
|
|---|
| 68 | QDBusConnection::sessionBus().connect(QString(), QString(), "com.trolltech.chat", "message", this, SLOT(messageSlot(QString,QString)));
|
|---|
| 69 | connect(iface, SIGNAL(action(QString,QString)), this, SLOT(actionSlot(QString,QString)));
|
|---|
| 70 |
|
|---|
| 71 | NicknameDialog dialog;
|
|---|
| 72 | dialog.cancelButton->setVisible(false);
|
|---|
| 73 | dialog.exec();
|
|---|
| 74 | m_nickname = dialog.nickname->text().trimmed();
|
|---|
| 75 | emit action(m_nickname, QLatin1String("joins the chat"));
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | ChatMainWindow::~ChatMainWindow()
|
|---|
| 79 | {
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void ChatMainWindow::rebuildHistory()
|
|---|
| 83 | {
|
|---|
| 84 | QString history = m_messages.join( QLatin1String("\n" ) );
|
|---|
| 85 | chatHistory->setPlainText(history);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void ChatMainWindow::messageSlot(const QString &nickname, const QString &text)
|
|---|
| 89 | {
|
|---|
| 90 | QString msg( QLatin1String("<%1> %2") );
|
|---|
| 91 | msg = msg.arg(nickname, text);
|
|---|
| 92 | m_messages.append(msg);
|
|---|
| 93 |
|
|---|
| 94 | if (m_messages.count() > 100)
|
|---|
| 95 | m_messages.removeFirst();
|
|---|
| 96 | rebuildHistory();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void ChatMainWindow::actionSlot(const QString &nickname, const QString &text)
|
|---|
| 100 | {
|
|---|
| 101 | QString msg( QLatin1String("* %1 %2") );
|
|---|
| 102 | msg = msg.arg(nickname, text);
|
|---|
| 103 | m_messages.append(msg);
|
|---|
| 104 |
|
|---|
| 105 | if (m_messages.count() > 100)
|
|---|
| 106 | m_messages.removeFirst();
|
|---|
| 107 | rebuildHistory();
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | void ChatMainWindow::textChangedSlot(const QString &newText)
|
|---|
| 111 | {
|
|---|
| 112 | sendButton->setEnabled(!newText.isEmpty());
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void ChatMainWindow::sendClickedSlot()
|
|---|
| 116 | {
|
|---|
| 117 | //emit message(m_nickname, messageLineEdit->text());
|
|---|
| 118 | QDBusMessage msg = QDBusMessage::createSignal("/", "com.trolltech.chat", "message");
|
|---|
| 119 | msg << m_nickname << messageLineEdit->text();
|
|---|
| 120 | QDBusConnection::sessionBus().send(msg);
|
|---|
| 121 | messageLineEdit->setText(QString());
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void ChatMainWindow::changeNickname()
|
|---|
| 125 | {
|
|---|
| 126 | NicknameDialog dialog(this);
|
|---|
| 127 | if (dialog.exec() == QDialog::Accepted) {
|
|---|
| 128 | QString old = m_nickname;
|
|---|
| 129 | m_nickname = dialog.nickname->text().trimmed();
|
|---|
| 130 | emit action(old, QString("is now known as %1").arg(m_nickname));
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void ChatMainWindow::aboutQt()
|
|---|
| 135 | {
|
|---|
| 136 | QMessageBox::aboutQt(this);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | void ChatMainWindow::exiting()
|
|---|
| 140 | {
|
|---|
| 141 | emit action(m_nickname, QLatin1String("leaves the chat"));
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | NicknameDialog::NicknameDialog(QWidget *parent)
|
|---|
| 145 | : QDialog(parent)
|
|---|
| 146 | {
|
|---|
| 147 | setupUi(this);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | int main(int argc, char **argv)
|
|---|
| 151 | {
|
|---|
| 152 | QApplication app(argc, argv);
|
|---|
| 153 |
|
|---|
| 154 | if (!QDBusConnection::sessionBus().isConnected()) {
|
|---|
| 155 | qWarning("Cannot connect to the D-Bus session bus.\n"
|
|---|
| 156 | "Please check your system settings and try again.\n");
|
|---|
| 157 | return 1;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | ChatMainWindow chat;
|
|---|
| 161 | chat.show();
|
|---|
| 162 | return app.exec();
|
|---|
| 163 | }
|
|---|