forked from blue/squawk
primitive messages receiving
This commit is contained in:
parent
4775c7b700
commit
3cc53dfaf6
15 changed files with 264 additions and 22 deletions
|
@ -23,7 +23,8 @@
|
|||
Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
|
||||
QWidget(parent),
|
||||
contact(p_contact),
|
||||
m_ui(new Ui::Conversation)
|
||||
m_ui(new Ui::Conversation),
|
||||
ker()
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
m_ui->splitter->setSizes({300, 0});
|
||||
|
@ -33,6 +34,9 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
|
|||
setState(p_contact->getAvailability());
|
||||
|
||||
connect(contact, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onContactChanged(Models::Item*, int, int)));
|
||||
connect(&ker, SIGNAL(enterPressed()), this, SLOT(onEnterPressed()));
|
||||
|
||||
m_ui->dialogBox->installEventFilter(&ker);
|
||||
}
|
||||
|
||||
Conversation::~Conversation()
|
||||
|
@ -83,3 +87,29 @@ void Conversation::onContactChanged(Models::Item* item, int row, int col)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Conversation::addMessage(const QMap<QString, QString>& data)
|
||||
{
|
||||
m_ui->dialogBox->append(data.value("from") + ": " + data.value("body"));
|
||||
}
|
||||
|
||||
bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
|
||||
{
|
||||
if (event->type()==QEvent::KeyPress) {
|
||||
QKeyEvent* key = static_cast<QKeyEvent*>(event);
|
||||
if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
|
||||
emit enterPressed();
|
||||
} else {
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Conversation::onEnterPressed()
|
||||
{
|
||||
qDebug() << "enter";
|
||||
}
|
||||
|
|
|
@ -29,6 +29,16 @@ namespace Ui
|
|||
class Conversation;
|
||||
}
|
||||
|
||||
class KeyEnterReceiver : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
protected:
|
||||
bool eventFilter(QObject* obj, QEvent* event);
|
||||
|
||||
signals:
|
||||
void enterPressed();
|
||||
};
|
||||
|
||||
class Conversation : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -38,6 +48,7 @@ public:
|
|||
|
||||
QString getJid() const;
|
||||
QString getAccount() const;
|
||||
void addMessage(const QMap<QString, QString>& data);
|
||||
|
||||
protected:
|
||||
void setState(Shared::Availability state);
|
||||
|
@ -46,10 +57,12 @@ protected:
|
|||
|
||||
protected slots:
|
||||
void onContactChanged(Models::Item* item, int row, int col);
|
||||
void onEnterPressed();
|
||||
|
||||
private:
|
||||
Models::Contact* contact;
|
||||
QScopedPointer<Ui::Conversation> m_ui;
|
||||
KeyEnterReceiver ker;
|
||||
};
|
||||
|
||||
#endif // CONVERSATION_H
|
||||
|
|
|
@ -6,7 +6,9 @@ Models::Contact::Contact(const QString& p_jid ,const QMap<QString, QVariant> &da
|
|||
jid(p_jid),
|
||||
availability(Shared::offline),
|
||||
state(Shared::none),
|
||||
presences()
|
||||
presences(),
|
||||
messages(),
|
||||
childMessages(0)
|
||||
{
|
||||
QMap<QString, QVariant>::const_iterator itr = data.find("state");
|
||||
if (itr != data.end()) {
|
||||
|
@ -66,7 +68,7 @@ void Models::Contact::setAvailability(Shared::Availability p_state)
|
|||
|
||||
int Models::Contact::columnCount() const
|
||||
{
|
||||
return 4;
|
||||
return 5;
|
||||
}
|
||||
|
||||
QVariant Models::Contact::data(int column) const
|
||||
|
@ -84,6 +86,8 @@ QVariant Models::Contact::data(int column) const
|
|||
return state;
|
||||
case 3:
|
||||
return availability;
|
||||
case 4:
|
||||
return getMessagesCount();
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
|
@ -136,9 +140,11 @@ void Models::Contact::refresh()
|
|||
{
|
||||
QDateTime lastActivity;
|
||||
Presence* presence = 0;
|
||||
unsigned int count = 0;
|
||||
for (QMap<QString, Presence*>::iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
|
||||
Presence* pr = itr.value();
|
||||
QDateTime la = pr->getLastActivity();
|
||||
count += pr->getMessagesCount();
|
||||
|
||||
if (la > lastActivity) {
|
||||
lastActivity = la;
|
||||
|
@ -151,6 +157,11 @@ void Models::Contact::refresh()
|
|||
} else {
|
||||
setAvailability(Shared::offline);
|
||||
}
|
||||
|
||||
if (childMessages != count) {
|
||||
childMessages = count;
|
||||
changed(4);
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Contact::_removeChild(int index)
|
||||
|
@ -183,7 +194,9 @@ void Models::Contact::setState(Shared::SubscriptionState p_state)
|
|||
|
||||
QIcon Models::Contact::getStatusIcon() const
|
||||
{
|
||||
if (state == Shared::both) {
|
||||
if (getMessagesCount() > 0) {
|
||||
return QIcon::fromTheme("mail-message");
|
||||
} else if (state == Shared::both) {
|
||||
return QIcon::fromTheme(Shared::availabilityThemeIcons[availability]);
|
||||
} else {
|
||||
return QIcon::fromTheme(Shared::subscriptionStateThemeIcons[state]);
|
||||
|
@ -204,3 +217,35 @@ QString Models::Contact::getAccountName() const
|
|||
return p->getName();
|
||||
}
|
||||
|
||||
void Models::Contact::addMessage(const QMap<QString, QString>& data)
|
||||
{
|
||||
const QString& res = data.value("fromResource");
|
||||
if (res.size() > 0) {
|
||||
QMap<QString, Presence*>::iterator itr = presences.find(res);
|
||||
if (itr == presences.end()) {
|
||||
qDebug() << "An attempt to add message to the roster to the unknown resource " << res << " of contact " << jid << " in account " << getAccountName() << ", skipping";
|
||||
return;
|
||||
}
|
||||
itr.value()->addMessage(data);
|
||||
} else {
|
||||
messages.emplace_back(data);
|
||||
changed(4);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Models::Contact::getMessagesCount() const
|
||||
{
|
||||
return messages.size() + childMessages;
|
||||
}
|
||||
|
||||
void Models::Contact::dropMessages()
|
||||
{
|
||||
if (messages.size() > 0) {
|
||||
messages.clear();
|
||||
changed(4);
|
||||
}
|
||||
|
||||
for (QMap<QString, Presence*>::iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
|
||||
itr.value()->dropMessages();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "../../global.h"
|
||||
#include <QMap>
|
||||
#include <QIcon>
|
||||
#include <deque>
|
||||
|
||||
namespace Models {
|
||||
|
||||
|
@ -32,6 +33,10 @@ public:
|
|||
void appendChild(Models::Item * child) override;
|
||||
QString getAccountName() const;
|
||||
|
||||
void addMessage(const QMap<QString, QString>& data);
|
||||
unsigned int getMessagesCount() const;
|
||||
void dropMessages();
|
||||
|
||||
protected:
|
||||
void _removeChild(int index) override;
|
||||
|
||||
|
@ -46,10 +51,13 @@ protected:
|
|||
void setJid(const QString p_jid);
|
||||
|
||||
private:
|
||||
typedef std::deque<QMap<QString, QString>> Messages;
|
||||
QString jid;
|
||||
Shared::Availability availability;
|
||||
Shared::SubscriptionState state;
|
||||
QMap<QString, Presence*> presences;
|
||||
Messages messages;
|
||||
unsigned int childMessages;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,8 @@ Models::Presence::Presence(const QMap<QString, QVariant>& data, Item* parentItem
|
|||
Item(Item::presence, data, parentItem),
|
||||
availability(Shared::offline),
|
||||
lastActivity(data.value("lastActivity").toDateTime()),
|
||||
status(data.value("status").toString())
|
||||
status(data.value("status").toString()),
|
||||
messages()
|
||||
{
|
||||
QMap<QString, QVariant>::const_iterator itr = data.find("availability");
|
||||
if (itr != data.end()) {
|
||||
|
@ -36,7 +37,7 @@ Models::Presence::~Presence()
|
|||
|
||||
int Models::Presence::columnCount() const
|
||||
{
|
||||
return 4;
|
||||
return 5;
|
||||
}
|
||||
|
||||
QVariant Models::Presence::data(int column) const
|
||||
|
@ -50,6 +51,8 @@ QVariant Models::Presence::data(int column) const
|
|||
return availability;
|
||||
case 3:
|
||||
return status;
|
||||
case 4:
|
||||
return getMessagesCount();
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
|
@ -117,3 +120,32 @@ void Models::Presence::update(const QString& key, const QVariant& value)
|
|||
setLastActivity(value.toDateTime());
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Models::Presence::getMessagesCount() const
|
||||
{
|
||||
return messages.size();
|
||||
}
|
||||
|
||||
void Models::Presence::addMessage(const QMap<QString, QString>& data)
|
||||
{
|
||||
messages.emplace_back(data);
|
||||
changed(4);
|
||||
}
|
||||
|
||||
void Models::Presence::dropMessages()
|
||||
{
|
||||
if (messages.size() > 0) {
|
||||
messages.clear();
|
||||
changed(4);
|
||||
}
|
||||
}
|
||||
|
||||
QIcon Models::Presence::getStatusIcon() const
|
||||
{
|
||||
if (getMessagesCount() > 0) {
|
||||
return QIcon::fromTheme("mail-message");
|
||||
} else {
|
||||
return QIcon::fromTheme(Shared::availabilityThemeIcons[availability]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "item.h"
|
||||
#include "../../global.h"
|
||||
#include <QDateTime>
|
||||
#include <QIcon>
|
||||
|
||||
namespace Models {
|
||||
|
||||
|
@ -44,13 +45,19 @@ public:
|
|||
|
||||
QString getStatus() const;
|
||||
void setStatus(const QString& p_state);
|
||||
QIcon getStatusIcon() const;
|
||||
|
||||
void update(const QString& key, const QVariant& value);
|
||||
unsigned int getMessagesCount() const;
|
||||
void dropMessages();
|
||||
void addMessage(const QMap<QString, QString>& data);
|
||||
|
||||
private:
|
||||
typedef std::deque<QMap<QString, QString>> Messages;
|
||||
Shared::Availability availability;
|
||||
QDateTime lastActivity;
|
||||
QString status;
|
||||
Messages messages;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
|
|||
break;
|
||||
case Item::presence:{
|
||||
Presence* presence = static_cast<Presence*>(item);
|
||||
result = QIcon::fromTheme(Shared::availabilityThemeIcons[presence->getAvailability()]);
|
||||
result = presence->getStatusIcon();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -492,3 +492,23 @@ void Models::Roster::removePresence(const QString& account, const QString& jid,
|
|||
cBeg->second->removePresence(name);
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Roster::addMessage(const QString& account, const QMap<QString, QString>& data)
|
||||
{
|
||||
ElId id(account, data.value("from"));
|
||||
|
||||
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id);
|
||||
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(id);
|
||||
|
||||
for (;cBeg != cEnd; ++cBeg) {
|
||||
cBeg->second->addMessage(data);
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Roster::dropMessages(const QString& account, const QString& jid)
|
||||
{
|
||||
ElId id(account, jid);
|
||||
for (std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id), cEnd = contacts.upper_bound(id) ;cBeg != cEnd; ++cBeg) {
|
||||
cBeg->second->dropMessages();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,8 @@ public:
|
|||
void changeContact(const QString& account, const QString& jid, const QMap<QString, QVariant>& data);
|
||||
void addPresence(const QString& account, const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
|
||||
void removePresence(const QString& account, const QString& jid, const QString& name);
|
||||
void addMessage(const QString& account, const QMap<QString, QString>& data);
|
||||
void dropMessages(const QString& account, const QString& jid);
|
||||
|
||||
QVariant data ( const QModelIndex& index, int role ) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
|
|
@ -184,6 +184,7 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
|
|||
connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*)));
|
||||
|
||||
conversations.insert(std::make_pair(id, conv));
|
||||
rosterModel.dropMessages(account, jid);
|
||||
|
||||
conv->show();
|
||||
}
|
||||
|
@ -201,3 +202,16 @@ void Squawk::onConversationClosed(QObject* parent)
|
|||
}
|
||||
conversations.erase(itr);
|
||||
}
|
||||
|
||||
void Squawk::accountMessage(const QString& account, const QMap<QString, QString>& data)
|
||||
{
|
||||
const QString& from = data.value("from");
|
||||
Conversations::iterator itr = conversations.find({account, from});
|
||||
if (itr != conversations.end()) {
|
||||
qDebug() << "adding message";
|
||||
itr->second->addMessage(data);
|
||||
} else {
|
||||
qDebug() << "pending message";
|
||||
rosterModel.addMessage(account, data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@ public slots:
|
|||
void addPresence(const QString& account, const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
|
||||
void removePresence(const QString& account, const QString& jid, const QString& name);
|
||||
void stateChanged(int state);
|
||||
void accountMessage(const QString& account, const QMap<QString, QString>& data);
|
||||
|
||||
private:
|
||||
typedef std::map<Models::Roster::ElId, Conversation*> Conversations;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue