imageeditor/imageeditorApp/src/ImageEditorAppUi.cpp
changeset 1 edfc90759b9f
child 8 18b321db4884
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 * Image Editor AppUI class.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 //	INCLUDE FILES
       
    22 #include <fbs.h>
       
    23 #include <f32file.h>
       
    24 #include <badesca.h> 
       
    25 #include <bautils.h> 
       
    26 #include <e32math.h>
       
    27  
       
    28 #include <aknnotifystd.h> 
       
    29 #include <aknwaitdialog.h> 
       
    30 #include <AknGlobalNote.h>
       
    31 #include <eikprogi.h>
       
    32 #include <sendui.h>
       
    33 #include <hlplch.h>
       
    34 #include <aknnotewrappers.h> 
       
    35 #include <CMessageData.h>
       
    36 #include <e32property.h>
       
    37 
       
    38 #include <AknDlgShut.h>
       
    39 #include <CAknFileNamePromptDialog.h> 
       
    40 #include <CAknMemorySelectionDialog.h>
       
    41 #include <PathInfo.h> 
       
    42 #include <csxhelp/sie.hlp.hrh>
       
    43 
       
    44 #ifdef VERBOSE
       
    45 #include <eikenv.h>
       
    46 #endif
       
    47 
       
    48 #include <AknCommonDialogsDynMem.h> 
       
    49 #include <CAknMemorySelectionDialogMultiDrive.h> 
       
    50 
       
    51 #include <ImageEditor.rsg>
       
    52 #include "ImageEditorUI.hrh"
       
    53 #include "ImageEditorPanics.h"
       
    54 
       
    55 #include "imageeditoruids.hrh"
       
    56 #include "commondefs.h"
       
    57 //#include "platform_security_literals.hrh"
       
    58 
       
    59 #include "ImageEditorApp.h"
       
    60 #include "ImageEditorAppUi.h"
       
    61 #include "ImageEditorUIView.h"
       
    62 #include "ImageEditorUI.hrh"
       
    63 #include "ImageEditorError.h"
       
    64 #include "PluginInfo.h"
       
    65 
       
    66 #include "ImageEditorPluginLocator.h"
       
    67 #include "ImageEditorPluginManager.h"
       
    68 #include "ImageEditorImageController.h"
       
    69 
       
    70 #include "ImageEditorUtils.h"
       
    71 #include "ResolutionUtil.h"
       
    72 #include "Callback.h"
       
    73 
       
    74 
       
    75 //	CONSTANTS
       
    76 
       
    77 //  Panic category
       
    78 _LIT(KComponentName, "ImageEditorAppUi");
       
    79 
       
    80 const TInt	KObConstructCallBackID          = 1;
       
    81 const TInt	KObInitPluginID				    = 1000;
       
    82 const TInt	KObReturnFromPluginID           = 1001;
       
    83 const TInt  KObCancelCallbackID				= 1002;
       
    84 const TInt  KObUndoDCallbackID				= 1003;
       
    85 const TInt  KObSendCallbackID               = 1004;
       
    86 const TInt  KObPrintCallbackID              = 1005;
       
    87 const TInt  KObSaveCallbackID				= 1006;
       
    88 
       
    89 //=============================================================================
       
    90 CImageEditorAppUi::CImageEditorAppUi() :
       
    91     //  Initialize application flags and variables
       
    92 	iDocumentName(),
       
    93 	iSourceFileName(),
       
    94     iImageSaved             (EFalse),
       
    95     iForeground             (ETrue),
       
    96     iExitFromApp            (EFalse),
       
    97     iSaveOnExit             (ETrue),
       
    98     iSysExit                (EFalse),
       
    99     iBusy                   (ETrue),
       
   100     iOrientationChanged     (EFalse),
       
   101     iSaving                 (EFalse),
       
   102     iSending                (EFalse),
       
   103     iPrinting               (EFalse),
       
   104     iCancelling				(EFalse),
       
   105     iFullScreen             (EFalse),
       
   106     iPluginsScanned         (EFalse),
       
   107     iImageLoaded            (EFalse),
       
   108     iEditorReady            (EFalse),
       
   109     iCancelTextInputActive  (EFalse),
       
   110     iProcessPriorityAltered (EFalse)
       
   111 {
       
   112 
       
   113 }
       
   114 
       
   115 //=============================================================================
       
   116 void CImageEditorAppUi::ConstructL()
       
   117 {
       
   118 	LOG(KImageEditorLogFile, "CImageEditorAppUi::ConstructL");
       
   119 
       
   120 	//	Initialize UI with standard values, read application resource file,
       
   121 	//	read default document
       
   122 
       
   123 #ifdef LANDSCAPE_ONLY
       
   124     BaseConstructL( EAppOrientationLandscape | EAknEnableSkin | EAknEnableMSK  );
       
   125 #else 
       
   126 #ifdef LANDSCAPE_SUPPORT
       
   127     BaseConstructL( EAppOrientationAutomatic|EAknEnableSkin | EAknEnableMSK );
       
   128 #else
       
   129     BaseConstructL( EAknEnableSkin | EAknEnableMSK );
       
   130 #endif
       
   131 #endif // LANDSCAPE_SUPPORT
       
   132 
       
   133     //  Set busy
       
   134     SetBusy();
       
   135 
       
   136     //	Create main view  
       
   137     CImageEditorUIView * view = new (ELeave) CImageEditorUIView;
       
   138     CleanupStack::PushL (view);
       
   139     view->ConstructL();
       
   140     view->ConstructMenuAndCbaEarlyL();
       
   141 	iEditorView = view;
       
   142     AddViewL (view); // transfers ownership
       
   143     CleanupStack::Pop();	// view
       
   144 	LOG (KImageEditorLogFile, "CImageEditorAppUi: Main view created");
       
   145 
       
   146     //  Activate view
       
   147 	ActivateLocalViewL  ( iEditorView->Id() );
       
   148     LOG(KImageEditorLogFile, "CImageEditorAppUi: View activated");
       
   149 
       
   150     //  Set default navi pane text
       
   151     ClearTitlePaneTextL();
       
   152     ClearNaviPaneTextL();
       
   153 
       
   154     //	Create CObCallback instance
       
   155 	iConstructionState = EConstructionStateAlloc;
       
   156 	iCallback = CObCallback::NewL ( (MObCallbackMethod *)this );
       
   157     iCallback->DoCallback (KObConstructCallBackID);
       
   158    
       
   159     // Volume key observer instance
       
   160     iVolumeKeyObserver = CImageEditorRemConObserver::NewL( *this );
       
   161     
       
   162 #ifdef S60_31_VOLUME_KEYS
       
   163 	iMGXRemConTarget = CMGXRemConTarget::NewL( this );
       
   164 #endif
       
   165 }
       
   166 
       
   167 //=============================================================================
       
   168 CImageEditorAppUi::~CImageEditorAppUi()
       
   169 {
       
   170     LOG(KImageEditorLogFile, "CImageEditorAppUi::~CImageEditorAppUi() starting...");
       
   171 
       
   172 	//KillWaitNote();
       
   173 	if (iWaitNote != NULL)
       
   174 		{
       
   175         delete iWaitNote;
       
   176         iWaitNote = NULL;
       
   177 		}
       
   178     
       
   179     delete iLocator;
       
   180     delete iImageController;
       
   181     delete iEditorManager;
       
   182 	delete iSendAppUi;
       
   183 	delete iCallback;
       
   184 	iSourceImageMgAlbumIdList.Close();
       
   185 
       
   186     iEditorView = NULL;
       
   187     
       
   188 	iFile.Close();
       
   189     
       
   190 #ifdef S60_31_VOLUME_KEYS
       
   191 	delete iMGXRemConTarget;
       
   192 #endif
       
   193 	
       
   194 	// deleted by CCoeEnv
       
   195 	iResolutionUtil = NULL;
       
   196 	delete iVolumeKeyObserver;
       
   197 
       
   198     LOG(KImageEditorLogFile, "CImageEditorAppUi: AppUi deleted");
       
   199 }
       
   200 
       
   201 //=============================================================================
       
   202 void CImageEditorAppUi::OperationReadyL ( 
       
   203 	TOperationCode		aOpId,
       
   204 	TInt				aError 
       
   205 	)
       
   206 {
       
   207     LOGFMT2(KImageEditorLogFile, "CImageEditorAppUi:OperationReadyL (aOpId:%d, aError:%d)", (TInt)aOpId, aError);
       
   208 
       
   209     //	Kill wait note
       
   210     KillWaitNote();
       
   211 
       
   212     //	Handle error
       
   213 	if ( aError != KErrNone )
       
   214 	{
       
   215 		switch ( aOpId ) 
       
   216 		{
       
   217 			case EOperationCodeLoad:
       
   218 			case EOperationCodeBlockLoad:
       
   219 			case EOperationCodeSearch:
       
   220             {
       
   221 
       
   222                 if (KErrCancel == aError)
       
   223                 {
       
   224                     // Loading image was cancelled. 
       
   225                     User::Leave (KSIEEErrorOkToExit);
       
   226                 }
       
   227                 else
       
   228                 {
       
   229                     User::Leave (KSIEEOpenFile);
       
   230                 }
       
   231                 break;
       
   232             }
       
   233             case EOperationCodeSave:
       
   234             case EOperationCodeBlockSave:
       
   235             {
       
   236                 LOG(KImageEditorLogFile, "CImageEditorAppUi: Save cancelled");
       
   237 #ifdef VERBOSE
       
   238                 LOGFMT3(KImageEditorLogFile, "\tiSourceFileName: %S, iDocumentName: %S, iTempSaveFileName: %S", &iSourceFileName, &iDocumentName, &iTempSaveFileName);
       
   239 #endif
       
   240 
       
   241                 iSaving = EFalse;
       
   242 
       
   243                 // delete the temporary files
       
   244                 DeleteTempFiles();
       
   245 
       
   246                 // Reset bitmap sink and screen size
       
   247                 UpdateScreenRectL();
       
   248                 iOrientationChanged = EFalse;
       
   249 
       
   250 
       
   251                 if (!iExitFromApp)
       
   252                 {
       
   253     
       
   254                     if (iSending)
       
   255                     {
       
   256                         // Restore document name
       
   257                         iEditorManager->SetImageName (iDocumentName);
       
   258                         iSending = EFalse;
       
   259                     }
       
   260                     if (KErrCancel == aError)
       
   261                     {
       
   262                         // KErrCancel means user cancel, not an error.
       
   263                         ResetBusy();
       
   264                     }
       
   265                     else
       
   266                     {
       
   267                         // Leave in case of error.
       
   268                         User::Leave (KSIEESaveFile);
       
   269                     }
       
   270                 }
       
   271                 else
       
   272                 {
       
   273                 	TInt exit = 0;
       
   274                     if (iSaveOnExit)
       
   275                     {
       
   276 	                    exit = ((CImageEditorUIView *)iEditorView)->LaunchExitWithoutSavingQueryL();
       
   277                     }
       
   278 
       
   279                     if (exit == EImageEditorSoftkeyCmdYes || !iSaveOnExit)
       
   280                     {
       
   281                         // If the image has not been saved before, the reserved target
       
   282                         // file has zero size and can be deleted.
       
   283                         TEntry entry;
       
   284                         TInt err = iEikonEnv->FsSession().Entry( iDocumentName, entry );
       
   285                         if ( err == KErrNone && entry.iSize == 0 )
       
   286                         {
       
   287                             BaflUtils::DeleteFile (iEikonEnv->FsSession(), iDocumentName);
       
   288                         }
       
   289 
       
   290                         // Exit the application
       
   291                         User::Leave (KSIEEErrorOkToExit);
       
   292                     }
       
   293                     else
       
   294                     {
       
   295                         iExitFromApp = EFalse;
       
   296                         ResetBusy();
       
   297                     }
       
   298                 }
       
   299                 SetOrientationL(EAppUiOrientationUnspecified);
       
   300                 break;
       
   301             }
       
   302             default:
       
   303             {
       
   304                 User::Leave (aError);
       
   305                 break;
       
   306             }
       
   307 		}
       
   308 	}
       
   309 	else
       
   310 	{
       
   311 		switch ( aOpId ) 
       
   312 		{
       
   313 
       
   314 			case EOperationCodeLoad:
       
   315 			case EOperationCodeBlockLoad:
       
   316             {
       
   317                 LOG(KImageEditorLogFile, "CImageEditorAppUi: Image loaded");
       
   318 
       
   319                 // Find out whether the source file belongs to any albums
       
   320                 ImageEditorUtils::FindAlbumsForImageFileL (
       
   321                     iSourceImageMgAlbumIdList,
       
   322                     iSourceFileName );
       
   323 
       
   324                 // Generate the name for the saved file
       
   325                 TInt err = ImageEditorUtils::GenerateNewDocumentNameL (
       
   326                     iEikonEnv->FsSession(), 
       
   327                     iSourceFileName, 
       
   328                     iDocumentName,
       
   329                     &iSourceImageMgAlbumIdList );
       
   330 
       
   331 				//	Delete old temp files, which could exist if the exit was not clean
       
   332 				DeleteTempFiles();
       
   333 	
       
   334                 // If there is not enough disk space to save the edited image, show
       
   335                 // error note but continue anyway (user can free up memory before saving)
       
   336                 if (KSIEENotEnoughDiskSpace == err)
       
   337                 {
       
   338                     ShowErrorNoteL (err);
       
   339                 }
       
   340                 else if (KErrNone != err)
       
   341                 {
       
   342                     // Fatal error 
       
   343                     User::Leave (err);
       
   344                 }                
       
   345                 iEditorManager->SetImageName (iDocumentName);
       
   346                 iImageLoaded = ETrue;
       
   347                 InitializeUiItemsL();
       
   348                 
       
   349                 if (!iOrientationChanged)
       
   350                 {
       
   351                     //  Immediately display the loaded image
       
   352                     ((CImageEditorUIView *)iEditorView)->
       
   353                         SetImageL ( iEditorManager->GetPreviewImage() );
       
   354                 }
       
   355                 
       
   356                 ApplicationReadyL();
       
   357 				
       
   358 				// Do not have to keep file open anymore. 
       
   359 				// Fixes problem with Online Printing
       
   360 				iFile.Close();
       
   361 
       
   362                 break;
       
   363             }
       
   364 
       
   365 			case EOperationCodeSave:
       
   366 			case EOperationCodeBlockSave:
       
   367             {
       
   368                 LOG(KImageEditorLogFile, "CImageEditorAppUi: Image saved");
       
   369 #ifdef VERBOSE
       
   370                 LOGFMT3(KImageEditorLogFile, "\tiSourceFileName: %S, iDocumentName: %S, iTempSaveFileName: %S", &iSourceFileName, &iDocumentName, &iTempSaveFileName);
       
   371 #endif
       
   372                 ResetBusy();
       
   373                 iSaving = EFalse;
       
   374 
       
   375 		        RFs & fs = iEikonEnv->FsSession();
       
   376 		        if ( BaflUtils::FileExists (fs, iTempSaveFileName ))
       
   377                 {
       
   378                     // Image has been successfully saved
       
   379                     iImageSaved = ETrue;                                         
       
   380                 
       
   381             		CFileMan* fileMan = CFileMan::NewL( fs );	
       
   382             		CleanupStack::PushL( fileMan );
       
   383             		
       
   384             		// the temporary image file and the target file are located in the same drive
       
   385             	    if ( iTempSaveFileName.Left(1) == iDocumentName.Left(1) )
       
   386             			{
       
   387             			User::LeaveIfError( fileMan->Rename( iTempSaveFileName, iDocumentName ));	
       
   388             			}
       
   389            			// the temporary image file and the target file are not in the same drive
       
   390             		else
       
   391             			{
       
   392             			User::LeaveIfError( fileMan->Move( iTempSaveFileName, iDocumentName ));	
       
   393             			}
       
   394             		CleanupStack::PopAndDestroy(); //fileMan
       
   395                                        
       
   396                     // Source file changes (at least if saved with a new name)
       
   397                     iSourceFileName = iDocumentName;
       
   398                     
       
   399                     // Update title
       
   400                     SetFileNameTitlePaneL(iDocumentName); 
       
   401                     iEditorManager->SetImageName(iDocumentName);
       
   402                     
       
   403                     LOGFMT(KImageEditorLogFile, "CImageEditorAppUi: Temp file renamed to %S", &iDocumentName);
       
   404 
       
   405                     // Purge undo stack if on the way to exit.
       
   406                     // Don't purge if printing or sending the image.
       
   407                     if( iExitFromApp )
       
   408                     {
       
   409                         iEditorManager->PurgeUndoRedoHistory();
       
   410                     }
       
   411 
       
   412                     FinalizeSaveL();
       
   413                 }
       
   414 			
       
   415                 if (iExitFromApp)
       
   416                 {
       
   417                     DeleteTempFiles();
       
   418                     User::Leave (KSIEEErrorOkToExit);
       
   419                 }
       
   420                 else 
       
   421                 {
       
   422                     if (iSending)
       
   423                     {
       
   424                         // Restore document name
       
   425                         SetBusy();
       
   426                         iEditorManager->SetImageName (iDocumentName);
       
   427                         iCallback->DoCallback (KObSendCallbackID);
       
   428                     }
       
   429                     
       
   430                     if (iPrinting)
       
   431                     {
       
   432                         // Restore document name
       
   433                         SetBusy();
       
   434                         iEditorManager->SetImageName (iDocumentName);
       
   435                         iCallback->DoCallback (KObPrintCallbackID);
       
   436                     }
       
   437 
       
   438                     // Reset bitmap sink and screen size
       
   439                     UpdateScreenRectL();
       
   440 
       
   441                     // Set saved image name for printing
       
   442                     ((CImageEditorUIView *)iEditorView)->SetImageFileName(iDocumentName);
       
   443 
       
   444                 }
       
   445 
       
   446                 // Update menu
       
   447                 SetUndoFlag ();
       
   448                 SetOrientationL(EAppUiOrientationUnspecified);
       
   449                 LOG(KImageEditorLogFile, "CImageEditorAppUi: Saving procedure completed");
       
   450                 break;
       
   451             }
       
   452 
       
   453             default:
       
   454             {
       
   455                 break;
       
   456             }
       
   457         }
       
   458     }
       
   459 }
       
   460 
       
   461 //=============================================================================
       
   462 void CImageEditorAppUi::RenderL ()
       
   463 {
       
   464 	LOG(KImageEditorLogFile, "CImageEditorAppUi: Rendering...");
       
   465 	SetBusy();
       
   466 	iEditorManager->ProcessImageL();
       
   467 	((CImageEditorUIView *)iEditorView)->GetContainer()->DrawNow();
       
   468 	ResetBusy();
       
   469 }
       
   470 
       
   471 //=============================================================================
       
   472 TKeyResponse CImageEditorAppUi::HandleKeyEventL (
       
   473     const TKeyEvent &	aKeyEvent,
       
   474 	TEventCode			aType
       
   475 	)
       
   476 {
       
   477     LOGFMT2(KImageEditorLogFile, "CImageEditorAppUi:HandleKeyEventL (aKeyEvent: %d, aType:\t %d)", aKeyEvent,aType);
       
   478 
       
   479     TKeyResponse response = EKeyWasNotConsumed;
       
   480 
       
   481     // if we are busy doing something
       
   482 	// then we consume the key
       
   483     if ( iBusy || (iConstructionState != EConstructionStateReady) )
       
   484     {
       
   485         response = EKeyWasConsumed;
       
   486     }
       
   487 
       
   488 	// we are only interested in real key events
       
   489 	// rather than ups or downs
       
   490 	else if (aType != EEventKey)
       
   491     {
       
   492         // These are handled separately here because pressing shift key doesn't
       
   493         // give EEventKey event at all
       
   494         if ( aType == EEventKeyDown )
       
   495         {
       
   496             switch (aKeyEvent.iScanCode)
       
   497 	        {
       
   498 		        case EStdKeyLeftShift:
       
   499 		        {
       
   500 		            iEditorView->HandleCommandL (EImageEditorPreGlobalZoomChange);
       
   501                     ZoomL( EZoomIn );
       
   502 			    	iEditorView->HandleCommandL (EImageEditorGlobalZoomChanged);
       
   503 			        response = EKeyWasConsumed;
       
   504 			        break;
       
   505 		        }
       
   506                 case EStdKeyRightShift:
       
   507 		        {
       
   508 		            iEditorView->HandleCommandL (EImageEditorPreGlobalZoomChange);
       
   509                     ZoomL( EZoomOut );
       
   510 			    	iEditorView->HandleCommandL (EImageEditorGlobalZoomChanged);
       
   511 			        response = EKeyWasConsumed;
       
   512 		            break;
       
   513 		        }
       
   514             }
       
   515         }
       
   516             
       
   517 	    response = EKeyWasNotConsumed;
       
   518     }
       
   519 
       
   520 	// scan codes for numbers are their ASCII codes
       
   521     else
       
   522     {
       
   523         TBool rotated = LandscapeScreenOrientation();
       
   524         
       
   525         switch (aKeyEvent.iScanCode)
       
   526 	    {
       
   527 
       
   528 		    case 0x30: // 0
       
   529 		    {
       
   530 #ifdef FULLSCREEN_AVAILABLE 
       
   531 		    	if (iFullScreen)
       
   532 			    {
       
   533 				    ResetFullScreenL();
       
   534 			    }
       
   535 			    else
       
   536 			    {
       
   537 				    SetFullScreenL();
       
   538 			    }
       
   539 			    response = EKeyWasConsumed;
       
   540 #endif // FULLSCREEN_AVAILABLE		    
       
   541                 break;
       
   542 		    }
       
   543 #ifndef LANDSCAPE_ROTATE_HOTKEYS
       
   544 	        case 0x31: // 1
       
   545 #else
       
   546             case 0x33: // 3
       
   547 #endif
       
   548 		    {
       
   549 		        RotateL (ERotationCounterClockwise);
       
   550 			    response = EKeyWasConsumed;
       
   551                 break;
       
   552 		    }
       
   553 
       
   554 		    case 0x32: // 2
       
   555 			{
       
   556 				if (!rotated)
       
   557 				{
       
   558 					PanL (EDirectionUp);
       
   559 	                response = EKeyWasConsumed;			
       
   560 				}
       
   561 				else
       
   562 				{
       
   563 					PanL (EDirectionLeft);
       
   564 				    response = EKeyWasConsumed;
       
   565 				}
       
   566                 break;				
       
   567 			}
       
   568 
       
   569 #ifndef LANDSCAPE_ROTATE_HOTKEYS
       
   570 		    case 0x33: // 3
       
   571 #else		    
       
   572 		    case 0x39: // 9
       
   573 #endif		    		    
       
   574 		    {
       
   575                 RotateL (ERotationClockwise);
       
   576 			    response = EKeyWasConsumed;
       
   577                 break;
       
   578 		    }
       
   579 
       
   580 		    case 0x34: // 4
       
   581 		    {
       
   582 				if (!rotated)
       
   583 				{
       
   584 		            PanL (EDirectionLeft);
       
   585 				    response = EKeyWasConsumed;
       
   586 				}
       
   587 				else
       
   588 				{
       
   589 					PanL (EDirectionDown);
       
   590 				    response = EKeyWasConsumed;	
       
   591 				}
       
   592                 break;
       
   593 		    }
       
   594 		    case 0x35: // 5
       
   595 		    {
       
   596 		        // No function		    	
       
   597                 break;
       
   598 		    }
       
   599 		    
       
   600             case 0x36: // 6 
       
   601 		    {	
       
   602 		    	if (!rotated)
       
   603 					{
       
   604 	                PanL (EDirectionRight);
       
   605 				    response = EKeyWasConsumed;
       
   606 					}
       
   607 				else
       
   608 					{
       
   609 					PanL (EDirectionUp);
       
   610 	                response = EKeyWasConsumed;		
       
   611 					}	
       
   612                 break;
       
   613 		    }
       
   614 		    
       
   615 		    case 0x38: // 8      		    
       
   616 		    {
       
   617 		    	if (!rotated)
       
   618 					{
       
   619 	                PanL (EDirectionDown);
       
   620 				    response = EKeyWasConsumed;
       
   621 					}
       
   622 				else
       
   623 					{
       
   624 					PanL (EDirectionRight);
       
   625 				    response = EKeyWasConsumed;
       
   626 					}
       
   627                 break;
       
   628 		    }
       
   629 
       
   630 		    // following cases handle four directions and select key
       
   631             case EStdKeyRightArrow: 
       
   632 		    {
       
   633                 PanL (EDirectionRight);
       
   634 			    response = EKeyWasConsumed;
       
   635                 break;
       
   636 		    }
       
   637 		    case EStdKeyLeftArrow: 
       
   638 		    {
       
   639                 PanL (EDirectionLeft);
       
   640 			    response = EKeyWasConsumed;
       
   641                 break;
       
   642 		    }
       
   643 		    case EStdKeyUpArrow: 
       
   644 		    {
       
   645                 PanL (EDirectionUp);
       
   646 			    response = EKeyWasConsumed;
       
   647                 break;
       
   648 		    }
       
   649 		    case EStdKeyDownArrow: 
       
   650 		    {
       
   651                 PanL (EDirectionDown);
       
   652 			    response = EKeyWasConsumed;
       
   653                 break;
       
   654 		    }
       
   655             case EStdKeyEnter: 
       
   656 		    {			    			                    
       
   657                 TBool zoomedIn = ( ( CImageEditorUIView *)iEditorView )->InZoomingState();                
       
   658                 if ( zoomedIn )
       
   659                     {
       
   660                     iEditorView->HandleCommandL (EImageEditorPreGlobalZoomChange);
       
   661                     ZoomL( EZoomIn );
       
   662     		    	iEditorView->HandleCommandL (EImageEditorGlobalZoomChanged);    		            
       
   663                     }
       
   664                 else
       
   665                     {
       
   666                     // display the plugin selection grid if not in zoomed state
       
   667                     HandleCommandL( EImageEditorMenuCmdApplyEffect );
       
   668                     }    
       
   669                 response = EKeyWasConsumed;                		        
       
   670                 break;
       
   671 		    }
       
   672 	        case 0x2a: // *
       
   673 		    case EStdKeyNkpAsterisk: // *
       
   674 		    case EStdKeyIncVolume:
       
   675 		    {
       
   676 		    	if ( iEditorManager->IsPluginLoaded() )
       
   677 		    	{
       
   678 		    		if ( iEditorManager->GetZoomMode()== EZoomIn3 )
       
   679 		    		{
       
   680 		    			  response = EKeyWasConsumed;
       
   681 		    			  break;
       
   682 		    		}
       
   683 		    	}
       
   684 			    iEditorView->HandleCommandL (EImageEditorPreGlobalZoomChange);
       
   685                 ZoomL (EZoomIn);
       
   686 				iEditorView->HandleCommandL (EImageEditorGlobalZoomChanged);
       
   687 			    response = EKeyWasConsumed;
       
   688                 break;
       
   689 		    }
       
   690 		    case 0x23: // #
       
   691 	        case EStdKeyHash: // #
       
   692 	        case EStdKeyDecVolume:
       
   693 	        {
       
   694 	        	  if ( iEditorManager->IsPluginLoaded() )
       
   695 	        	  {
       
   696 	        		   if ( iEditorManager->GetZoomMode()== EZoomNormal )
       
   697 	        		   {
       
   698 	        		       response = EKeyWasConsumed;
       
   699 	        		       break;
       
   700 	        		   }
       
   701 	        	  }
       
   702 	            iEditorView->HandleCommandL (EImageEditorPreGlobalZoomChange);
       
   703                 ZoomL (EZoomOut);
       
   704 				iEditorView->HandleCommandL (EImageEditorGlobalZoomChanged);
       
   705 				response = EKeyWasConsumed;
       
   706 	            break;
       
   707 	        }
       
   708 	        case EStdKeyYes: // green answer call key
       
   709 		    {
       
   710                 if (!iEditorManager->IsPluginLoaded())
       
   711                 {   
       
   712                     // when touch is enabled, dialer application is launched 
       
   713                     // by the framework and application should not consume
       
   714                     // this key. Otherwise image is saved and send menu opened.
       
   715 			        if ( !AknLayoutUtils::PenEnabled() )
       
   716 			            {
       
   717 			            if ( !iSending )
       
   718 			            	{
       
   719 			            	SaveChangesAndSendL();
       
   720 			            	} 
       
   721 			            response = EKeyWasConsumed;    
       
   722 			            }
       
   723 			        else
       
   724 			            {
       
   725 			            response = EKeyWasNotConsumed;
       
   726 			            }    				    
       
   727                 }
       
   728                 break;
       
   729 		    }		    		  
       
   730             case EStdKeyDevice3: // Select key (OK key)
       
   731             {
       
   732                 // display the plugin selection grid
       
   733                 HandleCommandL(EImageEditorMenuCmdApplyEffect);
       
   734                 response = EKeyWasConsumed;
       
   735                 break;
       
   736             }
       
   737 	        case EStdKeyNo: // end key
       
   738 		    {
       
   739 		    	iBusy = ETrue;
       
   740 			    response = EKeyWasNotConsumed;
       
   741                 break;
       
   742 		    }		    		  
       
   743 
       
   744 		    default:
       
   745 		    {
       
   746 			    response = EKeyWasNotConsumed;
       
   747                 break;
       
   748 		    }
       
   749         }
       
   750     }
       
   751 	return response;
       
   752 }
       
   753 
       
   754 //=============================================================================
       
   755 void CImageEditorAppUi::HandleCommandL (TInt aCommand)
       
   756 {
       
   757     LOGFMT(KImageEditorLogFile, "CImageEditorAppUi:HandleCommandL (%d)", aCommand);
       
   758 
       
   759     switch ( aCommand )
       
   760     {
       
   761 
       
   762         /** 
       
   763         *
       
   764         *   MENU COMMANDS
       
   765         *   
       
   766         */
       
   767 
       
   768         //  Launch plug-in selection grid
       
   769         case EImageEditorMenuCmdApplyEffect:
       
   770 		{
       
   771 			SetBusy();
       
   772             iCallback->DoCallback (KObInitPluginID);
       
   773             break;
       
   774 		}
       
   775 	
       
   776         //  Undo effect
       
   777 		case EImageEditorMenuCmdUndo:
       
   778         {
       
   779 			SetBusy();
       
   780             iCallback->DoCallback (KObUndoDCallbackID);
       
   781             break;
       
   782         }
       
   783 
       
   784         //  Switch to full screen
       
   785 		case EImageEditorMenuCmdFullScreen:
       
   786         {
       
   787 #ifdef FULLSCREEN_AVAILABLE         
       
   788             SetFullScreenL();
       
   789 #endif // FULLSCREEN_AVAILABLE         
       
   790             break;
       
   791         }
       
   792 
       
   793         //  Switch to normal screen
       
   794 		case EImageEditorMenuCmdNormalScreen:
       
   795         {
       
   796 #ifdef FULLSCREEN_AVAILABLE         
       
   797             ResetFullScreenL();
       
   798 #endif // FULLSCREEN_AVAILABLE            
       
   799             break;
       
   800         }
       
   801         
       
   802         // Increase Zoom
       
   803         case EImageEditorMenuCmdZoomIn:
       
   804         {
       
   805             ZoomL (EZoomIn);
       
   806             break;
       
   807         }
       
   808 
       
   809         // Decrease Zoom
       
   810         case EImageEditorMenuCmdZoomOut:
       
   811         {
       
   812             ZoomL (EZoomOut);
       
   813             break;
       
   814         }
       
   815         
       
   816         // Set zoom to minimum (=normal image)
       
   817         case EImageEditorMenuCmdFitToScreen:
       
   818         {
       
   819            ZoomL( EZoomMin );
       
   820            break;
       
   821         }
       
   822         
       
   823         //  Launch CSHelp
       
   824         case EAknCmdHelp:
       
   825         case EImageEditorMenuCmdHelp:
       
   826         {
       
   827             CArrayFix<TCoeHelpContext>* context = CCoeAppUi::AppHelpContextL();
       
   828             if (context)
       
   829             {
       
   830                 HlpLauncher::LaunchHelpApplicationL(iEikonEnv->WsSession(), context);
       
   831             }
       
   832             break;
       
   833         }
       
   834         case EImageEditorMenuCmdSave:
       
   835         {
       
   836         	if ( ! ((CImageEditorUIView*) iEditorView)->IsMemoryInCriticalLevel() )
       
   837         		{
       
   838         		QueryAndSaveL(); 
       
   839         		}
       
   840             break;
       
   841         }
       
   842         
       
   843         //  Close editor
       
   844         case EImageEditorMenuCmdExit:
       
   845         {
       
   846             TBool exitNow = ETrue;
       
   847             if ( iEditorManager->IsImageChanged() )
       
   848             {
       
   849             	if ( ! ((CImageEditorUIView*) iEditorView)->IsMemoryInCriticalLevel() )
       
   850             		{
       
   851             		// launch "Save changes?" query
       
   852             		TInt result = ((CImageEditorUIView *)iEditorView)->LaunchSaveChangesQueryL();            	
       
   853             		if( result == EImageEditorSoftkeyCmdYes )
       
   854             		{	
       
   855             			if (QueryAndSaveL())
       
   856             			{
       
   857             				SetBusy();
       
   858             				iExitFromApp = ETrue;
       
   859             				exitNow = EFalse;
       
   860             				LOG(KImageEditorLogFile, "CImageEditorAppUi:HandleCommandL: Hide application from Fast-swap window.");
       
   861             				CAknAppUi::HideApplicationFromFSW(ETrue);			     		
       
   862             			}
       
   863             			else
       
   864             			{
       
   865 			     			iExitFromApp = EFalse;
       
   866 			     			exitNow = EFalse;
       
   867             			}
       
   868 
       
   869             		}
       
   870             		else if( result == EImageEditorSoftkeyCmdNo )
       
   871             		{	
       
   872 		            	// do nothing, exitNow == ETrue
       
   873             		}
       
   874             		else
       
   875             		{
       
   876 	                	// Automatic cancel may have occurred, don't exit
       
   877 	                	iExitFromApp = EFalse;
       
   878 			     		exitNow = EFalse;
       
   879             		}
       
   880 	            }
       
   881             }
       
   882                 
       
   883             if (exitNow)
       
   884             {
       
   885                 DeleteTempFiles();
       
   886                 RunAppShutter();
       
   887                
       
   888                 
       
   889                 //Exit();
       
   890             }
       
   891             break;
       
   892         } 
       
   893 
       
   894         /** 
       
   895         *
       
   896         *   SOFTKEY COMMANDS
       
   897         *   
       
   898         */
       
   899 
       
   900         //  Back softkey pressed => Close editor
       
   901 		case EImageEditorSoftkeyCmdBack:
       
   902         {
       
   903         	TBool exitNow = ETrue;
       
   904         	if (iSaving || iCancelling)
       
   905         	{
       
   906         		// ignore	
       
   907         	}
       
   908         	if ( ! ((CImageEditorUIView*) iEditorView)->IsMemoryInCriticalLevel() )
       
   909         	{
       
   910             	if ( iEditorManager->IsImageChanged() )
       
   911             	{   
       
   912             		// launch "Save changes?" query    
       
   913                 	TInt result = ((CImageEditorUIView *)iEditorView)->LaunchSaveChangesQueryL();            	
       
   914                 	if( result == EImageEditorSoftkeyCmdYes )
       
   915                 	{
       
   916 			        	if (QueryAndSaveL())
       
   917 			        	{
       
   918 				        	SetBusy();
       
   919 				        	iExitFromApp = ETrue;
       
   920 				        	exitNow = EFalse;
       
   921 				        	LOG(KImageEditorLogFile, "CImageEditorAppUi:HandleCommandL: Hide application from Fast-swap window.");
       
   922 				        	CAknAppUi::HideApplicationFromFSW(ETrue);			     		
       
   923 			        	}
       
   924 			        	else
       
   925 			        	{
       
   926 			     			iExitFromApp = EFalse;
       
   927 			     			exitNow = EFalse;
       
   928 			        	}
       
   929 
       
   930                 	}
       
   931                 	else if( result == EImageEditorSoftkeyCmdNo )
       
   932                 	{
       
   933                 		// do nothing, exitNow == ETrue
       
   934                 	}
       
   935                 	else
       
   936                 	{
       
   937                 		// Automatic cancel may have occurred, don't exit
       
   938                 		iExitFromApp = EFalse;
       
   939                 		exitNow = EFalse;
       
   940                 	}
       
   941 	            }
       
   942             }
       
   943             
       
   944         	if (exitNow)
       
   945         	{
       
   946 	            DeleteTempFiles();
       
   947     	        RunAppShutter();
       
   948 	            //ProcessCommandL(EAknCmdExit);
       
   949         	}
       
   950            	break;
       
   951         }
       
   952 
       
   953         /** 
       
   954         *
       
   955         *   SYSTEM COMMANDS
       
   956         *   
       
   957         */
       
   958 
       
   959         case EEikCmdExit:
       
   960         {
       
   961 			LOG(KImageEditorLogFile, "CImageEditorAppUi: EEikCmdExit caught");
       
   962 			DeleteTempFiles();
       
   963             Exit();
       
   964             break;
       
   965         }
       
   966 
       
   967         /** 
       
   968         *
       
   969         *   MISC COMMANDS
       
   970         *   
       
   971         */
       
   972 
       
   973         //  Render image
       
   974         case EImageEditorCmdRender:
       
   975         {
       
   976             RenderL();
       
   977             break;
       
   978         }
       
   979 
       
   980         case EImageEditorCmdViewReady:
       
   981         {
       
   982 
       
   983             LOG(KImageEditorLogFile, "CImageEditorAppUi: View ready");
       
   984 			iEditorReady = ETrue;
       
   985 
       
   986             //  Initialize UI items
       
   987             InitializeUiItemsL();
       
   988 			ApplicationReadyL();
       
   989             break;
       
   990         }
       
   991 
       
   992 		case EImageEditorApplyPlugin:
       
   993         {
       
   994 			//	Return from plug-in
       
   995             SetBusy();
       
   996             iCallback->DoCallback (KObReturnFromPluginID);
       
   997             break;
       
   998         }
       
   999 
       
  1000         case EImageEditorCancelPlugin:
       
  1001         {
       
  1002 			SetBusy();
       
  1003 			iCancelling = ETrue;
       
  1004             iCallback->DoCallback (KObCancelCallbackID);
       
  1005             break;
       
  1006         }
       
  1007 
       
  1008         case EImageEditorAddFilterToEngine:
       
  1009         {
       
  1010             // Add the current plugin parameters to engine
       
  1011             // and continue with the same plugin
       
  1012 			iEditorManager->AddFilterToEngineL();
       
  1013             break;
       
  1014         }
       
  1015 
       
  1016         case EImageEditorStoreParameters:
       
  1017         {
       
  1018             break;
       
  1019 		}
       
  1020 
       
  1021         case EImageEditorUndoFilter:
       
  1022         {
       
  1023             // Undo one filter (can be called if one plugin
       
  1024             // has put more than one filter to the engine)
       
  1025             SetBusy();
       
  1026             iEditorManager->UndoL();
       
  1027             SetUndoFlag();
       
  1028             ((CImageEditorUIView *)iEditorView)->GetContainer()->DrawNow();
       
  1029 			ResetBusy();
       
  1030             break;
       
  1031         }
       
  1032 
       
  1033         case EImageEditorSaveAndPrint:
       
  1034         {
       
  1035             if ( iEditorManager->IsImageChanged() )
       
  1036             {
       
  1037                 iPrinting = ETrue;
       
  1038                 SaveImageL();
       
  1039             }
       
  1040             else
       
  1041             {
       
  1042                 SetBusy();
       
  1043                 iCallback->DoCallback (KObPrintCallbackID);
       
  1044             }
       
  1045             break;
       
  1046         }
       
  1047         case EImageEditorResetZoom:
       
  1048         {
       
  1049             // If this is called rendering must be done separately
       
  1050             iEditorManager->ZoomL( EZoomMin );
       
  1051             ((CImageEditorUIView *)iEditorView)->SetZoomModeL( iEditorManager->GetZoomMode() );
       
  1052             break;
       
  1053         }
       
  1054         case EImageEditorCmdTouchPan:
       
  1055             {   
       
  1056             TouchPanL();            
       
  1057             break;
       
  1058             }
       
  1059         case EImageEditorOpenContextMenu:
       
  1060             {   
       
  1061             CAknViewAppUi::ProcessCommandL(EAknSoftkeyContextOptions);
       
  1062             break;
       
  1063             }
       
  1064         default:
       
  1065         {
       
  1066             // CSendUi commands  
       
  1067             if (aCommand >= EImageEditorMenuCmdSend &&
       
  1068                 aCommand < EImageEditorMenuCmdSendLast)
       
  1069     		{
       
  1070 		    	SetBusy();
       
  1071                 SaveChangesAndSendL();
       
  1072                 break;
       
  1073             }
       
  1074 
       
  1075             /** 
       
  1076             *
       
  1077             *   BEVERLY HILLS 
       
  1078             *   
       
  1079             */
       
  1080             else
       
  1081             {
       
  1082                 // Do not handle unknown commands
       
  1083                 ResetBusy();
       
  1084                 break;
       
  1085             }
       
  1086         }
       
  1087 	}
       
  1088 }
       
  1089 
       
  1090 //=============================================================================
       
  1091 void CImageEditorAppUi::HandleWsEventL ( 
       
  1092 	const TWsEvent &		aEvent,
       
  1093 	CCoeControl *			aDestination 
       
  1094 	)
       
  1095 {
       
  1096 #ifdef VERBOSE
       
  1097     LOGFMT3 (KImageEditorLogFile, "CImageEditorAppUi::HandleWsEventL: aEvent.Type() == %d, aEvent.Key().iCode == %d, aEvent.Key().iScanCode == %d", aEvent.Type(), (TInt)aEvent.Key()->iCode, (TInt)aEvent.Key()->iScanCode);
       
  1098 #endif
       
  1099 
       
  1100     switch ( aEvent.Type() )
       
  1101     {
       
  1102 
       
  1103         case EEventFocusLost:
       
  1104         {
       
  1105 			iForeground = EFalse;
       
  1106 			break;
       
  1107         }
       
  1108 		
       
  1109         case EEventFocusGained:
       
  1110         {
       
  1111 
       
  1112     	    // Update the screen rect when we gain focus.
       
  1113 		    // This isn't really needed every time, it should only do this if something has changed
       
  1114             if (iImageController)
       
  1115             {
       
  1116                 UpdateScreenRectL();
       
  1117             }
       
  1118 
       
  1119             if (iFullScreen)
       
  1120             {
       
  1121                 StatusPane()->MakeVisible ( EFalse );
       
  1122             }
       
  1123 			iForeground = ETrue;
       
  1124 			break;
       
  1125         }
       
  1126 
       
  1127 		default:
       
  1128         {
       
  1129 			break;
       
  1130         }
       
  1131 
       
  1132     }
       
  1133 
       
  1134 	if ( ( aEvent.Type() == KAknUidValueEndKeyCloseEvent) && iEditorManager->IsImageChanged() )
       
  1135 		{
       
  1136 		if (iCancelling)
       
  1137 			{
       
  1138 			return;
       
  1139 			}
       
  1140 		iExitFromApp = ETrue;            
       
  1141 		if (iSaving)
       
  1142 			{
       
  1143 			iImageController->CancelOperation(ETrue);
       
  1144 			iSaveOnExit = EFalse;
       
  1145 
       
  1146 			return;
       
  1147 			}
       
  1148 
       
  1149 		TInt err = ImageEditorUtils::GenerateNewDocumentNameL (
       
  1150 											iEikonEnv->FsSession(), 
       
  1151 											iSourceFileName, 
       
  1152 											iDocumentName,
       
  1153 											&iSourceImageMgAlbumIdList 
       
  1154 											);
       
  1155 		SaveImageOverWriteL( EFalse );
       
  1156 		}
       
  1157 	else
       
  1158 		{
       
  1159 		TInt err( 0 );    
       
  1160 		TRAP( err, CAknViewAppUi::HandleWsEventL( aEvent, aDestination ) );
       
  1161 		}
       
  1162 }
       
  1163 
       
  1164 
       
  1165 //=============================================================================
       
  1166 void CImageEditorAppUi::HandleScreenDeviceChangedL()
       
  1167     {
       
  1168 
       
  1169 	LOG(KImageEditorLogFile, "CImageEditorAppUi::HandleScreenDeviceChangedL()");
       
  1170 
       
  1171     // Work-a-round to avoid softkeys to drawn incorrectly
       
  1172     // when a dialog is open when changing to view mode
       
  1173     if (iConstructionState == EConstructionStateReady &&
       
  1174         iSaving == EFalse &&
       
  1175         iSending == EFalse )
       
  1176     {   
       
  1177         StopDisplayingMenuBar();
       
  1178     }
       
  1179     
       
  1180     CAknViewAppUi::HandleScreenDeviceChangedL();
       
  1181 
       
  1182     TBool visibleFlag = EFalse;
       
  1183     if (((CImageEditorUIView *)iEditorView)->GetContainer())
       
  1184     {
       
  1185         visibleFlag = ((CImageEditorUIView *)iEditorView)->GetContainer()->IsVisible();
       
  1186     }
       
  1187 
       
  1188     // If orientation changes during loading - screen bitmap need to be recreated
       
  1189     // In the start-up there can be event from system without screen device change
       
  1190     if (!iImageLoaded && iConstructionState != EConstructionStateAlloc )
       
  1191 	    {
       
  1192         iOrientationChanged = ETrue;    
       
  1193         // Set landscape parameter
       
  1194         if (LandscapeScreenOrientation())
       
  1195             {
       
  1196             iResolutionUtil->SetLandscape(ETrue);
       
  1197             }
       
  1198         else
       
  1199             {
       
  1200             iResolutionUtil->SetLandscape(EFalse);
       
  1201             }
       
  1202         	
       
  1203      	iResolutionUtil->UpdateScreenMode();
       
  1204     	TRect rect;
       
  1205     	iResolutionUtil->GetClientRect(rect);
       
  1206     	CCoeControl * control = ((CImageEditorUIView *)iEditorView)->GetContainer();
       
  1207 		control->SetRect(rect);
       
  1208 	    }
       
  1209     
       
  1210     // Update screen rects if we are currently in the foreground.
       
  1211     else if ((iForeground && iImageController) || visibleFlag)
       
  1212     {
       
  1213         UpdateScreenRectL();
       
  1214     }
       
  1215 
       
  1216 }
       
  1217 
       
  1218 //=============================================================================
       
  1219 TBool CImageEditorAppUi::LandscapeScreenOrientation()
       
  1220 {
       
  1221 
       
  1222 	LOG(KImageEditorLogFile, "CImageEditorAppUi::LandscapeScreenOrientation()");
       
  1223 
       
  1224     TBool orientation = EFalse;
       
  1225 
       
  1226 #ifdef LANDSCAPE_SUPPORT
       
  1227 
       
  1228     TRect rect;
       
  1229 
       
  1230     AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EScreen, rect);
       
  1231 
       
  1232 	if ( rect.iBr.iX > rect.iBr.iY )
       
  1233         {
       
  1234         orientation = ETrue;
       
  1235         }
       
  1236 	else
       
  1237         {
       
  1238         orientation = EFalse;
       
  1239         }
       
  1240 
       
  1241 #endif
       
  1242 
       
  1243 	LOGFMT(KImageEditorLogFile, "CImageEditorAppUi: Orientation (%d)", (TInt)orientation);
       
  1244 
       
  1245     return orientation;
       
  1246 }
       
  1247 
       
  1248 //=============================================================================
       
  1249 void CImageEditorAppUi::HandleForegroundEventL (TBool aForeground)
       
  1250 {
       
  1251 	LOG(KImageEditorLogFile, "CImageEditorAppUi::HandleForegroundEventL()");
       
  1252 
       
  1253 	if (aForeground)
       
  1254 	{
       
  1255 		if ( !BaflUtils::FileExists( iEikonEnv->Static()->FsSession() , iSourceFileName  ) )
       
  1256 			{
       
  1257 			DeleteTempFiles();
       
  1258 			RunAppShutter();
       
  1259 			}
       
  1260 
       
  1261 		if ( iProcessPriorityAltered )
       
  1262 		{
       
  1263 		// Return to normal priority.
       
  1264 		RProcess myProcess;
       
  1265 		TProcessPriority priority = myProcess.Priority();
       
  1266 		if ( priority < iOriginalProcessPriority )
       
  1267 			{
       
  1268 			myProcess.SetPriority( iOriginalProcessPriority );
       
  1269 			}
       
  1270 		iProcessPriorityAltered = EFalse;
       
  1271 		}
       
  1272 
       
  1273         // Check that application is in sync with the system 
       
  1274         if (!iImageLoaded && iConstructionState != EConstructionStateAlloc )
       
  1275  	    	{
       
  1276         	iOrientationChanged = ETrue;    
       
  1277     		}
       
  1278         else if (iResolutionUtil && LandscapeScreenOrientation() != iResolutionUtil->GetLandscape())
       
  1279             {
       
  1280             UpdateScreenRectL();
       
  1281             }
       
  1282         
       
  1283 		iEditorView->HandleCommandL (EImageEditorFocusGained);
       
  1284 	}
       
  1285 	else
       
  1286 	{
       
  1287 		iEditorView->HandleCommandL (EImageEditorFocusLost);
       
  1288 		// Set the priority to low. This is needed to handle the situations 
       
  1289 		// where the engine is performing heavy processing while the application 
       
  1290 		// is in background.
       
  1291 		RProcess myProcess;
       
  1292 		iOriginalProcessPriority = myProcess.Priority();
       
  1293 		myProcess.SetPriority( EPriorityLow );
       
  1294 		iProcessPriorityAltered = ETrue;
       
  1295 
       
  1296 
       
  1297 	}
       
  1298     CAknViewAppUi::HandleForegroundEventL (aForeground);
       
  1299 }
       
  1300 
       
  1301 
       
  1302 //=============================================================================
       
  1303 TBool CImageEditorAppUi::ProcessCommandParametersL (
       
  1304 	TApaCommand		aCommand,
       
  1305 	TFileName &		aDocumentName,
       
  1306 	const TDesC8 &	aTail 
       
  1307 	)
       
  1308 {
       
  1309 	LOG(KImageEditorLogFile, "CImageEditorAppUi::ProcessCommandParametersL");
       
  1310 
       
  1311     /* ------------- : test only ----------
       
  1312     if ( this->ContainerAppUi() == NULL ) // launched as standalone
       
  1313 	{
       
  1314 	    aDocumentName = _L("C:\\Data\\Images\\test.jpg");
       
  1315 	};
       
  1316     // ---------------- test only ------------- */
       
  1317     
       
  1318 	//	Store command parameter document name 
       
  1319     iDocumentName.Copy (aDocumentName);
       
  1320 
       
  1321     //  Save original document name
       
  1322     iSourceFileName.Copy(aDocumentName);
       
  1323 
       
  1324     //  Set document name for printing
       
  1325     ((CImageEditorUIView *)iEditorView)->SetImageFileName(iDocumentName);
       
  1326 
       
  1327     //	Return default action
       
  1328 	return CAknViewAppUi::ProcessCommandParametersL (aCommand, aDocumentName, aTail);
       
  1329 	}
       
  1330 
       
  1331 //=============================================================================
       
  1332 void CImageEditorAppUi::OpenFileL(RFile &aFile)
       
  1333     {
       
  1334     LOG(KImageEditorLogFile, "CImageEditorAppUi::OpenFileL");
       
  1335 
       
  1336 	// This function should be called only when editor is started
       
  1337 	// as an embedded application.
       
  1338 
       
  1339     //	Store command parameter document name 
       
  1340     aFile.FullName(iDocumentName);
       
  1341 
       
  1342 	//  Open file for editor 
       
  1343 	//  File must be opened until editor is closed, otherwise
       
  1344 	//  shared session is lost -> Error code -14.
       
  1345     RFs& fs = iEikonEnv->FsSession();
       
  1346     fs.ShareProtected();
       
  1347 	TInt err = iFile.Open(fs, iDocumentName, EFileRead | EFileShareReadersOnly);
       
  1348 	if (KErrNone != err)
       
  1349 		{
       
  1350 		User::LeaveIfError(iFile.Open(fs, iDocumentName, EFileRead | EFileShareAny));
       
  1351 		}
       
  1352 
       
  1353     //  Save original document name
       
  1354     aFile.FullName(iSourceFileName);
       
  1355 
       
  1356     //  Set document name for printing
       
  1357     ((CImageEditorUIView *)iEditorView)->SetImageFileName(iDocumentName);
       
  1358 
       
  1359 	// If OpenFileL was not called when the startup sequence reached 
       
  1360 	// the point where it needs the document name, the construction was
       
  1361 	// suspended to wait for this. 
       
  1362 	if (EConstructionStateWaitForStartupParameters == iConstructionState)
       
  1363 		{
       
  1364 		// Resume construction
       
  1365 		iConstructionState = EConstructionStateLoadImage;
       
  1366 		iCallback->DoCallback (KObConstructCallBackID);
       
  1367 		}
       
  1368     }
       
  1369 
       
  1370 //=============================================================================
       
  1371 CArrayFix<TCoeHelpContext>* CImageEditorAppUi::HelpContextL() const
       
  1372 {
       
  1373     CArrayFixFlat<TCoeHelpContext>* array = 
       
  1374         new(ELeave)CArrayFixFlat<TCoeHelpContext>(1);
       
  1375     CleanupStack::PushL(array);
       
  1376     
       
  1377     TBool zoomedIn = ( ( CImageEditorUIView *)iEditorView )->InZoomingState();                
       
  1378     if ( zoomedIn )
       
  1379         {
       
  1380         array->AppendL(TCoeHelpContext(TUid::Uid(UID_IMAGE_EDITOR), KSIE_HLP_ZOOMING));    		            
       
  1381         }
       
  1382     else
       
  1383         {
       
  1384         array->AppendL(TCoeHelpContext(TUid::Uid(UID_IMAGE_EDITOR), KSIE_HLP_MAIN));
       
  1385         }
       
  1386                     
       
  1387     CleanupStack::Pop(array);
       
  1388     return array;
       
  1389 }
       
  1390 
       
  1391 //=============================================================================
       
  1392 void CImageEditorAppUi::InitializePluginL ()
       
  1393 {
       
  1394     LOG(KImageEditorLogFile, "CImageEditorAppUi: Initialising plugin");
       
  1395 
       
  1396     //	Get pointer to the view
       
  1397 	CImageEditorUIView * view = (CImageEditorUIView *)iEditorView;
       
  1398 	
       
  1399 	//	Close the previous plug-in 
       
  1400 	iEditorManager->ClosePlugin();
       
  1401 	
       
  1402     //	Open a new plug-in 
       
  1403     CPluginInfo * pgn_info = view->GetSelectedPluginInfoL();
       
  1404 
       
  1405     if (pgn_info)
       
  1406     {
       
  1407 	    iEditorManager->OpenPluginL ( pgn_info->PluginDll()->Des() );
       
  1408 
       
  1409         LOG(KImageEditorLogFile, "CImageEditorAppUi: Plugin opened");
       
  1410 
       
  1411         // current rect
       
  1412         TRect rect;
       
  1413         iResolutionUtil->GetClientRect(rect);
       
  1414 
       
  1415 	    //	Create a plug-in control
       
  1416 	    //  Ownership is moved to the plug-in
       
  1417 	    CCoeControl * control = NULL;
       
  1418 	    TInt err = iEditorManager->CreatePluginControlL (
       
  1419             rect,
       
  1420 		    view->GetContainer(),
       
  1421 		    control
       
  1422     	    );
       
  1423 
       
  1424         LOG(KImageEditorLogFile, "CImageEditorAppUi: Plugin control created");
       
  1425 
       
  1426         //  The plug-in activation was cancelled
       
  1427 	    if (err != KErrNone)
       
  1428 	    {
       
  1429             //	Cancel plug-in
       
  1430             iCancelling = ETrue;
       
  1431 		    iCallback->DoCallback (KObCancelCallbackID);
       
  1432 	    }
       
  1433 
       
  1434         //  Activate plug-in
       
  1435 	    else
       
  1436         {
       
  1437 
       
  1438             //  Register control to UI
       
  1439             view->ActivatePluginL (control);
       
  1440     
       
  1441             LOG(KImageEditorLogFile, "CImageEditorAppUi: Plugin activated");
       
  1442 
       
  1443 
       
  1444             //  If the filter did not have a control, close plug-in
       
  1445             if ( !control )
       
  1446             {
       
  1447                 iEditorManager->ClosePlugin();
       
  1448 
       
  1449             }
       
  1450                     
       
  1451             //  Set the last opened image to editor view
       
  1452             ((CImageEditorUIView *)iEditorView)->SetImageL (
       
  1453                 iEditorManager->GetPreviewImage()
       
  1454                 );
       
  1455             
       
  1456             ResetBusy();
       
  1457         }
       
  1458     }
       
  1459 	else
       
  1460 	{
       
  1461 		ResetBusy();
       
  1462 	}
       
  1463 }
       
  1464 
       
  1465 //=============================================================================
       
  1466 void CImageEditorAppUi::CreatePluginLocatorL ()
       
  1467 {
       
  1468     iLocator = CPluginLocator::NewL();
       
  1469 }   
       
  1470 
       
  1471 //=============================================================================
       
  1472 void CImageEditorAppUi::CreateEditorManagerL ()
       
  1473 {
       
  1474     iEditorManager = CImageEditorPluginManager::NewL();
       
  1475 }
       
  1476 
       
  1477 //=============================================================================
       
  1478 void CImageEditorAppUi::CreateImageControllerL ()
       
  1479 {
       
  1480 	iImageController = CImageController::NewL (this, iEditorManager);
       
  1481 }
       
  1482 
       
  1483 //=============================================================================
       
  1484 void CImageEditorAppUi::LoadPluginsL ()
       
  1485 {
       
  1486     //  Scan plug-ins
       
  1487     iLocator->ScanPluginsL ();
       
  1488 }
       
  1489 
       
  1490 //=============================================================================
       
  1491 void CImageEditorAppUi::InitializeUiItemsL ()
       
  1492 {
       
  1493 
       
  1494     LOG (KImageEditorLogFile, "CImageEditorAppUi: InitializeUiItemsL");
       
  1495 
       
  1496     //  Set UI items
       
  1497     if (iEditorReady && iPluginsScanned && iImageLoaded)
       
  1498     {
       
  1499 	    for (TInt i = 0, c = iLocator->CountPlugins(); i < c; ++i)
       
  1500         {	
       
  1501 	        ((CImageEditorUIView *)iEditorView)->AddPluginUiItemL (iLocator->GetPluginInfo(i));
       
  1502 	    }
       
  1503 
       
  1504 		//  Set the last opened image to editor view
       
  1505 		((CImageEditorUIView *)iEditorView)->SetImageL (
       
  1506             iEditorManager->GetPreviewImage()
       
  1507             );
       
  1508 
       
  1509         SetFileNameTitlePaneL(iSourceFileName);
       
  1510 
       
  1511        ((CImageEditorUIView *)iEditorView)->SetZoomModeL( iEditorManager->GetZoomMode() );
       
  1512 
       
  1513 		LOG (KImageEditorLogFile, "CImageEditorAppUi: Plugins initialized");
       
  1514 
       
  1515     }
       
  1516 }
       
  1517 
       
  1518 //=============================================================================
       
  1519 void CImageEditorAppUi::ApplicationReadyL()
       
  1520 {
       
  1521     LOG (KImageEditorLogFile, "CImageEditorAppUi: ApplicationReadyLL");
       
  1522 
       
  1523     //  Set UI items
       
  1524     if (iEditorReady && iPluginsScanned && iImageLoaded)
       
  1525     {     
       
  1526         if ( iOrientationChanged )
       
  1527         {
       
  1528         	LOG(KImageEditorLogFile, 
       
  1529         	    "CImageEditorAppUi: Orientation changed, updating screen rect");
       
  1530             // Check that application is in sync with the system 
       
  1531             // This should fix the problem with screen update after image is loading when
       
  1532             // screen is folded during loading
       
  1533             UpdateScreenRectL();
       
  1534             iOrientationChanged = EFalse;
       
  1535         }
       
  1536         else if ( ( LandscapeScreenOrientation() && 
       
  1537                   ( StatusPane()->CurrentLayoutResId() != 
       
  1538                   R_AVKON_WIDESCREEN_PANE_LAYOUT_USUAL_FLAT  ) ) )
       
  1539             {
       
  1540             LOG(KImageEditorLogFile, 
       
  1541             "CImageEditorAppUi: Status pane needs to be changed->updating screen rect");
       
  1542             // Updating screen rect updates also status pane to correct "flat version"
       
  1543             UpdateScreenRectL();
       
  1544             }
       
  1545             
       
  1546 		TRect screenRect;
       
  1547 		CResolutionUtil::Self()->GetClientRect(screenRect);
       
  1548 		iOldScreenRect = screenRect;
       
  1549 
       
  1550 		ResetBusy();
       
  1551 		
       
  1552 		// Open Plugin selection grid right away when application start is finished
       
  1553 		//HandleCommandL(EImageEditorMenuCmdApplyEffect);
       
  1554 		
       
  1555 	}
       
  1556 }
       
  1557 
       
  1558 
       
  1559 //=============================================================================
       
  1560 void CImageEditorAppUi::LoadImageL ()
       
  1561 {
       
  1562     LOGFMT (KImageEditorLogFile, "CImageEditorAppUi: Loading image (%S)", &iSourceFileName);
       
  1563 
       
  1564 	//	Load the image to be edited
       
  1565 	iImageController->LoadImageL (iSourceFileName);
       
  1566 }
       
  1567 
       
  1568 //=============================================================================
       
  1569 void CImageEditorAppUi::SaveImageL ()
       
  1570 {
       
  1571     LOG(KImageEditorLogFile, "CImageEditorAppUi: Saving image");
       
  1572 
       
  1573     // Check if source image still exists
       
  1574     if (!BaflUtils::FileExists(iEikonEnv->FsSession(), iSourceFileName))
       
  1575     {
       
  1576         if (iExitFromApp)
       
  1577         {
       
  1578             TInt exit = 
       
  1579                 ((CImageEditorUIView *)iEditorView)->LaunchExitWithoutSavingQueryL();
       
  1580             if (exit)
       
  1581             {
       
  1582                 // Exit
       
  1583                 User::Leave (KSIEEErrorOkToExit);
       
  1584             }
       
  1585             else
       
  1586             {
       
  1587                 iExitFromApp = EFalse;
       
  1588                 User::Leave(KSIEEInternal);
       
  1589             }
       
  1590         }
       
  1591         else
       
  1592         {
       
  1593             User::Leave(KSIEEInternal);
       
  1594         }
       
  1595     }
       
  1596 
       
  1597     // Check document name if not saved yet or memory card removed
       
  1598     if ( !iImageSaved ||
       
  1599          !BaflUtils::PathExists(iEikonEnv->FsSession(), iTempSaveFileName.Left(3)))
       
  1600     {
       
  1601         //  If not saved yet, check that document name is valid 
       
  1602         SetImageNameL(); 
       
  1603     }
       
  1604 
       
  1605 	//	Set temp save image name to engine
       
  1606     iEditorManager->SetImageName (iTempSaveFileName);
       
  1607     
       
  1608     SetLayoutAwareApp(EFalse);
       
  1609     //	Launch save wait note
       
  1610 	LaunchSaveWaitNoteL ();
       
  1611 
       
  1612     iCallback->DoCallback (KObSaveCallbackID);
       
  1613 
       
  1614 }
       
  1615 
       
  1616 //=============================================================================
       
  1617 void CImageEditorAppUi::SetImageNameL()
       
  1618     {
       
  1619     
       
  1620     // Create new document name
       
  1621     // (note that this also selects the drive where the image is saved)
       
  1622     TInt err = ImageEditorUtils::GenerateNewDocumentNameL (
       
  1623         iEikonEnv->FsSession(), 
       
  1624         iSourceFileName, 
       
  1625         iDocumentName,
       
  1626         &iSourceImageMgAlbumIdList 
       
  1627         );
       
  1628     User::LeaveIfError (err); // cannot save the image
       
  1629     iEditorManager->SetImageName (iDocumentName);
       
  1630 
       
  1631     // Change title pane text
       
  1632     SetFileNameTitlePaneL(iSourceFileName);
       
  1633 
       
  1634 	//	Check that everything is in order with the file
       
  1635 	CheckFileL (iDocumentName);
       
  1636 
       
  1637     // Generate temporary save file name
       
  1638     TParse tempFileParser;
       
  1639     tempFileParser.Set (KTempSaveFile, &KTempPath, &iDocumentName); 
       
  1640     iTempSaveFileName.Copy (tempFileParser.FullName());
       
  1641     BaflUtils::EnsurePathExistsL (iEikonEnv->FsSession(), iTempSaveFileName);
       
  1642 
       
  1643     }
       
  1644 
       
  1645 
       
  1646 //=============================================================================
       
  1647 void CImageEditorAppUi::SaveChangesAndSendL()
       
  1648 {
       
  1649 
       
  1650     LOG(KImageEditorLogFile, "CImageEditorAppUi::SaveChangesAndSendL()");
       
  1651 
       
  1652     iSending = ETrue;
       
  1653 
       
  1654     // Delete old temp files if exists
       
  1655     DeleteTempFiles();
       
  1656 
       
  1657     if (iEditorManager->IsImageChanged())
       
  1658     {
       
  1659         // Save image
       
  1660         SaveImageL();
       
  1661 
       
  1662     }
       
  1663     //	Otherwise send last saved image
       
  1664     else
       
  1665     {
       
  1666 	    SendImageL ();
       
  1667     }
       
  1668 
       
  1669 }
       
  1670 
       
  1671 //=============================================================================
       
  1672 void CImageEditorAppUi::SendImageL ()
       
  1673 {
       
  1674     LOG(KImageEditorLogFile, "CImageEditorAppUi::SendImageL()");
       
  1675 
       
  1676     ResetBusy();
       
  1677 
       
  1678     RFs& fs = iEikonEnv->FsSession();
       
  1679     TFileName fileToSend;        
       
  1680     
       
  1681     // Check if image has been modified and saved
       
  1682     if (BaflUtils::FileExists(fs, iDocumentName))
       
  1683     {
       
  1684         fileToSend.Append(iDocumentName);        
       
  1685     }
       
  1686     // Use source file if no changes done
       
  1687     else
       
  1688     {
       
  1689         fileToSend.Append(iSourceFileName);     
       
  1690     }
       
  1691     
       
  1692     LOGFMT(KImageEditorLogFile, "CImageEditorAppUi: Sending image: %S", &fileToSend);
       
  1693 
       
  1694     iEikonEnv->SetBusy(ETrue);
       
  1695         
       
  1696 	// Launch send query dialog
       
  1697 	TSendingCapabilities cap( 0, 0, TSendingCapabilities::ESupportsAttachments );
       
  1698 	((CImageEditorUIView *)iEditorView)->LaunchSendQueryL(cap, fileToSend);
       
  1699 
       
  1700     iSending = EFalse;
       
  1701     iEikonEnv->SetBusy(EFalse);
       
  1702 }
       
  1703 
       
  1704 //=============================================================================
       
  1705 void CImageEditorAppUi::CheckFileL (const TDesC & aFileName) const
       
  1706 {
       
  1707 	RFs & fs = iEikonEnv->FsSession();
       
  1708 
       
  1709 	//	Is file name valid
       
  1710     if ( !fs.IsValidName (aFileName) )
       
  1711 	{
       
  1712         User::Leave ( KSIEESaveFile );
       
  1713 	}
       
  1714 
       
  1715 }
       
  1716 
       
  1717 //=============================================================================
       
  1718 TBool CImageEditorAppUi::CallbackMethodL (TInt aParam)
       
  1719 {
       
  1720 	LOGFMT(KImageEditorLogFile, "CImageEditorAppUi::CallbackMethodL (%d)", aParam);
       
  1721 
       
  1722     TBool ret = EFalse;
       
  1723 
       
  1724 	switch (aParam) 
       
  1725 	{
       
  1726 
       
  1727 		case KObConstructCallBackID:
       
  1728 		{
       
  1729 
       
  1730 	        switch (iConstructionState) 
       
  1731 	        {
       
  1732 
       
  1733 		        case EConstructionStateAlloc:
       
  1734 		        {
       
  1735                     //  Launch wait note
       
  1736                     LaunchLoadWaitNoteL();
       
  1737                     
       
  1738                     // Initialize resolution utility. CResolutionUtil::InitializeL()
       
  1739                     // Returns the singleton instance of the class, creating it if
       
  1740                     // it does not yet exist.
       
  1741                     iResolutionUtil = CResolutionUtil::InitializeL();
       
  1742 					iResolutionUtil->SetFullScreen(EFalse);
       
  1743 
       
  1744 #ifdef LANDSCAPE_SUPPORT
       
  1745                     // Set landscape parameter
       
  1746                     if (LandscapeScreenOrientation())
       
  1747                     {
       
  1748                         iResolutionUtil->SetLandscape(ETrue);
       
  1749                     }
       
  1750                     else
       
  1751                     {
       
  1752                         iResolutionUtil->SetLandscape(EFalse);
       
  1753                     }
       
  1754 #else
       
  1755 #ifdef LANDSCAPE_ONLY
       
  1756 					iResolutionUtil->SetLandscape(ETrue);
       
  1757 #else
       
  1758                     iResolutionUtil->SetLandscape(EFalse);
       
  1759 #endif 
       
  1760 #endif // LANDSCAPE_SUPPORT
       
  1761 
       
  1762                     iResolutionUtil->UpdateScreenMode();
       
  1763                     LOG(KImageEditorLogFile, "CImageEditorAppUi: Resolution set");
       
  1764 
       
  1765                     //  Create the Send UI instance
       
  1766    	                iSendAppUi = CSendUi::NewL();
       
  1767                     ((CImageEditorUIView *)iEditorView)->SetSendAppUi(iSendAppUi);
       
  1768 
       
  1769                     //  Create plug-in locator
       
  1770                     CreatePluginLocatorL();
       
  1771 					LOG(KImageEditorLogFile, "Locator created");
       
  1772                     //  Create plug-in manager
       
  1773                     CreateEditorManagerL();
       
  1774 					LOG(KImageEditorLogFile, "Manager created");
       
  1775                     //  Create image controller
       
  1776                     CreateImageControllerL();
       
  1777 					LOG(KImageEditorLogFile, "Image Controller created");
       
  1778                     iConstructionState = EConstructionStateLoadImage;
       
  1779                     ret = ETrue;
       
  1780                     break;
       
  1781 		        }
       
  1782 
       
  1783 		        case EConstructionStateLoadImage:
       
  1784 		        {
       
  1785 					LOG(KImageEditorLogFile, "Loading image");
       
  1786 
       
  1787 					//	iSourceFileName is available (in embedded mode) only after 
       
  1788 					//	CImageEditorDocument::OpenFileL has been called by the system.
       
  1789 					//	If that has not been done 
       
  1790 					if (!iSourceFileName.Length())
       
  1791 					{
       
  1792 						iConstructionState = EConstructionStateWaitForStartupParameters;
       
  1793 						ret = EFalse;
       
  1794 					}
       
  1795 					else
       
  1796 					{
       
  1797 	                    //  Kill wait note before loading image. 
       
  1798 	                    //  Some corrupted images loading stacks if wait note is displayd
       
  1799 	                    //KillWaitNote();
       
  1800 	                   
       
  1801 	                    // Relaunch wait note
       
  1802 	                    //LaunchLoadWaitNoteL();
       
  1803 
       
  1804 	                    //  Load image
       
  1805 				        LoadImageL();
       
  1806 	                    
       
  1807 	                    SetFileNameTitlePaneL(iSourceFileName);
       
  1808 	                    iConstructionState = EConstructionStateScanPlugins;
       
  1809 	                    ret = ETrue;
       
  1810 					}
       
  1811                     break;
       
  1812 		        }
       
  1813 
       
  1814 		        case EConstructionStateScanPlugins:
       
  1815 		        {
       
  1816 					LOG(KImageEditorLogFile, "Scanning plugins");
       
  1817                     LoadPluginsL();
       
  1818                     iConstructionState = EConstructionStateInitUi;
       
  1819                     ret = ETrue;
       
  1820                     break;
       
  1821 		        }
       
  1822 
       
  1823                 case EConstructionStateInitUi:
       
  1824 		        {
       
  1825 					LOG(KImageEditorLogFile, "Init UI");
       
  1826                     //  Initialize UI items
       
  1827                     iPluginsScanned = ETrue;
       
  1828                     InitializeUiItemsL();
       
  1829 			        iConstructionState = EConstructionStateReady;
       
  1830                     ret = ETrue;
       
  1831                     break;
       
  1832 		        }
       
  1833 
       
  1834                 default:
       
  1835 		        {
       
  1836 					LOG(KImageEditorLogFile, "Ready");
       
  1837 					KillWaitNote();
       
  1838                     ApplicationReadyL();
       
  1839                     break;
       
  1840 		        }
       
  1841 	        }
       
  1842             break;
       
  1843         }
       
  1844 
       
  1845 		case KObReturnFromPluginID:
       
  1846 		{
       
  1847             SetUndoFlag();
       
  1848 			iEditorManager->ShowPluginPopup();
       
  1849 			iEditorManager->ClosePlugin();
       
  1850 
       
  1851             __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  1852             ((CImageEditorUIView *)iEditorView)->ActivateMainViewL();
       
  1853 
       
  1854             ((CImageEditorUIView *)iEditorView)->SetImageL (
       
  1855                     iEditorManager->GetPreviewImage());
       
  1856             
       
  1857 			ResetBusy();
       
  1858 			break;
       
  1859 		}
       
  1860 
       
  1861 		case KObInitPluginID:
       
  1862 		{
       
  1863 			InitializePluginL ();
       
  1864             SetUndoFlag();
       
  1865 			break;
       
  1866 		}
       
  1867 
       
  1868 		case KObCancelCallbackID:
       
  1869 		{
       
  1870 			iEditorManager->ClosePlugin();
       
  1871             iEditorManager->UndoL();
       
  1872 
       
  1873 
       
  1874             __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  1875             ((CImageEditorUIView *)iEditorView)->ActivateMainViewL();
       
  1876 
       
  1877             ((CImageEditorUIView *)iEditorView)->SetImageL (
       
  1878                 iEditorManager->GetPreviewImage());
       
  1879 
       
  1880             SetUndoFlag();
       
  1881 			ResetBusy();
       
  1882 			iCancelling = EFalse;
       
  1883 			break;
       
  1884 		}
       
  1885 
       
  1886 		case KObUndoDCallbackID:
       
  1887 		{
       
  1888             iEditorManager->UndoL();
       
  1889             SetUndoFlag();
       
  1890             ((CImageEditorUIView *)iEditorView)->GetContainer()->DrawNow();
       
  1891 			ResetBusy();
       
  1892 			break;
       
  1893 		}
       
  1894 
       
  1895 		case KObSendCallbackID:
       
  1896 		{
       
  1897             SendImageL ();
       
  1898             ResetBusy();
       
  1899 			break;
       
  1900 		}
       
  1901 
       
  1902         case KObPrintCallbackID:
       
  1903         {
       
  1904             // Reset first otherwise canceling print application
       
  1905             // leaves busy state active
       
  1906             iPrinting = EFalse;
       
  1907             ResetBusy();
       
  1908             ((CImageEditorUIView *)iEditorView)->
       
  1909                 HandleCommandL(EImageEditorDoPrint);
       
  1910             break;
       
  1911         }
       
  1912 
       
  1913         case KObSaveCallbackID:
       
  1914         {
       
  1915 			//	Start saving 
       
  1916 		    iImageController->SaveImageL (iTempSaveFileName);
       
  1917 
       
  1918     		iSaving = ETrue;
       
  1919             break;
       
  1920         }
       
  1921 
       
  1922         default:
       
  1923 		{
       
  1924 			break;
       
  1925 		}
       
  1926 	}
       
  1927 
       
  1928     return ret;
       
  1929 }
       
  1930 
       
  1931 //=============================================================================
       
  1932 void CImageEditorAppUi::SetBusy ()
       
  1933 {
       
  1934     LOG(KImageEditorLogFile, "CImageEditorAppUi: SetBusy()");
       
  1935     if (iEditorView)
       
  1936     {
       
  1937 	    ((CImageEditorUIView *)iEditorView)->SetBusy();
       
  1938     }
       
  1939     iBusy = ETrue;
       
  1940 }
       
  1941 
       
  1942 //=============================================================================
       
  1943 void CImageEditorAppUi::ResetBusy ()
       
  1944 {
       
  1945     LOG(KImageEditorLogFile, "CImageEditorAppUi: ResetBusy()");
       
  1946     // Keep busy until application is fully constructed
       
  1947     if (iConstructionState == EConstructionStateReady)
       
  1948     {
       
  1949         if (iEditorView)
       
  1950         {
       
  1951             ((CImageEditorUIView *)iEditorView)->ResetBusy();
       
  1952         }
       
  1953         iBusy = EFalse;
       
  1954     }
       
  1955 }
       
  1956 
       
  1957 //=============================================================================
       
  1958 void CImageEditorAppUi::SetFullScreenL ()
       
  1959 {
       
  1960     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  1961 
       
  1962     LOG(KImageEditorLogFile, "CImageEditorAppUi: Setting full screen");
       
  1963 
       
  1964     iFullScreen = ETrue; 
       
  1965 
       
  1966     //  Set to full screen mode
       
  1967    ((CImageEditorUIView *)iEditorView)->SetFullScreen();
       
  1968 
       
  1969     // In the startup image controller might not be created yet 
       
  1970     if (iImageController)
       
  1971     {
       
  1972         // Update new screen resolution
       
  1973         UpdateScreenRectL();
       
  1974     }
       
  1975     
       
  1976  
       
  1977 }
       
  1978 
       
  1979 //=============================================================================
       
  1980 void CImageEditorAppUi::ResetFullScreenL ()
       
  1981 {
       
  1982     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  1983 
       
  1984     LOG(KImageEditorLogFile, "CImageEditorAppUi: Resetting full screen");
       
  1985 
       
  1986     iFullScreen = EFalse;
       
  1987 
       
  1988     //  Set to full screen mode
       
  1989     ((CImageEditorUIView *)iEditorView)->ResetFullScreen();
       
  1990 
       
  1991     // In the startup image controller might not be created yet 
       
  1992     if (iImageController)
       
  1993     {
       
  1994         UpdateScreenRectL();
       
  1995     }
       
  1996    
       
  1997 }
       
  1998 
       
  1999 //=============================================================================
       
  2000 void CImageEditorAppUi::SetUndoFlag ()
       
  2001 {
       
  2002     if (iEditorView)
       
  2003     {
       
  2004         ((CImageEditorUIView *)iEditorView)->SetUndoFlag( iEditorManager->CanUndo() );
       
  2005         ((CImageEditorUIView *)iEditorView)->SetSaveFlag( iEditorManager->IsImageChanged() );
       
  2006     }
       
  2007 }
       
  2008 
       
  2009 //=============================================================================
       
  2010 void CImageEditorAppUi::SetTitlePaneTextL (const TDesC & aText)
       
  2011 {
       
  2012     const TInt KMaximumTitleTextLength = 13;
       
  2013 
       
  2014     if (iEditorView)
       
  2015     {
       
  2016         HBufC * title_text = HBufC::NewLC ( aText.Length() );
       
  2017         TPtr title_text_ptr = title_text->Des();
       
  2018 
       
  2019         //  No truncation
       
  2020         if ( aText.Length() - 4 < KMaximumTitleTextLength )
       
  2021         {
       
  2022             //  Copy whole file name 
       
  2023             title_text_ptr.Copy (aText);         
       
  2024         }
       
  2025 
       
  2026         //  With truncation
       
  2027         else
       
  2028         {
       
  2029 
       
  2030             //  Copy maximum amount of the file
       
  2031             title_text_ptr.Copy ( aText.Left (KMaximumTitleTextLength) );         
       
  2032 
       
  2033             //  Append horizontal ellipsis
       
  2034             title_text_ptr.Append ( KBaflCharTruncation );         
       
  2035 
       
  2036             //  Append sequence number
       
  2037             _LIT(KSearchWildcard, "*-???");
       
  2038             TInt offset = aText.Match (KSearchWildcard);
       
  2039             if ( offset != KErrNotFound )
       
  2040             {
       
  2041                 title_text_ptr.Append ( aText.Mid (offset, 4) );         
       
  2042             }
       
  2043             
       
  2044         }
       
  2045 
       
  2046         //  Set title pane text
       
  2047 	    ((CImageEditorUIView *)iEditorView)->SetTitlePaneTextL (title_text_ptr);
       
  2048     
       
  2049         CleanupStack::PopAndDestroy(); // title_text
       
  2050     }
       
  2051 }
       
  2052 
       
  2053 //=============================================================================
       
  2054 void CImageEditorAppUi::ClearTitlePaneTextL ()
       
  2055 {
       
  2056     if (iEditorView)
       
  2057     {
       
  2058 	    ((CImageEditorUIView *)iEditorView)->ClearTitlePaneTextL ();
       
  2059     }
       
  2060 }
       
  2061 
       
  2062 
       
  2063 //=============================================================================
       
  2064 void CImageEditorAppUi::SetFileNameTitlePaneL (const TDesC& aFullFileName)
       
  2065 {
       
  2066 	TParsePtrC parser (aFullFileName); 
       
  2067     SetTitlePaneTextL ( parser.Name() );
       
  2068 }
       
  2069 
       
  2070 //=============================================================================
       
  2071 void CImageEditorAppUi::ClearNaviPaneTextL()
       
  2072 {
       
  2073     if (iEditorView)
       
  2074     {
       
  2075 	    ((CImageEditorUIView *)iEditorView)->ClearNaviPaneTextL();
       
  2076     }
       
  2077 }
       
  2078 
       
  2079 
       
  2080 //=============================================================================
       
  2081 void CImageEditorAppUi::UpdateScreenRectL()
       
  2082     {
       
  2083     LOG(KImageEditorLogFile, "CImageEditorAppUi::UpdateScreenRectL()");
       
  2084     
       
  2085 	if (iEditorView == NULL ||
       
  2086 		iEditorManager == NULL ||
       
  2087 		iImageController == NULL)
       
  2088 		{
       
  2089 		return;
       
  2090 		}
       
  2091 	
       
  2092 	iEditorView->HandleCommandL (EImageEditorPreScreenModeChange);
       
  2093 
       
  2094     // Set Full screen parameter
       
  2095     iResolutionUtil->SetFullScreen(iFullScreen);
       
  2096 
       
  2097 #ifdef LANDSCAPE_SUPPORT
       
  2098     // Set landscape parameter
       
  2099     if (LandscapeScreenOrientation())
       
  2100         {
       
  2101         iResolutionUtil->SetLandscape(ETrue);
       
  2102         }
       
  2103     else
       
  2104         {
       
  2105         iResolutionUtil->SetLandscape(EFalse);
       
  2106         }
       
  2107 #else
       
  2108 #ifdef LANDSCAPE_ONLY
       
  2109 	iResolutionUtil->SetLandscape(ETrue);
       
  2110 #else
       
  2111         iResolutionUtil->SetLandscape(EFalse);
       
  2112 #endif
       
  2113 #endif
       
  2114 
       
  2115     // Update view layout
       
  2116     ((CImageEditorUIView *)iEditorView)->UpdateLayoutL(
       
  2117         iEikonEnv->ScreenDevice()->CurrentScreenMode());
       
  2118 
       
  2119 	LOGFMT(KImageEditorLogFile, "CImageEditorAppUi: CurrentScreenMode() = %d", iEikonEnv->ScreenDevice()->CurrentScreenMode() );
       
  2120 
       
  2121     // Set current screen size to resolution util
       
  2122     iResolutionUtil->UpdateScreenMode();
       
  2123 
       
  2124 	// Check if screen size has changed
       
  2125 	TRect newScreenRect;
       
  2126 	CResolutionUtil::Self()->GetClientRect(newScreenRect);
       
  2127 	if (iOldScreenRect == newScreenRect && !iOrientationChanged)
       
  2128 		{
       
  2129 		LOG(KImageEditorLogFile, "CImageEditorAppUi: Not setting new preview image");
       
  2130 		return;
       
  2131 		}
       
  2132     iOldScreenRect = newScreenRect;
       
  2133  
       
  2134     LOG(KImageEditorLogFile, "CImageEditorAppUi: Setting new preview image");
       
  2135     
       
  2136     // Create screen bitmap for new screen size
       
  2137     iImageController->CreatePreviewBitmapL();
       
  2138     if ((iConstructionState == EConstructionStateReady ||
       
  2139         iConstructionState == EConstructionStateInitUi) &&
       
  2140         !iSaving && iImageLoaded )
       
  2141         {
       
  2142         iEditorManager->SetScreenSizeL();
       
  2143         }
       
  2144    
       
  2145 	//  Set the new image pointer to editor view
       
  2146 	if (!iSaving)
       
  2147 		{
       
  2148 		((CImageEditorUIView *)iEditorView)->SetImageL (
       
  2149 	        iEditorManager->GetPreviewImage()
       
  2150 	        );		
       
  2151 		}
       
  2152 	else // if saving cannot update new image until saving is completed.
       
  2153 		{
       
  2154 		iOrientationChanged = ETrue; 
       
  2155 		((CImageEditorUIView *)iEditorView)->SetImageL ( NULL );
       
  2156 		}
       
  2157 
       
  2158 	//  Set container extent to whole screen
       
  2159 	CCoeControl * control = ((CImageEditorUIView *)iEditorView)->GetContainer();
       
  2160 	if (control)
       
  2161 		{
       
  2162 		if (iFullScreen)
       
  2163 			{
       
  2164 			control->SetExtentToWholeScreen();
       
  2165 			}
       
  2166 		else
       
  2167 			{
       
  2168 			TRect rect;
       
  2169 			iResolutionUtil->GetClientRect(rect);
       
  2170 			control->SetRect(rect);
       
  2171 			}
       
  2172 
       
  2173         if (control->IsFocused())
       
  2174             {
       
  2175             control->DrawNow();
       
  2176             }
       
  2177         }
       
  2178 
       
  2179 	iEditorView->HandleCommandL (EImageEditorPostScreenModeChange);
       
  2180 
       
  2181     }
       
  2182 
       
  2183 
       
  2184 //=============================================================================
       
  2185 void CImageEditorAppUi::ZoomL (const TZoom aZoom)
       
  2186     {
       
  2187     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2188     __ASSERT_ALWAYS( iEditorManager, User::Panic(KComponentName, EImageEditorPanicNoEditorManager) );
       
  2189     SetBusy();
       
  2190 
       
  2191     // Zoom
       
  2192  	iEditorManager->ZoomL (aZoom);
       
  2193 
       
  2194     RenderL();
       
  2195 	iEditorManager->GetSystemParameters();
       
  2196 
       
  2197     // Inform the view about current zoom mode
       
  2198     ((CImageEditorUIView *)iEditorView)->
       
  2199                             SetZoomModeL( iEditorManager->GetZoomMode() );
       
  2200     }
       
  2201 
       
  2202 //=============================================================================
       
  2203 void CImageEditorAppUi::PanL (TDirection aDirection)
       
  2204 {
       
  2205     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2206     __ASSERT_ALWAYS( iEditorManager, User::Panic(KComponentName, EImageEditorPanicNoEditorManager) );
       
  2207 
       
  2208 	iEditorView->HandleCommandL (EImageEditorPreGlobalPanChange);
       
  2209 
       
  2210     SetBusy();
       
  2211 
       
  2212 	// Pan
       
  2213     iEditorManager->PanL (aDirection);
       
  2214 
       
  2215     RenderL();
       
  2216     iEditorManager->GetSystemParameters();
       
  2217 
       
  2218     ResetBusy();
       
  2219 
       
  2220     iEditorView->HandleCommandL (EImageEditorGlobalPanChanged);
       
  2221 }
       
  2222 
       
  2223 //=============================================================================
       
  2224 void CImageEditorAppUi::TouchPanL ()
       
  2225 {
       
  2226     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2227     __ASSERT_ALWAYS( iEditorManager, User::Panic(KComponentName, EImageEditorPanicNoEditorManager) );
       
  2228 
       
  2229 	iEditorView->HandleCommandL (EImageEditorPreGlobalPanChange);
       
  2230 
       
  2231     SetBusy();
       
  2232     
       
  2233     // Fetch directions
       
  2234     TInt xMovement(0);
       
  2235     TInt yMovement(0);
       
  2236     
       
  2237     ((CImageEditorUIView *)iEditorView)->GetTouchPanDirections( xMovement, yMovement );
       
  2238     
       
  2239 	// Pan
       
  2240     iEditorManager->PanL( xMovement, yMovement );
       
  2241 
       
  2242     RenderL();
       
  2243     iEditorManager->GetSystemParameters();
       
  2244 
       
  2245     ResetBusy();
       
  2246 
       
  2247     iEditorView->HandleCommandL (EImageEditorGlobalPanChanged);
       
  2248 }
       
  2249 
       
  2250 //=============================================================================
       
  2251 void CImageEditorAppUi::RotateL (const TRotation aRot)
       
  2252 {
       
  2253 	__ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2254 	__ASSERT_ALWAYS( iEditorManager, User::Panic(KComponentName, EImageEditorPanicNoEditorManager) );
       
  2255 
       
  2256 	if (!iEditorManager->IsPluginLoaded())
       
  2257 	{
       
  2258 		iEditorManager->RotateL (aRot);
       
  2259 		RenderL();
       
  2260 		SetUndoFlag();
       
  2261 		iEditorView->HandleCommandL (EImageEditorGlobalRotationChanged);
       
  2262 	}
       
  2263 }
       
  2264 
       
  2265 //=============================================================================
       
  2266 void CImageEditorAppUi::LaunchLoadWaitNoteL ()
       
  2267 {
       
  2268     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2269     
       
  2270     SetBusy();
       
  2271 	((CImageEditorUIView *)iEditorView)->LaunchWaitDialog( );
       
  2272 
       
  2273 //    if (!iWaitNote)
       
  2274 //    {
       
  2275 //        ((CImageEditorUIView *)iEditorView)->LaunchLoadWaitDialogL( &iWaitNote, this );
       
  2276 //    }
       
  2277 }
       
  2278 
       
  2279 //=============================================================================
       
  2280 void CImageEditorAppUi::LaunchSaveWaitNoteL ()
       
  2281 {
       
  2282     __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2283     TAppUiOrientation orientation(EAppUiOrientationPortrait);
       
  2284     if(LandscapeScreenOrientation())
       
  2285     			{
       
  2286     			orientation = EAppUiOrientationLandscape;
       
  2287     			}
       
  2288     
       
  2289     SetOrientationL(orientation);
       
  2290     if (!iProgressNote)
       
  2291     {
       
  2292         ((CImageEditorUIView *)iEditorView)->LaunchSaveWaitDialogL( &iProgressNote, 100, this );
       
  2293 		iSaving = ETrue;
       
  2294     }
       
  2295 }
       
  2296 
       
  2297 //=============================================================================
       
  2298 void CImageEditorAppUi::KillWaitNote ()
       
  2299 {
       
  2300     
       
  2301 
       
  2302 /*    if (iWaitNote)
       
  2303     {
       
  2304         // If the application is exiting, iWaitNote->ProcessFinishedL()
       
  2305         // apparently does not finish in time and does something after the 
       
  2306         // application has been deleted, causing USER 0 panic on debug builds.
       
  2307         delete iWaitNote;
       
  2308         iWaitNote = NULL;
       
  2309     } */
       
  2310     
       
  2311       if (iProgressNote)
       
  2312           {
       
  2313           delete iProgressNote;
       
  2314           iProgressNote = NULL;
       
  2315           }
       
  2316       else
       
  2317           {
       
  2318           // This can be hidden in all cases. If dialog is not active, nothing 
       
  2319           // special happens
       
  2320           ((CImageEditorUIView *)iEditorView)->HideWaitDialog( );
       
  2321           // Commented due to multiple ResetBusy() call during initialization:
       
  2322           //ResetBusy();
       
  2323           }
       
  2324 }
       
  2325 
       
  2326 
       
  2327 //=============================================================================
       
  2328 void CImageEditorAppUi::IncrementProgressBarL(const TInt aIncrement)
       
  2329 {
       
  2330     __ASSERT_ALWAYS( iProgressNote, User::Panic(KComponentName, EImageEditorPanicNoProgressDialog) );
       
  2331 
       
  2332     CEikProgressInfo * progi = iProgressNote->GetProgressInfoL();
       
  2333     if (progi)
       
  2334     {
       
  2335         progi->IncrementAndDraw (aIncrement);
       
  2336     }
       
  2337 }
       
  2338 
       
  2339 //=============================================================================
       
  2340 void CImageEditorAppUi::DialogDismissedL( TInt aButtonId )
       
  2341 {
       
  2342     LOGFMT(KImageEditorLogFile, "CImageEditorAppUi::DialogDismissedL( %d )", aButtonId);
       
  2343 
       
  2344     if( aButtonId == EAknSoftkeyCancel )
       
  2345     {
       
  2346     
       
  2347     	if ( !iSaving )
       
  2348     	{
       
  2349 			iCallback->Cancel();
       
  2350         	iImageController->CancelOperation(ETrue);
       
  2351     	}
       
  2352     	else
       
  2353     	{
       
  2354         	iImageController->CancelOperation();
       
  2355     	}
       
  2356 
       
  2357 		iWaitNote = NULL;
       
  2358 		
       
  2359 		ResetBusy();
       
  2360     }
       
  2361 }
       
  2362 
       
  2363 //=============================================================================
       
  2364 TErrorHandlerResponse CImageEditorAppUi::HandleError (
       
  2365 	TInt					aError,
       
  2366     const SExtendedError &	aExtErr,
       
  2367 	TDes &					aErrorText,
       
  2368 	TDes &					aContextText
       
  2369 	)
       
  2370 {
       
  2371     LOGFMT(KImageEditorLogFile, "CImageEditorAppUi::HandleError: %d", aError);
       
  2372 
       
  2373 	//	Reset busy flag
       
  2374 	KillWaitNote ();
       
  2375 	ResetBusy();
       
  2376 	iCancelling = EFalse;
       
  2377 	iSaving = EFalse;
       
  2378 
       
  2379     TErrorHandlerResponse ret = ENoDisplay;
       
  2380 
       
  2381     // Handle Image Editor errors
       
  2382     if ( (aError < KSIEEErrorBase) && (aError > KSIEEErrorMax) )
       
  2383 	{
       
  2384         TRAPD ( err, HandleErrorL (aError) );
       
  2385 		if ( err == KErrNoMemory )
       
  2386 		{
       
  2387             Exit();
       
  2388 		}
       
  2389 	}
       
  2390 
       
  2391     // Let the framework handle system errors
       
  2392     else if ( aError == KErrNoMemory )
       
  2393     {
       
  2394 		iImageController->Cancel();
       
  2395         ret = CAknViewAppUi::HandleError (aError, aExtErr, aErrorText, aContextText);
       
  2396         Exit ();
       
  2397     }
       
  2398     // KErrCancel is returned when Bluetooth send is cancelled
       
  2399     // No need to show error dialog
       
  2400     else if ( aError == KErrCancel )
       
  2401     {
       
  2402         // Nothing to do
       
  2403     } 
       
  2404     else if ((aError == KErrArgument) || (aError == KErrInUse))
       
  2405     {
       
  2406     	//	Show error note
       
  2407 		HandleErrorL (aError);
       
  2408     }
       
  2409     //end
       
  2410     else if ( (aError <= KErrExtended) && (aError >= KLeaveExit) ) 
       
  2411     {
       
  2412         ret = CAknViewAppUi::HandleError (aError, aExtErr, aErrorText, aContextText);
       
  2413         // Close plugin if error occurs, otherwise plugin may crash
       
  2414         iEditorManager->ClosePlugin();
       
  2415 
       
  2416         __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2417         TRAP_IGNORE( ((CImageEditorUIView *)iEditorView)->ActivateMainViewL() );
       
  2418 
       
  2419         TRAP_IGNORE( ((CImageEditorUIView *)iEditorView)->SetImageL (
       
  2420             iEditorManager->GetPreviewImage()) );
       
  2421     }
       
  2422     else 
       
  2423     {
       
  2424         ret = CAknViewAppUi::HandleError (aError, aExtErr, aErrorText, aContextText);
       
  2425         // Close plugin if error occurs, otherwise plugin may crash
       
  2426         iEditorManager->ClosePlugin();
       
  2427 
       
  2428         __ASSERT_ALWAYS( iEditorView, User::Panic(KComponentName, EImageEditorPanicNoView) );
       
  2429 
       
  2430         TRAPD( err, ((CImageEditorUIView *)iEditorView)->ActivateMainViewL() );
       
  2431                     
       
  2432         TRAP_IGNORE( ((CImageEditorUIView *)iEditorView)->SetImageL (
       
  2433             iEditorManager->GetPreviewImage()) );
       
  2434         
       
  2435 		if ( err == KErrNoMemory )
       
  2436 		{
       
  2437             Exit();
       
  2438 		}
       
  2439     }
       
  2440     return ret;
       
  2441 }
       
  2442 
       
  2443 //=============================================================================
       
  2444 void CImageEditorAppUi::HandleErrorL (const TInt aError)
       
  2445 {
       
  2446 
       
  2447 	//	Show error note
       
  2448 	ShowErrorNoteL (aError);
       
  2449 
       
  2450 	//	Handle error
       
  2451 	switch ( aError ) 
       
  2452 	{
       
  2453 
       
  2454         case KSIEEErrorOkToExit:
       
  2455         case KSIEEOpenFile:
       
  2456         case KSIEEExifRead:
       
  2457         case KSIEEInternalNonRecoverable:
       
  2458         case KSIEEIncompatibleImage:
       
  2459 		{
       
  2460 			// For some weird reason, in some cases when trying to 
       
  2461 			// exit from CImageEditorAppUi::OperationReadyL, calling 
       
  2462 			// Exit() here won't exit the application.
       
  2463 			RunAppShutter();
       
  2464 			break;
       
  2465 		}
       
  2466 
       
  2467 		default:
       
  2468 		{
       
  2469             // Close plugin if error occurs, otherwise plugin may crash
       
  2470             if (iEditorManager)
       
  2471             {
       
  2472                 iEditorManager->ClosePlugin();
       
  2473 
       
  2474             }
       
  2475             if (iEditorView)
       
  2476             {
       
  2477                 ((CImageEditorUIView *)iEditorView)->ActivateMainViewL();
       
  2478                 ((CImageEditorUIView *)iEditorView)->SetImageL (
       
  2479                 iEditorManager->GetPreviewImage());
       
  2480             }
       
  2481 
       
  2482 			break;
       
  2483 		}
       
  2484 	}
       
  2485 }
       
  2486 
       
  2487 //=============================================================================
       
  2488 void CImageEditorAppUi::ShowErrorNoteL (const TInt aError) const
       
  2489 {
       
  2490 	TInt res_id = -1;
       
  2491 
       
  2492 	switch (aError)
       
  2493 	{
       
  2494         case KErrNotFound:
       
  2495 		case KSIEEOpenFile:
       
  2496         case KSIEEExifRead:
       
  2497         case KSIEEIncompatibleImage:
       
  2498 		{
       
  2499 			res_id = R_ERROR_LOADING;
       
  2500 			break;
       
  2501 		}
       
  2502 		case KSIEESaveFile:
       
  2503         case KSIEEExifUpdate:
       
  2504 		case KErrArgument:
       
  2505 		case KErrInUse:
       
  2506     	
       
  2507 		{
       
  2508 			res_id = R_ERROR_SAVING;
       
  2509 			break;
       
  2510 		}
       
  2511 		case KSIEENotEnoughDiskSpace:
       
  2512 		{
       
  2513 			res_id = R_ERROR_NOT_ENOUGH_DISK_SPACE;
       
  2514 			break;
       
  2515 		}
       
  2516 		case KSIEEProcessFile:
       
  2517 		{
       
  2518 			res_id = R_ERROR_PROCESSING;
       
  2519 			break;
       
  2520 		}
       
  2521 		case KSIEEEngine:
       
  2522 		{
       
  2523 			res_id = R_ERROR_ENGINE;
       
  2524 			break;
       
  2525 		}
       
  2526 		case KSIEEInternal:
       
  2527 		{
       
  2528 			res_id = R_ERROR_INTERNAL;
       
  2529 			break;
       
  2530 		}
       
  2531 		case KSIEEInternalNonRecoverable:
       
  2532 		{
       
  2533 			res_id = R_ERROR_INTERNAL_NON_RECOVERABLE;
       
  2534 			break;
       
  2535 		}
       
  2536         case KSIEEProtectedFile:
       
  2537         {
       
  2538             res_id = R_ERROR_PROTECTED_FILE;
       
  2539             break;
       
  2540         }
       
  2541     	default:
       
  2542 			break;
       
  2543 	}
       
  2544 
       
  2545     if (res_id != - 1)
       
  2546     {
       
  2547 		HBufC * textdata = iEikonEnv->AllocReadResourceLC (res_id);
       
  2548 	    TPtrC ptr = textdata->Des();
       
  2549 
       
  2550         LOGFMT(KImageEditorLogFile, "CImageEditorAppUi::ShowErrorNoteL: \"%S\"", &ptr);
       
  2551         
       
  2552       
       
  2553         CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
       
  2554         TInt noteId = globalNote->ShowNoteL( EAknGlobalErrorNote, ptr );
       
  2555         
       
  2556         User::After(1500042);  // waiting for 1 second
       
  2557 
       
  2558         // discard note
       
  2559         globalNote->CancelNoteL( noteId );
       
  2560 
       
  2561         
       
  2562        CleanupStack::PopAndDestroy(); //textdata 
       
  2563        CleanupStack::PopAndDestroy(); // GlobalNote
       
  2564     }
       
  2565 }
       
  2566 
       
  2567 //=============================================================================
       
  2568 void CImageEditorAppUi::DeleteTempFiles() const
       
  2569 {
       
  2570     // Get the used temp directory path.
       
  2571     // Take drive letter from iDocumentName
       
  2572     TParse parse;
       
  2573     parse.Set (KTempPath, &iDocumentName, NULL);
       
  2574     TPtrC tempDir = parse.DriveAndPath();
       
  2575 
       
  2576     LOGFMT (KImageEditorLogFile, "CImageEditorAppUi::DeleteTempFiles(): %S", &tempDir);
       
  2577     
       
  2578     // Delete all files in the temp directory
       
  2579     _LIT(KAllFiles, "*.*");
       
  2580     TFindFile file_finder (iEikonEnv->FsSession());
       
  2581     CDir* file_list; 
       
  2582     TInt err = file_finder.FindWildByDir (KAllFiles, tempDir, file_list);
       
  2583     while (err==KErrNone)
       
  2584     {
       
  2585         TInt i;
       
  2586         for (i=0; i<file_list->Count(); i++)
       
  2587         {
       
  2588             const TDesC& name = (*file_list)[i].iName;
       
  2589             const TDesC& related = file_finder.File();
       
  2590             TParse fullentry;
       
  2591             fullentry.Set (name, &related, NULL);
       
  2592             BaflUtils::DeleteFile (iEikonEnv->FsSession(), fullentry.FullName());
       
  2593         }
       
  2594         delete file_list;
       
  2595         err=file_finder.FindWild (file_list);
       
  2596     }
       
  2597     
       
  2598 }
       
  2599 
       
  2600 //=============================================================================
       
  2601 void CImageEditorAppUi::FinalizeSaveL()
       
  2602 {
       
  2603     LOG(KImageEditorLogFile, "CImageEditorAppUi::FinalizeSaveL");
       
  2604 
       
  2605 #ifdef FILE_TIME_STAMP_UPDATE    
       
  2606     // Set the timestamp of the saved file to original file's timestamp + 2 seconds.
       
  2607     // The idea is to make the original and edited images appear next to each other.
       
  2608     TEntry original;
       
  2609     TInt err = iEikonEnv->FsSession().Entry( iSourceFileName, original );
       
  2610     if (KErrNone == err)
       
  2611     {
       
  2612         TTime newTime = original.iModified + TTimeIntervalSeconds (2);
       
  2613         
       
  2614         CFileMan* fileMan = CFileMan::NewL( iEikonEnv->FsSession() );
       
  2615         CleanupStack::PushL (fileMan);
       
  2616         fileMan->Attribs(iDocumentName, 0, 0, newTime); // do not set or clear anything, mofify time 
       
  2617         CleanupStack::PopAndDestroy (fileMan);
       
  2618     }
       
  2619 #endif
       
  2620 
       
  2621     LOG(KImageEditorLogFile, "CImageEditorAppUi: Check if source image belongs to an album");
       
  2622    
       
  2623     iSourceImageMgAlbumIdList.Reset();
       
  2624     
       
  2625     LOG(KImageEditorLogFile, "CImageEditorAppUi: publish the filename for provider");
       
  2626     
       
  2627     // Publish & Subscribe API used for delivering document name to AIW provider
       
  2628     TInt err2 = RProperty::Define(KImageEditorProperties, EPropertyFilename, RProperty::EText);
       
  2629     
       
  2630     if (err2 != KErrAlreadyExists)
       
  2631     {
       
  2632         User::LeaveIfError(err2);   
       
  2633     }
       
  2634     
       
  2635     User::LeaveIfError(RProperty::Set(KImageEditorProperties, EPropertyFilename, iDocumentName));
       
  2636     
       
  2637     LOG(KImageEditorLogFile, "CImageEditorAppUi::FinalizeSaveL out");
       
  2638 }
       
  2639 
       
  2640 //=============================================================================
       
  2641 void CImageEditorAppUi::HandleVolumeUpL()
       
  2642 {
       
  2643 	LOG(KImageEditorLogFile, "CImageEditorAppUi::HandleVolumeUpL: in");
       
  2644 
       
  2645 	TKeyEvent key;
       
  2646 	key.iScanCode = EStdKeyIncVolume;
       
  2647 	key.iCode = EKeyIncVolume;
       
  2648 
       
  2649 	if( LandscapeScreenOrientation() )
       
  2650 	{
       
  2651 		key.iScanCode = EStdKeyDecVolume;
       
  2652 		key.iCode = EKeyDecVolume;
       
  2653 	}
       
  2654 
       
  2655 	key.iRepeats = 0;
       
  2656 	iEikonEnv->SimulateKeyEventL( key, EEventKey );
       
  2657 
       
  2658 	LOG(KImageEditorLogFile, "CImageEditorAppUi::HandleVolumeUpL: out");
       
  2659 }
       
  2660 
       
  2661 //=============================================================================
       
  2662 void CImageEditorAppUi::HandleVolumeDownL()
       
  2663 {
       
  2664 	LOG(KImageEditorLogFile, "CImageEditorAppUi::HandleVolumeDownL: in");
       
  2665 
       
  2666 	TKeyEvent key;
       
  2667 	key.iScanCode = EStdKeyDecVolume;
       
  2668 	key.iCode = EKeyDecVolume;
       
  2669 
       
  2670 	if( LandscapeScreenOrientation() )
       
  2671 	{
       
  2672 		key.iScanCode = EStdKeyIncVolume;
       
  2673 		key.iCode = EKeyIncVolume;
       
  2674 	}
       
  2675 
       
  2676 	key.iRepeats = 0;
       
  2677 	iEikonEnv->SimulateKeyEventL( key, EEventKey );
       
  2678 
       
  2679 	LOG(KImageEditorLogFile, "CImageEditorAppUi::HandleVolumeDownL: out");
       
  2680 }
       
  2681 
       
  2682 //=============================================================================
       
  2683 void CImageEditorAppUi::SaveImageOverWriteL(TBool aOverWrite)
       
  2684 {
       
  2685     LOG(KImageEditorLogFile, "CImageEditorAppUi: SaveImageOverWriteL: in");
       
  2686 
       
  2687     // Generate temporary save file name
       
  2688 	TParse tempFileParser;
       
  2689 	tempFileParser.Set (KTempSaveFile, &KTempPath, &iDocumentName); 
       
  2690 	iTempSaveFileName.Copy (tempFileParser.FullName());
       
  2691     BaflUtils::EnsurePathExistsL (iEikonEnv->FsSession(), iTempSaveFileName);
       
  2692 
       
  2693     if (iExitFromApp)
       
  2694     {
       
  2695         TInt exit = 
       
  2696             ((CImageEditorUIView *)iEditorView)->LaunchExitWithoutSavingQueryL();
       
  2697         if (exit)
       
  2698         {
       
  2699             // Exit
       
  2700             User::Leave (KSIEEErrorOkToExit);
       
  2701         }
       
  2702         else
       
  2703         {
       
  2704             iExitFromApp = EFalse;
       
  2705             User::Leave(KSIEEInternal);
       
  2706         }
       
  2707     }
       
  2708 
       
  2709 	// overwrite the existing file
       
  2710 	if (aOverWrite)
       
  2711 	{
       
  2712 		iDocumentName = iSourceFileName; 
       
  2713 	}
       
  2714 	
       
  2715 	// save with a new file name
       
  2716 	else
       
  2717 	{	        		
       
  2718    		// Check document name if not saved yet or memory card removed
       
  2719     	if ( !iImageSaved ||
       
  2720         	 !BaflUtils::PathExists(iEikonEnv->FsSession(), iTempSaveFileName.Left(3)))
       
  2721     	{
       
  2722         	//  If not saved yet, check that document name is valid 
       
  2723 			CheckFileL (iDocumentName);
       
  2724     	}
       
  2725 	}
       
  2726 	
       
  2727 	//	Set temp save image name to engine
       
  2728     iEditorManager->SetImageName (iTempSaveFileName);
       
  2729     		
       
  2730     //	Launch save wait note
       
  2731 	LaunchSaveWaitNoteL ();
       
  2732 
       
  2733     iCallback->DoCallback (KObSaveCallbackID);
       
  2734     LOG(KImageEditorLogFile, "CImageEditorAppUi: SaveImageOverWriteL: out");    
       
  2735 }
       
  2736 
       
  2737 //=============================================================================
       
  2738 TInt CImageEditorAppUi::QueryAndSaveL()
       
  2739     {
       
  2740 
       
  2741     TInt isSaved = 0;
       
  2742     TInt userSelection = 0;
       
  2743     TBool isFileOpen = EFalse;
       
  2744     RFs& fs = iEikonEnv->FsSession();
       
  2745     TInt err = fs.IsFileOpen( iSourceFileName, isFileOpen );
       
  2746             
       
  2747     // Check if .jpg or .jpeg file
       
  2748     TBool isJpgExtension = EFalse;
       
  2749 	TParsePtrC fileParse ( iSourceFileName );
       
  2750 	_LIT( KJpgExtension, ".jpg" );
       
  2751 	_LIT( KJpegExtension, ".jpeg" );
       
  2752 	TPtrC extension( fileParse.Ext() );
       
  2753 	if ( ( extension.MatchF( KJpgExtension ) == KErrNone ) || 
       
  2754 	     ( extension.MatchF( KJpegExtension ) == KErrNone ) )
       
  2755 	    {
       
  2756 	    isJpgExtension = ETrue;
       
  2757 	    }
       
  2758 		
       
  2759     // If source file is open (e.g. in ImageViewer application) don't give
       
  2760     // user an option to replace original but launch directly "Save with a
       
  2761     // new name query. Don't give other that jpg originals.
       
  2762     if ( !isFileOpen && isJpgExtension )
       
  2763         {
       
  2764         // the user selects to save with a new file name
       
  2765         // launch query with choices "Replace original" and 
       
  2766         // "Save with a new file name" 
       
  2767         userSelection = ( ( CImageEditorUIView *)iEditorView )->LaunchSaveImageQueryL();
       
  2768         } 
       
  2769     SetOrientationL(Orientation()); 
       
  2770     SetLayoutAwareApp(EFalse);
       
  2771     // If user has selected "Save with a new file name"
       
  2772     if( userSelection == 0 ) 
       
  2773         {
       
  2774         
       
  2775         // Multiple drive support enabled
       
  2776 #ifdef RD_MULTIPLE_DRIVE
       
  2777         
       
  2778         TDriveNumber driveNumber;
       
  2779         TFileName driveAndPath;
       
  2780         // new multi drive dialog
       
  2781         CAknMemorySelectionDialogMultiDrive* multiDriveDlg = 
       
  2782             CAknMemorySelectionDialogMultiDrive::NewL( ECFDDialogTypeSave, 
       
  2783                                                        EFalse );
       
  2784         CleanupStack::PushL( multiDriveDlg );
       
  2785 
       
  2786         // launch "Select memory" query (mds)
       
  2787         if ( multiDriveDlg->ExecuteL( driveNumber, &driveAndPath, NULL ) )
       
  2788             {        
       
  2789             // Generate a default name for the new file
       
  2790             TInt err = ImageEditorUtils::GenerateNewFileNameL(
       
  2791                                                 iEikonEnv->FsSession(),
       
  2792                                                 iSourceFileName, 
       
  2793                                                 iDocumentName,
       
  2794                                                 driveAndPath,
       
  2795                                                 NULL );
       
  2796                                     
       
  2797             driveAndPath.Append( PathInfo::ImagesPath() );
       
  2798                                    
       
  2799             // launch file name prompt dialog
       
  2800             // the generated name is shown as a default name in dialog
       
  2801             TBuf<160> tmp( iDocumentName.Left(160) );
       
  2802             iSavingQuery = ETrue;
       
  2803             if ( CAknFileNamePromptDialog::RunDlgLD( tmp, /* iDocumentName, */
       
  2804                                                      driveAndPath, 
       
  2805                                                      KNullDesC ) )
       
  2806                 {
       
  2807                 iDocumentName.Copy( tmp );
       
  2808                 iSavingQuery = EFalse;
       
  2809                 // add just queried filename to the path
       
  2810                 driveAndPath.Append( iDocumentName );
       
  2811     
       
  2812                 iDocumentName = driveAndPath;
       
  2813                                                
       
  2814                 SaveImageOverWriteL( EFalse );   
       
  2815                 isSaved = 1;
       
  2816                 }
       
  2817             iSavingQuery = EFalse;
       
  2818             }
       
  2819         CleanupStack::PopAndDestroy( multiDriveDlg );
       
  2820         
       
  2821 #else // No multiple drive support
       
  2822         CAknMemorySelectionDialog::TMemory selectedMemory(
       
  2823                             CAknMemorySelectionDialog::EPhoneMemory );
       
  2824     
       
  2825         // launch "Select memory" query
       
  2826         if ( CAknMemorySelectionDialog::RunDlgLD( selectedMemory ) )
       
  2827             {    
       
  2828             // create path for the image
       
  2829             TFileName driveAndPath;         
       
  2830             ImageEditorUtils::TMemorySelection memorySelection = 
       
  2831                                             ImageEditorUtils::ESelectPhone;
       
  2832             if( selectedMemory == CAknMemorySelectionDialog::EPhoneMemory )
       
  2833                 {
       
  2834                 memorySelection = ImageEditorUtils::ESelectPhone;
       
  2835                 driveAndPath.Copy( PathInfo::PhoneMemoryRootPath() );
       
  2836                 driveAndPath.Append( PathInfo::ImagesPath() );
       
  2837                 }
       
  2838             else if ( selectedMemory == 
       
  2839                       CAknMemorySelectionDialog::EMemoryCard )
       
  2840                 {
       
  2841                 memorySelection = ImageEditorUtils::ESelectMmc;
       
  2842                 driveAndPath.Copy( PathInfo::MemoryCardRootPath() );
       
  2843                 driveAndPath.Append( PathInfo::ImagesPath() );
       
  2844                 }         
       
  2845 
       
  2846             TInt err = ImageEditorUtils::GenerateNewDocumentNameL(
       
  2847                                                 iEikonEnv->FsSession(), 
       
  2848                                                 iSourceFileName, 
       
  2849                                                 iDocumentName,
       
  2850                                                 &iSourceImageMgAlbumIdList,
       
  2851                                                 NULL,
       
  2852                                                 memorySelection );     
       
  2853             
       
  2854             
       
  2855             // launch file name prompt dialog
       
  2856             // the generated name is shown as a default name
       
  2857             iSavingQuery = ETrue;
       
  2858             if ( CAknFileNamePromptDialog::RunDlgLD( iDocumentName,
       
  2859                                                      driveAndPath, 
       
  2860                                                      KNullDesC ) )
       
  2861                 {
       
  2862                 iSavingQuery = EFalse;
       
  2863                 TInt err = ImageEditorUtils::GenerateFilePathL(
       
  2864                                                 iEikonEnv->FsSession(),
       
  2865                                                 iSourceFileName, 
       
  2866                                                 iDocumentName,
       
  2867                                                 memorySelection
       
  2868                                                 );
       
  2869                                                     
       
  2870                 SaveImageOverWriteL( EFalse );   
       
  2871                 isSaved = 1;
       
  2872                 }
       
  2873             iSavingQuery = EFalse;
       
  2874             }
       
  2875 #endif
       
  2876         }
       
  2877     // the user selects to overwrite the original image
       
  2878     else if ( userSelection == 1 )
       
  2879         {
       
  2880         SaveImageOverWriteL( ETrue );
       
  2881         isSaved = 1;
       
  2882         }
       
  2883     return isSaved;
       
  2884     }
       
  2885 
       
  2886 // ----------------------------------------------------------------------------
       
  2887 // CImageEditorAppUi::HandleVolumeKeyEvent
       
  2888 // ----------------------------------------------------------------------------
       
  2889 //
       
  2890 void CImageEditorAppUi::HandleVolumeKeyEvent( TRemConCoreApiOperationId aOperationId,
       
  2891         							 		  TRemConCoreApiButtonAction /* aButtonAct */ )
       
  2892     {
       
  2893     if (((CImageEditorUIView *)iEditorView)->GetContainer())
       
  2894     {
       
  2895         if(! ((CImageEditorUIView *)iEditorView)->GetContainer()->IsFocused() )
       
  2896      	   {
       
  2897      	   return;
       
  2898      	   }
       
  2899     }
       
  2900     
       
  2901     if( iForeground )
       
  2902     	{
       
  2903     	TKeyEvent keyEvent;
       
  2904     	if( aOperationId == ERemConCoreApiVolumeUp )
       
  2905     		{
       
  2906     		keyEvent.iScanCode = EStdKeyIncVolume;
       
  2907     		TRAP_IGNORE( HandleKeyEventL( keyEvent, EEventKey ) );
       
  2908     		}
       
  2909     	else if( aOperationId == ERemConCoreApiVolumeDown )
       
  2910     		{
       
  2911     		keyEvent.iScanCode = EStdKeyDecVolume;
       
  2912     		TRAP_IGNORE( HandleKeyEventL( keyEvent, EEventKey ) );
       
  2913     		}
       
  2914     	}
       
  2915     }
       
  2916 
       
  2917 // End of File