pimappservices/calendar/server/src/agsmain.cpp
changeset 0 f979ecb2b13e
child 12 38571fd2a704
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2001-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 "agsmain.h"
       
    17 
       
    18 #include <bacline.h>
       
    19 #include <e32property.h>
       
    20 
       
    21 #include "agsalarm.h"
       
    22 #include "agmtlsproxy.h"
       
    23 #include "agmserv.h"
       
    24 #include "agssess.h"
       
    25 #include "agsentrymodel.h"
       
    26 #include "agsfilemanager.h"
       
    27 #include "agssignal.h"
       
    28 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
    29 #include "agssystemstateobserver.h"
       
    30 #endif
       
    31 #include <calnotification.h>
       
    32 
       
    33 static _LIT_SECURITY_POLICY_PASS(KReadPolicy);
       
    34 static _LIT_SECURITY_POLICY_S0(KWritePolicy, 0x10003a5b);
       
    35 
       
    36 /**
       
    37  @internalComponent
       
    38  */
       
    39 CAgnServUndertaker* CAgnServUndertaker::NewL()
       
    40 	{
       
    41 	CAgnServUndertaker* self = new(ELeave)CAgnServUndertaker();
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL();
       
    44 	CleanupStack::Pop(self);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 
       
    49 
       
    50 // policy element constants
       
    51 #define KPolicyElementReadUserData  		0
       
    52 #define KPolicyElementWriteUserData  		1
       
    53 
       
    54 const TUint KRangeCount = 4; 
       
    55 
       
    56 const TInt KOpCodeRanges[KRangeCount] = 
       
    57 	{	
       
    58 	KCapabilityNone,
       
    59 	KCapabilityReadUserData,
       
    60 	KCapabilityWriteUserData,
       
    61 	EAgnNotSupported,
       
    62 	};
       
    63 
       
    64 const TUint8 KElementsIndex[KRangeCount] =
       
    65 	{
       
    66 	CPolicyServer::EAlwaysPass,		
       
    67 	KPolicyElementReadUserData,		
       
    68 	KPolicyElementWriteUserData,	
       
    69 	CPolicyServer::ENotSupported	
       
    70 	};
       
    71 
       
    72 const CPolicyServer::TPolicyElement KPolicyElements[] = 
       
    73 	{
       
    74 	{_INIT_SECURITY_POLICY_C1(ECapabilityReadUserData), CPolicyServer::EFailClient}, // KPolicyElementReadUserData
       
    75 	{_INIT_SECURITY_POLICY_C1(ECapabilityWriteUserData), CPolicyServer::EFailClient}, // KPolicyElementWriteUserData
       
    76 	};
       
    77 
       
    78 
       
    79 const CPolicyServer::TPolicy KAgnSrvrPolicy =
       
    80 	{
       
    81 	CPolicyServer::EAlwaysPass, //specifies all connect attempts should pass
       
    82 	KRangeCount,
       
    83 	KOpCodeRanges,
       
    84 	KElementsIndex, 	// what each range is compared to 
       
    85 	KPolicyElements 	// what policies range is compared to
       
    86 	};
       
    87 
       
    88 void CAgnServUndertaker::ConstructL()
       
    89 	{
       
    90 	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
       
    91 	CActiveScheduler::Install(scheduler);
       
    92 
       
    93 	iServer = CAgnServer::InitL(this);
       
    94 	CActiveScheduler::Add(this);
       
    95 	}
       
    96 
       
    97 void CAgnServUndertaker::DoCancel()
       
    98 	{
       
    99 	}
       
   100 void CAgnServUndertaker::RunL()
       
   101 	{
       
   102 	if (!iCancelled)
       
   103 		{
       
   104 		Deque();
       
   105 		delete iServer;
       
   106 
       
   107 		CActiveScheduler::Stop();
       
   108 		}
       
   109 	}
       
   110 
       
   111 
       
   112 void CAgnServUndertaker::DestroyServer()
       
   113 	{
       
   114 	// Set the undertaker active, and make a request
       
   115 	if (!IsActive())
       
   116 		{
       
   117 		SetActive();
       
   118 		TRequestStatus* status = &iStatus;
       
   119 		User::RequestComplete(status,KErrNone);
       
   120 		}
       
   121 
       
   122 	iCancelled = EFalse;
       
   123 	}
       
   124 
       
   125 void CAgnServUndertaker::CancelUndertaker()
       
   126 	{
       
   127 	iCancelled = ETrue;
       
   128 	}
       
   129 
       
   130 // CAgnAlarmServerChangeListener
       
   131 //
       
   132 CAgnAlarmServerChangeListener* CAgnAlarmServerChangeListener::NewL(RASCliSession& aAlarmServer, CAgnServFileMgr& aFileMgr)
       
   133     {
       
   134     CAgnAlarmServerChangeListener* self = new(ELeave) CAgnAlarmServerChangeListener(aAlarmServer, aFileMgr);
       
   135     CleanupStack::PushL(self);
       
   136     self->ConstructL();
       
   137     CleanupStack::Pop(self);
       
   138     return (self);
       
   139     }
       
   140 
       
   141 CAgnAlarmServerChangeListener::CAgnAlarmServerChangeListener(RASCliSession& aAlarmServer, CAgnServFileMgr& aFileMgr)
       
   142     :CActive(EPriorityLow), iAlarmServer(aAlarmServer), iFileMgr(aFileMgr)
       
   143     {
       
   144     CActiveScheduler::Add(this);
       
   145     }
       
   146     
       
   147 void CAgnAlarmServerChangeListener::ConstructL()
       
   148     {
       
   149     SetActive();
       
   150     iAlarmServer.NotifyChange(iStatus, iAlarmId);
       
   151     }
       
   152 
       
   153 
       
   154 CAgnAlarmServerChangeListener::~CAgnAlarmServerChangeListener()
       
   155     {
       
   156     Cancel();
       
   157     }
       
   158 
       
   159 #ifdef __CAL_VERBOSE_LOGGING__
       
   160 TInt CAgnAlarmServerChangeListener::RunError(TInt aError)
       
   161     {
       
   162     AgmDebug::DebugLog("CAgnAlarmServerChangeListener::RunError has error %d", aError);
       
   163     return ( KErrNone );
       
   164     }
       
   165 #else
       
   166 TInt CAgnAlarmServerChangeListener::RunError(TInt /*aError*/)
       
   167     {
       
   168     return ( KErrNone );
       
   169     }
       
   170 #endif
       
   171 
       
   172 
       
   173 void CAgnAlarmServerChangeListener::RunL()
       
   174     {  
       
   175     TInt change(iStatus.Int());
       
   176     
       
   177     if (iStatus.Int() != KErrServerTerminated)
       
   178         {
       
   179         SetActive();
       
   180         iAlarmServer.NotifyChange(iStatus, iAlarmId);
       
   181         }
       
   182     
       
   183     // Tell each file that there has been a change in the alarm server
       
   184     const TInt KFileCount(iFileMgr.Count());
       
   185     
       
   186     for (TInt i(0) ; i < KFileCount ; ++i)
       
   187         {
       
   188         iFileMgr.File(i)->Model()->Alarm().ChangeObserver().HandleAlarmServerChangeL(change);
       
   189         }
       
   190     }
       
   191 
       
   192 
       
   193 void CAgnAlarmServerChangeListener::DoCancel()
       
   194     {
       
   195     if (iStatus == KRequestPending)
       
   196         {
       
   197         iAlarmServer.NotifyChangeCancel();
       
   198         }
       
   199     }
       
   200 
       
   201 
       
   202 
       
   203 // Initialize the server - create a CAgnServer object, and an
       
   204 // active scheduler
       
   205 
       
   206 CAgnServer* CAgnServer::InitL(CAgnServUndertaker* aUndertaker)
       
   207 	{
       
   208 	// start server
       
   209 	CAgnServer* server = CAgnServer::NewL();
       
   210 	server->iUndertaker = aUndertaker;
       
   211 	return server;
       
   212 	}
       
   213 
       
   214 
       
   215 CAgnServer::CAgnServer(TInt aPriority)
       
   216  : CPolicyServer(aPriority, KAgnSrvrPolicy, ESharableSessions), iServerMode(ETransientServer)
       
   217 	{
       
   218 	}
       
   219 
       
   220 
       
   221 // Create & start a new Agenda server
       
   222 CAgnServer* CAgnServer::NewL()
       
   223 	{
       
   224 	CAgnServer* self = new (ELeave) CAgnServer(KServerPriority);
       
   225 	CleanupStack::PushL(self);
       
   226 	self->ConstructL();
       
   227 
       
   228 
       
   229 
       
   230 	CleanupStack::Pop();		// self
       
   231 
       
   232 	return self;
       
   233 	}
       
   234 
       
   235 CAgnTlsProxy* CAgnServer::TimeZoneConverter()
       
   236 	{
       
   237 	return iProxy;
       
   238 	}
       
   239 
       
   240 // Create a file manager and connect to the file server
       
   241 // Rename the server process to "AgendaServer"
       
   242 // Set up the log file, if logging is required
       
   243 // Start the server
       
   244 //
       
   245 void CAgnServer::ConstructL()
       
   246 	{
       
   247 	CCommandLineArguments* args = CCommandLineArguments::NewLC();
       
   248 	_LIT(KCmdLineArgNonTransientServer, "-nontransient");
       
   249 	const TInt count = args->Count();
       
   250 	for (TInt i = 1; i < count; i++)
       
   251 		{
       
   252 		if (args->Arg(i).CompareF(KCmdLineArgNonTransientServer) == 0)
       
   253 			{
       
   254 			iServerMode = ENonTransientServer;
       
   255 			break;
       
   256 			}
       
   257 		}
       
   258 	CleanupStack::PopAndDestroy(args);	
       
   259 	
       
   260 	iProxy = CAgnTlsProxy::CreateL(CAgnTlsProxy::TAgnTlsTzRulesType_Server);
       
   261 			
       
   262 	User::LeaveIfError(iFs.Connect());
       
   263 	User::LeaveIfError(iAlarmServer.Connect());
       
   264 	iFileMgr = CAgnServFileMgr::NewL(iFs, *this);
       
   265 	iFileMgr->CreatePermanentDataL();
       
   266 	iBackupRestoreAgent = CAgnServBackupRestoreAgent::NewL(*iFileMgr);
       
   267 	iBackupRestoreAgent->Start();
       
   268 	iAlarmServerChangeListener = CAgnAlarmServerChangeListener::NewL(iAlarmServer, *iFileMgr);
       
   269 
       
   270 
       
   271 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   272 	TRAP_IGNORE(iSystemStateObserver = CCAgnSystemStateObserver::NewL(*iFileMgr));
       
   273 #endif
       
   274 	User::LeaveIfError(RThread().RenameMe(KAgendaServerName));
       
   275 
       
   276 	StartL(KAgendaServerName);
       
   277 
       
   278 	TInt err = RProperty::Define(KCalPubSubCategory, ECalPubSubTodoNotification, RProperty::EByteArray, KReadPolicy, KWritePolicy);   
       
   279 	if (err != KErrNone && err != KErrAlreadyExists)
       
   280 	    {
       
   281 	    User::Leave(err);
       
   282 	    }
       
   283 	    
       
   284 	err = RProperty::Define(KCalPubSubCategory, ECalPubSubEventNotification, RProperty::EByteArray, KReadPolicy, KWritePolicy);   
       
   285 	if (err != KErrNone && err != KErrAlreadyExists)
       
   286 	    {
       
   287 	    User::Leave(err);
       
   288 	    }
       
   289 	}
       
   290 
       
   291 
       
   292 CAgnServer::~CAgnServer()
       
   293 	{
       
   294 	delete iAlarmServerChangeListener;
       
   295 	
       
   296 	iAlarmServer.Close();
       
   297 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   298 	delete iSystemStateObserver;
       
   299 #endif
       
   300 	delete iBackupRestoreAgent;
       
   301 	delete iFileMgr;
       
   302 	iFs.Close();
       
   303 	
       
   304 
       
   305 	__ASSERT_DEBUG(iProxy != NULL, User::Invariant());
       
   306 	CAgnTlsProxy::Release(NULL);
       
   307 	}
       
   308 
       
   309 
       
   310 // Create a new server session
       
   311 
       
   312 CSession2* CAgnServer::NewSessionL(const TVersion& aVersion,const RMessage2& /*aMessage*/) const
       
   313 	{
       
   314 	TVersion ver(KAgnServerMajorVersion, KAgnServerMinorVersion, KAgnServerBuildVersion);
       
   315 	if (!User::QueryVersionSupported(ver,aVersion))
       
   316 		User::Leave(KErrNotSupported);
       
   317 
       
   318 	// Call this just in case a previous session caused the undertaker to run
       
   319 	iUndertaker->CancelUndertaker();
       
   320 
       
   321 	CAgnServer* nonConstPtr = const_cast<CAgnServer*>(this);
       
   322 	return CAgnServerSession::NewL(*nonConstPtr);
       
   323 	}
       
   324 
       
   325 
       
   326 void CAgnServer::SessionClosed()
       
   327 	{
       
   328 	// Called when a session exits
       
   329 	// If this is the last session for the server, then close down the active object
       
   330 	iSessionCount--;
       
   331 
       
   332 	ShutdownServer();
       
   333 	}
       
   334 
       
   335 // Try to shutdown the server making sure that no sessions are connected to it, its in transient mode and no file delay timers are running
       
   336 void CAgnServer::ShutdownServer()
       
   337 	{
       
   338 	if (iSessionCount == 0 && (iServerMode == ETransientServer) && !(iFileMgr->FileCloseTimersRunning()))
       
   339 		{
       
   340 		// Destroy the server
       
   341 		iUndertaker->DestroyServer();
       
   342 		}
       
   343 	}
       
   344 
       
   345 void CAgnServer::SessionAdded()
       
   346 	{
       
   347 	iSessionCount++;
       
   348 	}
       
   349 
       
   350 RFs* CAgnServer::FsSession()
       
   351 	{
       
   352 	return &iFs;
       
   353 	}
       
   354 
       
   355 void CAgnServer::FetchSessionsL(RPointerArray<CAgnServerSession>& aSessions)
       
   356   	{
       
   357   	iSessionIter.SetToFirst();
       
   358   	
       
   359   	while (iSessionIter != NULL)
       
   360   		{
       
   361   		aSessions.AppendL(static_cast<CAgnServerSession*>(iSessionIter++));
       
   362   		}
       
   363   	}
       
   364 
       
   365 TAgnServerMode CAgnServer::ServerMode()
       
   366 	{ 
       
   367 	return iServerMode; 
       
   368 	}
       
   369 	
       
   370 const CAgnServBackupRestoreAgent& CAgnServer::BackupRestoreAgent() const
       
   371 	{
       
   372 	return *iBackupRestoreAgent;	
       
   373 	}
       
   374 
       
   375 RASCliSession& CAgnServer::AlarmServer()
       
   376     {
       
   377     return iAlarmServer;    
       
   378     }
       
   379 
       
   380 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   381 const CCAgnSystemStateObserver& CAgnServer::SystemStateObserver() const
       
   382     {
       
   383     return *iSystemStateObserver;
       
   384     }
       
   385 #endif