emailservices/emailstore/base_plugin/src/baseplugindelayedops.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
child 18 578830873419
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Email interface implementation.
       
    15 *
       
    16 */
       
    17 #include "MsgStoreWritablePropertyContainer.h"
       
    18 
       
    19 #include "baseplugindelayedops.h"
       
    20 #include "baseplugindelayedopsprivate.h"
       
    21 
       
    22 
       
    23 ///////////////////////////////////////////////////
       
    24 // CDelayedOp                                    //
       
    25 ///////////////////////////////////////////////////
       
    26 
       
    27 /**
       
    28  * 
       
    29  */
       
    30 /*public virtual*/ EXPORT_C CDelayedOp::~CDelayedOp()
       
    31     {
       
    32     Cancel();
       
    33     __LOG_DESTRUCT
       
    34     }
       
    35 
       
    36 /**
       
    37  * 
       
    38  */
       
    39 /*public*/ EXPORT_C void CDelayedOp::SetContext(
       
    40     CBasePlugin& aPlugin, MDelayedOpsManager& aManager )
       
    41     {
       
    42     iPlugin = &aPlugin;
       
    43     iManager = &aManager;
       
    44     } 
       
    45 
       
    46 /**
       
    47  * 
       
    48  */
       
    49 /*protected*/ EXPORT_C CDelayedOp::CDelayedOp()
       
    50     : CAsyncOneShot( CActive::EPriorityIdle )
       
    51     {    
       
    52     }
       
    53     
       
    54 /**
       
    55  * 
       
    56  */
       
    57 /*private virtual*/ EXPORT_C void CDelayedOp::RunL()
       
    58     {
       
    59     __LOG_ENTER_SUPPRESS( "Run" );
       
    60     TRAPD( err, ExecuteOpL() );
       
    61     
       
    62     if ( KErrNone != err )
       
    63         {
       
    64         __LOG_WRITE8_FORMAT1_INFO(
       
    65             "Error while executing delayed operation: %d.", err );
       
    66         }
       
    67     
       
    68     //self-destroy.
       
    69     iManager->DequeueOp( *this );
       
    70     delete this;
       
    71     }
       
    72     
       
    73 /**
       
    74  * 
       
    75  */
       
    76 /*protected*/ EXPORT_C CBasePlugin& CDelayedOp::GetPlugin()
       
    77     {
       
    78     return *iPlugin;
       
    79     }
       
    80 
       
    81 /**
       
    82  * 
       
    83  */
       
    84 /*private virtual*/ EXPORT_C void CDelayedOp::DoCancel()
       
    85     {
       
    86     }
       
    87 
       
    88 
       
    89 ///////////////////////////////////////////////////
       
    90 // CDelayedOpsManager                            //
       
    91 ///////////////////////////////////////////////////
       
    92 
       
    93 /**
       
    94  * 
       
    95  */
       
    96 /*static public*/
       
    97 CDelayedOpsManager* CDelayedOpsManager::NewL( CBasePlugin& aPlugin )
       
    98     {
       
    99     CDelayedOpsManager* self = new (ELeave) CDelayedOpsManager( aPlugin );
       
   100     CleanupStack::PushL( self );
       
   101     self->ConstructL();
       
   102     CleanupStack::Pop( self );
       
   103     return self;
       
   104     }
       
   105     
       
   106 /**
       
   107  * 
       
   108  */
       
   109 /*public virtual*/ CDelayedOpsManager::~CDelayedOpsManager()
       
   110     {
       
   111     ExecutePendingOps();
       
   112     
       
   113     iDelayedOps.ResetAndDestroy();
       
   114     iDelayedOps.Close();
       
   115     
       
   116     __LOG_DESTRUCT
       
   117     }
       
   118     
       
   119 /**
       
   120  * 
       
   121  */
       
   122 /*public virtual*/ void CDelayedOpsManager::EnqueueOpL( CDelayedOp* aOp )
       
   123     {
       
   124     iDelayedOps.AppendL( aOp );
       
   125     aOp->SetContext( iPlugin, *this );        
       
   126     aOp->Call();
       
   127     }
       
   128     
       
   129 /**
       
   130  * 
       
   131  */
       
   132 /*public virtual*/ void CDelayedOpsManager::DequeueOp( const CDelayedOp& aOp )
       
   133     {
       
   134     TInt count = iDelayedOps.Count();
       
   135     for ( TInt i = 0; i < count; ++i )
       
   136         {
       
   137         CDelayedOp* op = iDelayedOps[i];
       
   138         if ( &aOp == op )
       
   139             {
       
   140             iDelayedOps.Remove( i );
       
   141             break;
       
   142             }
       
   143         }
       
   144     }
       
   145     
       
   146 /**
       
   147  * 
       
   148  */
       
   149 /*public virtual*/ TInt CDelayedOpsManager::Extension1(
       
   150     TUint /*aExtensionId*/, TAny*& /*a0*/, TAny* /*a1*/ )
       
   151     {
       
   152     return KErrNotSupported;
       
   153     }
       
   154 
       
   155 /**
       
   156  * 
       
   157  */
       
   158 /*private*/ CDelayedOpsManager::CDelayedOpsManager( CBasePlugin& aPlugin )
       
   159     : iPlugin ( aPlugin )
       
   160     {
       
   161     }
       
   162 
       
   163 /**
       
   164  * 
       
   165  */
       
   166 /*private*/ void CDelayedOpsManager::ConstructL()
       
   167     {
       
   168     __LOG_CONSTRUCT( "baseplugin", "CDelayedOpsManager" );
       
   169     }
       
   170 
       
   171 /**
       
   172  * 
       
   173  */
       
   174 /*private*/ void CDelayedOpsManager::ExecutePendingOps()
       
   175     {
       
   176     //check the count on every iteration to avoid missing operations being
       
   177     //enqueued by another operations.
       
   178     for ( TInt i = 0; i < iDelayedOps.Count(); ++i )
       
   179         {
       
   180         CDelayedOp* op = iDelayedOps[i];
       
   181         
       
   182         op->Cancel();
       
   183         TRAP_IGNORE( op->ExecuteOpL() );
       
   184         }
       
   185     }    
       
   186 
       
   187 
       
   188 ///////////////////////////////////////////////////
       
   189 // CDelayedDeleteMessagesOp                      //
       
   190 ///////////////////////////////////////////////////
       
   191 
       
   192 /**
       
   193  * 
       
   194  */
       
   195 /*public static */ CDelayedDeleteMessagesOp* CDelayedDeleteMessagesOp::NewLC(
       
   196     TMsgStoreId aMailBoxId,
       
   197     TMsgStoreId aFolderId,
       
   198     const RArray<TFSMailMsgId>& aMessages )
       
   199     {
       
   200     CDelayedDeleteMessagesOp* self = new (ELeave) CDelayedDeleteMessagesOp(
       
   201         aMailBoxId, aFolderId );
       
   202     CleanupStack::PushL( self );
       
   203     self->ConstructL( aMessages );
       
   204     return self;
       
   205     }
       
   206 
       
   207 /**
       
   208  * 
       
   209  */
       
   210 /*public static */ CDelayedDeleteMessagesOp* CDelayedDeleteMessagesOp::NewLC(
       
   211     TMsgStoreId aMailBoxId,
       
   212     TMsgStoreId aFolderId,
       
   213     TMsgStoreId aMsgId )
       
   214     {
       
   215     CDelayedDeleteMessagesOp* self = new (ELeave) CDelayedDeleteMessagesOp(
       
   216         aMailBoxId, aFolderId );
       
   217     CleanupStack::PushL( self );
       
   218     self->ConstructL( aMsgId );
       
   219     return self;
       
   220     }
       
   221 
       
   222 /**
       
   223  * 
       
   224  */
       
   225 /*public virtual*/ CDelayedDeleteMessagesOp::~CDelayedDeleteMessagesOp()
       
   226     {
       
   227     iMessages.Close();
       
   228     __LOG_DESTRUCT
       
   229     }
       
   230 
       
   231 
       
   232 /**
       
   233  * 
       
   234  */
       
   235 /*private*/
       
   236 void CDelayedDeleteMessagesOp::ConstructL(
       
   237     const RArray<TFSMailMsgId>& aMessages )
       
   238     {
       
   239     __LOG_CONSTRUCT( "baseplugin", "CDelayedDeleteMessagesOp" );
       
   240             
       
   241     TInt count = aMessages.Count();
       
   242     for ( TInt i = 0; i < count; ++i )
       
   243         {
       
   244         iMessages.AppendL( aMessages[i].Id() );
       
   245         }
       
   246     }
       
   247 
       
   248 /**
       
   249  * 
       
   250  */
       
   251 /*private*/
       
   252 void CDelayedDeleteMessagesOp::ConstructL(
       
   253     TMsgStoreId aMsgId )
       
   254     {
       
   255     __LOG_CONSTRUCT( "baseplugin", "CDelayedDeleteMessagesOp" );
       
   256     iImmediateDelete = ETrue;
       
   257     iMessages.AppendL( aMsgId );
       
   258     }
       
   259 
       
   260 /**
       
   261  * 
       
   262  */
       
   263 /*private*/ CDelayedDeleteMessagesOp::CDelayedDeleteMessagesOp(
       
   264     TMsgStoreId aMailBoxId,
       
   265     TMsgStoreId aFolderId )
       
   266     :
       
   267     iMailBoxId( aMailBoxId ), iFolderId( aFolderId ),
       
   268     iImmediateDelete( EFalse )
       
   269     {
       
   270     }
       
   271 
       
   272 /**
       
   273  * 
       
   274  */
       
   275 /*private*/ void CDelayedDeleteMessagesOp::ExecuteOpL()
       
   276     {
       
   277     __LOG_ENTER( "ExecuteOpL" );
       
   278     
       
   279     CMailboxInfo& mailBoxInfo
       
   280         = GetPlugin().GetMailboxInfoL( iMailBoxId );
       
   281     CMsgStoreMailBox& mailBox = mailBoxInfo();
       
   282 
       
   283     TInt count = iMessages.Count();
       
   284     for ( TInt i = 0; i < count; ++i )
       
   285         {
       
   286         TMsgStoreId msgId = iMessages[i];
       
   287         
       
   288         if ( EFalse == iImmediateDelete )
       
   289             {
       
   290             //try to find the message in the deleted items folder.
       
   291             CMsgStoreMessage* theMessage = NULL;
       
   292             TRAP_IGNORE( theMessage = mailBox.FetchMessageL(
       
   293               msgId, mailBoxInfo.iRootFolders.iFolders[EFSDeleted] ) );
       
   294             
       
   295             if ( NULL == theMessage )
       
   296                 {
       
   297                 //if not in deleted items then move it there.
       
   298                 __LOG_WRITE8_FORMAT1_INFO(
       
   299                      "Moving message 0x%X to the deleted items.", msgId );
       
   300                 mailBox.MoveMessageL(
       
   301                    msgId, KMsgStoreInvalidId,
       
   302                    mailBoxInfo.iRootFolders.iFolders[EFSDeleted] );
       
   303                 }
       
   304             else
       
   305                 {
       
   306                 //in deleted items, really delete it.
       
   307                 __LOG_WRITE8_FORMAT1_INFO( "Deleting message 0x%X.", msgId );
       
   308 
       
   309                 delete theMessage;
       
   310                 mailBox.DeleteMessageL( msgId, iFolderId );
       
   311                 }
       
   312             }
       
   313         else
       
   314             {
       
   315             mailBox.DeleteMessageL( msgId, iFolderId );
       
   316             }
       
   317         }
       
   318 
       
   319     __LOG_EXIT;
       
   320     }
       
   321 
       
   322 
       
   323 ///////////////////////////////////////////////////
       
   324 // CDelayedStorePropertiesOp                     //
       
   325 ///////////////////////////////////////////////////
       
   326 
       
   327 /**
       
   328  * 
       
   329  */
       
   330 /*public static*/
       
   331 CDelayedSetContentOp* CDelayedSetContentOp::NewLC(
       
   332     TMsgStoreId aMailBoxId,
       
   333     TMsgStoreId aMessageId,
       
   334     TMsgStoreId aMessagePartId,
       
   335     const TDesC& aContent )
       
   336     {
       
   337     CDelayedSetContentOp* self = new (ELeave) CDelayedSetContentOp(
       
   338         aMailBoxId, aMessageId, aMessagePartId );
       
   339     CleanupStack::PushL( self );
       
   340     self->ConstructL( aContent );
       
   341     return self;
       
   342     }
       
   343 
       
   344 /**
       
   345  * 
       
   346  */
       
   347 /*public static*/
       
   348 CDelayedSetContentOp* CDelayedSetContentOp::NewLC(
       
   349     TMsgStoreId aMailBoxId,
       
   350     TMsgStoreId aMessageId,
       
   351     TMsgStoreId aMessagePartId,
       
   352     TInt aContentLength )
       
   353     {
       
   354     CDelayedSetContentOp* self = new (ELeave) CDelayedSetContentOp(
       
   355         aMailBoxId, aMessageId, aMessagePartId, aContentLength );
       
   356     CleanupStack::PushL( self );
       
   357     self->ConstructL();
       
   358     return self;
       
   359     }
       
   360 
       
   361 /**
       
   362  * 
       
   363  */
       
   364 /*public virtual*/ CDelayedSetContentOp::~CDelayedSetContentOp()
       
   365     {
       
   366     delete iContent;
       
   367     
       
   368     __LOG_DESTRUCT
       
   369     }
       
   370 
       
   371 /**
       
   372  * 
       
   373  */
       
   374 /*private*/ void CDelayedSetContentOp::ConstructL( const TDesC& aContent )
       
   375     {
       
   376     __LOG_CONSTRUCT( "baseplugin", "CDelayedSetContentOp1" );
       
   377     
       
   378     iContent = aContent.AllocL();
       
   379     }
       
   380 
       
   381 /**
       
   382  * 
       
   383  */
       
   384 /*private*/ void CDelayedSetContentOp::ConstructL()
       
   385     {
       
   386     __LOG_CONSTRUCT( "baseplugin", "CDelayedSetContentOp2" );
       
   387     }
       
   388 
       
   389 /**
       
   390  * CDelayedOp::ExecuteOpL
       
   391  */
       
   392 /*public virtual*/ void CDelayedSetContentOp::ExecuteOpL()
       
   393     {
       
   394     __LOG_ENTER( "ExecuteOpL" )
       
   395 
       
   396     CMsgStoreMessagePart* part = FetchMessagePartLC();
       
   397 
       
   398     if ( iStepOne )
       
   399         {
       
   400         part->ReplaceContentL(
       
   401             TPtrC8( reinterpret_cast<const TUint8*>(
       
   402                 iContent->Ptr() ), iContent->Size() ) );
       
   403         __LOG_WRITE8_FORMAT1_INFO(
       
   404             "Stored the content of part 0x%X.", part->Id() )
       
   405         }
       
   406     else
       
   407         {
       
   408         part->AddOrUpdatePropertyL(
       
   409             KMsgStorePropertySize, static_cast<TUint32>( iContentLength ) );
       
   410         part->AddOrUpdatePropertyL(
       
   411             KMsgStorePropertyRetrievedSize,
       
   412             static_cast<TUint32>( iContentLength ) );
       
   413         
       
   414         part->StorePropertiesL();
       
   415         __LOG_WRITE8_FORMAT1_INFO(
       
   416             "Updated the properties of part 0x%X.", part->Id() )
       
   417         }
       
   418     
       
   419     CleanupStack::PopAndDestroy( part );    
       
   420     __LOG_EXIT
       
   421     }
       
   422 
       
   423 /**
       
   424  * 
       
   425  */
       
   426 /*private*/ CMsgStoreMessagePart* CDelayedSetContentOp::FetchMessagePartLC()
       
   427     {
       
   428     __LOG_ENTER_SUPPRESS( "FetchMessagePartLC" )
       
   429     CMailboxInfo& mailBox = GetPlugin().GetMailboxInfoL( iMailBoxId );
       
   430     
       
   431     CMsgStoreMessage* msg = mailBox().FetchMessageL(
       
   432         iMessageId, KMsgStoreInvalidId );
       
   433     CleanupStack::PushL( msg );
       
   434     
       
   435     CMsgStoreMessagePart* part;
       
   436     if ( KMsgStoreInvalidId == iMessagePartId )
       
   437         {
       
   438         __LOG_WRITE_INFO( "Fetched the message itself." );
       
   439         //watch the cleanupstack, part will point to the same thing thus the
       
   440         //popanddestroy is ok.
       
   441         part = msg;
       
   442         }
       
   443     else
       
   444         {
       
   445         __LOG_WRITE_INFO( "Fetched a child part of the message." );
       
   446         part = msg->ChildPartL( iMessagePartId, ETrue );
       
   447         CleanupStack::PopAndDestroy( msg );
       
   448         CleanupStack::PushL( part );
       
   449         }
       
   450     
       
   451     return part;
       
   452     }
       
   453 
       
   454 /**
       
   455  * 
       
   456  */
       
   457 /*private*/ CDelayedSetContentOp::CDelayedSetContentOp(
       
   458     TMsgStoreId aMailBoxId,
       
   459     TMsgStoreId aMessageId,
       
   460     TMsgStoreId aMessagePartId )
       
   461     : iMailBoxId( aMailBoxId ), iMessageId( aMessageId ),
       
   462       iMessagePartId( aMessagePartId ), iStepOne( ETrue )
       
   463     {
       
   464     }
       
   465 
       
   466 /**
       
   467  * 
       
   468  */
       
   469 /*private*/ CDelayedSetContentOp::CDelayedSetContentOp(
       
   470     TMsgStoreId aMailBoxId,
       
   471     TMsgStoreId aMessageId,
       
   472     TMsgStoreId aMessagePartId,
       
   473     TInt aContentLength )
       
   474     : iMailBoxId( aMailBoxId ), iMessageId( aMessageId ),
       
   475       iMessagePartId( aMessagePartId ), iContentLength( aContentLength ),
       
   476       iStepOne( EFalse )
       
   477     {
       
   478     }