squawk/ui/models/group.cpp

117 lines
2.9 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/>.
*/
#include "group.h"
#include "contact.h"
2020-04-17 23:17:47 +00:00
#include "reference.h"
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)
{
2020-04-18 12:02:01 +00:00
Item::_appendChild(child);
connect(child, &Item::childChanged, this, &Group::refresh);
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];
disconnect(child, &Item::childChanged, this, &Group::refresh);
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();
}
}
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;
}
}
}
return amount;
}