emailservices/psmruadapter/src/CPsMruAdapter.cpp
branchRCL_3
changeset 25 3533d4323edc
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 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:  Data Store Adapter for MRU list
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <CPsData.h>
       
    22 #include <CPcsDefs.h>
       
    23 #include <coemain.h>
       
    24 #include <data_caging_path_literals.hrh>
       
    25 #include <VPbkEng.rsg>
       
    26 //<cmail>
       
    27 #include "cfsmailclient.h"
       
    28 #include "CPsMruAdapter.h"
       
    29 //</cmail>
       
    30 #include <mmf/common/mmfcontrollerpluginresolver.h> // CleanupResetAndDestroy
       
    31 
       
    32 
       
    33 #define PRINT(x)
       
    34 
       
    35 #include "emailtrace.h"
       
    36 
       
    37 // ============================== MEMBER FUNCTIONS ============================
       
    38 
       
    39 // Identifier for mailbox Uri
       
    40 _LIT(KDefaultMailBoxURI, "mailbox://");
       
    41 // Separator for plugin/mailbox identifiers inside the Uri string
       
    42 const TText8 KDefaultMailBoxURISeparator = '/';
       
    43 // Maximum length of uri
       
    44 const TInt KMaximumMailboxUriLength = 64;
       
    45 
       
    46 // ----------------------------------------------------------------------------
       
    47 // CPsMruAdapter::NewL
       
    48 // Two Phase Construction
       
    49 // ----------------------------------------------------------------------------
       
    50 CPsMruAdapter* CPsMruAdapter::NewL( TAny* aPsDataPluginParameters )
       
    51     {
       
    52     FUNC_LOG;
       
    53     PRINT ( _L( "Enter CPsMruAdapter::NewL" ) );
       
    54     
       
    55     // Get the PsData plugin parametrs
       
    56     TPsDataPluginParams* params =
       
    57             reinterpret_cast<TPsDataPluginParams*>(aPsDataPluginParameters );
       
    58     CPsMruAdapter* self = new ( ELeave ) CPsMruAdapter();
       
    59     CleanupStack::PushL(self);
       
    60     self->ConstructL( params->iDataStoreObserver, params->iStoreListObserver );
       
    61     CleanupStack::Pop( self );
       
    62     
       
    63     PRINT ( _L( "End CPsMruAdapter::NewL" ) );
       
    64     
       
    65     return self;
       
    66     }
       
    67 
       
    68 // ----------------------------------------------------------------------------
       
    69 // CPsMruAdapter::CPsMruAdapter
       
    70 // Two Phase Construction
       
    71 // ----------------------------------------------------------------------------
       
    72 CPsMruAdapter::CPsMruAdapter()
       
    73     {
       
    74     FUNC_LOG;
       
    75     PRINT ( _L( "Enter CPsMruAdapter::CPsMruAdapter" ) );
       
    76     iDelayMailboxCreationPtr = NULL;
       
    77     PRINT ( _L( "End CPsMruAdapter::CPsMruAdapter" ) );
       
    78     }
       
    79 
       
    80 // ----------------------------------------------------------------------------
       
    81 // CPsMruAdapter::ConstructL
       
    82 // Two Phase Construction
       
    83 // ----------------------------------------------------------------------------
       
    84 void CPsMruAdapter::ConstructL( MDataStoreObserver* aObserverForDataStore,
       
    85         MStoreListObserver* aStoreListObserver )
       
    86     {
       
    87     FUNC_LOG;   
       
    88     PRINT ( _L( "Enter CPsMruAdapter::ConstructL" ) );
       
    89 
       
    90     // Mail client for FS Email framework
       
    91     iMailClient = CFSMailClient::NewL();
       
    92 
       
    93     // Register as observer for mail client, we'll need this to observe MRU list changes
       
    94     // When i.e mail is sent
       
    95     iMailClient->AddObserverL( *this );
       
    96 
       
    97     // This updates the list of all the supported datastores
       
    98     UpdateSupportedDataStoresList();
       
    99 
       
   100     // Store these for later use
       
   101     iDataStoreObserver = aObserverForDataStore;
       
   102     iStoreListObserver = aStoreListObserver;
       
   103 
       
   104     PRINT ( _L( "End CPsMruAdapter::ConstructL" ) );
       
   105     }
       
   106 
       
   107 // ----------------------------------------------------------------------------
       
   108 // CPsMruAdapter::~CPsMruAdapter
       
   109 // Destructor
       
   110 // ----------------------------------------------------------------------------
       
   111 CPsMruAdapter::~CPsMruAdapter()
       
   112     {
       
   113     FUNC_LOG;   
       
   114     PRINT ( _L( "Enter CPsMruAdapter::~CPsMruAdapter" ) );
       
   115 
       
   116     iSupportedUris.ResetAndDestroy();
       
   117 
       
   118     // Remove observer, we don't need it anymore because we're shutting down
       
   119     RemoveAllMailboxObservers();
       
   120     iObservedMailboxes.Close();
       
   121 
       
   122     if ( iMailClient )
       
   123         {
       
   124         iMailClient->RemoveObserver( *this );
       
   125         iMailClient->Close();
       
   126         }
       
   127     if(iDelayMailboxCreationPtr)
       
   128         {
       
   129         delete iDelayMailboxCreationPtr;
       
   130         iDelayMailboxCreationPtr = NULL;
       
   131         }
       
   132     
       
   133     iDelayedCreatedMailboxes.Reset();
       
   134     iDelayedCreatedMailboxes.Close();
       
   135     
       
   136     PRINT ( _L( "End CPsMruAdapter::~CPsMruAdapter" ) );
       
   137     }
       
   138 
       
   139 // ----------------------------------------------------------------------------
       
   140 // CPsMruAdapter::RequestForDataL
       
   141 // 
       
   142 // ----------------------------------------------------------------------------
       
   143 void CPsMruAdapter::RequestForDataL( TDesC& aDataStoreURI )
       
   144     {
       
   145     FUNC_LOG;   
       
   146     PRINT ( _L( "Enter CPsMruAdapter::RequestForDataL" ) );
       
   147 
       
   148     // Add data to datastore, fill only requested one
       
   149     FillDataStoreL( aDataStoreURI );
       
   150 
       
   151     PRINT ( _L( "Enter CPsMruAdapter::RequestForDataL" ) );
       
   152     }
       
   153 
       
   154 // ----------------------------------------------------------------------------
       
   155 // CPsMruAdapter::GetSupportedDataStoresL
       
   156 // 
       
   157 // ----------------------------------------------------------------------------
       
   158 
       
   159 void CPsMruAdapter::GetSupportedDataStoresL(
       
   160         RPointerArray<TDesC> &aDataStoresURIs )
       
   161     {
       
   162     FUNC_LOG;   
       
   163     for ( TInt i = 0; i < iSupportedUris.Count(); i++ )
       
   164         {
       
   165         aDataStoresURIs.Append( iSupportedUris[i] );
       
   166         }
       
   167     }
       
   168 
       
   169 // ----------------------------------------------------------------------------
       
   170 // CPsMruAdapter::RequestForDataExtensionL
       
   171 // 
       
   172 // ----------------------------------------------------------------------------
       
   173 TAny* CPsMruAdapter::RequestForDataExtensionL(TInt /*aItemId*/)
       
   174     {
       
   175     FUNC_LOG;   
       
   176     // No extention required for this since we have and interger as itemId
       
   177     // Simply return NULL
       
   178     return NULL;
       
   179     }
       
   180 
       
   181 // ----------------------------------------------------------------------------
       
   182 // CPsMruAdapter::UpdateSupportedDataStoresList
       
   183 // 
       
   184 // ----------------------------------------------------------------------------
       
   185 void CPsMruAdapter::UpdateSupportedDataStoresList()
       
   186     {
       
   187     FUNC_LOG;   
       
   188     // Mailboxes will be fetched to this array
       
   189     RPointerArray<CFSMailBox> mailBoxes;
       
   190 
       
   191     // List all mailboxes
       
   192     TFSMailMsgId plugin;
       
   193     iMailClient->ListMailBoxes( plugin, mailBoxes );
       
   194 
       
   195     iSupportedUris.ResetAndDestroy();
       
   196 
       
   197     // Add all mailboxes as data stores
       
   198     for ( TInt i = 0; i < mailBoxes.Count(); i++ )
       
   199         {
       
   200         // Get id of mailbox
       
   201         TFSMailMsgId id = mailBoxes[i]->GetId();
       
   202 
       
   203         // Convert it to string, this will be the unique identifier for this mailbox
       
   204         HBufC* identifier = HBufC::New( KMaximumMailboxUriLength );
       
   205         if ( identifier && GetUriFromMailboxIdentifier( id, *identifier ) )
       
   206             {
       
   207             // Add to supported Uris list
       
   208             if ( iSupportedUris.Append( identifier ) == KErrNone )
       
   209                 {
       
   210                 // Ownership of the string is successfully moved to the array
       
   211                 identifier = NULL;
       
   212                 }
       
   213             }
       
   214         delete identifier;
       
   215         }
       
   216 
       
   217     // Release allocated memory
       
   218     mailBoxes.ResetAndDestroy();
       
   219     }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // CPsMruAdapter::FillDataStoreL
       
   223 // ----------------------------------------------------------------------------
       
   224 TBool CPsMruAdapter::FillDataStoreL( TDesC& aDataStoreURI )
       
   225     {
       
   226     FUNC_LOG;   
       
   227     TBool result = EFalse;
       
   228 
       
   229     TFSMailMsgId dataStoreId;
       
   230 
       
   231     if ( GetMailboxIdentifierFromUri( aDataStoreURI, dataStoreId ) )
       
   232         {
       
   233         result = FillDataStoreL( dataStoreId, aDataStoreURI );
       
   234         }
       
   235 
       
   236     return result;
       
   237     }
       
   238 
       
   239 TBool CPsMruAdapter::FillDataStoreL( TFSMailMsgId& aId )
       
   240     {
       
   241     FUNC_LOG;   
       
   242     TBool result = EFalse;
       
   243 
       
   244     // Create Uri for this mailbox
       
   245     HBufC* identifier = HBufC::NewLC( KMaximumMailboxUriLength );
       
   246     if ( GetUriFromMailboxIdentifier( aId, *identifier ) )
       
   247         {
       
   248         result = FillDataStoreL( aId, *identifier );
       
   249         }
       
   250     CleanupStack::PopAndDestroy( identifier );
       
   251     return result;
       
   252     }
       
   253 
       
   254 void CPsMruAdapter::AddMruEmailsL( MDesCArray* aMruList, TDesC& aDataStoreURI )
       
   255     {
       
   256     FUNC_LOG;   
       
   257     TInt entryIndex = 0;
       
   258     // Add all data to data store, the format is:
       
   259     // index0: displayname
       
   260     // index1: email
       
   261     // index2: next displayname
       
   262     // index3: next email
       
   263     // etc..
       
   264     for ( int mruIndex = 0; mruIndex < aMruList->MdcaCount(); mruIndex += 2 )
       
   265         {
       
   266         TPtrC displayName = aMruList->MdcaPoint( mruIndex );
       
   267         TPtrC emailAddress = aMruList->MdcaPoint( mruIndex + 1 );
       
   268 
       
   269         CPsData* mruData = CPsData::NewL();
       
   270         CleanupStack::PushL(mruData);
       
   271         mruData->SetId( entryIndex++ );
       
   272 
       
   273         // Set the data
       
   274         mruData->SetDataL( 0, displayName );
       
   275         mruData->SetDataL( 1, KNullDesC );
       
   276         mruData->SetDataL( 2, emailAddress );
       
   277 
       
   278         iDataStoreObserver->AddData( aDataStoreURI, mruData );
       
   279         CleanupStack::Pop(mruData); // transferred ownership
       
   280         }
       
   281     }
       
   282 
       
   283 TBool CPsMruAdapter::FillDataStoreL( TFSMailMsgId& aId, TDesC& aDataStoreURI )
       
   284     {
       
   285     FUNC_LOG;   
       
   286     TBool result = EFalse;
       
   287 // code was simplified not to trace all mailboxes
       
   288 // function has trap in  Event() -case> TFSEventNewMailbox and in DeleayedMailboxCreationEventL()
       
   289 // should not leave when new mailbox only when new mail address
       
   290 // TODO SK how to avoid extra calls?    
       
   291     CFSMailBox *mailBox = iMailClient->GetMailBoxByUidLC(aId);
       
   292     if( mailBox )
       
   293         {
       
   294         AddMailboxObserverL( aId );
       
   295         
       
   296         // Get MRU list for this mailbox
       
   297         MDesCArray* mruList = mailBox->ListMrusL(); // TODO SK this value can be cached?
       
   298         //TODO add to cleanup stack?
       
   299         
       
   300         // update the caching status as InProgress
       
   301         iDataStoreObserver->UpdateCachingStatus( aDataStoreURI,
       
   302                 ECachingInProgress );
       
   303 
       
   304         // Update datastore contents, first reset
       
   305         iDataStoreObserver->RemoveAll( aDataStoreURI );
       
   306 
       
   307         TInt trap_err=KErrNone; // for trap macro
       
   308         if ( mruList )
       
   309             {
       
   310             // trap the error to enable returning status back
       
   311             TRAP(trap_err, AddMruEmailsL( mruList, aDataStoreURI ) );  
       
   312             delete mruList;
       
   313             }
       
   314         // update the caching status as Complete
       
   315         iDataStoreObserver->UpdateCachingStatus( aDataStoreURI,
       
   316                 ECachingComplete );
       
   317         if ( trap_err != KErrNone ) // check for error leave code 
       
   318             {  
       
   319             User::Leave(trap_err);       
       
   320             }
       
   321         result = ETrue;
       
   322         }   // if (mailBox)
       
   323     CleanupStack::PopAndDestroy( mailBox );
       
   324     return result;
       
   325     }
       
   326 
       
   327 // ----------------------------------------------------------------------------
       
   328 // CPsContactDataAdapter::IsDataStoresSupportedL
       
   329 // 
       
   330 // ----------------------------------------------------------------------------
       
   331 TBool CPsMruAdapter::IsDataStoresSupportedL( TDesC& aDataStoreURI )
       
   332     {
       
   333     FUNC_LOG;   
       
   334     for ( TInt i = 0; i < iSupportedUris.Count(); i++ )
       
   335         {
       
   336         if ( iSupportedUris[i]->Compare( aDataStoreURI ) == 0 )
       
   337             return ETrue;
       
   338         }
       
   339     return EFalse;
       
   340     }
       
   341 
       
   342 // ----------------------------------------------------------------------------
       
   343 // CPsContactDataAdapter::GetSupportedDataFieldsL
       
   344 // 
       
   345 // ----------------------------------------------------------------------------
       
   346 void CPsMruAdapter::GetSupportedDataFieldsL( RArray<TInt>& aDataFields )
       
   347     {
       
   348     FUNC_LOG;   
       
   349     aDataFields.Append( R_VPBK_FIELD_TYPE_FIRSTNAME );
       
   350     aDataFields.Append( R_VPBK_FIELD_TYPE_LASTNAME );
       
   351     aDataFields.Append( R_VPBK_FIELD_TYPE_EMAILGEN );
       
   352     }
       
   353 
       
   354 TBool CPsMruAdapter::GetMailboxIdentifierFromUri( TDesC& aUri, TFSMailMsgId& aId )
       
   355     {
       
   356     FUNC_LOG;   
       
   357     // Find first separator in reverse order
       
   358     TInt lastSeparator = aUri.LocateReverseF( KDefaultMailBoxURISeparator );
       
   359     if ( lastSeparator == KErrNotFound )
       
   360         return EFalse;
       
   361 
       
   362     // This is where plugin id string starts
       
   363     TInt pluginIdStartPosition = KDefaultMailBoxURI().Length();
       
   364     TInt mailboxIdStartPosition = lastSeparator + 1;
       
   365 
       
   366     // It cannot be further than mailbox id
       
   367     if ( pluginIdStartPosition >= mailboxIdStartPosition )
       
   368         return EFalse;
       
   369 
       
   370     // Use TLex to convert string to integer
       
   371     TLex pluginIdConverter( aUri.Mid( pluginIdStartPosition,
       
   372             mailboxIdStartPosition - pluginIdStartPosition - 1 ) );
       
   373 
       
   374     // Use TLex to convert string to integer
       
   375     TLex mailboxIdConverter( aUri.Mid( mailboxIdStartPosition, aUri.Length()
       
   376             - mailboxIdStartPosition ) );
       
   377 
       
   378     TInt pluginId;
       
   379     TInt mailboxId;
       
   380 
       
   381     // Get plugin ID
       
   382     if ( pluginIdConverter.Val( pluginId ) != KErrNone)
       
   383         return EFalse;
       
   384 
       
   385     // Get mailbox ID
       
   386     if ( mailboxIdConverter.Val( mailboxId ) != KErrNone)
       
   387         return EFalse;
       
   388 
       
   389     // Store and we're ready
       
   390     aId.SetPluginId( TUid::Uid( pluginId ) );
       
   391     aId.SetId( mailboxId );
       
   392 
       
   393     return ETrue;
       
   394     }
       
   395 
       
   396 TBool CPsMruAdapter::GetUriFromMailboxIdentifier( TFSMailMsgId& aId, HBufC& aUri )
       
   397     {
       
   398     FUNC_LOG;   
       
   399     // Add the uri identifier
       
   400     aUri.Des().Copy( KDefaultMailBoxURI );
       
   401     // Add plugin ID
       
   402     aUri.Des().AppendNum( aId.PluginId().iUid );
       
   403     // Add separator
       
   404     aUri.Des().Append( KDefaultMailBoxURISeparator );
       
   405     // Add mailbox id
       
   406     aUri.Des().AppendNum( aId.Id() );
       
   407     
       
   408     return true;
       
   409     }
       
   410 
       
   411 void CPsMruAdapter::EventL( TFSMailEvent aEvent, TFSMailMsgId aMailbox,
       
   412         TAny* /*aParam1*/, TAny* aParam2, TAny* /*aParam3*/ )
       
   413     {
       
   414     FUNC_LOG;   
       
   415     switch ( aEvent )
       
   416         {
       
   417         case TFSEventMailMoved:
       
   418         case TFSEventMailCopied:        
       
   419         case TFSEventNewMail:
       
   420             {
       
   421             // Check the new parent folder id for this message
       
   422             // For all these events, param2 indicates the new parent folder
       
   423             TFSMailMsgId* parentFolderId =
       
   424                     static_cast< TFSMailMsgId* >( aParam2 );
       
   425             if ( parentFolderId )
       
   426                 {
       
   427                 TFSFolderType folderType( EFSInbox ); 
       
   428                 if ( (*parentFolderId) == iPreviousParentFolderId && aMailbox == iPreviousMailboxId )
       
   429                     {
       
   430                     // we assume that folder with some id does not change 
       
   431                     // its type during mail synchronization
       
   432                     folderType = iPreviousParentFolderType;
       
   433                     }
       
   434                 else
       
   435                     {
       
   436                     // Get the parent folder object
       
   437                     CFSMailFolder* parentFolder = iMailClient->GetFolderByUidL(
       
   438                             aMailbox, *parentFolderId );
       
   439                     if ( parentFolder )
       
   440                         {
       
   441                         iPreviousParentFolderId = (*parentFolderId);
       
   442                         iPreviousMailboxId = aMailbox;
       
   443                         folderType = parentFolder->GetFolderType();
       
   444                         iPreviousParentFolderType = folderType;
       
   445                         delete parentFolder;
       
   446                         parentFolder = NULL;
       
   447                         }
       
   448                     }
       
   449                 // If it's sent/outbox folder,
       
   450                 // we'll consider that as a new message being sent
       
   451                 // and therefore we'll update the MRU list here
       
   452                 if ( ( folderType == EFSSentFolder ) || 
       
   453                      ( folderType == EFSOutbox ) )
       
   454                     {
       
   455                     FillDataStoreL( aMailbox );
       
   456                     }
       
   457                 }
       
   458             }
       
   459             break;
       
   460             
       
   461         case TFSEventNewMailbox:
       
   462             {
       
   463             CFSMailBox *mailboxPtr(NULL);
       
   464             // if mailbox is not ready it may leave here
       
   465             TRAPD(trap_err, mailboxPtr = iMailClient->GetMailBoxByUidL( aMailbox ));
       
   466             if ( trap_err != KErrNone )
       
   467                 {
       
   468                 mailboxPtr = NULL;
       
   469                 }
       
   470             if( mailboxPtr ) 
       
   471                 {
       
   472                 delete mailboxPtr;
       
   473                 mailboxPtr = NULL;
       
   474                 }
       
   475             else
       
   476                 {
       
   477                 // mailbox still does not exist
       
   478                 DeleayMailboxCreationEventL( aMailbox ); // start timer to postpone creation    
       
   479                 break;
       
   480                 }
       
   481             
       
   482             HBufC* identifier = HBufC::NewLC( KMaximumMailboxUriLength ); // new string ident
       
   483             if ( GetUriFromMailboxIdentifier( aMailbox, *identifier ) )
       
   484                 {
       
   485                 // Add to supported Uri list
       
   486                 iSupportedUris.AppendL( identifier );
       
   487 
       
   488                 // Add new data store to cache
       
   489                 iStoreListObserver->AddDataStore( *identifier );
       
   490 
       
   491                 // Add all data to data store
       
   492                 // FillDataStoreL( *identifier );
       
   493                 // FillDataStoreL removed - called by AddDataStore through RequestForDataL callback
       
   494 
       
   495                 // In case there is a problem with transferring strings to  - should be obsolete 
       
   496                 AddMailboxObserverL( aMailbox ); // will be added by FillDataStoreL
       
   497 
       
   498                 CleanupStack::Pop( identifier );
       
   499                 }
       
   500             else
       
   501                 {
       
   502                 CleanupStack::PopAndDestroy( identifier );
       
   503                 }
       
   504             }
       
   505             break;
       
   506 
       
   507         case TFSEventMailboxDeleted:
       
   508             {
       
   509             HBufC* identifier = HBufC::NewL( KMaximumMailboxUriLength );
       
   510             if ( GetUriFromMailboxIdentifier( aMailbox, *identifier ) )
       
   511                 {
       
   512                 RemoveMailboxObserver( aMailbox );
       
   513 
       
   514                 // Remove data store from cache
       
   515                 iStoreListObserver->RemoveDataStore( *identifier );
       
   516 
       
   517                 // Remove from supported Uri list
       
   518                 for ( TInt i = 0; i < iSupportedUris.Count(); i++ )
       
   519                     {
       
   520                     if ( iSupportedUris[i]->Compare( *identifier ) == 0 )
       
   521                         {
       
   522                         delete iSupportedUris[i];
       
   523                         iSupportedUris.Remove( i );
       
   524                         break;
       
   525                         }
       
   526                     }
       
   527                 }
       
   528             delete identifier;
       
   529             
       
   530             iPreviousParentFolderId = TFSMailMsgId();
       
   531             iPreviousMailboxId = TFSMailMsgId();  
       
   532             break;
       
   533             }
       
   534         case TFSEventMailboxSettingsChanged: // TODO SK check
       
   535             {
       
   536             iPreviousParentFolderId = TFSMailMsgId();
       
   537             iPreviousMailboxId = TFSMailMsgId();  
       
   538             break;
       
   539             }
       
   540         }
       
   541     }
       
   542 TBool CPsMruAdapter::AddMailboxObserverL( TFSMailMsgId& aId )
       
   543     {
       
   544     FUNC_LOG;   
       
   545     for( TInt index = 0; index < iObservedMailboxes.Count(); index++ )
       
   546         {
       
   547         if( iObservedMailboxes[index] == aId )
       
   548             {
       
   549             // Already observing
       
   550             return EFalse;
       
   551             }
       
   552         }
       
   553     
       
   554     iMailClient->SubscribeMailboxEventsL( aId, *this );
       
   555     iObservedMailboxes.Append( aId );
       
   556     return ETrue;
       
   557     }
       
   558 
       
   559 TBool CPsMruAdapter::RemoveMailboxObserver( TFSMailMsgId& aId )
       
   560     {
       
   561     FUNC_LOG;   
       
   562     for( TInt index = 0; index < iObservedMailboxes.Count(); index++ )
       
   563         {
       
   564         if( iObservedMailboxes[index] == aId )
       
   565             {
       
   566             iMailClient->UnsubscribeMailboxEvents( aId, *this );
       
   567             iObservedMailboxes.Remove( index );
       
   568             return ETrue;
       
   569             }
       
   570         }
       
   571     
       
   572     return EFalse;
       
   573     }
       
   574 
       
   575 void CPsMruAdapter::RemoveAllMailboxObservers()
       
   576     {
       
   577     FUNC_LOG;   
       
   578     for( TInt index = 0; index < iObservedMailboxes.Count(); index++ )
       
   579         {
       
   580         iMailClient->UnsubscribeMailboxEvents( iObservedMailboxes[index], *this );
       
   581         }
       
   582 
       
   583     iObservedMailboxes.Reset(); 
       
   584     }
       
   585 
       
   586 /**
       
   587  * If problem with NewMailbox this function will be called 
       
   588  * by CDelayMailboxCreationHelper timer to try it after some delay
       
   589  */
       
   590 TBool CPsMruAdapter::DeleayedMailboxCreationEventL()
       
   591     {
       
   592     FUNC_LOG;
       
   593     for ( int i = iDelayedCreatedMailboxes.Count()-1; i>=0; i-- )
       
   594         {
       
   595         CFSMailBox *mailboxPtr(NULL);
       
   596         // if mailbox is not ready it may leave there
       
   597         TRAPD(trap_err, mailboxPtr = iMailClient->GetMailBoxByUidL( iDelayedCreatedMailboxes[i] ));
       
   598         if ( trap_err != KErrNone )
       
   599             {
       
   600             mailboxPtr = NULL;
       
   601             }
       
   602         if ( mailboxPtr ) 
       
   603             {
       
   604             delete mailboxPtr;
       
   605             mailboxPtr = NULL;
       
   606             
       
   607             HBufC* identifier = HBufC::NewLC( KMaximumMailboxUriLength ); // new string ident
       
   608             if ( GetUriFromMailboxIdentifier( iDelayedCreatedMailboxes[i], *identifier ) )
       
   609                 {
       
   610 
       
   611                 // Add to supported Uri list
       
   612                 iSupportedUris.AppendL( identifier );
       
   613 
       
   614                 // Add new data store to cache
       
   615                 iStoreListObserver->AddDataStore( *identifier );
       
   616 
       
   617                 // Add all data to data store 
       
   618                 // removed because this is called by AddDataStore through RequestForDataL callback
       
   619                 // FillDataStoreL( *identifier );
       
   620 
       
   621                 // In case there is a problem with transferring strings to  - should be obsolete 
       
   622                 AddMailboxObserverL( iDelayedCreatedMailboxes[i] ); // will be added by FillDataStoreL
       
   623 
       
   624                 CleanupStack::Pop( identifier );
       
   625                 iDelayedCreatedMailboxes.Remove( i );       
       
   626                 }
       
   627             else
       
   628                 {
       
   629                 CleanupStack::PopAndDestroy( identifier );
       
   630                 }
       
   631             } 
       
   632         }
       
   633     return ( 0 == iDelayedCreatedMailboxes.Count() );
       
   634     }
       
   635 
       
   636 /**
       
   637  * If problem with NewMailbox this function will use 
       
   638  * CDelayMailboxCreationHelper to try it after some delay
       
   639  */
       
   640 void CPsMruAdapter::DeleayMailboxCreationEventL( TFSMailMsgId &aMailbox )
       
   641     {
       
   642     FUNC_LOG;   
       
   643     if ( NULL == iDelayMailboxCreationPtr )
       
   644         {
       
   645         iDelayMailboxCreationPtr = CDelayMailboxCreationHelper::NewL( this );
       
   646         }
       
   647     iDelayedCreatedMailboxes.Append( aMailbox );
       
   648     iDelayMailboxCreationPtr->StartDelayedCall();
       
   649     }
       
   650 
       
   651 // ---------------------------------------------------------------------------------------------------
       
   652 // class CDelayMailboxCreationHelper : public CTimer
       
   653 // ---------------------------------------------------------------------------------------------------
       
   654 
       
   655 // static construction leaving on the stack
       
   656 CDelayMailboxCreationHelper* CDelayMailboxCreationHelper::NewLC( CPsMruAdapter *aPsMruAdapterPtr )
       
   657     {
       
   658     FUNC_LOG;
       
   659     CDelayMailboxCreationHelper* self = new ( ELeave ) CDelayMailboxCreationHelper( aPsMruAdapterPtr );
       
   660     CleanupStack::PushL( self );
       
   661     self->ConstructL();
       
   662     return self;
       
   663     }
       
   664 
       
   665 // static construction leaving
       
   666 CDelayMailboxCreationHelper* CDelayMailboxCreationHelper::NewL( CPsMruAdapter *aPsMruAdapterPtr )
       
   667     {
       
   668     FUNC_LOG;
       
   669     CDelayMailboxCreationHelper* self = CDelayMailboxCreationHelper::NewLC( aPsMruAdapterPtr );
       
   670     CleanupStack::Pop( self );
       
   671     return self;
       
   672     }
       
   673 
       
   674 // used by MruAdapter when delayed datasource adding is needed
       
   675 void CDelayMailboxCreationHelper::StartDelayedCall()    
       
   676     {
       
   677     FUNC_LOG;
       
   678     if( IsActive() )  // don't call again in case the timer rq is pending
       
   679         {
       
   680         Cancel();
       
   681         }
       
   682     iNumberOfDelayedTrials = KNumberOfDelayedTrials;
       
   683     After( KDelayToRunAddMailbox );  // CTimer::After contains SetActive() 
       
   684     }
       
   685 
       
   686 // Limited unsuccesful call repeating
       
   687 void CDelayMailboxCreationHelper::RunL()
       
   688     {
       
   689     FUNC_LOG;
       
   690     iNumberOfDelayedTrials --; 
       
   691     User::LeaveIfError( iStatus.Int() );
       
   692     TBool Handled = iPsMruAdapterPtr->DeleayedMailboxCreationEventL();
       
   693     if (( Handled ) || ( 0 >= iNumberOfDelayedTrials ))
       
   694         { // no need to call again
       
   695         Cancel();
       
   696         }
       
   697     else
       
   698         { // wait once more
       
   699 //      SetActive is called by After
       
   700         After( KDelayToRunAddMailbox ); 
       
   701         }
       
   702     }
       
   703 
       
   704 // when leave from RunL, if err handled return KErrNone
       
   705 TInt CDelayMailboxCreationHelper::RunError( TInt aError )
       
   706     {
       
   707     FUNC_LOG;
       
   708     if ( KErrNone != aError )
       
   709         {
       
   710         Cancel(); // stop pending requiest
       
   711         }
       
   712     return KErrNone; //not desired to panic the thread in case of error returned
       
   713     }
       
   714 
       
   715 // 2nd phase of construction
       
   716 void CDelayMailboxCreationHelper::ConstructL()
       
   717     {
       
   718     FUNC_LOG;
       
   719     CTimer::ConstructL();
       
   720     CActiveScheduler::Add( this );
       
   721     }
       
   722 
       
   723 // c-tor   
       
   724 CDelayMailboxCreationHelper::CDelayMailboxCreationHelper(
       
   725     CPsMruAdapter *aPsMruAdapterPtr )
       
   726     : CTimer( EPriorityStandard )  //possible also EPriorityLow, EPriorityIdle
       
   727     , iPsMruAdapterPtr ( aPsMruAdapterPtr )  // not ownink ptr
       
   728     , iNumberOfDelayedTrials ( KNumberOfDelayedTrials ) //limited repeating num.
       
   729     {
       
   730     FUNC_LOG;
       
   731     }
       
   732 
       
   733 // d-tor
       
   734 CDelayMailboxCreationHelper::~CDelayMailboxCreationHelper()
       
   735     {
       
   736     FUNC_LOG;
       
   737     iPsMruAdapterPtr = NULL; // not owning interface ptr
       
   738     Cancel();  // stop pending request
       
   739     Deque();   // remove from CActiveScheduler
       
   740     }
       
   741 
       
   742 // End of file