examples/dbus/dbus-chat/chat.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    49 ChatMainWindow::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 
       
    79 ChatMainWindow::~ChatMainWindow()
       
    80 {
       
    81 }
       
    82 
       
    83 void ChatMainWindow::rebuildHistory()
       
    84 {
       
    85     QString history = m_messages.join( QLatin1String("\n" ) );
       
    86     chatHistory->setPlainText(history);
       
    87 }
       
    88 
       
    89 void 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 
       
   100 void 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 
       
   111 void ChatMainWindow::textChangedSlot(const QString &newText)
       
   112 {
       
   113     sendButton->setEnabled(!newText.isEmpty());
       
   114 }
       
   115 
       
   116 void 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 
       
   125 void 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 
       
   135 void ChatMainWindow::aboutQt()
       
   136 {
       
   137     QMessageBox::aboutQt(this);
       
   138 }
       
   139 
       
   140 void ChatMainWindow::exiting()
       
   141 {
       
   142     emit action(m_nickname, QLatin1String("leaves the chat"));
       
   143 }
       
   144 
       
   145 NicknameDialog::NicknameDialog(QWidget *parent)
       
   146     : QDialog(parent)
       
   147 {
       
   148     setupUi(this);
       
   149 }
       
   150 
       
   151 int 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 }