pimappservices/calendar/server/src/agssystemstateobserver.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <startupdomaindefs.h>
       
    17 #include <ssm/ssmsubstates.hrh>
       
    18 #include "agssystemstateobserver.h"
       
    19 #include "agmdebug.h"
       
    20 
       
    21 CCAgnSystemStateObserver* CCAgnSystemStateObserver::NewL(CAgnServFileMgr& aFileMgr)
       
    22 	{
       
    23 	CCAgnSystemStateObserver* self = new (ELeave) CCAgnSystemStateObserver(aFileMgr);
       
    24 	CleanupStack::PushL(self);
       
    25 	self->ConstructL();
       
    26 	CleanupStack::Pop(self);
       
    27 	return self;
       
    28 	}
       
    29 
       
    30 CCAgnSystemStateObserver::CCAgnSystemStateObserver (CAgnServFileMgr& aFileMgr) :
       
    31 	CActive(CActive::EPriorityStandard), // Standard priority
       
    32 	iFileMgr(aFileMgr), iShutdown(EFalse)
       
    33 	{
       
    34 	CActiveScheduler::Add(this); // Add to scheduler
       
    35 	}
       
    36 
       
    37 CCAgnSystemStateObserver::~CCAgnSystemStateObserver ()
       
    38 	{
       
    39 	Cancel (); // Cancel any request, if outstanding
       
    40 	iStateAwareSession.Close();
       
    41 	}
       
    42 
       
    43 
       
    44 void CCAgnSystemStateObserver::ConstructL ()
       
    45 	{
       
    46 	iWorkAround = SubscribeToShutdownNotifications();
       
    47 	if (!iWorkAround)
       
    48 		{
       
    49 		//Log and Fail silently if this does not connect.
       
    50 		TInt err = iStateAwareSession.Connect(KSM2AppServicesDomain4);
       
    51 		if (err == KErrNone)
       
    52 			{
       
    53 			iStateAwareSession.RequestStateNotification(iStatus);
       
    54 			SetActive();
       
    55 			}
       
    56 		else
       
    57 			{
       
    58 			_DBGLOG_BASIC(AgmDebug::DebugLog("%d : Failed to connect to the state aware session.", err);)
       
    59 			}
       
    60 		}
       
    61 	}
       
    62 
       
    63 void CCAgnSystemStateObserver::DoCancel ()
       
    64 	{
       
    65 	if (iWorkAround)
       
    66 		{
       
    67 		iNotification.Cancel();
       
    68 		}
       
    69 	else
       
    70 		{
       
    71 		iStateAwareSession.RequestStateNotificationCancel();
       
    72 		}
       
    73 	}
       
    74 
       
    75 void CCAgnSystemStateObserver::RunL ()
       
    76 	{
       
    77 	if (iStatus == KErrNone)
       
    78 		{
       
    79 		TSsmState state;
       
    80 		GetState(state);
       
    81 
       
    82 		if (state.MainState() == ESsmShutdown && state.SubState() == ESsmShutdownSubStateCritical)
       
    83 			{
       
    84 			iShutdown =  ETrue;
       
    85 			// This component only responds to shutdown state and the first substate critical event.
       
    86 			// It does nothing for the subsequent non critical event.
       
    87 
       
    88 			//This does not require clients to have closed sessions
       
    89 			iFileMgr.QueueAlarmsImmediatelyForShutdown();
       
    90 			
       
    91 			//This requires clients to close, however there will not be a delay while shutting down.
       
    92 			iFileMgr.CloseScheduledFilesImmediately();
       
    93 			}
       
    94 		else if (state.MainState() == ESsmNormal && state.SubState() == ESsmNormalSubState)
       
    95 			{
       
    96 			iShutdown = EFalse;
       
    97 			iFileMgr.RequeueAlarmsForShutdownCancellation();
       
    98 			}
       
    99 		AcknowledgeAndRequestStateNotification();
       
   100 		}
       
   101 	}
       
   102 
       
   103 TInt CCAgnSystemStateObserver::RunError (TInt aError)
       
   104 	{
       
   105 	if (aError != KErrNone)
       
   106 		{
       
   107 		_DBGLOG_BASIC(AgmDebug::DebugLog("%d : RunError in CCAgnSystemStateObserver.", aError);)
       
   108 		}
       
   109 	return KErrNone; //Don't want to panic the server.
       
   110 	}
       
   111 
       
   112 TBool CCAgnSystemStateObserver::IsShutdownInProgress() const
       
   113 	{
       
   114 	return iShutdown;
       
   115 	}
       
   116 
       
   117 TBool CCAgnSystemStateObserver::SubscribeToShutdownNotifications()
       
   118 	{
       
   119 	TBool workAround;
       
   120 	TInt shutdownkey = 111;
       
   121 	TUid uid = {0xE6AEDA09}; //test harness uid
       
   122 	TInt value;
       
   123 	workAround = (iNotification.Get(uid, shutdownkey, value) != KErrNotFound);
       
   124 	if (workAround)
       
   125 		{
       
   126 		iNotification.Attach(uid, shutdownkey);
       
   127 		iNotification.Subscribe(iStatus);
       
   128 		SetActive();
       
   129 		}
       
   130 	return workAround;
       
   131 	}
       
   132 
       
   133 void CCAgnSystemStateObserver::ConvertToSystemState(TSsmState& aState)
       
   134 	{
       
   135 	TUid testUid = {0xE6AEDA09};
       
   136 	TInt test_key = 111;
       
   137 	TInt value = 0;
       
   138 	iNotification.Get(testUid, test_key, value);
       
   139 	switch (value)
       
   140 		{
       
   141 	    case 0:
       
   142 		aState.Set(ESsmNormal, ESsmNormalSubState);
       
   143 		break;
       
   144 	    case 1: //ESsmShutdownCritical:
       
   145 		aState.Set(ESsmShutdown, ESsmShutdownSubStateCritical);
       
   146 		break;
       
   147 	    default: 
       
   148 		;
       
   149 		}
       
   150 	}
       
   151 
       
   152 void CCAgnSystemStateObserver::GetState(TSsmState& state)
       
   153 	{
       
   154 	if (iWorkAround)
       
   155 		{
       
   156 		ConvertToSystemState(state);
       
   157 		}
       
   158 	else
       
   159 		{
       
   160 		state = iStateAwareSession.State();
       
   161 		}
       
   162 	}
       
   163 
       
   164 void CCAgnSystemStateObserver::AcknowledgeAndRequestStateNotification()
       
   165 	{
       
   166 	if (iWorkAround)
       
   167 		{
       
   168 		SendSystemStateAck();
       
   169 		}
       
   170 	else
       
   171 		{
       
   172 		iStateAwareSession.AcknowledgeAndRequestStateNotification(KErrNone, iStatus);
       
   173 		SetActive();
       
   174 		}
       
   175 	}
       
   176 
       
   177 void CCAgnSystemStateObserver::SendSystemStateAck()
       
   178 	{
       
   179 	TUid testUid = {0xE6AEDA09};
       
   180 	TInt test_key = 111;
       
   181 	
       
   182 	TInt err = iNotification.Set(testUid, test_key, 2); //2 is Ack enum
       
   183 	iNotification.Subscribe(iStatus);
       
   184 	SetActive();
       
   185 	}