mobilemessaging/postcard/postcardsrc/PostcardOperationInsertImage.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2005 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:  
       
    15 *       CPostcardOperationInsertImage
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // ========== INCLUDE FILES ================================
       
    22 
       
    23 #include <AknsConstants.h>
       
    24 #include <data_caging_path_literals.hrh> 
       
    25 #include <apmstd.h>  // TDataType
       
    26 #include <f32file.h> // RFs, RFile
       
    27 #include <gulicon.h> // CGulIcon
       
    28 #include <apmstd.h>  // TDataType
       
    29 #include <e32des8.h>
       
    30 
       
    31 #include <IHLImageFactory.h>
       
    32 #include <IHLViewerFactory.h>
       
    33 #include <MIHLFileImage.h>
       
    34 #include <MIHLBitmap.h>
       
    35 #include <MIHLImageViewer.h>
       
    36 
       
    37 #include <mmsvattachmentmanager.h>
       
    38 #include <mmsvattachmentmanagersync.h>
       
    39 #include <cmsvattachment.h>
       
    40 #include <cmsvmimeheaders.h>
       
    41 
       
    42 #include <MsgMediaResolver.h>
       
    43 #include <MsgMediaInfo.h>
       
    44 #include <MsgImageInfo.h>
       
    45 #include <MsgMimeTypes.h>
       
    46 
       
    47 #include <mmsclient.h>
       
    48 #include <Postcard.rsg>
       
    49 
       
    50 #include "PostcardOperationInsertImage.h"
       
    51 #include "PostcardLaf.h"
       
    52 
       
    53 // ========== EXTERNAL DATA STRUCTURES =====================
       
    54 
       
    55 // ========== EXTERNAL FUNCTION PROTOTYPES =================
       
    56 
       
    57 // ========== CONSTANTS ====================================
       
    58 
       
    59 // ========== MACROS =======================================
       
    60 
       
    61 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    62 
       
    63 // ========== MODULE DATA STRUCTURES =======================
       
    64 
       
    65 // ========== LOCAL FUNCTION PROTOTYPES ====================
       
    66 
       
    67 // ========== LOCAL FUNCTIONS ==============================
       
    68 
       
    69 // ========== MEMBER FUNCTIONS =============================
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 // CPostcardOperationInsertImage::NewL
       
    73 //
       
    74 // Factory method.
       
    75 // ---------------------------------------------------------
       
    76 //
       
    77 CPostcardOperationInsertImage* CPostcardOperationInsertImage::NewL(
       
    78             MPostcardOperationObserver& aObserver,
       
    79             CPostcardDocument& aDocument,
       
    80             CPostcardAppUi& aAppUi,
       
    81             CMsgImageInfo* aInfo,
       
    82             RFs& aFs )
       
    83     {
       
    84     CPostcardOperationInsertImage* self = 
       
    85         new ( ELeave ) CPostcardOperationInsertImage( aObserver, aDocument, aAppUi, aInfo, aFs );
       
    86     CleanupStack::PushL( self );
       
    87     self->ConstructL( );
       
    88     CleanupStack::Pop( self );
       
    89     return self;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------
       
    93 // CPostcardOperationInsertImage::CPostcardOperationInsertImage
       
    94 // ---------------------------------------------------------
       
    95 CPostcardOperationInsertImage::CPostcardOperationInsertImage(
       
    96             MPostcardOperationObserver& aObserver,
       
    97             CPostcardDocument& aDocument,
       
    98             CPostcardAppUi& aAppUi,
       
    99             CMsgImageInfo* aInfo,
       
   100             RFs& aFs ) :
       
   101     CPostcardOperation( aObserver, aDocument, aAppUi, aFs ),
       
   102     iInfo( aInfo )
       
   103     {
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------
       
   107 // CPostcardOperationInsertImage::CPostcardOperationInsertImage
       
   108 // ---------------------------------------------------------
       
   109 CPostcardOperationInsertImage::~CPostcardOperationInsertImage( )
       
   110     {
       
   111     delete iImageProcessor;
       
   112     delete iInfo;
       
   113     if( iCompressFile )
       
   114     	{
       
   115     	iCompressFile->Close();
       
   116         delete iCompressFile;
       
   117         iCompressFile = NULL;
       
   118     	}
       
   119     Cancel( );
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------
       
   123 // CPostcardOperationInsertImage::Start
       
   124 // ---------------------------------------------------------
       
   125 void CPostcardOperationInsertImage::Start( TInt /*aArgument*/ )
       
   126     {
       
   127     iLaunchState = EPostcardInsertImageInitializing;
       
   128     CompleteSelf( KErrNone );
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------
       
   132 // CPostcardOperationInsertImage::Launch
       
   133 // ---------------------------------------------------------
       
   134 void CPostcardOperationInsertImage::DoLaunchStepL()
       
   135     {
       
   136     iFlags &= ~EPostcardInsertImageRequestActive;
       
   137     switch ( iLaunchState )
       
   138         {
       
   139         case EPostcardInsertImageInitializing:
       
   140             {
       
   141             InitL( );
       
   142             iLaunchState = EPostcardInsertImageCheckAttas;
       
   143             CompleteSelf( KErrNone );
       
   144             break;
       
   145             }
       
   146         case EPostcardInsertImageCheckAttas:
       
   147             {
       
   148             if( !CheckAttasL( ) )
       
   149             	{ // If there was error, this has been completed already
       
   150 	            iLaunchState = EPostcardInsertImageCreateAtta;
       
   151 	            CompleteSelf( KErrNone );
       
   152             	}
       
   153             break;
       
   154             }
       
   155         case EPostcardInsertImageCreateAtta:
       
   156             {
       
   157             DoCreateAttaL( );
       
   158             break;
       
   159             }
       
   160         case EPostcardInsertImageRename:
       
   161         	{
       
   162         	DoRenameAttaL( );
       
   163         	break;
       
   164         	}
       
   165         case EPostcardInsertProcessImage:
       
   166         	{
       
   167         	DoStartProcessImageL( );
       
   168         	break;
       
   169         	}
       
   170         case EPostcardInsertImageFinalizeSave:
       
   171             {
       
   172             DoFinalizeSaveL( );
       
   173             iLaunchState = EPostcardInsertImageCreateBitmap;
       
   174             CompleteSelf( KErrNone );
       
   175             break;
       
   176             }
       
   177       	case EPostcardInsertImageCreateBitmap:
       
   178       		{
       
   179       		DoCreateBitmapL( );
       
   180       		break;	
       
   181       		}
       
   182         case EPostcardInsertImageFinished:
       
   183             {
       
   184 		    UnInitL( );
       
   185             iObserver.PostcardOperationEvent(
       
   186                 EPostcardOperationInsertImage,
       
   187                 EPostcardOperationComplete ); 
       
   188             break;
       
   189             }
       
   190         default:
       
   191 		    UnInitL( );
       
   192             iObserver.PostcardOperationEvent(
       
   193                 EPostcardOperationInsertImage,
       
   194                 EPostcardOperationError ); 
       
   195             break;
       
   196         }
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // DoCreateAttaL
       
   201 // ---------------------------------------------------------
       
   202 void CPostcardOperationInsertImage::DoCreateAttaL( )
       
   203     {
       
   204     CMsvAttachment* attachment = CMsvAttachment::NewL( CMsvAttachment::EMsvFile );
       
   205     CleanupStack::PushL( attachment );
       
   206     
       
   207 	if( iImage )
       
   208 		{ // Remove the old image
       
   209 	 	TInt oldImage = TMsvIdToIndexL( iImage );
       
   210 		iStore->AttachmentManagerExtensionsL( ).RemoveAttachmentL( oldImage );
       
   211 		}
       
   212 
       
   213 	TSize size = iInfo->Dimensions( );
       
   214 
       
   215 	TInt width = size.iWidth;
       
   216 	TInt height = size.iHeight;
       
   217 	if( height > width )
       
   218 		{
       
   219 		width = size.iHeight;
       
   220 		height = size.iWidth;
       
   221 		}
       
   222 	
       
   223 	TBool needsScaling = EFalse;
       
   224 	TBool isJpeg = EFalse;
       
   225 	
       
   226     if( iInfo->MimeType( ).CompareF( KMsgMimeImageJpeg ) == 0 )
       
   227 		{
       
   228 		isJpeg = ETrue;
       
   229 		}
       
   230 	else if( iInfo->MimeType( ).CompareF( KMsgMimeImagePng ) == 0 )
       
   231         { // PNG always needs to be converted into jpeg
       
   232         needsScaling = ETrue;
       
   233         }
       
   234         
       
   235 	if( needsScaling )   
       
   236 		{
       
   237         // Needs converting first -> create just an empty RFile for now
       
   238    	    iCompressFile = new ( ELeave ) RFile;
       
   239 	    attachment->SetMimeTypeL( KMsgMimeImageJpeg );
       
   240 	    attachment->SetAttachmentNameL( KPostcardFilenameScaled );
       
   241         iManager->CreateAttachmentL( KPostcardFilenameScaled, *iCompressFile, attachment, iStatus );
       
   242         iLaunchState = EPostcardInsertProcessImage;
       
   243 	    iCompressAtta = attachment;
       
   244 
       
   245         iFlags |= EPostcardInsertImageRequestActive;
       
   246         }
       
   247     else
       
   248         {
       
   249         TBool needsCompress = NeedsCompress( ); 
       
   250         TParsePtrC parse( iInfo->FullFilePath() );
       
   251 
       
   252 		if( isJpeg )
       
   253 			{
       
   254 		    attachment->SetMimeTypeL( KMsgMimeImageJpeg );
       
   255 			}
       
   256 		else
       
   257 			{
       
   258 		    attachment->SetMimeTypeL( KMsgMimeImageGif );
       
   259 			}
       
   260 
       
   261         if( needsCompress )    
       
   262         	{ // Compression needed -> create just an empty attachment for now
       
   263     	    iCompressFile = new ( ELeave ) RFile;
       
   264     	    attachment->SetAttachmentNameL( KPostcardFilenameCompressed );
       
   265             iManager->CreateAttachmentL( KPostcardFilenameCompressed, *iCompressFile, attachment, iStatus );
       
   266             iFlags |= EPostcardInsertImageRequestActive;
       
   267 	        iLaunchState = EPostcardInsertProcessImage;
       
   268 			iCompressAtta = attachment;
       
   269         	}
       
   270         else
       
   271         	{
       
   272         	if( isJpeg )
       
   273         		{
       
   274 	    	    attachment->SetAttachmentNameL( KPostcardFilenameNoOp );
       
   275         		}
       
   276         	else
       
   277         		{
       
   278 	    	    attachment->SetAttachmentNameL( KPostcardFilenameNoOpGif );
       
   279         		}
       
   280             iManager->AddAttachmentL( iInfo->FullFilePath( ), attachment, iStatus );
       
   281             iFlags |= EPostcardInsertImageRequestActive;
       
   282             iLaunchState = EPostcardInsertImageFinalizeSave;
       
   283             iNoOpAtta = attachment;
       
   284         	}
       
   285         }
       
   286 
       
   287     CleanupStack::Pop( attachment );
       
   288     SetActive();    	    			
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------
       
   292 // DoStartProcessImageL
       
   293 // ---------------------------------------------------------
       
   294 void CPostcardOperationInsertImage::DoStartProcessImageL( )
       
   295 	{
       
   296 	if( !iImageProcessor )
       
   297 	    {
       
   298         iImageProcessor = new (ELeave) CUniImageProcessor( this );	    
       
   299 	    }
       
   300     RFile tempFile;
       
   301 	User::LeaveIfError( tempFile.Open( iFs, iInfo->FullFilePath( ), EFileRead | EFileShareAny ) );
       
   302     CleanupClosePushL( tempFile );    
       
   303 	
       
   304 	TSize maxDimensions = iDocument.MaxImageDimensions( );
       
   305 
       
   306 	TSize origDimensions = static_cast<CMsgImageInfo*>(iInfo)->Dimensions( );
       
   307 
       
   308 	if( origDimensions.iWidth < origDimensions.iHeight )
       
   309 		{ // It's portrait so swap max dimensions
       
   310 		origDimensions = maxDimensions;
       
   311 		maxDimensions.iWidth = origDimensions.iHeight;
       
   312 		maxDimensions.iHeight = origDimensions.iWidth;
       
   313 		}
       
   314 
       
   315     iImageProcessor->ProcessImageL( 	tempFile, 
       
   316     									*iCompressFile, 
       
   317     									maxDimensions,
       
   318     									KMsgMimeImageJpeg, 
       
   319     									ETrue,
       
   320     									iDocument.MaxImageSize( ) );
       
   321     CleanupStack::PopAndDestroy( &tempFile ); // tempFile
       
   322 	}
       
   323 
       
   324 // ---------------------------------------------------------
       
   325 // DoFinalizeSaveL
       
   326 // ---------------------------------------------------------
       
   327 void CPostcardOperationInsertImage::DoFinalizeSaveL( )
       
   328     {
       
   329     
       
   330     iHeaders = CMsvMimeHeaders::NewL( );
       
   331     
       
   332     CMsvAttachment* atta = iNoOpAtta;
       
   333     if( iScaleAtta )
       
   334     	{
       
   335     	atta = iScaleAtta;
       
   336     	}
       
   337     
       
   338     if( iCompressAtta )
       
   339     	{
       
   340 		atta = iCompressAtta;
       
   341     	}
       
   342     
       
   343     if( atta )
       
   344     	{
       
   345 		iHeaders->SetContentLocationL( atta->AttachmentName( ) );
       
   346 		iHeaders->SetContentTypeL( atta->MimeType( ) );
       
   347 	    iHeaders->StoreL( *atta );
       
   348     	}
       
   349 	
       
   350     // Lets remove the flag just in case
       
   351     iFlags &= ~EPostcardInsertImageConvertingImage;
       
   352     
       
   353 	iStore->Commit( ); // Is this needed?
       
   354     
       
   355     CheckAttasL( );
       
   356     
       
   357     iCommit = ETrue;
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------
       
   361 // DoCreateBitmapL
       
   362 // ---------------------------------------------------------
       
   363 void CPostcardOperationInsertImage::DoCreateBitmapL( )
       
   364 	{
       
   365 	RFile imageFile = iManager->GetAttachmentFileL( iImage );
       
   366 	CleanupClosePushL( imageFile );
       
   367 	
       
   368 	// The scaling for screen bitmap is started here
       
   369 
       
   370     iSourceImage = IHLImageFactory::OpenFileImageL( imageFile );
       
   371     iDestinationBitmap = IHLBitmap::CreateL();
       
   372     
       
   373     iImageHandler = IHLViewerFactory::CreateImageViewerL( PostcardLaf::Image( ).Size( ), 
       
   374                                                     *iSourceImage, 
       
   375                                                     *iDestinationBitmap, 
       
   376                                                     *this, 
       
   377                                                     TUint32( 0 ) );
       
   378     User::LeaveIfError( iImageHandler->SetSourceRectPosition( TPoint( 0, 0 ) ) );
       
   379 
       
   380     CleanupStack::PopAndDestroy( &imageFile );   // imageFile
       
   381 	}
       
   382 
       
   383 // ---------------------------------------------------------
       
   384 // NeedsCompress
       
   385 // ---------------------------------------------------------
       
   386 TBool CPostcardOperationInsertImage::NeedsCompress( )
       
   387     {
       
   388     if ( iInfo->FileSize( ) > iDocument.MaxMessageSize( ) )
       
   389         {
       
   390         return ETrue;
       
   391         }
       
   392     return EFalse;
       
   393     }
       
   394 
       
   395 // ---------------------------------------------------------
       
   396 // ImageProcessingReady
       
   397 // ---------------------------------------------------------
       
   398 void CPostcardOperationInsertImage::ImageProcessingReady( TSize aBitmapSize, TInt aFileSize, TBool /*aCompressed*/ )
       
   399 	{
       
   400     if( iCompressFile )
       
   401 	    {
       
   402         iCompressFile->Close();
       
   403         delete iCompressFile;
       
   404         iCompressFile = NULL;
       
   405 	    }
       
   406 
       
   407 	TBool imageFits = EFalse;
       
   408 	TSize maxDimensions = iDocument.MaxImageDimensions( );
       
   409 	if( aBitmapSize.iWidth <= maxDimensions.iWidth && 
       
   410 		aBitmapSize.iHeight <= maxDimensions.iHeight )
       
   411 		{
       
   412 		imageFits = ETrue;
       
   413 		}
       
   414 	else if( aBitmapSize.iWidth <= maxDimensions.iHeight && 
       
   415 		aBitmapSize.iHeight <= maxDimensions.iWidth )
       
   416 		{
       
   417 		imageFits = ETrue;
       
   418 		}
       
   419 
       
   420 	if( aBitmapSize.iWidth == 0 || !imageFits ||
       
   421 	    aFileSize > iDocument.MaxImageSize( ) )
       
   422 		{ // The process was not successful
       
   423         SetError( R_POSTCARD_PROCESS_NOT_SUCCESSFUL );
       
   424         iLaunchState = EPostcardInsertImageError;
       
   425         CompleteSelf( -1 );
       
   426         return;
       
   427 		}
       
   428 
       
   429 	iLaunchState = EPostcardInsertImageFinalizeSave;
       
   430     CompleteSelf( KErrNone );
       
   431 	}
       
   432 
       
   433 // ---------------------------------------------------------
       
   434 // CPostcardOperationInsertImage::DoRenameAttaL
       
   435 // ---------------------------------------------------------
       
   436 void CPostcardOperationInsertImage::DoRenameAttaL( )
       
   437 	{
       
   438 	if( iNoOpAtta )
       
   439 		{
       
   440 	    TInt count = iManager->AttachmentCount( );
       
   441 	    CMsvAttachment* atta = NULL;
       
   442 	    for(TInt a = 0; a<count; a++)
       
   443 	        {
       
   444 	        atta = iManager->GetAttachmentInfoL( a );
       
   445 	        CleanupStack::PushL( atta );
       
   446 			if( iScaleAtta->FilePath( ).CompareF( atta->FilePath( ) ) == 0 )
       
   447 				{
       
   448 				iManager->RenameAttachmentL( a, iNoOpAtta->AttachmentName( ), iStatus );			
       
   449 				SetActive( );
       
   450 				break;
       
   451 				}
       
   452 	        }
       
   453 		}
       
   454     iLaunchState = EPostcardInsertImageFinalizeSave;
       
   455 	}
       
   456 
       
   457 // ---------------------------------------------------------
       
   458 // CPostcardOperationInsertImage::ConstructL
       
   459 // ---------------------------------------------------------
       
   460 void CPostcardOperationInsertImage::ConstructL( )
       
   461     {
       
   462     }
       
   463 
       
   464 // ---------------------------------------------------------
       
   465 // CPostcardOperationInsertImage::ViewerBitmapChangedL
       
   466 // ---------------------------------------------------------
       
   467 void CPostcardOperationInsertImage::ViewerBitmapChangedL()
       
   468     {
       
   469     CFbsBitmap* bitmap = new (ELeave) CFbsBitmap;
       
   470     CFbsBitmap* mask = NULL;
       
   471     bitmap->Duplicate( iDestinationBitmap->Bitmap().Handle( ) );
       
   472     
       
   473     if ( iDestinationBitmap->HasMask() )
       
   474         {
       
   475         mask = new (ELeave) CFbsBitmap;
       
   476         mask->Duplicate( iDestinationBitmap->Mask().Handle( ) );
       
   477         }
       
   478     
       
   479     delete iDestinationBitmap;
       
   480     iDestinationBitmap = NULL;
       
   481     
       
   482     delete iSourceImage;
       
   483     iSourceImage = NULL;
       
   484     
       
   485     delete iImageHandler;
       
   486     iImageHandler = NULL;
       
   487     
       
   488     iLaunchState = EPostcardInsertImageFinished;
       
   489         
       
   490     CGulIcon* icon = NULL;
       
   491     TRAPD( err, icon = CGulIcon::NewL( bitmap, mask ) );
       
   492 
       
   493     if( err )
       
   494         { // Lets nullify it if there was an error
       
   495         icon = NULL;
       
   496         }
       
   497         
       
   498 	iAppUi.SetImage( icon );
       
   499 
       
   500 	CompleteSelf( KErrNone );        
       
   501         
       
   502     }
       
   503 
       
   504 // ---------------------------------------------------------
       
   505 // CPostcardOperationInsertImage::ViewerError
       
   506 // ---------------------------------------------------------
       
   507 void CPostcardOperationInsertImage::ViewerError( TInt /*aError*/ )
       
   508     {
       
   509     delete iDestinationBitmap;
       
   510     iDestinationBitmap = NULL;
       
   511     
       
   512     delete iSourceImage;
       
   513     iSourceImage = NULL;
       
   514     
       
   515     delete iImageHandler;
       
   516     iImageHandler = NULL;
       
   517 
       
   518 	SetError( R_POSTCARD_FORMAT_NOT_SUPPORTED );
       
   519     iLaunchState = EPostcardInsertImageError;
       
   520 	CompleteSelf( -3 );
       
   521 	return;
       
   522     }
       
   523     
       
   524 // ---------------------------------------------------------
       
   525 // CPostcardOperationInsertImage::DoCancel
       
   526 // ---------------------------------------------------------
       
   527 void CPostcardOperationInsertImage::DoCancel( )
       
   528     {
       
   529     if( iManager )
       
   530         {
       
   531         if( iFlags & EPostcardInsertImageRequestActive )
       
   532             {
       
   533             iManager->CancelRequest();                
       
   534             }
       
   535         }
       
   536     }
       
   537 
       
   538 // ---------------------------------------------------------
       
   539 // CPostcardOperationInsertImage::RunL
       
   540 // ---------------------------------------------------------
       
   541 void CPostcardOperationInsertImage::RunL( )
       
   542     {
       
   543     DoLaunchStepL( );
       
   544     }
       
   545 
       
   546 // ---------------------------------------------------------
       
   547 // RunError
       
   548 // ---------------------------------------------------------
       
   549 TInt CPostcardOperationInsertImage::RunError( TInt aError )
       
   550     {
       
   551     iLaunchState = -1;
       
   552     SetError( aError );
       
   553     iObserver.PostcardOperationEvent(
       
   554         EPostcardOperationInsertImage,
       
   555         EPostcardOperationError );     
       
   556     return KErrNone;
       
   557     }
       
   558 
       
   559 
       
   560 // EOF