basic sending messages

This commit is contained in:
Blue 2019-04-10 23:53:42 +03:00
parent d3f537856f
commit 5575cff1f5
14 changed files with 103 additions and 3 deletions

View File

@ -406,3 +406,12 @@ QString Core::Account::getFullJid() const
return getLogin() + "@" + getServer() + "/" + getResource();
}
void Core::Account::sendMessage(const QMap<QString, QString>& 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";
}
}

View File

@ -36,6 +36,7 @@ public:
void setResource(const QString& p_resource);
void setAvailability(Shared::Availability avail);
QString getFullJid() const;
void sendMessage(const QMap<QString, QString>& data);
signals:
void connectionStateChanged(int);

View File

@ -199,3 +199,14 @@ void Core::Squawk::onAccountMessage(const QMap<QString, QString>& data)
Account* acc = static_cast<Account*>(sender());
emit accountMessage(acc->getName(), data);
}
void Core::Squawk::sendMessage(const QString& account, const QMap<QString, QString>& 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);
}

View File

@ -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<QString, QString>& data);
private:
typedef std::deque<Account*> Accounts;

View File

@ -3,6 +3,7 @@
#include <QString>
#include <deque>
#include <QDateTime>
namespace Shared {
@ -51,6 +52,18 @@ static const std::deque<QString> availabilityNames = {"Online", "Away", "Absent"
static const std::deque<QString> 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

View File

@ -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<QString, QString>&)), squawk, SLOT(sendMessage(const QString&, const QMap<QString, QString>&)));
QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&)));
QObject::connect(squawk, SIGNAL(accountAvailabilityChanged(const QString&, int)), &w, SLOT(accountAvailabilityChanged(const QString&, int)));

View File

@ -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);
}

View File

@ -53,6 +53,9 @@ public:
QString getAccount() const;
void addMessage(const QMap<QString, QString>& data);
signals:
void sendMessage(const QString& message);
protected:
void setState(Shared::Availability state);
void setStatus(const QString& status);

View File

@ -1,5 +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),
@ -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<QString, QString>& msg = *itr;
container.push_back(msg);
}
for (QMap<QString, Presence*>::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<const Account*>(p);
return acc->getLogin() + "@" + acc->getServer();
}

View File

@ -14,6 +14,7 @@ class Contact : public Item
{
Q_OBJECT
public:
typedef std::deque<QMap<QString, QString>> Messages;
Contact(const QString& p_jid ,const QMap<QString, QVariant> &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<QString, QString>& 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<QMap<QString, QString>> Messages;
QString jid;
Shared::Availability availability;
Shared::SubscriptionState state;

View File

@ -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<QString, QString>& msg = *itr;
container.push_back(msg);
}
}

View File

@ -30,6 +30,7 @@ class Presence : public Models::Item
{
Q_OBJECT
public:
typedef std::deque<QMap<QString, QString>> Messages;
explicit Presence(const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Presence();
@ -51,9 +52,10 @@ public:
unsigned int getMessagesCount() const;
void dropMessages();
void addMessage(const QMap<QString, QString>& data);
void getMessages(Messages& container) const;
private:
typedef std::deque<QMap<QString, QString>> Messages;
Shared::Availability availability;
QDateTime lastActivity;
QString status;

View File

@ -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<QString, QString>
rosterModel.addMessage(account, data);
}
}
void Squawk::onConversationMessage(const QString& item)
{
Conversation* conv = static_cast<Conversation*>(sender());
emit sendMessage(conv->getAccount(), {
{"to", conv->getJid()},
{"body", item}
});
}

View File

@ -30,6 +30,7 @@ signals:
void connectAccount(const QString&);
void disconnectAccount(const QString&);
void changeState(int state);
void sendMessage(const QString& account, const QMap<QString, QString>& data);
public slots:
void newAccount(const QMap<QString, QVariant>& 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);
};