2019-09-01 19:46:12 +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/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2020-04-17 23:17:47 +00:00
|
|
|
Models::Contact::Contact(const Account* acc, const QString& p_jid ,const QMap<QString, QVariant> &data, Item *parentItem):
|
2020-08-11 22:49:51 +00:00
|
|
|
Element(Item::contact, acc, p_jid, data, parentItem),
|
2020-04-03 22:28:15 +00:00
|
|
|
availability(Shared::Availability::offline),
|
|
|
|
state(Shared::SubscriptionState::none),
|
2023-03-27 18:45:29 +00:00
|
|
|
encryption(Shared::EncryptionProtocol::none),
|
2023-03-17 20:59:51 +00:00
|
|
|
trust(),
|
2019-04-09 22:01:25 +00:00
|
|
|
presences(),
|
2020-08-11 22:49:51 +00:00
|
|
|
status()
|
2019-04-05 15:12:59 +00:00
|
|
|
{
|
2019-04-07 20:14:15 +00:00
|
|
|
QMap<QString, QVariant>::const_iterator itr = data.find("state");
|
2023-03-17 20:59:51 +00:00
|
|
|
if (itr != data.end())
|
2019-04-07 20:14:15 +00:00
|
|
|
setState(itr.value().toUInt());
|
2019-04-05 15:12:59 +00:00
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
itr = data.find("trust");
|
|
|
|
if (itr != data.end())
|
|
|
|
setTrust(itr.value().value<Shared::TrustSummary>());
|
2023-11-05 01:12:15 +00:00
|
|
|
|
|
|
|
itr = data.find("encryption");
|
|
|
|
if (itr != data.end())
|
|
|
|
setEncryption(itr.value().value<Shared::EncryptionProtocol>());
|
2019-04-05 15:12:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Models::Contact::~Contact() {}
|
|
|
|
|
|
|
|
void Models::Contact::setAvailability(unsigned int p_state) {
|
2020-04-03 22:28:15 +00:00
|
|
|
setAvailability(Shared::Global::fromInt<Shared::Availability>(p_state));
|
2019-04-05 15:12:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::setState(unsigned int p_state) {
|
2020-04-03 22:28:15 +00:00
|
|
|
setState(Shared::Global::fromInt<Shared::SubscriptionState>(p_state));
|
2019-04-07 20:14:15 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Shared::Availability Models::Contact::getAvailability() const {
|
2019-04-07 20:14:15 +00:00
|
|
|
return availability;
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::setAvailability(Shared::Availability p_state) {
|
2019-04-07 20:14:15 +00:00
|
|
|
if (availability != p_state) {
|
|
|
|
availability = p_state;
|
|
|
|
changed(3);
|
2019-04-05 15:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QString Models::Contact::getStatus() const {
|
2019-06-21 19:33:38 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::setStatus(const QString& p_state) {
|
2019-06-21 19:33:38 +00:00
|
|
|
if (status != p_state) {
|
|
|
|
status = p_state;
|
|
|
|
changed(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
int Models::Contact::columnCount() const {
|
2023-11-05 01:12:15 +00:00
|
|
|
return 10;
|
2019-04-05 15:12:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QVariant Models::Contact::data(int column) const {
|
2019-04-05 15:12:59 +00:00
|
|
|
switch (column) {
|
|
|
|
case 0:
|
2019-06-18 15:08:03 +00:00
|
|
|
return getContactName();
|
2019-04-05 15:12:59 +00:00
|
|
|
case 1:
|
|
|
|
return jid;
|
|
|
|
case 2:
|
2020-04-03 22:28:15 +00:00
|
|
|
return QVariant::fromValue(state);
|
2019-04-09 15:04:08 +00:00
|
|
|
case 3:
|
2020-04-03 22:28:15 +00:00
|
|
|
return QVariant::fromValue(availability);
|
2019-04-09 22:01:25 +00:00
|
|
|
case 4:
|
|
|
|
return getMessagesCount();
|
2019-06-21 19:33:38 +00:00
|
|
|
case 5:
|
|
|
|
return getStatus();
|
2019-10-16 19:38:35 +00:00
|
|
|
case 6:
|
2020-04-03 22:28:15 +00:00
|
|
|
return QVariant::fromValue(getAvatarState());
|
2019-10-16 19:38:35 +00:00
|
|
|
case 7:
|
|
|
|
return getAvatarPath();
|
2023-03-17 20:59:51 +00:00
|
|
|
case 8:
|
|
|
|
return QVariant::fromValue(getTrust());
|
2023-11-05 01:12:15 +00:00
|
|
|
case 9:
|
|
|
|
return QVariant::fromValue(getEncryption());
|
2019-04-05 15:12:59 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QString Models::Contact::getContactName() const {
|
|
|
|
if (name == "")
|
2019-06-18 15:08:03 +00:00
|
|
|
return jid;
|
2023-03-17 20:59:51 +00:00
|
|
|
else
|
2019-06-18 15:08:03 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::update(const QString& field, const QVariant& value) {
|
2019-04-05 15:12:59 +00:00
|
|
|
if (field == "name") {
|
|
|
|
setName(value.toString());
|
2019-04-07 20:14:15 +00:00
|
|
|
} else if (field == "availability") {
|
|
|
|
setAvailability(value.toUInt());
|
2019-04-05 15:12:59 +00:00
|
|
|
} else if (field == "state") {
|
2019-04-07 20:14:15 +00:00
|
|
|
setState(value.toUInt());
|
2023-03-17 20:59:51 +00:00
|
|
|
} else if (field == "trust") {
|
|
|
|
setTrust(value.value<Shared::TrustSummary>());
|
2023-11-05 01:12:15 +00:00
|
|
|
} else if (field == "encryption") {
|
|
|
|
setEncryption(value.value<Shared::EncryptionProtocol>());
|
2020-08-11 22:49:51 +00:00
|
|
|
} else {
|
|
|
|
Element::update(field, value);
|
2019-04-05 15:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 14:02:41 +00:00
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::addPresence(const QString& p_name, const QMap<QString, QVariant>& data) {
|
2019-04-07 14:02:41 +00:00
|
|
|
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();
|
2023-03-17 20:59:51 +00:00
|
|
|
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr)
|
2019-04-07 14:02:41 +00:00
|
|
|
pr->update(itr.key(), itr.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::removePresence(const QString& name) {
|
2019-04-07 14:02:41 +00:00
|
|
|
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);
|
2019-07-11 08:51:52 +00:00
|
|
|
removeChild(pr->row());
|
2019-05-30 09:36:21 +00:00
|
|
|
pr->deleteLater();
|
2019-04-07 14:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Models::Presence * Models::Contact::getPresence(const QString& name) {
|
2022-04-24 15:52:29 +00:00
|
|
|
QMap<QString, Presence*>::iterator itr = presences.find(name);
|
2023-03-17 20:59:51 +00:00
|
|
|
if (itr == presences.end())
|
2022-04-24 15:52:29 +00:00
|
|
|
return nullptr;
|
2023-03-17 20:59:51 +00:00
|
|
|
else
|
2022-04-24 15:52:29 +00:00
|
|
|
return itr.value();
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::refresh() {
|
2019-04-07 14:02:41 +00:00
|
|
|
QDateTime lastActivity;
|
|
|
|
Presence* presence = 0;
|
|
|
|
for (QMap<QString, Presence*>::iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
|
|
|
|
Presence* pr = itr.value();
|
|
|
|
QDateTime la = pr->getLastActivity();
|
|
|
|
|
|
|
|
if (la > lastActivity) {
|
|
|
|
lastActivity = la;
|
|
|
|
presence = pr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (presence != 0) {
|
2019-04-07 20:14:15 +00:00
|
|
|
setAvailability(presence->getAvailability());
|
2019-06-21 19:33:38 +00:00
|
|
|
setStatus(presence->getStatus());
|
2019-04-07 20:14:15 +00:00
|
|
|
} else {
|
2020-04-03 22:28:15 +00:00
|
|
|
setAvailability(Shared::Availability::offline);
|
2019-06-21 19:33:38 +00:00
|
|
|
setStatus("");
|
2019-04-07 14:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::_removeChild(int index) {
|
2019-04-09 15:04:08 +00:00
|
|
|
Item* child = childItems[index];
|
2019-11-03 18:46:40 +00:00
|
|
|
disconnect(child, &Item::childChanged, this, &Contact::refresh);
|
2019-04-07 14:02:41 +00:00
|
|
|
Item::_removeChild(index);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::_appendChild(Models::Item* child) {
|
2020-04-18 12:02:01 +00:00
|
|
|
Item::_appendChild(child);
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(child, &Item::childChanged, this, &Contact::refresh);
|
2019-04-07 14:02:41 +00:00
|
|
|
refresh();
|
|
|
|
}
|
2019-04-07 20:14:15 +00:00
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Shared::SubscriptionState Models::Contact::getState() const {
|
2019-04-07 20:14:15 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::setState(Shared::SubscriptionState p_state) {
|
2019-04-07 20:14:15 +00:00
|
|
|
if (state != p_state) {
|
|
|
|
state = p_state;
|
|
|
|
changed(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Shared::TrustSummary Models::Contact::getTrust() const {
|
|
|
|
return trust;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Contact::setTrust(const Shared::TrustSummary& p_trust) {
|
2023-03-17 23:50:04 +00:00
|
|
|
if (trust != p_trust) {
|
2023-03-17 20:59:51 +00:00
|
|
|
trust = p_trust;
|
|
|
|
changed(8);
|
2023-03-17 23:50:04 +00:00
|
|
|
}
|
2023-03-17 20:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QIcon Models::Contact::getStatusIcon(bool big) const {
|
2019-04-09 22:01:25 +00:00
|
|
|
if (getMessagesCount() > 0) {
|
2019-08-31 20:50:05 +00:00
|
|
|
return Shared::icon("mail-message", big);
|
2020-04-03 22:28:15 +00:00
|
|
|
} else if (state == Shared::SubscriptionState::both || state == Shared::SubscriptionState::to) {
|
2019-06-23 21:09:39 +00:00
|
|
|
return Shared::availabilityIcon(availability, big);;
|
2019-04-07 20:14:15 +00:00
|
|
|
} else {
|
2019-08-31 20:50:05 +00:00
|
|
|
return Shared::subscriptionStateIcon(state, big);
|
2019-04-07 20:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-09 15:04:08 +00:00
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::toOfflineState() {
|
2020-04-18 12:02:01 +00:00
|
|
|
std::deque<Item*>::size_type size = childItems.size();
|
|
|
|
if (size > 0) {
|
|
|
|
emit childIsAboutToBeRemoved(this, 0, size - 1);
|
|
|
|
for (std::deque<Item*>::size_type i = 0; i < size; ++i) {
|
|
|
|
Item* item = childItems[0];
|
|
|
|
disconnect(item, &Item::childChanged, this, &Contact::refresh);
|
|
|
|
Item::_removeChild(0);
|
|
|
|
item->deleteLater();
|
|
|
|
}
|
|
|
|
childItems.clear();
|
|
|
|
presences.clear();
|
|
|
|
emit childRemoved();
|
|
|
|
refresh();
|
2019-06-23 11:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 09:21:29 +00:00
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QString Models::Contact::getDisplayedName() const {
|
2019-09-24 09:21:29 +00:00
|
|
|
return getContactName();
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
void Models::Contact::handleRecconnect() {
|
2023-03-17 23:50:04 +00:00
|
|
|
if (getMessagesCount() > 0)
|
2021-05-16 21:52:59 +00:00
|
|
|
feed->requestLatestMessages();
|
|
|
|
}
|
2023-03-17 23:50:04 +00:00
|
|
|
|
|
|
|
bool Models::Contact::hasKeys(Shared::EncryptionProtocol protocol) const {
|
|
|
|
return trust.hasKeys(protocol);
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:12:15 +00:00
|
|
|
void Models::Contact::setEncryption(Shared::EncryptionProtocol p_enc) {
|
|
|
|
if (encryption != p_enc) {
|
|
|
|
encryption = p_enc;
|
|
|
|
changed(9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Contact::setEncryption(unsigned int p_enc) {
|
|
|
|
setEncryption(Shared::Global::fromInt<Shared::EncryptionProtocol>(p_enc));
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared::EncryptionProtocol Models::Contact::getEncryption() const {
|
|
|
|
return encryption;
|
|
|
|
}
|
|
|
|
|