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

Commit

Permalink
Browse files Browse the repository at this point in the history
[libcontacts] Provide functionality for libcommhistory
Report changes to cached contacts, including on resolution of unknown
addressing details.
  • Loading branch information
matthewvogt committed Aug 6, 2013
1 parent 0438ffa commit 7a88250
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 37 deletions.
2 changes: 1 addition & 1 deletion config.pri
@@ -1,5 +1,4 @@
include(package.pri)
include(contacts-namespace.pri)

CONFIG += qt link_pkgconfig
QT -= gui
Expand All @@ -11,6 +10,7 @@ equals(QT_MAJOR_VERSION, 4) {
}
equals(QT_MAJOR_VERSION, 5) {
PKGCONFIG += Qt5Contacts Qt5Versit qtcontacts-sqlite-qt5-extensions
DEFINES *= USING_QTPIM

# Needed for qt4 moc, which can't handle numeric tests
DEFINES *= QT_VERSION_5
Expand Down
18 changes: 0 additions & 18 deletions contacts-namespace.pri

This file was deleted.

93 changes: 78 additions & 15 deletions src/seasidecache.cpp
Expand Up @@ -37,7 +37,7 @@
#include "qcontactstatusflags_impl.h"

#include <QCoreApplication>
#ifdef USING_QTPIM
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QStandardPaths>
#else
#include <QDesktopServices>
Expand Down Expand Up @@ -67,7 +67,9 @@

#include <QtDebug>

USE_VERSIT_NAMESPACE
#ifdef USING_QTPIM
QTVERSIT_USE_NAMESPACE
#endif

namespace {

Expand Down Expand Up @@ -485,6 +487,40 @@ void SeasideCache::unregisterNameGroupChangeListener(SeasideNameGroupChangeListe
instancePtr->m_nameGroupChangeListeners.removeAll(listener);
}

void SeasideCache::registerChangeListener(ChangeListener *listener)
{
if (!instancePtr)
new SeasideCache;
instancePtr->m_changeListeners.append(listener);
}

void SeasideCache::unregisterChangeListener(ChangeListener *listener)
{
if (!instancePtr)
return;
instancePtr->m_changeListeners.removeAll(listener);
}

void SeasideCache::unregisterResolveListener(ResolveListener *listener)
{
if (!instancePtr)
return;

// We might have outstanding resolve requests for this listener
if (instancePtr->m_activeResolve && (instancePtr->m_activeResolve->listener == listener)) {
instancePtr->m_activeResolve = 0;
}

QList<ResolveData>::iterator it = instancePtr->m_resolveAddresses.begin();
for ( ; it != instancePtr->m_resolveAddresses.end(); ) {
if (it->listener == listener) {
it = instancePtr->m_resolveAddresses.erase(it);
} else {
++it;
}
}
}

QChar SeasideCache::nameGroupForCacheItem(CacheItem *cacheItem)
{
if (!cacheItem)
Expand Down Expand Up @@ -666,6 +702,8 @@ SeasideCache::CacheItem *SeasideCache::resolvePhoneNumber(ResolveListener *liste
CacheItem *item = itemByPhoneNumber(number, requireComplete);
if (!item) {
instancePtr->resolveAddress(listener, QString(), number, requireComplete);
} else if (requireComplete) {
ensureCompletion(item);
}

return item;
Expand All @@ -676,6 +714,8 @@ SeasideCache::CacheItem *SeasideCache::resolveEmailAddress(ResolveListener *list
CacheItem *item = itemByEmailAddress(address, requireComplete);
if (!item) {
instancePtr->resolveAddress(listener, address, QString(), requireComplete);
} else if (requireComplete) {
ensureCompletion(item);
}
return item;
}
Expand All @@ -685,6 +725,8 @@ SeasideCache::CacheItem *SeasideCache::resolveOnlineAccount(ResolveListener *lis
CacheItem *item = itemByOnlineAccount(localUid, remoteUid, requireComplete);
if (!item) {
instancePtr->resolveAddress(listener, localUid, remoteUid, requireComplete);
} else if (requireComplete) {
ensureCompletion(item);
}
return item;
}
Expand Down Expand Up @@ -1131,7 +1173,7 @@ bool SeasideCache::event(QEvent *event)
remoteFilter.setMatchFlags(QContactFilter::MatchExactly | QContactFilter::MatchFixedString); // allow case insensitive
remoteFilter.setValue(resolve.second);

m_fetchRequest.setFilter(localFilter | remoteFilter);
m_fetchRequest.setFilter(localFilter & remoteFilter);
}

// If completion is not required, we need to at least retrieve as much detail
Expand Down Expand Up @@ -1207,22 +1249,30 @@ void SeasideCache::contactsChanged(const QList<ContactIdType> &ids)

void SeasideCache::contactsRemoved(const QList<ContactIdType> &ids)
{
QList<ContactIdType> presentIds;

foreach (const ContactIdType &id, ids) {
if (CacheItem *item = existingItem(id)) {
// Report this item is about to be removed
foreach (ChangeListener *listener, m_changeListeners) {
listener->itemAboutToBeRemoved(item);
}
if (!m_keepPopulated) {
presentIds.append(id);
}
}
}

if (m_keepPopulated) {
m_refreshRequired = true;
requestUpdate();
} else {
// Remove these contacts if they're already in the cache
bool present = false;
foreach (const ContactIdType &id, ids) {
if (existingItem(id)) {
present = true;
m_expiredContacts[id] += -1;
}
}
if (present) {
requestUpdate();
// Remove these contacts if they're already in the cache; they won't be removed by syncing
foreach (const ContactIdType &id, presentIds) {
m_expiredContacts[id] += -1;
}
}

requestUpdate();
}

void SeasideCache::updateContacts()
Expand Down Expand Up @@ -1273,6 +1323,10 @@ void SeasideCache::updateContacts(const QList<ContactIdType> &contactIds)
m_contactsUpdated = true;
m_changedContacts.append(contactIds);

foreach (const ContactIdType &id, contactIds) {
m_reportIds.insert(internalId(id));
}

if (m_fetchPostponed.isValid()) {
// We are waiting to accumulate further changes
int remainder = MaxPostponementMs - m_fetchPostponed.elapsed();
Expand Down Expand Up @@ -1471,6 +1525,15 @@ void SeasideCache::contactsAvailable()
if (roleDataChanged) {
instancePtr->contactDataChanged(apiId);
}

if (m_reportIds.contains(item->iid)) {
m_reportIds.remove(item->iid);

// Report the change to this contact
foreach (ChangeListener *listener, m_changeListeners) {
listener->itemUpdated(item);
}
}
}
m_resultsRead = contacts.count();
notifyNameGroupsChanged(modifiedGroups);
Expand Down Expand Up @@ -1837,7 +1900,7 @@ void SeasideCache::requestStateChanged(QContactAbstractRequest::State state)
if (!m_fetchRequest.contacts().isEmpty()) {
item = itemById(apiId(m_fetchRequest.contacts().first()), false);
}
m_activeResolve->listener->addressResolved(item);
m_activeResolve->listener->addressResolved(m_activeResolve->first, m_activeResolve->second, item);

m_activeResolve = 0;
m_resolveAddresses.takeFirst();
Expand Down
23 changes: 20 additions & 3 deletions src/seasidecache.h
Expand Up @@ -65,11 +65,13 @@
#include <mgconfitem.h>
#endif

USE_CONTACTS_NAMESPACE

#ifdef USING_QTPIM
QTCONTACTS_USE_NAMESPACE

typedef QContactDetail::DetailType DetailTypeId;
#else
QTM_USE_NAMESPACE

typedef QString DetailTypeId;
#endif

Expand Down Expand Up @@ -186,7 +188,15 @@ class CONTACTCACHE_EXPORT SeasideCache : public QObject
{
virtual ~ResolveListener() {}

virtual void addressResolved(CacheItem *item) = 0;
virtual void addressResolved(const QString &first, const QString &second, CacheItem *item) = 0;
};

struct ChangeListener
{
virtual ~ChangeListener() {}

virtual void itemUpdated(CacheItem *item) = 0;
virtual void itemAboutToBeRemoved(CacheItem *item) = 0;
};

static SeasideCache *instance();
Expand Down Expand Up @@ -214,6 +224,11 @@ class CONTACTCACHE_EXPORT SeasideCache : public QObject
static void registerNameGroupChangeListener(SeasideNameGroupChangeListener *listener);
static void unregisterNameGroupChangeListener(SeasideNameGroupChangeListener *listener);

static void registerChangeListener(ChangeListener *listener);
static void unregisterChangeListener(ChangeListener *listener);

static void unregisterResolveListener(ResolveListener *listener);

static DisplayLabelOrder displayLabelOrder();

static int contactId(const QContact &contact);
Expand Down Expand Up @@ -357,6 +372,7 @@ private slots:
QList<QContactRelationship> m_relationshipsToSave;
QList<QContactRelationship> m_relationshipsToRemove;
QList<SeasideNameGroupChangeListener*> m_nameGroupChangeListeners;
QList<ChangeListener*> m_changeListeners;
QVector<ContactIdType> m_contacts[FilterTypesCount];
QList<ListModel *> m_models[FilterTypesCount];
QSet<QObject *> m_users;
Expand Down Expand Up @@ -393,6 +409,7 @@ private slots:
bool m_contactsUpdated;
QList<ContactIdType> m_constituentIds;
QList<ContactIdType> m_candidateIds;
QSet<quint32> m_reportIds;

struct ResolveData {
QString first;
Expand Down

0 comments on commit 7a88250

Please sign in to comment.