meetingrequest/mrgui/mrfieldbuilderplugin/src/cmrattachmentcommandhandler.cpp
branchRCL_3
changeset 12 4ce476e64c59
child 16 b5fbb9b25d57
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  MR attahcment field implementation
       
    15  *
       
    16  */
       
    17 
       
    18 #include "cmrattachmentcommandhandler.h"
       
    19 #include "cmropenattachmentcommand.h"
       
    20 #include "cmrremoveattachmentcommand.h"
       
    21 #include "cmrsaveattachmentcommand.h"
       
    22 #include "cmrsaveandopenattachmentcommand.h"
       
    23 #include "cesmrrichtextlink.h"
       
    24 #include "mcalremoteattachmentinfo.h"
       
    25 #include "mcalremoteattachment.h"
       
    26 #include "mcalremoteattachmentoperation.h"
       
    27 #include "ccalremoteattachmentinfo.h"
       
    28 #include "ccalremoteattachmentapi.h"
       
    29 #include "cesmrgenericfieldevent.h"
       
    30 #include "cesmrfieldcommandevent.h"
       
    31 #include "mesmrfieldeventqueue.h"
       
    32 #include "esmrdef.h"
       
    33 
       
    34 #include <esmrgui.rsg>
       
    35 #include <calentry.h>
       
    36 #include <calattachment.h>
       
    37 #include <f32file.h>
       
    38 #include <apgcli.h>
       
    39 #include <documenthandler.h>
       
    40 #include <stringloader.h>
       
    41 #include <AknGlobalNote.h>
       
    42 
       
    43 // DEBUG
       
    44 #include "emailtrace.h"
       
    45 
       
    46 
       
    47 // unnamed namespace for local definitions
       
    48 namespace { // codescanner::namespace
       
    49 
       
    50 _LIT8( KUnknownDatatype, "unknown");
       
    51 
       
    52 #if defined(_DEBUG)
       
    53 
       
    54 // Panic literal
       
    55 _LIT( KMRAttachmentCommandHandler, "MRAttachmentCommandHandler" );
       
    56 
       
    57 /**
       
    58  * Panic codes
       
    59  */
       
    60 enum TMRAttachmentCommandHandlerPanic
       
    61     {
       
    62     // Invalid command
       
    63     EMRAttachmentCommandHandlerInvalidCommand,
       
    64     // Attachment is not found
       
    65     EMRAttachmentCommandHandlerAttachmentNotFound,
       
    66     // Remote attachment information not set
       
    67     EMRAttachmentCommandHandlerRemoteInfoNotSet,
       
    68     // Remote command cannot be found
       
    69     EMRAttachmentCommandHandlerRemoteCommandNotFound,
       
    70     // Remote attachment is not found
       
    71     EMRAttachmentCommandHandlerRemoteAttachmentNotFound
       
    72     };
       
    73 
       
    74 void Panic( TMRAttachmentCommandHandlerPanic aPanic )
       
    75     {
       
    76     User::Panic( KMRAttachmentCommandHandler, aPanic );
       
    77     }
       
    78 
       
    79 #endif // _DEBUG
       
    80 
       
    81 /**
       
    82  * Shows information note when one attachment is saved.
       
    83  */
       
    84 void ShowInfoNoteL()
       
    85     {
       
    86     CAknGlobalNote* note = CAknGlobalNote::NewLC();
       
    87     HBufC* text =
       
    88             StringLoader::LoadLC( R_MEET_REQ_INFO_NOTE_ONE_ATTACHMENT_SAVED );
       
    89     note->ShowNoteL(
       
    90             EAknGlobalInformationNote,
       
    91             *text );
       
    92     CleanupStack::PopAndDestroy( 2, note );
       
    93     }
       
    94 
       
    95 /**
       
    96  * Shows information note when multiple attachments have been saved.
       
    97  */
       
    98 void ShowInfoNoteL( TInt aAttachmentCount )
       
    99     {
       
   100     CAknGlobalNote* note = CAknGlobalNote::NewLC();
       
   101     HBufC* text = StringLoader::LoadLC(
       
   102             R_MEET_REQ_INFO_NOTE_MULTIPLE_ATTACHMENTS_SAVED,
       
   103             aAttachmentCount );
       
   104     note->ShowNoteL(
       
   105                 EAknGlobalInformationNote,
       
   106                 *text );
       
   107     CleanupStack::PopAndDestroy( 2, note );
       
   108     }
       
   109 
       
   110 } // namespace
       
   111 
       
   112 // ======== MEMBER FUNCTIONS ========
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CMRAttachmentCommandHandler::CMRAttachmentCommandHandler
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 CMRAttachmentCommandHandler::CMRAttachmentCommandHandler(
       
   119         CCalEntry& aEntry,
       
   120         MESMRFieldEventQueue& aEventQueue) :
       
   121     iEntry( aEntry ),
       
   122     iEventQueue( aEventQueue )
       
   123     {
       
   124     FUNC_LOG;
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // CMRAttachmentCommandHandler::~CMRAttachmentCommandHandler
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 CMRAttachmentCommandHandler::~CMRAttachmentCommandHandler()
       
   132     {
       
   133     FUNC_LOG;
       
   134 
       
   135     iDownloadOperations.ResetAndDestroy();
       
   136     delete iDocHandler;
       
   137     }
       
   138 
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CMRAttachmentCommandHandler::NewL
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 CMRAttachmentCommandHandler* CMRAttachmentCommandHandler::NewL(
       
   145             CCalEntry& aEntry,
       
   146             MESMRFieldEventQueue& aEventQueue )
       
   147     {
       
   148     FUNC_LOG;
       
   149 
       
   150     CMRAttachmentCommandHandler* self =
       
   151             new (ELeave) CMRAttachmentCommandHandler( aEntry, aEventQueue );
       
   152     CleanupStack::PushL( self );
       
   153     self->ConstructL();
       
   154     CleanupStack::Pop( self );
       
   155     return self;
       
   156     }
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // CMRAttachmentCommandHandler::ConstructL
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 void CMRAttachmentCommandHandler::ConstructL()
       
   163     {
       
   164     FUNC_LOG;
       
   165 
       
   166     iDocHandler = CDocumentHandler::NewL();
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CMRAttachmentCommandHandler::HandleAttachmentCommandL
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void CMRAttachmentCommandHandler::HandleAttachmentCommandL(
       
   174         TInt aCommandId,
       
   175         const CESMRRichTextLink& aSelectedLink )
       
   176     {
       
   177     FUNC_LOG;
       
   178 
       
   179     TInt attachmentIndex( ResolveAttachmentIndexL( aSelectedLink.Value() ) );
       
   180     HandleAttachmentCommandInternalL( aCommandId, attachmentIndex );
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // CMRAttachmentCommandHandler::IsRemoteAttachmentL
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 TBool CMRAttachmentCommandHandler::IsRemoteAttachmentL(
       
   188         const CESMRRichTextLink& aSelectedLink )
       
   189     {
       
   190     FUNC_LOG;
       
   191 
       
   192     TInt index = ResolveAttachmentIndexL( aSelectedLink.Value() );
       
   193 
       
   194     CCalAttachment* attachment = iEntry.AttachmentL( index );
       
   195 
       
   196     TBool remoteAttachment( ETrue );
       
   197 
       
   198     if ( CCalAttachment::EFile == attachment->Type() )
       
   199         {
       
   200         remoteAttachment = EFalse;
       
   201         }
       
   202 
       
   203     return remoteAttachment;
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CMRAttachmentCommandHandler::SetRemoteAttachmentInformationL
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 void CMRAttachmentCommandHandler::SetRemoteAttachmentInformationL(
       
   211             CCalRemoteAttachmentApi& aRemoteAttaApi,
       
   212             CCalRemoteAttachmentInfo& aAttachmentInfo )
       
   213     {
       
   214     FUNC_LOG;
       
   215 
       
   216     iRemoteAttachmentApi = &aRemoteAttaApi;
       
   217     iAttachmentInfo = &aAttachmentInfo;
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CMRAttachmentCommandHandler::SetRemoteAttachmentInformationL
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CMRAttachmentCommandHandler::HandleRemoteAttachmentCommandL(
       
   225         TInt aCommandId,
       
   226         const CESMRRichTextLink& aSelectedLink )
       
   227     {
       
   228     FUNC_LOG;
       
   229 
       
   230     // Check that remote attachment information is being set
       
   231     __ASSERT_DEBUG(
       
   232             iRemoteAttachmentApi,
       
   233             Panic(EMRAttachmentCommandHandlerRemoteInfoNotSet) );
       
   234 
       
   235     __ASSERT_DEBUG(
       
   236             iAttachmentInfo,
       
   237             Panic(EMRAttachmentCommandHandlerRemoteInfoNotSet) );
       
   238 
       
   239     HandleRemoteAttachmentCommandInternalL(
       
   240             aCommandId,
       
   241             aSelectedLink.Value() );
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CMRAttachmentCommandHandler::RemoteOperations
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 RPointerArray<MCalRemoteAttachmentOperation>&
       
   249         CMRAttachmentCommandHandler::RemoteOperations()
       
   250     {
       
   251     FUNC_LOG;
       
   252     return iDownloadOperations;
       
   253     }
       
   254 
       
   255 // ---------------------------------------------------------------------------
       
   256 // CMRAttachmentCommandHandler::CurrentCommandInProgress
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 TInt CMRAttachmentCommandHandler::CurrentCommandInProgress() const
       
   260     {
       
   261     FUNC_LOG;
       
   262 
       
   263     return iCommandInProgress;
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // CMRAttachmentCommandHandler::Progress
       
   268 // ---------------------------------------------------------------------------
       
   269 //
       
   270 void CMRAttachmentCommandHandler::Progress(
       
   271          MCalRemoteAttachmentOperation* aOperation,
       
   272          TInt aPercentageCompleted )
       
   273     {
       
   274     FUNC_LOG;
       
   275 
       
   276     TRAP_IGNORE( NotifyProgressL(
       
   277             aOperation->AttachmentInformation().AttachmentLabel(),
       
   278             aPercentageCompleted ));
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CMRAttachmentCommandHandler::OperationCompleted
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CMRAttachmentCommandHandler::OperationCompleted(
       
   286         MCalRemoteAttachmentOperation* aOperation,
       
   287         RFile& aAttachment )
       
   288     {
       
   289     FUNC_LOG;
       
   290 
       
   291     TRAP_IGNORE( HandleOperationCompletedL( aOperation, aAttachment ) );
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // CMRAttachmentCommandHandler::OperationError
       
   296 // ---------------------------------------------------------------------------
       
   297 //
       
   298 void CMRAttachmentCommandHandler::OperationError(
       
   299         MCalRemoteAttachmentOperation* /*aOperation*/,
       
   300         TInt aErrorCode )
       
   301     {
       
   302     FUNC_LOG;
       
   303 
       
   304     // Operation failed or cancelled, hide download indicator
       
   305     TRAP_IGNORE( HideDownloadIndicatorL() );
       
   306 	iCommandInProgress = 0;
       
   307 
       
   308 	if ( aErrorCode != KErrCancel )
       
   309         {
       
   310         // Operation failed, show error note
       
   311         CCoeEnv::Static()->HandleError( aErrorCode );
       
   312         }
       
   313     }
       
   314 
       
   315 // ---------------------------------------------------------------------------
       
   316 // CMRAttachmentCommandHandler::ResolveAttachmentIndexL
       
   317 // ---------------------------------------------------------------------------
       
   318 //
       
   319 TInt CMRAttachmentCommandHandler::ResolveAttachmentIndexL(
       
   320         const TDesC& aAttachmentLabel )
       
   321     {
       
   322     FUNC_LOG;
       
   323 
       
   324     TInt index( KErrNotFound );
       
   325 
       
   326     TInt attachmentCount( iEntry.AttachmentCountL() );
       
   327     for (TInt i(0); i < attachmentCount && KErrNotFound == index; ++i )
       
   328         {
       
   329         CCalAttachment* attachment = iEntry.AttachmentL(i);
       
   330 
       
   331         TPtrC attachmentLabel( attachment->Label() );
       
   332         if ( !aAttachmentLabel.Compare(attachmentLabel) )
       
   333             {
       
   334             index = i;
       
   335             }
       
   336         }
       
   337 
       
   338     // Check that attachment is always found
       
   339     // If attachment is not found --> Our data is corrupted
       
   340     __ASSERT_DEBUG(
       
   341             KErrNotFound != index,
       
   342             Panic( EMRAttachmentCommandHandlerAttachmentNotFound)  );
       
   343 
       
   344     return index;
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // CMRAttachmentCommandHandler::ResolveAttachmentIndexL
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 TInt CMRAttachmentCommandHandler::CommandIndex(
       
   352         const TDesC& aAttachmentLabel )
       
   353     {
       
   354     FUNC_LOG;
       
   355 
       
   356     TInt index( KErrNotFound );
       
   357 
       
   358     TInt commandCount( iDownloadOperations.Count() );
       
   359     for ( TInt i(0); i < commandCount && KErrNotFound == index; ++i )
       
   360         {
       
   361         // There are download operations in progress
       
   362         const MCalRemoteAttachment& attachInfo =
       
   363                 iDownloadOperations[i]->AttachmentInformation();
       
   364 
       
   365         TPtrC label( attachInfo.AttachmentLabel() );
       
   366         if ( !label.Compare( aAttachmentLabel ) )
       
   367             {
       
   368             index = i;
       
   369             }
       
   370         }
       
   371 
       
   372     return index;
       
   373     }
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // CMRAttachmentCommandHandler::RemoteAttachmentIndex
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 TInt CMRAttachmentCommandHandler::RemoteAttachmentIndexL(
       
   380         const TDesC& aAttachmentLabel )
       
   381     {
       
   382     FUNC_LOG;
       
   383 
       
   384     TInt index( KErrNotFound );
       
   385 
       
   386     TInt remoteAttCount( iAttachmentInfo->AttachmentCount() );
       
   387     for ( TInt i(0); i < remoteAttCount && index == KErrNotFound; ++i )
       
   388         {
       
   389         TPtrC remoteAttName( iAttachmentInfo->AttachmentL(i).AttachmentLabel() );
       
   390 
       
   391         if ( !remoteAttName.Compare( aAttachmentLabel ) )
       
   392             {
       
   393             index = i;
       
   394             }
       
   395         }
       
   396 
       
   397     return index;
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CMRAttachmentCommandHandler::HandleOperationCompleted
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void CMRAttachmentCommandHandler::HandleOperationCompletedL(
       
   405         MCalRemoteAttachmentOperation* aOperation,
       
   406         RFile& aAttachment )
       
   407     {
       
   408     FUNC_LOG;
       
   409 
       
   410     // Notify 'download ready' event
       
   411     CESMRFieldCommandEvent* event = CESMRFieldCommandEvent::NewLC(
       
   412             NULL,
       
   413             EMRCmdHideAttachmentIndicator );
       
   414 
       
   415     iEventQueue.NotifyEventAsyncL( event );
       
   416 
       
   417     CleanupStack::Pop( event );
       
   418 
       
   419     // First search the correct operation from the list
       
   420     TInt operationIndex( CommandIndex(aOperation->AttachmentInformation().AttachmentLabel() ) );
       
   421 
       
   422     __ASSERT_DEBUG(
       
   423             operationIndex != KErrNotFound,
       
   424             Panic( EMRAttachmentCommandHandlerRemoteCommandNotFound) );
       
   425 
       
   426     // Resolve current download operation in progress
       
   427     MCalRemoteAttachmentOperation* operation = iDownloadOperations[ operationIndex ];
       
   428     CleanupDeletePushL( operation );
       
   429     iDownloadOperations.Remove( operationIndex );
       
   430 
       
   431     TInt attachmentIndex( ResolveAttachmentIndexL(
       
   432             operation->AttachmentInformation().AttachmentLabel() ) );
       
   433 
       
   434     if ( KErrNotFound == attachmentIndex )
       
   435         {
       
   436         User::Leave( KErrNotFound );
       
   437         }
       
   438 
       
   439     if ( EESMRViewerCancelAttachmentDownload != iCommandInProgress )
       
   440         {
       
   441         // Update the remote attachment to be local attachment ...
       
   442         UpdateLocalAttachmentL(
       
   443                 operation->AttachmentInformation().AttachmentLabel(),
       
   444                 aAttachment,
       
   445                 attachmentIndex );
       
   446         }
       
   447 
       
   448     // Then execute normal local file attachment command
       
   449     if ( EESMRViewerOpenAttachment == iCommandInProgress ||
       
   450          EESMRViewerSaveAttachment == iCommandInProgress ||
       
   451          EESMRViewerSaveAllAttachments == iCommandInProgress )
       
   452         {
       
   453         TInt attachmentIndex(
       
   454                 ResolveAttachmentIndexL(
       
   455                         operation->AttachmentInformation().AttachmentLabel() ) );
       
   456 
       
   457         if ( EESMRViewerOpenAttachment == iCommandInProgress )
       
   458             {
       
   459             // When opening attachment from remote storage --> Ask user to
       
   460             // save attachment first as well.
       
   461             HandleAttachmentCommandInternalL(
       
   462                     EESMRViewerOpenAndSaveAttachment, attachmentIndex );
       
   463 
       
   464             iCommandInProgress = 0;
       
   465             }
       
   466         else
       
   467             {
       
   468             HandleAttachmentCommandInternalL(
       
   469                     EESMRViewerSaveAttachment, attachmentIndex );
       
   470 
       
   471             if ( EESMRViewerSaveAllAttachments == iCommandInProgress &&
       
   472                  iAttachmentInfo->AttachmentCount() )
       
   473                 {
       
   474                 iCommandInProgress = 0;
       
   475 
       
   476                 // There are more attachments to be downloaded --> Download next
       
   477                 SaveNextRemoteAttachmentL();
       
   478                 }
       
   479             else
       
   480                 {
       
   481                 if ( !iAttachmentInfo->AttachmentCount() )
       
   482                     {
       
   483                     // All remote attachments have been saved. Show info note. 
       
   484                     ShowInfoNoteL( iEntry.AttachmentCountL() );
       
   485                     }
       
   486                 iCommandInProgress = 0;
       
   487                 }
       
   488             }
       
   489         }
       
   490 
       
   491     CleanupStack::PopAndDestroy( operation );
       
   492     }
       
   493 
       
   494 // ---------------------------------------------------------------------------
       
   495 // CMRAttachmentCommandHandler::HandleAttachmentCommandInternalL
       
   496 // ---------------------------------------------------------------------------
       
   497 //
       
   498 void CMRAttachmentCommandHandler::HandleAttachmentCommandInternalL(
       
   499             TInt aCommandId,
       
   500             TInt aAttachmentIndex )
       
   501     {
       
   502     FUNC_LOG;
       
   503 
       
   504     if ( iDownloadOperations.Count() )
       
   505         {
       
   506         // Cancel ongoing remote operations ...
       
   507         iDownloadOperations.ResetAndDestroy();
       
   508         }
       
   509 
       
   510     CMRAttachmentCommand* command = NULL;
       
   511 
       
   512     // Creating command
       
   513     switch ( aCommandId )
       
   514         {
       
   515         case EESMRViewerOpenAttachment:
       
   516         case EESMREditorOpenAttachment:
       
   517             {
       
   518             command = CMROpenAttachmentCommand::NewL( *iDocHandler );
       
   519             }
       
   520             break;
       
   521         case EESMREditorRemoveAttachment:
       
   522         case EESMREditorRemoveAllAttachments:
       
   523             {
       
   524             command = CMRRemoveAttachmentCommand::NewL();
       
   525             }
       
   526             break;
       
   527         case EESMRViewerSaveAttachment:
       
   528             {
       
   529             command = CMRSaveAttachmentCommand::NewL( *iDocHandler );
       
   530             }
       
   531             break;
       
   532         case EESMRViewerOpenAndSaveAttachment:
       
   533             {
       
   534             command = CMRSaveAndOpenAttachmentCommand::NewL( *iDocHandler );
       
   535             }
       
   536             break;
       
   537         case EESMRViewerSaveAllAttachments:
       
   538             {
       
   539             SaveAllAttachmentsL();
       
   540             }
       
   541             break;
       
   542         default:
       
   543             break;
       
   544         }
       
   545 
       
   546     if ( command )
       
   547         {
       
   548         CleanupDeletePushL( command );
       
   549 
       
   550         // Executing command. TRAP errors so cancellation can be handled
       
   551         // without an extra error note.
       
   552         TRAPD( error,
       
   553                 ExecuteCommandL( aCommandId, *command, aAttachmentIndex ) )
       
   554 
       
   555         if ( error != KErrCancel )
       
   556             {
       
   557             User::LeaveIfError( error );
       
   558             
       
   559             if ( aCommandId == EESMRViewerSaveAttachment
       
   560                  && iCommandInProgress != EESMRViewerSaveAllAttachments )
       
   561                 {
       
   562                 ShowInfoNoteL();
       
   563                 }
       
   564             }
       
   565         else // KErrCancel
       
   566             {
       
   567             iCommandInProgress = 0;
       
   568             }
       
   569 
       
   570         CleanupStack::PopAndDestroy( command );
       
   571         }
       
   572     }
       
   573 
       
   574 // ---------------------------------------------------------------------------
       
   575 // CMRAttachmentCommandHandler::UpdateLocalAttachmentL
       
   576 // ---------------------------------------------------------------------------
       
   577 //
       
   578 void CMRAttachmentCommandHandler::UpdateLocalAttachmentL(
       
   579         const TDesC& aAttachmentLabel,
       
   580         RFile& aAttachment,
       
   581         TInt aAttachmentIndex )
       
   582     {
       
   583     FUNC_LOG;
       
   584 
       
   585     // Create file attachment and add to calendar entry
       
   586     CCalAttachment* fileAttachment = CCalAttachment::NewFileL( aAttachment );
       
   587     CleanupStack::PushL( fileAttachment );
       
   588 
       
   589     RApaLsSession apaSession;
       
   590     TDataRecognitionResult dataType;
       
   591     User::LeaveIfError( apaSession.Connect() );
       
   592     apaSession.RecognizeData( aAttachment, dataType );
       
   593     apaSession.Close();
       
   594 
       
   595     // set attachment properties
       
   596     TPtrC8 contentType( dataType.iDataType.Des8() );
       
   597 
       
   598     if ( contentType.Length() )
       
   599         {
       
   600         fileAttachment->SetMimeTypeL( contentType );
       
   601         }
       
   602     else
       
   603         {
       
   604         fileAttachment->SetMimeTypeL( KUnknownDatatype );
       
   605         }
       
   606 
       
   607     fileAttachment->SetLabelL( aAttachmentLabel );
       
   608     iEntry.AddAttachmentL( *fileAttachment ); // calEntry takes ownership
       
   609     CleanupStack::Pop( fileAttachment );
       
   610 
       
   611     // Remove remote attachment
       
   612     CCalAttachment* attachment = iEntry.AttachmentL( aAttachmentIndex );
       
   613     iEntry.DeleteAttachmentL( *attachment );
       
   614     attachment = NULL;
       
   615 
       
   616     TInt remoteAttachmentIndex( RemoteAttachmentIndexL( aAttachmentLabel ) );
       
   617     if ( KErrNotFound != remoteAttachmentIndex )
       
   618         {
       
   619         iAttachmentInfo->RemoveAttachmentAtL( remoteAttachmentIndex );
       
   620         }
       
   621     }
       
   622 
       
   623 // ---------------------------------------------------------------------------
       
   624 // CMRAttachmentCommandHandler::NotifyProgressL
       
   625 // ---------------------------------------------------------------------------
       
   626 //
       
   627 void CMRAttachmentCommandHandler::NotifyProgressL(
       
   628             const TDesC& aAttachmentLabel,
       
   629             TInt aPercentageCompleted )
       
   630     {
       
   631     FUNC_LOG;
       
   632 
       
   633     CDesCArray* descArray = new (ELeave) CDesCArrayFlat( 1 );
       
   634     CleanupStack::PushL( descArray );
       
   635     descArray->AppendL( aAttachmentLabel );
       
   636     CArrayFix<TInt>* intArray = new (ELeave) CArrayFixFlat<TInt>( 1 );
       
   637     CleanupStack::PushL( intArray );
       
   638     intArray->AppendL( aPercentageCompleted );
       
   639 
       
   640     HBufC* statusText = StringLoader::LoadL(
       
   641             R_QTN_MEET_REQ_ATTACHMENTS_DOWNLOAD_PREFIX,
       
   642             *descArray,
       
   643             *intArray );
       
   644 
       
   645     CleanupStack::PushL( statusText );
       
   646 
       
   647     // Create field change event
       
   648     CESMRGenericFieldEvent* event = CESMRGenericFieldEvent::NewLC(
       
   649             NULL,
       
   650             MESMRFieldEvent::EESMRFieldChangeEvent );
       
   651 
       
   652     // Add this field as parameter
       
   653     TInt fieldId = EESMRFieldViewerAttachments;
       
   654     CESMRFieldEventValue* field = CESMRFieldEventValue::NewLC(
       
   655             MESMRFieldEventValue::EESMRInteger,
       
   656             &fieldId );
       
   657     event->AddParamL( field );
       
   658     CleanupStack::Pop( field );
       
   659 
       
   660     // Add new value as parameter
       
   661     CESMRFieldEventValue* value = CESMRFieldEventValue::NewLC(
       
   662             CESMRFieldEventValue::EESMRString,
       
   663             statusText );
       
   664 
       
   665     // Encapsulate value, so receiver gets the field value type
       
   666     event->AddParamL( value, ETrue );
       
   667     CleanupStack::Pop( value );
       
   668 
       
   669     // Send event
       
   670     iEventQueue.NotifyEventAsyncL( event );
       
   671     CleanupStack::Pop( event );
       
   672 
       
   673     // Ownership transferred to field event value
       
   674     CleanupStack::Pop( statusText );
       
   675     CleanupStack::PopAndDestroy( intArray );
       
   676     CleanupStack::PopAndDestroy( descArray );
       
   677     }
       
   678 
       
   679 // ---------------------------------------------------------------------------
       
   680 // CMRAttachmentCommandHandler::HandleRemoteAttachmentCommandInternalL
       
   681 // ---------------------------------------------------------------------------
       
   682 //
       
   683 void CMRAttachmentCommandHandler::HandleRemoteAttachmentCommandInternalL(
       
   684         TInt aCommandId,
       
   685         const TDesC& aRemoteAttachmentLabel )
       
   686     {
       
   687     FUNC_LOG;
       
   688 
       
   689     // Fetch correct attachment from the entry
       
   690     TInt index = ResolveAttachmentIndexL( aRemoteAttachmentLabel );
       
   691     CCalAttachment* attachment = iEntry.AttachmentL( index );
       
   692 
       
   693     if ( CCalAttachment::EFile == attachment->Type() )
       
   694         {
       
   695         // This is local attachment ...
       
   696         User::Leave( KErrArgument );
       
   697         }
       
   698 
       
   699     switch ( aCommandId )
       
   700         {
       
   701         case EESMRViewerOpenAttachment:
       
   702         case EESMRViewerSaveAttachment:
       
   703             {
       
   704             TInt remoteAttIndex(
       
   705                     RemoteAttachmentIndexL( aRemoteAttachmentLabel ) );
       
   706 
       
   707             RPointerArray<MCalRemoteAttachment> attachmentArray;
       
   708             CleanupClosePushL( attachmentArray );
       
   709             const MCalRemoteAttachment& remoteAttachment(
       
   710                     iAttachmentInfo->AttachmentL(remoteAttIndex) );
       
   711             attachmentArray.Append( &remoteAttachment );
       
   712 
       
   713             if ( !iCommandInProgress )
       
   714                 {
       
   715                 // There are no ongoing command for this attachment
       
   716                 // ==> We can start new one ...
       
   717 
       
   718                 iCommandInProgress = aCommandId;
       
   719 
       
   720                 // Before we can execute the actual command, we need to be sure
       
   721                 // that attachments are being downloaded to device.
       
   722                 iRemoteAttachmentApi->DownloadAttachmentsL(
       
   723                         attachmentArray,
       
   724                         iDownloadOperations,
       
   725                         *this );
       
   726                 }
       
   727 
       
   728             CleanupStack::PopAndDestroy( &attachmentArray );
       
   729             }
       
   730             break;
       
   731 
       
   732         case EESMRViewerCancelAttachmentDownload:
       
   733             {
       
   734             if ( EESMRViewerSaveAllAttachments == iCommandInProgress )
       
   735                 {
       
   736                 iCommandInProgress = aCommandId;
       
   737                 iDownloadOperations.ResetAndDestroy();
       
   738                 }
       
   739             else
       
   740                 {
       
   741                 // Cancelling is done by simply deleting the command
       
   742                 iCommandInProgress = aCommandId;
       
   743                 TInt remoteCommand( CommandIndex(
       
   744                         aRemoteAttachmentLabel ));
       
   745 
       
   746                 MCalRemoteAttachmentOperation* command =
       
   747                         iDownloadOperations[remoteCommand];
       
   748 
       
   749                 iDownloadOperations.Remove( remoteCommand );
       
   750                 delete command;
       
   751                 }
       
   752             }
       
   753             break;
       
   754         case EESMRViewerSaveAllAttachments:
       
   755             {
       
   756             SaveAllAttachmentsL();
       
   757             }
       
   758             break;
       
   759         default:
       
   760             break;
       
   761         }
       
   762     }
       
   763 
       
   764 // ---------------------------------------------------------------------------
       
   765 // CMRAttachmentCommandHandler::HideDownloadIndicatorL
       
   766 // ---------------------------------------------------------------------------
       
   767 //
       
   768 void CMRAttachmentCommandHandler::HideDownloadIndicatorL()
       
   769     {
       
   770     FUNC_LOG;
       
   771 
       
   772     CESMRFieldCommandEvent* event = CESMRFieldCommandEvent::NewLC(
       
   773         NULL,
       
   774         EMRCmdHideAttachmentIndicator );
       
   775     iEventQueue.NotifyEventAsyncL(event);
       
   776     CleanupStack::Pop(event);
       
   777     }
       
   778 
       
   779 // ---------------------------------------------------------------------------
       
   780 // CMRAttachmentCommandHandler::ExecuteCommandL
       
   781 // ---------------------------------------------------------------------------
       
   782 //
       
   783 void CMRAttachmentCommandHandler::ExecuteCommandL(
       
   784             TInt aCommandId,
       
   785             CMRAttachmentCommand& aCommand,
       
   786             TInt aAttachmentIndex )
       
   787     {
       
   788     // Executing command
       
   789     switch ( aCommandId )
       
   790         {
       
   791         case EESMREditorRemoveAllAttachments:
       
   792             {
       
   793             for( TInt i = iEntry.AttachmentCountL() ; i > 0; --i )
       
   794                 {
       
   795                 aCommand.ExecuteAttachmentCommandL( iEntry, i - 1 );
       
   796                 }
       
   797             }
       
   798         case EESMRViewerSaveAllAttachments:
       
   799              {
       
   800              for( TInt i = 0; i < iEntry.AttachmentCountL(); ++i )
       
   801                  {
       
   802                  aCommand.ExecuteAttachmentCommandL( iEntry, i );
       
   803                  }
       
   804              }
       
   805              break;
       
   806         default:
       
   807             {
       
   808             aCommand.ExecuteAttachmentCommandL( iEntry, aAttachmentIndex );
       
   809             }
       
   810             break;
       
   811         }
       
   812 
       
   813     }
       
   814 
       
   815 // ---------------------------------------------------------------------------
       
   816 // CMRAttachmentCommandHandler::SaveAllAttachmentsL
       
   817 // ---------------------------------------------------------------------------
       
   818 //
       
   819 void CMRAttachmentCommandHandler::SaveAllAttachmentsL()
       
   820     {
       
   821     // Save local attachments
       
   822     CMRAttachmentCommand* command =
       
   823             CMRSaveAttachmentCommand::NewL( *iDocHandler );
       
   824 
       
   825     TRAPD( error,
       
   826             ExecuteCommandL( EESMRViewerSaveAllAttachments, *command, 0 ) )
       
   827 
       
   828     delete command;
       
   829 
       
   830     if ( error != KErrCancel )
       
   831         {
       
   832         // Check if error happened during local attachment saving
       
   833         User::LeaveIfError( error );
       
   834 
       
   835         // Proceed with remote attachments
       
   836         if ( iAttachmentInfo->AttachmentCount() > 0 )
       
   837             {
       
   838             SaveNextRemoteAttachmentL();
       
   839             }
       
   840         else // Show Save all info note
       
   841             {
       
   842             ShowInfoNoteL( iEntry.AttachmentCountL() ); 
       
   843             }
       
   844         }
       
   845     }
       
   846 
       
   847 // ---------------------------------------------------------------------------
       
   848 // CMRAttachmentCommandHandler::SaveNextRemoteAttachmentL
       
   849 // ---------------------------------------------------------------------------
       
   850 //
       
   851 void CMRAttachmentCommandHandler::SaveNextRemoteAttachmentL()
       
   852     {
       
   853     __ASSERT_DEBUG( iAttachmentInfo->AttachmentCount() > 0,
       
   854                     Panic( EMRAttachmentCommandHandlerRemoteAttachmentNotFound ) );
       
   855     
       
   856     // Save first remote attachment of attachment info
       
   857     RPointerArray<MCalRemoteAttachment> attachmentArray;
       
   858     CleanupClosePushL( attachmentArray );
       
   859     const MCalRemoteAttachment& remoteAttachment(
       
   860             iAttachmentInfo->AttachmentL( 0 ) );
       
   861     attachmentArray.AppendL( &remoteAttachment );
       
   862 
       
   863     if ( !iCommandInProgress )
       
   864         {
       
   865         // There are no ongoing command for this attachment
       
   866         // ==> We can start new one ...
       
   867         iCommandInProgress = EESMRViewerSaveAllAttachments;
       
   868 
       
   869         // Before we can execute the actual command, we need to be sure
       
   870         // that attachments are being downloaded to device.
       
   871         iRemoteAttachmentApi->DownloadAttachmentsL(
       
   872                 attachmentArray,
       
   873                 iDownloadOperations,
       
   874                 *this );
       
   875         }
       
   876 
       
   877     CleanupStack::PopAndDestroy( &attachmentArray );
       
   878     }
       
   879 
       
   880 // EOF