From 73229100ce9e35071da128074cc73141700024d5 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 5 Aug 2013 13:16:49 -0500 Subject: [PATCH] [libcontacts] Add API for name grouping. Allow a customized name grouper to be set. --- src/seasidecache.cpp | 25 +++++++++++++++++ src/seasidecache.h | 8 ++++-- src/seasidenamegrouper.h | 60 ++++++++++++++++++++++++++++++++++++++++ src/src.pro | 6 ++-- 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/seasidenamegrouper.h diff --git a/src/seasidecache.cpp b/src/seasidecache.cpp index 2ba4b72..048353e 100644 --- a/src/seasidecache.cpp +++ b/src/seasidecache.cpp @@ -521,11 +521,36 @@ void SeasideCache::unregisterResolveListener(ResolveListener *listener) } } +void SeasideCache::setNameGrouper(SeasideNameGrouper *grouper) +{ + if (!instancePtr) + new SeasideCache; + instancePtr->m_nameGrouper.reset(grouper); + + allContactNameGroups = getAllContactNameGroups(); + QList groups = instancePtr->m_nameGrouper->allNameGroups(); + for (int i = groups.count() - 1; i > -1; --i) { + const QChar &group = groups.at(i); + if (!allContactNameGroups.contains(group)) + allContactNameGroups.prepend(group); + } +} + QChar SeasideCache::nameGroupForCacheItem(CacheItem *cacheItem) { if (!cacheItem) return QChar(); + if (!instancePtr) + new SeasideCache; + + if (!instancePtr->m_nameGrouper.isNull()) { + SeasideNameGrouper::DisplayLabelOrder order = (SeasideNameGrouper::DisplayLabelOrder)(int)SeasideCache::displayLabelOrder(); + QChar group = instancePtr->m_nameGrouper->nameGroupForContact(cacheItem->contact, order); + if (!group.isNull()) + return group; + } + QChar group; QString first; QString last; diff --git a/src/seasidecache.h b/src/seasidecache.h index 55aef3c..1228ebb 100644 --- a/src/seasidecache.h +++ b/src/seasidecache.h @@ -33,6 +33,7 @@ #define SEASIDECACHE_H #include "contactcacheexport.h" +#include "seasidenamegrouper.h" #include #include @@ -106,8 +107,8 @@ class CONTACTCACHE_EXPORT SeasideCache : public QObject }; enum DisplayLabelOrder { - FirstNameFirst, - LastNameFirst + FirstNameFirst = SeasideNameGrouper::FirstNameFirst, + LastNameFirst = SeasideNameGrouper::LastNameFirst }; enum ContactState { @@ -229,6 +230,8 @@ class CONTACTCACHE_EXPORT SeasideCache : public QObject static void unregisterResolveListener(ResolveListener *listener); + static void setNameGrouper(SeasideNameGrouper *grouper); + static DisplayLabelOrder displayLabelOrder(); static int contactId(const QContact &contact); @@ -371,6 +374,7 @@ private slots: QList > m_contactPairsToLink; QList m_relationshipsToSave; QList m_relationshipsToRemove; + QScopedPointer m_nameGrouper; QList m_nameGroupChangeListeners; QList m_changeListeners; QVector m_contacts[FilterTypesCount]; diff --git a/src/seasidenamegrouper.h b/src/seasidenamegrouper.h new file mode 100644 index 0000000..174422b --- /dev/null +++ b/src/seasidenamegrouper.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 Jolla Mobile + * + * You may use this file under the terms of the BSD license as follows: + * + * "Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Nemo Mobile nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." + */ + +#ifndef SEASIDENAMEGROUPER_H +#define SEASIDENAMEGROUPER_H + +#include +#include +#include + +#ifdef USING_QTPIM +QTCONTACTS_USE_NAMESPACE +#else +QTM_USE_NAMESPACE +#endif + +class CONTACTCACHE_EXPORT SeasideNameGrouper +{ +public: + SeasideNameGrouper() {} + virtual ~SeasideNameGrouper() {} + + enum DisplayLabelOrder { + FirstNameFirst, + LastNameFirst + }; + + virtual QChar nameGroupForContact(const QContact &contact, DisplayLabelOrder order) const = 0; + virtual QList allNameGroups() const = 0; +}; + +#endif // SEASIDENAMEGROUPER_H diff --git a/src/src.pro b/src/src.pro index ab79a87..d913985 100644 --- a/src/src.pro +++ b/src/src.pro @@ -43,11 +43,13 @@ HEADERS += \ $$PWD/normalization_p.h \ $$PWD/contactcacheexport.h \ $$PWD/seasidecache.h \ - $$PWD/synchronizelists.h + $$PWD/synchronizelists.h \ + $$PWD/seasidenamegrouper.h headers.files = \ $$PWD/contactcacheexport.h \ $$PWD/seasidecache.h \ - $$PWD/synchronizelists.h + $$PWD/synchronizelists.h \ + $$PWD/seasidenamegrouper.h headers.path = $$PREFIX/include/$$TARGET INSTALLS += headers