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