ipsservices/ipssosplugin/src/ipsplgimap4moveremoteop.cpp
changeset 0 8466d47a6819
child 1 12c456ceeff2
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 *       Move a selection of messages that may or may not be complete
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "emailtrace.h"
       
    21 #include "ipsplgheaders.h"
       
    22 
       
    23 const TInt KMoveRemoteOpPriority = CActive::EPriorityStandard;
       
    24 
       
    25 // ----------------------------------------------------------------------------
       
    26 // ----------------------------------------------------------------------------
       
    27 CIpsPlgImap4MoveRemoteOp* CIpsPlgImap4MoveRemoteOp::NewL(
       
    28     CMsvSession& aMsvSession,
       
    29     TRequestStatus& aObserverRequestStatus,
       
    30     TInt aFunctionId,
       
    31     TMsvId aService,
       
    32     CIpsPlgTimerOperation& aActivityTimer,
       
    33     const TImImap4GetMailInfo& aGetMailInfo,
       
    34     const CMsvEntrySelection& aSel,
       
    35     TFSMailMsgId aFSMailBoxId,
       
    36     MFSMailRequestObserver& aFSOperationObserver,
       
    37     TInt aFSRequestId )
       
    38     {
       
    39     FUNC_LOG;
       
    40     CIpsPlgImap4MoveRemoteOp* op = new (ELeave) CIpsPlgImap4MoveRemoteOp(
       
    41         aMsvSession,
       
    42         aObserverRequestStatus,
       
    43         aFunctionId,
       
    44         aService,
       
    45         aActivityTimer,
       
    46         aGetMailInfo,
       
    47         aFSMailBoxId,
       
    48         aFSOperationObserver,
       
    49         aFSRequestId );
       
    50         
       
    51     CleanupStack::PushL( op );
       
    52     op->ConstructL( aSel );
       
    53     CleanupStack::Pop( op );
       
    54     return op;
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // ----------------------------------------------------------------------------
       
    59 CIpsPlgImap4MoveRemoteOp::CIpsPlgImap4MoveRemoteOp(
       
    60     CMsvSession& aMsvSession,
       
    61     TRequestStatus& aObserverRequestStatus,
       
    62     TInt aFunctionId,
       
    63     TMsvId aService,
       
    64     CIpsPlgTimerOperation& aActivityTimer,
       
    65     const TImImap4GetMailInfo& aGetMailInfo,
       
    66     TFSMailMsgId aFSMailBoxId,
       
    67     MFSMailRequestObserver& aFSOperationObserver,
       
    68     TInt aFSRequestId )
       
    69     :
       
    70     CIpsPlgOnlineOperation(
       
    71     aMsvSession,
       
    72     KMoveRemoteOpPriority,
       
    73     aObserverRequestStatus,
       
    74     aActivityTimer,
       
    75     aFSMailBoxId,
       
    76     aFSOperationObserver,
       
    77     aFSRequestId ),
       
    78     iFunctionId(aFunctionId),
       
    79     iGetMailInfo(aGetMailInfo)
       
    80     {
       
    81     FUNC_LOG;
       
    82     iService = aService;
       
    83     }
       
    84 
       
    85 // ----------------------------------------------------------------------------
       
    86 // ----------------------------------------------------------------------------    
       
    87 CIpsPlgImap4MoveRemoteOp::~CIpsPlgImap4MoveRemoteOp()
       
    88     {
       
    89     FUNC_LOG;
       
    90     delete iLocalSel;
       
    91     delete iRemoteSel;
       
    92     }
       
    93 
       
    94 // ----------------------------------------------------------------------------
       
    95 // ----------------------------------------------------------------------------
       
    96 void CIpsPlgImap4MoveRemoteOp::ConstructL( const CMsvEntrySelection& aSel )
       
    97     {
       
    98     FUNC_LOG;
       
    99     BaseConstructL(KUidMsgTypeIMAP4);
       
   100     SortMessageSelectionL( aSel );
       
   101     DoConnectL();
       
   102     }
       
   103 
       
   104 // ----------------------------------------------------------------------------
       
   105 // ----------------------------------------------------------------------------
       
   106 void CIpsPlgImap4MoveRemoteOp::DoConnectL()
       
   107     {
       
   108     FUNC_LOG;
       
   109     iState = EConnecting;
       
   110     iStatus = KRequestPending;
       
   111 
       
   112     CIpsPlgImap4ConnectOp* connOp = CIpsPlgImap4ConnectOp::NewL(
       
   113         iMsvSession,
       
   114         KMoveRemoteOpPriority,
       
   115         iStatus, 
       
   116         iService,
       
   117         *iActivityTimer,
       
   118         iFSMailboxId,
       
   119         iFSOperationObserver,
       
   120         iFSRequestId,
       
   121         NULL, // event handler not needed whin plain connect
       
   122         ETrue,
       
   123         EFalse );
       
   124         
       
   125     delete iOperation;
       
   126     iOperation = connOp;
       
   127 
       
   128     SetActive();
       
   129     }
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // ----------------------------------------------------------------------------
       
   133 const TDesC8& CIpsPlgImap4MoveRemoteOp::ProgressL()
       
   134     {
       
   135     FUNC_LOG;
       
   136     if( iMoveErrorProgress && (iState == EIdle) )
       
   137         {
       
   138         // Completed, but with an error during move.
       
   139         return *iMoveErrorProgress;
       
   140         }        
       
   141     TImap4SyncProgress progg;
       
   142     progg.iErrorCode = KErrNone;
       
   143     TPckgBuf<TImap4SyncProgress> param(progg);
       
   144     iSyncProgress.Copy(param);
       
   145     return iSyncProgress;  
       
   146     }
       
   147     
       
   148 // ----------------------------------------------------------------------------
       
   149 // ----------------------------------------------------------------------------  
       
   150 const TDesC8& CIpsPlgImap4MoveRemoteOp::GetErrorProgressL(TInt aError)
       
   151     {
       
   152     FUNC_LOG;
       
   153     // Called to report a leave in DoRunL()
       
   154     if(!iProgressBuf().iGenericProgress.iErrorCode)
       
   155         {
       
   156         TImap4CompoundProgress& prog = iProgressBuf();
       
   157         prog.iGenericProgress.iOperation = 
       
   158             TImap4GenericProgress::EMoveWithinService;
       
   159         prog.iGenericProgress.iState = TImap4GenericProgress::EMoving;
       
   160         prog.iGenericProgress.iErrorCode = aError;
       
   161         }
       
   162     return iProgressBuf;
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------------------------
       
   166 // ----------------------------------------------------------------------------
       
   167 TFSProgress CIpsPlgImap4MoveRemoteOp::GetFSProgressL() const
       
   168     {
       
   169     FUNC_LOG;
       
   170     // might not never called, but gives something reasonable if called
       
   171     TFSProgress result = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
       
   172     if ( iState == EConnecting )
       
   173         {
       
   174         result.iProgressStatus = TFSProgress::EFSStatus_Connecting;
       
   175         }
       
   176     else
       
   177         {
       
   178         result.iProgressStatus = TFSProgress::EFSStatus_RequestComplete;
       
   179         }
       
   180     return result;
       
   181     }
       
   182 
       
   183 // ----------------------------------------------------------------------------
       
   184 // ----------------------------------------------------------------------------
       
   185 void CIpsPlgImap4MoveRemoteOp::RunL()
       
   186     {
       
   187     FUNC_LOG;
       
   188     TRAPD(err, DoRunL());
       
   189     if(err != KErrNone)
       
   190         {
       
   191         iSyncProgress().iErrorCode = err;
       
   192         Complete();
       
   193         }
       
   194     }
       
   195     
       
   196 // ----------------------------------------------------------------------------
       
   197 // ----------------------------------------------------------------------------
       
   198 void CIpsPlgImap4MoveRemoteOp::DoRunL()
       
   199     {
       
   200     FUNC_LOG;
       
   201     TInt err = iStatus.Int();
       
   202     switch(iState)
       
   203         {
       
   204         case EConnecting:
       
   205             {
       
   206             TBool connected = STATIC_CAST(CIpsPlgImap4ConnectOp*, iOperation)->Connected();
       
   207             if( !connected )
       
   208                 {
       
   209                 CompleteObserver( KErrCouldNotConnect );
       
   210                 return;
       
   211                 }
       
   212             DoMoveLocalL();
       
   213             }
       
   214             break;
       
   215         case ELocalMsgs:
       
   216             // Local move complete
       
   217             if( err != KErrNone )
       
   218                 {
       
   219                 // Failed to copy local messages.
       
   220                 iState = EIdle;
       
   221                 CompleteObserver( err );
       
   222                 }
       
   223             else
       
   224                 {
       
   225                 DoMoveRemoteL();
       
   226                 }
       
   227             break;
       
   228         case ERemoteMsgs:
       
   229             // Remote move complete.
       
   230             if( err != KErrNone && iOperation )
       
   231                 {
       
   232                 iMoveErrorProgress = iOperation->ProgressL().AllocL();
       
   233                 }
       
   234             iState = EIdle;
       
   235             // to be considered
       
   236             // if this fails, should we move the messages back to local 
       
   237             // directory
       
   238             CompleteObserver( err );
       
   239             break;
       
   240         case EIdle:
       
   241         default:
       
   242             break;
       
   243         }
       
   244     }
       
   245 
       
   246 // ----------------------------------------------------------------------------
       
   247 // ----------------------------------------------------------------------------
       
   248 void CIpsPlgImap4MoveRemoteOp::SortMessageSelectionL(const CMsvEntrySelection& aSel)
       
   249     {
       
   250     FUNC_LOG;
       
   251     if ( aSel.Count() == 0 )
       
   252         {
       
   253         User::Leave( KErrNotSupported );
       
   254         }
       
   255     // Sort messages into complete and incomplete selections.
       
   256     iLocalSel = new(ELeave) CMsvEntrySelection;
       
   257     iRemoteSel = new(ELeave) CMsvEntrySelection;
       
   258 
       
   259     TInt err;
       
   260     TMsvId id;
       
   261     TMsvId service;
       
   262     TMsvEntry tEntry;
       
   263     TInt selCount( aSel.Count() );
       
   264     for( TInt count(0); count < selCount; count++ )
       
   265         {
       
   266         id = aSel[count];
       
   267         err = iMsvSession.GetEntry( id, service, tEntry );
       
   268         if( KErrNone == err )
       
   269             {
       
   270             // local move is not needed, if the message is not fetched
       
   271             if( tEntry.Complete() )
       
   272                 {
       
   273                 if ( 0 < count )
       
   274                     {
       
   275                     // service id is not added to local, 
       
   276                     // service is already
       
   277                     // added in MoveMessagesL
       
   278                     iLocalSel->AppendL( id );
       
   279                     }
       
   280                 }
       
   281                 iRemoteSel->AppendL( id );
       
   282                 }
       
   283             }
       
   284         }
       
   285 
       
   286 // ----------------------------------------------------------------------------
       
   287 // ----------------------------------------------------------------------------
       
   288 void CIpsPlgImap4MoveRemoteOp::Complete()
       
   289     {
       
   290     FUNC_LOG;
       
   291     TRequestStatus* observer = &iObserverRequestStatus;
       
   292     User::RequestComplete( observer, KErrNone );
       
   293     }
       
   294     
       
   295 // ----------------------------------------------------------------------------
       
   296 // ----------------------------------------------------------------------------
       
   297 void CIpsPlgImap4MoveRemoteOp::DoMoveLocalL()
       
   298     {
       
   299     FUNC_LOG;
       
   300     iState = ELocalMsgs;
       
   301     iStatus = KRequestPending;
       
   302     if( iLocalSel->Count() )
       
   303         {
       
   304         // this gets the first msg to be moved
       
   305         CMsvEntry* cEntry = iMsvSession.GetEntryL( (*iLocalSel)[0] );
       
   306         CleanupStack::PushL( cEntry );
       
   307         // find the parent of the moved message...
       
   308         TMsvId parent = cEntry->Entry().Parent();
       
   309         // and use it as a context
       
   310         cEntry->SetEntryL( parent );
       
   311 
       
   312         delete iOperation;
       
   313         iOperation = NULL;
       
   314         iOperation = cEntry->MoveL( *iLocalSel, 
       
   315                                     iGetMailInfo.iDestinationFolder, 
       
   316                                     iStatus );
       
   317         CleanupStack::PopAndDestroy( cEntry ); 
       
   318         SetActive();
       
   319         }
       
   320     else
       
   321         {
       
   322         SetActive();
       
   323         CompleteThis();
       
   324         }
       
   325     }
       
   326 
       
   327 // ----------------------------------------------------------------------------
       
   328 // ----------------------------------------------------------------------------
       
   329 void CIpsPlgImap4MoveRemoteOp::DoMoveRemoteL()
       
   330     {
       
   331     FUNC_LOG;
       
   332     iState = ERemoteMsgs;
       
   333     iStatus = KRequestPending;
       
   334     // first element of the CMsvEntrySelection is the service which is then
       
   335     // followed by any messages
       
   336     if( iRemoteSel->Count() > 1 )
       
   337         {
       
   338         // Switch operations.
       
   339         delete iOperation;
       
   340         iOperation = NULL;
       
   341     
       
   342         // Filters are not used when performing 'move' operation, use normal 
       
   343         // getmail info instead
       
   344         TPckg<TImImap4GetMailInfo> param( iGetMailInfo );
       
   345         InvokeClientMtmAsyncFunctionL( iFunctionId, *iRemoteSel, iService, param );
       
   346         SetActive();
       
   347         }
       
   348     else
       
   349         {
       
   350         SetActive();
       
   351         CompleteThis();
       
   352         }
       
   353     }
       
   354     
       
   355 // ----------------------------------------------------------------------------
       
   356 // ----------------------------------------------------------------------------    
       
   357 TInt CIpsPlgImap4MoveRemoteOp::GetEngineProgress( const TDesC8& aProgress )
       
   358     {
       
   359     FUNC_LOG;
       
   360     if( !aProgress.Length() )
       
   361         {
       
   362         return KErrNone;
       
   363         }
       
   364     else
       
   365         {
       
   366         TPckgBuf<TImap4CompoundProgress> paramPack;
       
   367         paramPack.Copy( aProgress );
       
   368         const TImap4GenericProgress& progress = paramPack().iGenericProgress;
       
   369 
       
   370         return progress.iErrorCode;
       
   371         }
       
   372     }
       
   373 
       
   374 // class CIpsPlgImap4MoveRemoteOpObserver
       
   375 //
       
   376 
       
   377 // ----------------------------------------------------------------------------
       
   378 // ---------------------------------------------------------------------------- 
       
   379 CIpsPlgImap4MoveRemoteOpObserver* CIpsPlgImap4MoveRemoteOpObserver::NewL(
       
   380     CMsvSession& aSession, CIpsPlgEventHandler& aEventHandler,
       
   381     const TFSMailMsgId& aSourceFolder,
       
   382     const RArray<TFSMailMsgId>& aMessageIds )
       
   383     {
       
   384     FUNC_LOG;
       
   385     CIpsPlgImap4MoveRemoteOpObserver* self
       
   386         = new ( ELeave ) CIpsPlgImap4MoveRemoteOpObserver( aSession,
       
   387             aEventHandler, aSourceFolder.Id() );
       
   388     CleanupStack::PushL( self );
       
   389     self->ConstructL( aMessageIds );
       
   390     CleanupStack::Pop( self );
       
   391     return self;
       
   392     }
       
   393 
       
   394 // ----------------------------------------------------------------------------
       
   395 // ---------------------------------------------------------------------------- 
       
   396 CIpsPlgImap4MoveRemoteOpObserver::~CIpsPlgImap4MoveRemoteOpObserver()
       
   397     {
       
   398     FUNC_LOG;
       
   399     delete iSelection;
       
   400     }
       
   401 
       
   402 // ----------------------------------------------------------------------------
       
   403 // ---------------------------------------------------------------------------- 
       
   404 void CIpsPlgImap4MoveRemoteOpObserver::RequestResponseL( TFSProgress aEvent,
       
   405     TInt /*aRequestId*/ )
       
   406     {
       
   407     FUNC_LOG;
       
   408     if ( aEvent.iProgressStatus == TFSProgress::EFSStatus_RequestCancelled ||
       
   409          aEvent.iError != KErrNone )
       
   410         {
       
   411         // Assumes that still existing entries have not been moved.
       
   412         for ( TInt ii = iSelection->Count() - 1; ii >= 0; --ii )
       
   413             {
       
   414             TMsvId id = iSelection->At(ii);
       
   415             TMsvId dummy = KMsvNullIndexEntryIdValue;
       
   416             TMsvEntry entry;
       
   417             if ( iSession.GetEntry( id, dummy, entry ) != KErrNone )
       
   418                 {
       
   419                 iSelection->Delete( ii );
       
   420                 }
       
   421             }
       
   422 
       
   423         if ( iSelection->Count() )
       
   424             {
       
   425             iEventHandler.HandleSessionEventL(
       
   426                 MMsvSessionObserver::EMsvEntriesMoved,
       
   427                 iSelection, &iSourceFolderId, &iSourceFolderId );
       
   428             }
       
   429         }
       
   430     }
       
   431 
       
   432 // ----------------------------------------------------------------------------
       
   433 // ---------------------------------------------------------------------------- 
       
   434 CIpsPlgImap4MoveRemoteOpObserver::CIpsPlgImap4MoveRemoteOpObserver(
       
   435     CMsvSession& aSession, CIpsPlgEventHandler& aEventHandler,
       
   436     TMsvId aSourceFolderId )
       
   437     : iSession( aSession ), iEventHandler( aEventHandler ),
       
   438     iSourceFolderId( aSourceFolderId )
       
   439     {
       
   440     FUNC_LOG;
       
   441     }
       
   442 
       
   443 // ----------------------------------------------------------------------------
       
   444 // ----------------------------------------------------------------------------     
       
   445 void CIpsPlgImap4MoveRemoteOpObserver::ConstructL(
       
   446     const RArray<TFSMailMsgId>& aMessageIds )
       
   447     {
       
   448     FUNC_LOG;
       
   449     TInt count = aMessageIds.Count();
       
   450     iSelection = new ( ELeave ) CMsvEntrySelection;
       
   451     for ( TInt ii = 0; ii < count; ++ii )
       
   452         {
       
   453         iSelection->AppendL( aMessageIds[ii].Id() );
       
   454         }
       
   455     }
       
   456