2020-06-14 21:23:43 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rosterhandler.h"
|
|
|
|
#include "core/account.h"
|
|
|
|
|
|
|
|
Core::RosterHandler::RosterHandler(Core::Account* account):
|
|
|
|
QObject(),
|
|
|
|
acc(account),
|
|
|
|
contacts(),
|
|
|
|
conferences(),
|
|
|
|
groups(),
|
|
|
|
queuedContacts(),
|
2022-12-19 15:43:24 +00:00
|
|
|
outOfRosterContacts() {}
|
|
|
|
|
|
|
|
void Core::RosterHandler::initialize() {
|
2020-06-14 21:23:43 +00:00
|
|
|
connect(acc->rm, &QXmppRosterManager::rosterReceived, this, &RosterHandler::onRosterReceived);
|
|
|
|
connect(acc->rm, &QXmppRosterManager::itemAdded, this, &RosterHandler::onRosterItemAdded);
|
|
|
|
connect(acc->rm, &QXmppRosterManager::itemRemoved, this, &RosterHandler::onRosterItemRemoved);
|
|
|
|
connect(acc->rm, &QXmppRosterManager::itemChanged, this, &RosterHandler::onRosterItemChanged);
|
2022-12-19 15:43:24 +00:00
|
|
|
|
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
connect(acc->mm, &QXmppMucManager::roomAdded, this, &RosterHandler::onMucRoomAdded);
|
|
|
|
connect(acc->bm, &QXmppBookmarkManager::bookmarksReceived, this, &RosterHandler::bookmarksReceived);
|
2022-08-27 11:39:24 +00:00
|
|
|
|
|
|
|
connect(acc, &Account::pepSupportChanged, this, &RosterHandler::onPepSupportedChanged);
|
2023-03-17 23:50:04 +00:00
|
|
|
|
|
|
|
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
|
|
|
connect(acc->th, &TrustHandler::trustLevelsChanged, this, &RosterHandler::onTrustChanged);
|
|
|
|
#endif
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
Core::RosterHandler::~RosterHandler() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::RosterHandler::clear() {
|
|
|
|
for (const std::pair<const QString, Contact*>& pair : contacts)
|
|
|
|
delete pair.second;
|
|
|
|
|
|
|
|
for (const std::pair<const QString, Conference*>& pair : conferences)
|
|
|
|
delete pair.second;
|
|
|
|
|
|
|
|
contacts.clear();
|
|
|
|
conferences.clear();
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
|
|
|
|
void Core::RosterHandler::onRosterReceived() {
|
2020-06-14 21:23:43 +00:00
|
|
|
QStringList bj = acc->rm->getRosterBareJids();
|
|
|
|
for (int i = 0; i < bj.size(); ++i) {
|
|
|
|
const QString& jid = bj[i];
|
2020-07-26 19:41:30 +00:00
|
|
|
addedAccount(jid.toLower());
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onRosterItemAdded(const QString& bareJid) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = bareJid.toLower();
|
|
|
|
addedAccount(lcJid);
|
|
|
|
std::map<QString, QString>::const_iterator itr = queuedContacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr != queuedContacts.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
acc->rm->subscribe(lcJid, itr->second);
|
2020-06-14 21:23:43 +00:00
|
|
|
queuedContacts.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::addedAccount(const QString& jid) {
|
2020-06-14 21:23:43 +00:00
|
|
|
std::map<QString, Contact*>::const_iterator itr = contacts.find(jid);
|
|
|
|
QXmppRosterIq::Item re = acc->rm->getRosterEntry(jid);
|
|
|
|
Contact* contact;
|
|
|
|
bool newContact = false;
|
|
|
|
if (itr == contacts.end()) {
|
|
|
|
newContact = true;
|
|
|
|
contact = new Contact(jid, acc->name);
|
|
|
|
contacts.insert(std::make_pair(jid, contact));
|
|
|
|
} else {
|
|
|
|
contact = itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> gr = re.groups();
|
|
|
|
Shared::SubscriptionState state = castSubscriptionState(re.subscriptionType());
|
|
|
|
contact->setGroups(gr);
|
|
|
|
contact->setSubscriptionState(state);
|
|
|
|
contact->setName(re.name());
|
|
|
|
|
|
|
|
if (newContact) {
|
2023-03-16 19:38:05 +00:00
|
|
|
QMap<QString, QVariant> cData = contact->getInfo();
|
2023-03-17 20:59:51 +00:00
|
|
|
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
|
|
|
cData.insert("trust", QVariant::fromValue(acc->th->getSummary(jid)));
|
|
|
|
#endif
|
2020-06-14 21:23:43 +00:00
|
|
|
int grCount = 0;
|
|
|
|
for (QSet<QString>::const_iterator itr = gr.begin(), end = gr.end(); itr != end; ++itr) {
|
|
|
|
const QString& groupName = *itr;
|
|
|
|
addToGroup(jid, groupName);
|
|
|
|
emit acc->addContact(jid, groupName, cData);
|
|
|
|
grCount++;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
if (grCount == 0)
|
2020-06-14 21:23:43 +00:00
|
|
|
emit acc->addContact(jid, "", cData);
|
2023-03-16 19:38:05 +00:00
|
|
|
|
2022-08-27 11:39:24 +00:00
|
|
|
if (acc->pepSupport == Shared::Support::supported) {
|
2022-08-25 22:49:49 +00:00
|
|
|
acc->dm->requestInfo(jid);
|
|
|
|
//acc->dm->requestItems(jid);
|
|
|
|
}
|
2020-06-14 21:23:43 +00:00
|
|
|
handleNewContact(contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::addNewRoom(const QString& jid, const QString& nick, const QString& roomName, bool autoJoin) {
|
2020-06-14 21:23:43 +00:00
|
|
|
QXmppMucRoom* room = acc->mm->addRoom(jid);
|
|
|
|
QString lNick = nick;
|
2023-03-16 19:38:05 +00:00
|
|
|
if (lNick.size() == 0)
|
2020-06-14 21:23:43 +00:00
|
|
|
lNick = acc->getName();
|
2023-03-16 19:38:05 +00:00
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* conf = new Conference(jid, acc->getName(), autoJoin, roomName, lNick, room);
|
|
|
|
conferences.insert(std::make_pair(jid, conf));
|
|
|
|
|
|
|
|
handleNewConference(conf);
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
QMap<QString, QVariant> cData = conf->getInfo();
|
2020-06-14 21:23:43 +00:00
|
|
|
emit acc->addRoom(jid, cData);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::addContactRequest(const QString& jid, const QString& name, const QSet<QString>& groups) {
|
2020-06-14 21:23:43 +00:00
|
|
|
if (acc->state == Shared::ConnectionState::connected) {
|
|
|
|
std::map<QString, QString>::const_iterator itr = queuedContacts.find(jid);
|
|
|
|
if (itr != queuedContacts.end()) {
|
|
|
|
qDebug() << "An attempt to add contact " << jid << " to account " << acc->name << " but the account is already queued for adding, skipping";
|
|
|
|
} else {
|
|
|
|
queuedContacts.insert(std::make_pair(jid, "")); //TODO need to add reason here;
|
|
|
|
acc->rm->addItem(jid, name, groups);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qDebug() << "An attempt to add contact " << jid << " to account " << acc->name << " but the account is not in the connected state, skipping";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::removeContactRequest(const QString& jid) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
2020-06-14 21:23:43 +00:00
|
|
|
if (acc->state == Shared::ConnectionState::connected) {
|
2020-07-26 19:41:30 +00:00
|
|
|
std::set<QString>::const_iterator itr = outOfRosterContacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr != outOfRosterContacts.end()) {
|
|
|
|
outOfRosterContacts.erase(itr);
|
2020-07-26 19:41:30 +00:00
|
|
|
onRosterItemRemoved(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
acc->rm->removeItem(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to remove contact " << lcJid << " from account " << acc->name << " but the account is not in the connected state, skipping";
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::handleNewRosterItem(Core::RosterItem* contact) {
|
2020-06-14 21:23:43 +00:00
|
|
|
connect(contact, &RosterItem::needHistory, this->acc, &Account::onContactNeedHistory);
|
2020-08-21 20:57:48 +00:00
|
|
|
connect(contact, &RosterItem::historyResponse, this->acc, &Account::onContactHistoryResponse);
|
2020-06-14 21:23:43 +00:00
|
|
|
connect(contact, &RosterItem::nameChanged, this, &RosterHandler::onContactNameChanged);
|
|
|
|
connect(contact, &RosterItem::avatarChanged, this, &RosterHandler::onContactAvatarChanged);
|
2023-03-16 19:38:05 +00:00
|
|
|
connect(contact, &RosterItem::encryptionChanged, this, &RosterHandler::onContactEncryptionChanged);
|
2023-03-10 18:43:31 +00:00
|
|
|
connect(contact, &RosterItem::requestVCard, acc->delay, &DelayManager::Manager::getVCard);
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::handleNewContact(Core::Contact* contact) {
|
2020-06-14 21:23:43 +00:00
|
|
|
handleNewRosterItem(contact);
|
|
|
|
connect(contact, &Contact::groupAdded, this, &RosterHandler::onContactGroupAdded);
|
|
|
|
connect(contact, &Contact::groupRemoved, this, &RosterHandler::onContactGroupRemoved);
|
|
|
|
connect(contact, &Contact::subscriptionStateChanged, this, &RosterHandler::onContactSubscriptionStateChanged);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::handleNewConference(Core::Conference* contact) {
|
2020-06-14 21:23:43 +00:00
|
|
|
handleNewRosterItem(contact);
|
|
|
|
connect(contact, &Conference::nickChanged, this, &RosterHandler::onMucNickNameChanged);
|
|
|
|
connect(contact, &Conference::subjectChanged, this, &RosterHandler::onMucSubjectChanged);
|
|
|
|
connect(contact, &Conference::joinedChanged, this, &RosterHandler::onMucJoinedChanged);
|
|
|
|
connect(contact, &Conference::autoJoinChanged, this, &RosterHandler::onMucAutoJoinChanged);
|
|
|
|
connect(contact, &Conference::addParticipant, this, &RosterHandler::onMucAddParticipant);
|
|
|
|
connect(contact, &Conference::changeParticipant, this, &RosterHandler::onMucChangeParticipant);
|
|
|
|
connect(contact, &Conference::removeParticipant, this, &RosterHandler::onMucRemoveParticipant);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucAddParticipant(const QString& nickName, const QMap<QString, QVariant>& data) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
|
|
|
emit acc->addRoomParticipant(room->jid, nickName, data);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucChangeParticipant(const QString& nickName, const QMap<QString, QVariant>& data) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
|
|
|
emit acc->changeRoomParticipant(room->jid, nickName, data);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucRemoveParticipant(const QString& nickName) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
|
|
|
emit acc->removeRoomParticipant(room->jid, nickName);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucSubjectChanged(const QString& subject) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
2023-03-16 19:38:05 +00:00
|
|
|
emit acc->changeRoom(room->jid, {{"subject", subject}});
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onContactGroupAdded(const QString& group) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Contact* contact = static_cast<Contact*>(sender());
|
|
|
|
if (contact->groupsCount() == 1) {
|
|
|
|
// not sure i need to handle it here, the situation with grouped and ungrouped contacts handled on the client anyway
|
|
|
|
}
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
QMap<QString, QVariant> cData({
|
|
|
|
{"name", contact->getName()},
|
2023-03-16 19:38:05 +00:00
|
|
|
{"state", QVariant::fromValue(contact->getSubscriptionState())},
|
2023-03-17 23:50:04 +00:00
|
|
|
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
|
|
|
{"trust", QVariant::fromValue(acc->th->getSummary(contact->jid))},
|
|
|
|
#endif
|
2023-03-16 19:38:05 +00:00
|
|
|
{"encryption", contact->isEncryptionEnabled()}
|
2020-06-14 21:23:43 +00:00
|
|
|
});
|
|
|
|
addToGroup(contact->jid, group);
|
|
|
|
emit acc->addContact(contact->jid, group, cData);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onContactGroupRemoved(const QString& group) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Contact* contact = static_cast<Contact*>(sender());
|
|
|
|
if (contact->groupsCount() == 0) {
|
|
|
|
// not sure i need to handle it here, the situation with grouped and ungrouped contacts handled on the client anyway
|
|
|
|
}
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
emit acc->removeContact(contact->jid, group);
|
|
|
|
removeFromGroup(contact->jid, group);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onContactNameChanged(const QString& cname) {
|
|
|
|
RosterItem* contact = static_cast<RosterItem*>(sender());
|
|
|
|
emit acc->changeContact(contact->jid, {{"name", cname}});
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onContactEncryptionChanged(bool value) {
|
|
|
|
RosterItem* contact = static_cast<RosterItem*>(sender());
|
|
|
|
emit acc->changeContact(contact->jid, {{"encryption", value}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::RosterHandler::onContactSubscriptionStateChanged(Shared::SubscriptionState cstate) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Contact* contact = static_cast<Contact*>(sender());
|
2023-03-17 23:50:04 +00:00
|
|
|
emit acc->changeContact(contact->jid, {{"state", QVariant::fromValue(cstate)}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::RosterHandler::onTrustChanged(const QString& jid, const Shared::TrustSummary& trust) {
|
|
|
|
emit acc->changeContact(jid, {{"trust", QVariant::fromValue(trust)}});
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::addToGroup(const QString& jid, const QString& group) {
|
2020-06-14 21:23:43 +00:00
|
|
|
std::map<QString, std::set<QString>>::iterator gItr = groups.find(group);
|
|
|
|
if (gItr == groups.end()) {
|
|
|
|
gItr = groups.insert(std::make_pair(group, std::set<QString>())).first;
|
|
|
|
emit acc->addGroup(group);
|
|
|
|
}
|
2020-07-26 19:41:30 +00:00
|
|
|
gItr->second.insert(jid.toLower());
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::removeFromGroup(const QString& jid, const QString& group) {
|
2020-06-14 21:23:43 +00:00
|
|
|
QSet<QString> toRemove;
|
|
|
|
std::map<QString, std::set<QString>>::iterator itr = groups.find(group);
|
|
|
|
if (itr == groups.end()) {
|
|
|
|
qDebug() << "An attempt to remove contact" << jid << "of account" << acc->name << "from non existing group" << group << ", skipping";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::set<QString> contacts = itr->second;
|
2020-07-26 19:41:30 +00:00
|
|
|
std::set<QString>::const_iterator cItr = contacts.find(jid.toLower());
|
2020-06-14 21:23:43 +00:00
|
|
|
if (cItr != contacts.end()) {
|
|
|
|
contacts.erase(cItr);
|
|
|
|
if (contacts.size() == 0) {
|
|
|
|
emit acc->removeGroup(group);
|
|
|
|
groups.erase(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
Core::RosterItem * Core::RosterHandler::getRosterItem(const QString& jid) {
|
|
|
|
RosterItem* item = nullptr;
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
|
|
|
std::map<QString, Contact*>::const_iterator citr = contacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (citr != contacts.end()) {
|
|
|
|
item = citr->second;
|
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
std::map<QString, Conference*>::const_iterator coitr = conferences.find(lcJid);
|
2023-03-16 19:38:05 +00:00
|
|
|
if (coitr != conferences.end())
|
2020-06-14 21:23:43 +00:00
|
|
|
item = coitr->second;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
Core::Conference * Core::RosterHandler::getConference(const QString& jid) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* item = 0;
|
2020-07-26 19:41:30 +00:00
|
|
|
std::map<QString, Conference*>::const_iterator coitr = conferences.find(jid.toLower());
|
2023-03-17 23:50:04 +00:00
|
|
|
if (coitr != conferences.end())
|
2020-06-14 21:23:43 +00:00
|
|
|
item = coitr->second;
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
Core::Contact * Core::RosterHandler::getContact(const QString& jid) {
|
2020-06-14 21:23:43 +00:00
|
|
|
Contact* item = 0;
|
2020-07-26 19:41:30 +00:00
|
|
|
std::map<QString, Contact*>::const_iterator citr = contacts.find(jid.toLower());
|
2023-03-17 23:50:04 +00:00
|
|
|
if (citr != contacts.end())
|
2020-06-14 21:23:43 +00:00
|
|
|
item = citr->second;
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
Core::Contact * Core::RosterHandler::addOutOfRosterContact(const QString& jid) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
|
|
|
Contact* cnt = new Contact(lcJid, acc->name);
|
|
|
|
contacts.insert(std::make_pair(lcJid, cnt));
|
|
|
|
outOfRosterContacts.insert(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
cnt->setSubscriptionState(Shared::SubscriptionState::unknown);
|
2020-07-26 19:41:30 +00:00
|
|
|
emit acc->addContact(lcJid, "", QMap<QString, QVariant>({
|
2023-03-16 19:38:05 +00:00
|
|
|
{"state", QVariant::fromValue(Shared::SubscriptionState::unknown)},
|
|
|
|
{"encryption", false}
|
2020-06-14 21:23:43 +00:00
|
|
|
}));
|
|
|
|
handleNewContact(cnt);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onRosterItemChanged(const QString& bareJid) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = bareJid.toLower();
|
|
|
|
std::map<QString, Contact*>::const_iterator itr = contacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr == contacts.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to change non existing contact" << lcJid << "from account" << acc->name << ", skipping";
|
2020-06-14 21:23:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Contact* contact = itr->second;
|
2020-07-26 19:41:30 +00:00
|
|
|
QXmppRosterIq::Item re = acc->rm->getRosterEntry(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
|
|
|
|
Shared::SubscriptionState state = castSubscriptionState(re.subscriptionType());
|
|
|
|
|
|
|
|
contact->setGroups(re.groups());
|
|
|
|
contact->setSubscriptionState(state);
|
|
|
|
contact->setName(re.name());
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onRosterItemRemoved(const QString& bareJid) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = bareJid.toLower();
|
|
|
|
std::map<QString, Contact*>::const_iterator itr = contacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr == contacts.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to remove non existing contact" << lcJid << "from account" << acc->name << ", skipping";
|
2020-06-14 21:23:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Contact* contact = itr->second;
|
|
|
|
contacts.erase(itr);
|
|
|
|
QSet<QString> cGroups = contact->getGroups();
|
|
|
|
for (QSet<QString>::const_iterator itr = cGroups.begin(), end = cGroups.end(); itr != end; ++itr) {
|
2020-07-26 19:41:30 +00:00
|
|
|
removeFromGroup(lcJid, *itr);
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
2020-07-26 19:41:30 +00:00
|
|
|
emit acc->removeContact(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
|
|
|
|
contact->deleteLater();
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucRoomAdded(QXmppMucRoom* room) {
|
2020-06-14 21:23:43 +00:00
|
|
|
qDebug() << "room" << room->jid() << "added with name" << room->name()
|
|
|
|
<< ", account" << acc->getName() << "joined:" << room->isJoined();
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::bookmarksReceived(const QXmppBookmarkSet& bookmarks) {
|
2020-06-14 21:23:43 +00:00
|
|
|
QList<QXmppBookmarkConference> confs = bookmarks.conferences();
|
|
|
|
for (QList<QXmppBookmarkConference>::const_iterator itr = confs.begin(), end = confs.end(); itr != end; ++itr) {
|
|
|
|
const QXmppBookmarkConference& c = *itr;
|
|
|
|
|
2020-07-26 19:41:30 +00:00
|
|
|
QString jid = c.jid().toLower();
|
2020-06-14 21:23:43 +00:00
|
|
|
std::map<QString, Conference*>::const_iterator cItr = conferences.find(jid);
|
|
|
|
if (cItr == conferences.end()) {
|
|
|
|
addNewRoom(jid, c.nickName(), c.name(), c.autoJoin());
|
|
|
|
} else {
|
2023-03-16 19:38:05 +00:00
|
|
|
if (c.autoJoin())
|
2020-06-20 22:26:30 +00:00
|
|
|
cItr->second->setJoined(true);
|
2023-03-16 19:38:05 +00:00
|
|
|
else
|
2020-06-20 22:26:30 +00:00
|
|
|
cItr->second->setAutoJoin(false);
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucJoinedChanged(bool joined){
|
2020-06-14 21:23:43 +00:00
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
2023-03-16 19:38:05 +00:00
|
|
|
emit acc->changeRoom(room->jid, {{"joined", joined}});
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucAutoJoinChanged(bool autoJoin) {
|
2020-06-14 21:23:43 +00:00
|
|
|
storeConferences();
|
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
2023-03-16 19:38:05 +00:00
|
|
|
emit acc->changeRoom(room->jid, {{"autoJoin", autoJoin}});
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onMucNickNameChanged(const QString& nickName){
|
2020-06-14 21:23:43 +00:00
|
|
|
storeConferences();
|
|
|
|
Conference* room = static_cast<Conference*>(sender());
|
2023-03-16 19:38:05 +00:00
|
|
|
emit acc->changeRoom(room->jid, {{"nick", nickName}});
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
Shared::SubscriptionState Core::RosterHandler::castSubscriptionState(QXmppRosterIq::Item::SubscriptionType qs){
|
2020-06-14 21:23:43 +00:00
|
|
|
Shared::SubscriptionState state;
|
2023-03-16 19:38:05 +00:00
|
|
|
if (qs == QXmppRosterIq::Item::NotSet)
|
2020-06-14 21:23:43 +00:00
|
|
|
state = Shared::SubscriptionState::unknown;
|
2023-03-16 19:38:05 +00:00
|
|
|
else
|
2020-06-14 21:23:43 +00:00
|
|
|
state = static_cast<Shared::SubscriptionState>(qs);
|
2023-03-16 19:38:05 +00:00
|
|
|
|
2020-06-14 21:23:43 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::storeConferences() {
|
2020-06-14 21:23:43 +00:00
|
|
|
QXmppBookmarkSet bms = acc->bm->bookmarks();
|
|
|
|
QList<QXmppBookmarkConference> confs;
|
|
|
|
for (std::map<QString, Conference*>::const_iterator itr = conferences.begin(), end = conferences.end(); itr != end; ++itr) {
|
|
|
|
Conference* conference = itr->second;
|
|
|
|
QXmppBookmarkConference conf;
|
|
|
|
conf.setJid(conference->jid);
|
|
|
|
conf.setName(conference->getName());
|
|
|
|
conf.setNickName(conference->getNick());
|
|
|
|
conf.setAutoJoin(conference->getAutoJoin());
|
|
|
|
confs.push_back(conf);
|
|
|
|
}
|
|
|
|
bms.setConferences(confs);
|
|
|
|
acc->bm->setBookmarks(bms);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::clearConferences() {
|
2020-06-14 21:23:43 +00:00
|
|
|
for (std::map<QString, Conference*>::const_iterator itr = conferences.begin(), end = conferences.end(); itr != end; itr++) {
|
|
|
|
itr->second->deleteLater();
|
|
|
|
emit acc->removeRoom(itr->first);
|
|
|
|
}
|
|
|
|
conferences.clear();
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::removeRoomRequest(const QString& jid) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
|
|
|
std::map<QString, Conference*>::const_iterator itr = conferences.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr == conferences.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to remove non existing room" << lcJid << "from account" << acc->name << ", skipping";
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
itr->second->deleteLater();
|
|
|
|
conferences.erase(itr);
|
2020-07-26 19:41:30 +00:00
|
|
|
emit acc->removeRoom(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
storeConferences();
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
|
|
|
std::map<QString, Conference*>::const_iterator cItr = conferences.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (cItr == conferences.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
addNewRoom(lcJid, nick, "", autoJoin);
|
2020-06-14 21:23:43 +00:00
|
|
|
storeConferences();
|
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to add a MUC " << lcJid << " which is already present in the rester, skipping";
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::addContactToGroupRequest(const QString& jid, const QString& groupName) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
|
|
|
std::map<QString, Contact*>::const_iterator itr = contacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr == contacts.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to add non existing contact" << lcJid << "of account"
|
2020-06-14 21:23:43 +00:00
|
|
|
<< acc->name << "to the group" << groupName << ", skipping";
|
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
QXmppRosterIq::Item item = acc->rm->getRosterEntry(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
QSet<QString> groups = item.groups();
|
|
|
|
if (groups.find(groupName) == groups.end()) { //TODO need to change it, I guess that sort of code is better in qxmpp lib
|
|
|
|
groups.insert(groupName);
|
|
|
|
item.setGroups(groups);
|
|
|
|
|
|
|
|
QXmppRosterIq iq;
|
|
|
|
iq.setType(QXmppIq::Set);
|
|
|
|
iq.addItem(item);
|
|
|
|
acc->client.sendPacket(iq);
|
|
|
|
} else {
|
|
|
|
qDebug() << "An attempt to add contact" << jid << "of account"
|
|
|
|
<< acc->name << "to the group" << groupName << "but it's already in that group, skipping";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::removeContactFromGroupRequest(const QString& jid, const QString& groupName) {
|
2020-07-26 19:41:30 +00:00
|
|
|
QString lcJid = jid.toLower();
|
|
|
|
std::map<QString, Contact*>::const_iterator itr = contacts.find(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
if (itr == contacts.end()) {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to remove non existing contact" << lcJid << "of account"
|
2020-06-14 21:23:43 +00:00
|
|
|
<< acc->name << "from the group" << groupName << ", skipping";
|
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
QXmppRosterIq::Item item = acc->rm->getRosterEntry(lcJid);
|
2020-06-14 21:23:43 +00:00
|
|
|
QSet<QString> groups = item.groups();
|
|
|
|
QSet<QString>::const_iterator gItr = groups.find(groupName);
|
|
|
|
if (gItr != groups.end()) {
|
|
|
|
groups.erase(gItr);
|
|
|
|
item.setGroups(groups);
|
|
|
|
|
|
|
|
QXmppRosterIq iq;
|
|
|
|
iq.setType(QXmppIq::Set);
|
|
|
|
iq.addItem(item);
|
|
|
|
acc->client.sendPacket(iq);
|
|
|
|
} else {
|
2020-07-26 19:41:30 +00:00
|
|
|
qDebug() << "An attempt to remove contact" << lcJid << "of account"
|
2020-06-14 21:23:43 +00:00
|
|
|
<< acc->name << "from the group" << groupName << "but it's not in that group, skipping";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onContactAvatarChanged(Shared::Avatar type, const QString& path) {
|
2020-06-14 21:23:43 +00:00
|
|
|
RosterItem* item = static_cast<RosterItem*>(sender());
|
|
|
|
QMap<QString, QVariant> cData({
|
2023-03-16 19:38:05 +00:00
|
|
|
{"avatarState", QVariant::fromValue(type)},
|
2020-06-14 21:23:43 +00:00
|
|
|
{"avatarPath", path}
|
|
|
|
});
|
|
|
|
|
|
|
|
emit acc->changeContact(item->jid, cData);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::handleOffline() {
|
2020-06-14 21:23:43 +00:00
|
|
|
for (const std::pair<const QString, Conference*>& pair : conferences) {
|
|
|
|
pair.second->clearArchiveRequests();
|
2020-06-20 22:26:30 +00:00
|
|
|
pair.second->downgradeDatabaseState();
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
|
|
|
for (const std::pair<const QString, Contact*>& pair : contacts) {
|
|
|
|
pair.second->clearArchiveRequests();
|
2020-06-20 22:26:30 +00:00
|
|
|
pair.second->downgradeDatabaseState();
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|
2022-04-08 16:18:15 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:38:05 +00:00
|
|
|
void Core::RosterHandler::onPepSupportedChanged(Shared::Support support) {
|
2022-08-27 11:39:24 +00:00
|
|
|
if (support == Shared::Support::supported) {
|
|
|
|
for (const std::pair<const QString, Contact*>& pair : contacts) {
|
2023-03-16 19:38:05 +00:00
|
|
|
if (pair.second->getPepSupport() == Shared::Support::unknown)
|
2022-08-27 11:39:24 +00:00
|
|
|
acc->dm->requestInfo(pair.first);
|
2022-08-25 22:49:49 +00:00
|
|
|
}
|
2022-04-08 16:18:15 +00:00
|
|
|
}
|
2020-06-14 21:23:43 +00:00
|
|
|
}
|