messagingfw/alwaysonline/AlwaysOnlineManager/src/AlwaysOnlineManager.cpp
changeset 0 8e480a14352b
child 11 33d4877f7b9f
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     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 *     Alwaysonline manager implementation file
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <ecom/ecom.h>
       
    21 #include <ecom/resolver.h>
       
    22 #include <AlwaysOnlineManagerCommon.h>
       
    23 #include <CoreApplicationUIsSDKCRKeys.h>   // Offline key
       
    24 
       
    25 #include "AlwaysOnlineManager.h"
       
    26 #include "AlwaysOnlineManagerServer.h"
       
    27 #include "HandleServerCommandOperation.h"
       
    28 #include "AlwaysOnlineManagerLogging.h"
       
    29 #include "AOCenRepControl.h"
       
    30 #include "AOCommandParser.h"
       
    31 
       
    32 const TInt KPluginArrayGranularity = 3;
       
    33 const TInt KNWOperationsAllowed = 1;
       
    34 const TInt KManagerOperationsGranularity = 6;
       
    35 
       
    36 const TInt KDisabledPluginsGranulatiry = 3;
       
    37 
       
    38 const TUid KAOEmailPluginUid = {0x101F85ED};
       
    39 
       
    40 _LIT8( KStopPlugin, "1" );
       
    41 #ifdef _DEBUG
       
    42 _LIT( KAOManagerParsePanic, "AO parse" );
       
    43 #endif
       
    44 
       
    45 // ----------------------------------------------------------------------------
       
    46 // CAlwaysOnlineManager()
       
    47 // ----------------------------------------------------------------------------
       
    48 //
       
    49 CAlwaysOnlineManager::CAlwaysOnlineManager()
       
    50     {
       
    51     }
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // NewL()
       
    55 // ----------------------------------------------------------------------------
       
    56 //
       
    57 CAlwaysOnlineManager* CAlwaysOnlineManager::NewL(
       
    58     CAlwaysOnlineManagerServer* aServer )
       
    59     {
       
    60     CAlwaysOnlineManager* self = new(ELeave) CAlwaysOnlineManager;
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aServer );
       
    63     CleanupStack::Pop( self );
       
    64     return self;
       
    65     }
       
    66 
       
    67 // ----------------------------------------------------------------------------
       
    68 // ~CAlwaysOnlineManager()
       
    69 // ----------------------------------------------------------------------------
       
    70 //
       
    71 CAlwaysOnlineManager::~CAlwaysOnlineManager()
       
    72     {
       
    73     if ( iOperations )
       
    74         {
       
    75         iOperations->ResetAndDestroy();
       
    76         }
       
    77         
       
    78     delete iOperations;
       
    79     iRfs.Close();
       
    80     iConMon.Close();
       
    81     
       
    82     //we shouldn't never come here while phone is switched on
       
    83     if ( iPluginArray )
       
    84         {
       
    85         TInt idx = KErrNotFound;
       
    86         while( iPluginArray->Count() )
       
    87             {
       
    88             idx = iPluginArray->Count() - 1;
       
    89             // Delete object
       
    90             delete iPluginArray->At( idx );
       
    91             // Delete element
       
    92             iPluginArray->Delete( idx );
       
    93             }
       
    94         }
       
    95         
       
    96     delete iCenRepControl;
       
    97     delete iPluginArray;       
       
    98     delete iDisabledPluginUidsArray;
       
    99     delete iDiskSpaceObserver;
       
   100     delete iSession;
       
   101     }
       
   102     
       
   103 // ----------------------------------------------------------------------------
       
   104 // ConstructL()
       
   105 // ----------------------------------------------------------------------------
       
   106 //
       
   107 void CAlwaysOnlineManager::ConstructL( CAlwaysOnlineManagerServer* aServer )
       
   108     {
       
   109     // Create operation array to heap
       
   110     iOperations = new ( ELeave ) 
       
   111         CMsvSingleOpWatcherArray( KManagerOperationsGranularity );
       
   112         
       
   113     iCenRepControl = CAOCenRepControl::NewL( *this );
       
   114     
       
   115     iSession = CMsvSession::OpenSyncL(*this);
       
   116     iServer = aServer;
       
   117     iNetworkStatus = KErrNotFound;
       
   118     iOffline = ETrue;
       
   119     iLastDiskEvent = EAOManagerEmailCommandBase;
       
   120     iPluginsLoaded = EFalse;
       
   121     iPluginArray = new(ELeave)
       
   122         CArrayFixFlat<CAlwaysOnlineEComInterface*>(KPluginArrayGranularity);
       
   123     iDisabledPluginUidsArray = new ( ELeave ) 
       
   124         CArrayFixFlat<TUid>( KDisabledPluginsGranulatiry );
       
   125 
       
   126     User::LeaveIfError( iRfs.Connect() );
       
   127     iConMon.ConnectL();
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------------------------
       
   131 // DoStartL()
       
   132 // ----------------------------------------------------------------------------
       
   133 //
       
   134 void CAlwaysOnlineManager::DoStartL()
       
   135     {
       
   136     if( !iPluginsLoaded )
       
   137         {
       
   138         TRAPD( err, LoadPluginsL() );
       
   139         if ( err != KErrNone )
       
   140             {
       
   141             KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::DoStartL() Errors in LoadPluginsL. Err = %d", err );
       
   142             }
       
   143         else
       
   144             {
       
   145             // Nowadays, we don't care about the plugin count, we will start all
       
   146             // which are not disabled. At this point iPluginArray contains those
       
   147             // plugins which are enabled.
       
   148             TRAPD( err, StartPluginsL() );
       
   149             if ( err != KErrNone )
       
   150                 {
       
   151                 KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::DoStartL() Errors in StartPluginsL. Err = %d", err );
       
   152                 }
       
   153             else
       
   154                 {
       
   155                 //don't start observer unless plugins have been started                  
       
   156                 iDiskSpaceObserver = 
       
   157                     CAlwaysOnlineDiskSpaceObserver::NewL( this, *iSession );
       
   158                 iDiskSpaceObserver->SetLimitAndActivateL();
       
   159                 //check network status and start observing it
       
   160                 CheckNetworkStatusAndSetObserverL();
       
   161                 
       
   162                 //get state of KCoreAppUIsNetworkConnectionAllowed key from 
       
   163                 //CenRep and relay it to plugins
       
   164                 TInt state = KErrNotFound;
       
   165                 TInt result = iCenRepControl->GetCenRepKey(
       
   166                     KCRUidCoreApplicationUIs,
       
   167                     KCoreAppUIsNetworkConnectionAllowed, 
       
   168                     state );
       
   169                     
       
   170                 if ( result == KErrNone )
       
   171                     {
       
   172                     HandleOfflineEventL( state );
       
   173                     }
       
   174                 else
       
   175                     {
       
   176                     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::DoStartL() Could not get KCoreAppUIsNetworkConnectionAllowed from CenRep! Result = %d", result );
       
   177                     }
       
   178                 }
       
   179                 
       
   180             // Send suicide query to all started plugins.
       
   181             KAOMANAGER_LOGGER_WRITE("CAlwaysOnlineManager::DoStartL() Sending suicide queries.");
       
   182             QuerySuicideL();
       
   183             }
       
   184         }
       
   185     }
       
   186 
       
   187 // ----------------------------------------------------------------------------
       
   188 // LoadPluginsL()
       
   189 // ----------------------------------------------------------------------------
       
   190 //
       
   191 void CAlwaysOnlineManager::LoadPluginsL()
       
   192     {
       
   193     RImplInfoPtrArray pluginArray;
       
   194 
       
   195     //List all plugins which implement AlwaysOnlineManagerInterface
       
   196     REComSession::ListImplementationsL( KCEComInterfaceUid, pluginArray );
       
   197 
       
   198     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::LoadPluginsL() Count of plugins found: %d", pluginArray.Count() );
       
   199     
       
   200     // Update list of disabled plugins from cenrep
       
   201     iCenRepControl->UpdateDisabledPluginsUids( *iDisabledPluginUidsArray );
       
   202     
       
   203     if ( pluginArray.Count() )
       
   204         {
       
   205         TInt index = -1;
       
   206         TKeyArrayFix key( 0, ECmpTInt32 );
       
   207         TInt result = KErrNotFound;
       
   208         
       
   209         for( TInt i = 0; i < pluginArray.Count(); i++ )
       
   210             {
       
   211             CImplementationInformation* info = pluginArray[ i ];
       
   212             
       
   213             TUid id = info->ImplementationUid();
       
   214             delete info;
       
   215             info = NULL;
       
   216             
       
   217             result = iDisabledPluginUidsArray->Find( id, key, index );
       
   218             // if id is found from disabled plugins list, then we don't
       
   219             // load it.
       
   220             if ( result == 0 )
       
   221                 {
       
   222                 KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::LoadPluginsL() disabled plugin: 0x%x", id );
       
   223                 }
       
   224             else
       
   225                 {
       
   226                 CAlwaysOnlineEComInterface* implementation = NULL;
       
   227 
       
   228                 //be sure that if plugin leaves, manager doesn't. 
       
   229                 //This applies in every place!
       
   230                 TRAPD( err, implementation = 
       
   231                     CAlwaysOnlineEComInterface::NewL( id ) );
       
   232                 
       
   233                 if ( err == KErrNone )
       
   234                     {
       
   235                     implementation->SetStatusQueryObject( this );
       
   236                     iPluginArray->AppendL( implementation );
       
   237 
       
   238                     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::LoadPluginsL() plugin loaded succesfully: 0x%x", id);
       
   239                     }
       
   240                 else
       
   241                     {
       
   242                     // Just write to debug log, that all the plugins could not be loaded.
       
   243                     // Perhaps there should be some nice way to acknoledge the user or the system!
       
   244                     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::LoadPluginsL() plugin load failed: 0x%x", id);
       
   245                     }
       
   246                 }
       
   247             }//for
       
   248 
       
   249         //plugins should be loaded
       
   250         iPluginsLoaded = ETrue;
       
   251         
       
   252         }//if
       
   253     else
       
   254         {
       
   255         KAOMANAGER_LOGGER_WRITE("CAlwaysOnlineManager::LoadPluginsL() NO PLUGINS FOUND!");
       
   256         //no plugins. This shouldn't be, there's something wrong with build.
       
   257         //if plugins are not in build, server shouldn't be either!        
       
   258         }
       
   259     }
       
   260 
       
   261 // ----------------------------------------------------------------------------
       
   262 // StartPluginsL()
       
   263 // ----------------------------------------------------------------------------
       
   264 //
       
   265 void CAlwaysOnlineManager::StartPluginsL()
       
   266     {
       
   267     TInt count = iPluginArray->Count();
       
   268 
       
   269     TBuf8<1> dummyParam;
       
   270     for ( TInt i = 0; i < count; i++ )
       
   271         {
       
   272         InvokeCommandHandlerL( EAOManagerPluginStart, dummyParam, i );
       
   273         }
       
   274     KAOMANAGER_LOGGER_WRITE("CAlwaysOnlineManager::StartPluginsL() All plugins ordered to start");
       
   275     }
       
   276 
       
   277 // ----------------------------------------------------------------------------
       
   278 // QuerySuicideL()
       
   279 // ----------------------------------------------------------------------------
       
   280 //
       
   281 void CAlwaysOnlineManager::QuerySuicideL()
       
   282     {
       
   283     TInt count = iPluginArray->Count();
       
   284 
       
   285     TBuf8<1> dummyParam;
       
   286     for ( TInt i = 0; i < count; i++ )
       
   287         {
       
   288         InvokeCommandHandlerL( EAOManagerSuicideQuery, dummyParam, i );
       
   289         }
       
   290     KAOMANAGER_LOGGER_WRITE("CAlwaysOnlineManager::QuerySuicideL() Suicide query made for all plugins");
       
   291     }
       
   292 
       
   293 // ----------------------------------------------------------------------------
       
   294 // QueryStatusL()
       
   295 // ----------------------------------------------------------------------------
       
   296 //
       
   297 TAny* CAlwaysOnlineManager::QueryStatusL( TInt aQuery )
       
   298     {
       
   299     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::QueryStatusL()");
       
   300     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::QueryStatusL() Query received: %d", aQuery);
       
   301     
       
   302     switch( aQuery )
       
   303         {
       
   304         case EAOManagerStatusQueryRoaming:
       
   305             return reinterpret_cast<TAny*>(
       
   306                 iNetworkStatus == ENetworkRegistrationRoaming );
       
   307             
       
   308         case EAOManagerStatusQueryOffline:
       
   309             return reinterpret_cast<TAny*>( iOffline );
       
   310 
       
   311         case EAOManagerStatusQueryNetwork:
       
   312             return reinterpret_cast<TAny*>( iNetworkStatus );
       
   313         
       
   314         default:           
       
   315             return reinterpret_cast<TAny*>( KErrNotSupported );//unknown query
       
   316             
       
   317         }
       
   318     }
       
   319 
       
   320 // ----------------------------------------------------------------------------
       
   321 // HandleOfflineEventL()
       
   322 // ----------------------------------------------------------------------------
       
   323 //
       
   324 void CAlwaysOnlineManager::HandleOfflineEventL( TInt aEvent)
       
   325     {
       
   326     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleOfflineEventL()");
       
   327     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandeOfflineEventL() aEvent: %d", aEvent);
       
   328     TInt count = iPluginArray->Count();
       
   329 
       
   330     TInt command = KErrNotFound;
       
   331 
       
   332     if( aEvent == KNWOperationsAllowed )
       
   333         {
       
   334         command = EAOManagerNWOpsAllowed;
       
   335         iOffline = EFalse;
       
   336         }
       
   337     else 
       
   338         {
       
   339         command = EAOManagerNWOpsNotAllowed;
       
   340         iOffline = ETrue;
       
   341         }
       
   342 
       
   343     TBuf8<1> dummyParam;
       
   344     for( TInt i = 0; i<count; i++ )
       
   345         {
       
   346         InvokeCommandHandlerL( 
       
   347             static_cast<TManagerServerCommands>(command), dummyParam, i );
       
   348         }
       
   349     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleOfflineEventL()");
       
   350     }
       
   351 
       
   352 // ----------------------------------------------------------------------------
       
   353 // CheckNetworkStatusAndSetObserverL()
       
   354 // ----------------------------------------------------------------------------
       
   355 //
       
   356 void CAlwaysOnlineManager::CheckNetworkStatusAndSetObserverL()
       
   357     {
       
   358     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::CheckNetworkStatusAndSetObserverL()");
       
   359     TRequestStatus status;
       
   360     TInt registrationStatus( 0 );
       
   361 
       
   362     //check network status
       
   363     iConMon.GetIntAttribute(
       
   364         EBearerIdGSM, 0, KNetworkRegistration, 
       
   365         registrationStatus, status );
       
   366 
       
   367     User::WaitForRequest( status );		// CSI: 94 # This is server side context
       
   368 
       
   369     if ( status.Int() == KErrNone )
       
   370         {
       
   371         HandleRegistrationStatusL( registrationStatus );
       
   372         }
       
   373 
       
   374     //sets this as an observer for networkstatus
       
   375     iConMon.NotifyEventL(*this);
       
   376     
       
   377     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::CheckNetworkStatusAndSetObserverL()");
       
   378     }
       
   379 
       
   380 // ----------------------------------------------------------------------------
       
   381 // EventL()
       
   382 // ----------------------------------------------------------------------------
       
   383 //
       
   384 void CAlwaysOnlineManager::EventL( const CConnMonEventBase &aConnMonEvent )
       
   385     {
       
   386     switch ( aConnMonEvent.EventType() )
       
   387 		{
       
   388         case EConnMonNetworkRegistrationChange:
       
   389             {
       
   390 			CConnMonNetworkRegistrationChange* eventNetworkRegChange;
       
   391 			eventNetworkRegChange = ( CConnMonNetworkRegistrationChange* ) &aConnMonEvent;
       
   392 		        
       
   393 			TInt regStatus = eventNetworkRegChange->RegistrationStatus();
       
   394 
       
   395             HandleRegistrationStatusL( regStatus );
       
   396     
       
   397             }
       
   398 			break;
       
   399 		default:
       
   400 			break;
       
   401 		}; 
       
   402 
       
   403     }
       
   404 
       
   405 // ----------------------------------------------------------------------------
       
   406 // HandleRegistrationStatusL()
       
   407 // ----------------------------------------------------------------------------
       
   408 //
       
   409 void CAlwaysOnlineManager::HandleRegistrationStatusL( TInt aStatus )
       
   410     {
       
   411     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleRegistrationStatusL()");
       
   412     TInt command;
       
   413 
       
   414     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleRegistrationStatusL Reporting Network Status to plugins. aStatus: %d", aStatus );
       
   415     iNetworkStatus = aStatus;
       
   416 
       
   417     if ( aStatus == ENetworkRegistrationHomeNetwork )
       
   418         {
       
   419         command = EAOManagerStoppedRoaming;
       
   420         }
       
   421     else if ( aStatus == ENetworkRegistrationRoaming )
       
   422         {
       
   423         command = EAOManagerStartedRoaming;
       
   424         }
       
   425     else
       
   426         {
       
   427         // lets tell plugins we are offline while we don't know network status so that they
       
   428         // won't try any connections...
       
   429         KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleRegistrationStatusL() 2");
       
   430         return; //we don't care about other network states than these two
       
   431         }
       
   432 
       
   433     TInt count = iPluginArray->Count();
       
   434 
       
   435     //send changed status to all plugins
       
   436     TBuf8<1> dummyParam;
       
   437     for ( TInt i = 0; i<count; i++ )
       
   438         {
       
   439         InvokeCommandHandlerL( 
       
   440             static_cast<TManagerServerCommands>(command), dummyParam, i );
       
   441         }
       
   442     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleRegistrationStatusL()");
       
   443     }
       
   444 
       
   445 // ----------------------------------------------------------------------------
       
   446 // InvokeCommandHandlerL
       
   447 // ----------------------------------------------------------------------------
       
   448 //
       
   449 void CAlwaysOnlineManager::InvokeCommandHandlerL(
       
   450     const TManagerServerCommands aCommand,
       
   451     TDes8& aParameter,
       
   452     TInt aIndex )
       
   453     {
       
   454     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::InvokeCommandHandlerL()");
       
   455     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::InvokeCommandHandlerL() CommandHandler invoked for TManagerServerCommand: %d", aCommand);
       
   456     CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL(*this);
       
   457     CleanupStack::PushL( watcher );
       
   458 
       
   459     if ( !iSession )
       
   460         {
       
   461         KAOMANAGER_LOGGER_WRITE("CAlwaysOnlineManager::InvokeCommandHandlerL() iSession is NULL");
       
   462         iSession = CMsvSession::OpenSyncL(*this);
       
   463         KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::InvokeCommandHandlerL() iSession created to 0x%x", iSession );
       
   464         }
       
   465 
       
   466     CAOServerCommandHandler* op = CAOServerCommandHandler::NewL( 
       
   467         *iSession,
       
   468         watcher->iStatus,
       
   469         aCommand,
       
   470         aParameter,
       
   471         *iPluginArray->At(aIndex) );
       
   472     CleanupStack::PushL( op );
       
   473     iOperations->AppendL( watcher );
       
   474     watcher->SetOperation( op ); // takes immediately ownership
       
   475 
       
   476     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::InvokeCommandHandlerL() Async command invoked. Operation ID: %d ",op->Id()  );
       
   477 
       
   478     CleanupStack::Pop( 2, watcher ); // CSI: 47,12 # op, watcher
       
   479     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::InvokeCommandHandlerL()");
       
   480     }
       
   481 
       
   482 // ----------------------------------------------------------------------------
       
   483 // HandleDiskSpaceEventL()
       
   484 // ----------------------------------------------------------------------------
       
   485 //
       
   486 void CAlwaysOnlineManager::HandleDiskSpaceEventL( TInt aEvent )
       
   487     {
       
   488     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleDiskSpaceEventL()");
       
   489     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleDiskSpaceEventL() Got DiskSpace event: %d, Forwarding to plugins", aEvent );
       
   490 
       
   491     // Make sure there actually is a reason to send the event
       
   492     if ( aEvent == iLastDiskEvent )
       
   493         {
       
   494         return;
       
   495         }
       
   496         
       
   497     iLastDiskEvent = aEvent;       
       
   498 
       
   499     TInt count = iPluginArray->Count();
       
   500 
       
   501     //send event to all plugins
       
   502     TBuf8<1> dummyParam;
       
   503     for( TInt i = 0; i<count; i++ )
       
   504         {
       
   505         InvokeCommandHandlerL( 
       
   506             static_cast<TManagerServerCommands>(aEvent), dummyParam, i );
       
   507         }
       
   508     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleDiskSpaceEventL()");
       
   509     }
       
   510 
       
   511 
       
   512 // ----------------------------------------------------------------------------
       
   513 // CAlwaysOnlineManager::HandleSessionEventL
       
   514 // ----------------------------------------------------------------------------
       
   515 //
       
   516 void CAlwaysOnlineManager::HandleSessionEventL(
       
   517     TMsvSessionEvent aEvent,
       
   518     TAny* /*aArg1*/,
       
   519     TAny* /*aArg2*/,
       
   520     TAny* /*aArg3*/)
       
   521     {
       
   522     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleSessionEventL");
       
   523     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleSessionEventL() aEvent %d", aEvent);
       
   524     switch( aEvent )
       
   525         {
       
   526         case EMsvMediaUnavailable:
       
   527             //delete diskspace observer
       
   528             if( iDiskSpaceObserver )
       
   529                 {
       
   530                 iDiskSpaceObserver->Cancel();
       
   531                 delete iDiskSpaceObserver;
       
   532                 iDiskSpaceObserver = NULL;            
       
   533                 }
       
   534             break;
       
   535         case EMsvMediaAvailable:
       
   536             //restart diskspace observer
       
   537             if( !iDiskSpaceObserver )
       
   538                 {
       
   539                 iDiskSpaceObserver = 
       
   540                     CAlwaysOnlineDiskSpaceObserver::NewL( this, *iSession );
       
   541                 iDiskSpaceObserver->SetLimitAndActivateL();
       
   542                 }            
       
   543             break;
       
   544         case EMsvMediaChanged:
       
   545             //if media changed, old observer is obsolete, delete and restart.
       
   546             if( iDiskSpaceObserver )
       
   547                 {
       
   548                 iDiskSpaceObserver->Cancel();
       
   549                 delete iDiskSpaceObserver;
       
   550                 iDiskSpaceObserver = NULL;
       
   551 
       
   552                 iDiskSpaceObserver = 
       
   553                     CAlwaysOnlineDiskSpaceObserver::NewL( this, *iSession );
       
   554                 iDiskSpaceObserver->SetLimitAndActivateL();
       
   555                 }
       
   556             break;
       
   557 
       
   558         case EMsvCloseSession:
       
   559         case EMsvServerTerminated:
       
   560             iOperations->ResetAndDestroy();
       
   561             delete iSession;
       
   562             iSession = NULL;
       
   563             break;
       
   564 
       
   565 
       
   566         default:
       
   567             break;
       
   568         }
       
   569         
       
   570     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleSessionEventL");
       
   571     }
       
   572 
       
   573 // ----------------------------------------------------------------------------
       
   574 // CAlwaysOnlineManager::BroadcastClientCommandL
       
   575 // ----------------------------------------------------------------------------
       
   576 //
       
   577 void CAlwaysOnlineManager::BroadcastClientCommandL( 
       
   578     TAlwaysOnlineServerAPICommands aCommand, 
       
   579     TDes8&                         aParameter )
       
   580     {
       
   581     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::BroadcastClientCommandL()");
       
   582     KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::BroadcastClientCommandL() aCommand: %d", aCommand);
       
   583     // Translate TAlwaysOnlineServerAPICommands to TManagerServerCommands
       
   584     TManagerServerCommands command = (TManagerServerCommands)KErrNotFound;
       
   585     TBool invoke = ETrue;
       
   586 
       
   587     switch( aCommand )
       
   588         {
       
   589         
       
   590         // Start a plugin
       
   591         case EServerAPIBaseCommandStart:
       
   592             HandleStartPluginL( aParameter );
       
   593             command = EAOManagerPluginStart;
       
   594             invoke = EFalse;
       
   595             break;
       
   596         
       
   597         // Stop a plugin
       
   598         case EServerAPIBaseCommandStop:
       
   599             HandleStopPluginL( aParameter );
       
   600             command = EAOManagerPluginStop;
       
   601             invoke = EFalse;
       
   602             break;
       
   603         
       
   604         case EServerAPIBaseCommandAoSchedulerError:
       
   605             KAOMANAGER_LOGGER_WRITE("CAlwaysOnlineManager::BroadcastClientCommandL() AOScheduler ERROR!" );
       
   606             command = EAOManagerAOSchdulerError;
       
   607             break;
       
   608             
       
   609         // Suspend email agent
       
   610         case EServerAPIEmailTurnOff:
       
   611             command = EAOManagerMailboxAgentSuspend;
       
   612             break;
       
   613 
       
   614         // Turn the email agent back on
       
   615         case EServerAPIEmailTurnOn:
       
   616             command = EAOManagerMailboxAgentResume;
       
   617             break;
       
   618         
       
   619         case EServerAPIBaseAllowNWOperations:
       
   620             invoke = EFalse;
       
   621             break;
       
   622         case EServerAPIBaseSuspendNWOperations:
       
   623             invoke = EFalse;
       
   624             break;
       
   625         // Remove the agent completely
       
   626         case EServerAPIEmailAgentRemove:
       
   627             command = EAOManagerMailboxAgentRemove;
       
   628             break;
       
   629 
       
   630         // Handle the OMA Email Notification
       
   631         case EServerAPIEmailEMNReceived:
       
   632             command = EAOManagerEMNReceived;
       
   633             break;
       
   634 
       
   635         // Start temporary agent for manual IMAP4 connection
       
   636         case EServerAPIEmailUpdateMailWhileConnected:
       
   637             command = EAOManagerMailboxAgentUpdateMailWhileConnected;
       
   638             break;
       
   639         case EServerAPIEmailQueryState:
       
   640             command = EAOManagerQueryState;
       
   641             invoke = EFalse;
       
   642             BroadcastCommandDirectL( command, aParameter );
       
   643             break;
       
   644         case EServerAPIEmailCancelAllAndDisconnect:
       
   645             command = EAOManagerCancelAllAndDisconnect;
       
   646             break;
       
   647         case EServerAPIEmailCancelAllAndDoNotDisconnect:
       
   648             command = EAOManagerCancelAllAndDoNotDisconnect;
       
   649             break;
       
   650         case EServerAPIEmailSuspend:
       
   651             command = EAOManagerSuspend;
       
   652             break;
       
   653         case EServerAPIEmailContinue:
       
   654             command = EAOManagerContinue;
       
   655             break;
       
   656         case EServerAPIEmailDoNotDisconnect:
       
   657             command = EAOManagerDoNotDisconnect;
       
   658             break;
       
   659         case EServerAPIEmailSessionClosed:
       
   660             command = EAOManagerSessionClosed;
       
   661             invoke = EFalse;
       
   662             BroadcastCommandDirectL( command, aParameter );
       
   663             break;
       
   664         case EServerAPIEmailDisableAOEmailPlugin:
       
   665         	// Remove UID from plugin array and delete the instance
       
   666         	// of the email plugin
       
   667         	iCenRepControl->SetPluginStatus( KAOEmailPluginUid, ETrue );
       
   668         	for( TInt i = 0; i < iPluginArray->Count(); i++ )
       
   669 	        	{
       
   670 	        	if( KAOEmailPluginUid == iPluginArray->At(i)->InstanceUid() )
       
   671 	        		{
       
   672 	                delete iPluginArray->At( i );
       
   673 	                // Delete element
       
   674 	                iPluginArray->Delete( i );	        		
       
   675 	        		}
       
   676 	        	}
       
   677         	break;
       
   678         case EServerAPIEmailEnableAOEmailPlugin:
       
   679         	iCenRepControl->SetPluginStatus( KAOEmailPluginUid, EFalse );
       
   680         	break;
       
   681             
       
   682         // Ignore unrecognized command
       
   683         default:
       
   684             invoke = EFalse;
       
   685         };
       
   686 
       
   687     // EServerAPIBaseCommandStart and EServerAPIBaseCommandStop is only
       
   688     // sent to one plugin and at this point, it is already handled in
       
   689     // their own cases.
       
   690     if ( invoke )
       
   691         {
       
   692         TInt count = iPluginArray->Count();
       
   693 
       
   694         // Send event to all plugins
       
   695         for( TInt i = 0; i<count; i++ )
       
   696             {
       
   697             InvokeCommandHandlerL( 
       
   698                 static_cast<TManagerServerCommands>(command), aParameter, i );
       
   699             }
       
   700         }
       
   701     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::BroadcastClientCommandL()");
       
   702     }
       
   703 
       
   704 // ----------------------------------------------------------------------------
       
   705 // ----------------------------------------------------------------------------
       
   706 //
       
   707 void CAlwaysOnlineManager::BroadcastCommandDirectL( 
       
   708     TManagerServerCommands aCommand, 
       
   709     TDes8&                         aParameter )
       
   710     {
       
   711     TInt count = iPluginArray->Count();
       
   712     TInt ret = KErrNone;
       
   713     // Send event to all plugins
       
   714     for( TInt i = 0; i<count; i++ )
       
   715         {
       
   716         CAlwaysOnlineEComInterface* plugin = iPluginArray->At(i);
       
   717         TAny* result = plugin->HandleServerCommandL( aCommand, &aParameter );
       
   718         ret = reinterpret_cast<TInt>(result);
       
   719         }
       
   720     
       
   721     if ( ret != KErrNone )
       
   722         {
       
   723         User::Leave( ret );
       
   724         }
       
   725 
       
   726     }
       
   727 
       
   728 // ----------------------------------------------------------------------------
       
   729 // CAlwaysOnlineManager::OpCompleted
       
   730 // ----------------------------------------------------------------------------
       
   731 //
       
   732 void CAlwaysOnlineManager::OpCompleted(
       
   733     CMsvSingleOpWatcher& aOpWatcher,
       
   734     TInt /*aCompletionCode*/ )
       
   735     {
       
   736     TMsvOp opId = aOpWatcher.Operation().Id();
       
   737     const TInt count = iOperations->Count();  
       
   738 
       
   739     for ( TInt i=0; i < count; i++ )
       
   740         {
       
   741         CMsvOperation& op = (*iOperations)[i]->Operation();
       
   742 
       
   743         if ( op.Id() == opId )
       
   744             {
       
   745             KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::OpCompleted() Async operation %d completed ", opId );
       
   746             CMsvSingleOpWatcher* operation = (*iOperations)[i];
       
   747 
       
   748             // Attempt to handle operation
       
   749             TRAPD( err, HandleOpCompletionL( op.FinalProgress() ) );
       
   750             if ( err != KErrNone )
       
   751                 {
       
   752                 KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::OpCompleted() failed err:%d", err );
       
   753                 }
       
   754             
       
   755             delete operation;
       
   756             iOperations->Delete(i);
       
   757             KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::OpCompleted() Operations remaining: %d ", iOperations->Count() );
       
   758             break;
       
   759             }
       
   760         }
       
   761     }
       
   762     
       
   763 // ----------------------------------------------------------------------------
       
   764 // CAlwaysOnlineManager::HandleOpCompletionL
       
   765 // ----------------------------------------------------------------------------
       
   766 //
       
   767 void CAlwaysOnlineManager::HandleOpCompletionL( const TDesC8& aProgress )
       
   768     {
       
   769     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleOpCompletionL");
       
   770     
       
   771     TUid id;
       
   772     TInt command;
       
   773     TBuf8<KAOFinalProgressLength> result;
       
   774     
       
   775     if ( ParseProgressL( aProgress, id, command, result ) )
       
   776         {
       
   777         KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleOpCompletionL() : plugin: 0x%x", id);
       
   778         KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleOpCompletionL() : command: %d", command);
       
   779         KAOMANAGER_LOGGER_WRITE_FORMAT8("CAlwaysOnlineManager::HandleOpCompletionL() : result: %S", &result);
       
   780         
       
   781         switch ( static_cast<TManagerServerCommands>( command ) )
       
   782             {
       
   783             case EAOManagerSuicideQuery:
       
   784                 if ( result.Match( KStopPlugin ) > KErrNotFound )
       
   785                     {
       
   786                     StopPluginL( id );
       
   787                     }
       
   788                 break;
       
   789             case EAOManagerPluginStop:
       
   790                 DisablePlugin( id );
       
   791                 break;
       
   792             default:
       
   793                 // Currently, there is no other commands for plugins, which 
       
   794                 // returns something useful to server.
       
   795                 break;
       
   796             }    
       
   797         }
       
   798     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleOpCompletionL");
       
   799     }
       
   800 
       
   801 // ----------------------------------------------------------------------------
       
   802 // CAlwaysOnlineManager::ParseProgressL
       
   803 // ----------------------------------------------------------------------------
       
   804 //
       
   805 TBool CAlwaysOnlineManager::ParseProgressL( 
       
   806         const TDesC8& aProgress, 
       
   807         TUid& aUid,
       
   808         TInt& aCommand, 
       
   809         TDes8& aResult )
       
   810     {
       
   811     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::ParseProgressL");
       
   812     TBool parseOk = EFalse;
       
   813     
       
   814     // Parse final progress, received from CAOServerCommandHandler::FinalProgress()
       
   815     CAOCommandParser* parser = NULL;
       
   816     TRAPD( err, parser = CAOCommandParser::NewL( aProgress ) );
       
   817     
       
   818     if ( err == KErrNone )
       
   819         {
       
   820         CleanupStack::PushL( parser );
       
   821         
       
   822         TInt error = parser->Parse();
       
   823 
       
   824         __ASSERT_DEBUG( error != KErrNotFound, User::Panic( KAOManagerParsePanic, error ) );
       
   825         
       
   826         if ( error != KErrNotFound )
       
   827             {
       
   828             aUid = parser->Uid();
       
   829             aCommand = parser->Command();
       
   830             aResult = parser->Result().Left( aResult.MaxLength() );
       
   831             parseOk = ETrue;
       
   832             }
       
   833         
       
   834         CleanupStack::PopAndDestroy( parser );
       
   835         }
       
   836         
       
   837     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::ParseProgressL");
       
   838     
       
   839     return parseOk;
       
   840     }
       
   841 
       
   842 // ----------------------------------------------------------------------------
       
   843 // CAlwaysOnlineManager::HandleStartPluginL
       
   844 // ----------------------------------------------------------------------------
       
   845 //
       
   846 void CAlwaysOnlineManager::HandleStartPluginL( const TDes8& aParameter )
       
   847     {
       
   848     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleStartPluginL");
       
   849     
       
   850     TPckgBuf<TUid> paramPack;
       
   851     TUid pluginId;
       
   852     TInt error = KErrNone;
       
   853     
       
   854     // Make sure that the parameter length matches Id length
       
   855     if ( aParameter.Length() == sizeof( TUid ) )
       
   856         {
       
   857         paramPack.Copy( aParameter );
       
   858         
       
   859         // Get the mailbox id from the packet
       
   860         pluginId = paramPack();
       
   861         error = KErrNone;
       
   862         }
       
   863     else
       
   864         {
       
   865         error = KErrNotFound;
       
   866         }
       
   867 
       
   868     // Check if plugin already loaded.
       
   869     for ( TInt i = 0; i < iPluginArray->Count(); i++ )
       
   870         {
       
   871         if ( pluginId == iPluginArray->At( i )->InstanceUid() )
       
   872             {
       
   873             KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleStartPluginL() plugin already loaded: 0x%x", pluginId);
       
   874             // No need to load a plugin again.
       
   875             error = KErrAlreadyExists;
       
   876             break;
       
   877             }
       
   878         }
       
   879 
       
   880     // Load the plugin and call start for it.            
       
   881     CAlwaysOnlineEComInterface* implementation = NULL;
       
   882 
       
   883     if ( error == KErrNone )
       
   884         {
       
   885         TRAPD( err, implementation = 
       
   886             CAlwaysOnlineEComInterface::NewL( pluginId ) );
       
   887         
       
   888         if ( err == KErrNone )
       
   889             {
       
   890             implementation->SetStatusQueryObject( this );
       
   891             iPluginArray->AppendL( implementation );
       
   892             
       
   893             // Set the loaded plugin to be enabled (== delete UID from cenrep).
       
   894             iCenRepControl->SetPluginStatus( pluginId, EFalse );
       
   895             
       
   896             // Refresh the list of disabled plugins UIDs.
       
   897             iCenRepControl->UpdateDisabledPluginsUids( 
       
   898                 *iDisabledPluginUidsArray );
       
   899             
       
   900             TBuf8<1> dummyParam;
       
   901             // AppendL puts object to the end of the array, 
       
   902             // so we can start the last plugin
       
   903             TInt idx = iPluginArray->Count() - 1;
       
   904             InvokeCommandHandlerL( EAOManagerPluginStart, dummyParam, idx );
       
   905             
       
   906             KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleStartPluginL() plugin loaded succesfully: 0x%x", pluginId);
       
   907             }
       
   908         else
       
   909             {
       
   910             KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleStartPluginL() plugin load failed: 0x%x", pluginId);
       
   911             }
       
   912         }
       
   913     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleStartPluginL");
       
   914     }
       
   915 
       
   916 // ----------------------------------------------------------------------------
       
   917 // CAlwaysOnlineManager::HandleStopPluginL
       
   918 // ----------------------------------------------------------------------------
       
   919 //
       
   920 void CAlwaysOnlineManager::HandleStopPluginL( const TDes8& aParameter )
       
   921     {
       
   922     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::HandleStopPluginL");
       
   923     
       
   924     TPckgBuf<TUid> paramPack;
       
   925     TUid pluginId;
       
   926     
       
   927     // Make sure that the parameter length matches Id length
       
   928     if ( aParameter.Length() == sizeof( TUid ) )
       
   929         {
       
   930         paramPack.Copy( aParameter );
       
   931         
       
   932         // Get the mailbox id from the packet
       
   933         pluginId = paramPack();
       
   934         StopPluginL( pluginId );
       
   935         }
       
   936         
       
   937     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::HandleStopPluginL");
       
   938     }
       
   939 
       
   940 // ----------------------------------------------------------------------------
       
   941 // CAlwaysOnlineManager::StopPluginL
       
   942 // ----------------------------------------------------------------------------
       
   943 //
       
   944 void CAlwaysOnlineManager::StopPluginL(
       
   945     const TUid& aPlugin )
       
   946     {
       
   947     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::StopPluginL");
       
   948     
       
   949     // Find the plugin and send stop command.
       
   950     if ( iPluginArray->Count() )
       
   951         {
       
   952         // Get correct plugin from 
       
   953         for ( TInt i = 0; i < iPluginArray->Count(); i++ )
       
   954             {
       
   955             TUid id = ( iPluginArray->At( i ) )->InstanceUid();
       
   956 
       
   957             // Is it this plugin?                
       
   958             if ( id == aPlugin )
       
   959                 {
       
   960                 KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::StopPluginL() Calling stop to plugin: 0x%x", aPlugin);
       
   961                 TBuf8<1> dummyParam;
       
   962                 InvokeCommandHandlerL( 
       
   963                     static_cast<TManagerServerCommands>( EAOManagerPluginStop ), 
       
   964                     dummyParam, 
       
   965                     i );    
       
   966                 }
       
   967             }
       
   968         }
       
   969         
       
   970     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::StopPluginL");
       
   971     }
       
   972 
       
   973 // ----------------------------------------------------------------------------
       
   974 // CAlwaysOnlineManager::DisablePlugin
       
   975 // ----------------------------------------------------------------------------
       
   976 //
       
   977 void CAlwaysOnlineManager::DisablePlugin( const TUid& aPlugin )
       
   978     {
       
   979     KAOMANAGER_LOGGER_FN1("CAlwaysOnlineManager::DisablePlugin");
       
   980     
       
   981     // Delete the plugin and mark it disabled.
       
   982     if ( iPluginArray->Count() )
       
   983         {
       
   984         // Get correct plugin from 
       
   985         for ( TInt i = 0; i < iPluginArray->Count(); i++ )
       
   986             {
       
   987             TUid id = ( iPluginArray->At( i ) )->InstanceUid();
       
   988             
       
   989             // Is it this plugin?
       
   990             if ( id == aPlugin )
       
   991                 {
       
   992                 KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::DisablePlugin() Deleting plugin from array: 0x%x", aPlugin);
       
   993                 // Delete object
       
   994                 delete iPluginArray->At( i );
       
   995                 // Delete element
       
   996                 iPluginArray->Delete( i );
       
   997                 }
       
   998             }
       
   999             // Set this plugin as disabled.
       
  1000             iCenRepControl->SetPluginStatus( aPlugin, ETrue );
       
  1001             
       
  1002             // Refresh the list of disabled plugins UIDs.
       
  1003             iCenRepControl->UpdateDisabledPluginsUids( 
       
  1004                 *iDisabledPluginUidsArray );
       
  1005         }
       
  1006     
       
  1007     KAOMANAGER_LOGGER_FN2("CAlwaysOnlineManager::DisablePlugin");
       
  1008     }
       
  1009 
       
  1010 // ----------------------------------------------------------------------------
       
  1011 // CAlwaysOnlineManager::HandleNotifyInt
       
  1012 // ----------------------------------------------------------------------------    
       
  1013 //
       
  1014 void CAlwaysOnlineManager::HandleNotifyInt( 
       
  1015     TUint32 /* aId */, 
       
  1016     TInt aNewValue )
       
  1017     {   
       
  1018     TRAPD( err, HandleOfflineEventL( aNewValue ) );
       
  1019     if( err != KErrNone )
       
  1020         {
       
  1021         KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleNotifyInt() failed err:%d", err );
       
  1022         }
       
  1023     }
       
  1024 
       
  1025 // ----------------------------------------------------------------------------
       
  1026 // CAlwaysOnlineManager::HandleNotifyError
       
  1027 // ----------------------------------------------------------------------------    
       
  1028 //
       
  1029 void CAlwaysOnlineManager::HandleNotifyError( 
       
  1030     TUint32 /* aId */, 
       
  1031     TInt /* aError */, 
       
  1032     CCenRepNotifyHandler* /* aHandler */ )
       
  1033     {
       
  1034     TRAPD( err, iCenRepControl->ReCreateCoreAppUiCenRepSessionL() );
       
  1035     if( err != KErrNone )
       
  1036         {
       
  1037         KAOMANAGER_LOGGER_WRITE_FORMAT("CAlwaysOnlineManager::HandleNotifyError() failed err:%d", err );
       
  1038         }
       
  1039     }
       
  1040 
       
  1041 //EOF