From e48444636ab27fe80c667948877cde19ba1cf4ee Mon Sep 17 00:00:00 2001 From: blue Date: Thu, 11 Apr 2019 17:58:59 +0300 Subject: [PATCH] Refactoring, new messages thread beggining --- CMakeLists.txt | 2 + core/account.cpp | 33 +++--- core/account.h | 4 +- core/squawk.cpp | 6 +- core/squawk.h | 6 +- exception.cpp | 14 +++ exception.h | 22 ++++ global.cpp | 125 +++++++++++++++++++++++ global.h | 30 ++++++ main.cpp | 6 +- order.h | 136 +++++++++++++++++++++++++ ui/CMakeLists.txt | 1 + ui/conversation.cpp | 16 ++- ui/conversation.h | 6 +- ui/conversation.ui | 224 ++++++++++++++++++++++------------------- ui/messageline.cpp | 53 ++++++++++ ui/messageline.h | 45 +++++++++ ui/models/contact.cpp | 6 +- ui/models/contact.h | 4 +- ui/models/presence.cpp | 4 +- ui/models/presence.h | 4 +- ui/models/roster.cpp | 4 +- ui/models/roster.h | 2 +- ui/squawk.cpp | 14 ++- ui/squawk.h | 6 +- 25 files changed, 610 insertions(+), 163 deletions(-) create mode 100644 exception.cpp create mode 100644 exception.h create mode 100644 global.cpp create mode 100644 order.h create mode 100644 ui/messageline.cpp create mode 100644 ui/messageline.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b3f5269..0f51811 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ set(CMAKE_AUTOUIC ON) find_package(Qt5Widgets CONFIG REQUIRED) set(squawk_SRC main.cpp + global.cpp + exception.cpp signalcatcher.cpp ) diff --git a/core/account.cpp b/core/account.cpp index 8c27106..94ede01 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -336,14 +336,7 @@ void Core::Account::setResource(const QString& p_resource) void Core::Account::onMessageReceived(const QXmppMessage& msg) { QString from = msg.from(); - QStringList fcomps = from.split("/"); - QString fjid = fcomps.front(); - QString fresource = fcomps.back(); - QString to = msg.to(); - QStringList tcomps = to.split("/"); - QString tjid = tcomps.front(); - QString tresource = tcomps.back(); bool handled = false; switch (msg.type()) { case QXmppMessage::Normal: @@ -353,14 +346,16 @@ void Core::Account::onMessageReceived(const QXmppMessage& msg) QString body(msg.body()); if (body.size() != 0) { QString id(msg.id()); - emit message({ - {"body", body}, - {"from", fjid}, - {"to", tjid}, - {"fromResource", fresource}, - {"toResource", tresource}, - {"id", id} - }); + QDateTime time(msg.stamp()); + Shared::Message sMsg(Shared::Message::chat); + sMsg.setId(id); + sMsg.setFrom(from); + sMsg.setTo(to); + sMsg.setBody(body); + if (time.isValid()) { + sMsg.setTime(time); + } + emit message(sMsg); if (msg.isReceiptRequested() && id.size() > 0) { QXmppMessage receipt(getFullJid(), from, ""); @@ -384,8 +379,8 @@ void Core::Account::onMessageReceived(const QXmppMessage& msg) if (!handled) { qDebug() << "Message wasn't handled: "; - qDebug() << "- from: " << msg.from(); - qDebug() << "- to: " << msg.to(); + qDebug() << "- from: " << from; + qDebug() << "- to: " << to; qDebug() << "- body: " << msg.body(); qDebug() << "- type: " << msg.type(); qDebug() << "- state: " << msg.state(); @@ -406,10 +401,10 @@ QString Core::Account::getFullJid() const return getLogin() + "@" + getServer() + "/" + getResource(); } -void Core::Account::sendMessage(const QMap& data) +void Core::Account::sendMessage(const Shared::Message& data) { if (state == Shared::connected) { - client.sendMessage(data.value("to"), data.value("body")); + client.sendMessage(data.getTo(), data.getBody()); } else { qDebug() << "An attempt to send message with not connected account " << name << ", skipping"; } diff --git a/core/account.h b/core/account.h index a0fac23..322c351 100644 --- a/core/account.h +++ b/core/account.h @@ -36,7 +36,7 @@ public: void setResource(const QString& p_resource); void setAvailability(Shared::Availability avail); QString getFullJid() const; - void sendMessage(const QMap& data); + void sendMessage(const Shared::Message& data); signals: void connectionStateChanged(int); @@ -49,7 +49,7 @@ signals: void changeContact(const QString& jid, const QMap& data); void addPresence(const QString& jid, const QString& name, const QMap& data); void removePresence(const QString& jid, const QString& name); - void message(const QMap& data); + void message(const Shared::Message& data); private: QString name; diff --git a/core/squawk.cpp b/core/squawk.cpp index 3d62939..f7daaa4 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -86,7 +86,7 @@ void Core::Squawk::addAccount(const QString& login, const QString& server, const connect(acc, SIGNAL(addPresence(const QString&, const QString&, const QMap&)), this, SLOT(onAccountAddPresence(const QString&, const QString&, const QMap&))); connect(acc, SIGNAL(removePresence(const QString&, const QString&)), this, SLOT(onAccountRemovePresence(const QString&, const QString&))); - connect(acc, SIGNAL(message(const QMap&)), this, SLOT(onAccountMessage(const QMap&))); + connect(acc, SIGNAL(message(const Shared::Message&)), this, SLOT(onAccountMessage(const Shared::Message&))); QMap map = { {"login", login}, @@ -194,13 +194,13 @@ void Core::Squawk::onAccountAvailabilityChanged(int state) emit accountAvailabilityChanged(acc->getName(), state); } -void Core::Squawk::onAccountMessage(const QMap& data) +void Core::Squawk::onAccountMessage(const Shared::Message& data) { Account* acc = static_cast(sender()); emit accountMessage(acc->getName(), data); } -void Core::Squawk::sendMessage(const QString& account, const QMap& data) +void Core::Squawk::sendMessage(const QString& account, const Shared::Message& data) { AccountsMap::const_iterator itr = amap.find(account); if (itr == amap.end()) { diff --git a/core/squawk.h b/core/squawk.h index 9f364a9..25aa22c 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -35,7 +35,7 @@ signals: void addPresence(const QString& account, const QString& jid, const QString& name, const QMap& data); void removePresence(const QString& account, const QString& jid, const QString& name); void stateChanged(int state); - void accountMessage(const QString& account, const QMap& data); + void accountMessage(const QString& account, const Shared::Message& data); public slots: void start(); @@ -44,7 +44,7 @@ public slots: void connectAccount(const QString& account); void disconnectAccount(const QString& account); void changeState(int state); - void sendMessage(const QString& account, const QMap& data); + void sendMessage(const QString& account, const Shared::Message& data); private: typedef std::deque Accounts; @@ -68,7 +68,7 @@ private slots: void onAccountChangeContact(const QString& jid, const QMap& data); void onAccountAddPresence(const QString& jid, const QString& name, const QMap& data); void onAccountRemovePresence(const QString& jid, const QString& name); - void onAccountMessage(const QMap& data); + void onAccountMessage(const Shared::Message& data); }; } diff --git a/exception.cpp b/exception.cpp new file mode 100644 index 0000000..92b9f6e --- /dev/null +++ b/exception.cpp @@ -0,0 +1,14 @@ +#include "exception.h" + +Utils::Exception::Exception() +{ +} + +Utils::Exception::~Exception() +{ +} + +const char* Utils::Exception::what() const noexcept( true ) +{ + return getMessage().c_str(); +} \ No newline at end of file diff --git a/exception.h b/exception.h new file mode 100644 index 0000000..205c165 --- /dev/null +++ b/exception.h @@ -0,0 +1,22 @@ +#ifndef EXCEPTION_H +#define EXCEPTION_H + +#include +#include + +namespace Utils +{ + class Exception: + public std::exception + { + public: + Exception(); + virtual ~Exception(); + + virtual std::string getMessage() const = 0; + + const char* what() const noexcept( true ); + }; +} + +#endif // EXCEPTION_H diff --git a/global.cpp b/global.cpp new file mode 100644 index 0000000..a4fdf7a --- /dev/null +++ b/global.cpp @@ -0,0 +1,125 @@ +#include "global.h" + +Shared::Message::Message(Shared::Message::Type p_type): + jFrom(), + rFrom(), + jTo(), + rTo(), + id(), + body(), + time(), + type(p_type) +{ +} + +Shared::Message::Message(): + jFrom(), + rFrom(), + jTo(), + rTo(), + id(), + body(), + time(), + type(Message::normal) +{ +} + +QString Shared::Message::getBody() const +{ + return body; +} + +QString Shared::Message::getFrom() const +{ + QString from = jFrom; + if (rFrom.size() > 0) { + from += "/" + rFrom; + } + return from; +} + +QString Shared::Message::getTo() const +{ + QString to = jTo; + if (rTo.size() > 0) { + to += "/" + rTo; + } + return to; +} + +QString Shared::Message::getId() const +{ + return id; +} + +QDateTime Shared::Message::getTime() const +{ + return time; +} + +void Shared::Message::setBody(const QString& p_body) +{ + body = p_body; +} + +void Shared::Message::setFrom(const QString& from) +{ + QStringList list = from.split("/"); + if (list.size() == 1) { + jFrom = from; + } else { + jFrom = list.front(); + rFrom = list.back(); + } +} + +void Shared::Message::setTo(const QString& to) +{ + QStringList list = to.split("/"); + if (list.size() == 1) { + jTo = to; + } else { + jTo = list.front(); + rTo = list.back(); + } +} + +void Shared::Message::setId(const QString& p_id) +{ + id = p_id; +} + +void Shared::Message::setTime(const QDateTime& p_time) +{ + time = p_time; +} + +QString Shared::Message::getFromJid() const +{ + return jFrom; +} + +QString Shared::Message::getFromResource() const +{ + return rFrom; +} + +QString Shared::Message::getToJid() const +{ + return jTo; +} + +QString Shared::Message::getToResource() const +{ + return rTo; +} + +QString Shared::Message::getPenPalJid() const +{ + return jFrom; +} + +QString Shared::Message::getPenPalResource() const +{ + return rFrom; +} diff --git a/global.h b/global.h index 2e0fe27..f3d3e1e 100644 --- a/global.h +++ b/global.h @@ -54,14 +54,44 @@ static const std::deque subscriptionStateThemeIcons = {"edit-none", "ar class Message { public: + enum Type { + error, + normal, + chat, + groupChat, + headline + }; + Message(Type p_type); Message(); + + void setFrom(const QString& from); + void setTo(const QString& to); + void setTime(const QDateTime& p_time); + void setId(const QString& p_id); + void setBody(const QString& p_body); + + QString getFrom() const; + QString getFromJid() const; + QString getFromResource() const; + QString getTo() const; + QString getToJid() const; + QString getToResource() const; + QDateTime getTime() const; + QString getId() const; + QString getBody() const; + + QString getPenPalJid() const; + QString getPenPalResource() const; private: QString jFrom; QString rFrom; QString jTo; QString rTo; + QString id; + QString body; QDateTime time; + Type type; }; }; diff --git a/main.cpp b/main.cpp index 8b094b0..2944202 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,7 @@ int main(int argc, char *argv[]) { - qRegisterMetaType>("QMap"); + qRegisterMetaType("Shared::Message"); QApplication app(argc, argv); SignalCatcher sc(&app); @@ -34,7 +34,7 @@ int main(int argc, char *argv[]) QObject::connect(&w, SIGNAL(connectAccount(const QString&)), squawk, SLOT(connectAccount(const QString&))); QObject::connect(&w, SIGNAL(disconnectAccount(const QString&)), squawk, SLOT(disconnectAccount(const QString&))); QObject::connect(&w, SIGNAL(changeState(int)), squawk, SLOT(changeState(int))); - QObject::connect(&w, SIGNAL(sendMessage(const QString&, const QMap&)), squawk, SLOT(sendMessage(const QString&, const QMap&))); + QObject::connect(&w, SIGNAL(sendMessage(const QString&, const Shared::Message&)), squawk, SLOT(sendMessage(const QString&, const Shared::Message&))); QObject::connect(squawk, SIGNAL(newAccount(const QMap&)), &w, SLOT(newAccount(const QMap&))); QObject::connect(squawk, SIGNAL(accountAvailabilityChanged(const QString&, int)), &w, SLOT(accountAvailabilityChanged(const QString&, int))); @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) &w, SLOT(addPresence(const QString&, const QString&, const QString&, const QMap&))); QObject::connect(squawk, SIGNAL(removePresence(const QString&, const QString&, const QString&)), &w, SLOT(removePresence(const QString&, const QString&, const QString&))); QObject::connect(squawk, SIGNAL(stateChanged(int)), &w, SLOT(stateChanged(int))); - QObject::connect(squawk, SIGNAL(accountMessage(const QString&, const QMap&)), &w, SLOT(accountMessage(const QString&, const QMap&))); + QObject::connect(squawk, SIGNAL(accountMessage(const QString&, const Shared::Message&)), &w, SLOT(accountMessage(const QString&, const Shared::Message&))); coreThread->start(); diff --git a/order.h b/order.h new file mode 100644 index 0000000..54684af --- /dev/null +++ b/order.h @@ -0,0 +1,136 @@ +#ifndef ORDER_H +#define ORDER_H + +#include +#include + +#include "exception.h" + +namespace W +{ + template > + class Order + { + + class Duplicates: + public Utils::Exception + { + public: + Duplicates():Exception(){} + + std::string getMessage() const{return "Inserting element duplicates existing";} + }; + + class NotFound: + public Utils::Exception + { + public: + NotFound():Exception(){} + + std::string getMessage() const{return "Erasing element haven't been found";} + }; + + protected: + typedef std::list List; + + public: + typedef typename List::size_type size_type; + typedef typename List::const_iterator const_iterator; + typedef typename List::iterator iterator; + + protected: + typedef std::map Map; + typedef typename Map::const_iterator m_const_itr; + typedef typename Map::iterator m_itr; + + public: + Order(): + order(), + r_map() + {} + ~Order() {}; + + size_type size() const { + return order.size(); + } + + void push_back(data_type element) { + m_const_itr m_itr = r_map.find(element); + if (m_itr != r_map.end()) { + throw Duplicates(); + } + + const_iterator itr = order.insert(order.end(), element); + r_map.insert(std::make_pair(element, itr)); + } + + void erase(data_type element) { + m_const_itr itr = r_map.find(element); + if (itr == r_map.end()) { + throw NotFound(); + } + order.erase(itr->second); + r_map.erase(itr); + + } + + void clear() { + order.clear(); + r_map.clear(); + } + + void insert(const_iterator pos, data_type element) { + m_const_itr m_itr = r_map.find(element); + if (m_itr != r_map.end()) { + throw Duplicates(); + } + + const_iterator itr = order.insert(pos, element); + r_map.insert(std::make_pair(element, itr)); + } + + void insert(iterator pos, data_type element) { + m_const_itr m_itr = r_map.find(element); + if (m_itr != r_map.end()) { + throw Duplicates(); + } + + const_iterator itr = order.insert(pos, element); + r_map.insert(std::make_pair(element, itr)); + } + + const_iterator find(data_type element) const { + m_const_itr itr = r_map.find(element); + + if (itr == r_map.end()) { + return end(); + } else { + return itr->second; + } + } + + const_iterator begin() const { + return order.begin(); + } + + const_iterator end() const { + return order.end(); + } + + iterator begin() { + return order.begin(); + } + + iterator end() { + return order.end(); + } + + private: + List order; + Map r_map; + }; +} + + + +#endif // ORDER_H diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index a390327..c5f7a42 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -20,6 +20,7 @@ set(squawkUI_SRC models/contact.cpp models/presence.cpp conversation.cpp + messageline.cpp ) # Tell CMake to create the helloworld executable diff --git a/ui/conversation.cpp b/ui/conversation.cpp index 27008ff..ebff593 100644 --- a/ui/conversation.cpp +++ b/ui/conversation.cpp @@ -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& 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); } diff --git a/ui/conversation.h b/ui/conversation.h index 2c54012..4c74d19 100644 --- a/ui/conversation.h +++ b/ui/conversation.h @@ -23,6 +23,7 @@ #include #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& 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 m_ui; KeyEnterReceiver ker; }; diff --git a/ui/conversation.ui b/ui/conversation.ui index 62087d7..e1b2a52 100644 --- a/ui/conversation.ui +++ b/ui/conversation.ui @@ -115,10 +115,46 @@ - + + + true + QFrame::NoFrame + + Qt::ScrollBarAlwaysOff + + + true + + + + + 0 + 0 + 572 + 162 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + @@ -130,7 +166,10 @@ 0 - + + + 0 + 0 @@ -143,109 +182,86 @@ 0 - - 0 - - - - - - 0 - 0 - - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - .. - - - true - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - .. - - - true - - - - - - - - - - - .. - - - true - - - - - - - - 0 - 0 - - - - - - - - .. - - - true - - - - - + + + + + + + + + + .. + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + .. + + + true + + + + + + + + + + + .. + + + true + + + + + + + + 0 + 0 + + + + + + + + .. + + + true + + + + - + diff --git a/ui/messageline.cpp b/ui/messageline.cpp new file mode 100644 index 0000000..78a017e --- /dev/null +++ b/ui/messageline.cpp @@ -0,0 +1,53 @@ +/* + * + * Copyright (C) 2019 Юрий Губич + * + * 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 "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); +} + diff --git a/ui/messageline.h b/ui/messageline.h new file mode 100644 index 0000000..cd7af16 --- /dev/null +++ b/ui/messageline.h @@ -0,0 +1,45 @@ +/* + * + * Copyright (C) 2019 Юрий Губич + * + * 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 MESSAGELINE_H +#define MESSAGELINE_H + +#include +#include +#include +#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 Order; + std::map messageIndex; + Order messageOrder; + QVBoxLayout* layout; +}; + +#endif // MESSAGELINE_H diff --git a/ui/models/contact.cpp b/ui/models/contact.cpp index 8ec0e6b..76f01f9 100644 --- a/ui/models/contact.cpp +++ b/ui/models/contact.cpp @@ -218,9 +218,9 @@ QString Models::Contact::getAccountName() const return p->getName(); } -void Models::Contact::addMessage(const QMap& 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::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& msg = *itr; + const Shared::Message& msg = *itr; container.push_back(msg); } diff --git a/ui/models/contact.h b/ui/models/contact.h index 07beef5..b587293 100644 --- a/ui/models/contact.h +++ b/ui/models/contact.h @@ -14,7 +14,7 @@ class Contact : public Item { Q_OBJECT public: - typedef std::deque> Messages; + typedef std::deque Messages; Contact(const QString& p_jid ,const QMap &data, Item *parentItem = 0); ~Contact(); @@ -35,7 +35,7 @@ public: QString getAccountName() const; QString getAccountJid() const; - void addMessage(const QMap& data); + void addMessage(const Shared::Message& data); unsigned int getMessagesCount() const; void dropMessages(); void getMessages(Messages& container) const; diff --git a/ui/models/presence.cpp b/ui/models/presence.cpp index a5dd069..2caae08 100644 --- a/ui/models/presence.cpp +++ b/ui/models/presence.cpp @@ -126,7 +126,7 @@ unsigned int Models::Presence::getMessagesCount() const return messages.size(); } -void Models::Presence::addMessage(const QMap& 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& msg = *itr; + const Shared::Message& msg = *itr; container.push_back(msg); } } diff --git a/ui/models/presence.h b/ui/models/presence.h index 56a5788..4734528 100644 --- a/ui/models/presence.h +++ b/ui/models/presence.h @@ -30,7 +30,7 @@ class Presence : public Models::Item { Q_OBJECT public: - typedef std::deque> Messages; + typedef std::deque Messages; explicit Presence(const QMap &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& data); + void addMessage(const Shared::Message& data); void getMessages(Messages& container) const; diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index edcaefb..1af233f 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -493,9 +493,9 @@ void Models::Roster::removePresence(const QString& account, const QString& jid, } } -void Models::Roster::addMessage(const QString& account, const QMap& 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::iterator cBeg = contacts.lower_bound(id); std::multimap::iterator cEnd = contacts.upper_bound(id); diff --git a/ui/models/roster.h b/ui/models/roster.h index 0f9cb1b..83c8277 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -32,7 +32,7 @@ public: void changeContact(const QString& account, const QString& jid, const QMap& data); void addPresence(const QString& account, const QString& jid, const QString& name, const QMap& data); void removePresence(const QString& account, const QString& jid, const QString& name); - void addMessage(const QString& account, const QMap& 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; diff --git a/ui/squawk.cpp b/ui/squawk.cpp index fa580b7..fedeeea 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -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& 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 } } -void Squawk::onConversationMessage(const QString& item) +void Squawk::onConversationMessage(const Shared::Message& msg) { Conversation* conv = static_cast(sender()); - emit sendMessage(conv->getAccount(), { - {"to", conv->getJid()}, - {"body", item} - }); + + emit sendMessage(conv->getAccount(), msg); } diff --git a/ui/squawk.h b/ui/squawk.h index a71fecf..f854fb2 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -30,7 +30,7 @@ signals: void connectAccount(const QString&); void disconnectAccount(const QString&); void changeState(int state); - void sendMessage(const QString& account, const QMap& data); + void sendMessage(const QString& account, const Shared::Message& data); public slots: void newAccount(const QMap& account); @@ -45,7 +45,7 @@ public slots: void addPresence(const QString& account, const QString& jid, const QString& name, const QMap& data); void removePresence(const QString& account, const QString& jid, const QString& name); void stateChanged(int state); - void accountMessage(const QString& account, const QMap& data); + void accountMessage(const QString& account, const Shared::Message& data); private: typedef std::map 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); };