// EIKALSRV.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include "eikalsrv.h"
#include <t32alrt.h>
#include "eiksrv.pan"
#include <coemain.h>
#include "eikalsup.h"
#include <eiksrv.h>

//
// class CEikServAlarmAlertServer
//

CEikServAlarmAlertServer::CEikServAlarmAlertServer(TInt aPriority) : CServer(aPriority)
	{
	__DECLARE_NAME(_S("CEikServAlarmAlertServer"));
	}

CEikServAlarmAlertServer* CEikServAlarmAlertServer::NewL()	
	{
	CEikServAlarmAlertServer* server=new(ELeave) CEikServAlarmAlertServer(EActivePriorityIpcEventsHigh);
	server->iName=KNameAlarmAlertServer.AllocL();
	User::LeaveIfError(server->Start());
	return server;
	}

CSession* CEikServAlarmAlertServer::NewSessionL(RThread aClient,const TVersion &aVersion) const
	{
	if (iSession)
		aClient.Panic(__EIKON_SERVER, EEikServPanicAlarmAlertServerAlreadyConnected);
	TVersion v(1,0,0); // !! liaise with E32
	if (!User::QueryVersionSupported(v,aVersion))
		User::Leave(KErrNotSupported);
	((CEikServAlarmAlertServer*)this)->iSession=new(ELeave) CEikServAlarmAlertSession(aClient);
	iSession->ConstructL();
	return iSession;
	}

void CEikServAlarmAlertServer::TaskKeyPressed()
	{
	if (iSession)
		iSession->TaskKeyPressed();
	}

void CEikServAlarmAlertServer::HandleSwitchOnEvent()
	{
	if (iSession)
		iSession->HandleSwitchOnEvent();
	}

//
// class CEikServAlarmAlertSession
//

CEikServAlarmAlertSession::CEikServAlarmAlertSession(RThread& aClient)
	: CSession(aClient)
	{
	__DECLARE_NAME(_S("CEikServAlarmAlertSession"));
	}

CEikServAlarmAlertSession::~CEikServAlarmAlertSession()
	{
	CEikServAlarmAlertServer* server=AlarmAlertServer();
	if (server)
		server->SessionDied();
	delete(iAlarmSupervisor);
	}

void CEikServAlarmAlertSession::ConstructL()
	{
	iAlarmSupervisor=new(ELeave) CEikAlmControlSupervisor;
	iAlarmSupervisor->ConstructL();
	}

void CEikServAlarmAlertSession::TaskKeyPressed()
	{
	if (iVisible)
		iAlarmSupervisor->CmdTaskAwayFromAlarm();
	}

void CEikServAlarmAlertSession::HandleSwitchOnEvent()
	{
	iAlarmSupervisor->SynchronizeCountDownTimer();
	}

void CEikServAlarmAlertSession::ServiceL(const RMessage &aMessage)
	{
	TInt opCode=aMessage.Function();
	if (opCode==EAlmOpCodeVisible)
		{
		TBool visible=aMessage.Int0();
		if (visible!=iVisible)
			iVisible=visible;
		}
	iAlarmSupervisor->ServiceL(aMessage);
	}

CEikServAlarmAlertServer* CEikServAlarmAlertSession::AlarmAlertServer() const
	//
	// utility function to return a pointer to the server
	//
	{
	return STATIC_CAST(CEikServAlarmAlertServer*, CONST_CAST(CServer*, Server()));
	}
