ipsservices/ipssosplugin/src/ipsplgimap4populateop.cpp
changeset 0 8466d47a6819
child 11 0396474f30f5
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:  IMAP4 fetch message operation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include "ipsplgheaders.h"
       
    20 
       
    21 // Constants and defines
       
    22 const TInt KFetchOpPriority = CActive::EPriorityStandard;
       
    23 const TInt KIpsPlgSelectionGra = 16;
       
    24 
       
    25 // ----------------------------------------------------------------------------
       
    26 // ----------------------------------------------------------------------------
       
    27 EXPORT_C CIpsPlgImap4PopulateOp* CIpsPlgImap4PopulateOp::NewL(
       
    28     CMsvSession& aMsvSession,
       
    29     TRequestStatus& aObserverRequestStatus,
       
    30     TInt aPriority,
       
    31     TMsvId aService,
       
    32     CIpsPlgTimerOperation& aActivityTimer,
       
    33     const TImImap4GetPartialMailInfo& aPartialMailInfo,
       
    34     const CMsvEntrySelection& aSel,
       
    35     TFSMailMsgId aFSMailBoxId,
       
    36     MFSMailRequestObserver& aFSOperationObserver,
       
    37     TInt aFSRequestId,
       
    38     CIpsPlgEventHandler* aEventHandler,
       
    39     TBool aDoFilterSelection )
       
    40     {
       
    41     FUNC_LOG;
       
    42     CIpsPlgImap4PopulateOp* op = new (ELeave) CIpsPlgImap4PopulateOp(
       
    43         aMsvSession,
       
    44         aObserverRequestStatus,
       
    45         aPriority,
       
    46         aService,
       
    47         aActivityTimer,
       
    48         aPartialMailInfo,
       
    49         aFSMailBoxId,
       
    50         aFSOperationObserver,
       
    51         aFSRequestId,
       
    52         aEventHandler );
       
    53         
       
    54     CleanupStack::PushL(op);
       
    55     op->ConstructL( aSel, aDoFilterSelection );
       
    56     CleanupStack::Pop( op );
       
    57     return op;
       
    58     }
       
    59 
       
    60 // ----------------------------------------------------------------------------
       
    61 // ----------------------------------------------------------------------------
       
    62 CIpsPlgImap4PopulateOp::CIpsPlgImap4PopulateOp(
       
    63     CMsvSession& aMsvSession,
       
    64     TRequestStatus& aObserverRequestStatus,
       
    65     TInt aPriority,
       
    66     TMsvId aService,
       
    67     CIpsPlgTimerOperation& aActivityTimer,
       
    68     const TImImap4GetPartialMailInfo& aPartialMailInfo,
       
    69     TFSMailMsgId aFSMailBoxId,
       
    70     MFSMailRequestObserver& aFSOperationObserver,
       
    71     TInt aFSRequestId,
       
    72     CIpsPlgEventHandler* aEventHandler )
       
    73     :
       
    74     CIpsPlgOnlineOperation(
       
    75     aMsvSession,
       
    76     aPriority,
       
    77     aObserverRequestStatus,
       
    78     aActivityTimer,
       
    79     aFSMailBoxId,
       
    80     aFSOperationObserver,
       
    81     aFSRequestId),
       
    82     iPartialMailInfo(aPartialMailInfo),
       
    83     iSelection( KIpsPlgSelectionGra ),
       
    84     iEventHandler( aEventHandler )
       
    85     {
       
    86     FUNC_LOG;
       
    87     iService = aService;
       
    88     }
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // ----------------------------------------------------------------------------
       
    92 CIpsPlgImap4PopulateOp::~CIpsPlgImap4PopulateOp()
       
    93     {
       
    94     FUNC_LOG;
       
    95     iSelection.Close();
       
    96 
       
    97     if ( iTempSelection )
       
    98     	{
       
    99     	iTempSelection->Reset();
       
   100    	delete iTempSelection;
       
   101     	}
       
   102     }
       
   103 
       
   104 // ----------------------------------------------------------------------------
       
   105 // ----------------------------------------------------------------------------
       
   106 void CIpsPlgImap4PopulateOp::ConstructL( 
       
   107         const CMsvEntrySelection& aSel,
       
   108         TBool aDoFilterSelection )
       
   109     {
       
   110     FUNC_LOG;
       
   111     BaseConstructL( KUidMsgTypeIMAP4 );
       
   112     
       
   113     if ( aDoFilterSelection )
       
   114         {
       
   115         FilterSelectionL( aSel );
       
   116         }
       
   117     else
       
   118         {
       
   119         for ( TInt i = 1; i < aSel.Count(); i++ )
       
   120             {
       
   121             if ( aSel[i] != iService )
       
   122                 {
       
   123                 iSelection.AppendL( aSel.At(i) );
       
   124                 }
       
   125             }
       
   126         }
       
   127     iTempSelection = new ( ELeave ) CMsvEntrySelection();
       
   128     DoConnectL();
       
   129     }
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // ----------------------------------------------------------------------------
       
   133 TInt CIpsPlgImap4PopulateOp::IpsOpType() const
       
   134     {
       
   135     FUNC_LOG;
       
   136     return EIpsOpTypeImap4PopulateOp;
       
   137     }
       
   138 
       
   139 // ----------------------------------------------------------------------------
       
   140 // ----------------------------------------------------------------------------
       
   141 void CIpsPlgImap4PopulateOp::DoConnectL()
       
   142     {
       
   143     FUNC_LOG;
       
   144     iState = EStateConnecting;
       
   145     iStatus = KRequestPending;
       
   146 
       
   147     CIpsPlgImap4ConnectOp* connOp = CIpsPlgImap4ConnectOp::NewL(
       
   148         iMsvSession,
       
   149         KFetchOpPriority,
       
   150         iStatus, 
       
   151         iService,
       
   152         *iActivityTimer,
       
   153         iFSMailboxId,
       
   154         iFSOperationObserver,
       
   155         iFSRequestId,
       
   156         iEventHandler,
       
   157         ETrue, // Do plain connect
       
   158         EFalse );
       
   159         
       
   160     delete iOperation;
       
   161     iOperation = connOp;
       
   162 
       
   163     SetActive();
       
   164     }
       
   165     
       
   166 // ----------------------------------------------------------------------------
       
   167 // ----------------------------------------------------------------------------
       
   168 void CIpsPlgImap4PopulateOp::RunL()
       
   169     {
       
   170     FUNC_LOG;
       
   171     TRAPD(err, DoRunL());
       
   172     if(err != KErrNone )
       
   173         {
       
   174         iSyncProgress().iErrorCode = err;
       
   175         Complete();
       
   176         }
       
   177     }
       
   178 
       
   179 // ----------------------------------------------------------------------------
       
   180 // ----------------------------------------------------------------------------
       
   181 void CIpsPlgImap4PopulateOp::DoRunL()
       
   182     {
       
   183     FUNC_LOG;
       
   184     TInt err = iStatus.Int();
       
   185     delete iOperation;
       
   186     iOperation = NULL;
       
   187     
       
   188     switch( iState )
       
   189         {
       
   190         case EStateConnecting:
       
   191             {
       
   192             TMsvEntry tentry;
       
   193             TMsvId service;
       
   194             iMsvSession.GetEntry(iService, service, tentry );
       
   195             if( !tentry.Connected() )
       
   196                 {
       
   197                 CompleteObserver( KErrCouldNotConnect );
       
   198                 return;
       
   199                 }
       
   200             DoPopulateL();
       
   201             break;
       
   202             }
       
   203         case EStateFetching:         
       
   204             {
       
   205             if( err != KErrNone && iOperation )
       
   206                 {
       
   207                 iFetchErrorProgress = iOperation->ProgressL().AllocL();
       
   208                 iState = EStateIdle;
       
   209                 Complete();
       
   210                 }
       
   211             else
       
   212                 {
       
   213                 
       
   214                 }
       
   215             break;
       
   216             }
       
   217         case EStateInfoEntryChange:
       
   218             {
       
   219             DoPopulateL();
       
   220             break;
       
   221             }
       
   222         case EStateIdle:
       
   223         default:
       
   224             break;
       
   225         }
       
   226     }
       
   227 
       
   228 // ----------------------------------------------------------------------------
       
   229 // ----------------------------------------------------------------------------
       
   230 void CIpsPlgImap4PopulateOp::DoCancel()
       
   231     {
       
   232     FUNC_LOG;
       
   233     if( iOperation )
       
   234         {
       
   235         iOperation->Cancel();
       
   236         }
       
   237     CompleteObserver( KErrCancel );
       
   238     }
       
   239 
       
   240 // ----------------------------------------------------------------------------
       
   241 // ----------------------------------------------------------------------------
       
   242 const TDesC8& CIpsPlgImap4PopulateOp::ProgressL()
       
   243     {
       
   244     FUNC_LOG;
       
   245     if(iFetchErrorProgress && (iState == EStateIdle))
       
   246         {
       
   247         // Completed, but with an error during fetch.
       
   248         return *iFetchErrorProgress;
       
   249         }        
       
   250     TImap4SyncProgress progg;
       
   251     progg.iErrorCode = KErrNone;
       
   252     TPckgBuf<TImap4SyncProgress> param(progg);
       
   253     iSyncProgress.Copy(param);
       
   254     return iSyncProgress; 
       
   255     }
       
   256 
       
   257 // ----------------------------------------------------------------------------
       
   258 // ----------------------------------------------------------------------------
       
   259 const TDesC8& CIpsPlgImap4PopulateOp::GetErrorProgressL(TInt /*aError*/ )
       
   260     {
       
   261     FUNC_LOG;
       
   262     return *iFetchErrorProgress;
       
   263     }
       
   264 
       
   265 // ----------------------------------------------------------------------------
       
   266 // ----------------------------------------------------------------------------
       
   267 TFSProgress CIpsPlgImap4PopulateOp::GetFSProgressL() const
       
   268     {
       
   269     FUNC_LOG;
       
   270     // might not never called, but gives something reasonable if called
       
   271     TFSProgress result = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
       
   272     switch( iState )
       
   273         {
       
   274         case EStateConnecting:
       
   275             result.iProgressStatus = TFSProgress::EFSStatus_Connecting;
       
   276             break;
       
   277         case EStateFetching:
       
   278             result.iProgressStatus = TFSProgress::EFSStatus_Connected;
       
   279             break;
       
   280         default:
       
   281             result.iProgressStatus = TFSProgress::EFSStatus_RequestComplete;
       
   282             break;
       
   283         }
       
   284     if ( iStatus.Int() == KErrCancel )
       
   285         {
       
   286         result.iProgressStatus = TFSProgress::EFSStatus_RequestCancelled;
       
   287         result.iError = KErrCancel;
       
   288         }
       
   289     return result;
       
   290     }
       
   291 
       
   292 // ----------------------------------------------------------------------------
       
   293 // ----------------------------------------------------------------------------    
       
   294 TInt CIpsPlgImap4PopulateOp::GetEngineProgress( const TDesC8& aProgress )
       
   295     {
       
   296     FUNC_LOG;
       
   297     if( !aProgress.Length() )
       
   298         {
       
   299         return KErrNone;
       
   300         }
       
   301     else
       
   302         {
       
   303         TPckgBuf<TImap4CompoundProgress> paramPack;
       
   304         paramPack.Copy( aProgress );
       
   305         const TImap4GenericProgress& progress = paramPack().iGenericProgress;
       
   306         return progress.iErrorCode;        
       
   307         }    
       
   308     }    
       
   309 
       
   310 // ----------------------------------------------------------------------------
       
   311 // ----------------------------------------------------------------------------
       
   312 void CIpsPlgImap4PopulateOp::Complete()
       
   313     {
       
   314     FUNC_LOG;
       
   315     TRequestStatus* observer=&iObserverRequestStatus;
       
   316     User::RequestComplete(observer, KErrNone);
       
   317     }
       
   318 
       
   319 // ----------------------------------------------------------------------------
       
   320 // ----------------------------------------------------------------------------
       
   321 void CIpsPlgImap4PopulateOp::FilterSelectionL( 
       
   322         const CMsvEntrySelection& aSelection )
       
   323     {
       
   324     FUNC_LOG;
       
   325     iSelection.Reset();
       
   326     TMsvId messageId;
       
   327     // NOTE: this code is taken from symbian os source IMPCMTM.CPP
       
   328     // filter selection is in here because messages are
       
   329     // fetched separately then we dont have to make unneccessery imap 
       
   330     // client mtm calls
       
   331 
       
   332     for (TInt i=0; i<aSelection.Count(); i++)
       
   333         {
       
   334         messageId = (aSelection)[i];
       
   335         if ( messageId == iService )
       
   336             {
       
   337             continue;
       
   338             }
       
   339         TMsvEmailEntry entry;
       
   340         TMsvId service = KMsvNullIndexEntryId;          
       
   341         User::LeaveIfError(iMsvSession.GetEntry(messageId, service, entry));
       
   342         
       
   343         TBool isComplete = !(   ( entry.Complete() && entry.PartialDownloaded() ) 
       
   344                              || ( !entry.Complete() && ( !entry.BodyTextComplete() 
       
   345                              || ( iPartialMailInfo.iGetMailBodyParts == EGetImap4EmailBodyTextAndAttachments ) ) 
       
   346                                   && !entry.PartialDownloaded() ) );
       
   347 
       
   348         TBool isMsgEntry = entry.iType == KUidMsvMessageEntry;
       
   349         TBool isSizeUnderMax = entry.iSize <= iPartialMailInfo.iMaxEmailSize;
       
   350         TBool isParentComp = entry.Parent() == 
       
   351             iPartialMailInfo.iDestinationFolder && isComplete;
       
   352         
       
   353         if( IsPartialPopulate( )
       
   354               && !isComplete
       
   355               && entry.iType == KUidMsvMessageEntry )
       
   356             {
       
   357             iSelection.AppendL(messageId); 
       
   358             }
       
   359         else if ( isMsgEntry && isSizeUnderMax && !isParentComp )
       
   360             {
       
   361             iSelection.AppendL(messageId);
       
   362             }
       
   363         }
       
   364     }
       
   365 
       
   366 // ----------------------------------------------------------------------------
       
   367 // ----------------------------------------------------------------------------
       
   368 TBool CIpsPlgImap4PopulateOp::IsPartialPopulate( )
       
   369     {
       
   370     FUNC_LOG;
       
   371     // NOTE: this code is taken from symbian os source IMPCMTM.CPP
       
   372     // code is modified to this class purpose 
       
   373     
       
   374     TBool isPartialPopulate = EFalse;
       
   375     if(iPartialMailInfo.iPartialMailOptions == ENoSizeLimits &&
       
   376        iPartialMailInfo.iTotalSizeLimit == KMaxTInt &&
       
   377        iPartialMailInfo.iBodyTextSizeLimit == KMaxTInt && 
       
   378        iPartialMailInfo.iAttachmentSizeLimit == KMaxTInt && 
       
   379         (iPartialMailInfo.iGetMailBodyParts == EGetImap4EmailHeaders || 
       
   380          iPartialMailInfo.iGetMailBodyParts == EGetImap4EmailBodyText ||
       
   381          iPartialMailInfo.iGetMailBodyParts == EGetImap4EmailBodyTextAndAttachments ||
       
   382          iPartialMailInfo.iGetMailBodyParts == EGetImap4EmailAttachments ||
       
   383          iPartialMailInfo.iGetMailBodyParts == EGetImap4EmailBodyAlternativeText) )
       
   384         {
       
   385         isPartialPopulate = EFalse;
       
   386         }
       
   387     else
       
   388         {
       
   389         isPartialPopulate = ETrue;
       
   390         }
       
   391 
       
   392     return isPartialPopulate;
       
   393     }
       
   394 
       
   395 // ----------------------------------------------------------------------------
       
   396 // ----------------------------------------------------------------------------
       
   397 void CIpsPlgImap4PopulateOp::DoPopulateL( )
       
   398     {
       
   399     FUNC_LOG;
       
   400     if ( iSelection.Count() > 0 )
       
   401         {
       
   402         TMsvEmailEntry tEntry;
       
   403         TMsvId dummy;
       
   404         TInt lastIndex = iSelection.Count()-1;
       
   405         User::LeaveIfError( iMsvSession.GetEntry(
       
   406                 iSelection[lastIndex], dummy, tEntry ) );
       
   407         
       
   408         iState = EStateFetching;
       
   409 
       
   410         iTempSelection->Reset();
       
   411         iTempSelection->AppendL( iService );
       
   412         iTempSelection->AppendL( iSelection[lastIndex] );
       
   413         iSelection.Remove(lastIndex);
       
   414         
       
   415         
       
   416         iStatus = KRequestPending;
       
   417     
       
   418         // Filters are not used when performing 'fetch'
       
   419         // operation, use normal getmail info instead
       
   420         TPckg<TImImap4GetPartialMailInfo> param(iPartialMailInfo);
       
   421         iBaseMtm->SwitchCurrentEntryL( iService );
       
   422         iOperation = iBaseMtm->InvokeAsyncFunctionL(
       
   423                 KIMAP4MTMPopulateMailSelectionWhenAlreadyConnected, 
       
   424                 *iTempSelection, param, this->iStatus);
       
   425         iState = EStateInfoEntryChange;
       
   426         SetActive();
       
   427         }
       
   428     else
       
   429         {
       
   430         iState = EStateIdle;
       
   431         CompleteObserver();
       
   432         }
       
   433     }
       
   434 
       
   435 // End of File