| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "ChatWidget.h" |
|---|
| 21 | |
|---|
| 22 | #include "QuteComChat.h" |
|---|
| 23 | #include "Widget.h" |
|---|
| 24 | |
|---|
| 25 | #include "ui_ChatWidget.h" |
|---|
| 26 | |
|---|
| 27 | #include <coipmanager_threaded/chatsessionmanager/TChatSession.h> |
|---|
| 28 | |
|---|
| 29 | #include <qtcoreutil/KeyEventFilter.h> |
|---|
| 30 | #include <util/SafeConnect.h> |
|---|
| 31 | |
|---|
| 32 | #include <util/SafeDelete.h> |
|---|
| 33 | |
|---|
| 34 | #include <QtGui/QtGui> |
|---|
| 35 | |
|---|
| 36 | ChatWidget::ChatWidget(QuteComChat * qutecomChat, TChatSession * session) |
|---|
| 37 | : QWidget(qutecomChat) { |
|---|
| 38 | |
|---|
| 39 | _qutecomChat = qutecomChat; |
|---|
| 40 | _session = session; |
|---|
| 41 | |
|---|
| 42 | _ui = new Ui::ChatWidget(); |
|---|
| 43 | QWidget * internalWidget = new QWidget(); |
|---|
| 44 | _ui->setupUi(internalWidget); |
|---|
| 45 | |
|---|
| 46 | Widget::createLayout(this); |
|---|
| 47 | layout()->setMargin(10); |
|---|
| 48 | layout()->addWidget(internalWidget); |
|---|
| 49 | |
|---|
| 50 | _ui->contactList->hide(); |
|---|
| 51 | |
|---|
| 52 | SAFE_CONNECT(_ui->sendButton, SIGNAL(clicked()), SLOT(sendClickedSlot())); |
|---|
| 53 | |
|---|
| 54 | _keyEventFilter = new KeyPressEventFilter(this, SLOT(keyPressed(QEvent *))); |
|---|
| 55 | _ui->userEntry->installEventFilter(_keyEventFilter); |
|---|
| 56 | |
|---|
| 57 | SAFE_CONNECT(_session, SIGNAL(messageAddedSignal()), |
|---|
| 58 | SLOT(messageAddedSlot())); |
|---|
| 59 | |
|---|
| 60 | SAFE_CONNECT(_session, SIGNAL(statusMessageReceivedSignal(EnumChatStatusMessage::ChatStatusMessage, std::string)), |
|---|
| 61 | SLOT(statusMessageReceivedSlot(EnumChatStatusMessage::ChatStatusMessage, std::string))); |
|---|
| 62 | |
|---|
| 63 | SAFE_CONNECT(_session, SIGNAL(typingStateChangedSignal(Contact, EnumChatTypingState::ChatTypingState)), |
|---|
| 64 | SLOT(typingStateChangedSlot(Contact, EnumChatTypingState::ChatTypingState))); |
|---|
| 65 | |
|---|
| 66 | messageAddedSlot(); |
|---|
| 67 | |
|---|
| 68 | // We call start for User created Session. |
|---|
| 69 | _session->start(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | ChatWidget::~ChatWidget() { |
|---|
| 73 | OWSAFE_DELETE(_session); |
|---|
| 74 | OWSAFE_DELETE(_keyEventFilter); |
|---|
| 75 | OWSAFE_DELETE(_ui); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void ChatWidget::messageAddedSlot() { |
|---|
| 79 | _ui->history->clear(); |
|---|
| 80 | ChatMessageList list = _session->getMessageHistory(); |
|---|
| 81 | for (ChatMessageList::const_iterator it = list.begin(); |
|---|
| 82 | it != list.end(); |
|---|
| 83 | ++it) { |
|---|
| 84 | QString message = "<font color='grey'>%1</font> - <font color='red'>%2</font>:<br /> %3"; |
|---|
| 85 | message = message.arg(QString::fromStdString((*it).getTime().toString())); |
|---|
| 86 | message = message.arg(QString::fromStdString((*it).getPeer().getPeerId())); |
|---|
| 87 | message = message.arg(QString::fromUtf8((*it).getMessage().c_str())); |
|---|
| 88 | _ui->history->append(message); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void ChatWidget::statusMessageReceivedSlot(EnumChatStatusMessage::ChatStatusMessage status, std::string message) { |
|---|
| 93 | if (status == EnumChatStatusMessage::ChatStatusMessageReceived) { |
|---|
| 94 | _qutecomChat->showStatusBarMessage("Chat message received: " + message); |
|---|
| 95 | } else if (status == EnumChatStatusMessage::ChatStatusMessageError) { |
|---|
| 96 | _qutecomChat->showStatusBarMessage("Chat message sending error: " + message); |
|---|
| 97 | } else if (status == EnumChatStatusMessage::ChatStatusMessageInfo) { |
|---|
| 98 | _qutecomChat->showStatusBarMessage("Chat information message: " + message); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void ChatWidget::typingStateChangedSlot(Contact contact, EnumChatTypingState::ChatTypingState state) { |
|---|
| 103 | if (state == EnumChatTypingState::ChatTypingStateNotTyping) { |
|---|
| 104 | _qutecomChat->showStatusBarMessage(contact.getCompleteName() + "not typing"); |
|---|
| 105 | } else if (state == EnumChatTypingState::ChatTypingStateTyping) { |
|---|
| 106 | _qutecomChat->showStatusBarMessage(contact.getCompleteName() + "typing"); |
|---|
| 107 | } else if (state == EnumChatTypingState::ChatTypingStateStopTyping) { |
|---|
| 108 | _qutecomChat->showStatusBarMessage(contact.getCompleteName() + "stopped typing"); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void ChatWidget::sendClickedSlot() { |
|---|
| 113 | QString message = _ui->userEntry->toPlainText(); |
|---|
| 114 | |
|---|
| 115 | _session->sendMessage(message.toStdString()); |
|---|
| 116 | |
|---|
| 117 | _ui->userEntry->clear(); |
|---|
| 118 | _ui->userEntry->ensureCursorVisible(); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | bool ChatWidget::keyPressed(QEvent * event) { |
|---|
| 122 | QKeyEvent * e = static_cast<QKeyEvent *>(event); |
|---|
| 123 | if ((e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) { |
|---|
| 124 | event->accept(); |
|---|
| 125 | sendClickedSlot(); |
|---|
| 126 | return true; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | return false; |
|---|
| 130 | } |
|---|