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

Commit

Permalink
[libcontacts] Allow multiple listeners for any contact
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvogt committed Aug 9, 2013
1 parent 4ac0ef8 commit 3e0f808
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
18 changes: 15 additions & 3 deletions src/seasidecache.cpp
Expand Up @@ -1227,7 +1227,6 @@ bool SeasideCache::event(QEvent *event)
QHash<quint32, CacheItem>::iterator cacheItem = m_people.find(iid);
if (cacheItem != m_people.end()) {
delete cacheItem->itemData;
delete cacheItem->modelData;
m_people.erase(cacheItem);
}
}
Expand Down Expand Up @@ -1281,6 +1280,15 @@ void SeasideCache::contactsRemoved(const QList<ContactIdType> &ids)
foreach (ChangeListener *listener, m_changeListeners) {
listener->itemAboutToBeRemoved(item);
}

ItemListener *listener = item->listeners;
while (listener) {
ItemListener *next = listener->next;
listener->itemAboutToBeRemoved(item);
listener = next;
}
item->listeners = 0;

if (!m_keepPopulated) {
presentIds.append(id);
}
Expand Down Expand Up @@ -1376,9 +1384,13 @@ void SeasideCache::updateCache(CacheItem *item, const QContact &contact, bool pa
}

item->statusFlags = contact.detail<QContactStatusFlags>().flagsValue();
if (item->modelData) {
item->modelData->contactChanged(contact, item->contactState);

ItemListener *listener = item->listeners;
while (listener) {
listener->itemUpdated(item);
listener = listener->next;
}

if (item->itemData) {
item->itemData->updateContact(contact, &item->contact, item->contactState);
} else {
Expand Down
46 changes: 39 additions & 7 deletions src/seasidecache.h
Expand Up @@ -134,28 +134,60 @@ class CONTACTCACHE_EXPORT SeasideCache : public QObject
virtual QList<int> constituents() const = 0;
};

struct ModelData
struct CacheItem;
struct ItemListener
{
virtual ~ModelData() {}
ItemListener() : next(0), key(0) {}
virtual ~ItemListener() {}

virtual void contactChanged(const QContact &newContact, ContactState state) = 0;
virtual void itemUpdated(CacheItem *item) = 0;
virtual void itemAboutToBeRemoved(CacheItem *item) = 0;

ItemListener *next;
void *key;
};

struct CacheItem
{
CacheItem() : itemData(0), modelData(0), iid(0), contactState(ContactAbsent) {}
CacheItem() : itemData(0), iid(0), contactState(ContactAbsent), listeners(0) {}
CacheItem(const QContact &contact)
: contact(contact), itemData(0), modelData(0), iid(internalId(contact)),
statusFlags(contact.detail<QContactStatusFlags>().flagsValue()), contactState(ContactAbsent) {}
: contact(contact), itemData(0), iid(internalId(contact)),
statusFlags(contact.detail<QContactStatusFlags>().flagsValue()), contactState(ContactAbsent), listeners(0) {}

ContactIdType apiId() const { return SeasideCache::apiId(contact); }

ItemListener *appendListener(ItemListener *listener, void *key)
{
if (listeners) {
ItemListener *existing(listeners);
while (existing->next) {
existing = existing->next;
}
existing->next = listener;
} else {
listeners = listener;
}

listener->next = 0;
listener->key = key;
return listener;
}

ItemListener *listener(void *key)
{
ItemListener *l(listeners);
while (l && (l->key != key) && (l->next)) {
l = l->next;
}
return (l && (l->key == key)) ? l : 0;
}

QContact contact;
ItemData *itemData;
ModelData *modelData;
quint32 iid;
quint64 statusFlags;
ContactState contactState;
ItemListener *listeners;
};

struct ContactLinkRequest
Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Expand Up @@ -10,7 +10,7 @@ target.path = $$PREFIX/lib
INSTALLS += target

# set version for generated pkgconfig files
VERSION=0.0.7
VERSION=0.0.9
QMAKE_PKGCONFIG_INCDIR = $$PREFIX/include/$${PACKAGENAME}
QMAKE_PKGCONFIG_LIBDIR = $$PREFIX/lib
QMAKE_PKGCONFIG_DESTDIR = pkgconfig
Expand Down

0 comments on commit 3e0f808

Please sign in to comment.