Carbon copies basic support

This commit is contained in:
Blue 2019-04-12 18:22:10 +03:00
parent 6e9e100188
commit 48e735b0e9
10 changed files with 215 additions and 52 deletions

View file

@ -19,13 +19,16 @@
#include "conversation.h"
#include "ui_conversation.h"
#include <QDebug>
#include <QScrollBar>
Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
QWidget(parent),
contact(p_contact),
m_ui(new Ui::Conversation),
line(new MessageLine()),
ker()
ker(),
activePalResource(),
thread()
{
m_ui->setupUi(this);
m_ui->splitter->setSizes({300, 0});
@ -47,7 +50,10 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
addMessage(*itr);
}
line->setMyName(p_contact->getAccountName());
m_ui->scrollArea->setWidget(line);
m_ui->scrollArea->verticalScrollBar()->setBackgroundRole(QPalette::Base);
}
Conversation::~Conversation()
@ -61,6 +67,7 @@ void Conversation::setName(const QString& name)
m_ui->nameLabel->setText(getJid());
} else {
m_ui->nameLabel->setText(name);
line->setPalName(getJid(), name);
}
}
@ -101,7 +108,19 @@ void Conversation::onContactChanged(Models::Item* item, int row, int col)
void Conversation::addMessage(const Shared::Message& data)
{
int pos = m_ui->scrollArea->verticalScrollBar()->sliderPosition();
int max = m_ui->scrollArea->verticalScrollBar()->maximum();
line->message(data);
if (pos == max) {
m_ui->scrollArea->verticalScrollBar()->setSliderPosition(m_ui->scrollArea->verticalScrollBar()->maximum());
}
if (!data.getOutgoing()) {
const QString& res = data.getPenPalResource();
if (res.size() > 0) {
setPalResource(res);
}
}
}
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
@ -133,17 +152,33 @@ bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
return QObject::eventFilter(obj, event);
}
QString Conversation::getPalResource() const
{
return activePalResource;
}
void Conversation::setPalResource(const QString& res)
{
activePalResource = res;
}
void Conversation::onEnterPressed()
{
QString body(m_ui->messageEditor->toPlainText());
const QString& aJid = contact->getAccountJid();
m_ui->messageEditor->clear();
Shared::Message msg(Shared::Message::chat);
msg.setFromJid(aJid);
msg.setFromResource(contact->getAccountResource());
msg.setTo(contact->getJid());
msg.setBody(body);
msg.setOutgoing(true);
line->message(msg);
emit sendMessage(msg);
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());
qDebug() << "sending message from " << contact->getAccountResource();
msg.setToJid(contact->getJid());
msg.setToResource(activePalResource);
msg.setBody(body);
msg.setOutgoing(true);
msg.generateRandomId();
line->message(msg);
emit sendMessage(msg);
}
}

View file

@ -52,8 +52,11 @@ public:
QString getJid() const;
QString getAccount() const;
QString getPalResource() const;
void addMessage(const Shared::Message& data);
void setPalResource(const QString& res);
signals:
void sendMessage(const Shared::Message& message);
@ -71,6 +74,8 @@ private:
MessageLine* line;
QScopedPointer<Ui::Conversation> m_ui;
KeyEnterReceiver ker;
QString activePalResource;
QString thread;
};
#endif // CONVERSATION_H

View file

@ -1,6 +1,6 @@
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2019 Юрий Губич <y.gubich@initi.ru>
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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
@ -22,7 +22,9 @@ MessageLine::MessageLine(QWidget* parent):
QWidget(parent),
messageIndex(),
messageOrder(),
layout(new QVBoxLayout())
layout(new QVBoxLayout()),
myName(),
palNames()
{
setLayout(layout);
setBackgroundRole(QPalette::Base);
@ -39,12 +41,11 @@ void MessageLine::message(const Shared::Message& msg)
QHBoxLayout* hBox = new QHBoxLayout();
QWidget* message = new QWidget();
message->setLayout(vBox);
//message->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
message->setBackgroundRole(QPalette::AlternateBase);
message->setAutoFillBackground(true);;
QLabel* body = new QLabel(msg.getBody());
QLabel* sender = new QLabel(msg.getFrom());
QLabel* sender = new QLabel();
QFont f;
f.setBold(true);
sender->setFont(f);
@ -57,9 +58,17 @@ void MessageLine::message(const Shared::Message& msg)
if (msg.getOutgoing()) {
body->setAlignment(Qt::AlignRight);
sender->setAlignment(Qt::AlignRight);
sender->setText(myName);
hBox->addStretch();
hBox->addWidget(message);
} else {
QString jid = msg.getFromJid();
std::map<QString, QString>::iterator itr = palNames.find(jid);
if (itr != palNames.end()) {
sender->setText(itr->second);
} else {
sender->setText(jid);
}
hBox->addWidget(message);
hBox->addStretch();
}
@ -67,3 +76,17 @@ void MessageLine::message(const Shared::Message& msg)
layout->addLayout(hBox);
}
void MessageLine::setMyName(const QString& name)
{
myName = name;
}
void MessageLine::setPalName(const QString& jid, const QString& name)
{
std::map<QString, QString>::iterator itr = palNames.find(jid);
if (itr == palNames.end()) {
palNames.insert(std::make_pair(jid, name));
} else {
itr->second = name;
}
}

View file

@ -1,6 +1,6 @@
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2019 Юрий Губич <y.gubich@initi.ru>
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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
@ -34,12 +34,17 @@ public:
~MessageLine();
void message(const Shared::Message& msg);
void setMyName(const QString& name);
void setPalName(const QString& jid, const QString& name);
private:
typedef W::Order<Shared::Message*> Order;
std::map<QString, Shared::Message*> messageIndex;
Order messageOrder;
QVBoxLayout* layout;
QString myName;
std::map<QString, QString> palNames;
};
#endif // MESSAGELINE_H

View file

@ -156,12 +156,14 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
if (item.isValid()) {
Models::Item* node = static_cast<Models::Item*>(item.internalPointer());
Models::Contact* contact = 0;
QString res;
switch (node->type) {
case Models::Item::contact:
contact = static_cast<Models::Contact*>(node);
break;
case Models::Item::presence:
contact = static_cast<Models::Contact*>(node->parentItem());
res = node->getName();
break;
default:
m_ui->roster->expand(item);
@ -177,6 +179,10 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
itr->second->show();
itr->second->raise();
itr->second->activateWindow();
if (res.size() > 0) {
itr->second->setPalResource(res);
}
} else {
Conversation* conv = new Conversation(contact);
@ -188,6 +194,10 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
rosterModel.dropMessages(account, jid);
conv->show();
if (res.size() > 0) {
itr->second->setPalResource(res);
}
}
}
}
@ -209,11 +219,11 @@ void Squawk::accountMessage(const QString& account, const Shared::Message& data)
const QString& from = data.getPenPalJid();
Conversations::iterator itr = conversations.find({account, from});
if (itr != conversations.end()) {
qDebug() << "adding message";
itr->second->addMessage(data);
} else {
qDebug() << "pending message";
rosterModel.addMessage(account, data);
if (!data.getForwarded()) {
rosterModel.addMessage(account, data);
}
}
}