squawk/ui/models/item.cpp

359 lines
9.4 KiB
C++
Raw 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-03 15:09:29 +00:00
#include "item.h"
2019-08-28 11:40:55 +00:00
#include "account.h"
2020-04-17 23:17:47 +00:00
#include "reference.h"
#include "contact.h"
2019-08-28 11:40:55 +00:00
#include <QDebug>
2019-04-03 15:09:29 +00:00
Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_parent):
2019-04-03 18:15:36 +00:00
QObject(),
2019-04-03 15:09:29 +00:00
type(p_type),
name(""),
2019-04-03 15:09:29 +00:00
childItems(),
2020-04-16 20:23:02 +00:00
parent(p_parent),
references()
{
QMap<QString, QVariant>::const_iterator itr = p_data.find("name");
if (itr != p_data.end()) {
setName(itr.value().toString());
}
}
2019-04-03 15:09:29 +00:00
Models::Item::Item(const Models::Item& other):
QObject(),
type(other.type),
name(other.name),
childItems(),
parent(nullptr)
{
}
2019-04-03 15:09:29 +00:00
Models::Item::~Item()
{
2020-04-17 23:17:47 +00:00
for (Reference* ref : references) {
Item* parent = ref->parentItem();
if (parent != nullptr) {
parent->removeChild(ref->row());
}
delete ref;
}
2019-04-03 15:09:29 +00:00
2020-04-17 23:17:47 +00:00
for (Item* child : childItems) {
delete child;
2019-04-03 15:09:29 +00:00
}
}
void Models::Item::setName(const QString& p_name)
{
2019-04-03 18:15:36 +00:00
if (name != p_name) {
name = p_name;
2019-04-07 14:02:41 +00:00
changed(0);
2019-04-03 18:15:36 +00:00
}
2019-04-03 15:09:29 +00:00
}
void Models::Item::appendChild(Models::Item* child)
{
2019-04-07 14:02:41 +00:00
bool moving = false;
2019-09-24 09:21:29 +00:00
int newRow = 0;
std::deque<Item*>::const_iterator before = childItems.begin();
2020-04-17 23:17:47 +00:00
Type ct = child->type;
if (ct == reference) {
ct = static_cast<Reference*>(child)->dereference()->type;
}
2019-09-24 09:21:29 +00:00
while (before != childItems.end()) {
Item* bfr = *before;
2020-04-17 23:17:47 +00:00
Type bt = bfr->type;
if (bt == reference) {
bt = static_cast<Reference*>(bfr)->dereference()->type;
}
if (bt > ct) {
2019-09-24 09:21:29 +00:00
break;
2020-04-17 23:17:47 +00:00
} else if (bt == ct && bfr->getDisplayedName() > child->getDisplayedName()) {
2019-09-24 09:21:29 +00:00
break;
}
newRow++;
before++;
}
2020-04-17 23:17:47 +00:00
if (child->parent != nullptr) {
2019-09-24 09:21:29 +00:00
int oldRow = child->row();
2019-04-07 14:02:41 +00:00
moving = true;
emit childIsAboutToBeMoved(child->parent, oldRow, oldRow, this, newRow);
child->parent->_removeChild(oldRow);
} else {
emit childIsAboutToBeInserted(this, newRow, newRow);
}
2019-09-24 09:21:29 +00:00
childItems.insert(before, child);
2019-04-07 14:02:41 +00:00
child->parent = this;
QObject::connect(child, &Item::childChanged, this, &Item::childChanged);
QObject::connect(child, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted);
QObject::connect(child, &Item::childInserted, this, &Item::childInserted);
QObject::connect(child, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved);
QObject::connect(child, &Item::childRemoved, this, &Item::childRemoved);
QObject::connect(child, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved);
QObject::connect(child, &Item::childMoved, this, &Item::childMoved);
2019-04-07 14:02:41 +00:00
if (moving) {
emit childMoved();
} else {
emit childInserted();
}
2019-04-03 15:09:29 +00:00
}
Models::Item * Models::Item::child(int row)
{
return childItems[row];
}
int Models::Item::childCount() const
{
return childItems.size();
}
int Models::Item::row() const
{
2020-04-17 23:17:47 +00:00
if (parent != nullptr) {
2019-04-03 15:09:29 +00:00
std::deque<Item*>::const_iterator itr = parent->childItems.begin();
std::deque<Item*>::const_iterator end = parent->childItems.end();
for (int i = 0; itr != end; ++itr, ++i) {
if (*itr == this) {
return i;
}
}
}
2020-04-17 23:17:47 +00:00
return -1; //TODO not sure how it helps
2019-04-03 15:09:29 +00:00
}
Models::Item * Models::Item::parentItem()
{
return parent;
}
2019-04-09 15:04:08 +00:00
const Models::Item * Models::Item::parentItemConst() const
{
return parent;
}
2019-04-03 15:09:29 +00:00
int Models::Item::columnCount() const
{
return 2;
2019-04-03 15:09:29 +00:00
}
QString Models::Item::getName() const
{
return name;
}
QVariant Models::Item::data(int column) const
{
if (column != 0) {
return QVariant();
}
return name;
}
2019-04-05 15:12:59 +00:00
void Models::Item::removeChild(int index)
{
2019-04-07 14:02:41 +00:00
emit childIsAboutToBeRemoved(this, index, index);
_removeChild(index);
2019-04-07 14:02:41 +00:00
emit childRemoved();
2019-04-05 15:12:59 +00:00
}
2019-04-07 14:02:41 +00:00
void Models::Item::_removeChild(int index)
2019-04-05 15:12:59 +00:00
{
2019-04-07 14:02:41 +00:00
Item* child = childItems[index];
QObject::disconnect(child, &Item::childChanged, this, &Item::childChanged);
QObject::disconnect(child, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted);
QObject::disconnect(child, &Item::childInserted, this, &Item::childInserted);
QObject::disconnect(child, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved);
QObject::disconnect(child, &Item::childRemoved, this, &Item::childRemoved);
QObject::disconnect(child, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved);
QObject::disconnect(child, &Item::childMoved, this, &Item::childMoved);
2019-04-07 14:02:41 +00:00
childItems.erase(childItems.begin() + index);
2020-04-17 23:17:47 +00:00
child->parent = nullptr;
2019-04-05 15:12:59 +00:00
}
2019-04-07 14:02:41 +00:00
void Models::Item::changed(int col)
{
2020-04-17 23:17:47 +00:00
if (parent != nullptr) {
2019-04-07 14:02:41 +00:00
emit childChanged(this, row(), col);
}
}
void Models::Item::toOfflineState()
{
for (std::deque<Item*>::iterator itr = childItems.begin(), end = childItems.end(); itr != end; ++itr) {
Item* it = *itr;
it->toOfflineState();
}
}
2019-08-28 11:40:55 +00:00
2020-04-17 23:17:47 +00:00
const Models::Account * Models::Item::getParentAccount() const
2019-08-28 11:40:55 +00:00
{
const Item* p = this;
2020-04-17 23:17:47 +00:00
while (p != nullptr && p->type != Item::account) {
2019-08-28 11:40:55 +00:00
p = p->parentItemConst();
}
2020-04-17 23:17:47 +00:00
return static_cast<const Account*>(p);
2019-08-28 11:40:55 +00:00
}
QString Models::Item::getAccountJid() const
{
2020-04-17 23:17:47 +00:00
const Account* acc = getParentAccount();
if (acc == nullptr) {
2019-08-28 11:40:55 +00:00
return "";
}
return acc->getLogin() + "@" + acc->getServer();
}
QString Models::Item::getAccountResource() const
{
2020-04-17 23:17:47 +00:00
const Account* acc = getParentAccount();
if (acc == nullptr) {
2019-08-28 11:40:55 +00:00
return "";
}
return acc->getResource();
}
QString Models::Item::getAccountName() const
{
2020-04-17 23:17:47 +00:00
const Account* acc = getParentAccount();
if (acc == nullptr) {
2019-08-28 11:40:55 +00:00
return "";
}
return acc->getName();
}
2019-09-24 09:21:29 +00:00
Shared::Availability Models::Item::getAccountAvailability() const
{
2020-04-17 23:17:47 +00:00
const Account* acc = getParentAccount();
if (acc == nullptr) {
return Shared::Availability::offline;
}
return acc->getAvailability();
}
Shared::ConnectionState Models::Item::getAccountConnectionState() const
{
2020-04-17 23:17:47 +00:00
const Account* acc = getParentAccount();
if (acc == nullptr) {
return Shared::ConnectionState::disconnected;
}
return acc->getState();
}
2019-09-24 09:21:29 +00:00
QString Models::Item::getDisplayedName() const
{
return name;
}
void Models::Item::onChildChanged(Models::Item* item, int row, int col)
{
Item* parent = item->parentItem();
2020-04-17 23:17:47 +00:00
if (parent != nullptr && parent == this) {
2019-09-24 09:21:29 +00:00
if (item->columnInvolvedInDisplay(col)) {
int newRow = 0;
std::deque<Item*>::const_iterator before = childItems.begin();
2020-04-17 23:17:47 +00:00
Type ct = item->type;
if (ct == reference) {
ct = static_cast<Reference*>(item)->dereference()->type;
}
2019-09-24 09:21:29 +00:00
while (before != childItems.end()) {
Item* bfr = *before;
2020-04-17 23:17:47 +00:00
Type bt = bfr->type;
if (bt == reference) {
bt = static_cast<Reference*>(bfr)->dereference()->type;
}
if (bt > ct) {
2019-09-24 09:21:29 +00:00
break;
2020-04-17 23:17:47 +00:00
} else if (bt == ct && bfr->getDisplayedName() > item->getDisplayedName()) {
2019-09-24 09:21:29 +00:00
break;
}
newRow++;
before++;
}
if (newRow != row || (before != childItems.end() && *before != item)) {
emit childIsAboutToBeMoved(this, row, row, this, newRow);
std::deque<Item*>::const_iterator old = childItems.begin();
old += row;
childItems.erase(old);
childItems.insert(before, item);
emit childMoved();
}
}
}
emit childChanged(item, row, col);
}
bool Models::Item::columnInvolvedInDisplay(int col)
{
return col == 0;
}
2020-04-17 23:17:47 +00:00
void Models::Item::addReference(Models::Reference* ref)
{
references.insert(ref);
}
void Models::Item::removeReference(Models::Reference* ref)
{
std::set<Reference*>::const_iterator itr = references.find(ref);
if (itr != references.end()) {
references.erase(itr);
}
}
int Models::Item::getContact(const QString& jid) const
{
int index = -1;
for (std::deque<Item*>::size_type i = 0; i < childItems.size(); ++i) {
const Models::Item* item = childItems[i];
const Contact* cnt = nullptr;
if (item->type == Item::reference) {
item = static_cast<const Reference*>(item)->dereferenceConst();
}
if (item->type == Item::contact) {
cnt = static_cast<const Contact*>(item);
if (cnt->getJid() == jid) {
index = i;
break;
}
}
}
return index;
}
std::set<Models::Reference *>::size_type Models::Item::referencesCount() const
{
return references.size();
}