squawk/ui/models/contact.cpp

428 lines
11 KiB
C++
Raw Permalink Normal View History

/*
* 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/>.
*/
2019-04-05 15:12:59 +00:00
#include "contact.h"
2019-08-28 11:40:55 +00:00
2019-04-07 14:02:41 +00:00
#include <QDebug>
2019-04-05 15:12:59 +00:00
Models::Contact::Contact(const QString& p_jid ,const QMap<QString, QVariant> &data, Item *parentItem):
2019-04-05 15:12:59 +00:00
Item(Item::contact, data, parentItem),
jid(p_jid),
availability(Shared::offline),
state(Shared::none),
avatarState(Shared::Avatar::empty),
2019-04-09 22:01:25 +00:00
presences(),
messages(),
childMessages(0),
status(),
avatarPath()
2019-04-05 15:12:59 +00:00
{
QMap<QString, QVariant>::const_iterator itr = data.find("state");
if (itr != data.end()) {
setState(itr.value().toUInt());
}
itr = data.find("avatarState");
if (itr != data.end()) {
setAvatarState(itr.value().toUInt());
}
itr = data.find("avatarPath");
if (itr != data.end()) {
setAvatarPath(itr.value().toString());
}
2019-04-05 15:12:59 +00:00
}
Models::Contact::~Contact()
{
}
QString Models::Contact::getJid() const
{
return jid;
}
void Models::Contact::setJid(const QString p_jid)
{
if (jid != p_jid) {
jid = p_jid;
2019-04-07 14:02:41 +00:00
changed(1);
2019-04-05 15:12:59 +00:00
}
}
void Models::Contact::setAvailability(unsigned int p_state)
2019-04-05 15:12:59 +00:00
{
if (p_state <= Shared::availabilityHighest) {
Shared::Availability state = static_cast<Shared::Availability>(p_state);
setAvailability(state);
} else {
qDebug() << "An attempt to set invalid availability " << p_state << " to the contact " << jid;
}
2019-04-05 15:12:59 +00:00
}
void Models::Contact::setState(unsigned int p_state)
2019-04-05 15:12:59 +00:00
{
if (p_state <= Shared::subscriptionStateHighest) {
Shared::SubscriptionState state = static_cast<Shared::SubscriptionState>(p_state);
setState(state);
} else {
qDebug() << "An attempt to set invalid subscription state " << p_state << " to the contact " << jid;
}
}
Shared::Availability Models::Contact::getAvailability() const
{
return availability;
}
void Models::Contact::setAvailability(Shared::Availability p_state)
{
if (availability != p_state) {
availability = p_state;
changed(3);
2019-04-05 15:12:59 +00:00
}
}
QString Models::Contact::getStatus() const
{
return status;
}
void Models::Contact::setStatus(const QString& p_state)
{
if (status != p_state) {
status = p_state;
changed(5);
}
}
2019-04-05 15:12:59 +00:00
int Models::Contact::columnCount() const
{
return 8;
2019-04-05 15:12:59 +00:00
}
QVariant Models::Contact::data(int column) const
{
switch (column) {
case 0:
return getContactName();
2019-04-05 15:12:59 +00:00
case 1:
return jid;
case 2:
return state;
2019-04-09 15:04:08 +00:00
case 3:
return availability;
2019-04-09 22:01:25 +00:00
case 4:
return getMessagesCount();
case 5:
return getStatus();
case 6:
return static_cast<quint8>(getAvatarState());
case 7:
return getAvatarPath();
2019-04-05 15:12:59 +00:00
default:
return QVariant();
}
}
QString Models::Contact::getContactName() const
{
if (name == "") {
return jid;
} else {
return name;
}
}
2019-04-05 15:12:59 +00:00
void Models::Contact::update(const QString& field, const QVariant& value)
{
if (field == "name") {
setName(value.toString());
} else if (field == "jid") {
setJid(value.toString());
} else if (field == "availability") {
setAvailability(value.toUInt());
2019-04-05 15:12:59 +00:00
} else if (field == "state") {
setState(value.toUInt());
} else if (field == "avatarState") {
setAvatarState(value.toUInt());
} else if (field == "avatarPath") {
setAvatarPath(value.toString());
2019-04-05 15:12:59 +00:00
}
}
2019-04-07 14:02:41 +00:00
void Models::Contact::addPresence(const QString& p_name, const QMap<QString, QVariant>& data)
{
QMap<QString, Presence*>::iterator itr = presences.find(p_name);
if (itr == presences.end()) {
Presence* pr = new Presence(data);
pr->setName(p_name);
presences.insert(p_name, pr);
appendChild(pr);
} else {
Presence* pr = itr.value();
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
pr->update(itr.key(), itr.value());
}
}
}
void Models::Contact::removePresence(const QString& name)
{
QMap<QString, Presence*>::iterator itr = presences.find(name);
if (itr == presences.end()) {
2019-04-09 15:04:08 +00:00
qDebug() << "an attempt to remove non existing presence " << name << " from the contact " << jid << " of account " << getAccountName() << ", skipping";
2019-04-07 14:02:41 +00:00
} else {
Presence* pr = itr.value();
2019-05-30 09:36:21 +00:00
presences.erase(itr);
removeChild(pr->row());
2019-05-30 09:36:21 +00:00
pr->deleteLater();
2019-04-07 14:02:41 +00:00
}
}
void Models::Contact::refresh()
{
QDateTime lastActivity;
Presence* presence = 0;
2019-04-09 22:01:25 +00:00
unsigned int count = 0;
2019-04-07 14:02:41 +00:00
for (QMap<QString, Presence*>::iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
Presence* pr = itr.value();
QDateTime la = pr->getLastActivity();
2019-04-09 22:01:25 +00:00
count += pr->getMessagesCount();
2019-04-07 14:02:41 +00:00
if (la > lastActivity) {
lastActivity = la;
presence = pr;
}
}
if (presence != 0) {
setAvailability(presence->getAvailability());
setStatus(presence->getStatus());
} else {
setAvailability(Shared::offline);
setStatus("");
2019-04-07 14:02:41 +00:00
}
2019-04-09 22:01:25 +00:00
if (childMessages != count) {
childMessages = count;
changed(4);
}
2019-04-07 14:02:41 +00:00
}
void Models::Contact::_removeChild(int index)
{
2019-04-09 15:04:08 +00:00
Item* child = childItems[index];
disconnect(child, &Item::childChanged, this, &Contact::refresh);
2019-04-07 14:02:41 +00:00
Item::_removeChild(index);
refresh();
}
void Models::Contact::appendChild(Models::Item* child)
{
Item::appendChild(child);
connect(child, &Item::childChanged, this, &Contact::refresh);
2019-04-07 14:02:41 +00:00
refresh();
}
Shared::SubscriptionState Models::Contact::getState() const
{
return state;
}
void Models::Contact::setState(Shared::SubscriptionState p_state)
{
if (state != p_state) {
state = p_state;
changed(2);
}
}
QIcon Models::Contact::getStatusIcon(bool big) const
{
2019-04-09 22:01:25 +00:00
if (getMessagesCount() > 0) {
return Shared::icon("mail-message", big);
2019-12-17 16:54:53 +00:00
} else if (state == Shared::both || state == Shared::to) {
return Shared::availabilityIcon(availability, big);;
} else {
return Shared::subscriptionStateIcon(state, big);
}
}
2019-04-09 15:04:08 +00:00
void Models::Contact::addMessage(const Shared::Message& data)
2019-04-09 22:01:25 +00:00
{
const QString& res = data.getPenPalResource();
2019-04-09 22:01:25 +00:00
if (res.size() > 0) {
QMap<QString, Presence*>::iterator itr = presences.find(res);
if (itr == presences.end()) {
// this is actually the place when I can spot someone's invisible presence, and there is nothing criminal in it, cuz the sender sent us a message
// therefore he have revealed himself
// the only issue is to find out when the sender is gone offline
Presence* pr = new Presence({});
pr->setName(res);
pr->setAvailability(Shared::invisible);
pr->setLastActivity(QDateTime::currentDateTimeUtc());
presences.insert(res, pr);
appendChild(pr);
pr->addMessage(data);
2019-04-09 22:01:25 +00:00
return;
}
itr.value()->addMessage(data);
} else {
messages.emplace_back(data);
changed(4);
}
}
void Models::Contact::changeMessage(const QString& id, const QMap<QString, QVariant>& data)
{
bool found = false;
for (Shared::Message& msg : messages) {
if (msg.getId() == id) {
msg.change(data);
found = true;
break;
}
}
if (!found) {
for (Presence* pr : presences) {
found = pr->changeMessage(id, data);
if (found) {
break;
}
}
}
}
2019-04-09 22:01:25 +00:00
unsigned int Models::Contact::getMessagesCount() const
{
return messages.size() + childMessages;
}
void Models::Contact::dropMessages()
{
if (messages.size() > 0) {
messages.clear();
changed(4);
}
for (QMap<QString, Presence*>::iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
itr.value()->dropMessages();
}
}
2019-04-10 20:53:42 +00:00
void Models::Contact::getMessages(Models::Contact::Messages& container) const
{
for (Messages::const_iterator itr = messages.begin(), end = messages.end(); itr != end; ++itr) {
const Shared::Message& msg = *itr;
2019-04-10 20:53:42 +00:00
container.push_back(msg);
}
for (QMap<QString, Presence*>::const_iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
itr.value()->getMessages(container);
}
}
void Models::Contact::toOfflineState()
{
emit childIsAboutToBeRemoved(this, 0, childItems.size());
for (std::deque<Item*>::size_type i = 0; i < childItems.size(); ++i) {
Item* item = childItems[i];
disconnect(item, &Item::childChanged, this, &Contact::refresh);
Item::_removeChild(i);
item->deleteLater();
}
childItems.clear();
presences.clear();
emit childRemoved();
refresh();
}
2019-09-24 09:21:29 +00:00
QString Models::Contact::getDisplayedName() const
{
return getContactName();
}
bool Models::Contact::columnInvolvedInDisplay(int col)
{
return Item::columnInvolvedInDisplay(col) && col == 1;
}
Models::Contact * Models::Contact::copy() const
{
Contact* cnt = new Contact(*this);
return cnt;
}
Models::Contact::Contact(const Models::Contact& other):
Item(other),
jid(other.jid),
availability(other.availability),
state(other.state),
presences(),
messages(other.messages),
childMessages(0)
{
for (const Presence* pres : other.presences) {
Presence* pCopy = new Presence(*pres);
presences.insert(pCopy->getName(), pCopy);
Item::appendChild(pCopy);
connect(pCopy, &Item::childChanged, this, &Contact::refresh);
}
refresh();
}
QString Models::Contact::getAvatarPath() const
{
return avatarPath;
}
Shared::Avatar Models::Contact::getAvatarState() const
{
return avatarState;
}
void Models::Contact::setAvatarPath(const QString& path)
{
if (path != avatarPath) {
avatarPath = path;
changed(7);
}
}
void Models::Contact::setAvatarState(Shared::Avatar p_state)
{
if (avatarState != p_state) {
avatarState = p_state;
changed(6);
}
}
void Models::Contact::setAvatarState(unsigned int p_state)
{
if (p_state <= static_cast<quint8>(Shared::Avatar::valid)) {
Shared::Avatar state = static_cast<Shared::Avatar>(p_state);
setAvatarState(state);
} else {
qDebug() << "An attempt to set invalid avatar state" << p_state << "to the contact" << jid << ", skipping";
}
}