videocollection/videocollectionwrapper/src/videothumbnailfetcher.cpp
changeset 34 bbb98528c666
child 17 69946d1824c4
equal deleted inserted replaced
33:48e74db5d516 34:bbb98528c666
       
     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: VideoThumbnailFetcher class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <qpixmap.h>
       
    21 #include <thumbnailmanager_qt.h>
       
    22 
       
    23 #include "videothumbnailfetcher.h"
       
    24 
       
    25 // ================= MEMBER FUNCTIONS =======================
       
    26 //
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // VideoThumbnailFetcher::VideoThumbnailFetcher()
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 VideoThumbnailFetcher::VideoThumbnailFetcher() :
       
    33     mThumbnailManager(0),
       
    34     mPaused(false),
       
    35     mTbnCreationEnabled(true)
       
    36 {
       
    37     mThumbnailManager = new ThumbnailManager();
       
    38     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailMedium);
       
    39     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForPerformance);
       
    40     mThumbnailManager->setMode(ThumbnailManager::CropToAspectRatio);
       
    41     
       
    42     connect(mThumbnailManager, SIGNAL(thumbnailReady( QPixmap , void * , int , int )),
       
    43                 this, SLOT(thumbnailReadySlot( QPixmap , void * , int , int )));
       
    44 }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // VideoThumbnailFetcher::~VideoThumbnailFetcher()
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 VideoThumbnailFetcher::~VideoThumbnailFetcher()
       
    51 {
       
    52     cancelFetches();
       
    53 
       
    54     disconnect(mThumbnailManager, SIGNAL(thumbnailReady( QPixmap , void * , int , int )),
       
    55                 this, SLOT(thumbnailReadySlot( QPixmap , void * , int , int )));
       
    56     
       
    57     delete mThumbnailManager;
       
    58     mThumbnailManager = 0;
       
    59 }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // VideoThumbnailFetcher::addFetch()
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void VideoThumbnailFetcher::addFetch(const QString fileName, void *internal, int priority)
       
    66 {
       
    67     ThumbnailFetchData *fetch = new ThumbnailFetchData;
       
    68     fetch->mFileName = fileName;
       
    69     fetch->mInternal = internal;
       
    70     fetch->mPriority = priority;
       
    71     mFetchList.append(fetch);
       
    72 }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // VideoThumbnailFetcher::continueFetching()
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void VideoThumbnailFetcher::continueFetching()
       
    79 {
       
    80     mPaused = false;
       
    81     
       
    82     // First fetch all thumbnails that have been created already, next
       
    83     // start thumbnail creation for one thumbnail at a time. 
       
    84     if(!mFetchList.isEmpty())
       
    85     {
       
    86         startThumbnailFetches();
       
    87     }
       
    88     else if(!mCreationList.isEmpty())
       
    89     {
       
    90         startThumbnailCreation();
       
    91     }
       
    92 
       
    93     // All thumbnails have been fetched, report it.
       
    94     if(mFetchList.isEmpty() && mCreationList.isEmpty() && mStartedFetchList.isEmpty())
       
    95     {
       
    96         emit allThumbnailsFetched();
       
    97     }
       
    98 }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // VideoThumbnailFetcher::startThumbnailFetches()
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void VideoThumbnailFetcher::startThumbnailFetches()
       
   105 {
       
   106     if(!mThumbnailManager)
       
   107         return;
       
   108     
       
   109     // Only fetch those thumbnails that are already been created.
       
   110     mThumbnailManager->setMode(ThumbnailManager::DoNotCreate);
       
   111     
       
   112     // Push all from thumbnail manager.
       
   113     while(!mFetchList.isEmpty())
       
   114     {
       
   115         ThumbnailFetchData *fetch = mFetchList.takeFirst();
       
   116 
       
   117         int requestId = mThumbnailManager->getThumbnail(fetch->mFileName,
       
   118                fetch->mInternal, fetch->mPriority);
       
   119         
       
   120         if(requestId != -1)
       
   121         {
       
   122             // Request succeed, add to list of started fetches.
       
   123             mStartedFetchList.insert(requestId, fetch);
       
   124         }
       
   125         else
       
   126         {
       
   127             // Request failed, free internal data.
       
   128             delete fetch->mInternal;
       
   129             delete fetch;
       
   130         }
       
   131     }
       
   132 }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // VideoThumbnailFetcher::startThumbnailCreation()
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void VideoThumbnailFetcher::startThumbnailCreation()
       
   139 {
       
   140     if(!mThumbnailManager || !mTbnCreationEnabled)
       
   141         return;
       
   142     
       
   143     mThumbnailManager->setMode(ThumbnailManager::CropToAspectRatio);
       
   144     
       
   145     // Do nothing if list is empty. 
       
   146     if(mCreationList.isEmpty())
       
   147         return;
       
   148     
       
   149     // Find fetch with highest priority. 
       
   150     int highestPriority = 0;
       
   151     int indexWithHighestPriority = 0;
       
   152     
       
   153     for(int i = 0; i < mCreationList.count(); i++)
       
   154     {
       
   155         if(mCreationList.at(i)->mPriority > highestPriority)
       
   156         {
       
   157             indexWithHighestPriority = i;
       
   158             highestPriority = mCreationList.at(i)->mPriority;
       
   159         }
       
   160     }
       
   161     
       
   162     ThumbnailFetchData *fetch = mCreationList.takeAt(indexWithHighestPriority);
       
   163     
       
   164     // Do request to thumbnail manager.
       
   165     int requestId = mThumbnailManager->getThumbnail(fetch->mFileName,
       
   166             fetch->mInternal, fetch->mPriority);
       
   167     
       
   168     // Request failed, free internal data.
       
   169     if(requestId == -1)
       
   170     {
       
   171         delete fetch->mInternal;
       
   172         delete fetch;
       
   173     }
       
   174     else 
       
   175     {
       
   176         // Don't keep track of fetches when creating thumbnails, if
       
   177         // it fails with -1 it would be only tried to create again.  
       
   178         delete fetch;
       
   179     }
       
   180 }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // VideoThumbnailFetcher::pauseFetching()
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void VideoThumbnailFetcher::pauseFetching()
       
   187 {
       
   188     mPaused = true;
       
   189 }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // VideoThumbnailFetcher::cancelFetches()
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 void VideoThumbnailFetcher::cancelFetches()
       
   196 {
       
   197     // Clear list of started fetches, thumbnail manager has the internal 
       
   198     // pointer.
       
   199     QList<int> keys = mStartedFetchList.keys();
       
   200     for(int i = 0; i < keys.count(); i++ )
       
   201     {
       
   202         delete mStartedFetchList.take(keys[i]);
       
   203     }
       
   204     
       
   205     // Merge lists and free data.
       
   206     mFetchList.append(mCreationList);
       
   207     mCreationList.clear();
       
   208     while(!mFetchList.isEmpty())
       
   209     {
       
   210         ThumbnailFetchData *fetch = mFetchList.takeFirst();
       
   211         delete fetch->mInternal;
       
   212         delete fetch;
       
   213     }
       
   214 }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // VideoThumbnailFetcher::fetchCount()
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 int VideoThumbnailFetcher::fetchCount()
       
   221 {
       
   222     return mFetchList.count() + mCreationList.count() + mStartedFetchList.count();
       
   223 }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // VideoThumbnailFetcher::enableThumbnailCreation()
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void VideoThumbnailFetcher::enableThumbnailCreation(bool enable)
       
   230 {
       
   231     mTbnCreationEnabled = enable;
       
   232 }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // VideoThumbnailFetcher::thumbnailReadySlot()
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 void VideoThumbnailFetcher::thumbnailReadySlot(QPixmap tnData, void *internal, int requestId, int error)
       
   239 {
       
   240     // Thumbnail has not been generated yet, put it into creation list.
       
   241     if(error == -1 && internal)
       
   242     {
       
   243         if(mStartedFetchList.contains(requestId))
       
   244         {
       
   245             ThumbnailFetchData *fetch = mStartedFetchList.take(requestId);
       
   246             mCreationList.append(fetch);
       
   247         }
       
   248         else
       
   249         {
       
   250             // Fetch data was not found, meaning cancelFetches was called.
       
   251             delete internal;
       
   252         }
       
   253     }
       
   254     else
       
   255     {
       
   256         // Report that thumbnail was fetched.
       
   257         emit thumbnailReady(tnData, internal, error);
       
   258         
       
   259         if(mStartedFetchList.contains(requestId))
       
   260             delete mStartedFetchList.take(requestId);
       
   261     }
       
   262     
       
   263     // Continue the fetching process.
       
   264     if(!mPaused && mStartedFetchList.isEmpty())
       
   265         continueFetching();
       
   266 }
       
   267 
       
   268 // End of file.