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

Commit

Permalink
[libcontacts] Optimize fetching of presence updates
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvogt committed Dec 10, 2013
1 parent e201832 commit 2070b9d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
59 changes: 50 additions & 9 deletions src/seasidecache.cpp
Expand Up @@ -182,6 +182,16 @@ QContactFetchHint basicFetchHint()
return fetchHint;
}

QContactFetchHint presenceFetchHint()
{
QContactFetchHint fetchHint(basicFetchHint());

setDetailTypesHint(fetchHint, DetailList() << detailType<QContactPresence>()
<< detailType<QContactGlobalPresence>());

return fetchHint;
}

QContactFetchHint metadataFetchHint(quint32 fetchTypes = 0)
{
QContactFetchHint fetchHint(basicFetchHint());
Expand Down Expand Up @@ -1510,6 +1520,28 @@ bool SeasideCache::event(QEvent *event)
m_fetchRequest.setSorting(m_sortOrder);
m_fetchRequest.start();

m_fetchProcessedCount = 0;
} else if (!m_presenceChangedContacts.isEmpty() && !m_fetchRequest.isActive()) {
const int maxRequestIds = 200;

#ifdef USING_QTPIM
QContactIdFilter filter;
#else
QContactLocalIdFilter filter;
#endif
if (m_presenceChangedContacts.count() > maxRequestIds) {
filter.setIds(m_presenceChangedContacts.mid(0, maxRequestIds));
m_presenceChangedContacts = m_presenceChangedContacts.mid(maxRequestIds);
} else {
filter.setIds(m_presenceChangedContacts);
m_presenceChangedContacts.clear();
}

m_fetchRequest.setFilter(filter & aggregateFilter());
m_fetchRequest.setFetchHint(presenceFetchHint());
m_fetchRequest.setSorting(m_sortOrder);
m_fetchRequest.start();

m_fetchProcessedCount = 0;
} else if (!m_contactsToAppend.isEmpty() || !m_contactsToUpdate.isEmpty()) {
applyPendingContactUpdates();
Expand Down Expand Up @@ -1612,14 +1644,14 @@ void SeasideCache::timerEvent(QTimerEvent *event)
void SeasideCache::contactsAdded(const QList<ContactIdType> &ids)
{
if (m_keepPopulated) {
updateContacts(ids);
updateContacts(ids, &m_changedContacts);
}
}

void SeasideCache::contactsChanged(const QList<ContactIdType> &ids)
{
if (m_keepPopulated) {
updateContacts(ids);
updateContacts(ids, &m_changedContacts);
} else {
// Update these contacts if they're already in the cache
QList<ContactIdType> presentIds;
Expand All @@ -1628,14 +1660,24 @@ void SeasideCache::contactsChanged(const QList<ContactIdType> &ids)
presentIds.append(id);
}
}
updateContacts(presentIds);
updateContacts(presentIds, &m_changedContacts);
}
}

void SeasideCache::contactsPresenceChanged(const QList<ContactIdType> &ids)
{
// For now, treat presence change equivalently to other changes
contactsChanged(ids);
if (m_keepPopulated) {
updateContacts(ids, &m_presenceChangedContacts);
} else {
// Update these contacts if they're already in the cache
QList<ContactIdType> presentIds;
foreach (const ContactIdType &id, ids) {
if (existingItem(id)) {
presentIds.append(id);
}
}
updateContacts(presentIds, &m_presenceChangedContacts);
}
}

void SeasideCache::contactsRemoved(const QList<ContactIdType> &ids)
Expand Down Expand Up @@ -1688,7 +1730,7 @@ void SeasideCache::updateContacts()
contactIds.append(it->apiId());
}

updateContacts(contactIds);
updateContacts(contactIds, &m_changedContacts);
}

void SeasideCache::fetchContacts()
Expand All @@ -1714,7 +1756,7 @@ void SeasideCache::fetchContacts()
}
}

void SeasideCache::updateContacts(const QList<ContactIdType> &contactIds)
void SeasideCache::updateContacts(const QList<ContactIdType> &contactIds, QList<ContactIdType> *updateList)
{
// Wait for new changes to be reported
static const int PostponementIntervalMs = 500;
Expand All @@ -1724,7 +1766,7 @@ void SeasideCache::updateContacts(const QList<ContactIdType> &contactIds)

if (!contactIds.isEmpty()) {
m_contactsUpdated = true;
m_changedContacts.append(contactIds);
updateList->append(contactIds);

if (m_fetchPostponed.isValid()) {
// We are waiting to accumulate further changes
Expand Down Expand Up @@ -1947,7 +1989,6 @@ void SeasideCache::contactsAvailable()
m_fetchProcessedCount += contacts.count();
fetchHint = m_fetchRequest.fetchHint();
}

if (contacts.isEmpty())
return;

Expand Down
3 changes: 2 additions & 1 deletion src/seasidecache.h
Expand Up @@ -396,7 +396,7 @@ private slots:
void requestUpdate();
void appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);
void fetchContacts();
void updateContacts(const QList<ContactIdType> &contactIds);
void updateContacts(const QList<ContactIdType> &contactIds, QList<ContactIdType> *updateList);
void applyPendingContactUpdates();
void applyContactUpdates(const QList<QContact> &contacts, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);

Expand Down Expand Up @@ -443,6 +443,7 @@ private slots:
QList<QPair<QSet<DetailTypeId>, QList<QContact> > > m_contactsToUpdate;
QList<ContactIdType> m_contactsToRemove;
QList<ContactIdType> m_changedContacts;
QList<ContactIdType> m_presenceChangedContacts;
QSet<ContactIdType> m_aggregatedContacts;
QList<QContactId> m_contactsToFetchConstituents;
QList<QContactId> m_contactsToFetchCandidates;
Expand Down

0 comments on commit 2070b9d

Please sign in to comment.