squawk/core/conference.cpp

328 lines
11 KiB
C++
Raw Normal View History

2019-08-21 09:35:07 +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 "conference.h"
#include <QDebug>
Core::Conference::Conference(const QString& p_jid, const QString& p_account, bool p_autoJoin, const QString& p_name, const QString& p_nick, QXmppMucRoom* p_room):
RosterItem(p_jid, p_account),
nick(p_nick),
room(p_room),
joined(false),
2020-04-13 19:57:23 +00:00
autoJoin(p_autoJoin),
exParticipants()
2019-08-21 09:35:07 +00:00
{
2019-08-28 11:40:55 +00:00
muc = true;
2019-08-21 09:35:07 +00:00
name = p_name;
connect(room, &QXmppMucRoom::joined, this, &Conference::onRoomJoined);
connect(room, &QXmppMucRoom::left, this, &Conference::onRoomLeft);
connect(room, &QXmppMucRoom::nameChanged, this, &Conference::onRoomNameChanged);
connect(room, &QXmppMucRoom::subjectChanged, this, &Conference::onRoomSubjectChanged);
connect(room, &QXmppMucRoom::participantAdded, this, &Conference::onRoomParticipantAdded);
connect(room, &QXmppMucRoom::participantChanged, this, &Conference::onRoomParticipantChanged);
connect(room, &QXmppMucRoom::participantRemoved, this, &Conference::onRoomParticipantRemoved);
connect(room, &QXmppMucRoom::nickNameChanged, this, &Conference::onRoomNickNameChanged);
connect(room, &QXmppMucRoom::error, this, &Conference::onRoomError);
2019-08-21 09:35:07 +00:00
room->setNickName(nick);
if (autoJoin)
2019-08-21 09:35:07 +00:00
room->join();
2020-04-13 19:57:23 +00:00
archive->readAllResourcesAvatars(exParticipants);
2019-08-21 09:35:07 +00:00
}
Core::Conference::~Conference(){
if (joined)
room->leave();
room->deleteLater();
2019-08-21 09:35:07 +00:00
}
QString Core::Conference::getNick() const {
2019-08-21 09:35:07 +00:00
return nick;
}
bool Core::Conference::getAutoJoin() const {
2019-08-21 09:35:07 +00:00
return autoJoin;
}
bool Core::Conference::getJoined() const {
2019-08-21 09:35:07 +00:00
return joined;
}
void Core::Conference::setJoined(bool p_joined) {
2019-08-21 09:35:07 +00:00
if (joined != p_joined) {
if (p_joined)
2019-08-21 09:35:07 +00:00
room->join();
else
2019-08-21 09:35:07 +00:00
room->leave();
}
}
void Core::Conference::setAutoJoin(bool p_autoJoin) {
2019-08-21 09:35:07 +00:00
if (autoJoin != p_autoJoin) {
autoJoin = p_autoJoin;
emit autoJoinChanged(autoJoin);
}
}
void Core::Conference::setNick(const QString& p_nick) {
2019-08-21 09:35:07 +00:00
if (nick != p_nick) {
if (joined) {
room->setNickName(p_nick);
} else {
nick = p_nick;
emit nickChanged(nick);
}
}
}
void Core::Conference::onRoomJoined() {
2019-08-21 09:35:07 +00:00
joined = true;
emit joinedChanged(joined);
}
void Core::Conference::onRoomLeft() {
2019-08-21 09:35:07 +00:00
joined = false;
emit joinedChanged(joined);
}
void Core::Conference::onRoomNameChanged(const QString& p_name) {
2019-08-21 09:35:07 +00:00
setName(p_name);
}
void Core::Conference::onRoomNickNameChanged(const QString& p_nick) {
2019-08-21 09:35:07 +00:00
if (p_nick != nick) {
nick = p_nick;
emit nickChanged(nick);
}
}
void Core::Conference::onRoomError(const QXmppStanza::Error& err) {
qDebug() << "MUC" << jid << "error:" << err.text();
}
void Core::Conference::onRoomParticipantAdded(const QString& p_name) {
QStringList comps = p_name.split("/");
QString resource = comps.back();
2020-04-05 13:25:27 +00:00
QXmppPresence pres = room->participantPresence(p_name);
QXmppMucItem mi = pres.mucItem();
if (resource == jid)
2020-04-05 13:25:27 +00:00
resource = "";
2020-04-13 19:57:23 +00:00
std::map<QString, Archive::AvatarInfo>::const_iterator itr = exParticipants.find(resource);
bool hasAvatar = itr != exParticipants.end();
2020-04-05 13:25:27 +00:00
if (resource.size() > 0) {
QDateTime lastInteraction = pres.lastUserInteraction();
if (!lastInteraction.isValid())
lastInteraction = QDateTime::currentDateTimeUtc();
2019-12-30 20:22:04 +00:00
QMap<QString, QVariant> cData = {
{"lastActivity", lastInteraction},
{"availability", pres.availableStatusType()},
{"status", pres.statusText()},
{"affiliation", mi.affiliation()},
{"role", mi.role()},
{"client", QVariant::fromValue(
Shared::ClientId(
pres.capabilityNode(),
pres.capabilityVer().toBase64(),
pres.capabilityHash())
)
}
2019-12-30 20:22:04 +00:00
};
careAboutAvatar(hasAvatar, itr->second, cData, resource, p_name);
2019-12-30 20:22:04 +00:00
emit addParticipant(resource, cData);
if (!hasAvatar) // because this way vCard is already requested, no need to handle possible avatar update
return;
}
handlePossibleAvatarUpdate(pres, resource, hasAvatar, itr->second);
}
void Core::Conference::handlePossibleAvatarUpdate (
const QXmppPresence& pres,
const QString& resource,
bool hasAvatar,
const Archive::AvatarInfo& info
) {
2020-04-05 13:25:27 +00:00
switch (pres.vCardUpdateType()) {
case QXmppPresence::VCardUpdateNone: //this presence has nothing to do with photo
break;
case QXmppPresence::VCardUpdateNotReady: //let's say the photo didn't change here
break;
case QXmppPresence::VCardUpdateNoPhoto: //there is no photo, need to drop if any
if (!hasAvatar || !info.autogenerated)
2020-04-05 13:25:27 +00:00
setAutoGeneratedAvatar(resource);
break;
case QXmppPresence::VCardUpdateValidPhoto: //there is a photo, need to load
if (hasAvatar) {
if (info.autogenerated || info.hash != pres.photoHash())
emit requestVCard(pres.from());
2020-04-05 13:25:27 +00:00
} else {
emit requestVCard(pres.from());
2020-04-05 13:25:27 +00:00
}
break;
}
}
void Core::Conference::onRoomParticipantChanged(const QString& p_name) {
QStringList comps = p_name.split("/");
QString resource = comps.back();
2020-04-05 13:25:27 +00:00
QXmppPresence pres = room->participantPresence(p_name);
QXmppMucItem mi = pres.mucItem();
handlePresence(pres);
if (resource != jid) {
QDateTime lastInteraction = pres.lastUserInteraction();
if (!lastInteraction.isValid())
lastInteraction = QDateTime::currentDateTimeUtc();
emit changeParticipant(resource, {
{"lastActivity", lastInteraction},
{"availability", pres.availableStatusType()},
{"status", pres.statusText()},
{"affiliation", mi.affiliation()},
{"role", mi.role()},
{"client", QVariant::fromValue(
Shared::ClientId(
pres.capabilityNode(),
pres.capabilityVer().toBase64(),
pres.capabilityHash())
)
}
});
}
}
void Core::Conference::onRoomParticipantRemoved(const QString& p_name) {
QStringList comps = p_name.split("/");
QString resource = comps.back();
if (resource == jid) {
qDebug() << "Room" << jid << "is reporting of removing his own presence from the list of participants. Not sure what to do with that yet, skipping";
} else {
emit removeParticipant(resource);
}
2019-08-21 09:35:07 +00:00
}
QString Core::Conference::getSubject() const {
if (joined)
return room->subject();
else
return "";
}
void Core::Conference::onRoomSubjectChanged(const QString& p_name) {
emit subjectChanged(p_name);
}
2019-12-30 20:22:04 +00:00
void Core::Conference::handlePresence(const QXmppPresence& pres) {
2019-12-30 20:22:04 +00:00
QString id = pres.from();
QStringList comps = id.split("/");
QString jid = comps.front();
QString resource("");
if (comps.size() > 1)
2019-12-30 20:22:04 +00:00
resource = comps.back();
Archive::AvatarInfo info;
bool hasAvatar = readAvatarInfo(info, resource);
handlePossibleAvatarUpdate(pres, resource, hasAvatar, info);
2019-12-30 20:22:04 +00:00
}
bool Core::Conference::setAutoGeneratedAvatar(const QString& resource) {
2020-04-13 19:57:23 +00:00
Archive::AvatarInfo newInfo;
bool result = RosterItem::setAutoGeneratedAvatar(newInfo, resource);
2019-12-30 20:22:04 +00:00
if (result && resource.size() != 0) {
2020-04-13 19:57:23 +00:00
std::map<QString, Archive::AvatarInfo>::iterator itr = exParticipants.find(resource);
if (itr == exParticipants.end())
2020-04-13 19:57:23 +00:00
exParticipants.insert(std::make_pair(resource, newInfo));
else
2020-04-13 19:57:23 +00:00
itr->second = newInfo;
2019-12-30 20:22:04 +00:00
emit changeParticipant(resource, {
{"avatarState", static_cast<uint>(Shared::Avatar::autocreated)},
2020-04-13 19:57:23 +00:00
{"avatarPath", avatarPath(resource) + "." + newInfo.type}
2019-12-30 20:22:04 +00:00
});
}
return result;
}
bool Core::Conference::setAvatar(const QByteArray& data, Archive::AvatarInfo& info, const QString& resource) {
2020-04-13 19:57:23 +00:00
bool result = RosterItem::setAvatar(data, info, resource);
2019-12-30 20:22:04 +00:00
if (result && resource.size() != 0) {
if (data.size() > 0) {
2020-04-13 19:57:23 +00:00
std::map<QString, Archive::AvatarInfo>::iterator itr = exParticipants.find(resource);
if (itr == exParticipants.end())
2020-04-13 19:57:23 +00:00
exParticipants.insert(std::make_pair(resource, info));
else
2020-04-13 19:57:23 +00:00
itr->second = info;
2019-12-30 20:22:04 +00:00
emit changeParticipant(resource, {
{"avatarState", static_cast<uint>(Shared::Avatar::autocreated)},
2020-04-13 19:57:23 +00:00
{"avatarPath", avatarPath(resource) + "." + info.type}
2019-12-30 20:22:04 +00:00
});
} else {
2020-04-13 19:57:23 +00:00
std::map<QString, Archive::AvatarInfo>::iterator itr = exParticipants.find(resource);
if (itr != exParticipants.end())
2020-04-13 19:57:23 +00:00
exParticipants.erase(itr);
2019-12-30 20:22:04 +00:00
emit changeParticipant(resource, {
{"avatarState", static_cast<uint>(Shared::Avatar::empty)},
{"avatarPath", ""}
});
}
}
return result;
}
void Core::Conference::handleResponseVCard(const QXmppVCardIq& card, const QString &resource, Shared::VCard& out) {
RosterItem::handleResponseVCard(card, resource, out);
if (resource.size() > 0)
2019-12-30 20:22:04 +00:00
emit changeParticipant(resource, {
{"avatarState", static_cast<uint>(out.getAvatarType())},
{"avatarPath", out.getAvatarPath()}
2019-12-30 20:22:04 +00:00
});
}
2020-04-13 19:57:23 +00:00
QMap<QString, QVariant> Core::Conference::getAllAvatars() const {
2020-04-13 19:57:23 +00:00
QMap<QString, QVariant> result;
for (const std::pair<const QString, Archive::AvatarInfo>& pair : exParticipants)
2020-04-13 19:57:23 +00:00
result.insert(pair.first, avatarPath(pair.first) + "." + pair.second.type);
2020-04-13 19:57:23 +00:00
return result;
}
QMap<QString, QVariant> Core::Conference::getInfo() const {
QMap<QString, QVariant> data = RosterItem::getInfo();
data.insert("autoJoin", getAutoJoin());
data.insert("joined", getJoined());
data.insert("nick", getNick());
data.insert("avatars", getAllAvatars());
return data;
}