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