contentstorage/caclient/src/caiconcache.cpp
changeset 87 9d806967057c
child 73 4bc7b118b3df
equal deleted inserted replaced
86:e492551a0d54 87:9d806967057c
       
     1 /*
       
     2  * Copyright (c)2008 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:  ?Description
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QCoreApplication>
       
    19 #include "caiconcache.h"
       
    20 #include "caservice.h"
       
    21 #include "caentry.h"
       
    22 #include "caobjectadapter.h"
       
    23 #include "caclienttest_global.h"
       
    24 
       
    25 // Constants
       
    26 static const int maxCost = 250;
       
    27 static const QChar separator = '*';
       
    28 
       
    29 QSharedPointer<CaIconCache> CaIconCache::mInstance(0);
       
    30 
       
    31 /*!
       
    32  *   \class CaIconCache
       
    33  *
       
    34  *   \brief This class provides icon caching mechanism
       
    35  *
       
    36  *   Class implements singleton design patern. Use cache() method
       
    37  *   to get an instance of CaIconCache
       
    38  *
       
    39  */
       
    40 
       
    41 /*!
       
    42  Returns an instance of CaIconCache
       
    43  \retval CaIconCache instance
       
    44 
       
    45  */
       
    46 CaIconCache *CaIconCache::cache()
       
    47 {
       
    48     if (!mInstance.data()) {
       
    49         mInstance = QSharedPointer<CaIconCache>(new CaIconCache());
       
    50         mInstance->setParent(QCoreApplication::instance());
       
    51     }
       
    52     return mInstance.data();
       
    53 }
       
    54 
       
    55 /*!
       
    56 Destructor
       
    57  */
       
    58 CaIconCache::~CaIconCache()
       
    59 {
       
    60     CACLIENTTEST_FUNC_ENTRY("CaIconCache::~CaIconCache");
       
    61     mCache.clear();
       
    62     delete mNotifier;
       
    63     CACLIENTTEST_FUNC_EXIT("CaIconCache::~CaIconCache");
       
    64 }
       
    65 
       
    66 /*!
       
    67 Constructor
       
    68  */
       
    69 CaIconCache::CaIconCache(QObject *parent): QObject(parent),
       
    70     mCache(), mService(CaService::instance()), mNotifier(0)
       
    71 {
       
    72     mCache.setMaxCost(maxCost);
       
    73     CaNotifierFilter filter;
       
    74     mNotifier = mService->createNotifier(filter);
       
    75     connect(mNotifier, SIGNAL(entryChanged(const CaEntry &,ChangeType)),
       
    76             this, SLOT(remove(const CaEntry &,ChangeType)));
       
    77 
       
    78 }
       
    79 
       
    80 /*!
       
    81  Checks if icon is already cached
       
    82  \param entry an entry
       
    83  \param size size of an icon
       
    84  \retval true if icon exist in cache
       
    85 
       
    86  */
       
    87 bool CaIconCache::exist(const CaEntry &entry, const QSize &size)
       
    88 {
       
    89     CACLIENTTEST_FUNC_ENTRY("CaIconCache::exist");
       
    90     bool result(false);
       
    91     result = mCache.contains(key(entry,size));
       
    92     CACLIENTTEST_FUNC_EXIT("CaIconCache::exist");
       
    93     return result;
       
    94 }
       
    95 
       
    96 /*!
       
    97  Returns an icon from a cache
       
    98  \param entry an entry
       
    99  \param size size of an icon
       
   100  \retval icon
       
   101 
       
   102  */
       
   103 
       
   104 HbIcon CaIconCache::icon(const CaEntry &entry, const QSize &size)
       
   105 {
       
   106     CACLIENTTEST_FUNC_ENTRY("CaIconCache::icon");
       
   107     HbIcon icon;
       
   108     icon = *mCache.object(key(entry,size));
       
   109     CACLIENTTEST_FUNC_EXIT("CaIconCache::icon");
       
   110     return icon;
       
   111 }
       
   112 
       
   113 /*!
       
   114  Insert an icon to a cache
       
   115  \param entry an entry
       
   116  \param size size of an icon
       
   117  \param icon icon to be cached
       
   118 
       
   119  */
       
   120 void CaIconCache::insert(const CaEntry &entry, const QSize &size,
       
   121                          const HbIcon &icon)
       
   122 {
       
   123     CACLIENTTEST_FUNC_ENTRY("CaIconCache::insert");
       
   124     mCache.insert(key(entry,size),new HbIcon(icon));
       
   125     CACLIENTTEST_FUNC_EXIT("CaIconCache::insert");
       
   126 }
       
   127 
       
   128 /*!
       
   129  Removes icon from a cache
       
   130  \param entry an entry
       
   131  \param changeTypa indicates if entry was updated, removed or added
       
   132  */
       
   133 void CaIconCache::remove(const CaEntry &entry, ChangeType changeType)
       
   134 {
       
   135     CACLIENTTEST_FUNC_ENTRY("CaIconCache::remove");
       
   136     if (changeType != AddChangeType) {
       
   137         QString entryKey = key(entry);
       
   138         entryKey.append(separator);
       
   139         QList<QString> keys = mCache.keys();
       
   140         foreach(QString cacheKey,keys) {
       
   141             if (cacheKey.contains(entryKey)) {
       
   142                 mCache.remove(cacheKey);
       
   143             }
       
   144         }
       
   145     }
       
   146     CACLIENTTEST_FUNC_EXIT("CaIconCache::remove");
       
   147 }
       
   148 
       
   149 /*!
       
   150  Generates a key
       
   151  \param entry an entry
       
   152  \return key
       
   153  */
       
   154 
       
   155 QString CaIconCache::key(const CaEntry &entry, const QSize &size)
       
   156 {
       
   157     QString key;
       
   158     if (!entry.iconDescription().filename().isEmpty()) {
       
   159         key.append(entry.iconDescription().filename());
       
   160     } else {
       
   161         key.append(separator);
       
   162         key.append(entry.id());
       
   163     }
       
   164     if (size.isValid()) {
       
   165         key.append(separator);
       
   166         key.append(size.height());
       
   167         key.append(separator);
       
   168         key.append(size.width());
       
   169     }
       
   170     return key;
       
   171 }