Refactoring, new messages thread beggining

This commit is contained in:
Blue 2019-04-11 17:58:59 +03:00
parent 5575cff1f5
commit e48444636a
25 changed files with 610 additions and 163 deletions

View file

@ -20,6 +20,7 @@ set(squawkUI_SRC
models/contact.cpp
models/presence.cpp
conversation.cpp
messageline.cpp
)
# Tell CMake to create the helloworld executable

View file

@ -24,6 +24,7 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
QWidget(parent),
contact(p_contact),
m_ui(new Ui::Conversation),
line(new MessageLine()),
ker()
{
m_ui->setupUi(this);
@ -44,6 +45,8 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
for (Models::Contact::Messages::const_iterator itr = deque.begin(), end = deque.end(); itr != end; ++itr) {
addMessage(*itr);
}
m_ui->scrollArea->setWidget(line);
}
Conversation::~Conversation()
@ -95,9 +98,9 @@ void Conversation::onContactChanged(Models::Item* item, int row, int col)
}
}
void Conversation::addMessage(const QMap<QString, QString>& data)
void Conversation::addMessage(const Shared::Message& data)
{
m_ui->dialogBox->append(data.value("from") + ": " + data.value("body"));
line->message(data);
}
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
@ -131,8 +134,13 @@ bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
void Conversation::onEnterPressed()
{
QString msg(m_ui->messageEditor->toPlainText());
QString body(m_ui->messageEditor->toPlainText());
const QString& aJid = contact->getAccountJid();
m_ui->messageEditor->clear();
m_ui->dialogBox->append(contact->getAccountJid() + ": " + msg);
Shared::Message msg(Shared::Message::chat);
msg.setFrom(aJid);
msg.setTo(contact->getJid());
msg.setBody(body);
line->message(msg);
emit sendMessage(msg);
}

View file

@ -23,6 +23,7 @@
#include <QScopedPointer>
#include "../global.h"
#include "models/contact.h"
#include "messageline.h"
namespace Ui
{
@ -51,10 +52,10 @@ public:
QString getJid() const;
QString getAccount() const;
void addMessage(const QMap<QString, QString>& data);
void addMessage(const Shared::Message& data);
signals:
void sendMessage(const QString& message);
void sendMessage(const Shared::Message& message);
protected:
void setState(Shared::Availability state);
@ -67,6 +68,7 @@ protected slots:
private:
Models::Contact* contact;
MessageLine* line;
QScopedPointer<Ui::Conversation> m_ui;
KeyEnterReceiver ker;
};

View file

@ -115,10 +115,46 @@
</layout>
</item>
<item>
<widget class="QTextBrowser" name="dialogBox">
<widget class="QScrollArea" name="scrollArea">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>572</width>
<height>162</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</widget>
</item>
</layout>
@ -130,7 +166,10 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
@ -143,109 +182,86 @@
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0" colspan="2">
<widget class="QWidget" name="widget_3" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="smilesButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="smiley-shape">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="attachButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="document-send-symbolic">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="edit-clear-all">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="sendButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="document-send">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="smilesButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="smiley-shape">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="attachButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="document-send-symbolic">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="edit-clear-all">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="sendButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="document-send">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0" colspan="2">
<item>
<widget class="QPlainTextEdit" name="messageEditor">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">

53
ui/messageline.cpp Normal file
View file

@ -0,0 +1,53 @@
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2019 Юрий Губич <y.gubich@initi.ru>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "messageline.h"
MessageLine::MessageLine(QWidget* parent):
QWidget(parent),
messageIndex(),
messageOrder(),
layout(new QVBoxLayout())
{
setLayout(layout);
setBackgroundRole(QPalette::Base);
}
MessageLine::~MessageLine()
{
}
void MessageLine::message(const Shared::Message& msg)
{
QVBoxLayout* vBox = new QVBoxLayout();
QHBoxLayout* hBox = new QHBoxLayout();
QWidget* message = new QWidget();
message->setLayout(vBox);
QLabel* body = new QLabel(msg.getBody());
QLabel* sender = new QLabel(msg.getFrom());
vBox->addWidget(body);
vBox->addWidget(sender);
hBox->addStretch();
hBox->addWidget(message);
layout->addItem(hBox);
}

45
ui/messageline.h Normal file
View file

@ -0,0 +1,45 @@
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2019 Юрий Губич <y.gubich@initi.ru>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef MESSAGELINE_H
#define MESSAGELINE_H
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include "../global.h"
#include "../order.h"
class MessageLine : public QWidget
{
Q_OBJECT
public:
MessageLine(QWidget* parent = 0);
~MessageLine();
void message(const Shared::Message& msg);
private:
typedef W::Order<Shared::Message*> Order;
std::map<QString, Shared::Message*> messageIndex;
Order messageOrder;
QVBoxLayout* layout;
};
#endif // MESSAGELINE_H

View file

