diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index cc8af38..ce7063e 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -22,8 +22,9 @@ set(squawkUI_SRC models/presence.cpp models/group.cpp models/room.cpp - conversation.cpp - messageline.cpp + widgets/conversation.cpp + widgets/chat.cpp + widgets/messageline.cpp newcontact.cpp ) diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 0d58d5a..7cb45e9 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -241,7 +241,7 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item) itr->second->setPalResource(res); } } else { - Conversation* conv = new Conversation(contact); + Chat* conv = new Chat(contact); conv->setAttribute(Qt::WA_DeleteOnClose); connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*))); diff --git a/ui/squawk.h b/ui/squawk.h index 55e7593..2b49efe 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -28,7 +28,7 @@ #include #include "accounts.h" -#include "conversation.h" +#include "widgets/chat.h" #include "models/roster.h" #include "newcontact.h" diff --git a/ui/widgets/chat.cpp b/ui/widgets/chat.cpp new file mode 100644 index 0000000..3f20b9f --- /dev/null +++ b/ui/widgets/chat.cpp @@ -0,0 +1,72 @@ +/* + * Squawk messenger. + * Copyright (C) 2019 Yury Gubich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "chat.h" + +Chat::Chat(Models::Contact* p_contact, QWidget* parent): + Conversation(p_contact->getAccountJid(), p_contact->getAccountResource(), p_contact->getJid(), "", p_contact->getAccountName(), parent), + contact(p_contact) +{ + setName(p_contact->getContactName()); + updateState(); + setStatus(p_contact->getStatus()); + + connect(contact, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onContactChanged(Models::Item*, int, int))); + + line->setMyName(p_contact->getAccountName()); + + Models::Contact::Messages deque; + contact->getMessages(deque); + + for (Models::Contact::Messages::const_iterator itr = deque.begin(), end = deque.end(); itr != end; ++itr) { + addMessage(*itr); + } +} + +Chat::~Chat() +{ +} + +void Chat::onContactChanged(Models::Item* item, int row, int col) +{ + if (item == contact) { + switch (col) { + case 0: + setName(contact->getContactName()); + break; + case 3: + updateState(); + break; + case 5: + setStatus(contact->getStatus()); + break; + } + } +} + +void Chat::updateState() +{ + Shared::Availability av = contact->getAvailability(); + statusIcon->setPixmap(Shared::availabilityIcon(av, true).pixmap(40)); + statusIcon->setToolTip(Shared::availabilityNames[av]); +} + +void Chat::setStatus(const QString& status) +{ + statusLabel->setText(status); +} diff --git a/ui/widgets/chat.h b/ui/widgets/chat.h new file mode 100644 index 0000000..db57493 --- /dev/null +++ b/ui/widgets/chat.h @@ -0,0 +1,47 @@ +/* + * Squawk messenger. + * Copyright (C) 2019 Yury Gubich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef CHAT_H +#define CHAT_H + +#include "conversation.h" +#include "../models/contact.h" + +namespace Ui +{ +class Chat; +} +class Chat : public Conversation +{ + Q_OBJECT +public: + Chat(Models::Contact* p_contact, QWidget* parent = 0); + ~Chat(); + +protected slots: + void onContactChanged(Models::Item* item, int row, int col); + +private: + void updateState(); + void setStatus(const QString& status); + +private: + Models::Contact* contact; +}; + +#endif // CHAT_H diff --git a/ui/conversation.cpp b/ui/widgets/conversation.cpp similarity index 81% rename from ui/conversation.cpp rename to ui/widgets/conversation.cpp index 7e320df..48e35a1 100644 --- a/ui/conversation.cpp +++ b/ui/widgets/conversation.cpp @@ -24,14 +24,19 @@ #include #include -Conversation::Conversation(Models::Contact* p_contact, QWidget* parent): +Conversation::Conversation(const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent): QWidget(parent), - contact(p_contact), + myJid(mJid), + myResource(mRes), + palJid(pJid), + activePalResource(pRes), + account(acc), line(new MessageLine()), m_ui(new Ui::Conversation()), ker(), - activePalResource(), thread(), + statusIcon(0), + statusLabel(0), scroll(down), manualSliderChange(false), requestingHistory(false), @@ -41,26 +46,15 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent): m_ui->splitter->setSizes({300, 0}); m_ui->splitter->setStretchFactor(1, 0); - setName(p_contact->getContactName()); - updateState(); - setStatus(p_contact->getStatus()); + statusIcon = m_ui->statusIcon; + statusLabel = m_ui->statusLabel; - connect(contact, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onContactChanged(Models::Item*, int, int))); connect(&ker, SIGNAL(enterPressed()), this, SLOT(onEnterPressed())); connect(m_ui->sendButton, SIGNAL(clicked(bool)), this, SLOT(onEnterPressed())); + connect(line, SIGNAL(resize(int)), this, SLOT(onMessagesResize(int))); //connect(m_ui->attachButton, SIGNAL(clicked(bool)), this, SLOT(onAttach())); m_ui->messageEditor->installEventFilter(&ker); - line->setMyName(p_contact->getAccountName()); - - Models::Contact::Messages deque; - contact->getMessages(deque); - - for (Models::Contact::Messages::const_iterator itr = deque.begin(), end = deque.end(); itr != end; ++itr) { - addMessage(*itr); - } - - connect(line, SIGNAL(resize(int)), this, SLOT(onMessagesResize(int))); QScrollBar* vs = m_ui->scrollArea->verticalScrollBar(); m_ui->scrollArea->setWidget(line); @@ -106,43 +100,14 @@ void Conversation::setName(const QString& name) line->setPalName(getJid(), name); } -void Conversation::updateState() -{ - Shared::Availability av = contact->getAvailability(); - m_ui->statusIcon->setPixmap(Shared::availabilityIcon(av, true).pixmap(40)); - m_ui->statusIcon->setToolTip(Shared::availabilityNames[av]); -} - -void Conversation::setStatus(const QString& status) -{ - m_ui->statusLabel->setText(status); -} - QString Conversation::getAccount() const { - return contact->getAccountName(); + return account; } QString Conversation::getJid() const { - return contact->getJid(); -} - -void Conversation::onContactChanged(Models::Item* item, int row, int col) -{ - if (item == contact) { - switch (col) { - case 0: - setName(contact->getContactName()); - break; - case 3: - updateState(); - break; - case 5: - setStatus(contact->getStatus()); - break; - } - } + return palJid; } void Conversation::addMessage(const Shared::Message& data) @@ -207,12 +172,11 @@ void Conversation::onEnterPressed() QString body(m_ui->messageEditor->toPlainText()); if (body.size() > 0) { - const QString& aJid = contact->getAccountJid(); m_ui->messageEditor->clear(); Shared::Message msg(Shared::Message::chat); - msg.setFromJid(aJid); - msg.setFromResource(contact->getAccountResource()); - msg.setToJid(contact->getJid()); + msg.setFromJid(myJid); + msg.setFromResource(myResource); + msg.setToJid(palJid); msg.setToResource(activePalResource); msg.setBody(body); msg.setOutgoing(true); diff --git a/ui/conversation.h b/ui/widgets/conversation.h similarity index 87% rename from ui/conversation.h rename to ui/widgets/conversation.h index ab000b6..f461c1c 100644 --- a/ui/conversation.h +++ b/ui/widgets/conversation.h @@ -21,8 +21,7 @@ #include #include -#include "../global.h" -#include "models/contact.h" +#include "../../global.h" #include "messageline.h" namespace Ui @@ -47,7 +46,7 @@ class Conversation : public QWidget { Q_OBJECT public: - Conversation(Models::Contact* p_contact, QWidget* parent = 0); + Conversation(const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent = 0); ~Conversation(); QString getJid() const; @@ -65,31 +64,33 @@ signals: void shown(); protected: - void updateState(); - void setStatus(const QString& status); void setName(const QString& name); void applyVisualEffects(); protected slots: - void onContactChanged(Models::Item* item, int row, int col); void onEnterPressed(); void onMessagesResize(int amount); void onSliderValueChanged(int value); void onAttach(); void onFileSelected(); -private: +protected: enum Scroll { nothing, keep, down }; - Models::Contact* contact; + QString myJid; + QString myResource; + QString palJid; + QString activePalResource; + QString account; MessageLine* line; QScopedPointer m_ui; KeyEnterReceiver ker; - QString activePalResource; QString thread; + QLabel* statusIcon; + QLabel* statusLabel; Scroll scroll; bool manualSliderChange; bool requestingHistory; diff --git a/ui/conversation.ui b/ui/widgets/conversation.ui similarity index 100% rename from ui/conversation.ui rename to ui/widgets/conversation.ui diff --git a/ui/messageline.cpp b/ui/widgets/messageline.cpp similarity index 100% rename from ui/messageline.cpp rename to ui/widgets/messageline.cpp diff --git a/ui/messageline.h b/ui/widgets/messageline.h similarity index 100% rename from ui/messageline.h rename to ui/widgets/messageline.h