email/alwaysonlineemailplugin/src/AlwaysOnlineEmailAgent.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *     Agent which holds an array of email protocol specific agents
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <MuiuMsvUiServiceUtilities.h>
       
    21 #include <SenduiMtmUids.h>
       
    22 #include <ImumInternalApi.h>            // CImumInternalApi
       
    23 #include <ImumInSettingsData.h>         // CImumInSettingsData
       
    24 #include <AlwaysOnlineManagerCommon.h>
       
    25 
       
    26 #include "AlwaysOnlineEmailAgent.h"
       
    27 #include "AlwaysOnlineImapAgent.h"
       
    28 #include "AlwaysOnlinePopAgent.h"
       
    29 #include "AlwaysOnlineEmailPluginTimer.h"
       
    30 #include "AlwaysOnlineEmailEMNResolver.h"
       
    31 #include "AlwaysOnlineEmailPluginLogging.h"
       
    32 #include "AlwaysOnlineEmailLoggingTools.h"
       
    33 
       
    34 const TInt KAOHalfSecond = 500000;
       
    35 
       
    36 const TInt KEmailAgentArrayGranularity = 2;
       
    37 // How many times we try to load mail settings before giving up.
       
    38 const TInt KMailSettingsRetryCount     = 3;
       
    39 
       
    40 // ----------------------------------------------------------------------------
       
    41 // CAlwaysOnlineEmailAgent::CAlwaysOnlineEmailAgent()
       
    42 // ----------------------------------------------------------------------------
       
    43 CAlwaysOnlineEmailAgent::CAlwaysOnlineEmailAgent(
       
    44     CMsvSession& aMsvSession,
       
    45     MAlwaysOnlineStatusQueryInterface& aAlwaysOnlineManager)
       
    46     :
       
    47     iAlwaysOnlineManager( aAlwaysOnlineManager ),
       
    48     iSession( aMsvSession ),
       
    49     iEmnResolver( NULL )
       
    50     {
       
    51     }
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // CAlwaysOnlineEmailAgent::~CAlwaysOnlineEmailAgent()
       
    55 // ----------------------------------------------------------------------------
       
    56 CAlwaysOnlineEmailAgent::~CAlwaysOnlineEmailAgent()
       
    57     {
       
    58     AOLOG_IN( "CAlwaysOnlineEmailAgent::~CAlwaysOnlineEmailAgent" );
       
    59     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::~CAlwaysOnlineEmailAgent");
       
    60     if ( iMailAgentArray )
       
    61         {
       
    62         iMailAgentArray->ResetAndDestroy();
       
    63         }
       
    64     delete iMailAgentArray;
       
    65     delete iClientMtmRegistry;
       
    66     delete iEmnResolver;
       
    67     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::~CAlwaysOnlineEmailAgent");
       
    68     }
       
    69 
       
    70 // ----------------------------------------------------------------------------
       
    71 // CAlwaysOnlineEmailAgent::NewL()
       
    72 // ----------------------------------------------------------------------------
       
    73 CAlwaysOnlineEmailAgent* CAlwaysOnlineEmailAgent::NewL(
       
    74     CMsvSession& aMsvSession,
       
    75     MAlwaysOnlineStatusQueryInterface& aAlwaysOnlineManager )
       
    76     {
       
    77     AOLOG_IN( "CAlwaysOnlineEmailAgent::NewL" );
       
    78     CAlwaysOnlineEmailAgent* self = new (ELeave)CAlwaysOnlineEmailAgent(
       
    79         aMsvSession, aAlwaysOnlineManager );
       
    80     CleanupStack::PushL( self );
       
    81     self->ConstructL();
       
    82     CleanupStack::Pop( self );
       
    83     return self;
       
    84     }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 // CAlwaysOnlineEmailAgent::ConstructL
       
    88 // ----------------------------------------------------------------------------
       
    89 void CAlwaysOnlineEmailAgent::ConstructL()
       
    90     {
       
    91     AOLOG_IN( "CAlwaysOnlineEmailAgent::ConstructL" );
       
    92     iClientMtmRegistry = CClientMtmRegistry::NewL( iSession );
       
    93     iMailAgentArray = new (ELeave) CAOEmailAgentArray(
       
    94         KEmailAgentArrayGranularity );
       
    95     iEmnResolver = CAlwaysOnlineEmailEMNResolver::NewL();
       
    96     }
       
    97 
       
    98 // ----------------------------------------------------------------------------
       
    99 // CAlwaysOnlineEmailAgent::StartL()
       
   100 // ----------------------------------------------------------------------------
       
   101 void CAlwaysOnlineEmailAgent::StartL()
       
   102     {
       
   103     AOLOG_IN( "CAlwaysOnlineEmailAgent::StartL" );
       
   104     RefreshMailAgentArrayL();
       
   105     ActivateOnlineMailboxesL();
       
   106     }
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 // CAlwaysOnlineEmailAgent::SuspendNWOperationsL()
       
   110 // ----------------------------------------------------------------------------
       
   111 void CAlwaysOnlineEmailAgent::SuspendNWOperationsL()
       
   112     {
       
   113     AOLOG_IN( "CAlwaysOnlineEmailAgent::SuspendNWOperationsL" );
       
   114     const TInt count = iMailAgentArray->Count();
       
   115     for ( TInt loop = 0; loop < count; loop++ )
       
   116         {
       
   117         iMailAgentArray->At( loop )->Suspend();
       
   118         }
       
   119     }
       
   120 
       
   121 // ----------------------------------------------------------------------------
       
   122 // CAlwaysOnlineEmailAgent::ResumeNWOperationsL()
       
   123 // ----------------------------------------------------------------------------
       
   124 void CAlwaysOnlineEmailAgent::ResumeNWOperationsL()
       
   125     {
       
   126     AOLOG_IN( "CAlwaysOnlineEmailAgent::ResumeNWOperationsL" );
       
   127     const TInt count = iMailAgentArray->Count();
       
   128     for ( TInt loop = 0; loop < count; loop++ )
       
   129         {
       
   130         iMailAgentArray->At( loop )->ResumeL();
       
   131         }
       
   132     }
       
   133 
       
   134 // ----------------------------------------------------------------------------
       
   135 // CAlwaysOnlineEmailAgent::RoamingEventL()
       
   136 // ----------------------------------------------------------------------------
       
   137 void CAlwaysOnlineEmailAgent::RoamingEventL()
       
   138     {
       
   139     AOLOG_IN( "CAlwaysOnlineEmailAgent::RoamingEventL" );
       
   140     const TInt count = iMailAgentArray->Count();
       
   141     for ( TInt loop = 0; loop < count; loop++ )
       
   142         {
       
   143         iMailAgentArray->At( loop )->HandleRoamingEventL();
       
   144         }
       
   145     }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // CAlwaysOnlineEmailAgent::HomeNetworkEventL()
       
   149 // ----------------------------------------------------------------------------
       
   150 void CAlwaysOnlineEmailAgent::HomeNetworkEventL()
       
   151     {
       
   152     AOLOG_IN( "CAlwaysOnlineEmailAgent::HomeNetworkEventL" );
       
   153     const TInt count = iMailAgentArray->Count();
       
   154     for ( TInt loop = 0; loop < count; loop++ )
       
   155         {
       
   156         iMailAgentArray->At( loop )->HandleHomeNetworkEventL();
       
   157         }
       
   158     }
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // CAlwaysOnlineEmailAgent::HandleOutOfDiskL()
       
   162 // ----------------------------------------------------------------------------
       
   163 void CAlwaysOnlineEmailAgent::HandleOutOfDiskL()
       
   164     {
       
   165     AOLOG_IN( "CAlwaysOnlineEmailAgent::HandleOutOfDiskL" );
       
   166     const TInt count = iMailAgentArray->Count();
       
   167     for ( TInt loop = 0; loop < count; loop++ )
       
   168         {
       
   169         iMailAgentArray->At( loop )->HandleOutOfDiskEventL();
       
   170         }
       
   171     }
       
   172 
       
   173 // ----------------------------------------------------------------------------
       
   174 // CAlwaysOnlineEmailAgent::HandleEMNMessageL()
       
   175 // ----------------------------------------------------------------------------
       
   176 void CAlwaysOnlineEmailAgent::HandleEMNMessageL(
       
   177     const TDesC8& aParameters )
       
   178     {
       
   179     AOLOG_IN( "CAlwaysOnlineEmailAgent::HandleEMNMessageL" );
       
   180     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::HandleEMNL");
       
   181     TEMNElement elements;
       
   182 
       
   183     if ( iEmnResolver->ParameterDispatchTEMNElement(
       
   184         aParameters, elements ) == KErrNone )
       
   185         {
       
   186         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleEMNL(): Find EMN mailbox" );
       
   187 
       
   188         CAlwaysOnlineEmailAgentBase* mailbox =
       
   189             iEmnResolver->FindEMNMailbox( elements.mailbox, *iMailAgentArray );
       
   190 
       
   191         if ( mailbox )
       
   192             {
       
   193             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleEMNL(): EMN mailbox found" );
       
   194             mailbox->ConnectL();
       
   195             }
       
   196         else
       
   197             {
       
   198             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleEMNL(): EMN mailbox not found" );
       
   199             }
       
   200         }
       
   201 
       
   202     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::HandleEMNL");
       
   203     }
       
   204 
       
   205 // ----------------------------------------------------------------------------
       
   206 // CAlwaysOnlineEmailAgent::GetMailboxesLC()
       
   207 // ----------------------------------------------------------------------------
       
   208 CMsvEntrySelection* CAlwaysOnlineEmailAgent::GetMailboxesLC()
       
   209     {
       
   210     AOLOG_IN( "CAlwaysOnlineEmailAgent::GetMailboxesLC" );
       
   211     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::GetMailboxesLC");
       
   212     CMsvEntrySelection* mailboxSelection =
       
   213         MsvUiServiceUtilities::GetListOfAccountsWithMTML(
       
   214             iSession, KSenduiMtmImap4Uid );
       
   215 	CleanupStack::PushL( mailboxSelection );
       
   216 
       
   217     CMsvEntrySelection* pop3Selection =
       
   218         MsvUiServiceUtilities::GetListOfAccountsWithMTML(
       
   219             iSession, KSenduiMtmPop3Uid );
       
   220 	CleanupStack::PushL( pop3Selection );
       
   221 
       
   222     const TInt count = pop3Selection->Count();
       
   223     TInt loop = 0;
       
   224     for ( loop = 0; loop < count; loop++ )
       
   225         {
       
   226         mailboxSelection->AppendL( (*pop3Selection)[loop] );
       
   227         }
       
   228     CleanupStack::PopAndDestroy( pop3Selection );
       
   229     pop3Selection = NULL;
       
   230 
       
   231     // remove mailboxes from mailboxSelection which are already in iMailAgentArray
       
   232     for ( loop = mailboxSelection->Count() - 1; loop >= 0; loop-- )
       
   233         {
       
   234         if ( FindMailbox( (*mailboxSelection)[loop] ) )
       
   235             {
       
   236             // already in iMailAgentArray, delete it
       
   237             mailboxSelection->Delete( loop );
       
   238             }
       
   239         }
       
   240     // mailboxSelection in CleanupStack
       
   241     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::GetMailboxesLC");
       
   242     return mailboxSelection;
       
   243 	}
       
   244 
       
   245 // ----------------------------------------------------------------------------
       
   246 // CAlwaysOnlineEmailAgent::RefreshMailAgentArrayLC
       
   247 // ----------------------------------------------------------------------------
       
   248 void CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL()
       
   249     {
       
   250     AOLOG_IN( "CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL" );
       
   251     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL");
       
   252     // delete first mailboxes from the iMailAgentArray, which does not exists anymore.
       
   253     TInt cnt = iMailAgentArray->Count();
       
   254     TBool isHealthy = EFalse;
       
   255     TBool setActive = EFalse;
       
   256     TMsvId mailboxId;
       
   257     TMsvEntry tEntry;
       
   258     TMsvId service;
       
   259 
       
   260     CImumInternalApi* emailApi = CreateEmailApiLC( &iSession );
       
   261     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL() Mail Agents in array: %d", cnt);
       
   262 
       
   263     TInt i;
       
   264     for ( i = cnt; i > 0; i-- )
       
   265         {
       
   266         mailboxId = ( *iMailAgentArray )[i-1]->MailboxId();
       
   267         isHealthy = emailApi->HealthServicesL().IsMailboxHealthy( mailboxId );
       
   268         if ( isHealthy )
       
   269             {
       
   270             setActive = IsMailAgentActiveL( mailboxId );
       
   271             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL() setActive = %d", setActive);
       
   272             }
       
   273 
       
   274         //remove if mailbox has been deleted, AO setting disabled,
       
   275         //EMN turned off or mailbox is not healthy.
       
   276         if ( !setActive || !isHealthy ||
       
   277              iSession.GetEntry( mailboxId, service, tEntry ) != KErrNone )
       
   278             {
       
   279             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL() Deleting agent for mailbox: %d", mailboxId);
       
   280             delete iMailAgentArray->At( i-1 );
       
   281             iMailAgentArray->Delete( i-1 );
       
   282             }
       
   283         }
       
   284 
       
   285     // then add new mailboxes...
       
   286     CMsvEntrySelection* mailboxes = GetMailboxesLC();
       
   287     const TInt count = mailboxes->Count();
       
   288     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL() New mailboxes count: %d", count);
       
   289     for ( i = 0; i < count; i++ )
       
   290         {
       
   291         mailboxId = mailboxes->At( i );
       
   292         isHealthy = emailApi->HealthServicesL().IsMailboxHealthy( mailboxId );
       
   293 
       
   294         if ( isHealthy && IsMailAgentActiveL( mailboxId ) )
       
   295             {
       
   296             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL() Adding agent for mailbox: %d", (*mailboxes)[i]);
       
   297             AddMailboxL( ( *mailboxes )[i] );
       
   298             }
       
   299         }
       
   300     CleanupStack::PopAndDestroy( 2, emailApi ); // CSI: 12,47 # nothing wrong here
       
   301     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::RefreshMailAgentArrayL");
       
   302     }
       
   303 
       
   304 // ----------------------------------------------------------------------------
       
   305 // CAlwaysOnlineEmailAgent::AddMailboxL
       
   306 // ----------------------------------------------------------------------------
       
   307 CAlwaysOnlineEmailAgentBase* CAlwaysOnlineEmailAgent::AddMailboxL(
       
   308     TMsvId aMailboxId )
       
   309     {
       
   310     AOLOG_IN( "CAlwaysOnlineEmailAgent::AddMailboxL" );
       
   311     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::AddMailboxL");
       
   312     CAlwaysOnlineEmailAgentBase* mailAgent = NULL;
       
   313     if ( !FindMailbox( aMailboxId ) )
       
   314         {
       
   315         TMsvEntry tEntry;
       
   316         TMsvId service;
       
   317         if ( iSession.GetEntry( aMailboxId, service, tEntry ) == KErrNone &&
       
   318              tEntry.iType == KUidMsvServiceEntry )
       
   319             {
       
   320             if ( tEntry.iMtm == KSenduiMtmImap4Uid )
       
   321                 {
       
   322                 mailAgent = CAlwaysOnlineImap4Agent::NewL(
       
   323                     iSession, *iClientMtmRegistry, aMailboxId,
       
   324                     iAlwaysOnlineManager, *this );
       
   325                 CleanupStack::PushL( mailAgent );
       
   326                 iMailAgentArray->AppendL( mailAgent ); // takes ownership
       
   327                 CleanupStack::Pop( mailAgent );
       
   328                 }
       
   329             else if ( tEntry.iMtm == KSenduiMtmPop3Uid )
       
   330                 {
       
   331                 mailAgent = CAlwaysOnlinePop3Agent::NewL(
       
   332                     iSession, *iClientMtmRegistry, aMailboxId,
       
   333                     iAlwaysOnlineManager, *this );
       
   334                 CleanupStack::PushL( mailAgent );
       
   335                 iMailAgentArray->AppendL( mailAgent ); // takes ownership
       
   336                 CleanupStack::Pop( mailAgent );
       
   337                 }
       
   338             }
       
   339         }
       
   340     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::AddMailboxL");
       
   341     return mailAgent;
       
   342     }
       
   343 
       
   344 
       
   345 // ----------------------------------------------------------------------------
       
   346 // CAlwaysOnlineEmailAgent::ActivateOnlineMailboxesL
       
   347 // ----------------------------------------------------------------------------
       
   348 void CAlwaysOnlineEmailAgent::ActivateOnlineMailboxesL()
       
   349     {
       
   350     AOLOG_IN( "CAlwaysOnlineEmailAgent::ActivateOnlineMailboxesL" );
       
   351     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::ActivateOnlineMailboxesL");
       
   352     const TInt count = iMailAgentArray->Count();
       
   353 
       
   354     for ( TInt loop = 0; loop < count; loop++ )
       
   355         {
       
   356         (*iMailAgentArray)[loop]->StartL();
       
   357         }//for
       
   358     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::ActivateOnlineMailboxesL");
       
   359     }
       
   360 
       
   361 
       
   362 // ----------------------------------------------------------------------------
       
   363 // CAlwaysOnlineEmailAgent::FindMailbox
       
   364 // ----------------------------------------------------------------------------
       
   365 CAlwaysOnlineEmailAgentBase* CAlwaysOnlineEmailAgent::FindMailbox(
       
   366     TMsvId aMailboxId )
       
   367     {
       
   368     AOLOG_IN( "CAlwaysOnlineEmailAgent::FindMailbox" );
       
   369     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::FindMailbox");
       
   370     // Get number of agents
       
   371     const TInt count = iMailAgentArray->Count();
       
   372     CAlwaysOnlineEmailAgentBase* mailAgent = NULL;
       
   373 
       
   374     // Search through the agents, until correct is found
       
   375     for ( TInt loop = 0; loop < count && !mailAgent; loop++ )
       
   376         {
       
   377         if ( aMailboxId == (*iMailAgentArray)[loop]->MailboxId() )
       
   378             {
       
   379             mailAgent = (*iMailAgentArray)[loop];
       
   380             }
       
   381 
       
   382         }
       
   383 
       
   384     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::FindMailbox");
       
   385     // Return the pointer
       
   386     return mailAgent;
       
   387     }
       
   388 
       
   389 // ----------------------------------------------------------------------------
       
   390 // DeleteMailbox
       
   391 // ----------------------------------------------------------------------------
       
   392 TBool CAlwaysOnlineEmailAgent::DeleteMailbox( TMsvId aMailboxId )
       
   393     {
       
   394     AOLOG_IN( "CAlwaysOnlineEmailAgent::DeleteMailbox" );
       
   395     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::DeleteMailbox");
       
   396     const TInt count = iMailAgentArray->Count();
       
   397     for ( TInt loop = 0; loop < count; loop++ )
       
   398         {
       
   399         if ( aMailboxId == (*iMailAgentArray)[loop]->MailboxId() )
       
   400             {
       
   401             delete iMailAgentArray->At( loop );
       
   402             iMailAgentArray->Delete( loop );
       
   403             return ETrue;
       
   404             }
       
   405 
       
   406         }
       
   407     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::DeleteMailbox");
       
   408     return EFalse;
       
   409     }
       
   410 
       
   411 // ----------------------------------------------------------------------------
       
   412 //  CAlwaysOnlineEmailAgent::MailboxSetActivityL
       
   413 // ----------------------------------------------------------------------------
       
   414 void CAlwaysOnlineEmailAgent::MailboxSetActivityL(
       
   415     const TMsvId aMailboxId,
       
   416     const TBool  aState )
       
   417     {
       
   418     AOLOG_IN( "CAlwaysOnlineEmailAgent::MailboxSetActivityL" );
       
   419     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::MailboxSetActiviytL");
       
   420     // Mailbox settings changed, update the array
       
   421     MailboxSettingsChangedL( aMailboxId );
       
   422 
       
   423     // Find the agent from array
       
   424     CAlwaysOnlineEmailAgentBase* agent = FindMailbox( aMailboxId );
       
   425 
       
   426     // Check that the agent is valid
       
   427     if ( agent )
       
   428         {
       
   429         if ( !aState )
       
   430             {
       
   431             // Suspend the agent
       
   432             agent->Suspend();
       
   433             }
       
   434         else
       
   435             {
       
   436             agent->ResumeL( ETrue );
       
   437             }
       
   438         }
       
   439     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::MailboxSetActiviytL");
       
   440     }
       
   441 
       
   442 // ----------------------------------------------------------------------------
       
   443 // CAlwaysOnlineEmailAgent::MailboxSettingsChangedL
       
   444 // ----------------------------------------------------------------------------
       
   445 void CAlwaysOnlineEmailAgent::MailboxSettingsChangedL( TMsvId aMailboxId )
       
   446     {
       
   447     AOLOG_IN( "CAlwaysOnlineEmailAgent::MailboxSettingsChangedL" );
       
   448     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::MailboxSettingsChangedL");
       
   449     RefreshMailAgentArrayL();
       
   450     CAlwaysOnlineEmailAgentBase* mailAgent = FindMailbox( aMailboxId );
       
   451     if ( mailAgent )
       
   452         {
       
   453         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::MailboxSettingsChangedL() Calling StartL for: %d", aMailboxId);
       
   454         mailAgent->StartL();
       
   455         }
       
   456     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::MailboxSettingsChangedL");
       
   457     }
       
   458 
       
   459 
       
   460 // ----------------------------------------------------------------------------
       
   461 // CAlwaysOnlineEmailAgent::RemoveMailAgentL
       
   462 // ----------------------------------------------------------------------------
       
   463 void CAlwaysOnlineEmailAgent::RemoveMailAgentL()
       
   464     {
       
   465     AOLOG_IN( "CAlwaysOnlineEmailAgent::RemoveMailAgentL" );
       
   466     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::RemoveMailAgentL");
       
   467     RefreshMailAgentArrayL();
       
   468     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::RemoveMailAgentL");
       
   469     }
       
   470 
       
   471 // ----------------------------------------------------------------------------
       
   472 // CAlwaysOnlineEmailAgent::SwitchOffAllAgentsL
       
   473 // ----------------------------------------------------------------------------
       
   474 void CAlwaysOnlineEmailAgent::SwitchOffAllAgentsL()
       
   475     {
       
   476     AOLOG_IN( "CAlwaysOnlineEmailAgent::SwitchOffAllAgentsL" );
       
   477     KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::SwitchOffAllAgentsL() Deleting all agents from agent array!");
       
   478     while ( iMailAgentArray->Count() )
       
   479         {
       
   480         delete iMailAgentArray->At( 0 );
       
   481         iMailAgentArray->Delete( 0 );
       
   482         }
       
   483 
       
   484     }
       
   485 
       
   486 // ----------------------------------------------------------------------------
       
   487 // CAlwaysOnlineEmailAgent::IsMailAgentActiveL
       
   488 // ----------------------------------------------------------------------------
       
   489 TBool CAlwaysOnlineEmailAgent::IsMailAgentActiveL( TMsvId aMailboxId )
       
   490     {
       
   491     AOLOG_IN( "CAlwaysOnlineEmailAgent::IsMailAgentActiveL" );
       
   492     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineEmailAgent::IsMailAgentActiveL");
       
   493     TMsvEntry entry;
       
   494     TMsvId service;
       
   495 
       
   496     TInt err = iSession.GetEntry( aMailboxId, service, entry );
       
   497     if ( err != KErrNone )
       
   498         {
       
   499         return EFalse;
       
   500         }
       
   501 
       
   502     TInt cnt = 0;
       
   503     CImumInternalApi* emailApi = CreateEmailApiLC( &iSession );
       
   504     CImumInSettingsData* settings = NULL;
       
   505 
       
   506     err = KErrNotFound;
       
   507     while ( err && cnt < KMailSettingsRetryCount )
       
   508         {
       
   509         // No need to push, since no leaving method called after
       
   510         TRAP( err, settings =
       
   511             emailApi->MailboxServicesL().LoadMailboxSettingsL( aMailboxId ) );
       
   512         if ( err || !settings )
       
   513             {
       
   514             // wait for half a second and try again
       
   515             RTimer timer;
       
   516             TRequestStatus timerStatus;
       
   517             timer.CreateLocal();
       
   518             timer.After( timerStatus, KAOHalfSecond );
       
   519             User::WaitForRequest(timerStatus); // CSI: 94 # not UI code
       
   520             cnt++;
       
   521             }
       
   522         }
       
   523 
       
   524 
       
   525     // Did we got settings or did we fail 3 times?
       
   526     if ( err && cnt == KMailSettingsRetryCount )
       
   527         {
       
   528         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::IsMailAgentActiveL: failed to load settings, leaving. Err = %d", err );
       
   529         User::Leave( err );
       
   530         }
       
   531 
       
   532     TBool result = EFalse;
       
   533 
       
   534     TInt emnState = 0;
       
   535     settings->GetAttr( TImumDaSettings::EKeyAutoNotifications, emnState );
       
   536     TInt aoState = 0;
       
   537     settings->GetAttr( TImumDaSettings::EKeyAutoRetrieval, aoState );
       
   538 
       
   539     if ( emnState != TImumDaSettings::EValueNotificationsOff )
       
   540         {
       
   541         result = ETrue;
       
   542         }
       
   543     else if ( aoState != TImumDaSettings::EValueAutoOff )
       
   544         {
       
   545         result = ETrue;
       
   546         }
       
   547     else
       
   548         {
       
   549         // There might be temporary agent running.
       
   550         CAlwaysOnlineEmailAgentBase* mailAgent = FindMailbox( aMailboxId );
       
   551         result = mailAgent != NULL; // CSI: 64 # conversion to TBool
       
   552         }
       
   553 
       
   554     CleanupStack::PopAndDestroy( emailApi );
       
   555     emailApi = NULL;
       
   556     delete settings;
       
   557     settings = NULL;
       
   558 
       
   559     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineEmailAgent::IsMailAgentActiveL");
       
   560 
       
   561     return result;
       
   562     }
       
   563 
       
   564 // ----------------------------------------------------------------------------
       
   565 //  CAlwaysOnlineEmailAgent::ParameterDispatchMsvId
       
   566 // ----------------------------------------------------------------------------
       
   567 TInt CAlwaysOnlineEmailAgent::ParameterDispatchMsvId(
       
   568     const TDesC8& aParameters,
       
   569     TMsvId& aMailboxId ) const
       
   570     {
       
   571     AOLOG_IN( "CAlwaysOnlineEmailAgent::ParameterDispatchMsvId" );
       
   572     KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::ParameterDispatchMsvId Dispatch the message");
       
   573 
       
   574     // Presume that wrong type of parameter is given
       
   575     TInt err = KErrNotSupported;
       
   576 
       
   577     // Unpack parameters to see to which mailbox command is for
       
   578     TPckgBuf<TMsvId> paramPack;
       
   579 
       
   580     // Make sure that the parameter length matches Id length
       
   581     if ( aParameters.Length() == sizeof( TMsvId ) )
       
   582         {
       
   583         paramPack.Copy( aParameters );
       
   584 
       
   585         // Get the mailbox id from the packet
       
   586         aMailboxId = paramPack();
       
   587         err = KErrNone;
       
   588         }
       
   589 
       
   590     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineEmailAgent::ParameterDispatchMsvId Mailbox receiving message: 0x%x", aMailboxId );
       
   591     return err;
       
   592     }
       
   593 
       
   594 // ----------------------------------------------------------------------------
       
   595 //  CAlwaysOnlineEmailAgent::HandleAOStateL
       
   596 // ----------------------------------------------------------------------------
       
   597 void CAlwaysOnlineEmailAgent::HandleAOStateL(
       
   598     const TInt    aCommand,
       
   599     const TDesC8& aParameters )
       
   600     {
       
   601     AOLOG_IN( "CAlwaysOnlineEmailAgent::HandleAOStateL" );
       
   602     // Get the msvid from parameter
       
   603     TMsvId mailboxId;
       
   604 
       
   605     // Make sure parameter has correct type of information in
       
   606     if ( ParameterDispatchMsvId( aParameters, mailboxId ) != KErrNone )
       
   607         {
       
   608         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleAOState -> Invalid parameter!");
       
   609         return;
       
   610         }
       
   611 
       
   612     // Handle the command
       
   613     switch ( aCommand )
       
   614         {
       
   615         case EAOManagerMailboxAgentSuspend:
       
   616             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleAOState -> Command: Suspend agent");
       
   617             MailboxSetActivityL( mailboxId, EFalse );
       
   618             break;
       
   619 
       
   620         case EAOManagerMailboxAgentResume:
       
   621             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleAOState -> Command: Resume agent");
       
   622             MailboxSetActivityL( mailboxId, ETrue );
       
   623             break;
       
   624 
       
   625         case EAOManagerMailboxAgentRemove:
       
   626             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineEmailAgent::HandleAOState -> Command: Remove agent");
       
   627             DeleteMailbox( mailboxId );
       
   628             break;
       
   629 
       
   630         case EAOManagerMailboxAgentUpdateMailWhileConnected:
       
   631             MailboxUpdateMailWhileConnectedL( mailboxId ); // fetch new mail but do not reconnect if connection is dropped!
       
   632             break;
       
   633 
       
   634         default:
       
   635             break;
       
   636         }
       
   637     }
       
   638 
       
   639 // ----------------------------------------------------------------------------
       
   640 //  CAlwaysOnlineEmailAgent::MailboxSetActivityL
       
   641 // ----------------------------------------------------------------------------
       
   642 void CAlwaysOnlineEmailAgent::MailboxUpdateMailWhileConnectedL(
       
   643     const TMsvId aMailboxId )
       
   644     {
       
   645     AOLOG_IN( "CAlwaysOnlineEmailAgent::MailboxUpdateMailWhileConnectedL" );
       
   646     TMsvEntry tEntry;
       
   647     TMsvId service;
       
   648 
       
   649     // this is valid only for imap4 mailboxes which is already connected
       
   650     if ( iSession.GetEntry( aMailboxId, service, tEntry ) == KErrNone
       
   651         && tEntry.iMtm == KSenduiMtmImap4Uid
       
   652         && tEntry.iType == KUidMsvServiceEntry
       
   653         && tEntry.Connected() )
       
   654 
       
   655         {
       
   656         // Find the agent from array
       
   657         CAlwaysOnlineEmailAgentBase* agent = FindMailbox( aMailboxId );
       
   658 
       
   659         // Check that the agent is valid
       
   660         if ( agent )
       
   661             {
       
   662             // do nothing, this is handled by default
       
   663             CAlwaysOnlineImap4Agent* mailAgent =
       
   664                 static_cast<CAlwaysOnlineImap4Agent*>( agent );
       
   665             mailAgent->SetUpdateMailWhileConnectedL( ETrue );
       
   666             }
       
   667         else
       
   668             {
       
   669             // create temporary agent...
       
   670             CAlwaysOnlineImap4Agent* mailAgent =
       
   671                 CAlwaysOnlineImap4Agent::NewL(
       
   672                     iSession, *iClientMtmRegistry, aMailboxId,
       
   673                     iAlwaysOnlineManager, *this );
       
   674             CleanupStack::PushL( mailAgent );
       
   675             iMailAgentArray->AppendL( mailAgent ); // takes ownership
       
   676             CleanupStack::Pop( mailAgent );
       
   677             mailAgent->SetUpdateMailWhileConnectedL( EFalse );
       
   678             }
       
   679         }
       
   680     }
       
   681 
       
   682 
       
   683 
       
   684 //EOF