@ -218,9 +218,9 @@ QString Models::Contact::getAccountName() const
return p->getName();
}
void Models::Contact::addMessage(const QMap<QString, QString>& data)
void Models::Contact::addMessage(const Shared::Message& data)
{
const QString& res = data.value("fromResource");
const QString& res = data.getPenPalResource();
if (res.size() > 0) {
QMap<QString, Presence*>::iterator itr = presences.find(res);
if (itr == presences.end()) {
@ -254,7 +254,7 @@ void Models::Contact::dropMessages()
void Models::Contact::getMessages(Models::Contact::Messages& container) const
{
for (Messages::const_iterator itr = messages.begin(), end = messages.end(); itr != end; ++itr) {
const QMap<QString, QString>& msg = *itr;
const Shared::Message& msg = *itr;
container.push_back(msg);
}

View file

@ -14,7 +14,7 @@ class Contact : public Item
{
Q_OBJECT
public:
typedef std::deque<QMap<QString, QString>> Messages;
typedef std::deque<Shared::Message> Messages;
Contact(const QString& p_jid ,const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Contact();
@ -35,7 +35,7 @@ public:
QString getAccountName() const;
QString getAccountJid() const;
void addMessage(const QMap<QString, QString>& data);
void addMessage(const Shared::Message& data);
unsigned int getMessagesCount() const;
void dropMessages();
void getMessages(Messages& container) const;

View file

@ -126,7 +126,7 @@ unsigned int Models::Presence::getMessagesCount() const
return messages.size();
}
void Models::Presence::addMessage(const QMap<QString, QString>& data)
void Models::Presence::addMessage(const Shared::Message& data)
{
messages.emplace_back(data);
changed(4);
@ -152,7 +152,7 @@ QIcon Models::Presence::getStatusIcon() const
void Models::Presence::getMessages(Models::Presence::Messages& container) const
{
for (Messages::const_iterator itr = messages.begin(), end = messages.end(); itr != end; ++itr) {
const QMap<QString, QString>& msg = *itr;
const Shared::Message& msg = *itr;
container.push_back(msg);
}
}

View file

@ -30,7 +30,7 @@ class Presence : public Models::Item
{
Q_OBJECT
public:
typedef std::deque<QMap<QString, QString>> Messages;
typedef std::deque<Shared::Message> Messages;
explicit Presence(const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Presence();
@ -51,7 +51,7 @@ public:
void update(const QString& key, const QVariant& value);
unsigned int getMessagesCount() const;
void dropMessages();
void addMessage(const QMap<QString, QString>& data);
void addMessage(const Shared::Message& data);
void getMessages(Messages& container) const;

View file

@ -493,9 +493,9 @@ void Models::Roster::removePresence(const QString& account, const QString& jid,
}
}
void Models::Roster::addMessage(const QString& account, const QMap<QString, QString>& data)
void Models::Roster::addMessage(const QString& account, const Shared::Message& data)
{
ElId id(account, data.value("from"));
ElId id(account, data.getPenPalJid());
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id);
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(id);

View file

@ -32,7 +32,7 @@ public:
void changeContact(const QString& account, const QString& jid, const QMap<QString, QVariant>& data);
void addPresence(const QString& account, const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
void removePresence(const QString& account, const QString& jid, const QString& name);
void addMessage(const QString& account, const QMap<QString, QString>& data);
void addMessage(const QString& account, const Shared::Message& data);
void dropMessages(const QString& account, const QString& jid);
QVariant data ( const QModelIndex& index, int role ) const override;

View file

@ -182,7 +182,7 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
conv->setAttribute(Qt::WA_DeleteOnClose);
connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*)));
connect(conv, SIGNAL(sendMessage(const QString&)), this, SLOT(onConversationMessage(const QString&)));
connect(conv, SIGNAL(sendMessage(const Shared::Message&)), this, SLOT(onConversationMessage(const Shared::Message&)));
conversations.insert(std::make_pair(id, conv));
rosterModel.dropMessages(account, jid);
@ -204,9 +204,9 @@ void Squawk::onConversationClosed(QObject* parent)
conversations.erase(itr);
}
void Squawk::accountMessage(const QString& account, const QMap<QString, QString>& data)
void Squawk::accountMessage(const QString& account, const Shared::Message& data)
{
const QString& from = data.value("from");
const QString& from = data.getPenPalJid();
Conversations::iterator itr = conversations.find({account, from});
if (itr != conversations.end()) {
qDebug() << "adding message";
@ -217,11 +217,9 @@ void Squawk::accountMessage(const QString& account, const QMap<QString, QString>
}
}
void Squawk::onConversationMessage(const QString& item)
void Squawk::onConversationMessage(const Shared::Message& msg)
{
Conversation* conv = static_cast<Conversation*>(sender());
emit sendMessage(conv->getAccount(), {
{"to", conv->getJid()},
{"body", item}
});
emit sendMessage(conv->getAccount(), msg);
}

View file

@ -30,7 +30,7 @@ signals:
void connectAccount(const QString&);
void disconnectAccount(const QString&);
void changeState(int state);
void sendMessage(const QString& account, const QMap<QString, QString>& data);
void sendMessage(const QString& account, const Shared::Message& data);
public slots:
void newAccount(const QMap<QString, QVariant>& account);
@ -45,7 +45,7 @@ public slots:
void addPresence(const QString& account, const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
void removePresence(const QString& account, const QString& jid, const QString& name);
void stateChanged(int state);
void accountMessage(const QString& account, const QMap<QString, QString>& data);
void accountMessage(const QString& account, const Shared::Message& data);
private:
typedef std::map<Models::Roster::ElId, Conversation*> Conversations;
@ -64,7 +64,7 @@ private slots:
void onConversationClosed(QObject* parent = 0);
void onComboboxActivated(int index);
void onRosterItemDoubleClicked(const QModelIndex& item);
void onConversationMessage(const QString& item);
void onConversationMessage(const Shared::Message& msg);
};