ipsservices/ipssosplugin/src/ipsplgpop3fetchoperation.cpp
changeset 0 8466d47a6819
child 13 0396474f30f5
child 18 578830873419
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  POP3 fetch message operation class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "ipsplgheaders.h"
       
    21 
       
    22 // Constants and defines
       
    23 const TInt KFetchOpPriority = CActive::EPriorityStandard;
       
    24 
       
    25 // ----------------------------------------------------------------------------
       
    26 // ----------------------------------------------------------------------------
       
    27 CIpsPlgPop3FetchOperation* CIpsPlgPop3FetchOperation::NewL(
       
    28     CMsvSession& aMsvSession,
       
    29     TRequestStatus& aObserverRequestStatus,
       
    30     TInt aFunctionId,
       
    31     TMsvId aService,
       
    32     CIpsPlgTimerOperation& aActivityTimer,
       
    33     const TImPop3GetMailInfo& aGetMailInfo,
       
    34     const CMsvEntrySelection& aSel,
       
    35     TFSMailMsgId aFSMailBoxId,
       
    36     MFSMailRequestObserver& aFSOperationObserver,
       
    37     TInt aFSRequestId,
       
    38     CIpsPlgEventHandler* aEventHandler )
       
    39     {
       
    40     FUNC_LOG;
       
    41     CIpsPlgPop3FetchOperation* op = new(ELeave) CIpsPlgPop3FetchOperation(
       
    42         aMsvSession, aObserverRequestStatus, aFunctionId, aService,
       
    43         aActivityTimer, aGetMailInfo, aFSMailBoxId, aFSOperationObserver,
       
    44         aFSRequestId, aEventHandler );
       
    45         
       
    46     CleanupStack::PushL(op);
       
    47     op->ConstructL(aSel);
       
    48     CleanupStack::Pop( op );
       
    49     return op;
       
    50     }
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // ~CIpsPlgPop3FetchOperation
       
    54 // ----------------------------------------------------------------------------
       
    55 CIpsPlgPop3FetchOperation::~CIpsPlgPop3FetchOperation()
       
    56     {
       
    57     FUNC_LOG;
       
    58     delete iFetchErrorProgress;
       
    59     delete iSelection;
       
    60     
       
    61     delete iEntry;
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // ProgressL
       
    66 // ----------------------------------------------------------------------------
       
    67 const TDesC8& CIpsPlgPop3FetchOperation::ProgressL()
       
    68     {
       
    69     FUNC_LOG;
       
    70     
       
    71     if(iFetchErrorProgress && (iState == EStateIdle))
       
    72         {
       
    73         // Completed, but with an error during fetch.
       
    74         return *iFetchErrorProgress;
       
    75         }
       
    76     else
       
    77         {
       
    78         TPop3Progress progg;
       
    79         progg.iErrorCode = KErrNone;
       
    80         TPckgBuf<TPop3Progress> param(progg);
       
    81         iProgressBuf.Copy(param);
       
    82         return iProgressBuf;   
       
    83         }
       
    84     }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 // ConstructL
       
    88 // ----------------------------------------------------------------------------
       
    89 void CIpsPlgPop3FetchOperation::ConstructL(const CMsvEntrySelection& aSel)
       
    90     {
       
    91     FUNC_LOG;
       
    92 
       
    93     BaseConstructL( KUidMsgTypePOP3 );
       
    94 
       
    95     iSelection = aSel.CopyL();
       
    96 
       
    97     iSelection->InsertL( 0, iService );
       
    98     // For Get Mail API, first selection element must be service.
       
    99 
       
   100     // It is possible that the complete flag is on and we want to
       
   101     // download the attachment. In these cases we need to clear the
       
   102     // complete flag.
       
   103 
       
   104     TInt count = iSelection->Count();
       
   105 
       
   106     for ( ; iEntryIndex < count && !iOperation; iEntryIndex++ )
       
   107         {
       
   108         delete iEntry;
       
   109         iEntry = NULL;
       
   110         
       
   111         iEntry = iMsvSession.GetEntryL( iSelection->At( iEntryIndex ) );
       
   112 
       
   113         TMsvEntry entry = iEntry->Entry();
       
   114         
       
   115         TBool complete = entry.Complete();
       
   116 
       
   117         if ( complete )
       
   118             {
       
   119             entry.SetComplete( EFalse );
       
   120 
       
   121             iOperation = iEntry->ChangeL( entry, iStatus );
       
   122 
       
   123             SetActive();
       
   124             
       
   125             iState = EStateClearCompleteFlag;
       
   126             }
       
   127         }
       
   128 
       
   129     if ( !iOperation )
       
   130         {
       
   131         DoConnectL();
       
   132         }
       
   133     }
       
   134 
       
   135 // ----------------------------------------------------------------------------
       
   136 // DoConnectL
       
   137 // ----------------------------------------------------------------------------
       
   138 void CIpsPlgPop3FetchOperation::DoConnectL()
       
   139     {
       
   140     FUNC_LOG;
       
   141     
       
   142 
       
   143     iState = EStateConnecting;
       
   144     iStatus = KRequestPending;
       
   145     
       
   146     // when connecting for the fetch operation, don't let connect operation to do fetch,
       
   147     // because we do it by ourself. That's why give 0 to connect operation.    
       
   148     CIpsPlgPop3ConnectOp* connOp = CIpsPlgPop3ConnectOp::NewL(
       
   149         iMsvSession, iStatus, iService, EFalse, *iActivityTimer,
       
   150         iFSMailboxId, iFSOperationObserver, iFSRequestId, NULL,EFalse );
       
   151         
       
   152     delete iOperation;
       
   153     iOperation = connOp;
       
   154 
       
   155     SetActive();
       
   156     }
       
   157 
       
   158 // ----------------------------------------------------------------------------
       
   159 // DoFetchL
       
   160 // ----------------------------------------------------------------------------
       
   161 void CIpsPlgPop3FetchOperation::DoFetchL()
       
   162     {
       
   163     FUNC_LOG;
       
   164 
       
   165     iState = EStateFetching;
       
   166 
       
   167     // Switch operations.
       
   168     delete iOperation;
       
   169     iOperation = NULL;
       
   170     iStatus = KRequestPending;
       
   171 
       
   172     // Filters are not used when performing 'fetch' operation, use normal 
       
   173     // getmail info instead
       
   174     TPckg<TImPop3GetMailInfo> param( iGetMailInfo );
       
   175     InvokeClientMtmAsyncFunctionL( iFunctionId, *iSelection, iService, param );
       
   176     SetActive();
       
   177     
       
   178     if ( iEventHandler )
       
   179         {
       
   180         iEventHandler->SetNewPropertyEvent( 
       
   181             iService, KIpsSosEmailSyncStarted, KErrNone );
       
   182         } 
       
   183     
       
   184     }
       
   185 
       
   186 
       
   187 
       
   188 // ----------------------------------------------------------------------------
       
   189 // ----------------------------------------------------------------------------
       
   190 CIpsPlgPop3FetchOperation::CIpsPlgPop3FetchOperation(
       
   191     CMsvSession& aMsvSession,
       
   192     TRequestStatus& aObserverRequestStatus,
       
   193     TInt aFunctionId,
       
   194     TMsvId aService,
       
   195     CIpsPlgTimerOperation& aActivityTimer,
       
   196     const TImPop3GetMailInfo& aGetMailInfo,
       
   197     TFSMailMsgId aFSMailBoxId,
       
   198     MFSMailRequestObserver& aFSOperationObserver,
       
   199     TInt aFSRequestId,
       
   200     CIpsPlgEventHandler* aEventHandler )
       
   201     : 
       
   202     CIpsPlgOnlineOperation( aMsvSession, KFetchOpPriority,
       
   203         aObserverRequestStatus, aActivityTimer, aFSMailBoxId,
       
   204         aFSOperationObserver, aFSRequestId), iFunctionId( aFunctionId ),
       
   205         iGetMailInfo( aGetMailInfo ), iEventHandler( aEventHandler )
       
   206     {
       
   207     FUNC_LOG;
       
   208     iService = aService;
       
   209     }
       
   210 
       
   211 // ----------------------------------------------------------------------------
       
   212 // ----------------------------------------------------------------------------
       
   213 void CIpsPlgPop3FetchOperation::RunL()
       
   214     {
       
   215     FUNC_LOG;
       
   216 
       
   217     TRAP( iError, DoRunL() );
       
   218     
       
   219     if(iError != KErrNone)
       
   220         {        
       
   221         // Notify observer we have finished.
       
   222         CompleteObserver();
       
   223         }    
       
   224     }
       
   225 
       
   226 // ----------------------------------------------------------------------------
       
   227 // ----------------------------------------------------------------------------
       
   228 void CIpsPlgPop3FetchOperation::DoCancel()
       
   229     {
       
   230     FUNC_LOG;
       
   231 
       
   232     CIpsPlgOnlineOperation::DoCancel();
       
   233     if(iState == EStateFetching)
       
   234         {
       
   235         // Cancelled while fetching. Need to disconnect.
       
   236         iMsvSession.StopService( iService );
       
   237         // Ignore return value, nothing we can do.
       
   238         }
       
   239     if ( iEventHandler )
       
   240         {
       
   241         iEventHandler->SetNewPropertyEvent( 
       
   242             iService, KIpsSosEmailSyncCompleted, KErrCancel );
       
   243         }
       
   244     }
       
   245 
       
   246 // ----------------------------------------------------------------------------
       
   247 // DoRunL
       
   248 // ----------------------------------------------------------------------------
       
   249 void CIpsPlgPop3FetchOperation::DoRunL()
       
   250     {
       
   251     FUNC_LOG;
       
   252 
       
   253     switch(iState)
       
   254         {
       
   255         case EStateClearCompleteFlag:
       
   256             {
       
   257             // First clean things
       
   258             delete iOperation;
       
   259             iOperation = NULL;
       
   260 
       
   261             delete iEntry;
       
   262             iEntry = NULL;
       
   263             
       
   264             // Leave if setting the complete flag was a failure.
       
   265             User::LeaveIfError( iStatus.Int() );
       
   266             
       
   267             TInt count = iSelection->Count();
       
   268 
       
   269             for ( ; iEntryIndex < count && !iOperation; iEntryIndex++ )
       
   270                 {
       
   271                 delete iEntry;
       
   272                 iEntry = NULL;
       
   273                 
       
   274                 iEntry = iMsvSession.GetEntryL(
       
   275                     iSelection->At( iEntryIndex ) );
       
   276 
       
   277                 TMsvEntry entry = iEntry->Entry();
       
   278                 
       
   279                 TBool complete = entry.Complete();
       
   280 
       
   281                 if ( complete )
       
   282                     {
       
   283                     entry.SetComplete( EFalse );
       
   284 
       
   285                     iOperation = iEntry->ChangeL( entry, iStatus );
       
   286 
       
   287                     SetActive();
       
   288                     }
       
   289                 }
       
   290 
       
   291             if ( !iOperation )
       
   292                 {
       
   293                 DoConnectL();
       
   294                 }
       
   295             
       
   296             break;
       
   297             }
       
   298         case EStateConnecting:
       
   299             {
       
   300             // Connect complete.
       
   301             TBool connected = 
       
   302                 STATIC_CAST( CIpsPlgPop3ConnectOp*, iOperation )->Connected();
       
   303             if(!connected)
       
   304                 {
       
   305                 CompleteObserver( KErrCouldNotConnect );
       
   306                 return;
       
   307                 }
       
   308             DoFetchL();
       
   309             }
       
   310             break;
       
   311         case EStateFetching:         
       
   312             {
       
   313             TInt err = iStatus.Int();
       
   314 
       
   315             if( KErrNone != err )
       
   316                 {
       
   317                 TPckgBuf<TPop3Progress> paramPack;
       
   318                 if ( iOperation )
       
   319                     {
       
   320                     paramPack.Copy( iOperation->ProgressL() );
       
   321                     }
       
   322                 TPop3Progress& progress = paramPack();
       
   323                 progress.iErrorCode = err;
       
   324                 iFetchErrorProgress = paramPack.AllocL();
       
   325                 }
       
   326             
       
   327             iState = EStateIdle;
       
   328             CompleteObserver( err );
       
   329             if ( iEventHandler )
       
   330                 {
       
   331                 iEventHandler->SetNewPropertyEvent( 
       
   332                     iService, KIpsSosEmailSyncCompleted, err );
       
   333                 }
       
   334             }
       
   335             break;
       
   336             
       
   337         default:
       
   338             break;
       
   339         }        
       
   340     }
       
   341 
       
   342 // ----------------------------------------------------------------------------
       
   343 // ----------------------------------------------------------------------------
       
   344 const TDesC8& CIpsPlgPop3FetchOperation::GetErrorProgressL( TInt /* aError */ )
       
   345     {
       
   346     FUNC_LOG;
       
   347 
       
   348     return *iFetchErrorProgress;
       
   349     }
       
   350 
       
   351 // ----------------------------------------------------------------------------
       
   352 // ----------------------------------------------------------------------------
       
   353 TFSProgress CIpsPlgPop3FetchOperation::GetFSProgressL() const
       
   354     {
       
   355     FUNC_LOG;
       
   356     // might not never called, but gives something reasonable if called
       
   357     TFSProgress result = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
       
   358     result.iError = KErrNone;
       
   359     switch( iState )
       
   360         {
       
   361         case EStateConnecting:
       
   362             result.iProgressStatus = TFSProgress::EFSStatus_Connecting;
       
   363             break;
       
   364         case EStateFetching:
       
   365             result.iProgressStatus = TFSProgress::EFSStatus_Connected;
       
   366             break;
       
   367         default:
       
   368             result.iProgressStatus = TFSProgress::EFSStatus_RequestComplete;
       
   369             break;
       
   370         }
       
   371     if ( iStatus.Int() == KErrCancel )
       
   372         {
       
   373         result.iProgressStatus = TFSProgress::EFSStatus_RequestCancelled;
       
   374         result.iError = KErrCancel;
       
   375         }
       
   376     
       
   377     return result;
       
   378     }
       
   379 
       
   380 // EOF
       
   381