phonebookengines/contactsmodel/cntsrv/src/CCntSession.Cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2005-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 /**
       
    17  @file
       
    18  @internalComponent
       
    19  @released
       
    20 */
       
    21 
       
    22 #include "CCntSession.h"
       
    23 #include "CCntDbManagerController.h"
       
    24 #include "CCntStateMachine.h"
       
    25 #include "CCntBackupRestoreAgent.h"
       
    26 #include "CCntPackager.h"
       
    27 #include "CViewSubSessions.h"
       
    28 #include "CCntIpcCodes.h"
       
    29 #include "CCntRequest.h"
       
    30 #include "CCntEventQueue.h"
       
    31 #include <cntdef.h>  // for tcontactitemid.
       
    32 #include "CIniFileManager.h"
       
    33 #include "CntSpeedDials.h"
       
    34 #include "cntviewprivate.h"
       
    35 #include <cntviewstore.h>
       
    36 #include "CCntLogger.h"
       
    37 #include "CCntMsgHandler.h"
       
    38 #include "CCntItemMsgHandler.h"
       
    39 #include "CCntEventMsgHandler.h"
       
    40 #include "CCntTransactionMsgHandler.h"
       
    41 #include "CCntFileManagerMsgHandler.h"
       
    42 #include "CCntPropertiesMsgHandler.h"
       
    43 #include "CCntViewMsgHandler.h"
       
    44 
       
    45 
       
    46 #define KPhoneBookServerUid 0x102030A1
       
    47 
       
    48 extern void DebugLogIPC(const TDesC& aMethod, TInt aOpCode, TUint aSessionId, TInt aErrCode);
       
    49 
       
    50 
       
    51 CCntSession::CCntSession(CCntPackager& aPackager, TUint aSessionId)
       
    52 	: iPackager(aPackager),
       
    53 	iTimeOut(KOneSecond),
       
    54 	iSessionId(aSessionId)
       
    55 	{
       
    56 	}
       
    57 
       
    58 	
       
    59 CCntSession* CCntSession::NewL(CCntPackager& aPackager, TUint aSessionId)
       
    60 	{
       
    61 	CCntSession* CntSession = new (ELeave) CCntSession(aPackager, aSessionId);
       
    62 	CleanupStack::PushL(CntSession);
       
    63 	CntSession->ConstructL();
       
    64 	CleanupStack::Pop(CntSession);
       
    65 	return CntSession;
       
    66 	}
       
    67 	
       
    68 	
       
    69 void CCntSession::ConstructL()
       
    70 	{
       
    71 	CContactItemViewDef* itemDef = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields, CContactItemViewDef::EIncludeHiddenFields);
       
    72 	itemDef->AddL(KUidContactFieldMatchAll);
       
    73 	iView = CContactViewDef::NewL(itemDef);
       
    74 	CleanupStack::Pop(itemDef);
       
    75 	
       
    76 	// Message handlers
       
    77     CCntItemMsgHandler* itemMsgHandler = CCntItemMsgHandler::NewLC(*this);
       
    78 	CCntEventMsgHandler* eventMsgHandler = CCntEventMsgHandler::NewLC(*this);
       
    79 	CCntTransactionMsgHandler* transMsgHandler = CCntTransactionMsgHandler::NewLC(*this);
       
    80 	CCntFileManagerMsgHandler* fileMsgHandler = CCntFileManagerMsgHandler::NewLC(*this);
       
    81 	CCntPropertiesMsgHandler* propMsgHandler = CCntPropertiesMsgHandler::NewLC(*this);
       
    82 	CCntViewMsgHandler* viewMsgHandler = CCntViewMsgHandler::NewLC(*this);
       
    83 	
       
    84 	/** 
       
    85 	iMsgHandlerArray takes ownership of Message Handlers.
       
    86 	*/	
       
    87 	iMsgHandlerArray.InsertL(itemMsgHandler, ECntItemMsgHandler);
       
    88 	iMsgHandlerArray.InsertL(eventMsgHandler, ECntEventMsgHandler);
       
    89 	iMsgHandlerArray.InsertL(transMsgHandler, ECntTransactionMsgHandler);
       
    90 	iMsgHandlerArray.InsertL(fileMsgHandler, ECntFileManagerMsgHandler);
       
    91 	iMsgHandlerArray.InsertL(propMsgHandler, ECntPropertiesMsgHandler);
       
    92 	iMsgHandlerArray.InsertL(viewMsgHandler, ECntViewMsgHandler);
       
    93 	
       
    94 	// Pop Message handlers on CleanupStack
       
    95 	CleanupStack::Pop(6, itemMsgHandler);
       
    96 	}
       
    97 
       
    98 
       
    99 /**
       
   100 Maps to RSessionBase::Close() called on client-side.
       
   101 */
       
   102 CCntSession::~CCntSession()
       
   103 	{
       
   104 	if(iManager) // CCntDbManager instance only allocated on open/replace/create
       
   105 		{
       
   106 		// Unlock any contact items that have not been explicity unlocked by this
       
   107 		// session prior to this point.
       
   108 		TRAP_IGNORE(SessionUnlockL());
       
   109 		iManager->GetPersistenceLayer().ContactProperties().SessionDeleted(iSessionId);
       
   110 		
       
   111 		// Un-register for database event notifications.
       
   112 		iManager->UnRegisterDatabaseEventObserver(*this);
       
   113 
       
   114 		if (CSession2::Server())
       
   115 			{
       
   116 			// Call the Database Manager Controller to close the database and
       
   117 			// destroy the CCntDbManager instance associated with this session.
       
   118 			Server().Controller().CloseDatabase(*iManager);
       
   119 			}
       
   120 
       
   121 		}
       
   122 
       
   123  	delete iView;
       
   124  	
       
   125 	iMsgHandlerArray.ResetAndDestroy();
       
   126 	iMsgHandlerArray.Close();
       
   127 
       
   128 	if (CSession2::Server())
       
   129 		{
       
   130 		Server().SessionClosing();
       
   131 		}
       
   132 	}
       
   133 
       
   134 
       
   135 void CCntSession::SessionUnlockL()
       
   136 	{
       
   137 	CReqInternalSessionUnlock* request = CReqInternalSessionUnlock::NewLC(iSessionId);
       
   138 	iManager->StateMachineL().ProcessRequestL(request);  // ownership transferred
       
   139 	
       
   140 	// ProcessRequestL received ownership of the request, the request only need
       
   141 	// to be popped from CleanupStack.		
       
   142 	CleanupStack::Pop(request);
       
   143 	}
       
   144 
       
   145 
       
   146 /**
       
   147 Top level client session function code parser. Delegates to message handlers,
       
   148 if the op code is not handled by the 1st message handler, it is delegated to the 2nd
       
   149 message handler.  If the op code is not handled by the 2nd message handler, it
       
   150 delegates to the 3rd and so on.
       
   151 
       
   152 The delegation to message handlers is prioritised. This results in quick handling of critical
       
   153 operations.
       
   154 
       
   155 Message handlers return KErrNotFound if op code is not handled. If KErrNotFound then op code is
       
   156 delegated to the next handler. The last message handler leaves with KErrNotFound if op code not
       
   157 handled. The leave is caught in the ServiceError() method.
       
   158 */
       
   159 void CCntSession::ServiceL(const RMessage2& aMessage)
       
   160 	{
       
   161 	#if defined(__PROFILE_DEBUG__)
       
   162 		RDebug::Print(_L("[CNTMODEL] MTD: CCntSession::ServiceL"));
       
   163 	#endif 
       
   164 		
       
   165 	// The most likely operation is a ContactItem CRUD operation.
       
   166 	
       
   167      DEBUG_PRINT4(__VERBOSE_DEBUG__,_L("[CNTMODEL] IPC"), aMessage.Function(), iSessionId, KErrNone);
       
   168 
       
   169 	TSecureId ClientSID = aMessage.SecureId();
       
   170 	if (ClientSID == KPhoneBookServerUid && iManager)
       
   171 		{
       
   172 		iManager->GetPersistenceLayer().ContactProperties().DisableSynchroniser(iSessionId);			
       
   173 		}
       
   174 		
       
   175 	// Delegate to message handlers.
       
   176 	for (TInt index = 0; index < iMsgHandlerArray.Count(); index++) 
       
   177 		{
       
   178 		TInt retCode = iMsgHandlerArray[index]->HandleMessageL(aMessage);
       
   179 		
       
   180 		/** Message handled or if message handler returned KErrNotFound,
       
   181 		delegate to next handler.
       
   182 		*/
       
   183 		if(retCode == KErrNone)
       
   184 			{
       
   185 			break;
       
   186 			}
       
   187 		}
       
   188 	}
       
   189 
       
   190 
       
   191 /**
       
   192 We just complete the message with the error code.
       
   193 */
       
   194 void CCntSession::ServiceError(const RMessage2& aMessage, TInt aError)
       
   195 	{
       
   196 	
       
   197 	DEBUG_PRINT4(__VERBOSE_DEBUG__,_L("[CNTMODEL] IPC_ERROR"), aMessage.Function(), iSessionId, aError);
       
   198 
       
   199 	aMessage.Complete(aError);
       
   200 	}
       
   201 
       
   202 	
       
   203 /**
       
   204 Process a database event.  At this level we queue the event for consumption by
       
   205 the client.
       
   206 */
       
   207 void CCntSession::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
   208 	{
       
   209 	/**
       
   210 	Delegate to CntEventMsgHandler.
       
   211 	*/	
       
   212 	CCntEventMsgHandler* eventMsgHandler = static_cast<CCntEventMsgHandler*>(iMsgHandlerArray[ECntEventMsgHandler]);
       
   213 	eventMsgHandler->QueueEvent(aEvent);
       
   214 	}
       
   215 
       
   216 
       
   217 void CCntSession::UnRegisterDatabaseEventObserver()
       
   218 	{
       
   219 	// This session is not going to be interested in database event
       
   220 	// notifications so un-register it.
       
   221 	
       
   222 	if(iManager == NULL) 
       
   223 		{
       
   224 		User::Leave(KErrNotReady);
       
   225 		}
       
   226 		
       
   227 	iManager->UnRegisterDatabaseEventObserver(*this);
       
   228 	}
       
   229