mpviewplugins/mpcollectionviewplugin/src/mpcollectionalbumartmanager.cpp
changeset 19 4e84c994a771
child 20 82baf59ce8dd
equal deleted inserted replaced
5:2a40e88564c8 19:4e84c994a771
       
     1 
       
     2 
       
     3 
       
     4 /*
       
     5 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     6 * All rights reserved.
       
     7 * This component and the accompanying materials are made available
       
     8 * under the terms of "Eclipse Public License v1.0"
       
     9 * which accompanies this distribution, and is available
       
    10 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    11 *
       
    12 * Initial Contributors:
       
    13 * Nokia Corporation - initial contribution.
       
    14 *
       
    15 * Contributors:
       
    16 *
       
    17 * Description: Music Player collection album art manager.
       
    18 *
       
    19 */
       
    20 
       
    21 #include <thumbnailmanager_qt.h>
       
    22 
       
    23 #include "mpcollectionalbumartmanager.h"
       
    24 #include "mptrace.h"
       
    25 
       
    26 const int KMaxThumbnailReq = 5;
       
    27 
       
    28 /*!
       
    29     \class MpCollectionAlbumArtManager
       
    30     \brief Music Player collection album art manager.
       
    31 
       
    32     Collection album art manager provides access to album art needed for
       
    33     display in certain collection views. It hides interface to the thumbnail
       
    34     manager and also implements a caching mechanism for performance reasons.
       
    35 */
       
    36 
       
    37 /*!
       
    38     \fn void albumArtReady( int index )
       
    39 
       
    40     This signal is emitted when album art for \a index is ready.
       
    41 
       
    42     \sa getAlbumArt()
       
    43  */
       
    44 
       
    45 /*!
       
    46     \fn void albumCacheReady()
       
    47 
       
    48     This signal is emitted when album art cache is ready.
       
    49 
       
    50     \sa cacheAlbumArt()
       
    51  */
       
    52 
       
    53 /*!
       
    54  Constructs the album art manager.
       
    55  */
       
    56 MpCollectionAlbumArtManager::MpCollectionAlbumArtManager( QObject *parent )
       
    57     : QObject(parent),
       
    58       mCachingInProgress(false),
       
    59       mRequestCount(0)
       
    60 {
       
    61     TX_ENTRY
       
    62     mThumbnailManager = new ThumbnailManager(this);
       
    63     mThumbnailManager->setMode(ThumbnailManager::Default);
       
    64     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
       
    65     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailSmall);
       
    66     
       
    67     connect( mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
    68              this, SLOT(thumbnailReady(QPixmap, void *, int, int)) );
       
    69     TX_EXIT
       
    70 }
       
    71 
       
    72 /*!
       
    73  Destructs the album art manager.
       
    74  */
       
    75 MpCollectionAlbumArtManager::~MpCollectionAlbumArtManager()
       
    76 {
       
    77     TX_ENTRY
       
    78     cancel();
       
    79     mImageCache.clear();
       
    80     TX_EXIT
       
    81 }
       
    82 
       
    83 /*!
       
    84  Returns the album art for the given \a albumArtUri. If the album art is not
       
    85  available in its cache, an asynchronous request is made to the thumbnail manager
       
    86  and a null icon is returned.
       
    87 
       
    88  \sa signal albumArtReady
       
    89  */
       
    90 HbIcon MpCollectionAlbumArtManager::albumArt( const QString& albumArtUri, int index )
       
    91 {
       
    92     TX_ENTRY_ARGS("albumArtUri=" << albumArtUri << ", index=" << index);
       
    93     HbIcon icon;
       
    94     if ( mImageCache.contains(albumArtUri) ) {
       
    95         icon = mImageCache.value(albumArtUri);
       
    96         TX_EXIT_ARGS("true - album art returned");
       
    97     }
       
    98     else {
       
    99         if ( mRequestCount < KMaxThumbnailReq ) {
       
   100             // Using negative index as priority will ensure that thumbnail requests
       
   101             // are processed in the order they were requested.
       
   102             int *clientData = new int(index);
       
   103             int reqId = mThumbnailManager->getThumbnail( albumArtUri, clientData, -index );
       
   104             if ( reqId != -1 ) {
       
   105                 mTnmReqMap.insert( reqId, albumArtUri );
       
   106                 mRequestCount++;
       
   107                 TX_EXIT_ARGS("false - album art requested");
       
   108             }
       
   109             else {
       
   110                 TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   111             }
       
   112         }
       
   113         else {
       
   114             mRequestQueue.enqueue( qMakePair(albumArtUri, index) );
       
   115             TX_EXIT_ARGS("false - request queued");
       
   116         }
       
   117     }
       
   118     return icon;
       
   119 }
       
   120 
       
   121 /*!
       
   122  Request to cache the album art for the items specified in \a albumArtList.
       
   123  Returns 'true' if caching is started. If all items already exist in cache,
       
   124  'false' is returned.
       
   125 
       
   126  \sa signal albumCacheReady
       
   127  */
       
   128 bool MpCollectionAlbumArtManager::cacheAlbumArt( const QStringList albumArtList )
       
   129 {
       
   130     TX_ENTRY
       
   131     int allAvailable = true;
       
   132     if ( !albumArtList.empty() ) {
       
   133         QString albumArtUri;
       
   134         int reqId;
       
   135         QStringListIterator iter(albumArtList);
       
   136         while ( iter.hasNext() ) {
       
   137             albumArtUri = iter.next();
       
   138             if ( !mImageCache.contains(albumArtUri) ) {
       
   139                 reqId = mThumbnailManager->getThumbnail( albumArtUri );
       
   140                 if ( reqId != -1 ) {
       
   141                     mTnmReqMap.insert( reqId, albumArtUri );
       
   142                     mRequestCount++;
       
   143                     allAvailable = false;
       
   144                 }
       
   145                 else {
       
   146                     TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   147                 }
       
   148                 TX_LOG_ARGS(albumArtUri);
       
   149             }
       
   150         }
       
   151     }
       
   152 
       
   153     if ( allAvailable ) {
       
   154         TX_EXIT_ARGS("Caching is done!");
       
   155         return false;
       
   156     }
       
   157     else {
       
   158         TX_EXIT_ARGS("Caching is in progress!");
       
   159         mCachingInProgress = true;
       
   160         return true;
       
   161     }
       
   162 }
       
   163 
       
   164 /*!
       
   165  Cancels all outstanding album art requests.
       
   166 
       
   167  \sa getAlbumArt, cacheAlbumArt
       
   168  */
       
   169 void MpCollectionAlbumArtManager::cancel()
       
   170 {
       
   171     TX_ENTRY
       
   172     if ( !mTnmReqMap.empty() ) {
       
   173         QMapIterator<int, QString> iter(mTnmReqMap);
       
   174         while ( iter.hasNext() ) {
       
   175             iter.next();
       
   176             bool result = mThumbnailManager->cancelRequest(iter.key());
       
   177         }
       
   178     }
       
   179     mTnmReqMap.clear();
       
   180     mRequestQueue.clear();
       
   181     mRequestCount = 0;
       
   182     mCachingInProgress = false;
       
   183     TX_EXIT
       
   184 }
       
   185 
       
   186 
       
   187 /*!
       
   188  Slot to be called when thumbnail bitmap generation or loading is complete.
       
   189  */
       
   190 void MpCollectionAlbumArtManager::thumbnailReady( const QPixmap& pixmap, void *data, int id, int error )
       
   191 {
       
   192     TX_ENTRY_ARGS("id=" << id << ", error=" << error);
       
   193 
       
   194     // Find the index
       
   195     if ( mTnmReqMap.contains(id) ) {
       
   196         // Remove the request whether it completed successfully or with error.
       
   197         QString albumArtUri = mTnmReqMap[id];
       
   198         mTnmReqMap.remove( id );
       
   199         mRequestCount--;
       
   200 
       
   201         if ( mCachingInProgress ) {
       
   202             if ( error == 0 ) {
       
   203                 QIcon qicon(pixmap);
       
   204                 HbIcon icon(qicon);
       
   205                 mImageCache.insert(albumArtUri, icon);
       
   206             }
       
   207             else {
       
   208                 TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   209             }
       
   210             if ( mTnmReqMap.empty() ) {
       
   211                 TX_LOG_ARGS("Album art cache ready!");
       
   212                 mCachingInProgress = false;
       
   213                 emit albumCacheReady();
       
   214                 return;
       
   215             }
       
   216         }
       
   217         else {
       
   218             if ( error == 0 ) {
       
   219                 int *clientData = (int *)data;
       
   220                 int index = *clientData;
       
   221                 delete clientData;
       
   222 
       
   223                 QIcon qicon(pixmap);
       
   224                 HbIcon icon(qicon);
       
   225                 mImageCache.insert(albumArtUri, icon);
       
   226                 TX_LOG_ARGS("Album art ready for index=" << index);
       
   227                 emit albumArtReady(index);
       
   228             }
       
   229             else {
       
   230                 TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   231             }
       
   232         }
       
   233     }
       
   234 
       
   235     // Check to see if any request is pending in the queue
       
   236     while ( !mRequestQueue.isEmpty()
       
   237             && (mRequestCount < KMaxThumbnailReq) ) {
       
   238         QPair<QString, int> req = mRequestQueue.dequeue();
       
   239         QString albumArtUri = req.first;
       
   240         int index = req.second;
       
   241 
       
   242         // Using negative index as priority will ensure that thumbnail requests
       
   243         // are processed in the order they were requested.
       
   244         int *clientData = new int(index);
       
   245         int reqId = mThumbnailManager->getThumbnail( albumArtUri, clientData, -index );
       
   246         if ( reqId != -1 ) {
       
   247             mTnmReqMap.insert( reqId, albumArtUri );
       
   248             mRequestCount++;
       
   249         }
       
   250         else {
       
   251             TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   252         }
       
   253     }
       
   254     TX_EXIT
       
   255 }
       
   256