ipsservices/ipssosplugin/src/ipsplgimap4fetchattachmentop.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *       IpsPlgImap4FetchAttachmentOp implementation file
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "emailtrace.h"
       
    21 #include "ipsplgheaders.h"
       
    22 
       
    23 // Constants and defines
       
    24 const TInt KFetchOpPriority = CActive::EPriorityStandard;
       
    25 const TInt KIpsAttaFetchProgressReportInterval = 1000000; // 1 sec
       
    26 
       
    27 // ----------------------------------------------------------------------------
       
    28 // ----------------------------------------------------------------------------
       
    29 CIpsFetchProgReport* CIpsFetchProgReport::NewL( 
       
    30     CIpsPlgImap4FetchAttachmentOp& aAttaOp )
       
    31     {
       
    32     FUNC_LOG;
       
    33     CIpsFetchProgReport* self = new (ELeave) CIpsFetchProgReport( aAttaOp );
       
    34     CleanupStack::PushL( self );
       
    35     self->ConstructL( );
       
    36     CleanupStack::Pop( self );
       
    37     return self;
       
    38     }
       
    39     
       
    40 // ----------------------------------------------------------------------------
       
    41 // ----------------------------------------------------------------------------    
       
    42 CIpsFetchProgReport::CIpsFetchProgReport( 
       
    43     CIpsPlgImap4FetchAttachmentOp& aAttaOp ) : 
       
    44     CActive( CActive::EPriorityStandard ), 
       
    45     iAttaOp(aAttaOp)
       
    46     {
       
    47     FUNC_LOG;
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // ----------------------------------------------------------------------------    
       
    52 CIpsFetchProgReport::~CIpsFetchProgReport()
       
    53     {
       
    54     FUNC_LOG;
       
    55     Cancel();
       
    56     iTimer.Close();
       
    57     }
       
    58     
       
    59 // ----------------------------------------------------------------------------
       
    60 // ----------------------------------------------------------------------------	    
       
    61 void CIpsFetchProgReport::ConstructL()
       
    62     {
       
    63     FUNC_LOG;
       
    64     CActiveScheduler::Add(this);
       
    65     User::LeaveIfError( iTimer.CreateLocal() );
       
    66     AdjustTimer();
       
    67     }
       
    68     
       
    69 // ----------------------------------------------------------------------------
       
    70 // ----------------------------------------------------------------------------
       
    71 void CIpsFetchProgReport::AdjustTimer()   
       
    72     {
       
    73     FUNC_LOG;
       
    74     TTimeIntervalMicroSeconds32 sec( KIpsAttaFetchProgressReportInterval ); 
       
    75     TTime time;
       
    76     time.HomeTime();
       
    77     time = time + sec;
       
    78     iTimer.At(iStatus, time);
       
    79     SetActive();    
       
    80     }
       
    81     
       
    82 // ----------------------------------------------------------------------------
       
    83 // ----------------------------------------------------------------------------    
       
    84 void CIpsFetchProgReport::DoCancel()
       
    85     {
       
    86     FUNC_LOG;
       
    87     iTimer.Cancel();
       
    88     }
       
    89     
       
    90 // ----------------------------------------------------------------------------
       
    91 // ----------------------------------------------------------------------------	
       
    92 void CIpsFetchProgReport::RunL()
       
    93     {
       
    94     FUNC_LOG;
       
    95     iAttaOp.ReportProgressL();
       
    96     AdjustTimer();
       
    97     }
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // ----------------------------------------------------------------------------
       
   101 CIpsPlgImap4FetchAttachmentOp* CIpsPlgImap4FetchAttachmentOp::NewL(
       
   102     CMsvSession& aMsvSession,
       
   103     TRequestStatus& aObserverRequestStatus,
       
   104     TInt aFunctionId,
       
   105     TMsvId aService,
       
   106     CIpsPlgTimerOperation& aActivityTimer,
       
   107     const TImImap4GetMailInfo& aGetMailInfo,
       
   108     const CMsvEntrySelection& aSel,
       
   109     TFSMailMsgId aFSMailBoxId,
       
   110     MFSMailRequestObserver& aFSOperationObserver,
       
   111     TInt aFSRequestId )
       
   112     {
       
   113     FUNC_LOG;
       
   114     CIpsPlgImap4FetchAttachmentOp* op = new (
       
   115         ELeave) CIpsPlgImap4FetchAttachmentOp(
       
   116         aMsvSession,
       
   117         aObserverRequestStatus,
       
   118         aFunctionId,
       
   119         aService,
       
   120         aActivityTimer,
       
   121         aGetMailInfo,
       
   122         aFSMailBoxId,
       
   123         aFSOperationObserver,
       
   124         aFSRequestId );
       
   125         
       
   126     CleanupStack::PushL( op );
       
   127     op->ConstructL( aSel );
       
   128     CleanupStack::Pop( op );
       
   129     return op;
       
   130     }
       
   131 
       
   132 // ----------------------------------------------------------------------------
       
   133 // ----------------------------------------------------------------------------
       
   134 CIpsPlgImap4FetchAttachmentOp::CIpsPlgImap4FetchAttachmentOp(
       
   135     CMsvSession& aMsvSession,
       
   136     TRequestStatus& aObserverRequestStatus,
       
   137     TInt aFunctionId,
       
   138     TMsvId aService,
       
   139     CIpsPlgTimerOperation& aActivityTimer,
       
   140     const TImImap4GetMailInfo& aGetMailInfo,
       
   141     TFSMailMsgId aFSMailBoxId,
       
   142     MFSMailRequestObserver& aFSOperationObserver,
       
   143     TInt aFSRequestId )
       
   144     :
       
   145     CIpsPlgOnlineOperation(
       
   146     aMsvSession,
       
   147     KFetchOpPriority,
       
   148     aObserverRequestStatus,
       
   149     aActivityTimer,
       
   150     aFSMailBoxId,
       
   151     aFSOperationObserver,
       
   152     aFSRequestId),
       
   153     iSelection( NULL ),
       
   154     iGetMailInfo(aGetMailInfo),
       
   155     iFunctionId(aFunctionId)
       
   156     {
       
   157     FUNC_LOG;
       
   158     iService = aService;
       
   159     }
       
   160 
       
   161 // ----------------------------------------------------------------------------
       
   162 // ----------------------------------------------------------------------------
       
   163 CIpsPlgImap4FetchAttachmentOp::~CIpsPlgImap4FetchAttachmentOp()
       
   164     {
       
   165     FUNC_LOG;
       
   166     delete iSelection;
       
   167     delete iProgReport;
       
   168     }
       
   169 
       
   170 // ----------------------------------------------------------------------------
       
   171 // ----------------------------------------------------------------------------
       
   172 void CIpsPlgImap4FetchAttachmentOp::ConstructL( const CMsvEntrySelection& aSel )
       
   173     {
       
   174     FUNC_LOG;
       
   175     BaseConstructL( KUidMsgTypeIMAP4 );
       
   176     iSelection = aSel.CopyL();
       
   177     DoConnectL();
       
   178     }
       
   179 
       
   180 // ----------------------------------------------------------------------------
       
   181 // ----------------------------------------------------------------------------
       
   182 void CIpsPlgImap4FetchAttachmentOp::DoConnectL()
       
   183     {
       
   184     FUNC_LOG;
       
   185     iState = EStateConnecting;
       
   186     iStatus = KRequestPending;
       
   187 
       
   188     CIpsPlgImap4ConnectOp* connOp = CIpsPlgImap4ConnectOp::NewL(
       
   189         iMsvSession,
       
   190         KFetchOpPriority,
       
   191         iStatus, 
       
   192         iService,
       
   193         *iActivityTimer,
       
   194         iFSMailboxId,
       
   195         iFSOperationObserver,
       
   196         iFSRequestId,
       
   197         NULL, // event handler not needed whin plain connect
       
   198         ETrue,
       
   199         EFalse );
       
   200         
       
   201     delete iOperation;
       
   202     iOperation = connOp;
       
   203 
       
   204     SetActive();
       
   205     }
       
   206     
       
   207 // ----------------------------------------------------------------------------
       
   208 // ----------------------------------------------------------------------------
       
   209 void CIpsPlgImap4FetchAttachmentOp::RunL()
       
   210     {
       
   211     FUNC_LOG;
       
   212     TRAPD(err, DoRunL());
       
   213     if(err != KErrNone)
       
   214         {
       
   215         iProgress().iGenericProgress.iErrorCode = err;
       
   216         Complete();
       
   217         }
       
   218     }
       
   219 
       
   220 // ----------------------------------------------------------------------------
       
   221 // ----------------------------------------------------------------------------
       
   222 void CIpsPlgImap4FetchAttachmentOp::DoRunL()
       
   223     {
       
   224     FUNC_LOG;
       
   225     switch( iState )
       
   226         {
       
   227         case EStateConnecting:
       
   228             {
       
   229             TBool connected = STATIC_CAST(
       
   230                 CIpsPlgImap4ConnectOp*, iOperation)->Connected();
       
   231             if(!connected)
       
   232                 {
       
   233                 CompleteObserver( KErrCouldNotConnect );
       
   234                 return;
       
   235                 }
       
   236             DoFetchAttachmentL();
       
   237             break;
       
   238             }
       
   239         case EStateFetching:         
       
   240             {
       
   241             delete iProgReport;
       
   242             iProgReport = NULL;
       
   243             
       
   244             TInt err = iStatus.Int();
       
   245 
       
   246             if( err != KErrNone && iOperation )
       
   247                 {
       
   248                 iFetchErrorProgress = iOperation->ProgressL().AllocL();
       
   249                 }
       
   250             
       
   251             iState = EStateIdle;
       
   252             CompleteObserver( err );
       
   253             break;
       
   254             }
       
   255         default:
       
   256             break;
       
   257         }
       
   258     }
       
   259 
       
   260 // ----------------------------------------------------------------------------
       
   261 // ----------------------------------------------------------------------------
       
   262 const TDesC8& CIpsPlgImap4FetchAttachmentOp::ProgressL()
       
   263     {
       
   264     FUNC_LOG;
       
   265     if(iFetchErrorProgress && (iState == EStateIdle))
       
   266         {
       
   267         // Completed, but with an error during fetch.
       
   268         return *iFetchErrorProgress;
       
   269         }
       
   270     
       
   271     if ( iOperation )
       
   272         {
       
   273         iProgress.Copy( iOperation->ProgressL() );
       
   274         }
       
   275     else
       
   276         {
       
   277         TImap4CompoundProgress progg;
       
   278         progg.iGenericProgress.iErrorCode = KErrNone;
       
   279         progg.iGenericProgress.iState = TImap4GenericProgress::EIdle;
       
   280         iProgress.Copy( TPckgBuf<TImap4CompoundProgress>(progg) );
       
   281         }
       
   282            
       
   283     return iProgress;
       
   284     }
       
   285     
       
   286 // ----------------------------------------------------------------------------
       
   287 // ----------------------------------------------------------------------------
       
   288 void CIpsPlgImap4FetchAttachmentOp::ReportProgressL()
       
   289     {
       
   290     FUNC_LOG;
       
   291     TInt error = KErrNone;
       
   292     TFSProgress fsProgress = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
       
   293     if ( iOperation && iState == EStateFetching )
       
   294         {
       
   295         TRAP(error, iProgress.Copy( iOperation->ProgressL() ) );
       
   296         }
       
   297     
       
   298     if ( error == KErrNone )
       
   299         {
       
   300         const TImap4GenericProgress& progg = iProgress().iGenericProgress;
       
   301         fsProgress.iProgressStatus = TFSProgress::EFSStatus_Status;
       
   302         fsProgress.iMaxCount = progg.iBytesToDo;
       
   303         fsProgress.iCounter = progg.iBytesDone;
       
   304         fsProgress.iError = progg.iErrorCode;
       
   305         }
       
   306     else if ( error == KErrNotReady )
       
   307         {
       
   308         // This error indicates that operation not started yet
       
   309         // and waiting to other operation compleion
       
   310         fsProgress.iProgressStatus = TFSProgress::EFSStatus_Waiting;
       
   311         fsProgress.iMaxCount = 1;
       
   312         fsProgress.iCounter = 0;
       
   313         fsProgress.iError = KErrNone;
       
   314         }
       
   315     else
       
   316         {
       
   317         User::Leave( error );
       
   318         }
       
   319     
       
   320     iFSOperationObserver.RequestResponseL( fsProgress, iFSRequestId );
       
   321     }
       
   322 
       
   323 // ----------------------------------------------------------------------------
       
   324 // ----------------------------------------------------------------------------
       
   325 const TDesC8& CIpsPlgImap4FetchAttachmentOp::GetErrorProgressL(TInt /*aError*/ )
       
   326     {
       
   327     return *iFetchErrorProgress;
       
   328     }
       
   329 
       
   330 // ----------------------------------------------------------------------------
       
   331 // ----------------------------------------------------------------------------
       
   332 TFSProgress CIpsPlgImap4FetchAttachmentOp::GetFSProgressL() const
       
   333     {
       
   334     FUNC_LOG;
       
   335     // might not never called, but gives something reasonable if called
       
   336     TFSProgress result = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
       
   337     result.iError = KErrNone;
       
   338     switch( iState )
       
   339         {
       
   340         case EStateConnecting:
       
   341             result.iProgressStatus = TFSProgress::EFSStatus_Connecting;
       
   342             break;
       
   343         case EStateFetching:
       
   344             result.iProgressStatus = TFSProgress::EFSStatus_Connected;
       
   345             break;
       
   346         default:
       
   347             result.iProgressStatus = TFSProgress::EFSStatus_RequestComplete;
       
   348             break;
       
   349         }
       
   350     if ( iStatus.Int() == KErrCancel )
       
   351         {
       
   352         result.iProgressStatus = TFSProgress::EFSStatus_RequestCancelled;
       
   353         result.iError = KErrCancel;
       
   354         }
       
   355     
       
   356     return result;
       
   357     }
       
   358 
       
   359 // ----------------------------------------------------------------------------
       
   360 // ----------------------------------------------------------------------------
       
   361 void CIpsPlgImap4FetchAttachmentOp::Complete()
       
   362     {
       
   363     FUNC_LOG;
       
   364     TRequestStatus* observer=&iObserverRequestStatus;
       
   365     User::RequestComplete(observer, KErrNone);
       
   366     }
       
   367 
       
   368 // ----------------------------------------------------------------------------
       
   369 // ----------------------------------------------------------------------------
       
   370 void CIpsPlgImap4FetchAttachmentOp::DoFetchAttachmentL( )
       
   371     {
       
   372     FUNC_LOG;
       
   373     iState = EStateFetching;
       
   374 
       
   375     // Switch operations.
       
   376     delete iOperation;
       
   377     iOperation = NULL;
       
   378     iStatus = KRequestPending;
       
   379 
       
   380     iProgReport = CIpsFetchProgReport::NewL( *this );
       
   381 
       
   382     // Filters are not used when performing 'fetch' operation, 
       
   383     // use normal getmail info instead
       
   384     TPckg<TImImap4GetMailInfo> param(iGetMailInfo);
       
   385     InvokeClientMtmAsyncFunctionL( iFunctionId, *iSelection, iService, param );
       
   386     SetActive();
       
   387     }
       
   388     
       
   389 // ----------------------------------------------------------------------------
       
   390 // ----------------------------------------------------------------------------    
       
   391 TInt CIpsPlgImap4FetchAttachmentOp::GetEngineProgress( const TDesC8& aProgress )
       
   392     {
       
   393     FUNC_LOG;
       
   394     if( !aProgress.Length() )
       
   395         {
       
   396         return KErrNone;
       
   397         }
       
   398     else
       
   399         {
       
   400         TPckgBuf<TImap4CompoundProgress> paramPack;
       
   401         paramPack.Copy( aProgress );
       
   402         const TImap4GenericProgress& progress = paramPack().iGenericProgress;
       
   403 
       
   404         return progress.iErrorCode;
       
   405         }    
       
   406     }        
       
   407 
       
   408 
       
   409 // End of File
       
   410