Basic roster receiving

This commit is contained in:
Blue 2019-04-04 00:23:51 +03:00
parent d14883ad91
commit 7b30228249
10 changed files with 151 additions and 3 deletions

View File

@ -1,4 +1,5 @@
#include "account.h" #include "account.h"
#include <qxmpp/QXmppRosterManager.h>
using namespace Core; using namespace Core;
@ -9,15 +10,19 @@ Account::Account(const QString& p_login, const QString& p_server, const QString&
server(p_server), server(p_server),
password(p_password), password(p_password),
client(), client(),
state(Shared::disconnected) state(Shared::disconnected),
groups()
{ {
QObject::connect(&client, SIGNAL(connected()), this, SLOT(onClientConnected())); QObject::connect(&client, SIGNAL(connected()), this, SLOT(onClientConnected()));
QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
QXmppRosterManager& rm = client.rosterManager();
QObject::connect(&rm, SIGNAL(rosterReceived()), this, SLOT(onRosterReceived()));
} }
Account::~Account() Account::~Account()
{ {
} }
Shared::ConnectionState Core::Account::getState() const Shared::ConnectionState Core::Account::getState() const
@ -85,3 +90,31 @@ QString Core::Account::getServer() const
{ {
return server; return server;
} }
void Core::Account::onRosterReceived()
{
QXmppRosterManager& rm = client.rosterManager();
QStringList bj = rm.getRosterBareJids();
for (int i = 0; i < bj.size(); ++i) {
const QString& jid = bj[i];
QXmppRosterIq::Item re = rm.getRosterEntry(jid);
QSet<QString> gr = re.groups();
int grCount = 0;
for (QSet<QString>::const_iterator itr = gr.begin(), end = gr.end(); itr != end; ++itr) {
const QString& groupName = *itr;
std::map<QString, int>::iterator gItr = groups.find(groupName);
if (gItr == groups.end()) {
gItr = groups.insert(std::make_pair(groupName, 0)).first;
emit addGroup(groupName);
}
gItr->second++;
emit addContact(jid, re.name(), groupName);
grCount++;
}
if (grCount == 0) {
emit addContact(jid, re.name(), "");
}
}
}

View File

@ -2,6 +2,7 @@
#define CORE_ACCOUNT_H #define CORE_ACCOUNT_H
#include <QtCore/QObject> #include <QtCore/QObject>
#include <map>
#include <qxmpp/QXmppClient.h> #include <qxmpp/QXmppClient.h>
#include "../global.h" #include "../global.h"
@ -27,6 +28,9 @@ public:
signals: signals:
void connectionStateChanged(int); void connectionStateChanged(int);
void addGroup(const QString& name);
void removeGroup(const QString& name);
void addContact(const QString& jid, const QString& name, const QString& group);
private: private:
QString name; QString name;
@ -35,10 +39,12 @@ private:
QString password; QString password;
QXmppClient client; QXmppClient client;
Shared::ConnectionState state; Shared::ConnectionState state;
std::map<QString, int> groups;
private slots: private slots:
void onClientConnected(); void onClientConnected();
void onClientDisconnected(); void onClientDisconnected();
void onRosterReceived();
}; };

View File

@ -74,6 +74,9 @@ void Core::Squawk::addAccount(const QString& login, const QString& server, const
amap.insert(std::make_pair(name, acc)); amap.insert(std::make_pair(name, acc));
connect(acc, SIGNAL(connectionStateChanged(int)), this, SLOT(onAccountConnectionStateChanged(int))); connect(acc, SIGNAL(connectionStateChanged(int)), this, SLOT(onAccountConnectionStateChanged(int)));
connect(acc, SIGNAL(addContact(const QString&, const QString&, const QString&)), this, SLOT(onAccountAddContact(const QString&, const QString&, const QString&)));
connect(acc, SIGNAL(addGroup(const QString&)), this, SLOT(onAccountAddGroup(const QString&)));
connect(acc, SIGNAL(removeGroup(const QString&)), this, SLOT(onAccountRemoveGroup(const QString&)));
QMap<QString, QVariant> map = { QMap<QString, QVariant> map = {
{"login", login}, {"login", login},
@ -112,3 +115,20 @@ void Core::Squawk::onAccountConnectionStateChanged(int state)
emit accountConnectionStateChanged(acc->getName(), state); emit accountConnectionStateChanged(acc->getName(), state);
} }
void Core::Squawk::onAccountAddContact(const QString& jid, const QString& name, const QString& group)
{
Account* acc = static_cast<Account*>(sender());
emit addContact(acc->getName(), jid, name, group);
}
void Core::Squawk::onAccountAddGroup(const QString& name)
{
Account* acc = static_cast<Account*>(sender());
emit addGroup(acc->getName(), name);
}
void Core::Squawk::onAccountRemoveGroup(const QString& name)
{
Account* acc = static_cast<Account*>(sender());
emit removeGroup(acc->getName(), name);
}

View File

@ -25,6 +25,9 @@ signals:
void quit(); void quit();
void newAccount(const QMap<QString, QVariant>&); void newAccount(const QMap<QString, QVariant>&);
void accountConnectionStateChanged(const QString&, int); void accountConnectionStateChanged(const QString&, int);
void addGroup(const QString& account, const QString& name);
void removeGroup(const QString& account, const QString& name);
void addContact(const QString& account, const QString& jid, const QString& name, const QString& group);
public slots: public slots:
void start(); void start();
@ -45,6 +48,9 @@ private:
private slots: private slots:
void onAccountConnectionStateChanged(int state); void onAccountConnectionStateChanged(int state);
void onAccountAddGroup(const QString& name);
void onAccountRemoveGroup(const QString& name);
void onAccountAddContact(const QString& jid, const QString& name, const QString& group);
}; };
} }

