phonebookengines/cntlistmodel/inc/cntcache.h
changeset 81 640d30f4fb64
parent 77 c18f9fa7f42e
child 84 63017c97b1d6
equal deleted inserted replaced
77:c18f9fa7f42e 81:640d30f4fb64
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Class for asynchronously fetching and caching
       
    15 *              basic contact info for list views.
       
    16 *
       
    17 */
       
    18 
       
    19 #ifndef CNTCACHE_H
       
    20 #define CNTCACHE_H
       
    21 
       
    22 #include <QObject>
       
    23 #include <QSharedData>
       
    24 #include <HbIcon>
       
    25 #include <cntuids.h>
       
    26 #include <qcontactmanager.h>
       
    27 #include <cntinfoprovider.h>
       
    28 #include <collate.h>
       
    29 
       
    30 class CntContactInfoData;
       
    31 class CntNameFetcher;
       
    32 class CntCacheThread;
       
    33 class CntInfoCacheItem;
       
    34 class CntIconCacheItem;
       
    35 class CntNameCacheItem;
       
    36 
       
    37 QTM_USE_NAMESPACE
       
    38 
       
    39 /*
       
    40    Info about one contact that can be used by listviews:
       
    41    - the id
       
    42    - the full name, properly formatted
       
    43    - text, secondary information like phone number
       
    44    - icon1, the main icon
       
    45    - icon2, a secondary icon
       
    46  */
       
    47 class CntContactInfo : public QObject
       
    48 {
       
    49     Q_OBJECT
       
    50 public:
       
    51     CntContactInfo();
       
    52     CntContactInfo(int id, const QString& name, const QString& text, const HbIcon& icon1, const HbIcon& icon2);
       
    53     ~CntContactInfo();
       
    54 
       
    55     CntContactInfo(const CntContactInfo& other);
       
    56     CntContactInfo& operator=(const CntContactInfo& other);
       
    57 
       
    58     int id() const;
       
    59     QString name() const;
       
    60     QString text() const;
       
    61     HbIcon icon1() const;
       
    62     HbIcon icon2() const;
       
    63 
       
    64 private:
       
    65     QSharedDataPointer<CntContactInfoData> d;
       
    66 };
       
    67 
       
    68 /*
       
    69    Singleton class that acts as a proxy to get CntContactInfo objects for contacts.
       
    70    It also implements caching for faster access. This is why the fetchContactInfo()
       
    71    function takes a row number and the full list of contact IDs rather than just a
       
    72    contact ID -- the former allows caching ahead.
       
    73    
       
    74    The usage pattern for clients is to call fetchContactInfo() to get at least the
       
    75    name of the contact. If all the info is cached then it will be provided. If not,
       
    76    then the uncached info is fetched asynchronously and contactInfoUpdated signals
       
    77    are emitted as the pieces of information arrive -- up to three times per contact;
       
    78    once for text, once for icon1 and once for icon2.
       
    79  */
       
    80 class CntCache : public QObject
       
    81 {
       
    82     friend class TestCntCache;
       
    83     friend class TestCntListModel;
       
    84     Q_OBJECT
       
    85 public:
       
    86     static CntCache* instance(QContactManager *manager);
       
    87     CntContactInfo fetchContactInfo(int row, const QList<QContactLocalId>& idList);
       
    88     QList<QContactLocalId> sortIdsByName(const QSet<QContactLocalId>* idFilter = NULL) const;
       
    89     QList<QContactLocalId> sortIdsByName(const QStringList searchList) const;
       
    90 
       
    91 signals:
       
    92     void contactInfoUpdated(QContactLocalId contactId);
       
    93     void contactsChanged(const QList<QContactLocalId> &changedContacts);
       
    94     void contactsRemoved(const QList<QContactLocalId> &removedContacts);
       
    95     void contactsAdded(const QList<QContactLocalId> &addedContacts);
       
    96     void dataChanged();
       
    97 
       
    98 private:
       
    99     CntCache(QContactManager *manager);
       
   100     ~CntCache();
       
   101     void loadNames();
       
   102     bool contactExists(QContactLocalId contactId) const;
       
   103     QString contactName(QContactLocalId contactId) const;
       
   104     CntInfoCacheItem* createInfoCacheItem(int contactId);
       
   105     CntIconCacheItem* createIconCacheItem(const QString &iconName);
       
   106     void updateReadAheadCache(int mostRecentRow, const QList<QContactLocalId> &idList);
       
   107     void emitContactInfoUpdated(int contactId);
       
   108 
       
   109 private slots:
       
   110     void onShutdown();
       
   111     void reformatNames(CntNameOrder newFormat);
       
   112     void onNewInfo(int contactId, const ContactInfoField &infoField, const QString &infoValue);
       
   113     void onInfoCancelled(int contactId);
       
   114     void scheduleOneReadAheadItem();
       
   115     void onNewIcon(const QString &iconName, const HbIcon &icon);
       
   116     void onIconCancelled(const QString &iconName);
       
   117     void updateContacts(const QList<QContactLocalId> &changedContacts);
       
   118     void removeContacts(const QList<QContactLocalId> &removedContacts);
       
   119     void addContacts(const QList<QContactLocalId> &addedContacts);
       
   120     void setNameList(QList<CntNameCacheItem *> newSortedNames);
       
   121 
       
   122 private:
       
   123     static CntCache *mInstance;                   // the one and only instance of CntCache
       
   124     QContactManager *mContactManager;             // for getting notifications about changes to contacts
       
   125     CntCacheThread *mWorker;                      // the background thread that does the actual fetching
       
   126     CntNameFetcher *mNameFetcher;                 // the helper that fetches contact names
       
   127 
       
   128     QList<CntNameCacheItem *> mSortedNames;       // list of all contact names, in sorted order
       
   129     QHash<QContactLocalId, CntNameCacheItem *> mNameCache;    // cache with all contact names, indexed by contact ids
       
   130     QHash<int,CntInfoCacheItem *> mInfoCache;     // cache with contact info, indexed by contact ids
       
   131     QHash<QString,CntIconCacheItem *> mIconCache; // cache with icons, indexed by icon name
       
   132     QList< QPair<int, int> > mReadAheadCache;     // cache with contacts to prefetch (they are likely to be needed soon)
       
   133 
       
   134     int mNextInfoCacheOrder;                      // cache order for the next item to be updated/inserted in info cache
       
   135     int mNextIconCacheOrder;                      // cache order for the next item to be updated/inserted in icon cache
       
   136     int mEmittedContactId;                        // id of the last contact emitted to UI
       
   137     int mUrgentContacts;                          // the number of contacts left that need to be fetched asap
       
   138     
       
   139     bool mHasModifiedNames;                       // monitors whether any names have changed since file cache was last updated
       
   140     bool mAllNamesFetchStarted;                   // false until the asynch fetching of all names from the DB has started;
       
   141                                                   // this operation is done only once
       
   142 };
       
   143 
       
   144 #endif