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-03 15:09:29 +00:00
|
|
|
#include "account.h"
|
2020-04-17 23:17:47 +00:00
|
|
|
#include "contact.h"
|
|
|
|
#include "reference.h"
|
2019-04-07 20:14:15 +00:00
|
|
|
#include <QDebug>
|
2019-04-03 15:09:29 +00:00
|
|
|
|
|
|
|
Models::Account::Account(const QMap<QString, QVariant>& data, Models::Item* parentItem):
|
|
|
|
Item(account, data, parentItem),
|
|
|
|
login(data.value("login").toString()),
|
|
|
|
password(data.value("password").toString()),
|
|
|
|
server(data.value("server").toString()),
|
2019-04-12 05:51:36 +00:00
|
|
|
resource(data.value("resource").toString()),
|
2019-05-24 14:46:34 +00:00
|
|
|
error(data.value("error").toString()),
|
2019-10-16 19:38:35 +00:00
|
|
|
avatarPath(data.value("avatarPath").toString()),
|
2020-04-03 22:28:15 +00:00
|
|
|
state(Shared::ConnectionState::disconnected),
|
2020-04-04 16:40:32 +00:00
|
|
|
availability(Shared::Availability::offline),
|
2021-05-16 21:52:59 +00:00
|
|
|
passwordType(Shared::AccountPassword::plain),
|
2022-04-12 20:33:10 +00:00
|
|
|
wasEverConnected(false),
|
|
|
|
active(false)
|
2019-04-03 15:09:29 +00:00
|
|
|
{
|
2019-04-07 20:14:15 +00:00
|
|
|
QMap<QString, QVariant>::const_iterator sItr = data.find("state");
|
|
|
|
if (sItr != data.end()) {
|
|
|
|
setState(sItr.value().toUInt());
|
|
|
|
}
|
|
|
|
QMap<QString, QVariant>::const_iterator aItr = data.find("availability");
|
|
|
|
if (aItr != data.end()) {
|
|
|
|
setAvailability(aItr.value().toUInt());
|
|
|
|
}
|
2020-04-07 20:33:03 +00:00
|
|
|
QMap<QString, QVariant>::const_iterator pItr = data.find("passwordType");
|
|
|
|
if (pItr != data.end()) {
|
|
|
|
setPasswordType(pItr.value().toUInt());
|
|
|
|
}
|
2022-04-12 20:33:10 +00:00
|
|
|
QMap<QString, QVariant>::const_iterator acItr = data.find("active");
|
|
|
|
if (acItr != data.end()) {
|
|
|
|
setActive(acItr.value().toBool());
|
|
|
|
}
|
2019-04-03 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Models::Account::~Account()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-07 20:14:15 +00:00
|
|
|
void Models::Account::setState(Shared::ConnectionState p_state)
|
2019-04-03 15:09:29 +00:00
|
|
|
{
|
2019-04-03 18:15:36 +00:00
|
|
|
if (state != p_state) {
|
|
|
|
state = p_state;
|
2019-04-07 14:02:41 +00:00
|
|
|
changed(2);
|
2021-05-16 21:52:59 +00:00
|
|
|
switch (state) {
|
|
|
|
case Shared::ConnectionState::disconnected:
|
|
|
|
toOfflineState();
|
|
|
|
break;
|
|
|
|
case Shared::ConnectionState::connected:
|
|
|
|
if (wasEverConnected) {
|
|
|
|
emit reconnected();
|
|
|
|
} else {
|
|
|
|
wasEverConnected = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-06-23 11:31:03 +00:00
|
|
|
}
|
2019-04-03 18:15:36 +00:00
|
|
|
}
|
2019-04-03 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:14:15 +00:00
|
|
|
void Models::Account::setAvailability(unsigned int p_state)
|
|
|
|
{
|
2020-04-03 22:28:15 +00:00
|
|
|
setAvailability(Shared::Global::fromInt<Shared::Availability>(p_state));
|
2019-04-07 20:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setState(unsigned int p_state)
|
|
|
|
{
|
2020-04-03 22:28:15 +00:00
|
|
|
setState(Shared::Global::fromInt<Shared::ConnectionState>(p_state));
|
2019-04-07 20:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Availability Models::Account::getAvailability() const
|
|
|
|
{
|
|
|
|
return availability;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setAvailability(Shared::Availability p_avail)
|
|
|
|
{
|
|
|
|
if (availability != p_avail) {
|
|
|
|
availability = p_avail;
|
2019-05-24 14:46:34 +00:00
|
|
|
changed(6);
|
2019-04-07 20:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 21:09:39 +00:00
|
|
|
QIcon Models::Account::getStatusIcon(bool big) const
|
2019-04-07 20:14:15 +00:00
|
|
|
{
|
2020-04-03 22:28:15 +00:00
|
|
|
if (state == Shared::ConnectionState::connected) {
|
2019-06-23 21:09:39 +00:00
|
|
|
return Shared::availabilityIcon(availability, big);
|
2020-04-03 22:28:15 +00:00
|
|
|
} else if (state == Shared::ConnectionState::disconnected) {
|
|
|
|
return Shared::availabilityIcon(Shared::Availability::offline, big);
|
2019-04-07 20:14:15 +00:00
|
|
|
} else {
|
2019-07-01 13:53:01 +00:00
|
|
|
return Shared::connectionStateIcon(state);
|
2019-04-07 20:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 15:09:29 +00:00
|
|
|
QString Models::Account::getLogin() const
|
|
|
|
{
|
|
|
|
return login;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Account::getPassword() const
|
|
|
|
{
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Account::getServer() const
|
|
|
|
{
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2019-04-07 20:14:15 +00:00
|
|
|
Shared::ConnectionState Models::Account::getState() const
|
2019-04-03 15:09:29 +00:00
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setLogin(const QString& p_login)
|
|
|
|
{
|
2019-04-03 18:15:36 +00:00
|
|
|
if (login != p_login) {
|
|
|
|
login = p_login;
|
2019-05-24 14:46:34 +00:00
|
|
|
changed(4);
|
2019-04-03 18:15:36 +00:00
|
|
|
}
|
2019-04-03 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setPassword(const QString& p_password)
|
|
|
|
{
|
2019-04-03 18:15:36 +00:00
|
|
|
if (password != p_password) {
|
|
|
|
password = p_password;
|
2019-05-24 14:46:34 +00:00
|
|
|
changed(5);
|
2019-04-03 18:15:36 +00:00
|
|
|
}
|
2019-04-03 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setServer(const QString& p_server)
|
|
|
|
{
|
2019-04-03 18:15:36 +00:00
|
|
|
if (server != p_server) {
|
|
|
|
server = p_server;
|
2019-04-07 14:02:41 +00:00
|
|
|
changed(1);
|
2019-04-03 18:15:36 +00:00
|
|
|
}
|
2019-04-03 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Account::data(int column) const
|
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case 0:
|
|
|
|
return Item::data(column);
|
|
|
|
case 1:
|
|
|
|
return server;
|
|
|
|
case 2:
|
2020-04-03 22:28:15 +00:00
|
|
|
return Shared::Global::getName(state);
|
2019-04-03 15:09:29 +00:00
|
|
|
case 3:
|
2019-05-24 14:46:34 +00:00
|
|
|
return error;
|
2019-04-03 15:09:29 +00:00
|
|
|
case 4:
|
2019-05-24 14:46:34 +00:00
|
|
|
return login;
|
2019-04-07 20:14:15 +00:00
|
|
|
case 5:
|
2019-05-24 14:46:34 +00:00
|
|
|
return password;
|
2019-04-12 05:51:36 +00:00
|
|
|
case 6:
|
2020-04-03 22:28:15 +00:00
|
|
|
return Shared::Global::getName(availability);
|
2019-05-24 14:46:34 +00:00
|
|
|
case 7:
|
2019-04-12 05:51:36 +00:00
|
|
|
return resource;
|
2019-10-16 19:38:35 +00:00
|
|
|
case 8:
|
|
|
|
return avatarPath;
|
2020-04-04 16:40:32 +00:00
|
|
|
case 9:
|
|
|
|
return Shared::Global::getName(passwordType);
|
2022-04-12 20:33:10 +00:00
|
|
|
case 10:
|
|
|
|
return active;
|
2019-04-03 15:09:29 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Account::columnCount() const
|
|
|
|
{
|
2022-04-12 20:33:10 +00:00
|
|
|
return 11;
|
2019-04-03 15:09:29 +00:00
|
|
|
}
|
2019-04-03 18:15:36 +00:00
|
|
|
|
|
|
|
void Models::Account::update(const QString& field, const QVariant& value)
|
|
|
|
{
|
|
|
|
if (field == "name") {
|
|
|
|
setName(value.toString());
|
|
|
|
} else if (field == "server") {
|
|
|
|
setServer(value.toString());
|
|
|
|
} else if (field == "login") {
|
|
|
|
setLogin(value.toString());
|
|
|
|
} else if (field == "password") {
|
|
|
|
setPassword(value.toString());
|
|
|
|
} else if (field == "state") {
|
2019-04-07 20:14:15 +00:00
|
|
|
setState(value.toUInt());
|
|
|
|
} else if (field == "availability") {
|
|
|
|
setAvailability(value.toUInt());
|
2019-04-12 05:51:36 +00:00
|
|
|
} else if (field == "resource") {
|
|
|
|
setResource(value.toString());
|
2019-05-24 14:46:34 +00:00
|
|
|
} else if (field == "error") {
|
|
|
|
setError(value.toString());
|
2019-10-16 19:38:35 +00:00
|
|
|
} else if (field == "avatarPath") {
|
|
|
|
setAvatarPath(value.toString());
|
2020-04-04 16:40:32 +00:00
|
|
|
} else if (field == "passwordType") {
|
|
|
|
setPasswordType(value.toUInt());
|
2022-04-12 20:33:10 +00:00
|
|
|
} else if (field == "active") {
|
|
|
|
setActive(value.toBool());
|
2019-04-03 18:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 05:51:36 +00:00
|
|
|
|
|
|
|
QString Models::Account::getResource() const
|
|
|
|
{
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setResource(const QString& p_resource)
|
|
|
|
{
|
|
|
|
if (resource != p_resource) {
|
|
|
|
resource = p_resource;
|
2019-05-24 14:46:34 +00:00
|
|
|
changed(7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Account::getError() const
|
|
|
|
{
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setError(const QString& p_resource)
|
|
|
|
{
|
|
|
|
if (error != p_resource) {
|
|
|
|
error = p_resource;
|
|
|
|
changed(3);
|
2019-04-12 05:51:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:31:03 +00:00
|
|
|
void Models::Account::toOfflineState()
|
|
|
|
{
|
2020-04-03 22:28:15 +00:00
|
|
|
setAvailability(Shared::Availability::offline);
|
2019-06-23 11:31:03 +00:00
|
|
|
Item::toOfflineState();
|
|
|
|
}
|
2019-10-16 19:38:35 +00:00
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
QString Models::Account::getAvatarPath() const
|
2019-10-16 19:38:35 +00:00
|
|
|
{
|
|
|
|
return avatarPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setAvatarPath(const QString& path)
|
|
|
|
{
|
|
|
|
avatarPath = path;
|
|
|
|
changed(8); //it's uncoditional because the path doesn't change when one avatar of the same type replaces another, sha1 sums checks are on the backend
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:13:56 +00:00
|
|
|
QString Models::Account::getBareJid() const
|
|
|
|
{
|
|
|
|
return login + "@" + server;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Account::getFullJid() const
|
|
|
|
{
|
|
|
|
return getBareJid() + "/" + resource;
|
|
|
|
}
|
2020-04-04 16:40:32 +00:00
|
|
|
|
|
|
|
Shared::AccountPassword Models::Account::getPasswordType() const
|
|
|
|
{
|
|
|
|
return passwordType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setPasswordType(Shared::AccountPassword pt)
|
|
|
|
{
|
|
|
|
if (passwordType != pt) {
|
|
|
|
passwordType = pt;
|
|
|
|
changed(9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setPasswordType(unsigned int pt)
|
|
|
|
{
|
|
|
|
setPasswordType(Shared::Global::fromInt<Shared::AccountPassword>(pt));
|
|
|
|
}
|
2022-04-12 20:33:10 +00:00
|
|
|
|
|
|
|
bool Models::Account::getActive() const
|
|
|
|
{
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Account::setActive(bool p_active)
|
|
|
|
{
|
|
|
|
if (active != p_active) {
|
|
|
|
active = p_active;
|
|
|
|
changed(10);
|
|
|
|
}
|
|
|
|
}
|