very bad and basic archive support

This commit is contained in:
Blue 2019-04-13 23:38:20 +03:00
parent 48e735b0e9
commit fad72d8db2
11 changed files with 180 additions and 20 deletions

View file

@ -24,11 +24,12 @@
Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
QWidget(parent),
contact(p_contact),
m_ui(new Ui::Conversation),
line(new MessageLine()),
m_ui(new Ui::Conversation()),
ker(),
activePalResource(),
thread()
thread(),
scroll(nothing)
{
m_ui->setupUi(this);
m_ui->splitter->setSizes({300, 0});
@ -51,9 +52,11 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
}
line->setMyName(p_contact->getAccountName());
connect(line, SIGNAL(resize(int)), this, SLOT(onMessagesResize(int)));
m_ui->scrollArea->setWidget(line);
m_ui->scrollArea->verticalScrollBar()->setBackgroundRole(QPalette::Base);
m_ui->scrollArea->verticalScrollBar()->setAutoFillBackground(true);;
}
Conversation::~Conversation()
@ -110,10 +113,17 @@ void Conversation::addMessage(const Shared::Message& data)
{
int pos = m_ui->scrollArea->verticalScrollBar()->sliderPosition();
int max = m_ui->scrollArea->verticalScrollBar()->maximum();
line->message(data);
MessageLine::Position place = line->message(data);
if (place == MessageLine::invalid) {
return;
}
if (pos == max) {
m_ui->scrollArea->verticalScrollBar()->setSliderPosition(m_ui->scrollArea->verticalScrollBar()->maximum());
if (scroll == nothing) {
if (pos == max) {
scroll = down;
} else if (place != MessageLine::end) { //todo make some better handling of that situation
scroll = keep;
}
}
if (!data.getOutgoing()) {
const QString& res = data.getPenPalResource();
@ -172,13 +182,28 @@ void Conversation::onEnterPressed()
Shared::Message msg(Shared::Message::chat);
msg.setFromJid(aJid);
msg.setFromResource(contact->getAccountResource());
qDebug() << "sending message from " << contact->getAccountResource();
msg.setToJid(contact->getJid());
msg.setToResource(activePalResource);
msg.setBody(body);
msg.setOutgoing(true);
msg.generateRandomId();
line->message(msg);
addMessage(msg);
emit sendMessage(msg);
}
}
void Conversation::onMessagesResize(int amount)
{
switch (scroll) {
case down:
m_ui->scrollArea->verticalScrollBar()->setValue(m_ui->scrollArea->verticalScrollBar()->maximum());
break;
case keep:
m_ui->scrollArea->verticalScrollBar()->setValue(m_ui->scrollArea->verticalScrollBar()->value() - amount);
break;
default:
break;
}
scroll = nothing;
}

View file

@ -68,14 +68,21 @@ protected:
protected slots:
void onContactChanged(Models::Item* item, int row, int col);
void onEnterPressed();
void onMessagesResize(int amount);
private:
enum Scroll {
nothing,
keep,
down
};
Models::Contact* contact;
MessageLine* line;
QScopedPointer<Ui::Conversation> m_ui;
KeyEnterReceiver ker;
QString activePalResource;
QString thread;
Scroll scroll;
};
#endif // CONVERSATION_H

View file

@ -17,6 +17,7 @@
*/
#include "messageline.h"
#include <QDebug>
MessageLine::MessageLine(QWidget* parent):
QWidget(parent),
@ -24,7 +25,8 @@ MessageLine::MessageLine(QWidget* parent):
messageOrder(),
layout(new QVBoxLayout()),
myName(),
palNames()
palNames(),
views()
{
setLayout(layout);
setBackgroundRole(QPalette::Base);
@ -33,10 +35,38 @@ MessageLine::MessageLine(QWidget* parent):
MessageLine::~MessageLine()
{
for (Index::const_iterator itr = messageIndex.begin(), end = messageIndex.end(); itr != end; ++itr) {
delete itr->second;
}
}
void MessageLine::message(const Shared::Message& msg)
MessageLine::Position MessageLine::message(const Shared::Message& msg)
{
QString id = msg.getId();
Index::iterator itr = messageIndex.find(id);
if (itr != messageIndex.end()) {
qDebug() << "received more then one message with the same id, skipping yet the new one";
return invalid;
}
Shared::Message* copy = new Shared::Message(msg);
std::pair<Order::const_iterator, bool> result = messageOrder.insert(std::make_pair(msg.getTime(), copy));
if (!result.second) {
qDebug() << "Error appending a message into a message list - seems like the time of that message exactly matches the time of some other message, can't put them in order, skipping yet";
delete copy;
return invalid;
}
messageIndex.insert(std::make_pair(id, copy));
int index = std::distance<Order::const_iterator>(messageOrder.begin(), result.first); //need to make with binary indexed tree
Position res = invalid;
if (index == 0) {
res = beggining;
} else if (index == messageIndex.size() - 1) {
res = end;
} else {
res = middle;
}
QVBoxLayout* vBox = new QVBoxLayout();
QHBoxLayout* hBox = new QHBoxLayout();
QWidget* message = new QWidget();
@ -73,7 +103,13 @@ void MessageLine::message(const Shared::Message& msg)
hBox->addStretch();
}
layout->addLayout(hBox);
if (res == end) {
layout->addLayout(hBox);
} else {
layout->insertLayout(index, hBox);
}
return res;
}
void MessageLine::setMyName(const QString& name)
@ -90,3 +126,9 @@ void MessageLine::setPalName(const QString& jid, const QString& name)
itr->second = name;
}
}
void MessageLine::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
emit resize(event->size().height() - event->oldSize().height());
}

View file

@ -21,30 +21,52 @@
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QResizeEvent>
#include "../global.h"
#include "../order.h"
class MessageLine : public QWidget
{
Q_OBJECT
public:
enum Position {
beggining,
middle,
end,
invalid
};
MessageLine(QWidget* parent = 0);
~MessageLine();
void message(const Shared::Message& msg);
Position message(const Shared::Message& msg);
void setMyName(const QString& name);
void setPalName(const QString& jid, const QString& name);
signals:
void resize(int amount);
protected:
void resizeEvent(QResizeEvent * event) override;
private:
typedef W::Order<Shared::Message*> Order;
std::map<QString, Shared::Message*> messageIndex;
struct Comparator {
bool operator()(const Shared::Message& a, const Shared::Message& b) const {
return a.getTime() < b.getTime();
}
bool operator()(const Shared::Message* a, const Shared::Message* b) const {
return a->getTime() < b->getTime();
}
};
typedef std::map<QDateTime, Shared::Message*> Order;
typedef std::map<QString, Shared::Message*> Index;
Index messageIndex;
Order messageOrder;
QVBoxLayout* layout;
QString myName;
std::map<QString, QString> palNames;
std::deque<QHBoxLayout*> views;
};
#endif // MESSAGELINE_H

View file

@ -198,6 +198,7 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
if (res.size() > 0) {
itr->second->setPalResource(res);
}
requestArchive(account, jid);
}
}
}

View file

@ -31,6 +31,7 @@ signals:
void disconnectAccount(const QString&);
void changeState(int state);
void sendMessage(const QString& account, const Shared::Message& data);
void requestArchive(const QString& account, const QString& jid);
public slots:
void newAccount(const QMap<QString, QVariant>& account);