email/alwaysonlineemailplugin/src/AlwaysOnlineEmailPlugin.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 *     Main plugin class
       
    16 *
       
    17 */
       
    18 
       
    19 #include <msvids.h>
       
    20 #include "AlwaysOnlineEmailPlugin.h"
       
    21 #include <AlwaysOnlineManagerCommon.h>
       
    22 #include <msvuids.h>
       
    23 #include <SenduiMtmUids.h>
       
    24 #include <msvapi.h>
       
    25 #include <AlwaysOnlineManagerClient.h>
       
    26 
       
    27 #include "AlwaysOnlineEmailAgent.h"
       
    28 #include "AlwaysOnlineEmailPluginLogging.h"
       
    29 #include "AlwaysOnlineEmailLoggingTools.h"
       
    30 
       
    31 const TInt KAOEmailLaunchStartTime = 3000000;
       
    32 const TInt KAOEmailLaunchRetryTime = 3000000;
       
    33 typedef TTimeIntervalMicroSeconds32 TAOEmailMs;
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // Constructor
       
    37 // ----------------------------------------------------------------------------
       
    38 CEComEmailPlugin::CEComEmailPlugin( ) : CAlwaysOnlineEComInterface()
       
    39     {
       
    40     AOLOG_IN( "CEComEmailPlugin::CEComEmailPlugin" );
       
    41     iPluginStarted = EFalse;
       
    42     }
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // NewL
       
    46 // ----------------------------------------------------------------------------
       
    47 CEComEmailPlugin* CEComEmailPlugin::NewL( )
       
    48     {
       
    49     AOLOG_IN( "CEComEmailPlugin::NewL" );
       
    50     CEComEmailPlugin* self = new (ELeave) CEComEmailPlugin();
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop( self );
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // ConstructL
       
    59 // ----------------------------------------------------------------------------
       
    60 void CEComEmailPlugin::ConstructL()
       
    61     {
       
    62     AOLOG_IN( "CEComEmailPlugin::ConstructL" );
       
    63     iSession = CMsvSession::OpenSyncL( *this );
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 //  ~CEComEmailPlugin()
       
    68 // ----------------------------------------------------------------------------
       
    69 CEComEmailPlugin::~CEComEmailPlugin()
       
    70     {
       
    71     AOLOG_IN( "CEComEmailPlugin::~CEComEmailPlugin" );
       
    72     KAOEMAIL_LOGGER_WRITE("CEComEmailPlugin::~CEComEmailPlugin()");
       
    73     delete iSessionStarter;
       
    74     delete iEmailAgent;
       
    75     delete iSession;
       
    76     }
       
    77 
       
    78 // ----------------------------------------------------------------------------
       
    79 // HandleSessionEventL() from MMsvSessionObserver
       
    80 // ----------------------------------------------------------------------------
       
    81 void CEComEmailPlugin::HandleSessionEventL(
       
    82     TMsvSessionEvent    aEvent,
       
    83     TAny*               /* aArg1 */,
       
    84     TAny*               /* aArg2 */,
       
    85     TAny*               /* aArg3 */)
       
    86     {
       
    87     AOLOG_IN( "CEComEmailPlugin::HandleSessionEventL" );
       
    88     AOLOG_WRMV( "aEvent: ", EAoMsvSessionEvent, aEvent, EAoLogSt3 );
       
    89     KAOEMAIL_LOGGER_WRITE_FORMAT("CEComEmailPlugin::HandleSessionEventL() aEvent %d", aEvent);
       
    90     //we can receive session events even before plugin is started..we MUST NOT try to handle those.
       
    91     //they would just result in access violation because iEmailAgent is NULL at that time.
       
    92     if ( iPluginStarted )
       
    93         {
       
    94         switch (aEvent)
       
    95             {
       
    96             case EMsvMediaUnavailable:
       
    97                 iEmailAgent->SwitchOffAllAgentsL();
       
    98                 break;
       
    99 
       
   100             case EMsvMediaChanged:
       
   101             case EMsvMediaAvailable:
       
   102                 iEmailAgent->RefreshMailAgentArrayL();
       
   103                 iEmailAgent->ActivateOnlineMailboxesL();
       
   104                 break;
       
   105 
       
   106             case EMsvCloseSession:
       
   107             case EMsvServerTerminated:
       
   108                 HandleSessionTerminatedL();
       
   109                 break;
       
   110 
       
   111                 //let these fall through.
       
   112             case EMsvEntriesCreated:
       
   113             case EMsvEntriesDeleted:
       
   114             case EMsvEntriesChanged:
       
   115             case EMsvServerReady:
       
   116             case EMsvMediaIncorrect:
       
   117             case EMsvIndexLoaded:
       
   118             case EMsvIndexFailedToLoad:
       
   119             case EMsvCorruptedIndexRebuilding:
       
   120             case EMsvMtmGroupInstalled:
       
   121             case EMsvMtmGroupDeInstalled:
       
   122             case EMsvGeneralError:
       
   123             default:
       
   124                 //no handling for these now
       
   125                 break;
       
   126             }//switch
       
   127         }//if
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------------------------
       
   131 //  HandleServerCommandL
       
   132 // ----------------------------------------------------------------------------
       
   133 TAny* CEComEmailPlugin::HandleServerCommandL(
       
   134     TInt aCommand,
       
   135     TDesC8* aParameters )
       
   136     {
       
   137     AOLOG_IN( "CEComEmailPlugin::HandleServerCommandL" );
       
   138     AOLOG_WRMV( "aCommand: ", EAoManagerServerCommands, aCommand, EAoLogSt3 );
       
   139     KAOEMAIL_LOGGER_WRITE_FORMAT("CEComEmailPlugin::HandleServerCommandL() aCommand %d", aCommand);
       
   140 
       
   141     TInt result = KErrNone;
       
   142 
       
   143     // Make system check
       
   144     VerifyPluginHealthL();
       
   145 
       
   146     switch ( aCommand )
       
   147         {
       
   148         case EAOManagerPluginStart:
       
   149             ActivateOnlineMailboxesL();
       
   150         
       
   151             iPluginStarted = ETrue;
       
   152             break;
       
   153             
       
   154         case EAOManagerPluginStop:
       
   155             // Return value does not matter, plugin will be stopped anyway.
       
   156             result = KErrNotSupported;
       
   157             break;
       
   158 
       
   159         case EAOManagerNWOpsNotAllowed:
       
   160             iEmailAgent->SuspendNWOperationsL();
       
   161             break;
       
   162             
       
   163         case EAOManagerNWOpsAllowed:
       
   164             iEmailAgent->ResumeNWOperationsL();
       
   165             break;
       
   166             
       
   167         case EAOManagerStartedRoaming:
       
   168             iEmailAgent->RoamingEventL();
       
   169             break;
       
   170             
       
   171         case EAOManagerStoppedRoaming:
       
   172             iEmailAgent->HomeNetworkEventL();
       
   173             break;
       
   174             
       
   175         case EAOManagerDiskSpaceAboveCritical:
       
   176             // Not handled at the moment because 
       
   177             // switching off at below critical
       
   178             result = KErrNotSupported;
       
   179             break;
       
   180             
       
   181         case EAOManagerDiskSpaceBelowCritical:
       
   182             iEmailAgent->HandleOutOfDiskL();
       
   183             break;
       
   184             
       
   185         case EAOManagerSuicideQuery:
       
   186             // Always tell to server, that we don't 
       
   187             // want to make a suicide.
       
   188             result = EFalse;
       
   189             break;
       
   190 
       
   191         // Mailbox handling
       
   192         case EAOManagerMailboxAgentSuspend:
       
   193         case EAOManagerMailboxAgentResume:
       
   194         case EAOManagerMailboxAgentRemove:
       
   195         case EAOManagerEMNReceived:
       
   196         case EAOManagerMailboxAgentUpdateMailWhileConnected:
       
   197             HandleCmdMailboxL( aCommand, aParameters );
       
   198             break;
       
   199         
       
   200         // EAOManagerAOSchdulerError not handled because 
       
   201         // there's no badly behaving active objects
       
   202         case EAOManagerAOSchdulerError:
       
   203             KAOEMAIL_LOGGER_WRITE("CEComEmailPlugin::HandleServerCommandL() EAOManagerAOSchdulerError recieved");
       
   204         default:
       
   205             result = KErrNotSupported;
       
   206             break;
       
   207         }
       
   208 
       
   209     KAOEMAIL_LOGGER_WRITE_FORMAT("CEComEmailPlugin::HandleServerCommandL() result %d", result);
       
   210 
       
   211     return reinterpret_cast<TAny*>( result );
       
   212     }
       
   213 
       
   214 // ----------------------------------------------------------------------------
       
   215 //  CEComEmailPlugin::ActivateOnlineMailboxesL
       
   216 // ----------------------------------------------------------------------------
       
   217 void CEComEmailPlugin::ActivateOnlineMailboxesL()
       
   218     {
       
   219     AOLOG_IN( "CEComEmailPlugin::ActivateOnlineMailboxesL" );
       
   220     // Start the agent, if everything is ok
       
   221     VerifyPluginHealthL();
       
   222     iEmailAgent->StartL();
       
   223     }
       
   224 
       
   225 // ----------------------------------------------------------------------------
       
   226 //  CEComEmailPlugin::HandleCmdMailboxL
       
   227 // ----------------------------------------------------------------------------
       
   228 void CEComEmailPlugin::HandleCmdMailboxL(
       
   229     const TInt    aCommand,
       
   230     const TDesC8* aParameters )
       
   231     {
       
   232     AOLOG_IN( "CEComEmailPlugin::HandleCmdMailboxL" );
       
   233     // Check for NULL pointer and return
       
   234     if ( !aParameters )
       
   235         {
       
   236         // Just don't care about the false alarm,
       
   237         // ignore them to keep things running
       
   238         // smoothly.
       
   239         return;
       
   240         }
       
   241 
       
   242     switch( aCommand )
       
   243         {
       
   244         // Suspend Agent
       
   245         case EAOManagerMailboxAgentSuspend:
       
   246         case EAOManagerMailboxAgentResume:
       
   247         case EAOManagerMailboxAgentRemove:
       
   248         case EAOManagerMailboxAgentUpdateMailWhileConnected:
       
   249             iEmailAgent->HandleAOStateL( aCommand, *aParameters );
       
   250             break;
       
   251 
       
   252         // Mail notification
       
   253         case EAOManagerEMNReceived:
       
   254             iEmailAgent->HandleEMNMessageL( *aParameters );
       
   255             break;
       
   256 
       
   257         default:
       
   258             break;
       
   259         }
       
   260     }
       
   261 
       
   262 // ----------------------------------------------------------------------------
       
   263 //  CEComEmailPlugin::LaunchMsvSession
       
   264 // ----------------------------------------------------------------------------
       
   265 TInt CEComEmailPlugin::LaunchMsvSession( TAny* aSelf )
       
   266     {
       
   267     AOLOG_IN( "CEComEmailPlugin::LaunchMsvSession" );
       
   268     return reinterpret_cast< CEComEmailPlugin* >( aSelf )->LaunchSession();
       
   269     }
       
   270 
       
   271 // ----------------------------------------------------------------------------
       
   272 //  CEComEmailPlugin::LaunchSession
       
   273 // ----------------------------------------------------------------------------
       
   274 TInt CEComEmailPlugin::LaunchSession()
       
   275     {
       
   276     AOLOG_IN( "CEComEmailPlugin::LaunchSession" );
       
   277     KAOEMAIL_LOGGER_FN1("CEComEmailPlugin::LaunchSession");
       
   278     const TInt KAOEmailRetryCounter = 20;
       
   279     TInt error = KErrNone;
       
   280     iLaunchCounter++;
       
   281 
       
   282     // Try activating mailboxex
       
   283     if ( iLaunchCounter < KAOEmailRetryCounter )
       
   284         {
       
   285         TRAP( error, ActivateOnlineMailboxesL() );
       
   286         }
       
   287     // Else just give up for trying and hang around for miracle to happen...
       
   288 
       
   289     // Remove the timer
       
   290     if ( error == KErrNone )
       
   291         {
       
   292         delete iSessionStarter;
       
   293         iSessionStarter = NULL;
       
   294         iPluginStarted = ETrue;
       
   295         }
       
   296     KAOEMAIL_LOGGER_WRITE_FORMAT( "LaunchSession, error = %d", error);
       
   297     KAOEMAIL_LOGGER_FN2("CEComEmailPlugin::LaunchSession");
       
   298     return error;
       
   299     }
       
   300 
       
   301 // ----------------------------------------------------------------------------
       
   302 //  CEComEmailPlugin::HandleSessionTerminatedL
       
   303 // ----------------------------------------------------------------------------
       
   304 void CEComEmailPlugin::HandleSessionTerminatedL()
       
   305     {
       
   306     AOLOG_IN( "CEComEmailPlugin::HandleSessionTerminatedL" );
       
   307     KAOEMAIL_LOGGER_FN1("CEComEmailPlugin::HandleSessionTerminatedL");
       
   308 
       
   309     // Messaging Server has been terminated, recreate connection to it
       
   310     delete iEmailAgent;
       
   311     iEmailAgent = NULL;
       
   312     iPluginStarted = EFalse;
       
   313     delete iSession;
       
   314     iSession = NULL;
       
   315 
       
   316     // Check if timer already exist for recreating the
       
   317     if ( !iSessionStarter )
       
   318         {
       
   319         iSessionStarter = CPeriodic::NewL( CActive::EPriorityIdle );
       
   320         }
       
   321 
       
   322     // If the timer isn't active, start it
       
   323     if ( !( iSessionStarter->IsActive() ) )
       
   324         {
       
   325         // Determine the intervals
       
   326         const TAOEmailMs startTimeInterval = KAOEmailLaunchStartTime;
       
   327         const TAOEmailMs retryTimeInterval = KAOEmailLaunchRetryTime;
       
   328         iLaunchCounter = 0;
       
   329 
       
   330         // Start the timer
       
   331         iSessionStarter->Start(
       
   332             startTimeInterval,
       
   333             retryTimeInterval,
       
   334             TCallBack( LaunchMsvSession, this ) );
       
   335         }
       
   336     KAOEMAIL_LOGGER_FN2("CEComEmailPlugin::HandleSessionTerminatedL");
       
   337     }
       
   338 
       
   339 // ----------------------------------------------------------------------------
       
   340 // CEComEmailPlugin::VerifyPluginHealthL()
       
   341 // ----------------------------------------------------------------------------
       
   342 void CEComEmailPlugin::VerifyPluginHealthL()
       
   343     {
       
   344     AOLOG_IN( "CEComEmailPlugin::VerifyPluginHealthL" );
       
   345 
       
   346     // Create new session, if it doesn't exist yet
       
   347     if ( !iSession )
       
   348         {
       
   349         KAOEMAIL_LOGGER_WRITE("iSession is NULL");
       
   350         iSession = CMsvSession::OpenSyncL( *this );
       
   351         KAOEMAIL_LOGGER_WRITE_FORMAT("Created iSession 0x%x", iSession);
       
   352         }
       
   353 
       
   354     // Create new agent, if there isn't such yet
       
   355     if ( !iEmailAgent && iSession )
       
   356         {
       
   357         KAOEMAIL_LOGGER_WRITE("iEmailAgent is NULL");
       
   358         iEmailAgent = CAlwaysOnlineEmailAgent::NewL(
       
   359             *iSession, *iStatusQueryObject );
       
   360         }
       
   361     }
       
   362 
       
   363 
       
   364 //EOF