Example #1
0
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;
}
Example #2
0
/**
 * TODO
 */
bool PsiContactListModel::setData(const QModelIndex& index, const QVariant& data, int role)
{
	if (role == ActivateRole) {
		if (!index.isValid())
			return false;

		ContactListItem* item = static_cast<ContactListItem*>(index.internalPointer());
		PsiContact* contact = dynamic_cast<PsiContact*>(item);
		if (!contact)
			return false;

		contact->activate();
		return true;
	}
	else if (role == Qt::EditRole) {
		ContactListItem* item = static_cast<ContactListItem*>(index.internalPointer());
		PsiContactGroup* group   = 0;
		PsiContact*      contact = 0;
		QString name = data.toString();
		if ((contact = dynamic_cast<PsiContact*>(item))) {
			if (name.isEmpty()) {
				QMessageBox::information(0, tr("Error"), tr("You can't set a blank name."));
				return false;
			}
			else {
				contact->setName(name);
			}
		}
		else if ((group = dynamic_cast<PsiContactGroup*>(item))) {
			if (name.isEmpty()) {
				QMessageBox::information(0, tr("Error"), tr("You can't set a blank group name."));
				return false;
			}
			else {
				// make sure we don't have it already
				if (group->account()->groupList().contains(name)) {
					QMessageBox::information(0, tr("Error"), tr("You already have a group with that name."));
					return false;
				}

				group->setName(name);
			}
		}
		emit dataChanged(index, index);
		return true;
	}

	return ContactListModel::setData(index, data, role);
}