mtpdataproviders/mtpimagedp/src/cmtpimagedpthumbnailcreator.cpp
changeset 0 d0791faffa3f
child 2 4843bb5893b6
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <e32base.h>
       
    22 #include <e32property.h>
       
    23 #include <fbs.h>
       
    24 #include <caf/content.h>
       
    25 #include <icl/imagedata.h>
       
    26 #include <sysutil.h>
       
    27 #include <pathinfo.h> // PathInfo
       
    28 #include <bautils.h> // FileExists
       
    29 #include <mtp/cmtptypeopaquedata.h>
       
    30 #include "cmtpimagedpthumbnailcreator.h"
       
    31 #include "mtpimagedpconst.h"
       
    32 #include "mtpimagedputilits.h"
       
    33 
       
    34 __FLOG_STMT(_LIT8(KComponent,"CMTPImageDpThumbnailCreator");)
       
    35 // --------------------------------------------------------------------------
       
    36 // CMTPImageDpThumbnailCreator::NewL
       
    37 // 2-phased constructor.
       
    38 // --------------------------------------------------------------------------
       
    39 //
       
    40 CMTPImageDpThumbnailCreator* CMTPImageDpThumbnailCreator::NewL()
       
    41     {
       
    42     CMTPImageDpThumbnailCreator* self= new (ELeave) CMTPImageDpThumbnailCreator();
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop(self);
       
    46     return self;
       
    47     }
       
    48  
       
    49 // --------------------------------------------------------------------------
       
    50 // CMTPImageDpThumbnailCreator::~CMTPImageDpThumbnailCreator
       
    51 // C++ destructor.
       
    52 // --------------------------------------------------------------------------
       
    53 //    
       
    54 CMTPImageDpThumbnailCreator::~CMTPImageDpThumbnailCreator()
       
    55     {
       
    56     __FLOG(_L8(">> ~CMTPImageDpThumbnailCreator"));
       
    57     Cancel();
       
    58     if(EGetting == iState)
       
    59         {
       
    60         iThumbMgr->CancelRequest(iCurrentReq);
       
    61         }
       
    62     delete iData;
       
    63     delete iImgEnc;
       
    64 #ifdef MTPTHUMBSCALING
       
    65     delete iScaler;
       
    66 #endif
       
    67     delete iBitmap;  
       
    68     delete iObjectSource;
       
    69     delete iThumbMgr;
       
    70     if(iActiveSchedulerWait->IsStarted())
       
    71         {
       
    72         *iCreationErr = KErrNotReady;
       
    73         iActiveSchedulerWait->AsyncStop();
       
    74         }
       
    75     delete iActiveSchedulerWait;
       
    76     __FLOG(_L8("<< ~CMTPImageDpThumbnailCreator"));
       
    77     __FLOG_CLOSE;
       
    78     }
       
    79  
       
    80 // --------------------------------------------------------------------------
       
    81 // CMTPImageDpThumbnailCreator::CMTPImageDpThumbnailCreator
       
    82 // C++ constructor.
       
    83 // --------------------------------------------------------------------------
       
    84 //    
       
    85 CMTPImageDpThumbnailCreator::CMTPImageDpThumbnailCreator(): 
       
    86 CActive(EPriorityStandard)
       
    87     {
       
    88     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
    89     __FLOG(_L8("CMTPImageDpThumbnailCreator::CMTPImageDpThumbnailCreator(), begin"));
       
    90     CActiveScheduler::Add(this);  
       
    91     __FLOG(_L8("CMTPImageDpThumbnailCreator::CMTPImageDpThumbnailCreator(), end"));
       
    92     }
       
    93 
       
    94 // --------------------------------------------------------------------------
       
    95 // CMTPImageDpThumbnailCreator::ConstructL
       
    96 // 2nd phase constructor.
       
    97 // --------------------------------------------------------------------------
       
    98 //
       
    99 void CMTPImageDpThumbnailCreator::ConstructL()
       
   100     {
       
   101     __FLOG(_L8("CMTPImageDpThumbnailCreator::ConstructL(), begin"));
       
   102     iThumbMgr = CThumbnailManager::NewL( *this ); 
       
   103     iThumbMgr->SetThumbnailSizeL( EFullScreenThumbnailSize );
       
   104 #ifdef MTPTHUMBSCALING
       
   105     iScaler = CBitmapScaler::NewL();
       
   106 #endif    
       
   107     iActiveSchedulerWait = new (ELeave) CActiveSchedulerWait();
       
   108     __FLOG(_L8("CMTPImageDpThumbnailCreator::ConstructL(), end"));
       
   109     }
       
   110 
       
   111 // --------------------------------------------------------------------------
       
   112 // CMTPImageDpThumbnailCreator::DoCancel
       
   113 // From CActive.
       
   114 // --------------------------------------------------------------------------
       
   115 //
       
   116 void CMTPImageDpThumbnailCreator::DoCancel()
       
   117     {
       
   118     __FLOG_VA((_L8(">> CMTPImageDpThumbnailCreator::DoCancel() iState %d iStatus 0x%X"), iState, iStatus.Int()));
       
   119     switch(iState)
       
   120         {
       
   121 #ifdef MTPTHUMBSCALING
       
   122         case EScaling:
       
   123             iScaler->Cancel();
       
   124             break;
       
   125 #endif
       
   126         case EEncoding:
       
   127             iImgEnc->Cancel();
       
   128             break;
       
   129         default:
       
   130             break;
       
   131         }
       
   132     if(iActiveSchedulerWait->IsStarted())
       
   133         {
       
   134         *iCreationErr = KErrCancel;
       
   135         iActiveSchedulerWait->AsyncStop();
       
   136         }
       
   137     // we will not continue creating thumbs.
       
   138     __FLOG_VA((_L8("<< CMTPImageDpThumbnailCreator::DoCancel() iState %d"), iState));
       
   139     }
       
   140 
       
   141 // --------------------------------------------------------------------------
       
   142 // CMTPImageDpThumbnailCreator::RunL
       
   143 // From CActive.
       
   144 // --------------------------------------------------------------------------
       
   145 //    
       
   146 void CMTPImageDpThumbnailCreator::RunL()
       
   147     {
       
   148     __FLOG_VA((_L8(">> CMTPImageDpThumbnailCreator::RunL() iState %d iStatus %d"), iState, iStatus.Int()));
       
   149     User::LeaveIfError(iStatus.Int());
       
   150     switch (iState)
       
   151         { 
       
   152 #ifdef MTPTHUMBSCALING
       
   153         case EGetted:
       
   154             {
       
   155             ScaleBitmap();
       
   156             iState = EScaling;
       
   157             break;
       
   158             }
       
   159 #endif
       
   160         case EScaling:
       
   161             {
       
   162             EncodeImageL( );
       
   163             iState=EEncoding;
       
   164             break;
       
   165             }
       
   166         case EEncoding:
       
   167             {
       
   168             iState=EIdle;
       
   169             if (iThumbMgr->Flags() == CThumbnailManager::EDoNotCreate)
       
   170                 {
       
   171                 __FLOG_VA((_L8("CMTPImageDpThumbnailCreator::RunL(),EDoNotCreate; iState %d"), iState));
       
   172                 delete iData;
       
   173                 iData = HBufC8::NewL(1);
       
   174                 iBuffer->Write(*iData);
       
   175                 }
       
   176             else{
       
   177                 iBuffer->Write(*iData);
       
   178                 }
       
   179             
       
   180             __FLOG_VA((_L8("<< CMTPImageDpThumbnailCreator::RunL(),iBuffer->Write(*iData); iState %d"), iState));
       
   181             if(iActiveSchedulerWait->IsStarted())
       
   182                 {
       
   183                 iActiveSchedulerWait->AsyncStop();
       
   184                 }
       
   185             break;
       
   186             }
       
   187         default:
       
   188             {
       
   189             User::Leave(KErrGeneral);
       
   190             break;
       
   191             }
       
   192         }
       
   193     __FLOG_VA((_L8("<< CMTPImageDpThumbnailCreator::RunL() iState %d"), iState));
       
   194     }
       
   195 
       
   196 // --------------------------------------------------------------------------
       
   197 // RunError
       
   198 // --------------------------------------------------------------------------
       
   199 //    
       
   200 TInt CMTPImageDpThumbnailCreator::RunError(TInt aErr)
       
   201     {
       
   202     __FLOG_VA((_L8(">> CMTPImageDpThumbnailCreator::RunError() err 0x%X"), aErr));
       
   203     iState=EIdle;
       
   204     if(iActiveSchedulerWait->IsStarted())
       
   205         {
       
   206         *iCreationErr = aErr;
       
   207         iActiveSchedulerWait->AsyncStop();
       
   208         }
       
   209     // no need to cancel iScalerP since only leave is issued if scaler creation fails
       
   210     __FLOG(_L8("<< CMTPImageDpThumbnailCreator::RunError()"));
       
   211     return KErrNone;
       
   212     }
       
   213 
       
   214 // --------------------------------------------------------------------------
       
   215 // 
       
   216 // --------------------------------------------------------------------------
       
   217 //
       
   218 void CMTPImageDpThumbnailCreator::GetThumbnailL(const TDesC& aFileName, CMTPTypeOpaqueData& aThumbName,  TInt& result)
       
   219     {
       
   220     __FLOG(_L8(">> CMtpImageDphumbnailCreator::GetThumbnailL()"));
       
   221     iBuffer = &aThumbName;
       
   222     GetThumbL(aFileName);
       
   223     iCreationErr = &result;		//reset the err flag
       
   224     *iCreationErr = KErrNone;
       
   225     __FLOG(_L8("<< CMTPImageDpThumbnailCreator::CreateThumbnailL()"));
       
   226     iActiveSchedulerWait->Start();
       
   227     }
       
   228 
       
   229 // --------------------------------------------------------------------------
       
   230 // 
       
   231 // --------------------------------------------------------------------------
       
   232 //
       
   233 void CMTPImageDpThumbnailCreator::ClearThumbnailData()
       
   234     {
       
   235     delete iData;
       
   236     iData = NULL;
       
   237     delete iBitmap;
       
   238     iBitmap = NULL;
       
   239     delete iObjectSource;
       
   240     iObjectSource = NULL;
       
   241     delete iImgEnc;
       
   242     iImgEnc = NULL;
       
   243     }
       
   244 
       
   245 void CMTPImageDpThumbnailCreator::GetThumbL(const TDesC& aFileName)
       
   246     {
       
   247     __FLOG(_L8(">> CMtpImageDphumbnailCreator::GetThumbL()"));
       
   248     // Create an object source representing a path to a file on local
       
   249     // file system.
       
   250     delete iObjectSource;
       
   251     iObjectSource = NULL;
       
   252     
       
   253     iObjectSource = CThumbnailObjectSource::NewL(aFileName, KJpegMimeType);
       
   254     iCurrentReq = iThumbMgr->GetThumbnailL( *iObjectSource );
       
   255     iState = EGetting;
       
   256     __FLOG(_L8("<< CMtpImageDphumbnailCreator::GetThumbL()"));
       
   257     }
       
   258 
       
   259 #ifdef MTPTHUMBSCALING
       
   260 // --------------------------------------------------------------------------
       
   261 // CMTPImageDpThumbnailCreator::ScaleBitmapL
       
   262 // Scales the bitmap to the thumbnail size.
       
   263 // --------------------------------------------------------------------------
       
   264 //
       
   265 void CMTPImageDpThumbnailCreator::ScaleBitmap()
       
   266     {
       
   267     __FLOG(_L8("CMTPImageDpThumbnailCreator::ScaleBitmapL(), begin"));
       
   268     TSize size( KThumbWidht, KThumbHeigth ); // size 160x120      
       
   269     // Resize image to thumbnail size 
       
   270     iScaler->Scale( &iStatus, *iBitmap, size );
       
   271     SetActive();
       
   272     __FLOG(_L8("CMTPImageDpThumbnailCreator::ScaleBitmapL(), end"));
       
   273     }
       
   274 #endif
       
   275 
       
   276 // --------------------------------------------------------------------------
       
   277 // CMTPImageDpThumbnailCreator::EncodeImageL
       
   278 // Encodes bitmap as a jpeg image.
       
   279 // --------------------------------------------------------------------------
       
   280 //
       
   281 void CMTPImageDpThumbnailCreator::EncodeImageL( )
       
   282     {
       
   283     __FLOG(_L8(">> CMTPImageDpThumbnailCreator::EncodeImageL()"));
       
   284 
       
   285     delete iData;
       
   286     iData = NULL;
       
   287     
       
   288     delete iImgEnc;
       
   289     iImgEnc = NULL;
       
   290     
       
   291     // Convert bitmap to jpg
       
   292     iImgEnc = CImageEncoder::DataNewL( iData, KPtpMimeJPEG, CImageEncoder::EPreferFastEncode );
       
   293     iImgEnc->Convert( &iStatus, *iBitmap );
       
   294     SetActive();
       
   295     __FLOG(_L8("<< CMTPImageDpThumbnailCreator::EncodeImageL()"));
       
   296     }
       
   297 
       
   298 //
       
   299 //
       
   300 void CMTPImageDpThumbnailCreator::ThumbnailReady( TInt aError, MThumbnailData& aThumbnail, TThumbnailRequestId aId )
       
   301     {
       
   302     // This function must not leave.
       
   303     __FLOG(_L8(">> CMTPImageDpThumbnailCreator::ThumbnailReady()"));
       
   304     if(iCurrentReq != aId)
       
   305         {
       
   306         __FLOG(_L8("CMTPImageDpThumbnailCreator::ThumbnailReady(),iCurrentReq != aId"));
       
   307         return;
       
   308         }
       
   309     if (aError == KErrNone)
       
   310         {
       
   311         TRAP_IGNORE(iThumbMgr->SetFlagsL(CThumbnailManager::EDefaultFlags));
       
   312         delete iBitmap;   
       
   313         // Claim ownership of the bitmap instance for later use
       
   314         iBitmap = aThumbnail.DetachBitmap();
       
   315 #ifdef MTPTHUMBSCALING
       
   316         iState = EGetted;
       
   317 #else
       
   318         iState = EScaling;				//direct set to Scaling state jump the scaling function
       
   319 #endif
       
   320         }
       
   321     else if ((iThumbMgr->Flags() == CThumbnailManager::EDoNotCreate) && (aError == KErrNotFound))
       
   322         {
       
   323         __FLOG(_L8("CMTPImageDpThumbnailCreator::ThumbnailReady(),EDoNotCreate, KErrNotFound"));
       
   324         iState = EEncoding;
       
   325         iThumbMgr->CreateThumbnails(*iObjectSource);
       
   326         aError = KErrNone;
       
   327         }
       
   328     iStatus=KRequestPending;
       
   329     TRequestStatus* status=&iStatus;
       
   330     User::RequestComplete(status, aError);
       
   331     SetActive();
       
   332     __FLOG(_L8("<< CMTPImageDpThumbnailCreator::ThumbnailReady()"));
       
   333     }
       
   334 
       
   335 void CMTPImageDpThumbnailCreator::ThumbnailPreviewReady( MThumbnailData& /*aThumbnail*/, TThumbnailRequestId /*aId*/ )
       
   336     {
       
   337     
       
   338     }
       
   339 
       
   340 // End of file