email/mail/EditorSrc/cmsgmailrestoreattaop.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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:  State machine -based operation for restoring sinle attachment.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "cmsgmailrestoreattaop.h"
       
    22 #include "MsgMailEditorDocument.h"
       
    23 #include "MsgMailDRMHandler.h"
       
    24 #include "MailUtils.h"
       
    25 #include "MailLog.h"
       
    26 #include <MsgEditorAppUi.rsg>       // resource identifiers
       
    27 #include <MsgMailEditor.rsg>
       
    28 #include <mmsvattachmentmanager.h>
       
    29 #include <MsgAttachmentModel.h>
       
    30 #include <f32file.h>
       
    31 #include <caf/caferr.h>
       
    32 
       
    33 //Content-Transfer-Encoding: types
       
    34 _LIT8(KMimeApplicationOctet, " application/octet-stream");
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CMsgMailRestoreAttaOp::CMsgMailRestoreAttaOp
       
    39 // C++ default constructor can NOT contain any code, that
       
    40 // might leave.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CMsgMailRestoreAttaOp::CMsgMailRestoreAttaOp(
       
    44     TInt aIndex,
       
    45     CMsgMailEditorDocument& aDocument )
       
    46     : CMsgMailBaseOp( aDocument ),
       
    47       iIndex( aIndex )
       
    48     {
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CMsgMailRestoreAttaOp::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void CMsgMailRestoreAttaOp::ConstructL()
       
    57     {
       
    58     LOG( "CMsgMailRestoreAttaOp::ConstructL, creating DRM handler..." );
       
    59     iDrmHandler = MsgMailDRMHandler::NewL();
       
    60     LOG( "CMsgMailRestoreAttaOp::ConstructL, getting atta mgr..." );
       
    61     iManager = &( iDocument.GetAttachmentManagerL() );
       
    62     LOG( "CMsgMailRestoreAttaOp::ConstructL, getting atta handle..." );    
       
    63     LOG( "CMsgMailRestoreAttaOp::ConstructL, ...finished" );
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CMsgMailRestoreAttaOp::NewL
       
    68 // Two-phased constructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CMsgMailRestoreAttaOp* CMsgMailRestoreAttaOp::NewL(
       
    72     TInt aIndex,
       
    73     CMsgMailEditorDocument& aDocument )
       
    74     {
       
    75     CMsgMailRestoreAttaOp* self =
       
    76         new( ELeave ) CMsgMailRestoreAttaOp( aIndex, aDocument );
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     
       
    80     CleanupStack::Pop(self);
       
    81     return self;
       
    82     }
       
    83 
       
    84 // Destructor
       
    85 CMsgMailRestoreAttaOp::~CMsgMailRestoreAttaOp()
       
    86     {
       
    87     LOG( "CMsgMailRestoreAttaOp::~CMsgMailRestoreAttaOp" );
       
    88     Cancel();    
       
    89     delete iDrmHandler;
       
    90     }
       
    91     
       
    92 // -----------------------------------------------------------------------------
       
    93 // CMsgMailRestoreAttaOp::HandleStateActionL
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CMsgMailRestoreAttaOp::HandleStateActionL()
       
    97     {
       
    98     switch ( iState )
       
    99         {
       
   100         case ECheckAttaMime:
       
   101             {
       
   102             CheckAttaMimeL();
       
   103             break;
       
   104             }
       
   105         case ECheckAttaDRM:
       
   106             {
       
   107             CheckAttaDRML();
       
   108             break;
       
   109             }   
       
   110         case EFinalize:
       
   111             {
       
   112             Finalize();
       
   113             break;
       
   114             }
       
   115         default:
       
   116             {
       
   117             // should never come here
       
   118             ASSERT( EFalse );
       
   119             break;
       
   120             }
       
   121         }
       
   122     }
       
   123     
       
   124 // -----------------------------------------------------------------------------
       
   125 // CMsgMailRestoreAttaOp::SetNextState
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CMsgMailRestoreAttaOp::SetNextState()
       
   129     {
       
   130     switch ( iState )
       
   131         {
       
   132         case EIdleState:
       
   133             {
       
   134             iState = ECheckAttaMime;
       
   135             break;
       
   136             }
       
   137         case ECheckAttaMime:
       
   138             {
       
   139             iState = ECheckAttaDRM;
       
   140             break;
       
   141             }
       
   142         case ECheckAttaDRM:     
       
   143             {
       
   144             iState = EFinalize;
       
   145             break;
       
   146             }        
       
   147         case EFinalize:
       
   148         default:
       
   149             {            
       
   150             iState = EIdleState;
       
   151             break;
       
   152             }
       
   153         }        
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CMsgMailRestoreAttaOp::HandleOperationCancel
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CMsgMailRestoreAttaOp::HandleOperationCancel()
       
   161     {
       
   162     LOG( "CMsgMailRestoreAttaOp::HandleOperationCancel" );
       
   163     if ( iState == ECheckAttaMime )
       
   164         {     
       
   165         // cancel possibly ongoing ModifyAttachmentInfoL() request, doesn't do
       
   166         // anything if request isn't outstanding
       
   167         LOG( "HandleOperationCancel, cancel manager's request" );        
       
   168         iManager->CancelRequest();
       
   169         }
       
   170     }
       
   171     
       
   172 // -----------------------------------------------------------------------------
       
   173 // CMsgMailRestoreAttaOp::CheckAttaMimeL
       
   174 // -----------------------------------------------------------------------------
       
   175 //    
       
   176 void CMsgMailRestoreAttaOp::CheckAttaMimeL()
       
   177     {
       
   178     CMsvAttachment* info = iManager->GetAttachmentInfoL( iIndex );
       
   179     CleanupStack::PushL( info );
       
   180     const TDesC8& infoMime = info->MimeType();
       
   181 
       
   182 	if ( infoMime.Length() == 0 || 
       
   183 	     !( KMimeApplicationOctet().FindF( infoMime ) == KErrNotFound ) )
       
   184 	    {
       
   185 	    // mime type missing. try to resolve it
       
   186 	    RFile atta = iManager->GetAttachmentFileL( iIndex );
       
   187 	    TDataType mime = iDocument.ResolveMimeType( atta );
       
   188 	    atta.Close();
       
   189 	    //Check if recognizing failed
       
   190 	    if( mime.Des().Length() > 0 )
       
   191             {
       
   192             info->SetMimeTypeL( mime.Des8() );
       
   193             // takes ownership of info
       
   194             iManager->ModifyAttachmentInfoL( info, iStatus );
       
   195             SetActive(); // asynchronous request made -> set active
       
   196             CleanupStack::Pop( info );        
       
   197             }    
       
   198 	    }
       
   199 
       
   200     // if atta info isn't modified then we must complete this step manually,
       
   201     // and also take care of deleting info
       
   202     if ( !IsActive() )	    
       
   203         {
       
   204         CompleteStateAction();
       
   205         CleanupStack::PopAndDestroy( info );        
       
   206         }
       
   207     }
       
   208     
       
   209 // -----------------------------------------------------------------------------
       
   210 // CMsgMailRestoreAttaOp::CheckAttaDRML
       
   211 // -----------------------------------------------------------------------------
       
   212 //    
       
   213 void CMsgMailRestoreAttaOp::CheckAttaDRML()
       
   214     {
       
   215     CMsvAttachment* info = iManager->GetAttachmentInfoL( iIndex );
       
   216     CleanupStack::PushL( info );    
       
   217     
       
   218     RFile atta = iManager->GetAttachmentFileL( iIndex );
       
   219     CleanupClosePushL( atta );
       
   220 	CMsgAttachmentInfo::TDRMDataType drmType = 
       
   221 	    iDrmHandler->GetDRMDataTypeL( atta );
       
   222     TBool isClosedFile( MailUtils::IsClosedFileL( atta ) );
       
   223     // atta
       
   224     CleanupStack::PopAndDestroy(); // CSI: 12 # RFile
       
   225 	    
       
   226 	if ( drmType == CMsgAttachmentInfo::EForwardLockedOrCombinedDelivery ||
       
   227 	     isClosedFile )
       
   228         {
       
   229         // Context might be changed, so refresh manager reference
       
   230         // This is some historical magic that could be made nicer
       
   231         iManager = &( iDocument.GetAttachmentManagerL() );
       
   232         // Remove attachment
       
   233         iManager->RemoveAttachmentL( info->Id(), iStatus );
       
   234         iIsFLAtta = ETrue;
       
   235         SetActive(); // asynchronous request made -> set active	
       
   236         }
       
   237 	else
       
   238 		{
       
   239         TDataType mime( info->MimeType() );
       
   240 	    iDocument.AttachmentModel().AddAttachmentL( info->AttachmentName(),
       
   241 		                                            info->Size(),
       
   242                                             		info->Id(),
       
   243                                             		info->Complete(),
       
   244                                             		mime,
       
   245                                             		drmType );   
       
   246 		iIsFLAtta = EFalse;
       
   247 		CompleteStateAction(); // nothing async -> complete manually
       
   248 		}
       
   249     CleanupStack::PopAndDestroy( info ); 
       
   250     }
       
   251     
       
   252 // -----------------------------------------------------------------------------
       
   253 // CMsgMailRestoreAttaOp::Finalize
       
   254 // -----------------------------------------------------------------------------
       
   255 //    
       
   256 void CMsgMailRestoreAttaOp::Finalize()
       
   257     {
       
   258     if ( iIsFLAtta )
       
   259         { // complete the operation with error code indicating that attachment
       
   260           // was removed due to DRM reasons
       
   261         CompleteStateAction( KErrCANoPermission );
       
   262         }    
       
   263     else
       
   264         { // no problem
       
   265         CompleteStateAction();
       
   266         }
       
   267     }
       
   268         
       
   269 // End Of File