2019-07-11 08:51:52 +00:00
|
|
|
/*
|
2019-09-01 19:46:12 +00:00
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
2019-07-11 08:51:52 +00:00
|
|
|
*
|
|
|
|
* 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 "room.h"
|
2020-04-03 22:28:15 +00:00
|
|
|
#include "shared/icons.h"
|
|
|
|
|
2019-07-11 08:51:52 +00:00
|
|
|
#include <QIcon>
|
2019-09-02 11:17:28 +00:00
|
|
|
#include <QDebug>
|
2019-07-11 08:51:52 +00:00
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
Models::Room::Room(const Account* acc, const QString& p_jid, const QMap<QString, QVariant>& data, Models::Item* parentItem):
|
|
|
|
Element(room, acc, p_jid, data, parentItem),
|
2019-07-11 08:51:52 +00:00
|
|
|
autoJoin(false),
|
|
|
|
joined(false),
|
|
|
|
nick(""),
|
2019-09-03 20:28:58 +00:00
|
|
|
subject(""),
|
2020-04-13 19:57:23 +00:00
|
|
|
participants(),
|
|
|
|
exParticipantAvatars()
|
2019-07-11 08:51:52 +00:00
|
|
|
{
|
|
|
|
QMap<QString, QVariant>::const_iterator itr = data.find("autoJoin");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setAutoJoin(itr.value().toBool());
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = data.find("joined");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setJoined(itr.value().toBool());
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = data.find("nick");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setNick(itr.value().toString());
|
|
|
|
}
|
2019-09-03 20:28:58 +00:00
|
|
|
|
|
|
|
itr = data.find("subject");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setSubject(itr.value().toString());
|
|
|
|
}
|
2019-12-17 16:54:53 +00:00
|
|
|
|
2020-04-13 19:57:23 +00:00
|
|
|
itr = data.find("avatars");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
QMap<QString, QVariant> avs = itr.value().toMap();
|
|
|
|
for (QMap<QString, QVariant>::const_iterator itr = avs.begin(), end = avs.end(); itr != end; ++itr) {
|
|
|
|
exParticipantAvatars.insert(std::make_pair(itr.key(), itr.value().toString()));
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Models::Room::~Room()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Room::columnCount() const
|
|
|
|
{
|
2019-09-03 20:28:58 +00:00
|
|
|
return 7;
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Models::Room::getAutoJoin() const
|
|
|
|
{
|
|
|
|
return autoJoin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Models::Room::getJoined() const
|
|
|
|
{
|
|
|
|
return joined;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getNick() const
|
|
|
|
{
|
|
|
|
return nick;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getRoomName() const
|
|
|
|
{
|
|
|
|
if (name.size() == 0) {
|
|
|
|
return jid;
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Room::data(int column) const
|
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case 0:
|
|
|
|
return getRoomName();
|
|
|
|
case 1:
|
|
|
|
return jid;
|
|
|
|
case 2:
|
|
|
|
return getJoined();
|
|
|
|
case 3:
|
|
|
|
return getAutoJoin();
|
|
|
|
case 4:
|
|
|
|
return getNick();
|
2019-08-31 20:50:05 +00:00
|
|
|
case 5:
|
|
|
|
return getMessagesCount();
|
2019-09-03 20:28:58 +00:00
|
|
|
case 6:
|
|
|
|
return getSubject();
|
2019-12-17 16:54:53 +00:00
|
|
|
case 7:
|
|
|
|
return static_cast<quint8>(getAvatarState());
|
|
|
|
case 8:
|
|
|
|
return getAvatarPath();
|
2019-07-11 08:51:52 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setAutoJoin(bool p_autoJoin)
|
|
|
|
{
|
|
|
|
if (autoJoin != p_autoJoin) {
|
|
|
|
autoJoin = p_autoJoin;
|
|
|
|
changed(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setJoined(bool p_joined)
|
|
|
|
{
|
|
|
|
if (joined != p_joined) {
|
|
|
|
joined = p_joined;
|
|
|
|
changed(2);
|
2019-09-02 11:17:28 +00:00
|
|
|
if (!joined) {
|
|
|
|
toOfflineState();
|
|
|
|
}
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setNick(const QString& p_nick)
|
|
|
|
{
|
|
|
|
if (nick != p_nick) {
|
|
|
|
nick = p_nick;
|
|
|
|
changed(4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::update(const QString& field, const QVariant& value)
|
|
|
|
{
|
|
|
|
if (field == "name") {
|
|
|
|
setName(value.toString());
|
|
|
|
} else if (field == "joined") {
|
|
|
|
setJoined(value.toBool());
|
|
|
|
} else if (field == "autoJoin") {
|
|
|
|
setAutoJoin(value.toBool());
|
|
|
|
} else if (field == "nick") {
|
|
|
|
setNick(value.toString());
|
2019-09-03 20:28:58 +00:00
|
|
|
} else if (field == "subject") {
|
|
|
|
setSubject(value.toString());
|
2020-08-11 22:49:51 +00:00
|
|
|
} else {
|
|
|
|
Element::update(field, value);
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon Models::Room::getStatusIcon(bool big) const
|
|
|
|
{
|
2020-08-11 22:49:51 +00:00
|
|
|
if (getMessagesCount() > 0) {
|
2019-08-31 20:50:05 +00:00
|
|
|
return Shared::icon("mail-message", big);
|
2019-07-11 08:51:52 +00:00
|
|
|
} else {
|
2019-08-31 20:50:05 +00:00
|
|
|
if (autoJoin) {
|
|
|
|
if (joined) {
|
2020-04-03 22:28:15 +00:00
|
|
|
return Shared::connectionStateIcon(Shared::ConnectionState::connected, big);
|
2019-08-31 20:50:05 +00:00
|
|
|
} else {
|
2020-04-03 22:28:15 +00:00
|
|
|
return Shared::connectionStateIcon(Shared::ConnectionState::disconnected, big);
|
2019-08-31 20:50:05 +00:00
|
|
|
}
|
2019-07-11 08:51:52 +00:00
|
|
|
} else {
|
2019-08-31 20:50:05 +00:00
|
|
|
if (joined) {
|
2020-04-03 22:28:15 +00:00
|
|
|
return Shared::connectionStateIcon(Shared::ConnectionState::connecting, big);
|
2019-08-31 20:50:05 +00:00
|
|
|
} else {
|
2020-04-03 22:28:15 +00:00
|
|
|
return Shared::connectionStateIcon(Shared::ConnectionState::error, big);
|
2019-08-31 20:50:05 +00:00
|
|
|
}
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getStatusText() const
|
|
|
|
{
|
|
|
|
if (autoJoin) {
|
|
|
|
if (joined) {
|
2019-10-05 11:27:39 +00:00
|
|
|
return tr("Subscribed");
|
2019-07-11 08:51:52 +00:00
|
|
|
} else {
|
2019-10-05 11:27:39 +00:00
|
|
|
return tr("Temporarily unsubscribed");
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (joined) {
|
2019-10-05 11:27:39 +00:00
|
|
|
return tr("Temporarily subscribed");
|
2019-07-11 08:51:52 +00:00
|
|
|
} else {
|
2019-10-05 11:27:39 +00:00
|
|
|
return tr("Unsubscribed");
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 20:50:05 +00:00
|
|
|
|
2019-09-02 11:17:28 +00:00
|
|
|
|
|
|
|
void Models::Room::toOfflineState()
|
|
|
|
{
|
|
|
|
emit childIsAboutToBeRemoved(this, 0, childItems.size());
|
2019-11-17 10:24:12 +00:00
|
|
|
for (std::deque<Item*>::size_type i = 0; i < childItems.size(); ++i) {
|
2019-09-02 11:17:28 +00:00
|
|
|
Item* item = childItems[i];
|
|
|
|
Item::_removeChild(i);
|
|
|
|
item->deleteLater();
|
|
|
|
}
|
|
|
|
childItems.clear();
|
|
|
|
participants.clear();
|
|
|
|
emit childRemoved();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::addParticipant(const QString& p_name, const QMap<QString, QVariant>& data)
|
|
|
|
{
|
|
|
|
std::map<QString, Participant*>::const_iterator itr = participants.find(p_name);
|
|
|
|
if (itr != participants.end()) {
|
|
|
|
qDebug() << "An attempt to add already existing participant" << p_name << "to the room" << name << ", updating instead";
|
|
|
|
handleParticipantUpdate(itr, data);
|
|
|
|
} else {
|
2020-04-13 19:57:23 +00:00
|
|
|
std::map<QString, QString>::const_iterator eitr = exParticipantAvatars.find(name);
|
|
|
|
if (eitr != exParticipantAvatars.end()) {
|
|
|
|
exParticipantAvatars.erase(eitr);
|
|
|
|
}
|
|
|
|
|
2019-09-02 11:17:28 +00:00
|
|
|
Participant* part = new Participant(data);
|
|
|
|
part->setName(p_name);
|
|
|
|
participants.insert(std::make_pair(p_name, part));
|
|
|
|
appendChild(part);
|
2019-12-31 18:14:12 +00:00
|
|
|
emit participantJoined(*part);
|
2019-09-02 11:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::changeParticipant(const QString& p_name, const QMap<QString, QVariant>& data)
|
|
|
|
{
|
|
|
|
std::map<QString, Participant*>::const_iterator itr = participants.find(p_name);
|
|
|
|
if (itr == participants.end()) {
|
|
|
|
qDebug() << "An attempt to change non existing participant" << p_name << "from the room" << name << ", skipping";
|
|
|
|
} else {
|
|
|
|
handleParticipantUpdate(itr, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::removeParticipant(const QString& p_name)
|
|
|
|
{
|
|
|
|
std::map<QString, Participant*>::const_iterator itr = participants.find(p_name);
|
|
|
|
if (itr == participants.end()) {
|
|
|
|
qDebug() << "An attempt to remove non existing participant" << p_name << "from the room" << name << ", skipping";
|
|
|
|
} else {
|
|
|
|
Participant* p = itr->second;
|
|
|
|
participants.erase(itr);
|
|
|
|
removeChild(p->row());
|
2020-04-13 19:57:23 +00:00
|
|
|
|
|
|
|
if (p->getAvatarState() != Shared::Avatar::empty) {
|
|
|
|
exParticipantAvatars.insert(std::make_pair(p_name, p->getAvatarPath()));
|
|
|
|
}
|
|
|
|
|
2019-09-02 11:17:28 +00:00
|
|
|
p->deleteLater();
|
2019-12-31 18:14:12 +00:00
|
|
|
emit participantLeft(p_name);
|
2019-09-02 11:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 15:52:29 +00:00
|
|
|
Models::Participant * Models::Room::getParticipant(const QString& p_name)
|
|
|
|
{
|
|
|
|
std::map<QString, Participant*>::const_iterator itr = participants.find(p_name);
|
|
|
|
if (itr == participants.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 11:17:28 +00:00
|
|
|
void Models::Room::handleParticipantUpdate(std::map<QString, Participant*>::const_iterator itr, const QMap<QString, QVariant>& data)
|
|
|
|
{
|
|
|
|
Participant* part = itr->second;
|
|
|
|
const QString& p_name = itr->first;
|
|
|
|
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
|
|
|
|
part->update(itr.key(), itr.value());
|
|
|
|
}
|
|
|
|
if (p_name != part->getName()) {
|
|
|
|
participants.erase(itr);
|
|
|
|
participants.insert(std::make_pair(part->getName(), part));
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 20:28:58 +00:00
|
|
|
|
|
|
|
QString Models::Room::getSubject() const
|
|
|
|
{
|
|
|
|
return subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setSubject(const QString& sub)
|
|
|
|
{
|
|
|
|
if (sub != subject) {
|
|
|
|
subject = sub;
|
|
|
|
changed(6);
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 09:21:29 +00:00
|
|
|
|
|
|
|
QString Models::Room::getDisplayedName() const
|
|
|
|
{
|
|
|
|
return getRoomName();
|
|
|
|
}
|
|
|
|
|
2019-12-31 18:14:12 +00:00
|
|
|
std::map<QString, const Models::Participant &> Models::Room::getParticipants() const
|
|
|
|
{
|
|
|
|
std::map<QString, const Models::Participant&> result;
|
|
|
|
|
|
|
|
for (std::pair<QString, Models::Participant*> pair : participants) {
|
|
|
|
result.emplace(pair.first, *(pair.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getParticipantIconPath(const QString& name) const
|
|
|
|
{
|
|
|
|
std::map<QString, Models::Participant*>::const_iterator itr = participants.find(name);
|
|
|
|
if (itr == participants.end()) {
|
2020-08-20 21:32:30 +00:00
|
|
|
std::map<QString, QString>::const_iterator eitr = exParticipantAvatars.find(name);
|
|
|
|
if (eitr != exParticipantAvatars.end()) {
|
|
|
|
return eitr->second;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2019-12-31 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return itr->second->getAvatarPath();
|
|
|
|
}
|
2020-04-13 19:57:23 +00:00
|
|
|
|
|
|
|
std::map<QString, QString> Models::Room::getExParticipantAvatars() const
|
|
|
|
{
|
|
|
|
return exParticipantAvatars;
|
|
|
|
}
|