Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
[libcontacts] Ensure that previously retrieved details are maintained
Browse files Browse the repository at this point in the history
Whenever we update a contact in the cache, copy any existing details
of the contact to the new copy, if those details are present in the
cache but not in the new query.
  • Loading branch information
matthewvogt committed Oct 2, 2013
1 parent 5e5693d commit a35ae46
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
62 changes: 35 additions & 27 deletions src/seasidecache.cpp
Expand Up @@ -670,6 +670,9 @@ SeasideCache::CacheItem *SeasideCache::itemById(const ContactIdType &id, bool re
} else {
// Insert a new item into the cache if the one doesn't exist.
item = &(instancePtr->m_people[iid]);
item->iid = iid;
item->contactState = ContactAbsent;

#ifdef USING_QTPIM
item->contact.setId(id);
#else
Expand Down Expand Up @@ -1688,6 +1691,17 @@ bool SeasideCache::updateContactIndexing(const QContact &oldContact, const QCont
return modified;
}

void updateDetailsFromCache(QContact &contact, SeasideCache::CacheItem *item, const QSet<DetailTypeId> &queryDetailTypes)
{
// Copy any existing detail types that are in the current record to the new instance
foreach (const QContactDetail &existing, item->contact.details()) {
if (!queryDetailTypes.contains(detailType(existing))) {
QContactDetail copy(existing);
contact.saveDetail(&copy);
}
}
}

void SeasideCache::contactsAvailable()
{
QContactAbstractRequest *request = static_cast<QContactAbstractRequest *>(sender());
Expand Down Expand Up @@ -1721,7 +1735,7 @@ void SeasideCache::contactsAvailable()
FilterType type(m_populateProgress == FetchFavorites ? FilterFavorites
: (m_populateProgress == FetchMetadata ? FilterAll
: FilterOnline));
appendContacts(contacts, type, partialFetch);
appendContacts(contacts, type, partialFetch, queryDetailTypes);
} else {
// An update.
QSet<QString> modifiedGroups;
Expand All @@ -1730,30 +1744,21 @@ void SeasideCache::contactsAvailable()
QContact contact = contacts.at(i);
quint32 iid = internalId(contact);

CacheItem *item = existingItem(iid);
QString oldNameGroup;
QString oldDisplayLabel;

const bool preexisting = (item != 0);
CacheItem *item = existingItem(iid);
if (!item) {
// We haven't seen this contact before
item = &(m_people[iid]);
item->iid = iid;
}

QString oldNameGroup;
QString oldDisplayLabel;

if (preexisting) {
} else {
oldNameGroup = item->nameGroup;
oldDisplayLabel = item->displayLabel;

if (partialFetch) {
// Copy any existing detail types that are in the current record to the new instance
foreach (const QContactDetail &existing, item->contact.details()) {
if (!queryDetailTypes.contains(detailType(existing))) {
QContactDetail copy(existing);
contact.saveDetail(&copy);
}
}
// Update our new instance with any details not returned by the current query
updateDetailsFromCache(contact, item, queryDetailTypes);
}
}

Expand Down Expand Up @@ -1913,7 +1918,7 @@ int SeasideCache::insertRange(FilterType filter, int index, int count, const QLi
return end - index + 1;
}

void SeasideCache::appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch)
void SeasideCache::appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes)
{
if (!contacts.isEmpty()) {
QList<quint32> &cacheIds = m_contacts[filterType];
Expand All @@ -1932,21 +1937,24 @@ void SeasideCache::appendContacts(const QList<QContact> &contacts, FilterType fi

foreach (QContact contact, contacts) {
quint32 iid = internalId(contact);

cacheIds.append(iid);

CacheItem &cacheItem = m_people[iid];

// If we have already requested this contact as a favorite, don't update with fewer details
if ((cacheItem.iid == 0) ||
(cacheItem.contact.detail<QContactFavorite>().isFavorite() == false)) {
cacheItem.iid = iid;
updateContactIndexing(QContact(), contact, iid, QSet<DetailTypeId>(), &cacheItem);
updateCache(&cacheItem, contact, partialFetch);
CacheItem *item = existingItem(iid);
if (!item) {
item = &(m_people[iid]);
item->iid = iid;
} else {
if (partialFetch) {
// Update our new instance with any details not returned by the current query
updateDetailsFromCache(contact, item, queryDetailTypes);
}
}

updateContactIndexing(item->contact, contact, iid, queryDetailTypes, item);
updateCache(item, contact, partialFetch);

if (filterType == FilterAll) {
addToContactNameGroup(iid, nameGroup(&cacheItem), &modifiedGroups);
addToContactNameGroup(iid, nameGroup(item), &modifiedGroups);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/seasidecache.h
Expand Up @@ -390,7 +390,7 @@ private slots:
void keepPopulated(quint32 fetchTypes);

void requestUpdate();
void appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch);
void appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);
void fetchContacts();
void updateContacts(const QList<ContactIdType> &contactIds);

Expand Down

0 comments on commit a35ae46

Please sign in to comment.