mpdata/src/mpcollectionalbumartmanager.cpp
changeset 22 ecf06a08d4d9
child 29 8192e5b5c935
equal deleted inserted replaced
20:82baf59ce8dd 22:ecf06a08d4d9
       
     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 #include <hbicon.h>
       
    23 
       
    24 #include "mpcollectionalbumartmanager.h"
       
    25 #include "mpmpxcollectiondata.h"
       
    26 #include "mptrace.h"
       
    27 
       
    28 const int KInitCacheSize = 10;
       
    29 const int KMaxCacheSize = 50;
       
    30 
       
    31 /*!
       
    32     \class MpCollectionAlbumArtManager
       
    33     \brief Music Player collection album art manager.
       
    34 
       
    35     Collection album art manager provides access to album art needed for
       
    36     display in certain collection views. It hides interface to the thumbnail
       
    37     manager and also implements a caching mechanism for performance reasons.
       
    38 */
       
    39 
       
    40 /*!
       
    41     \fn void albumArtReady( int index )
       
    42 
       
    43     This signal is emitted when album art for \a index is ready.
       
    44 
       
    45     \sa getAlbumArt()
       
    46  */
       
    47 
       
    48 /*!
       
    49     \fn void albumCacheReady()
       
    50 
       
    51     This signal is emitted when album art cache is ready.
       
    52 
       
    53     \sa cacheAlbumArt()
       
    54  */
       
    55 
       
    56 /*!
       
    57  Constructs the album art manager.
       
    58  */
       
    59 MpCollectionAlbumArtManager::MpCollectionAlbumArtManager( MpMpxCollectionData *data, QObject *parent )
       
    60     : QObject(parent),
       
    61       mCollectionData(data),
       
    62       mThumbnailManager(0),
       
    63       mCachingInProgress(false),
       
    64       mDefaultIcon(0),
       
    65       mPendingRequest(false)
       
    66 {
       
    67     TX_ENTRY
       
    68     mThumbnailManager = new ThumbnailManager(this);
       
    69     mThumbnailManager->setMode(ThumbnailManager::Default);
       
    70     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
       
    71     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailSmall);
       
    72 
       
    73     connect( mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
    74              this, SLOT(thumbnailReady(QPixmap, void *, int, int)) );
       
    75 
       
    76     mImageCache.setMaxCost(KMaxCacheSize);
       
    77     //TODO: Change to final resource when available
       
    78     HbIcon albumArt( "qtg_large_music" );
       
    79     mDefaultIcon = new QIcon( albumArt.qicon() );
       
    80     TX_EXIT
       
    81 }
       
    82 
       
    83 /*!
       
    84  Destructs the album art manager.
       
    85  */
       
    86 MpCollectionAlbumArtManager::~MpCollectionAlbumArtManager()
       
    87 {
       
    88     TX_ENTRY
       
    89     cancel();
       
    90     delete mThumbnailManager;
       
    91     delete mDefaultIcon;
       
    92     TX_EXIT
       
    93 }
       
    94 
       
    95 /*!
       
    96  Returns the album art for the given \a index. If the album art is not
       
    97  available in its cache, an asynchronous request is made to the thumbnail manager
       
    98  and default icon is returned.
       
    99 
       
   100  \sa signal albumArtReady
       
   101  */
       
   102 const QIcon* MpCollectionAlbumArtManager::albumArt( int index )
       
   103 {
       
   104     TX_ENTRY_ARGS("index=" << index);
       
   105     QIcon *icon = mImageCache[index];
       
   106     if ( !icon ) {
       
   107         icon = mDefaultIcon;
       
   108         if ( !mRequestQueue.contains(index) ) {
       
   109             // Icon was not found in cache. If the item has AlbumArtUri, request it
       
   110             // through ThumbnailManager interface.
       
   111             QString albumArtUri = mCollectionData->itemData(index, MpMpxCollectionData::AlbumArtUri);
       
   112             if ( !albumArtUri.isEmpty() ) {
       
   113                 if ( !mPendingRequest ) {
       
   114                     void *clientData = reinterpret_cast<void *>(index);
       
   115                     mRequestId = mThumbnailManager->getThumbnail( albumArtUri, clientData );
       
   116                     if ( mRequestId != -1 ) {
       
   117                         mPendingRequest = true;
       
   118                         TX_EXIT_ARGS("false - album art requested");
       
   119                     }
       
   120                     else {
       
   121                         TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   122                     }
       
   123                 }
       
   124                 else {
       
   125                     mRequestQueue.append( index );
       
   126                     TX_EXIT_ARGS("false - request queued");
       
   127                 }
       
   128             }
       
   129         }
       
   130     }
       
   131     return icon;
       
   132 }
       
   133 
       
   134 /*!
       
   135  Before providing the new data to the view (list, grid, etc.), we want
       
   136  to make sure that we have enough album arts for the first screen.
       
   137  */
       
   138 void MpCollectionAlbumArtManager::cacheFirstScreen()
       
   139 {
       
   140     TX_ENTRY
       
   141     int count = mCollectionData->count();
       
   142     int initCount = ( count > KInitCacheSize ) ? KInitCacheSize : count;
       
   143     for ( int i = 0; i < initCount; i++ ) {
       
   144         albumArt(i);
       
   145     }
       
   146     if ( mPendingRequest ) {
       
   147         mCachingInProgress = true;
       
   148     }
       
   149     TX_EXIT
       
   150 }
       
   151 
       
   152 /*!
       
   153  Cancels all outstanding album art requests.
       
   154 
       
   155  \sa getAlbumArt, cacheAlbumArt
       
   156  */
       
   157 void MpCollectionAlbumArtManager::cancel()
       
   158 {
       
   159     TX_ENTRY
       
   160     if ( mPendingRequest ) {
       
   161         mThumbnailManager->cancelRequest(mRequestId);
       
   162     }
       
   163     mImageCache.clear();
       
   164     mRequestQueue.clear();
       
   165     mPendingRequest = false;
       
   166     mCachingInProgress = false;
       
   167     TX_EXIT
       
   168 }
       
   169 
       
   170 /*!
       
   171  Slot to be called when thumbnail bitmap generation or loading is complete.
       
   172  */
       
   173 void MpCollectionAlbumArtManager::thumbnailReady( QPixmap pixmap, void *data, int id, int error )
       
   174 {
       
   175     int index = reinterpret_cast<int>(data);
       
   176     TX_ENTRY_ARGS("index=" << index << ", id=" << id << ", error=" << error);
       
   177     if ( !error && id == mRequestId && !pixmap.isNull() ) {
       
   178         // Find the index
       
   179         mImageCache.insert(index, new QIcon(pixmap));
       
   180         TX_LOG_ARGS("Album art ready for index=" << index);
       
   181         if ( !mCachingInProgress ) {
       
   182             emit albumArtReady(index);
       
   183         }
       
   184     }
       
   185     else {
       
   186         TX_EXIT_ARGS("Err: thumbnail manager returned error for getThumbnail request!");
       
   187     }
       
   188 
       
   189     if ( mCachingInProgress ) {
       
   190         if ( index >= (KInitCacheSize - 1) || !mRequestQueue.count() ) {
       
   191             mCachingInProgress = false;
       
   192             for ( int i = 0; i <= KInitCacheSize; ++i ) {
       
   193                 if ( mImageCache.contains(i) ) {
       
   194                     emit albumArtReady(i);
       
   195                 }
       
   196             }
       
   197         }
       
   198     }
       
   199 
       
   200     mPendingRequest = false;
       
   201     if ( mRequestQueue.count() ) {
       
   202         int index = mRequestQueue.takeFirst();
       
   203         QString albumArtUri = mCollectionData->itemData(index, MpMpxCollectionData::AlbumArtUri);
       
   204         void *clientData = reinterpret_cast<void *>(index);
       
   205         mRequestId = mThumbnailManager->getThumbnail( albumArtUri, clientData );
       
   206         if ( mRequestId != -1 ) {
       
   207             mPendingRequest = true;
       
   208             TX_EXIT_ARGS("next album art requested");
       
   209         }
       
   210         else {
       
   211             TX_EXIT_ARGS("Err: thumbnail manager returned (-1) for getThumbnail request!");
       
   212         }
       
   213     }
       
   214     TX_EXIT
       
   215 }
       
   216