phonebookengines/mobcntmodel/src/mobcnticonmanager.cpp
changeset 37 fd64c38c277d
parent 31 2a11b5b00470
child 40 b46a585f6909
equal deleted inserted replaced
31:2a11b5b00470 37:fd64c38c277d
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "mobcnticonmanager.h"
       
    19 #include <thumbnailmanager_qt.h>
       
    20 #include <QDebug>
       
    21 
       
    22 const int KMaxQueueSize = 25;
       
    23 const int KContactFetchTimer1 = 400;
       
    24 const int KContactFetchTimer2 = 20;
       
    25 /*!
       
    26  Constructor
       
    27  */
       
    28 MobCntIconManager::MobCntIconManager(QObject *parent)
       
    29     : QObject(parent),
       
    30       mQueueCount(0)
       
    31 {
       
    32     mThumbnailManager = new ThumbnailManager(this);
       
    33     mThumbnailManager->setMode(ThumbnailManager::Default);
       
    34     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForPerformance);
       
    35     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailSmall);
       
    36 
       
    37     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
    38              this, SLOT(thumbnailReady(QPixmap, void *, int, int)));
       
    39              
       
    40     mTimer = new QTimer();
       
    41     connect( mTimer, SIGNAL(timeout()), this, SLOT(timerTimeout()) );
       
    42                       
       
    43 }
       
    44 
       
    45 /*!
       
    46  Destructor
       
    47  */
       
    48 MobCntIconManager::~MobCntIconManager()
       
    49 {
       
    50     cancel();
       
    51     mImageCache.clear();
       
    52     delete mTimer;
       
    53 }
       
    54 
       
    55 /*!
       
    56 Get the icon for the requested avatarPath or send a new request to the thumbnailmanager if it doesn't exist yet
       
    57 */
       
    58 QIcon MobCntIconManager::contactIcon(const QString &avatarPath, int index)
       
    59 {
       
    60     QIcon icon;
       
    61     if ( mImageCache.contains(avatarPath) )
       
    62     {
       
    63         icon = mImageCache.value(avatarPath);
       
    64 		qDebug() << "MobCntIconManager::contactIcon:: image was cached";
       
    65     }
       
    66     else
       
    67     {      
       
    68         mTimer->stop();
       
    69         mTimer->start(KContactFetchTimer1); 
       
    70         mQueueCount++;
       
    71         mRequestQueue.enqueue(qMakePair(avatarPath, index));
       
    72         if( mQueueCount > KMaxQueueSize )
       
    73         {
       
    74            mRequestQueue.dequeue();
       
    75            mQueueCount--;
       
    76         }        
       
    77     }
       
    78     return icon;
       
    79 }
       
    80 
       
    81 /*!
       
    82 Cancel all requests
       
    83 */
       
    84 void MobCntIconManager::cancel()
       
    85 {
       
    86     if (!mTnmReqMap.empty())
       
    87     {
       
    88         QMapIterator<int, QString> iter(mTnmReqMap);
       
    89         while (iter.hasNext())
       
    90         {
       
    91             iter.next();
       
    92             bool result = mThumbnailManager->cancelRequest(iter.key());
       
    93         }
       
    94     }
       
    95     mTnmReqMap.clear();
       
    96     mRequestQueue.clear();
       
    97     mQueueCount = 0;
       
    98 }
       
    99 
       
   100 /*!
       
   101 Called when thumbnailmanager finishes creating a thumbnail, emits a signal for MobCntModel to update icon for a contact
       
   102 */
       
   103 void MobCntIconManager::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
       
   104 {
       
   105 	qDebug() << "MobCntIconManager::thumbnailReady:: error is " << error;
       
   106 	
       
   107     // Find the index
       
   108     if (mTnmReqMap.contains(id))
       
   109     {
       
   110         QString avatarPath = mTnmReqMap[id];
       
   111         mTnmReqMap.remove(id);
       
   112         if ( error == 0 )
       
   113         {
       
   114             int *clientData = (int *)data;
       
   115             int index = *clientData;
       
   116 			qDebug() << "MobCntIconManager::thumbnailReady:: data is " << index;
       
   117             QIcon icon(pixmap);
       
   118             mImageCache.insert(avatarPath, icon);
       
   119             emit contactIconReady(index);
       
   120 			qDebug() << "MobCntIconManager::thumbnailReady - signal emitted";
       
   121             if (!mRequestQueue.isEmpty())
       
   122             {
       
   123                 mTimer->start(KContactFetchTimer2);                
       
   124             }
       
   125     
       
   126             delete clientData;
       
   127             
       
   128         } else {
       
   129         	  thumbnailLoad();
       
   130         }
       
   131     }
       
   132 }
       
   133 
       
   134 void MobCntIconManager::thumbnailLoad()
       
   135 {
       
   136 	mTimer->stop();
       
   137     if (!mRequestQueue.isEmpty())
       
   138     { 
       
   139 		mQueueCount--;
       
   140         QPair<QString, int> req = mRequestQueue.dequeue();
       
   141         QString avatarPath = req.first;
       
   142         int index = req.second;
       
   143         int *clientData = new int(index);
       
   144         int reqId = mThumbnailManager->getThumbnail(avatarPath, clientData, 0);
       
   145         mTnmReqMap.insert(reqId, avatarPath);
       
   146     }
       
   147  
       
   148 }
       
   149 
       
   150 void MobCntIconManager::timerTimeout()
       
   151 {
       
   152     thumbnailLoad();	
       
   153 }
       
   154 
       
   155