From 023494de0bf16d9b966dbb94af9508f6fd24f9d0 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 28 Aug 2019 14:40:55 +0300 Subject: [PATCH] initial functionality of mucs --- core/account.cpp | 64 ++++++++++++++++++++++++++++++++++--- core/account.h | 1 + core/archive.cpp | 5 +-- core/conference.cpp | 1 + core/rosteritem.cpp | 28 ++++++++++++---- core/rosteritem.h | 3 ++ ui/CMakeLists.txt | 1 + ui/models/contact.cpp | 42 +----------------------- ui/models/contact.h | 5 --- ui/models/item.cpp | 41 ++++++++++++++++++++++++ ui/models/item.h | 6 ++++ ui/models/roster.cpp | 2 +- ui/models/roster.h | 2 +- ui/squawk.cpp | 53 ++++++++++++++++++------------ ui/squawk.h | 1 + ui/widgets/chat.cpp | 34 ++++++++++++++++++++ ui/widgets/chat.h | 6 ++++ ui/widgets/conversation.cpp | 20 +----------- ui/widgets/conversation.h | 5 +-- ui/widgets/messageline.cpp | 55 +++++++++++++++++++++---------- ui/widgets/messageline.h | 2 ++ ui/widgets/room.cpp | 46 ++++++++++++++++++++++++++ ui/widgets/room.h | 43 +++++++++++++++++++++++++ 23 files changed, 347 insertions(+), 119 deletions(-) create mode 100644 ui/widgets/room.cpp create mode 100644 ui/widgets/room.h diff --git a/core/account.cpp b/core/account.cpp index 9e8dfec..1bae04e 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -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(data.getType())); //it is safe here, my type is compatible + RosterItem* ri = 0; std::map::const_iterator itr = contacts.find(data.getPenPalJid()); + if (itr != contacts.end()) { + ri = itr->second; + } else { + std::map::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::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::const_iterator itr = contacts.find(jid); + bool gr = false; if (itr != contacts.end()) { contact = itr->second; } else { std::map::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()); 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); - msg.setType(QXmppMessage::Chat); + 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(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); diff --git a/core/account.h b/core/account.h index 650bf64..d060f4b 100644 --- a/core/account.h +++ b/core/account.h @@ -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; diff --git a/core/archive.cpp b/core/archive.cpp index 1a16895..d38e3c5 100644 --- a/core/archive.cpp +++ b/core/archive.cpp @@ -270,6 +270,7 @@ unsigned int Core::Archive::addElements(const std::list& 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 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 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); diff --git a/core/conference.cpp b/core/conference.cpp index 9daa228..b6e8819 100644 --- a/core/conference.cpp +++ b/core/conference.cpp @@ -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())); diff --git a/core/rosteritem.cpp b/core/rosteritem.cpp index 1e4d7a0..1c9f5e5 100644 --- a/core/rosteritem.cpp +++ b/core/rosteritem.cpp @@ -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->isFromTheBeginning()) { - archiveState = beginning; - } else { - if (archive->size() != 0) { + + if (archive->size() != 0) { + if (archive->isFromTheBeginning()) { + archiveState = beginning; + } else { 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; +} diff --git a/core/rosteritem.h b/core/rosteritem.h index 3e15b9c..e4284af 100644 --- a/core/rosteritem.h +++ b/core/rosteritem.h @@ -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 appendCache; std::list responseCache; std::list> requestCache; + bool muc; private: void nextRequest(); diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index ce7063e..511f985 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -24,6 +24,7 @@ set(squawkUI_SRC models/room.cpp widgets/conversation.cpp widgets/chat.cpp + widgets/room.cpp widgets/messageline.cpp newcontact.cpp ) diff --git a/ui/models/contact.cpp b/ui/models/contact.cpp index b7dcb3a..b8f128f 100644 --- a/ui/models/contact.cpp +++ b/ui/models/contact.cpp @@ -1,6 +1,6 @@ #include "contact.h" + #include -#include "account.h" Models::Contact::Contact(const QString& p_jid ,const QMap &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(p); -} - void Models::Contact::toOfflineState() { emit childIsAboutToBeRemoved(this, 0, childItems.size()); diff --git a/ui/models/contact.h b/ui/models/contact.h index 1e3c2fa..ea8c469 100644 --- a/ui/models/contact.h +++ b/ui/models/contact.h @@ -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(); diff --git a/ui/models/item.cpp b/ui/models/item.cpp index 2f3513b..5251cfb 100644 --- a/ui/models/item.cpp +++ b/ui/models/item.cpp @@ -1,4 +1,7 @@ #include "item.h" +#include "account.h" + +#include Models::Item::Item(Type p_type, const QMap &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(getParentAccount()); + if (acc == 0) { + return ""; + } + return acc->getLogin() + "@" + acc->getServer(); +} + +QString Models::Item::getAccountResource() const +{ + const Account* acc = static_cast(getParentAccount()); + if (acc == 0) { + return ""; + } + return acc->getResource(); +} + +QString Models::Item::getAccountName() const +{ + const Account* acc = static_cast(getParentAccount()); + if (acc == 0) { + return ""; + } + return acc->getName(); +} diff --git a/ui/models/item.h b/ui/models/item.h index 67e6598..2f6fc5e 100644 --- a/ui/models/item.h +++ b/ui/models/item.h @@ -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; diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 6309920..2e62bb5 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -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; diff --git a/ui/models/roster.h b/ui/models/roster.h index 4e394b4..862c5b4 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -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; diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 7cb45e9..ee17ce5 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -213,50 +213,63 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item) if (item.isValid()) { Models::Item* node = static_cast(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(node); + id = new Models::Roster::ElId(contact->getAccountName(), contact->getJid()); break; case Models::Item::presence: contact = static_cast(node->parentItem()); + id = new Models::Roster::ElId(contact->getAccountName(), contact->getJid()); res = node->getName(); break; + case Models::Item::room: + room = static_cast(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(); + conv = itr->second; + } else if (contact != 0) { + created = true; + conv = new Chat(contact); + } else if (room != 0) { + created = true; + conv = new Room(room); + } - if (res.size() > 0) { - itr->second->setPalResource(res); + 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)); } - } else { - Chat* conv = new Chat(contact); - - 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)); conv->show(); + conv->raise(); + conv->activateWindow(); if (res.size() > 0) { conv->setPalResource(res); } } + } } } diff --git a/ui/squawk.h b/ui/squawk.h index 2b49efe..2d655be 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -29,6 +29,7 @@ #include "accounts.h" #include "widgets/chat.h" +#include "widgets/room.h" #include "models/roster.h" #include "newcontact.h" diff --git a/ui/widgets/chat.cpp b/ui/widgets/chat.cpp index 3f20b9f..4822cb6 100644 --- a/ui/widgets/chat.cpp +++ b/ui/widgets/chat.cpp @@ -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); +} + diff --git a/ui/widgets/chat.h b/ui/widgets/chat.h index db57493..ffb10f6 100644 --- a/ui/widgets/chat.h +++ b/ui/widgets/chat.h @@ -32,9 +32,15 @@ class Chat : public Conversation 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(); diff --git a/ui/widgets/conversation.cpp b/ui/widgets/conversation.cpp index 48e35a1..e67f67c 100644 --- a/ui/widgets/conversation.cpp +++ b/ui/widgets/conversation.cpp @@ -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); } } diff --git a/ui/widgets/conversation.h b/ui/widgets/conversation.h index f461c1c..ff1c53b 100644 --- a/ui/widgets/conversation.h +++ b/ui/widgets/conversation.h @@ -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 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(); diff --git a/ui/widgets/messageline.cpp b/ui/widgets/messageline.cpp index f498788..62b808f 100644 --- a/ui/widgets/messageline.cpp +++ b/ui/widgets/messageline.cpp @@ -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,25 +116,40 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg) message->setGraphicsEffect(effect); - if (msg.getOutgoing()) { - //body->setAlignment(Qt::AlignRight); - sender->setAlignment(Qt::AlignRight); - time->setAlignment(Qt::AlignRight); - sender->setText(myName); - hBox->addStretch(); - hBox->addWidget(message); - } else { - QString jid = msg.getFromJid(); - std::map::iterator itr = palNames.find(jid); - if (itr != palNames.end()) { - sender->setText(itr->second); + 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(jid); + sender->setText(msg.getFromResource()); + hBox->addWidget(message); + hBox->addStretch(); + } + } else { + if (msg.getOutgoing()) { + //body->setAlignment(Qt::AlignRight); + sender->setAlignment(Qt::AlignRight); + time->setAlignment(Qt::AlignRight); + sender->setText(myName); + hBox->addStretch(); + hBox->addWidget(message); + } else { + QString jid = msg.getFromJid(); + std::map::iterator itr = palNames.find(jid); + if (itr != palNames.end()) { + sender->setText(itr->second); + } else { + sender->setText(jid); + } + hBox->addWidget(message); + hBox->addStretch(); } - hBox->addWidget(message); - hBox->addStretch(); } - + if (res == end) { layout->addLayout(hBox); } else { diff --git a/ui/widgets/messageline.h b/ui/widgets/messageline.h index a1b4773..efdc34c 100644 --- a/ui/widgets/messageline.h +++ b/ui/widgets/messageline.h @@ -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 palNames; std::deque views; + bool room; }; #endif // MESSAGELINE_H diff --git a/ui/widgets/room.cpp b/ui/widgets/room.cpp new file mode 100644 index 0000000..5ee70ae --- /dev/null +++ b/ui/widgets/room.cpp @@ -0,0 +1,46 @@ +/* + * 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 "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); +} diff --git a/ui/widgets/room.h b/ui/widgets/room.h new file mode 100644 index 0000000..e328f8c --- /dev/null +++ b/ui/widgets/room.h @@ -0,0 +1,43 @@ +/* + * 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 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