View File

@ -34,6 +34,10 @@ int main(int argc, char *argv[])
QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&))); QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&)));
QObject::connect(squawk, SIGNAL(accountConnectionStateChanged(const QString&, int)), &w, SLOT(accountConnectionStateChanged(const QString&, int))); QObject::connect(squawk, SIGNAL(accountConnectionStateChanged(const QString&, int)), &w, SLOT(accountConnectionStateChanged(const QString&, int)));
QObject::connect(squawk, SIGNAL(addContact(const QString&, const QString&, const QString&, const QString&)),
&w, SLOT(addContact(const QString&, const QString&, const QString&, const QString&)));
QObject::connect(squawk, SIGNAL(addGroup(const QString&, const QString&)), &w, SLOT(addGroup(const QString&, const QString&)));
QObject::connect(squawk, SIGNAL(removeGroup(const QString&, const QString&)), &w, SLOT(removeGroup(const QString&, const QString&)));
coreThread->start(); coreThread->start();

View File

@ -15,7 +15,7 @@ class Item : public QObject{
enum Type { enum Type {
account, account,
group, group,
contect, contact,
conversation, conversation,
root root
}; };

View File

@ -1,4 +1,5 @@
#include "roster.h" #include "roster.h"
#include <QDebug>
#include <QIcon> #include <QIcon>
using namespace Models; using namespace Models;
@ -8,6 +9,8 @@ Models::Roster::Roster(QObject* parent):
accountsModel(new Accounts()), accountsModel(new Accounts()),
root(new Item(Item::root, {{"name", "root"}})), root(new Item(Item::root, {{"name", "root"}})),
accounts(), accounts(),
groups(),
contacts(),
elements() elements()
{ {
connect(accountsModel, connect(accountsModel,
@ -193,4 +196,54 @@ void Models::Roster::onAccountDataChanged(const QModelIndex& tl, const QModelInd
} }
} }
void Models::Roster::addGroup(const QString& account, const QString& name)
{
std::map<QString, Account*>::iterator itr = accounts.find(account);
if (itr != accounts.end()) {
Account* acc = itr->second;
Item* group = new Item(Item::group, {{"name", name}}, acc);
beginInsertRows(createIndex(acc->row(), 0, acc), acc->childCount(), acc->childCount());
acc->appendChild(group);
groups.insert(std::make_pair(name, group));
elements.insert({{account, name}, group});
endInsertRows();
} else {
qDebug() << "An attempt to add group " << name << " to non existing account " << account << ", skipping";
}
}
void Models::Roster::addContact(const QString& account, const QString& jid, const QString& name, const QString& group)
{
Item* parent;
if (group == "") {
std::map<QString, Account*>::iterator itr = accounts.find(account);
if (itr == accounts.end()) {
qDebug() << "An attempt to add a contact " << name << " to non existing account " << account << ", skipping";
return;
}
parent = itr->second;
} else {
std::map<QString, Item*>::iterator itr = groups.find(group);
if (itr == groups.end()) {
qDebug() << "An attempt to add a contact " << name << " to non existing group " << group << ", skipping";
return;
}
parent = itr->second;
}
QString sName = name;
if (sName == "") {
sName = jid;
}
Item* contact = new Item(Item::contact, {{"name", sName}}, parent);
beginInsertRows(createIndex(parent->row(), 0, parent), parent->childCount(), parent->childCount());
parent->appendChild(contact);
contacts.insert(std::make_pair(jid, contact));
elements.insert({{account, jid}, contact});
endInsertRows();
}
void Models::Roster::removeGroup(const QString& account, const QString& name)
{
}

View File

@ -23,6 +23,9 @@ public:
void addAccount(const QMap<QString, QVariant> &data); void addAccount(const QMap<QString, QVariant> &data);
void updateAccount(const QString& account, const QString& field, const QVariant& value); void updateAccount(const QString& account, const QString& field, const QVariant& value);
void addGroup(const QString& account, const QString& name);
void removeGroup(const QString& account, const QString& name);
void addContact(const QString& account, const QString& jid, const QString& name, const QString& group);
QVariant data ( const QModelIndex& index, int role ) const override; QVariant data ( const QModelIndex& index, int role ) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
@ -37,6 +40,8 @@ public:
private: private:
Item* root; Item* root;
std::map<QString, Account*> accounts; std::map<QString, Account*> accounts;
std::map<QString, Item*> groups;
std::map<QString, Item*> contacts;
std::map<ElId, Item*> elements; std::map<ElId, Item*> elements;
private slots: private slots:

View File

@ -85,3 +85,21 @@ void Squawk::accountConnectionStateChanged(const QString& account, int state)
{ {
rosterModel.updateAccount(account, "state", state); rosterModel.updateAccount(account, "state", state);
} }
void Squawk::addContact(const QString& account, const QString& jid, const QString& name, const QString& group)
{
rosterModel.addContact(account, jid, name, group);
}
void Squawk::addGroup(const QString& account, const QString& name)
{
rosterModel.addGroup(account, name);
}
void Squawk::removeGroup(const QString& account, const QString& name)
{
}

View File

@ -32,6 +32,9 @@ signals:
public slots: public slots:
void newAccount(const QMap<QString, QVariant>& account); void newAccount(const QMap<QString, QVariant>& account);
void accountConnectionStateChanged(const QString& account, int state); void accountConnectionStateChanged(const QString& account, int state);
void addGroup(const QString& account, const QString& name);
void removeGroup(const QString& account, const QString& name);
void addContact(const QString& account, const QString& jid, const QString& name, const QString& group);
private: private:
QScopedPointer<Ui::Squawk> m_ui; QScopedPointer<Ui::Squawk> m_ui;