logsui/logsengine/src/logsthumbnailmanager.cpp
changeset 0 4a5361db8937
equal deleted inserted replaced
-1:000000000000 0:4a5361db8937
       
     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 "logsthumbnailmanager.h"
       
    19 #include "logslogger.h"
       
    20 #include "logsengdefs.h"
       
    21 #include <thumbnailmanager_qt.h>
       
    22 #include <hbicon.h>
       
    23 
       
    24 const int KMaxQueueSize = 25;
       
    25 const int KContactFetchTimer1 = 400;
       
    26 const int KContactFetchTimer2 = 20;
       
    27 const int KLogsMaxCacheSize   = 50;
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 LogsThumbIconManager::LogsThumbIconManager(QObject *parent)
       
    34     : QObject(parent),
       
    35       mQueueCount(0),
       
    36       mDefaultIcon(0)
       
    37 {
       
    38     mThumbnailManager = new ThumbnailManager(this);
       
    39     mThumbnailManager->setMode(ThumbnailManager::Default);
       
    40     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForPerformance);
       
    41     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailSmall);
       
    42 
       
    43     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
    44              this, SLOT(thumbnailReady(QPixmap, void *, int, int)));
       
    45              
       
    46     mTimer = new QTimer();
       
    47     connect( mTimer, SIGNAL(timeout()), this, SLOT(timerTimeout()) );
       
    48     mDefaultIcon = new HbIcon(logsThumbUnknownId);
       
    49     mImageCache.setMaxCost(KLogsMaxCacheSize);
       
    50 }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 LogsThumbIconManager::~LogsThumbIconManager()
       
    57 {
       
    58     cancel();
       
    59     mImageCache.clear();
       
    60     delete mTimer;
       
    61     delete mDefaultIcon;
       
    62 }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // Get the icon for the requested avatarPath or send a new request
       
    66 // to the thumbnailmanager if it doesn't exist yet, default icon is returned
       
    67 // if cached icon does not exist
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 QIcon& LogsThumbIconManager::contactIcon(const QString &avatarPath, int index)
       
    71 {
       
    72     LOGS_QDEBUG( "logs [ENG] -> LogsThumbIconManager::contactIcon()" )
       
    73     QIcon* icon = 0;
       
    74     if ( avatarPath.isEmpty() ){
       
    75         icon = &defaultIcon();
       
    76     } else if ( mImageCache.contains(avatarPath) ) {
       
    77         icon = mImageCache.object(avatarPath);
       
    78         LOGS_QDEBUG( "logs [ENG] -> LogsThumbIconManager::contactIcon() image was cached" )
       
    79     }
       
    80     else
       
    81     {
       
    82         icon = &defaultIcon();
       
    83         mTimer->stop();
       
    84         mTimer->start(KContactFetchTimer1); 
       
    85         mQueueCount++;
       
    86         mRequestQueue.enqueue(qMakePair(avatarPath, index));
       
    87         if( mQueueCount > KMaxQueueSize ){
       
    88             LOGS_QDEBUG( "logs [ENG] -> LogsThumbIconManager::contactIcon() image was not cached" )
       
    89             mRequestQueue.dequeue();
       
    90             mQueueCount--;
       
    91         }        
       
    92     }
       
    93     return *icon;
       
    94 }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 QIcon& LogsThumbIconManager::defaultIcon()
       
   101 {
       
   102     return mDefaultIcon->qicon();
       
   103 }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // Cancel all requests
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void LogsThumbIconManager::cancel()
       
   110 {
       
   111     LOGS_QDEBUG( "logs [ENG] -> LogsThumbIconManager::cancel()" )
       
   112     if (!mTnmReqMap.empty()){
       
   113         QMapIterator<int, QString> iter(mTnmReqMap);
       
   114         while (iter.hasNext()){
       
   115             iter.next();
       
   116             bool result = mThumbnailManager->cancelRequest(iter.key());
       
   117         }
       
   118     }
       
   119     mTnmReqMap.clear();
       
   120     mRequestQueue.clear();
       
   121     mQueueCount = 0;
       
   122     mImageCache.clear();
       
   123 }
       
   124 
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // Called when thumbnailmanager finishes creating a thumbnail,
       
   128 // emits a signal for LogsMatchesModelModel to update icon for a contact
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 void LogsThumbIconManager::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
       
   132 {
       
   133 	LOGS_QDEBUG_2("LogsThumbIconManager::thumbnailReady:: error:", error );
       
   134     // Find the index
       
   135     if (mTnmReqMap.contains(id)){
       
   136         QString avatarPath = mTnmReqMap[id];
       
   137         LOGS_QDEBUG_3( "LogsThumbIconManager::thumbnailReady (avatarPath, error):", avatarPath, error );
       
   138         mTnmReqMap.remove(id);
       
   139         if ( error == 0 ){
       
   140             int *clientData = (int *)data;
       
   141             int index = *clientData;
       
   142             QIcon* icon = new QIcon(pixmap);
       
   143             LOGS_QDEBUG_3( "LogsThumbIconManager::thumbnailReady (avatarPath, mImageCache.count()):", avatarPath, mImageCache.count() );
       
   144             mImageCache.insert(avatarPath, icon);
       
   145             LOGS_QDEBUG_3( "LogsThumbIconManager::thumbnailReady (avatarPath, mImageCache.count()):", avatarPath, mImageCache.count() );
       
   146             emit contactIconReady(index);
       
   147             LOGS_QDEBUG("LogsThumbIconManager::thumbnailReady - signal emitted");
       
   148             if (!mRequestQueue.isEmpty()){
       
   149                 mTimer->start(KContactFetchTimer2);                
       
   150             }
       
   151             delete clientData;
       
   152         } else {
       
   153         	  thumbnailLoad();
       
   154         }
       
   155     }
       
   156 }
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void LogsThumbIconManager::thumbnailLoad()
       
   162 {
       
   163     LOGS_QDEBUG( "logs [ENG] -> LogsThumbIconManager::thumbnailLoad()->" )
       
   164 	mTimer->stop();
       
   165     if (!mRequestQueue.isEmpty()){
       
   166 		mQueueCount--;
       
   167         QPair<QString, int> req = mRequestQueue.dequeue();
       
   168         QString avatarPath = req.first;
       
   169         int index = req.second;
       
   170         int *clientData = new int(index);
       
   171         LOGS_QDEBUG_2("LogsThumbIconManager::thumbnailLoad clientData is ", clientData );
       
   172         int reqId = mThumbnailManager->getThumbnail(avatarPath, clientData, 0);
       
   173         LOGS_QDEBUG_2("LogsThumbIconManager::thumbnailLoad reqId is ", reqId );
       
   174         mTnmReqMap.insert(reqId, avatarPath);
       
   175     }
       
   176     LOGS_QDEBUG( "logs [ENG] -> LogsThumbIconManager::thumbnailLoad()<-" )
       
   177 }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void LogsThumbIconManager::timerTimeout()
       
   184 {
       
   185     thumbnailLoad();
       
   186  	
       
   187 }
       
   188 
       
   189