emailservices/emailframework/commonlib/src/CFSMailBox.cpp
branchRCL_3
changeset 25 3533d4323edc
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 2007-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:  common mailbox object
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include <bamdesca.h>
       
    20 
       
    21 
       
    22 #include "cfsmailplugin.h"
       
    23 #include "cmrcalendarinfoimpl.h"
       
    24 #include "emailextensionuids.hrh"
       
    25 
       
    26 #include "cfsmailbox.h"
       
    27 #include "cfsmailrequestobserver.h"
       
    28 
       
    29 #include "cmailboxstateext.h"
       
    30 #include "cconnectionstatusqueryext.h" 
       
    31 
       
    32 const TInt KMaxMruEntries( 150 );
       
    33 
       
    34 // ================= MEMBER FUNCTIONS ==========================================
       
    35 // -----------------------------------------------------------------------------
       
    36 // CFSMailBox::NewLC
       
    37 // -----------------------------------------------------------------------------
       
    38 EXPORT_C CFSMailBox* CFSMailBox::NewLC(TFSMailMsgId aMailBoxId)
       
    39 {
       
    40     FUNC_LOG;
       
    41   CFSMailBox* api = new (ELeave) CFSMailBox();
       
    42   CleanupStack:: PushL(api);
       
    43   api->ConstructL(aMailBoxId);
       
    44   return api;
       
    45 } 
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CFSMailBox::NewL
       
    49 // -----------------------------------------------------------------------------
       
    50 EXPORT_C CFSMailBox* CFSMailBox::NewL(TFSMailMsgId aMailBoxId)
       
    51 {
       
    52     FUNC_LOG;
       
    53   CFSMailBox* api =  CFSMailBox::NewLC(aMailBoxId);
       
    54   CleanupStack:: Pop(api);
       
    55   return api;
       
    56 }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CFSMailBox::CFSMailBox
       
    60 // -----------------------------------------------------------------------------
       
    61 CFSMailBox::CFSMailBox()
       
    62 {
       
    63     FUNC_LOG;
       
    64     // get requesthandler pointer
       
    65     iRequestHandler = static_cast<CFSMailRequestHandler*>(Dll::Tls());
       
    66 }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CFSMailBox::~CFSMailBox
       
    70 // -----------------------------------------------------------------------------
       
    71 EXPORT_C CFSMailBox::~CFSMailBox()
       
    72 {
       
    73     FUNC_LOG;
       
    74     // extension KMailboxExtMrCalInfo needs to be released here because of
       
    75     // MR UI
       
    76     CEmailExtension* extension = NULL;
       
    77     TRAPD( err, extension = CExtendableEmail::ExtensionL( KMailboxExtMrCalInfo ) );
       
    78     if( extension != NULL && err==KErrNone )
       
    79         {
       
    80         // Release extension needs to be called twice because, previous
       
    81         // CExtendableEmail::ExtensionL( KMailboxExtMrCalInfo ) call increased
       
    82         // reference counter with one, so there is need to relase this one also.
       
    83         while( extension->DecRef() > 1 ) {}
       
    84         CExtendableEmail::ReleaseExtension( extension );
       
    85         }
       
    86     iFolders.ResetAndDestroy();
       
    87 }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CFSMailBox::ConstructL
       
    91 // -----------------------------------------------------------------------------
       
    92 void CFSMailBox::ConstructL(TFSMailMsgId aMailBoxId)
       
    93 {
       
    94     FUNC_LOG;
       
    95     // <cmail>
       
    96     const TFSMailMsgId myId = aMailBoxId;
       
    97     CFSMailBoxBase::ConstructL(myId);   
       
    98     iMailBoxId = aMailBoxId; 
       
    99     // </cmail>    
       
   100 }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CFSMailBox::GoOnlineL
       
   104 // -----------------------------------------------------------------------------
       
   105 EXPORT_C void CFSMailBox::GoOnlineL()
       
   106     {
       
   107     FUNC_LOG;
       
   108     
       
   109     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   110         {
       
   111         plugin->GoOnlineL( GetId() );
       
   112         }
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CFSMailBox::GoOfflineL
       
   117 // -----------------------------------------------------------------------------
       
   118 EXPORT_C void CFSMailBox::GoOfflineL()
       
   119     {
       
   120     FUNC_LOG;
       
   121     
       
   122     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   123         {
       
   124         plugin->GoOfflineL( GetId() );
       
   125         }
       
   126         
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CFSMailBox::CancelSyncL
       
   131 // -----------------------------------------------------------------------------
       
   132 EXPORT_C void CFSMailBox::CancelSyncL()
       
   133     {
       
   134     FUNC_LOG;
       
   135     
       
   136     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   137         {
       
   138         plugin->CancelSyncL( GetId() );
       
   139         }
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CFSMailBox::GetLastSyncStatusL
       
   144 // -----------------------------------------------------------------------------
       
   145 EXPORT_C TFSProgress CFSMailBox::GetLastSyncStatusL()
       
   146     {
       
   147     FUNC_LOG;
       
   148     TFSProgress progress;
       
   149     progress.iError = EFalse;
       
   150     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   151         {
       
   152         progress = plugin->GetLastSyncStatusL( GetId() );       
       
   153         }
       
   154     return progress;
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CFSMailBox::RefreshNowL
       
   159 // -----------------------------------------------------------------------------
       
   160 EXPORT_C TInt CFSMailBox::RefreshNowL(
       
   161     MFSMailRequestObserver& aOperationObserver )
       
   162     {
       
   163     FUNC_LOG;
       
   164 
       
   165     TFSPendingRequest request;
       
   166     request.iRequestId = 0;
       
   167     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   168         {
       
   169         // init asynchronous request
       
   170         request = iRequestHandler->InitAsyncRequestL( GetId().PluginId(),
       
   171                                                         aOperationObserver );
       
   172         MFSMailRequestObserver* observer = request.iObserver;
       
   173         TRAPD(err,plugin->RefreshNowL( GetId(), *observer, request.iRequestId));
       
   174         if(err != KErrNone)
       
   175             {
       
   176             iRequestHandler->CompleteRequest(request.iRequestId);
       
   177             User::Leave(err);
       
   178             }
       
   179         }
       
   180     return request.iRequestId;
       
   181 
       
   182    }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CFSMailBox::RefreshNowL
       
   186 // -----------------------------------------------------------------------------
       
   187 EXPORT_C TInt CFSMailBox::RefreshNowL( )
       
   188     {
       
   189     FUNC_LOG;
       
   190 
       
   191     TFSPendingRequest request;
       
   192     request.iRequestId = 0;
       
   193     MFSMailRequestObserver* observer = NULL;
       
   194     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   195         {
       
   196         // init asynchronous request
       
   197         request = iRequestHandler->InitAsyncRequestL( GetId().PluginId(), *observer );
       
   198         
       
   199         observer = request.iObserver;
       
   200         TRAPD(err,plugin->RefreshNowL( GetId(), *observer, request.iRequestId));
       
   201         if(err != KErrNone)
       
   202             {
       
   203             iRequestHandler->CompleteRequest(request.iRequestId);
       
   204             User::Leave(err);
       
   205             }
       
   206         }
       
   207     return request.iRequestId;
       
   208 
       
   209    }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CFSMailBox::CreateMessageToSend
       
   213 // -----------------------------------------------------------------------------
       
   214 EXPORT_C CFSMailMessage* CFSMailBox::CreateMessageToSend( )
       
   215 {
       
   216     FUNC_LOG;
       
   217 
       
   218     CFSMailMessage* message = NULL;
       
   219     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   220         {
       
   221         TRAPD(err,message = plugin->CreateMessageToSendL( GetId() ));
       
   222         if(err != KErrNone)
       
   223             {
       
   224             message = NULL;
       
   225             }
       
   226         }
       
   227     return message;
       
   228 }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CFSMailBox::CreateForwardMessage
       
   232 // -----------------------------------------------------------------------------
       
   233 EXPORT_C CFSMailMessage* CFSMailBox::CreateForwardMessage(  TFSMailMsgId aOriginalMessageId,
       
   234                                                             const TDesC& aHeaderDescriptor )
       
   235 {
       
   236     FUNC_LOG;
       
   237     CFSMailMessage* message = NULL;
       
   238     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   239         {
       
   240         TRAPD(err,message = plugin->CreateForwardMessageL( GetId(), 
       
   241                                                                 aOriginalMessageId,
       
   242                                                                 aHeaderDescriptor ));
       
   243         if(err != KErrNone)
       
   244             {
       
   245             message = NULL;
       
   246             }
       
   247         }
       
   248     return message;
       
   249 }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CFSMailBox::CreateReplyMessage
       
   253 // -----------------------------------------------------------------------------
       
   254 EXPORT_C CFSMailMessage* CFSMailBox::CreateReplyMessage( TFSMailMsgId aOriginalMessageId,
       
   255                                                          TBool aReplyToAll,
       
   256                                                          const TDesC& aHeaderDescriptor )
       
   257 {
       
   258     FUNC_LOG;
       
   259     CFSMailMessage* message = NULL;
       
   260     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   261         {
       
   262         TRAPD(err,message = plugin->CreateReplyMessageL( GetId(),
       
   263                                                             aOriginalMessageId,
       
   264                                                             aReplyToAll,
       
   265                                                             aHeaderDescriptor ));
       
   266         if(err != KErrNone)
       
   267             {
       
   268             message = NULL;
       
   269             }
       
   270         }
       
   271     return message;
       
   272 }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // CFSMailBox::GetStandardFolderId
       
   276 // -----------------------------------------------------------------------------
       
   277 EXPORT_C TFSMailMsgId CFSMailBox::GetStandardFolderId(TFSFolderType aFolderType)
       
   278 {
       
   279     FUNC_LOG;
       
   280 
       
   281     TFSMailMsgId folderId;
       
   282     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   283         {
       
   284         TRAPD(err, folderId = plugin->GetStandardFolderIdL(GetId(), aFolderType ));
       
   285         if(err != KErrNone)
       
   286             {
       
   287             folderId.SetNullId();
       
   288             }
       
   289         }
       
   290     return folderId;
       
   291 }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CFSMailBox::SendMessageL
       
   295 // -----------------------------------------------------------------------------
       
   296 EXPORT_C void CFSMailBox::SendMessageL( CFSMailMessage& aMessage )
       
   297     {
       
   298     FUNC_LOG;
       
   299     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   300         {
       
   301         UpdateMrusL( aMessage.GetToRecipients(),
       
   302                      aMessage.GetCCRecipients(),
       
   303                      aMessage.GetBCCRecipients() );
       
   304         plugin->SendMessageL( aMessage );
       
   305         
       
   306         }
       
   307     }
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CFSMailBox::ListFolders
       
   311 // -----------------------------------------------------------------------------
       
   312 EXPORT_C void CFSMailBox::ListFolders(  TFSMailMsgId aFolder,
       
   313                                         RPointerArray<CFSMailFolder>& aFolderList)
       
   314 {
       
   315     FUNC_LOG;
       
   316 
       
   317     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   318     {
       
   319         TRAPD(err, plugin->ListFoldersL(GetId(),aFolder,aFolderList));
       
   320         if(err != KErrNone)
       
   321             {
       
   322             aFolderList.ResetAndDestroy();
       
   323             }
       
   324     }
       
   325 }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CFSMailBox::ListFolders
       
   329 // -----------------------------------------------------------------------------
       
   330 EXPORT_C RPointerArray<CFSMailFolder>& CFSMailBox::ListFolders( )
       
   331 {   
       
   332     iFolders.ResetAndDestroy();
       
   333     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   334     {
       
   335         TRAPD(err,plugin->ListFoldersL(GetId(),iFolders));
       
   336         if(err != KErrNone)
       
   337             {
       
   338             iFolders.ResetAndDestroy();
       
   339             }
       
   340     }
       
   341 
       
   342     return iFolders;
       
   343 }
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CFSMailBox::GetBrandingIdL
       
   347 // -----------------------------------------------------------------------------
       
   348 EXPORT_C TDesC& CFSMailBox::GetBrandingIdL( )
       
   349 {
       
   350     FUNC_LOG;
       
   351 	if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   352         {
       
   353         TDesC& result = plugin->GetBrandingIdL( GetId() );
       
   354         return result;
       
   355         }
       
   356 
       
   357     return BrandingId();
       
   358 }
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 // CFSMailBox::MoveMessagesL
       
   362 // -----------------------------------------------------------------------------
       
   363 EXPORT_C void CFSMailBox::MoveMessagesL( const RArray<TFSMailMsgId>& aMessageIds, 
       
   364                                          const TFSMailMsgId aSourceFolderId, 
       
   365                                          const TFSMailMsgId aDestinationFolderId )
       
   366 {
       
   367     FUNC_LOG;
       
   368     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   369         {
       
   370         plugin->MoveMessagesL(GetId(), aMessageIds, aSourceFolderId, aDestinationFolderId);
       
   371         }
       
   372 }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CFSMailBox::MoveMessagesL
       
   376 // -----------------------------------------------------------------------------
       
   377 EXPORT_C TInt CFSMailBox::MoveMessagesL( MFSMailRequestObserver& aOperationObserver,
       
   378                                          const RArray<TFSMailMsgId>& aMessageIds, 
       
   379                                          const TFSMailMsgId aSourceFolderId, 
       
   380                                          const TFSMailMsgId aDestinationFolderId )
       
   381 {
       
   382     FUNC_LOG;
       
   383     TFSPendingRequest request;
       
   384     request.iRequestId = 0;
       
   385     if( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   386         {
       
   387         // init asynchronous request
       
   388         request = iRequestHandler->InitAsyncRequestL( GetId().PluginId(),
       
   389                                                         aOperationObserver );        
       
   390         
       
   391         plugin->MoveMessagesL(
       
   392             GetId(), 
       
   393             aMessageIds, 
       
   394             aSourceFolderId, 
       
   395             aDestinationFolderId, 
       
   396             aOperationObserver, 
       
   397             request.iRequestId );
       
   398         }
       
   399     return request.iRequestId;
       
   400 }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CFSMailBox::CopyMessagesL
       
   404 // -----------------------------------------------------------------------------
       
   405 EXPORT_C void CFSMailBox::CopyMessagesL( const RArray<TFSMailMsgId>& aMessageIds, 
       
   406                                          RArray<TFSMailMsgId>& aNewMessages, 
       
   407                                          const TFSMailMsgId aSourceFolderId, 
       
   408                                          const TFSMailMsgId aDestinationFolderId )
       
   409 {
       
   410     FUNC_LOG;
       
   411     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   412         {
       
   413         plugin->CopyMessagesL(GetId(), aMessageIds, aNewMessages,
       
   414                                 aSourceFolderId, aDestinationFolderId );    
       
   415         }
       
   416 }
       
   417 
       
   418 // -----------------------------------------------------------------------------
       
   419 // CFSMailBox::SearchL
       
   420 // -----------------------------------------------------------------------------
       
   421 EXPORT_C void CFSMailBox::SearchL( const RPointerArray<TDesC>& /*aSearchStrings*/,
       
   422                                    const TFSMailSortCriteria&  /*aSortCriteria*/,
       
   423                                    MFSMailBoxSearchObserver&   /*aSearchObserver*/,
       
   424                                    const RArray<TFSMailMsgId>  /*aFolderIds */ )
       
   425     {
       
   426     FUNC_LOG;
       
   427 
       
   428     }
       
   429     
       
   430 // -----------------------------------------------------------------------------
       
   431 // CFSMailBox::SearchL
       
   432 // -----------------------------------------------------------------------------
       
   433 EXPORT_C void CFSMailBox::SearchL( const RPointerArray<TDesC>& aSearchStrings,
       
   434                                    const TFSMailSortCriteria& aSortCriteria,
       
   435                                    MFSMailBoxSearchObserver& aSearchObserver )
       
   436     {
       
   437     FUNC_LOG;
       
   438     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   439         {
       
   440 
       
   441         // get mailbox folder list
       
   442         iFolders.ResetAndDestroy();
       
   443         plugin->ListFoldersL(GetId(),iFolders);
       
   444 
       
   445         TFSMailMsgId draftsFolderId = GetStandardFolderId( EFSDraftsFolder );
       
   446         TFSMailMsgId outboxId = GetStandardFolderId( EFSOutbox );
       
   447         
       
   448         // remove outbox, drafts folder from folder list
       
   449         RArray<TFSMailMsgId> folderIds;
       
   450         CleanupClosePushL( folderIds );  
       
   451         for(TInt i=0;i<iFolders.Count();i++)
       
   452         {
       
   453             TFSMailMsgId id = iFolders[i]->GetFolderId();
       
   454             if( id != draftsFolderId && id != outboxId )
       
   455             {
       
   456                 folderIds.Append(id);
       
   457             }
       
   458         }
       
   459         
       
   460         // start search
       
   461         plugin->SearchL( GetId(), folderIds, aSearchStrings, aSortCriteria, aSearchObserver );
       
   462         CleanupStack::PopAndDestroy( &folderIds );
       
   463         }
       
   464     }
       
   465 
       
   466 // -----------------------------------------------------------------------------
       
   467 // CFSMailBox::CancelSearch
       
   468 // -----------------------------------------------------------------------------
       
   469 EXPORT_C void CFSMailBox::CancelSearch()
       
   470     {
       
   471     FUNC_LOG;
       
   472     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   473         {
       
   474         plugin->CancelSearch( GetId() );
       
   475         }
       
   476     }
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CFSMailBox::ClearSearchResultCache
       
   480 // -----------------------------------------------------------------------------
       
   481 EXPORT_C void CFSMailBox::ClearSearchResultCache()
       
   482     {
       
   483     FUNC_LOG;
       
   484     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   485         {
       
   486         plugin->ClearSearchResultCache( GetId() );
       
   487         }
       
   488     }
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CFSMailBox::ListMrusL
       
   492 // -----------------------------------------------------------------------------
       
   493 EXPORT_C MDesCArray* CFSMailBox::ListMrusL() const
       
   494     {
       
   495     FUNC_LOG;
       
   496     MDesCArray* mruList(0);
       
   497     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId() ) )
       
   498         {
       
   499         mruList = plugin->GetMrusL( GetId() );
       
   500         }
       
   501     return mruList;
       
   502     }
       
   503 
       
   504 
       
   505 // -----------------------------------------------------------------------------
       
   506 // CFSMailBox::CurrentSyncState
       
   507 // -----------------------------------------------------------------------------
       
   508 EXPORT_C TSSMailSyncState CFSMailBox::CurrentSyncState() const
       
   509     {
       
   510     FUNC_LOG;
       
   511     TSSMailSyncState syncState(Idle);
       
   512     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   513         {
       
   514         syncState = plugin->CurrentSyncState( GetId() );
       
   515         }
       
   516     return syncState;
       
   517     }
       
   518 
       
   519 // -----------------------------------------------------------------------------
       
   520 // CFSMailBox::HasCapability
       
   521 // -----------------------------------------------------------------------------
       
   522 EXPORT_C TBool CFSMailBox::HasCapability( const TFSMailBoxCapabilities aCapability ) const
       
   523 {
       
   524     FUNC_LOG;
       
   525     TBool capability = EFalse;
       
   526     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   527         {
       
   528         TRAPD( err,capability = plugin->MailboxHasCapabilityL( aCapability,GetId() )) ;
       
   529         if ( err != KErrNone )
       
   530             {
       
   531             capability = EFalse;
       
   532             }
       
   533         }
       
   534     return capability;
       
   535 }
       
   536 
       
   537 // -----------------------------------------------------------------------------
       
   538 // CFSMailBox::GetMailBoxStatus
       
   539 // -----------------------------------------------------------------------------
       
   540 EXPORT_C TFSMailBoxStatus CFSMailBox::GetMailBoxStatus()
       
   541 {
       
   542     FUNC_LOG;
       
   543     TFSMailBoxStatus status(EFSMailBoxOffline);
       
   544     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId() ) )
       
   545     {
       
   546         status = plugin->GetMailBoxStatus( GetId() );
       
   547     }
       
   548     return status;
       
   549 }
       
   550 
       
   551 // -----------------------------------------------------------------------------
       
   552 // CFSMailBox::SetCredentialsL
       
   553 // -----------------------------------------------------------------------------
       
   554 EXPORT_C void CFSMailBox::SetCredentialsL( const TDesC& aUsername, const TDesC& aPassword )
       
   555     {
       
   556     FUNC_LOG;
       
   557     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId() ) )
       
   558         {
       
   559         plugin->SetCredentialsL( GetId(), aUsername, aPassword );
       
   560         }
       
   561     }
       
   562 
       
   563 // -----------------------------------------------------------------------------
       
   564 // CFSMailBox::RemoveDownLoadedAttachmentsL
       
   565 // -----------------------------------------------------------------------------
       
   566 EXPORT_C void CFSMailBox::RemoveDownLoadedAttachmentsL()
       
   567     {
       
   568     FUNC_LOG;
       
   569     
       
   570     CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() );
       
   571     if ( plugin )
       
   572         {
       
   573         // get inbox folder from plugin
       
   574         TFSMailMsgId folderId = GetStandardFolderId( EFSInbox );
       
   575         CFSMailFolder* folder = plugin->GetFolderByUidL( GetId(), folderId );
       
   576         if ( folder )
       
   577             {
       
   578             folder->RemoveDownLoadedAttachmentsL();
       
   579             delete folder;
       
   580             }
       
   581         }
       
   582     }
       
   583 
       
   584 // -----------------------------------------------------------------------------
       
   585 // CFSMailBox::GetConnectionId
       
   586 // -----------------------------------------------------------------------------
       
   587 EXPORT_C TInt CFSMailBox::GetConnectionId( TUint32& aConnectionId )
       
   588     {
       
   589     FUNC_LOG;
       
   590     TInt rcode = KErrNotSupported;
       
   591     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   592         {
       
   593         rcode = plugin->GetConnectionId( GetId(), aConnectionId );
       
   594         }
       
   595     return rcode;
       
   596     }
       
   597 
       
   598 // -----------------------------------------------------------------------------
       
   599 // CFSMailBox::IsConnectionAllowedWhenRoaming
       
   600 // -----------------------------------------------------------------------------
       
   601 EXPORT_C TInt CFSMailBox::IsConnectionAllowedWhenRoaming( TBool& aConnectionAllowed )
       
   602     {
       
   603     FUNC_LOG;
       
   604     TInt rcode = KErrNotSupported;
       
   605     if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   606         {
       
   607         rcode = plugin->IsConnectionAllowedWhenRoaming( GetId(), aConnectionAllowed );
       
   608         }
       
   609     return rcode;
       
   610     }
       
   611 
       
   612 // -----------------------------------------------------------------------------
       
   613 // CFSMailBox::CreateMessageFromFileL
       
   614 // -----------------------------------------------------------------------------
       
   615 EXPORT_C CFSMailMessage* CFSMailBox::CreateMessageFromFileL( const RFile& aFile )
       
   616 {
       
   617     FUNC_LOG;
       
   618     CFSMailMessage* message = NULL;
       
   619     if(CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId()))
       
   620     {
       
   621         message = plugin->CreateMessageFromFileL( GetId(), aFile );
       
   622     }
       
   623     return message;
       
   624 }
       
   625 
       
   626 // -----------------------------------------------------------------------------
       
   627 // CFSMailBox::UpdateMrusL
       
   628 // -----------------------------------------------------------------------------
       
   629 void CFSMailBox::UpdateMrusL(
       
   630     const RPointerArray<CFSMailAddress>& aRecipients,
       
   631     const RPointerArray<CFSMailAddress>& aCCRecipients,
       
   632     const RPointerArray<CFSMailAddress>& aBCCRecipients ) const
       
   633     {
       
   634     FUNC_LOG;
       
   635     
       
   636     // First lets make a copy of the current mru list
       
   637     // whose content we can later alter as we wish
       
   638     MDesCArray* currentMruList( NULL );
       
   639 
       
   640     CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid(GetId());
       
   641     if ( !plugin )
       
   642         {
       
   643         User::Leave( KErrGeneral );
       
   644         }
       
   645     currentMruList = plugin->GetMrusL( GetId() );
       
   646     if ( !currentMruList )
       
   647         {
       
   648         // This should not happen because previous function
       
   649         // should leave in error cases and if there are no
       
   650         // entries then the pointer should still be pointing
       
   651         // to valid array.
       
   652         User::Leave( KErrGeneral );
       
   653         }
       
   654     
       
   655     CDesCArraySeg* newMruList( NULL );
       
   656     TRAPD( error, newMruList = CopyArrayL( *currentMruList ) );
       
   657         
       
   658     delete currentMruList;
       
   659        
       
   660     if ( error != KErrNone )
       
   661         {
       
   662         User::Leave( error );
       
   663         }
       
   664         
       
   665     CleanupStack::PushL( newMruList );
       
   666         
       
   667     // Now check that all given recipients are found from the
       
   668     // mru list.
       
   669 
       
   670     // Notice that the order here has a meaning. For example
       
   671     // if the latest used address is appended to the end, then
       
   672     // the aRecipients' addresses are found from the end because
       
   673     // they are updated after cc and bcc recipients.
       
   674     UpdateMruListL( *newMruList, aBCCRecipients );
       
   675     UpdateMruListL( *newMruList, aCCRecipients );
       
   676     UpdateMruListL( *newMruList, aRecipients );
       
   677     
       
   678     // Finally update the new mru list to the plugin
       
   679     plugin->SetMrusL( GetId(), newMruList );
       
   680     
       
   681     CleanupStack::PopAndDestroy( newMruList );
       
   682     }
       
   683 
       
   684 // -----------------------------------------------------------------------------
       
   685 // CFSMailBox::CopyArrayL
       
   686 // -----------------------------------------------------------------------------    
       
   687 CDesCArraySeg* CFSMailBox::CopyArrayL( MDesCArray& aArrayToBeCopied ) const
       
   688     {
       
   689     FUNC_LOG;
       
   690     CDesCArraySeg* newArray = new (ELeave) CDesCArraySeg( 10 );
       
   691     CleanupStack::PushL( newArray );
       
   692     
       
   693     TInt itemCount( aArrayToBeCopied.MdcaCount() );
       
   694     TInt index( 0 );
       
   695     while ( index < itemCount )
       
   696         {        
       
   697         newArray->AppendL( aArrayToBeCopied.MdcaPoint( index ) );
       
   698         ++index;
       
   699         }
       
   700     
       
   701     CleanupStack::Pop( newArray );
       
   702     return newArray;
       
   703     }
       
   704 
       
   705 // -----------------------------------------------------------------------------
       
   706 // CFSMailBox::UpdateMruListL
       
   707 // -----------------------------------------------------------------------------
       
   708 void CFSMailBox::UpdateMruListL(
       
   709     CDesCArraySeg& aMruList,
       
   710     const RPointerArray<CFSMailAddress>& aNewRecentlyUsedOnes ) const
       
   711     {
       
   712     FUNC_LOG;
       
   713     TUint newCount( aNewRecentlyUsedOnes.Count() );
       
   714     TUint newIndexer( 0 );
       
   715     
       
   716     while ( newIndexer < newCount )
       
   717         {
       
   718         if ( aNewRecentlyUsedOnes[newIndexer] )
       
   719             {
       
   720             // The address is used as a search string because every
       
   721             // address does not have a display name
       
   722             TDesC& searchedAddress(
       
   723                 aNewRecentlyUsedOnes[newIndexer]->GetEmailAddress() );
       
   724             TInt position( -1 );
       
   725             
       
   726             TInt found(
       
   727                 FindAddressFromMruList( aMruList, searchedAddress, position ) );
       
   728             
       
   729             if ( found != 0 )
       
   730                 {
       
   731                 AddAndRemoveExcessMruL( aMruList,
       
   732                                         *aNewRecentlyUsedOnes[newIndexer] );
       
   733                 }
       
   734             else
       
   735                 {
       
   736                 SetAsMostRecentMruL( aMruList,
       
   737                                      position,
       
   738                                      *aNewRecentlyUsedOnes[newIndexer] );
       
   739                 }
       
   740             }        
       
   741         
       
   742         ++newIndexer;
       
   743         }
       
   744     }
       
   745 
       
   746 // -----------------------------------------------------------------------------
       
   747 // CFSMailBox::FindAddressFromMruList
       
   748 // -----------------------------------------------------------------------------
       
   749 TInt CFSMailBox::FindAddressFromMruList( CDesCArraySeg& aMruList,
       
   750                                          TDesC& searchedAddress,
       
   751                                          TInt& aPos ) const
       
   752     {
       
   753     FUNC_LOG;
       
   754     // CDesCArray::Find() is not used here because there is
       
   755     // possibility that we have to go through the whole array
       
   756     // and return the index for one specific match. Find() returns
       
   757     // only the index of the first match and searching for the rest
       
   758     // using Find() would cause undesired complexity.
       
   759     
       
   760     
       
   761     const TInt KMruListCount( aMruList.Count() );
       
   762     // Start indexing from 1 because the first
       
   763     // address is on that index if it exists.
       
   764     TInt mruListIndexer( 1 );
       
   765     while( mruListIndexer < KMruListCount )
       
   766         {
       
   767         TPtrC address( aMruList[mruListIndexer] );
       
   768         if ( address == searchedAddress )
       
   769             {
       
   770             aPos = mruListIndexer;
       
   771             return 0;
       
   772             }
       
   773         
       
   774         // We are only interested of the addresses so let's
       
   775         // check only every other descriptor.
       
   776         // (the addresses)
       
   777         mruListIndexer = mruListIndexer + 2;
       
   778         }
       
   779     
       
   780     aPos = aMruList.Count();
       
   781     return 1;
       
   782     }
       
   783 
       
   784 
       
   785 
       
   786 
       
   787 // -----------------------------------------------------------------------------
       
   788 // CFSMailBox::AddAndRemoveExcessMruL
       
   789 // -----------------------------------------------------------------------------
       
   790 void CFSMailBox::AddAndRemoveExcessMruL( CDesCArraySeg& aMruList,
       
   791                                          CFSMailAddress& aToBeAdded ) const
       
   792     {
       
   793     FUNC_LOG;
       
   794     if ( aMruList.Count() == KMaxMruEntries )
       
   795         {
       
   796         // Remove the oldest entry pair from the beginning
       
   797         aMruList.Delete( 0, 2 );
       
   798         }
       
   799     // Latest address is always found from the end.
       
   800     AppendMruItemL( aMruList, aToBeAdded );
       
   801     }
       
   802 
       
   803 // -----------------------------------------------------------------------------
       
   804 // CFSMailBox::SetAsMostRecentMruL
       
   805 // -----------------------------------------------------------------------------
       
   806 void CFSMailBox::SetAsMostRecentMruL( CDesCArraySeg& aMruList,
       
   807                                       TInt aPosition,
       
   808                                       CFSMailAddress& aMostRecent ) const
       
   809     {
       
   810     FUNC_LOG;
       
   811     // Position of the address is given so the possible display name is
       
   812     // in the previous slot. Delete both.
       
   813     aMruList.Delete( aPosition - 1, 2 );
       
   814     // Latest address is always found from the end.
       
   815     AppendMruItemL( aMruList, aMostRecent );
       
   816     }
       
   817 
       
   818 // -----------------------------------------------------------------------------
       
   819 // CFSMailBox::AppendMruItemL
       
   820 // -----------------------------------------------------------------------------
       
   821 void CFSMailBox::AppendMruItemL( CDesCArraySeg& aMruList,
       
   822                                  CFSMailAddress& aToBeAppended ) const
       
   823     {
       
   824     FUNC_LOG;
       
   825     // In the array, display name is always the first and then comes
       
   826     // the actual address.
       
   827     
       
   828     // <cmail> avoid setting email address as display name so it won't 
       
   829     // be displayed twice in the list
       
   830     TDesC* displayName  = &aToBeAppended.GetDisplayName();    
       
   831     TDesC* emailAddress = &aToBeAppended.GetEmailAddress();
       
   832 
       
   833     if( displayName->Length() > 0 && displayName->Compare(*emailAddress) == 0 )
       
   834     {
       
   835         aMruList.AppendL( KNullDesC );
       
   836     }
       
   837     else
       
   838     {
       
   839         aMruList.AppendL( *displayName );                
       
   840     }
       
   841     
       
   842     aMruList.AppendL( *emailAddress );    
       
   843     }
       
   844 
       
   845 // -----------------------------------------------------------------------------
       
   846 // CFSMailBox::ReleaseExtension
       
   847 // -----------------------------------------------------------------------------
       
   848 EXPORT_C void CFSMailBox::ReleaseExtension( CEmailExtension* aExtension )
       
   849     {
       
   850     FUNC_LOG;
       
   851     // MR UI needs KMailboxExtMrCalInfo to not be released until CFSMailBox 
       
   852     // exists. It is released in desctrucor
       
   853     if( aExtension->Uid() != KMailboxExtMrCalInfo )
       
   854         {
       
   855         if( aExtension->Uid() == KEmailSettingExtensionUid ||
       
   856 		    aExtension->Uid() == KEmailConnectionStatusQueryExtensionUid )
       
   857             {
       
   858             // check that plugin supports requested extension.
       
   859             if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   860                 {
       
   861                 // remove extension from plugin
       
   862                  plugin->ReleaseExtension(aExtension);            
       
   863                 }
       
   864             }
       
   865         else
       
   866             {
       
   867             // no specialized behaviour, call base class
       
   868             CExtendableEmail::ReleaseExtension( aExtension );
       
   869             }
       
   870         }
       
   871     else
       
   872         {
       
   873         if( !aExtension->DecRef() )
       
   874             {
       
   875             aExtension->IncRef();
       
   876             }
       
   877         }
       
   878     }
       
   879     
       
   880 // -----------------------------------------------------------------------------
       
   881 // CFSMailBox::ExtensionL
       
   882 // -----------------------------------------------------------------------------
       
   883 EXPORT_C CEmailExtension* CFSMailBox::ExtensionL( const TUid& aInterfaceUid )
       
   884     {
       
   885     FUNC_LOG;
       
   886     CEmailExtension* extension = NULL;
       
   887     if ( aInterfaceUid == KMailboxExtMrCalInfo )
       
   888         {
       
   889         extension = CExtendableEmail::ExtensionL( aInterfaceUid );
       
   890         if ( extension == NULL )
       
   891             {
       
   892             extension = new ( ELeave ) CMRCalendarInfoImpl();
       
   893             CleanupStack::PushL( extension );
       
   894             iExtensions.AddL( extension );
       
   895             CleanupStack::Pop(); // calInfo
       
   896             }
       
   897         }    
       
   898     else if ( aInterfaceUid == KEmailMailboxStateExtensionUid ||
       
   899               aInterfaceUid == KEmailSettingExtensionUid || 
       
   900 			  aInterfaceUid == KEmailConnectionStatusQueryExtensionUid )
       
   901         {
       
   902         if ( !extension )
       
   903             {
       
   904             // check that plugin supports requested extension.
       
   905             if ( CFSMailPlugin* plugin = iRequestHandler->GetPluginByUid( GetId() ) )
       
   906                 {
       
   907                 // request extension from plugin, leaves if not supported
       
   908                 extension = plugin->ExtensionL( aInterfaceUid );            
       
   909                 }
       
   910     
       
   911             }
       
   912         }
       
   913     else
       
   914         {
       
   915         User::Leave( KErrNotSupported );
       
   916         }
       
   917     return extension;
       
   918     }
       
   919