phonebookui/cntlistmodel/cnticonfetcher.cpp
changeset 81 640d30f4fb64
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: Fetches icons for contacts.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <thumbnailmanager_qt.h>
       
    19 #include <cnticonfetcher.h>
       
    20 #include <cntdebug.h>
       
    21 
       
    22 // maximum amount of scheduled jobs; if there are more jobs, the least
       
    23 // important job is cancelled
       
    24 const int MaxIconJobs = 20;
       
    25 
       
    26 // the id that states that no icon is currently pending from thumbnail manager
       
    27 const int NoIconRequest = -1;
       
    28 
       
    29 /*!
       
    30     \class CntIconJob
       
    31     \brief Holds info about one icon job.
       
    32  */
       
    33 
       
    34 /*!
       
    35     \class CntIconFetcher
       
    36     \brief CntIconFetcher asynchronously fetches contact icons.
       
    37 
       
    38     CntIconFetcher queues requests for contact icons that are to be cached.
       
    39     It fetches the icons later, when asked by the client to do so.
       
    40 
       
    41     Internally CntIconFetcher uses thumbnail manager to fetch the icons.
       
    42  */
       
    43 
       
    44 /*!
       
    45     Creates a CntIconFetcher object.
       
    46  */
       
    47 CntIconFetcher::CntIconFetcher()
       
    48     : CntAbstractFetcher(MaxIconJobs),
       
    49       mThumbnailManager(NULL),
       
    50       mIconRequestId(NoIconRequest)
       
    51 {
       
    52     CNT_ENTRY
       
    53 
       
    54     // create & connect the thumbnail manager
       
    55     mThumbnailManager = new ThumbnailManager(this);
       
    56     mThumbnailManager->setMode(ThumbnailManager::Default);
       
    57     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForPerformance);
       
    58     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailSmall);
       
    59     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
    60             this, SLOT(forwardIconToClient(QPixmap, void *, int, int)));
       
    61 
       
    62     CNT_EXIT
       
    63 }
       
    64 
       
    65 /*!
       
    66     Cleans up and destructs the CntIconFetcher object.
       
    67  */
       
    68 CntIconFetcher::~CntIconFetcher()
       
    69 {
       
    70     CNT_ENTRY
       
    71 
       
    72     if (mIconRequestId != NoIconRequest) {
       
    73         mThumbnailManager->cancelRequest(mIconRequestId);
       
    74     }
       
    75 
       
    76     delete mThumbnailManager;
       
    77     mThumbnailManager = NULL;
       
    78 
       
    79     CNT_EXIT
       
    80 }
       
    81 
       
    82 /*!
       
    83     Processes the next scheduled job. This function must not be
       
    84     called until the previous job has completed.
       
    85  */
       
    86 void CntIconFetcher::processNextJob()
       
    87 {
       
    88     CNT_ENTRY
       
    89 
       
    90     Q_ASSERT(mIconRequestId == NoIconRequest);
       
    91 
       
    92     if (hasScheduledJobs()) {
       
    93         // request icon from thumbnail manager
       
    94         CntIconJob *job = static_cast<CntIconJob *>(takeNextJob());
       
    95         mIconRequestName = job->iconName;
       
    96         mIconRequestId = mThumbnailManager->getThumbnail(mIconRequestName, NULL, 0);
       
    97         delete job;
       
    98     } else if (hasCancelledJobs()) {
       
    99         CntIconJob *job = static_cast<CntIconJob *>(takeNextCancelledJob());
       
   100         emit iconCancelled(job->iconName);
       
   101         delete job;
       
   102     }
       
   103 
       
   104     CNT_EXIT
       
   105 }
       
   106 
       
   107 /*!
       
   108     \return true if a job is currently being processed; otherwise returns false.
       
   109  */
       
   110 bool CntIconFetcher::isProcessingJob()
       
   111 {
       
   112     return (mIconRequestId != NoIconRequest);
       
   113 }
       
   114 
       
   115 /*!
       
   116     Forwards an icon from thumbnail manager to the client.
       
   117  */
       
   118 void CntIconFetcher::forwardIconToClient(const QPixmap &pixmap, void *data, int id, int error)
       
   119 {
       
   120     CNT_ENTRY
       
   121 
       
   122     Q_UNUSED(data);
       
   123 
       
   124     if (id != mIconRequestId) {
       
   125         // this pixmap was requested by someone else sharing the same thumbnail manager instance
       
   126         CNT_EXIT_ARGS("not our pixmap")
       
   127         return;
       
   128     }
       
   129 
       
   130     mIconRequestId = NoIconRequest;
       
   131 
       
   132     if (error == 0) {
       
   133         emit iconFetched(mIconRequestName, HbIcon(pixmap));
       
   134     } else {
       
   135         emit iconFetched(mIconRequestName, HbIcon());
       
   136     }
       
   137 
       
   138     CNT_EXIT
       
   139 }