2019-03-29 14:54:34 +00:00
|
|
|
#include "account.h"
|
2019-04-03 21:23:51 +00:00
|
|
|
#include <qxmpp/QXmppRosterManager.h>
|
2019-03-29 14:54:34 +00:00
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
2019-03-30 20:13:13 +00:00
|
|
|
Account::Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent):
|
2019-03-29 14:54:34 +00:00
|
|
|
QObject(parent),
|
2019-03-30 20:13:13 +00:00
|
|
|
name(p_name),
|
|
|
|
login(p_login),
|
|
|
|
server(p_server),
|
2019-03-29 14:54:34 +00:00
|
|
|
password(p_password),
|
2019-03-31 21:05:09 +00:00
|
|
|
client(),
|
2019-04-03 21:23:51 +00:00
|
|
|
state(Shared::disconnected),
|
|
|
|
groups()
|
2019-03-29 14:54:34 +00:00
|
|
|
{
|
2019-03-31 21:05:09 +00:00
|
|
|
QObject::connect(&client, SIGNAL(connected()), this, SLOT(onClientConnected()));
|
|
|
|
QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
|
2019-04-03 21:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
QXmppRosterManager& rm = client.rosterManager();
|
2019-04-06 10:14:32 +00:00
|
|
|
|
2019-04-03 21:23:51 +00:00
|
|
|
QObject::connect(&rm, SIGNAL(rosterReceived()), this, SLOT(onRosterReceived()));
|
2019-04-06 10:14:32 +00:00
|
|
|
QObject::connect(&rm, SIGNAL(itemAdded(const QString&)), this, SLOT(onRosterItemAdded(const QString&)));
|
|
|
|
QObject::connect(&rm, SIGNAL(itemRemoved(const QString&)), this, SLOT(onRosterItemRemoved(const QString&)));
|
|
|
|
QObject::connect(&rm, SIGNAL(itemChanged(const QString&)), this, SLOT(onRosterItemChanged(const QString&)));
|
2019-03-29 14:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Account::~Account()
|
|
|
|
{
|
2019-03-31 21:05:09 +00:00
|
|
|
}
|
2019-03-29 14:54:34 +00:00
|
|
|
|
2019-03-31 21:05:09 +00:00
|
|
|
Shared::ConnectionState Core::Account::getState() const
|
|
|
|
{
|
|
|
|
return state;
|
2019-03-29 14:54:34 +00:00
|
|
|
}
|
2019-03-31 21:05:09 +00:00
|
|
|
|
|
|
|
void Core::Account::connect()
|
|
|
|
{
|
|
|
|
if (state == Shared::disconnected) {
|
|
|
|
client.connectToServer(login + "@" + server, password);
|
|
|
|
state = Shared::connecting;
|
|
|
|
emit connectionStateChanged(state);
|
|
|
|
} else {
|
|
|
|
qDebug("An attempt to connect an account which is already connected, skipping");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Account::disconnect()
|
|
|
|
{
|
|
|
|
if (state != Shared::disconnected) {
|
2019-04-02 21:58:43 +00:00
|
|
|
client.disconnectFromServer();
|
2019-03-31 21:05:09 +00:00
|
|
|
state = Shared::disconnected;
|
|
|
|
emit connectionStateChanged(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Account::onClientConnected()
|
|
|
|
{
|
|
|
|
if (state == Shared::connecting) {
|
|
|
|
state = Shared::connected;
|
|
|
|
emit connectionStateChanged(state);
|
|
|
|
} else {
|
|
|
|
qDebug("Something weird had happened - xmpp client reported about successful connection but account wasn't in connecting state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 15:46:18 +00:00
|
|
|
void Core::Account::onClientDisconnected()
|
2019-03-31 21:05:09 +00:00
|
|
|
{
|
|
|
|
if (state != Shared::disconnected) {
|
|
|
|
state = Shared::disconnected;
|
|
|
|
emit connectionStateChanged(state);
|
|
|
|
} else {
|
2019-04-03 18:15:36 +00:00
|
|
|
//qDebug("Something weird had happened - xmpp client reported about being disconnection but account was already in disconnected state");
|
2019-03-31 21:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Core::Account::getName() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-04-02 21:58:43 +00:00
|
|
|
QString Core::Account::getLogin() const
|
|
|
|
{
|
|
|
|
return login;
|
|
|
|
}
|
2019-03-31 21:05:09 +00:00
|
|
|
|
2019-04-02 21:58:43 +00:00
|
|
|
QString Core::Account::getPassword() const
|
|
|
|
{
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Core::Account::getServer() const
|
|
|
|
{
|
|
|
|
return server;
|
|
|
|
}
|
2019-04-03 21:23:51 +00:00
|
|
|
|
|
|
|
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];
|
2019-04-06 10:14:32 +00:00
|
|
|
addedAccount(jid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Account::onRosterItemAdded(const QString& bareJid)
|
|
|
|
{
|
|
|
|
addedAccount(bareJid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Account::onRosterItemChanged(const QString& bareJid)
|
|
|
|
{
|
|
|
|
QXmppRosterManager& rm = client.rosterManager();
|
|
|
|
QXmppRosterIq::Item re = rm.getRosterEntry(bareJid);
|
|
|
|
QSet<QString> newGroups = re.groups();
|
|
|
|
QSet<QString> oldGroups;
|
|
|
|
|
|
|
|
emit changeContact(bareJid, re.name());
|
|
|
|
|
|
|
|
for (std::map<QString, std::set<QString>>::iterator itr = groups.begin(), end = groups.end(); itr != end; ++itr) {
|
|
|
|
std::set<QString>& contacts = itr->second;
|
|
|
|
std::set<QString>::const_iterator cItr = contacts.find(bareJid);
|
|
|
|
if (cItr != contacts.end()) {
|
|
|
|
oldGroups.insert(itr->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> toRemove = oldGroups - newGroups;
|
|
|
|
QSet<QString> toAdd = newGroups - oldGroups;
|
|
|
|
|
|
|
|
QSet<QString> removeGroups;
|
|
|
|
for (QSet<QString>::iterator itr = toRemove.begin(), end = toRemove.end(); itr != end; ++itr) {
|
|
|
|
const QString& groupName = *itr;
|
|
|
|
std::set<QString>& contacts = groups.find(groupName)->second;
|
|
|
|
contacts.erase(bareJid);
|
|
|
|
emit removeContact(bareJid, groupName);
|
|
|
|
if (contacts.size() == 0) {
|
|
|
|
removeGroups.insert(groupName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (QSet<QString>::iterator itr = toAdd.begin(), end = toAdd.end(); itr != end; ++itr) {
|
|
|
|
const QString& groupName = *itr;
|
|
|
|
std::map<QString, std::set<QString>>::iterator cItr = groups.find(groupName);
|
|
|
|
if (cItr == groups.end()) {
|
|
|
|
cItr = groups.insert(std::make_pair(groupName, std::set<QString>())).first;
|
|
|
|
emit addGroup(groupName);
|
|
|
|
}
|
|
|
|
cItr->second.insert(bareJid);
|
|
|
|
emit addContact(bareJid, re.name(), groupName);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (QSet<QString>::iterator itr = removeGroups.begin(), end = removeGroups.end(); itr != end; ++itr) {
|
|
|
|
const QString& groupName = *itr;
|
|
|
|
emit removeGroup(groupName);
|
|
|
|
groups.erase(groupName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Account::onRosterItemRemoved(const QString& bareJid)
|
|
|
|
{
|
|
|
|
emit removeContact(bareJid);
|
|
|
|
|
|
|
|
QSet<QString> toRemove;
|
|
|
|
for (std::map<QString, std::set<QString>>::iterator itr = groups.begin(), end = groups.end(); itr != end; ++itr) {
|
|
|
|
std::set<QString> contacts = itr->second;
|
|
|
|
std::set<QString>::const_iterator cItr = contacts.find(bareJid);
|
|
|
|
if (cItr != contacts.end()) {
|
|
|
|
contacts.erase(cItr);
|
|
|
|
if (contacts.size() == 0) {
|
|
|
|
toRemove.insert(itr->first);
|
2019-04-03 21:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-06 10:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (QSet<QString>::iterator itr = toRemove.begin(), end = toRemove.end(); itr != end; ++itr) {
|
|
|
|
const QString& groupName = *itr;
|
|
|
|
emit removeGroup(groupName);
|
|
|
|
groups.erase(groupName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Account::addedAccount(const QString& jid)
|
|
|
|
{
|
|
|
|
QXmppRosterManager& rm = client.rosterManager();
|
|
|
|
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, std::set<QString>>::iterator gItr = groups.find(groupName);
|
|
|
|
if (gItr == groups.end()) {
|
|
|
|
gItr = groups.insert(std::make_pair(groupName, std::set<QString>())).first;
|
|
|
|
emit addGroup(groupName);
|
2019-04-03 21:23:51 +00:00
|
|
|
}
|
2019-04-06 10:14:32 +00:00
|
|
|
gItr->second.insert(jid);
|
|
|
|
emit addContact(jid, re.name(), groupName);
|
|
|
|
grCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grCount == 0) {
|
|
|
|
emit addContact(jid, re.name(), "");
|
2019-04-03 21:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|