diff --git a/core/account.cpp b/core/account.cpp index 9524961..8c27106 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -406,3 +406,12 @@ QString Core::Account::getFullJid() const return getLogin() + "@" + getServer() + "/" + getResource(); } +void Core::Account::sendMessage(const QMap& data) +{ + if (state == Shared::connected) { + client.sendMessage(data.value("to"), data.value("body")); + } else { + qDebug() << "An attempt to send message with not connected account " << name << ", skipping"; + } +} + diff --git a/core/account.h b/core/account.h index 31cfb08..a0fac23 100644 --- a/core/account.h +++ b/core/account.h @@ -36,6 +36,7 @@ public: void setResource(const QString& p_resource); void setAvailability(Shared::Availability avail); QString getFullJid() const; + void sendMessage(const QMap& data); signals: void connectionStateChanged(int); diff --git a/core/squawk.cpp b/core/squawk.cpp index c1c8e1b..3d62939 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -199,3 +199,14 @@ void Core::Squawk::onAccountMessage(const QMap& data) Account* acc = static_cast(sender()); emit accountMessage(acc->getName(), data); } + +void Core::Squawk::sendMessage(const QString& account, const QMap& data) +{ + AccountsMap::const_iterator itr = amap.find(account); + if (itr == amap.end()) { + qDebug("An attempt to send a message with non existing account, skipping"); + return; + } + + itr->second->sendMessage(data); +} diff --git a/core/squawk.h b/core/squawk.h index cc89993..9f364a9 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -44,6 +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); private: typedef std::deque Accounts; diff --git a/global.h b/global.h index 38fb83d..2e0fe27 100644 --- a/global.h +++ b/global.h @@ -3,6 +3,7 @@ #include #include +#include namespace Shared { @@ -51,6 +52,18 @@ static const std::deque availabilityNames = {"Online", "Away", "Absent" static const std::deque subscriptionStateThemeIcons = {"edit-none", "arrow-down-double", "arrow-up-double", "dialog-ok", "question"}; +class Message { +public: + Message(); + +private: + QString jFrom; + QString rFrom; + QString jTo; + QString rTo; + QDateTime time; +}; + }; #endif // GLOBAL_H diff --git a/main.cpp b/main.cpp index 94bf777..8b094b0 100644 --- a/main.cpp +++ b/main.cpp @@ -34,6 +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(squawk, SIGNAL(newAccount(const QMap&)), &w, SLOT(newAccount(const QMap&))); QObject::connect(squawk, SIGNAL(accountAvailabilityChanged(const QString&, int)), &w, SLOT(accountAvailabilityChanged(const QString&, int))); diff --git a/ui/conversation.cpp b/ui/conversation.cpp index 4cbfdd7..27008ff 100644 --- a/ui/conversation.cpp +++ b/ui/conversation.cpp @@ -37,6 +37,13 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent): connect(&ker, SIGNAL(enterPressed()), this, SLOT(onEnterPressed())); m_ui->messageEditor->installEventFilter(&ker); + + Models::Contact::Messages deque; + contact->getMessages(deque); + + for (Models::Contact::Messages::const_iterator itr = deque.begin(), end = deque.end(); itr != end; ++itr) { + addMessage(*itr); + } } Conversation::~Conversation() @@ -124,5 +131,8 @@ bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event) void Conversation::onEnterPressed() { - qDebug() << "enter"; + QString msg(m_ui->messageEditor->toPlainText()); + m_ui->messageEditor->clear(); + m_ui->dialogBox->append(contact->getAccountJid() + ": " + msg); + emit sendMessage(msg); } diff --git a/ui/conversation.h b/ui/conversation.h index 1bdec69..2c54012 100644 --- a/ui/conversation.h +++ b/ui/conversation.h @@ -53,6 +53,9 @@ public: QString getAccount() const; void addMessage(const QMap& data); +signals: + void sendMessage(const QString& message); + protected: void setState(Shared::Availability state); void setStatus(const QString& status); diff --git a/ui/models/contact.cpp b/ui/models/contact.cpp index eb13178..8ec0e6b 100644 --- a/ui/models/contact.cpp +++ b/ui/models/contact.cpp @@ -1,5 +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), @@ -249,3 +250,30 @@ void Models::Contact::dropMessages() itr.value()->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; + container.push_back(msg); + } + + for (QMap::const_iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) { + itr.value()->getMessages(container); + } +} + +QString Models::Contact::getAccountJid() const +{ + const Item* p = this; + do { + p = p->parentItemConst(); + } while (p != 0 && p->type != Item::account); + + if (p == 0) { + qDebug() << "An attempt to request account jid of the contact " << jid << " but the parent account wasn't found, returning empty string"; + return ""; + } + const Account* acc = static_cast(p); + return acc->getLogin() + "@" + acc->getServer(); +} diff --git a/ui/models/contact.h b/ui/models/contact.h index 29e690a..07beef5 100644 --- a/ui/models/contact.h +++ b/ui/models/contact.h @@ -14,6 +14,7 @@ class Contact : public Item { Q_OBJECT public: + typedef std::deque> Messages; Contact(const QString& p_jid ,const QMap &data, Item *parentItem = 0); ~Contact(); @@ -32,10 +33,12 @@ public: void appendChild(Models::Item * child) override; QString getAccountName() const; + QString getAccountJid() const; void addMessage(const QMap& data); unsigned int getMessagesCount() const; void dropMessages(); + void getMessages(Messages& container) const; protected: void _removeChild(int index) override; @@ -51,7 +54,6 @@ protected: void setJid(const QString p_jid); private: - typedef std::deque> Messages; QString jid; Shared::Availability availability; Shared::SubscriptionState state; diff --git a/ui/models/presence.cpp b/ui/models/presence.cpp index a6d3ccb..a5dd069 100644 --- a/ui/models/presence.cpp +++ b/ui/models/presence.cpp @@ -149,3 +149,10 @@ 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; + container.push_back(msg); + } +} diff --git a/ui/models/presence.h b/ui/models/presence.h index bc95bb1..56a5788 100644 --- a/ui/models/presence.h +++ b/ui/models/presence.h @@ -30,6 +30,7 @@ class Presence : public Models::Item { Q_OBJECT public: + typedef std::deque> Messages; explicit Presence(const QMap &data, Item *parentItem = 0); ~Presence(); @@ -51,9 +52,10 @@ public: unsigned int getMessagesCount() const; void dropMessages(); void addMessage(const QMap& data); + + void getMessages(Messages& container) const; private: - typedef std::deque> Messages; Shared::Availability availability; QDateTime lastActivity; QString status; diff --git a/ui/squawk.cpp b/ui/squawk.cpp index a95cffe..fa580b7 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -182,6 +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&))); conversations.insert(std::make_pair(id, conv)); rosterModel.dropMessages(account, jid); @@ -215,3 +216,12 @@ void Squawk::accountMessage(const QString& account, const QMap rosterModel.addMessage(account, data); } } + +void Squawk::onConversationMessage(const QString& item) +{ + Conversation* conv = static_cast(sender()); + emit sendMessage(conv->getAccount(), { + {"to", conv->getJid()}, + {"body", item} + }); +} diff --git a/ui/squawk.h b/ui/squawk.h index 725c41b..a71fecf 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -30,6 +30,7 @@ signals: void connectAccount(const QString&); void disconnectAccount(const QString&); void changeState(int state); + void sendMessage(const QString& account, const QMap& data); public slots: void newAccount(const QMap& account); @@ -63,6 +64,7 @@ private slots: void onConversationClosed(QObject* parent = 0); void onComboboxActivated(int index); void onRosterItemDoubleClicked(const QModelIndex& item); + void onConversationMessage(const QString& item); };