コード例 #1
0
ファイル: contactlistmodel.cpp プロジェクト: psi-im/psi
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;
}
コード例 #2
0
ファイル: contactlistmodel.cpp プロジェクト: psi-im/psi
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);
}
コード例 #3
0
void ContactListUtil::removeContact(PsiContact* contact, QMimeData* _selection, ContactListDragModel* model, QWidget* widget, QObject* obj)
{
	Q_ASSERT(model);
	QModelIndexList indexes = model ? model->indexesFor(contact, _selection) : QModelIndexList();
	if (model && !indexes.isEmpty()) {
		QMimeData* selection = model->mimeData(indexes);
		QString selectionData = confirmationData(contact, _selection, model);

		// WARNING: selection could also contain groups. and when there are groups,
		// all groups' contacts are marked for deletion too
		QList<PsiContact*> contactsLost = model->contactsLostByRemove(selection);
		bool removeConfirmed = contactsLost.isEmpty();
		bool confirmWithoutPrompt = false;

		if (!removeConfirmed) {
			QStringList contactNames = contactNamesFor(contactsLost);

			QString destructiveActionName;
			QString msg;
			bool doPrompt = true;

			// don't prompt when removing single muc or not-in-list contacts
			if(contactsLost.count() == 1 && (contactsLost[0]->isPrivate() || !contactsLost[0]->inList())) {
				doPrompt = false;
			}

			if (!contactNames.isEmpty()) {
				msg = tr("This will permanently remove<br>"
						 "%1"
						 "<br>from your contact list."
                         ).arg(contactNames.join(", ").replace("\\40","@"));
			}

#ifdef YAPSI
			QString complimentaryActionName;
			const char* complimentaryActionSlot = 0;

			ContactListModelSelection contactListSelection(selection);
			if (contactNames.count() > 1 || contactListSelection.groups().count()) {
				QStringList tmp;
				QStringList tmpContactNames = contactNames;
				while (!tmpContactNames.isEmpty()) {
					if (tmp.count() >= 10)
						break;

					tmp << QString("%1. %2").arg(tmp.count() + 1).arg(tmpContactNames.takeFirst());
				}

				QString andNContacts;
				if (!tmpContactNames.isEmpty()) {
					andNContacts = tr("and %n contacts ", 0,
									  tmpContactNames.count());
				}

				if (contactListSelection.groups().count() > 1) {
					msg = tr("This will permanently remove:<br>"
							 "%1"
							 "<br>%2and %n groups from your contact list.",
							 0,
							 contactListSelection.groups().count()
							).arg(tmp.join("<br>"))
							 .arg(andNContacts);
				}
				else if (contactListSelection.groups().count() == 1) {
					msg = tr("This will permanently remove:<br>"
							 "%1"
							 "<br>%2and \"%3\" group from your contact list."
							).arg(tmp.join("<br>"))
							 .arg(andNContacts)
							 .arg(contactListSelection.groups().first().fullName);
				}
				else {
					msg = tr("This will permanently remove:<br>"
							 "%1"
							 "<br>%2from your contact list."
							).arg(tmp.join("<br>"))
							 .arg(andNContacts);
				}
			}

			if (indexes.count() == 1 && model->indexType(indexes.first()) == ContactListModel::GroupType) {
				if (YaContactListContactsModel::virtualUnremovableGroups().contains(contactListSelection.groups().first().fullName)) {
					msg = tr("This is a system group and can't be removed. "
							 "Permanently remove all its contacts from your contact list?");
					destructiveActionName = tr("Clear Group");
				}
				else {
					msg = tr("This will permanently remove<br>"
							 "%1"
							 "<br>group and all its contacts from your contact list.").arg(TextUtil::escape(indexes.first().data().toString()));
				}
			}
			else if (indexes.count() == 1 && model->indexType(indexes.first()) == ContactListModel::ContactType) {
				Q_ASSERT(contactListSelection.contacts().count() == 1);
				ContactListModelSelection::Contact c = contactListSelection.contacts().first();

				PsiAccount* account = contactList()->getAccount(c.account);
				PsiContact* psiContact = account ? account->findContact(c.jid) : 0;
				QStringList contactGroupsLostByRemove;
				if (psiContact) {
					contactGroupsLostByRemove = model->contactGroupsLostByRemove(psiContact, selection);
				}

				if (psiContact && !psiContact->inList() && psiContact->blockAvailable()) {
					msg = tr("This will permanently remove %1 from your contact list. "
							 "You could block it in order to avoid further messages.")
                            .arg(contactNames.join(", ").replace("\\40","@"));
					destructiveActionName = tr("Delete");
					complimentaryActionName = tr("Block");
					complimentaryActionSlot = "blockContactConfirmation";
				}
				else if (psiContact && psiContact->groups().count() > 1 && contactGroupsLostByRemove.count() == 1) {
					// TODO: needs to be translated
					msg = tr("This will remove %1 from \"%2\" group. "
							 "You could also remove it from all groups.")
						  .arg(contactNames.join(", "))
						  .arg(contactGroupsLostByRemove.first());
					destructiveActionName = tr("Delete");
					complimentaryActionName = tr("Delete From All Groups");
					// TODO: needs to be implemented
					complimentaryActionSlot = "removeContactFullyConfirmation";
				}
			}
#endif

			if (!msg.isEmpty()) {
				if (doPrompt) {
#ifdef YAPSI
					if (complimentaryActionSlot) {
						RemoveConfirmationMessageBoxManager::instance()->
							removeConfirmation(selectionData,
											   obj, "removeContactConfirmation",
											   obj, complimentaryActionSlot,
											   tr("Deleting contacts"),
											   msg,
											   widget,
											   destructiveActionName,
											   complimentaryActionName);
					}
#else
					if (false) {
					}
#endif
					else {
						RemoveConfirmationMessageBoxManager::instance()->
							removeConfirmation(selectionData,
											   obj, "removeContactConfirmation",
											   tr("Deleting contacts"),
											   msg,
											   widget,
											   destructiveActionName);
					}

					removeConfirmed = false;
				}
				else {
					confirmWithoutPrompt = true;
				}
			}
		}

		QMetaObject::invokeMethod(obj, "removeContactConfirmation", Qt::DirectConnection,
								  QGenericReturnArgument(),
								  Q_ARG(QString, selectionData), Q_ARG(bool, removeConfirmed));

		if(confirmWithoutPrompt) {
			QMetaObject::invokeMethod(obj, "removeContactConfirmation", Qt::QueuedConnection,
								  QGenericReturnArgument(),
								  Q_ARG(QString, selectionData), Q_ARG(bool, true));
		}

		delete selection;
	}