imageeditor/EditorProvider/src/JpegRotatorHelper.cpp
changeset 1 edfc90759b9f
equal deleted inserted replaced
0:57d4cdd99204 1:edfc90759b9f
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description: 
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include    "JpegRotatorHelper.h"
       
    23 #include    "JpegRotator.h"
       
    24 #include    "ImageEditorUtils.h"
       
    25 #include    <e32panic.h>
       
    26 #include    <bautils.h>
       
    27 #include    <ImageEditorProviderInternal.rsg>
       
    28 
       
    29 //  debug log
       
    30 #include <imageeditordebugutils.h>
       
    31 _LIT(KEditorServiceProviderLogFile,"EditorServiceProvider.log");
       
    32 
       
    33 // ================= MEMBER FUNCTIONS =======================
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // Default constructor
       
    37 // 
       
    38 // ---------------------------------------------------------
       
    39 //
       
    40 CJpegRotatorHelper::CJpegRotatorHelper(
       
    41 	RFs& aFsSession, 
       
    42 	MJpegRotatorHelperObserver& aObserver) :
       
    43 	CActive (EPriorityIdle), 
       
    44 	iFsSession (aFsSession), 
       
    45 	iObserver (aObserver)
       
    46 	{
       
    47 	CActiveScheduler::Add(this);
       
    48 	}
       
    49 
       
    50 // ---------------------------------------------------------
       
    51 // Destructor
       
    52 // 
       
    53 // ---------------------------------------------------------
       
    54 //
       
    55 CJpegRotatorHelper::~CJpegRotatorHelper()
       
    56 	{
       
    57 	Cancel();
       
    58 	delete iJpegRotator;
       
    59 	}
       
    60 
       
    61 // ---------------------------------------------------------
       
    62 // Rotate
       
    63 // 
       
    64 // ---------------------------------------------------------
       
    65 //
       
    66 void CJpegRotatorHelper::StartRotateL (
       
    67 	const CDesCArray* aSourceFileList,
       
    68 	CDesCArray* aTargetFileList,
       
    69 	CJpegRotator::TRotationMode aMode,
       
    70 	TBool aPreserveOriginalFiles )
       
    71 	{
       
    72 	LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::StartRotateL: In");
       
    73 
       
    74 	__ASSERT_DEBUG(!IsActive(), User::Panic(_L("CJpegRotatorHelper"),EReqAlreadyActive));
       
    75 
       
    76 	// ownership not taken
       
    77 	iTargetFileList = aTargetFileList;
       
    78 	iSourceFileList = aSourceFileList;
       
    79 	iMode = aMode;
       
    80 	iPreserveOriginalFiles = aPreserveOriginalFiles;
       
    81 	
       
    82 	// instantiate JpegRotator
       
    83 	delete iJpegRotator;
       
    84 	iJpegRotator = NULL;
       
    85 	iJpegRotator = CJpegRotator::NewL( iFsSession );
       
    86 
       
    87 	// Generate names for the rotated images
       
    88 	for( TInt i = 0; i < iSourceFileList->MdcaCount(); i++ )
       
    89 		{
       
    90 		TPtrC source( iSourceFileList->MdcaPoint(i) );
       
    91 		
       
    92 		TFileName target;
       
    93 		if (!aPreserveOriginalFiles)
       
    94 			{
       
    95 			LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::StartRotateL: Not preserve original files");
       
    96 			// If we intend to replace the original file, the generated
       
    97 			// file must be on the same drive.
       
    98 			TInt drive;
       
    99 			User::LeaveIfError( RFs::CharToDrive(source[0], drive) );
       
   100 			if (EDriveC == drive)
       
   101 				{
       
   102 				User::LeaveIfError( ImageEditorUtils::GenerateNewDocumentNameL( iFsSession, source, target, NULL, iTargetFileList, ImageEditorUtils::ESelectPhone) );
       
   103 				}
       
   104 			else if (EDriveE == drive)
       
   105 				{
       
   106 				User::LeaveIfError( ImageEditorUtils::GenerateNewDocumentNameL( iFsSession, source, target, NULL, iTargetFileList, ImageEditorUtils::ESelectMmc) );
       
   107 				}
       
   108 			else
       
   109 				{
       
   110 				User::Leave(KErrArgument);
       
   111 				}
       
   112 			}
       
   113 		else
       
   114 			{
       
   115 			LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::StartRotateL: Preserve original files");			
       
   116 			User::LeaveIfError( ImageEditorUtils::GenerateNewDocumentNameL( iFsSession, source, target, NULL, iTargetFileList) );
       
   117 			}
       
   118 		iTargetFileList->AppendL( target );
       
   119 		}
       
   120 
       
   121 	// Start waiting for rotator completion
       
   122 	iStatus = KRequestPending;
       
   123 	SetActive();
       
   124 
       
   125 	// Start rotating the first file
       
   126 	LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::StartRotateL: Starting rotate...");
       
   127 	iCounter = 0;
       
   128 	iJpegRotator->RotateImageL (iStatus, iSourceFileList->MdcaPoint(iCounter), iTargetFileList->MdcaPoint(iCounter), aMode );
       
   129 
       
   130 	// Launch wait dialog
       
   131 	LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::StartRotateL: Launching wait note");
       
   132 	iWaitDialog = new (ELeave) CAknWaitDialog (reinterpret_cast<CEikDialog**>(&iWaitDialog), ETrue );
       
   133 	iWaitDialog->PrepareLC( R_PROVIDER_WAIT_DIALOG );
       
   134 	iWaitDialog->SetCallback( this );
       
   135 	iWaitDialog->RunLD();
       
   136 
       
   137 	LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::StartRotateL: Out");
       
   138 	}
       
   139 
       
   140 // ---------------------------------------------------------
       
   141 // CJpegRotatorHelper::FinalizeRotatedFileL
       
   142 // 
       
   143 // ---------------------------------------------------------
       
   144 //
       
   145 void CJpegRotatorHelper::FinalizeRotatedFileL( TInt aCurrentFileIndex )
       
   146 	{
       
   147 	LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::FinalizeRotatedFileL");
       
   148 
       
   149 #ifdef FILE_TIME_STAMP_UPDATE
       
   150 	// Set the timestamp of the saved file to original file's timestamp + 1 second.
       
   151 	// The idea is to make the original and edited images appear next to each other.
       
   152 	TEntry original;
       
   153 	TInt err = iFsSession.Entry( iSourceFileList->MdcaPoint(aCurrentFileIndex), original );
       
   154 	if (KErrNone == err)
       
   155 		{
       
   156 		TTime newTime = original.iModified;
       
   157 
       
   158 		CFileMan* fileMan = CFileMan::NewL( iFsSession );
       
   159 		CleanupStack::PushL (fileMan);
       
   160 		fileMan->Attribs( iTargetFileList->MdcaPoint(aCurrentFileIndex), 0, 0, newTime ); // do not set or clear anything, mofify time 
       
   161 		CleanupStack::PopAndDestroy (fileMan);
       
   162 		}
       
   163 #endif
       
   164 
       
   165 	if (!iPreserveOriginalFiles)
       
   166 		{
       
   167 		LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::FinalizeRotatedFileL 2");
       
   168 		// Delete the source file and rename the temporary 
       
   169 		// target file to the source file name
       
   170 		TFileName sourceFile( iSourceFileList->MdcaPoint(aCurrentFileIndex) );
       
   171 		TInt err = BaflUtils::DeleteFile( iFsSession, sourceFile );
       
   172 		err = BaflUtils::RenameFile (
       
   173 			iFsSession, 
       
   174 			iTargetFileList->MdcaPoint(aCurrentFileIndex),
       
   175 			sourceFile );
       
   176 		User::LeaveIfError( err );
       
   177 
       
   178 		// Replace the file name in the target file list
       
   179 		iTargetFileList->Delete(aCurrentFileIndex);
       
   180 		iTargetFileList->InsertL(aCurrentFileIndex, sourceFile);
       
   181 		}
       
   182 	}
       
   183 
       
   184 // ---------------------------------------------------------
       
   185 // CJpegRotatorHelper::RunL
       
   186 // 
       
   187 // ---------------------------------------------------------
       
   188 //
       
   189 void CJpegRotatorHelper::RunL()
       
   190 	{
       
   191     LOGFMT( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL (status: %d)", iStatus.Int());
       
   192 
       
   193 	if( KErrNone == iStatus.Int() )
       
   194 		{
       
   195 		FinalizeRotatedFileL( iCounter );
       
   196 
       
   197 		if( (iCounter + 1) < iSourceFileList->MdcaCount() )
       
   198 			{
       
   199 			// Start rotating the next file
       
   200 			LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: Starting rotate next...");
       
   201 
       
   202 			iStatus = KRequestPending;
       
   203 			SetActive();
       
   204 
       
   205 			iCounter++;
       
   206 			iJpegRotator->RotateImageL (iStatus, iSourceFileList->MdcaPoint(iCounter), iTargetFileList->MdcaPoint(iCounter), (CJpegRotator::TRotationMode)iMode );
       
   207 			}
       
   208 		else
       
   209 			{
       
   210 			// All files rotated. Finish operation.
       
   211 			delete iJpegRotator;
       
   212 			iJpegRotator = NULL;
       
   213 			iTargetFileList = NULL;
       
   214 
       
   215 			// Dismiss wait dialog
       
   216 		    if (iWaitDialog)
       
   217 				{
       
   218 				LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: deleting wait note" );
       
   219 		        TRAP_IGNORE( iWaitDialog->ProcessFinishedL() );
       
   220 				LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: process finished" );
       
   221 				delete iWaitDialog;
       
   222 				LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: dialog deleted" );
       
   223 				iWaitDialog = NULL;
       
   224 				}
       
   225 
       
   226 			// Notify completion to observer
       
   227 			iObserver.RotateOperationReadyL ( iStatus.Int() );
       
   228 			}
       
   229 		}
       
   230 	else
       
   231 		{
       
   232 		
       
   233 		// Try delete failed target file
       
   234 		BaflUtils::DeleteFile( iFsSession, iTargetFileList->MdcaPoint(iCounter));
       
   235 		
       
   236 		delete iJpegRotator;
       
   237 		iJpegRotator = NULL;
       
   238 		iTargetFileList = NULL; // not owned
       
   239 
       
   240         // Dismiss wait dialog
       
   241 	    if (iWaitDialog)
       
   242 			{
       
   243 			LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: deleting wait note" );
       
   244 	        TRAP_IGNORE( iWaitDialog->ProcessFinishedL() );
       
   245 			LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: process finished" );
       
   246 			delete iWaitDialog;
       
   247 			LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::RunL: dialog deleted" );
       
   248 			iWaitDialog = NULL;
       
   249 			}
       
   250 
       
   251 		iObserver.RotateOperationReadyL ( iStatus.Int() );
       
   252 
       
   253 		}
       
   254 	}
       
   255 
       
   256 // ----------------------------------------------------
       
   257 // MProgressDialogCallBack
       
   258 // 
       
   259 // ----------------------------------------------------
       
   260 //
       
   261 void CJpegRotatorHelper::DialogDismissedL( TInt aButtonId )
       
   262 	{
       
   263 	LOGFMT(KEditorServiceProviderLogFile, "CJpegRotatorHelper::DialogDismissedL( %d )", aButtonId);
       
   264 	
       
   265 	if( EAknSoftkeyCancel == aButtonId )
       
   266 		{
       
   267 		Cancel();
       
   268 		}
       
   269 	}
       
   270 
       
   271 // ---------------------------------------------------------
       
   272 // CJpegRotatorHelper::DoCancel()
       
   273 // 
       
   274 // ---------------------------------------------------------
       
   275 //
       
   276 void CJpegRotatorHelper::DoCancel()
       
   277 	{
       
   278 	LOG( KEditorServiceProviderLogFile, "CJpegRotatorHelper::DoCancel");
       
   279 
       
   280 	iJpegRotator->Cancel();
       
   281 
       
   282     // Try delete failed target file
       
   283 	BaflUtils::DeleteFile( iFsSession, iTargetFileList->MdcaPoint(iCounter));
       
   284 
       
   285 
       
   286 	if( KRequestPending == iStatus.Int() )
       
   287 		{
       
   288 		TRequestStatus* status = &iStatus;
       
   289 		User::RequestComplete(status, KErrCancel);
       
   290 		}
       
   291 	}
       
   292 
       
   293 // End of File  
       
   294