initial functionality of mucs

This commit is contained in:
Blue 2019-08-28 14:40:55 +03:00
parent e2cc1bae2e
commit 023494de0b
23 changed files with 347 additions and 119 deletions

View File

@ -436,7 +436,7 @@ void Core::Account::onMessageReceived(const QXmppMessage& msg)
handled = handleChatMessage(msg);
break;
case QXmppMessage::GroupChat:
qDebug() << "received a message with type \"GroupChat\", not sure what to do with it now, skipping";
handled = handleGroupMessage(msg);
break;
case QXmppMessage::Error:
qDebug() << "received a message with type \"Error\", not sure what to do with it now, skipping";
@ -482,11 +482,25 @@ void Core::Account::sendMessage(const Shared::Message& data)
msg.setId(data.getId());
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
RosterItem* ri = 0;
std::map<QString, Contact*>::const_iterator itr = contacts.find(data.getPenPalJid());
if (itr != contacts.end()) {
ri = itr->second;
} else {
std::map<QString, Conference*>::const_iterator ritr = conferences.find(data.getPenPalJid());
if (ritr != conferences.end()) {
ri = ritr->second;
}
}
itr->second->appendMessageToArchive(data);
if (ri != 0) {
if (!ri->isMuc()) {
ri->appendMessageToArchive(data);
}
}
client.sendPacket(msg);
} else {
qDebug() << "An attempt to send message with not connected account " << name << ", skipping";
}
@ -541,6 +555,39 @@ bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bo
return false;
}
bool Core::Account::handleGroupMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing)
{
const QString& body(msg.body());
if (body.size() != 0) {
const QString& id(msg.id());
Shared::Message sMsg(Shared::Message::groupChat);
initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
QString jid = sMsg.getPenPalJid();
std::map<QString, Conference*>::const_iterator itr = conferences.find(jid);
Conference* cnt;
if (itr != conferences.end()) {
cnt = itr->second;
} else {
return false;
}
cnt->appendMessageToArchive(sMsg);
emit message(sMsg);
if (!forwarded && !outgoing) {
if (msg.isReceiptRequested() && id.size() > 0) {
QXmppMessage receipt(getFullJid(), msg.from(), "");
receipt.setReceiptId(id);
client.sendPacket(receipt);
}
}
return true;
}
return false;
}
void Core::Account::initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing, bool forwarded, bool guessing) const
{
const QDateTime& time(source.stamp());
@ -595,17 +642,20 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
qDebug() << "An archive request for " << jid << ", before " << before;
RosterItem* contact = 0;
std::map<QString, Contact*>::const_iterator itr = contacts.find(jid);
bool gr = false;
if (itr != contacts.end()) {
contact = itr->second;
} else {
std::map<QString, Conference*>::const_iterator citr = conferences.find(jid);
if (citr != conferences.end()) {
contact = citr->second;
gr = true;
}
}
if (contact == 0) {
qDebug() << "An attempt to request archive for" << jid << "in account" << name << ", but the contact with such id wasn't found, skipping";
emit responseArchive(contact->jid, std::list<Shared::Message>());
return;
}
@ -613,7 +663,11 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
QXmppMessage msg(getFullJid(), jid, "", "");
QString last = Shared::generateUUID();
msg.setId(last);
if (gr) {
msg.setType(QXmppMessage::GroupChat);
} else {
msg.setType(QXmppMessage::Chat);
}
msg.setState(QXmppMessage::Active);
client.sendPacket(msg);
QTimer* timer = new QTimer;
@ -632,6 +686,7 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
void Core::Account::onContactNeedHistory(const QString& before, const QString& after, const QDateTime& at)
{
RosterItem* contact = static_cast<RosterItem*>(sender());
QString to = "";
QXmppResultSetQuery query;
query.setMax(100);
if (before.size() > 0) {
@ -648,7 +703,7 @@ void Core::Account::onContactNeedHistory(const QString& before, const QString& a
qDebug() << "Remote query from" << after << ", to" << before;
QString q = am->retrieveArchivedMessages("", "", contact->jid, start, QDateTime(), query);
QString q = am->retrieveArchivedMessages(to, "", contact->jid, start, QDateTime(), query);
achiveQueries.insert(std::make_pair(q, contact->jid));
}
@ -939,6 +994,7 @@ void Core::Account::bookmarksReceived(const QXmppBookmarkSet& bookmarks)
QXmppMucRoom* room = mm->addRoom(jid);
QString nick = c.nickName();
Conference* conf = new Conference(jid, getName(), c.autoJoin(), c.name(), nick == "" ? getName() : nick, room);
conferences.insert(std::make_pair(jid, conf));
handleNewConference(conf);

View File

@ -151,6 +151,7 @@ private:
void handleNewRosterItem(RosterItem* contact);
void handleNewConference(Conference* contact);
bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
bool handleGroupMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
void addToGroup(const QString& jid, const QString& group);
void removeFromGroup(const QString& jid, const QString& group);
void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const;

View File

@ -270,6 +270,7 @@ unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messag
if (rc) {
qDebug() << "An element couldn't be inserted into the index, aborting the transaction" << mdb_strerror(rc);
} else {
//qDebug() << "element added with id" << message.getId() << "stamp" << message.getTime();
success++;
}
} else {
@ -321,9 +322,10 @@ std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id
rc = mdb_cursor_open(txn, order, &cursor);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
if (rc) {
qDebug() << "Error getting before " << mdb_strerror(rc) << ", id:" << id;
qDebug() << "Error getting before" << mdb_strerror(rc) << ", id:" << id;
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw Empty(jid.toStdString());
}
} else {
@ -334,7 +336,6 @@ std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id
if (rc) {
qDebug() <<"Error getting before: no reference message" << mdb_strerror(rc) << ", id:" << id;
mdb_txn_abort(txn);
printKeys();
throw NotFound(stdId, jid.toStdString());
} else {
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);

View File

@ -27,6 +27,7 @@ Core::Conference::Conference(const QString& p_jid, const QString& p_account, boo
joined(false),
autoJoin(p_autoJoin)
{
muc = true;
name = p_name;
connect(room, SIGNAL(joined()), this, SLOT(onRoomJoined()));

View File

@ -32,13 +32,15 @@ Core::RosterItem::RosterItem(const QString& pJid, const QString& account, QObjec
hisoryCache(),
appendCache(),
responseCache(),
requestCache()
requestCache(),
muc(false)
{
archive->open(account);
if (archive->size() != 0) {
if (archive->isFromTheBeginning()) {
archiveState = beginning;
} else {
if (archive->size() != 0) {
archiveState = chunk;
}
}
@ -231,7 +233,7 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
case beginning:
if (finished) {
archiveState = complete;
archive->addElements(appendCache);
added += archive->addElements(appendCache);
appendCache.clear();
nextRequest();
} else {
@ -241,7 +243,7 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
case chunk:
if (finished) {
archiveState = end;
archive->addElements(appendCache);
added += archive->addElements(appendCache);
appendCache.clear();
nextRequest();
} else {
@ -252,6 +254,8 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
wasEmpty = true;
archiveState = end;
case end:
added += archive->addElements(appendCache);
appendCache.clear();
if (finished && (added > 0 || !wasEmpty)) {
archiveState = complete;
archive->setFromTheBeginning(true);
@ -317,3 +321,13 @@ void Core::RosterItem::requestFromEmpty(int count, const QString& before)
}
}
QString Core::RosterItem::getServer() const
{
QStringList lst = jid.split("@");
return lst.back();
}
bool Core::RosterItem::isMuc() const
{
return muc;
}

View File

@ -50,6 +50,8 @@ public:
ArchiveState getArchiveState() const;
QString getName() const;
void setName(const QString& n);
QString getServer() const;
bool isMuc() const;
void addMessageToArchive(const Shared::Message& msg);
void appendMessageToArchive(const Shared::Message& msg);
@ -78,6 +80,7 @@ protected:
std::list<Shared::Message> appendCache;
std::list<Shared::Message> responseCache;
std::list<std::pair<int, QString>> requestCache;
bool muc;
private:
void nextRequest();

View File

@ -24,6 +24,7 @@ set(squawkUI_SRC
models/room.cpp
widgets/conversation.cpp
widgets/chat.cpp
widgets/room.cpp
widgets/messageline.cpp
newcontact.cpp
)

View File

@ -1,6 +1,6 @@
#include "contact.h"
#include <QDebug>
#include "account.h"
Models::Contact::Contact(const QString& p_jid ,const QMap<QString, QVariant> &data, Item *parentItem):
Item(Item::contact, data, parentItem),
@ -227,16 +227,6 @@ QIcon Models::Contact::getStatusIcon(bool big) const
}
}
QString Models::Contact::getAccountName() const
{
const Account* acc = getParentAccount();
if (acc == 0) {
qDebug() << "An attempt to request account name of the contact " << jid << " but the parent account wasn't found, returning empty string";
return "";
}
return acc->getName();
}
void Models::Contact::addMessage(const Shared::Message& data)
{
const QString& res = data.getPenPalResource();
@ -291,36 +281,6 @@ void Models::Contact::getMessages(Models::Contact::Messages& container) const
}
}
QString Models::Contact::getAccountJid() const
{
const Account* acc = getParentAccount();
if (acc == 0) {
qDebug() << "An attempt to request account jid of the contact " << jid << " but the parent account wasn't found, returning empty string";
return "";
}
return acc->getLogin() + "@" + acc->getServer();
}
QString Models::Contact::getAccountResource() const
{
const Account* acc = getParentAccount();
if (acc == 0) {
qDebug() << "An attempt to request account resource of the contact " << jid << " but the parent account wasn't found, returning empty string";
return "";
}
return acc->getResource();
}
const Models::Account * Models::Contact::getParentAccount() const
{
const Item* p = this;
do {
p = p->parentItemConst();
} while (p != 0 && p->type != Item::account);
return static_cast<const Account*>(p);
}
void Models::Contact::toOfflineState()
{
emit childIsAboutToBeRemoved(this, 0, childItems.size());

View File

@ -10,7 +10,6 @@
namespace Models {
class Account;
class Contact : public Item
{
Q_OBJECT
@ -33,9 +32,6 @@ public:
void removePresence(const QString& name);
void appendChild(Models::Item * child) override;
QString getAccountName() const;
QString getAccountJid() const;
QString getAccountResource() const;
QString getContactName() const;
QString getStatus() const;
@ -46,7 +42,6 @@ public:
protected:
void _removeChild(int index) override;
const Account* getParentAccount() const;
protected slots:
void refresh();

View File

@ -1,4 +1,7 @@
#include "item.h"
#include "account.h"
#include <QDebug>
Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_parent):
QObject(),
@ -153,3 +156,41 @@ void Models::Item::toOfflineState()
it->toOfflineState();
}
}
const Models::Item * Models::Item::getParentAccount() const
{
const Item* p = this;
while (p != 0 && p->type != Item::account) {
p = p->parentItemConst();
}
return p;
}
QString Models::Item::getAccountJid() const
{
const Account* acc = static_cast<const Account*>(getParentAccount());
if (acc == 0) {
return "";
}
return acc->getLogin() + "@" + acc->getServer();
}
QString Models::Item::getAccountResource() const
{
const Account* acc = static_cast<const Account*>(getParentAccount());
if (acc == 0) {
return "";
}
return acc->getResource();
}
QString Models::Item::getAccountName() const
{
const Account* acc = static_cast<const Account*>(getParentAccount());
if (acc == 0) {
return "";
}
return acc->getName();
}

View File

@ -47,11 +47,17 @@ class Item : public QObject{
Item *parentItem();
const Item *parentItemConst() const;
QString getAccountName() const;
QString getAccountJid() const;
QString getAccountResource() const;
const Type type;
protected:
virtual void changed(int col);
virtual void _removeChild(int index);
const Item* getParentAccount() const;
protected:
QString name;

View File

@ -184,7 +184,7 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
if (count > 0) {
str += QString("New messages: ") + std::to_string(count).c_str() + "\n";
}
str += QString("Subscription: ") + rm->getStatusText() + "\n";
str += QString("Subscription: ") + rm->getStatusText();
result = str;
}
break;

View File

@ -72,7 +72,7 @@ private slots:
public:
class ElId {
public:
ElId (const QString& p_account, const QString& p_name);
ElId (const QString& p_account = "", const QString& p_name = "");
const QString account;
const QString name;

View File

@ -213,50 +213,63 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
if (item.isValid()) {
Models::Item* node = static_cast<Models::Item*>(item.internalPointer());
Models::Contact* contact = 0;
Models::Room* room = 0;
QString res;
Models::Roster::ElId* id = 0;
switch (node->type) {
case Models::Item::contact:
contact = static_cast<Models::Contact*>(node);
id = new Models::Roster::ElId(contact->getAccountName(), contact->getJid());
break;
case Models::Item::presence:
contact = static_cast<Models::Contact*>(node->parentItem());
id = new Models::Roster::ElId(contact->getAccountName(), contact->getJid());
res = node->getName();
break;
case Models::Item::room:
room = static_cast<Models::Room*>(node);
id = new Models::Roster::ElId(room->getAccountName(), room->getJid());
break;
default:
m_ui->roster->expand(item);
break;
}
if (contact != 0) {
QString jid = contact->getJid();
QString account = contact->getAccountName();
Models::Roster::ElId id(account, jid);
Conversations::const_iterator itr = conversations.find(id);
if (id != 0) {
Conversations::const_iterator itr = conversations.find(*id);
Conversation* conv = 0;
bool created = false;
if (itr != conversations.end()) {
itr->second->show();
itr->second->raise();
itr->second->activateWindow();
if (res.size() > 0) {
itr->second->setPalResource(res);
conv = itr->second;
} else if (contact != 0) {
created = true;
conv = new Chat(contact);
} else if (room != 0) {
created = true;
conv = new Room(room);
}
} else {
Chat* conv = new Chat(contact);
if (conv != 0) {
if (created) {
conv->setAttribute(Qt::WA_DeleteOnClose);
connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*)));
connect(conv, SIGNAL(sendMessage(const Shared::Message&)), this, SLOT(onConversationMessage(const Shared::Message&)));
connect(conv, SIGNAL(requestArchive(const QString&)), this, SLOT(onConversationRequestArchive(const QString&)));
connect(conv, SIGNAL(shown()), this, SLOT(onConversationShown()));
conversations.insert(std::make_pair(id, conv));
conversations.insert(std::make_pair(*id, conv));
}
conv->show();
conv->raise();
conv->activateWindow();
if (res.size() > 0) {
conv->setPalResource(res);
}
}
}
}
}

View File

@ -29,6 +29,7 @@
#include "accounts.h"
#include "widgets/chat.h"
#include "widgets/room.h"
#include "models/roster.h"
#include "newcontact.h"

View File

@ -70,3 +70,37 @@ void Chat::setStatus(const QString& status)
{
statusLabel->setText(status);
}
void Chat::handleSendMessage(const QString& text)
{
Shared::Message msg(Shared::Message::chat);
msg.setFromJid(myJid);
msg.setFromResource(myResource);
msg.setToJid(palJid);
msg.setToResource(activePalResource);
msg.setBody(text);
msg.setOutgoing(true);
msg.generateRandomId();
msg.setCurrentTime();
addMessage(msg);
emit sendMessage(msg);
}
void Chat::addMessage(const Shared::Message& data)
{
Conversation::addMessage(data);
if (!data.getOutgoing()) { //TODO need to check if that was the last message
const QString& res = data.getPenPalResource();
if (res.size() > 0) {
setPalResource(res);
}
}
}
void Chat::setName(const QString& name)
{
Conversation::setName(name);
line->setPalName(getJid(), name);
}

View File

@ -33,8 +33,14 @@ public:
Chat(Models::Contact* p_contact, QWidget* parent = 0);
~Chat();
void addMessage(const Shared::Message & data) override;
protected slots:
void onContactChanged(Models::Item* item, int row, int col);
void handleSendMessage(const QString & text) override;
protected:
void setName(const QString & name) override;
private:
void updateState();

View File

@ -97,7 +97,6 @@ void Conversation::setName(const QString& name)
{
m_ui->nameLabel->setText(name);
setWindowTitle(name);
line->setPalName(getJid(), name);
}
QString Conversation::getAccount() const
@ -119,13 +118,6 @@ void Conversation::addMessage(const Shared::Message& data)
if (place == MessageLine::invalid) {
return;
}
if (!data.getOutgoing()) {
const QString& res = data.getPenPalResource();
if (res.size() > 0) {
setPalResource(res);
}
}
}
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
@ -173,17 +165,7 @@ void Conversation::onEnterPressed()
if (body.size() > 0) {
m_ui->messageEditor->clear();
Shared::Message msg(Shared::Message::chat);
msg.setFromJid(myJid);
msg.setFromResource(myResource);
msg.setToJid(palJid);
msg.setToResource(activePalResource);
msg.setBody(body);
msg.setOutgoing(true);
msg.generateRandomId();
msg.setCurrentTime();
addMessage(msg);
emit sendMessage(msg);
handleSendMessage(body);
}
}

View File

@ -52,7 +52,7 @@ public:
QString getJid() const;
QString getAccount() const;
QString getPalResource() const;
void addMessage(const Shared::Message& data);
virtual void addMessage(const Shared::Message& data);
void setPalResource(const QString& res);
void responseArchive(const std::list<Shared::Message> list);
@ -64,8 +64,9 @@ signals:
void shown();
protected:
void setName(const QString& name);
virtual void setName(const QString& name);
void applyVisualEffects();
virtual void handleSendMessage(const QString& text) = 0;
protected slots:
void onEnterPressed();

View File

@ -30,7 +30,8 @@ MessageLine::MessageLine(QWidget* parent):
layout(new QVBoxLayout()),
myName(),
palNames(),
views()
views(),
room(false)
{
setLayout(layout);
setBackgroundRole(QPalette::Base);
@ -44,6 +45,11 @@ MessageLine::~MessageLine()
}
}
void MessageLine::setRoom(bool p_room)
{
room = p_room;
}
MessageLine::Position MessageLine::message(const Shared::Message& msg)
{
QString id = msg.getId();
@ -110,6 +116,20 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg)
message->setGraphicsEffect(effect);
if (room) {
if (msg.getFromResource() == myName) {
//body->setAlignment(Qt::AlignRight);
sender->setAlignment(Qt::AlignRight);
time->setAlignment(Qt::AlignRight);
sender->setText(myName);
hBox->addStretch();
hBox->addWidget(message);
} else {
sender->setText(msg.getFromResource());
hBox->addWidget(message);
hBox->addStretch();
}
} else {
if (msg.getOutgoing()) {
//body->setAlignment(Qt::AlignRight);
sender->setAlignment(Qt::AlignRight);
@ -128,6 +148,7 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg)
hBox->addWidget(message);
hBox->addStretch();
}
}
if (res == end) {
layout->addLayout(hBox);

View File

@ -45,6 +45,7 @@ public:
QString firstMessageId() const;
void showBusyIndicator();
void hideBusyIndicator();
void setRoom(bool p_room);
signals:
void resize(int amount);
@ -70,6 +71,7 @@ private:
QString myName;
std::map<QString, QString> palNames;
std::deque<QHBoxLayout*> views;
bool room;
};
#endif // MESSAGELINE_H

46
ui/widgets/room.cpp Normal file
View File

@ -0,0 +1,46 @@
/*
* Squawk messenger.
* 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
* 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 "room.h"
Room::Room(Models::Room* p_room, QWidget* parent):
Conversation(p_room->getAccountJid(), p_room->getAccountResource(), p_room->getJid(), "", p_room->getAccountName(), parent),
room(p_room)
{
setName(p_room->getName());
line->setMyName(room->getNick());
line->setRoom(true);
}
Room::~Room()
{
}
void Room::handleSendMessage(const QString& text)
{
Shared::Message msg(Shared::Message::groupChat);
msg.setFromJid(myJid);
msg.setFromResource(myResource);
msg.setToJid(palJid);
//msg.setToResource(activePalResource);
msg.setBody(text);
msg.setOutgoing(true);
msg.generateRandomId();
msg.setCurrentTime();
emit sendMessage(msg);
}

43
ui/widgets/room.h Normal file
View File

@ -0,0 +1,43 @@
/*
* Squawk messenger.
* 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
* 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 ROOM_H
#define ROOM_H
#include "conversation.h"
#include "../models/room.h"
/**
* @todo write docs
*/
class Room : public Conversation
{
Q_OBJECT
public:
Room(Models::Room* p_room, QWidget* parent = 0);
~Room();
protected:
void handleSendMessage(const QString & text) override;
private:
Models::Room* room;
};
#endif // ROOM_H