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

Commit

Permalink
[libcontacts] Update changed contacts progressively
Browse files Browse the repository at this point in the history
Update contacts in small batches to prevent blocking the UI thread.
  • Loading branch information
matthewvogt committed Dec 9, 2013
1 parent 380fa72 commit 7ef500e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/seasidecache.cpp
Expand Up @@ -1531,6 +1531,12 @@ bool SeasideCache::event(QEvent *event)
m_contactIdRequest.setFilter(favoriteFilter());
m_contactIdRequest.setSorting(m_sortOrder);
m_contactIdRequest.start();
} else if (!m_contactsToUpdate.isEmpty()) {
applyPendingContactUpdates();
if (!m_contactsToUpdate.isEmpty()) {
// Send another event to trigger further processing
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
} else {
m_updatesPending = false;

Expand Down Expand Up @@ -1912,6 +1918,9 @@ void SeasideCache::contactsAvailable()
fetchHint = m_fetchRequest.fetchHint();
}

if (contacts.isEmpty())
return;

QSet<DetailTypeId> queryDetailTypes;
foreach (const DetailTypeId &typeId, detailTypesHint(fetchHint)) {
queryDetailTypes.insert(typeId);
Expand All @@ -1925,7 +1934,49 @@ void SeasideCache::contactsAvailable()
: FilterOnline));
appendContacts(contacts, type, partialFetch, queryDetailTypes);
} else {
// An update.
if (m_activeResolve) {
// Process these results immediately
applyContactUpdates(contacts, partialFetch, queryDetailTypes);
} else {
// Add these contacts to the list to be progressively appended
QList<QPair<QSet<DetailTypeId>, QList<QContact> > >::iterator it = m_contactsToUpdate.begin(), end = m_contactsToUpdate.end();
for ( ; it != end; ++it) {
if ((*it).first == queryDetailTypes) {
(*it).second.append(contacts);
break;
}
}
if (it == end) {
m_contactsToUpdate.append(qMakePair(queryDetailTypes, contacts));
}

requestUpdate();
}
}
}

void SeasideCache::applyPendingContactUpdates()
{
const int updateBatchSize = 3;

QList<QPair<QSet<DetailTypeId>, QList<QContact> > >::iterator it = m_contactsToUpdate.begin();

QSet<DetailTypeId> &detailTypes((*it).first);
const bool partialFetch = !detailTypes.isEmpty();

// Update a small number of retrieved contacts
QList<QContact> &updatedContacts((*it).second);
if (updatedContacts.count() > updateBatchSize) {
applyContactUpdates(updatedContacts.mid(0, updateBatchSize), partialFetch, detailTypes);
updatedContacts = updatedContacts.mid(updateBatchSize);
} else {
applyContactUpdates(updatedContacts, partialFetch, detailTypes);
m_contactsToUpdate.erase(it);
}
}

void SeasideCache::applyContactUpdates(const QList<QContact> &contacts, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes)
{
QSet<QString> modifiedGroups;

foreach (QContact contact, contacts) {
Expand Down Expand Up @@ -1979,7 +2030,6 @@ void SeasideCache::contactsAvailable()

notifyNameGroupsChanged(modifiedGroups);
}
}

void SeasideCache::addToContactNameGroup(quint32 iid, const QString &group, QSet<QString> *modifiedGroups)
{
Expand Down
3 changes: 3 additions & 0 deletions src/seasidecache.h
Expand Up @@ -395,6 +395,8 @@ private slots:
void appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);
void fetchContacts();
void updateContacts(const QList<ContactIdType> &contactIds);
void applyPendingContactUpdates();
void applyContactUpdates(const QList<QContact> &contacts, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);

void resolveUnknownAddresses(const QString &first, const QString &second, CacheItem *item);
bool updateContactIndexing(const QContact &oldContact, const QContact &contact, quint32 iid, const QSet<DetailTypeId> &queryDetailTypes, CacheItem *item);
Expand Down Expand Up @@ -435,6 +437,7 @@ private slots:
QHash<ContactIdType, QContact> m_contactsToSave;
QHash<QString, QSet<quint32> > m_contactNameGroups;
QList<QContact> m_contactsToCreate;
QList<QPair<QSet<DetailTypeId>, QList<QContact> > > m_contactsToUpdate;
QList<ContactIdType> m_contactsToRemove;
QList<ContactIdType> m_changedContacts;
QSet<ContactIdType> m_aggregatedContacts;
Expand Down

0 comments on commit 7ef500e

Please sign in to comment.