QMap<int,GContactsStatus> GContactsBackend::modifyContacts( QList<QContact> &aContactList, const QStringList &aContactIdList) { FUNCTION_CALL_TRACE; Q_ASSERT (iMgr); GContactsStatus status; QMap<int,QContactManager::Error> errors; QMap<int,GContactsStatus> statusMap; for (int i = 0; i < aContactList.size(); i++) { LOG_DEBUG("Replacing item's ID " << aContactList.at(i)); LOG_DEBUG("Id of the contact to be replaced" << aContactIdList.at(i)); QContact contact; getButeoContact(QContactId::fromString(aContactIdList.at(i)), contact); if (!contact.isEmpty()) { aContactList[i].setId(QContactId::fromString(aContactIdList.at(i))); } } if(iMgr->saveContacts(&aContactList , &errors)) { LOG_DEBUG("Batch Modification of Contacts Succeeded"); } else { LOG_DEBUG("Batch Modification of Contacts Failed"); } // QContactManager will populate errorMap only for errors, but we use this as a status map, // so populate NoError if there's no error. // TODO QContactManager populates indices from the aContactList, but we populate keys, is this OK? for (int i = 0; i < aContactList.size(); i++) { QContactId contactId = aContactList.at(i).id(); if( !errors.contains(i) ) { LOG_DEBUG("No error for contact with id " << contactId << " and index " << i); status.errorCode = QContactManager::NoError; statusMap.insert(i, status); } else { LOG_DEBUG("contact with id " << contactId << " and index " << i <<" is in error"); QContactManager::Error errorCode = errors.value(i); status.errorCode = errorCode; statusMap.insert(i, status); } } return statusMap; }
/*! Adds QContactTag details to the \a contacts, based on the \a relationships. Tags are created such that if a group contact, B with display label "b", has a HasMember relationship with a contact, A, then a QContactTag, "b", is added to A. Group contacts can be passed in with the \a contacts list. If a contact is part of a group which is not in \a contacts, the \a manager is queried to find them. */ void createTagsFromGroups(QList<QContact>* contacts, const QList<QContactRelationship>& relationships, const QContactManager* manager) { // Map from QContactIds to group names QMap<QContactId, QString> groupMap; // Map from QContactIds to indices into the contacts list QMap<QContactId, int> indexMap; // Build up groupMap and indexMap for (int i = 0; i < contacts->size(); ++i) { QContact contact = contacts->at(i); if (contact.type() == QContactType::TypeGroup) { // In practice, you may want to check that there aren't two distinct groups with the // same name, and you may want to use a field other than display label. groupMap.insert(contact.id(), contact.displayLabel()); } indexMap.insert(contact.id(), i); } // Now add all the tags specified by the group relationships foreach (const QContactRelationship& rel, relationships) { if (rel.relationshipType() == QContactRelationship::HasMember && indexMap.contains(rel.second())) { QString groupName = groupMap.value(rel.first()); // Have we seen the group before? if (groupName.isEmpty()) { // Try and find the group in the manager QContactId groupId = rel.second(); QContactFetchHint fetchHint; fetchHint.setDetailDefinitionsHint(QStringList(QContactDisplayLabel::DefinitionName)); QContact contact = manager->contact(groupId.localId(), fetchHint); if (!contact.isEmpty()) { groupName = contact.displayLabel(); groupMap.insert(groupId, groupName); // Cache the group id/name } } if (!groupName.isEmpty()) { // Add the tag QContactTag tag; tag.setTag(groupName); (*contacts)[indexMap.value(rel.second())].saveDetail(&tag); } } } }