engine/collectionframework/thumbnailcreator/src/glxtngeneratethumbnailtask.cpp
changeset 71 27f2d7aec52a
parent 69 45459746d5e8
child 72 0a8e959402e5
equal deleted inserted replaced
69:45459746d5e8 71:27f2d7aec52a
     1 /*
       
     2 * Copyright (c) 2008-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:   Classes for thumbnail-related tasks.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 /**
       
    21  * @internal reviewed 31/07/2007 by Simon Brooks
       
    22  */
       
    23 
       
    24 #include "glxtngeneratethumbnailtask.h"
       
    25 
       
    26 #include <bitdev.h>
       
    27 #include <fbs.h>
       
    28 #include <glxassert.h>
       
    29 #include <glxlog.h>
       
    30 #include <glxtracer.h>
       
    31 #include <glxthumbnail.h>
       
    32 
       
    33 #include "glxtnfileinfo.h"
       
    34 #include "glxtnfileutility.h"
       
    35 #include "glxtnimageutility.h"
       
    36 #include "glxtnsavethumbnailtask.h"
       
    37 #include "glxtntaskmanager.h"
       
    38 #include "glxtnthumbnailrequest.h"
       
    39 #include "glxtnvideoutility.h"
       
    40 #include "mglxtnthumbnailcreatorclient.h"
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // NewL
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CGlxtnGenerateThumbnailTask* CGlxtnGenerateThumbnailTask::NewL(
       
    47         const TGlxThumbnailRequest& aRequestInfo,
       
    48         CGlxtnFileUtility& aFileUtility, MGlxtnThumbnailCreatorClient& aClient)
       
    49     {
       
    50     TRACER("CGlxtnGenerateThumbnailTask::NewL()");
       
    51     CGlxtnGenerateThumbnailTask* task
       
    52                 = new (ELeave) CGlxtnGenerateThumbnailTask(
       
    53                                         aRequestInfo, aFileUtility, aClient);
       
    54     CleanupStack::PushL(task);
       
    55     task->ConstructL(aRequestInfo.iBitmapHandle);
       
    56     CleanupStack::Pop( task );
       
    57     return task;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // Constructor
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CGlxtnGenerateThumbnailTask::CGlxtnGenerateThumbnailTask(
       
    65         const TGlxThumbnailRequest& aRequestInfo,
       
    66         CGlxtnFileUtility& aFileUtility, MGlxtnThumbnailCreatorClient& aClient) :
       
    67     CGlxtnLoadThumbnailTask( KGlxtnTaskIdGenerateThumbnail,
       
    68                             aRequestInfo, aFileUtility, aClient )
       
    69 	{
       
    70     TRACER("CGlxtnGenerateThumbnailTask::CGlxtnGenerateThumbnailTask()");
       
    71 	}
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // ConstructL
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CGlxtnGenerateThumbnailTask::ConstructL(TInt aBitmapHandle)
       
    78 	{
       
    79     TRACER("CGlxtnGenerateThumbnailTask::ConstructL()");
       
    80 	CGlxtnLoadThumbnailTask::ConstructL( aBitmapHandle );
       
    81 
       
    82     iImageUtility = new (ELeave) CGlxtnImageUtility(iFileUtility.FsSession());
       
    83 	}
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // Destructor
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 CGlxtnGenerateThumbnailTask::~CGlxtnGenerateThumbnailTask() 
       
    90 	{
       
    91     TRACER("CGlxtnGenerateThumbnailTask::~CGlxtnGenerateThumbnailTask()");
       
    92 	delete iImageUtility;
       
    93     delete iVideoUtility;
       
    94     delete iImage;
       
    95 	}
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // SetManager
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CGlxtnGenerateThumbnailTask::SetManager(CGlxtnTaskManager* aManager)
       
   102     {
       
   103     TRACER("void CGlxtnGenerateThumbnailTask::SetManager()");
       
   104     iTaskManager = aManager;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // DoStartL
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 TBool CGlxtnGenerateThumbnailTask::DoStartL(TRequestStatus& aStatus)
       
   112     {
       
   113     TRACER("CGlxtnGenerateThumbnailTask::DoStartL()");
       
   114     iInfo = new (ELeave) CGlxtnFileInfo;
       
   115     Client().FetchFileInfoL(iInfo, ItemId(), &aStatus);
       
   116     iState = EStateFetchingUri;
       
   117 
       
   118     return ETrue; // Request has been issued
       
   119     } 
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // DoCancel
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CGlxtnGenerateThumbnailTask::DoCancel() 
       
   126 	{
       
   127     TRACER("void CGlxtnGenerateThumbnailTask::DoCancel()");
       
   128 	CGlxtnLoadThumbnailTask::DoCancel();
       
   129 
       
   130 	iImageUtility->Cancel();
       
   131 
       
   132 	if ( iVideoUtility ) 
       
   133 		{
       
   134 		iVideoUtility->Cancel();
       
   135 		}
       
   136 
       
   137     iFileUtility.ClearBadFileMarker();
       
   138 	}
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // DoRunL
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 TBool CGlxtnGenerateThumbnailTask::DoRunL(TRequestStatus& aStatus) 
       
   145 	{
       
   146     TRACER("TBool CGlxtnGenerateThumbnailTask::DoRunL()");
       
   147 	TInt error = aStatus.Int();
       
   148 
       
   149 	TBool active = EFalse;
       
   150 
       
   151     if ( KErrNone == error )
       
   152         {
       
   153         switch ( iState )
       
   154             {
       
   155             case EStateFetchingUri:
       
   156                 {
       
   157                 User::LeaveIfNull(iInfo);
       
   158                 iBadFileMarkerNotNeededFlag = ETrue;
       
   159 
       
   160                 iInfo->IdentifyFileL(iVideo, iProtected);
       
   161                 // leave if file is DRM protected and client has no DRM 
       
   162                 // capablity
       
   163                 if ( iProtected && !iDrmAllowed )
       
   164                     {
       
   165                     User::Leave(KErrAccessDenied);
       
   166                     }
       
   167 
       
   168                 if ( !LoadThumbnailL( aStatus ) )
       
   169                     {
       
   170                     iBadFileMarkerNotNeededFlag = EFalse;
       
   171                     iFileUtility.CheckBadFileListL( iInfo->FilePath() );
       
   172                     HighQualityDecodeL( aStatus );
       
   173                     }
       
   174                 active = ETrue;
       
   175                 }
       
   176                 break;
       
   177 
       
   178             case EStateLoading:
       
   179                 active = HandleLoadedThumbnailL( aStatus );
       
   180                 break;
       
   181 
       
   182             case EStateDecodingThumbnail:
       
   183                 // Nothing to do
       
   184                 break;
       
   185 
       
   186             case EStateDecodingImage:
       
   187                 {
       
   188                 iImageUtility->FreeDecoder();
       
   189 
       
   190                 // Resize empty bitmap to required size
       
   191                 TSize targetSize(iRequestedSize);
       
   192                 iImageUtility->AdjustSize(targetSize, iImage->SizeInPixels());
       
   193                 User::LeaveIfError(iThumbnail->Resize(targetSize));
       
   194 
       
   195                 iImageUtility->FilterImageL(&aStatus, iImage, iImage, iThumbnail);
       
   196                 iState = EStateFiltering;
       
   197                 active = ETrue;
       
   198                 }
       
   199                 break;
       
   200 
       
   201             case EStateFiltering:
       
   202                 {
       
   203                 TSize targetSize(iRequestedSize);
       
   204                 const TSize srcSize = iImage->SizeInPixels();
       
   205                 iImageUtility->AdjustSize(targetSize, iImage->SizeInPixels());
       
   206                 iImageUtility->ScaleImageL(aStatus, *iImage, srcSize, *iThumbnail, targetSize);
       
   207                 iState = EStateScaling;
       
   208                 active = ETrue;
       
   209                 }
       
   210                 break;
       
   211 
       
   212             case EStateScaling:
       
   213             	{
       
   214             	User::LeaveIfError(iThumbnail->SetDisplayMode(KGlxThumbnailDisplayMode));
       
   215             	           	
       
   216                 // Create task to save generated thumbnail
       
   217                 if ( Storage() && !iInfo->iTemporary
       
   218                     && !SizeRequiresDrmRights()
       
   219                     && iFileUtility.IsPersistentSize(iRequestedSize) )
       
   220                     {
       
   221                     CGlxtnSaveThumbnailTask* task
       
   222                         = CGlxtnSaveThumbnailTask::NewL(ItemId(), iInfo,
       
   223                                         iRequestedSize, iThumbnail, Storage());
       
   224                     iThumbnail = NULL;  // Task has ownership
       
   225                     iInfo = NULL;       // Task has ownership
       
   226                     iTaskManager->AddTaskL(task);
       
   227                     }
       
   228                 break;
       
   229             	}
       
   230 
       
   231             default:
       
   232                 GLX_ASSERT_ALWAYS( EFalse, Panic( EGlxPanicIllegalState ),
       
   233                             "CGlxtnGenerateThumbnailTask: Illegal state" );
       
   234                 break;
       
   235             }
       
   236         }
       
   237     else
       
   238         {
       
   239         if ( EStateLoading == iState )
       
   240             {
       
   241             // Loading failed - generate new thumbnail instead
       
   242             HighQualityDecodeL( aStatus );
       
   243             active = ETrue;
       
   244             }
       
   245         }
       
   246 
       
   247     if ( !active )
       
   248         {
       
   249         if ( !iBadFileMarkerNotNeededFlag )
       
   250             {
       
   251             iFileUtility.ClearBadFileMarker();
       
   252             }
       
   253         Client().ThumbnailFetchComplete(ItemId(),
       
   254                                         EGlxThumbnailQualityHigh, error);
       
   255         }
       
   256 
       
   257     return active; 
       
   258 	}
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // DoRunError
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 TBool CGlxtnGenerateThumbnailTask::DoRunError(TInt aError)
       
   265     {
       
   266     TRACER("TBool CGlxtnGenerateThumbnailTask::DoRunError(TInt aError)");
       
   267     iFileUtility.ClearBadFileMarker();
       
   268     Client().ThumbnailFetchComplete(ItemId(), EGlxThumbnailQualityHigh, aError);
       
   269 
       
   270     return EFalse;
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // HighQualityDecodeL
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 void CGlxtnGenerateThumbnailTask::HighQualityDecodeL(
       
   278                                                 TRequestStatus& aStatus )
       
   279     {
       
   280     TRACER("void CGlxtnGenerateThumbnailTask::HighQualityDecodeL()");
       
   281     RArray<TSize> targetSizes;
       
   282     CleanupClosePushL(targetSizes);
       
   283     targetSizes.AppendL( TSize(iRequestedSize) );
       
   284     if ( iVideo )
       
   285         {
       
   286         iVideoUtility = new (ELeave) CGlxtnVideoUtility;
       
   287         iVideoUtility->GetVideoFrameL( &aStatus, iImage, iInfo->FilePath(), targetSizes, KGlxThumbnailDisplayMode );
       
   288         }
       
   289     else
       
   290         {
       
   291         iImage = iImageUtility->DecodeImageL( aStatus, iInfo->FilePath(),
       
   292                                     targetSizes, KGlxThumbnailDisplayMode );
       
   293         }
       
   294     CleanupStack::PopAndDestroy(&targetSizes);
       
   295     iState = EStateDecodingImage;
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // SizeRequiresDrmRights
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 TBool CGlxtnGenerateThumbnailTask::SizeRequiresDrmRights()
       
   303     {
       
   304     TRACER("TBool CGlxtnGenerateThumbnailTask::SizeRequiresDrmRights()");
       
   305     if ( iProtected )
       
   306         {
       
   307         if ( iVideo )
       
   308             {
       
   309             return ETrue;
       
   310             }
       
   311 
       
   312         // Rights are required if thumbnail doesn't fit in KGlxThumbnailDrmWidth
       
   313         // by KGlxThumbnailDrmHeight box either portrait or landscape
       
   314         if ( (iRequestedSize.iWidth > KGlxThumbnailDrmWidth
       
   315                 || iRequestedSize.iHeight > KGlxThumbnailDrmHeight)
       
   316             && (iRequestedSize.iWidth > KGlxThumbnailDrmHeight
       
   317                 || iRequestedSize.iHeight > KGlxThumbnailDrmWidth) )
       
   318             {
       
   319             return ETrue;
       
   320             }
       
   321 
       
   322         const TSize& imageSize(iImageUtility->OriginalSize());
       
   323 
       
   324         // Rights are required if thumbnail is greater than 25% of image size
       
   325         // Due to preserving aspect ratio, this will only happen if both width
       
   326         // and height are greater than half the image width and height
       
   327         if ( iRequestedSize.iWidth > imageSize.iWidth / 2
       
   328             && iRequestedSize.iHeight> imageSize.iHeight / 2 )
       
   329             {
       
   330             return ETrue;
       
   331             }
       
   332         }
       
   333 
       
   334     return EFalse;
       
   335     }