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