void ContactListModel::Private::removeContact(PsiContact *contact) { Q_ASSERT(monitoredContacts.contains(contact)); if (!monitoredContacts.contains(contact)) return; while (monitoredContacts.contains(contact)) { QModelIndex index = monitoredContacts.take(contact); if (!index.isValid()) { continue; } q->beginRemoveRows(index.parent(), index.row(), index.row()); ContactListItem *item = q->toItem(index); ContactListItem *group = item->parent(); delete item; q->endRemoveRows(); index = q->toModelIndex(group); // Delete empty group if (group->isGroup() && !group->childCount()) { q->beginRemoveRows(index.parent(), index.row(), index.row()); delete group; q->endRemoveRows(); } else { // Update group emit q->dataChanged(index, index); } } disconnect(contact, nullptr, this, nullptr); operationQueue.remove(contact); }
bool ContactListModel::setData(const QModelIndex &index, const QVariant &data, int role) { if (!index.isValid()) return false; ContactListItem *item = toItem(index); if (!item) return false; PsiContact *contact = item->isContact() ? item->contact() : nullptr; if (role == ActivateRole) { if (!contact) return false; contact->activate(); return true; } else if (role == Qt::EditRole) { QString name = data.toString(); if (contact) { item->setName(name); emit dataChanged(index, index); } else if (item->isGroup() && !name.isEmpty()) { QString oldName = item->name(); QList<PsiContact*> contacts; for (int i = 0; i < item->childCount(); ++i) { if (item->child(i)->isContact()) contacts << item->child(i)->contact(); } for (PsiContact *contact: contacts) { QStringList groups = contact->groups(); groups.removeOne(oldName); groups << name; contact->setGroups(groups); } } return true; } else if (role == ExpandedRole) { if (!item->isContact()) { item->setExpanded(data.toBool()); } } return true; }
void ContactListModel::Private::contactUpdated() { PsiContact *contact = qobject_cast<PsiContact*>(sender()); Q_ASSERT(monitoredContacts.contains(contact)); if (!monitoredContacts.contains(contact)) return; // Check for groups changing // Maybe very difficult and should be simplified? QList<ContactListItem*> groupItems; for (const QPersistentModelIndex &index: monitoredContacts.values(contact)) { ContactListItem *item = q->toItem(index); ContactListItem *parent = item->parent(); if (parent && parent->isGroup()) { groupItems << parent; } } Operation operation = Operation::UpdateContact; ContactListItem::SpecialGroupType specialGroupType = specialGroupFor(contact); if (specialGroupType == ContactListItem::SpecialGroupType::NoneSpecialGroupType) { QStringList groups1; for (ContactListItem *item: groupItems) { groups1 << item->name(); } groups1.sort(); QStringList groups2 = contact->groups(); groups2.sort(); if (groups1 != groups2) { operation = Operation::ContactGroupsChanged; } } else if (groupItems.size() > 1 || (!groupItems.isEmpty() && groupItems.first()->specialGroupType() != specialGroupType)) { operation = Operation::ContactGroupsChanged; } addOperation(contact, operation); }