2019-06-22 20:37:22 +00:00
|
|
|
/*
|
2019-09-01 19:46:12 +00:00
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
2019-06-22 20:37:22 +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 "group.h"
|
|
|
|
#include "contact.h"
|
2020-04-17 23:17:47 +00:00
|
|
|
#include "reference.h"
|
2019-06-22 20:37:22 +00:00
|
|
|
|
|
|
|
Models::Group::Group(const QMap<QString, QVariant>& data, Models::Item* parentItem):
|
|
|
|
Item(group, data, parentItem),
|
|
|
|
unreadMessages(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Models::Group::~Group()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-18 12:02:01 +00:00
|
|
|
void Models::Group::_appendChild(Models::Item* child)
|
2019-06-22 20:37:22 +00:00
|
|
|
{
|
2020-04-18 12:02:01 +00:00
|
|
|
Item::_appendChild(child);
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(child, &Item::childChanged, this, &Group::refresh);
|
2019-06-22 20:37:22 +00:00
|
|
|
changed(1);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Group::columnCount() const
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Group::data(int column) const
|
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case 0:
|
|
|
|
return getName();
|
|
|
|
case 1:
|
|
|
|
return QVariant((unsigned int)childItems.size());
|
|
|
|
case 2:
|
|
|
|
return unreadMessages;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Group::_removeChild(int index)
|
|
|
|
{
|
|
|
|
Item* child = childItems[index];
|
2019-11-03 18:46:40 +00:00
|
|
|
disconnect(child, &Item::childChanged, this, &Group::refresh);
|
2019-06-22 20:37:22 +00:00
|
|
|
Item::_removeChild(index);
|
|
|
|
changed(1);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Models::Group::getUnreadMessages() const
|
|
|
|
{
|
|
|
|
return unreadMessages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Group::setUnreadMessages(unsigned int amount)
|
|
|
|
{
|
|
|
|
if (unreadMessages != amount) {
|
|
|
|
unreadMessages = amount;
|
|
|
|
changed(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Group::refresh()
|
|
|
|
{
|
|
|
|
unsigned int newAmount(0);
|
|
|
|
|
2020-04-18 12:02:01 +00:00
|
|
|
for (Models::Item* item : childItems) {
|
|
|
|
if (item->type == reference) {
|
|
|
|
item = static_cast<Reference*>(item)->dereference();
|
|
|
|
}
|
|
|
|
if (item->type == contact) {
|
|
|
|
Models::Contact* cnt = static_cast<Models::Contact*>(item);
|
|
|
|
newAmount += cnt->getMessagesCount();
|
|
|
|
}
|
2019-06-22 20:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setUnreadMessages(newAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Models::Group::getOnlineContacts() const
|
|
|
|
{
|
|
|
|
unsigned int amount(0);
|
|
|
|
|
2020-04-18 12:02:01 +00:00
|
|
|
for (Models::Item* item : childItems) {
|
|
|
|
if (item->type == reference) {
|
|
|
|
item = static_cast<Reference*>(item)->dereference();
|
|
|
|
}
|
|
|
|
if (item->type == contact) {
|
|
|
|
Models::Contact* cnt = static_cast<Models::Contact*>(item);
|
|
|
|
if (cnt->getAvailability() != Shared::Availability::offline) {
|
|
|
|
++amount;
|
|
|
|
}
|
2019-06-22 20:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|