pimprotocols/phonebooksync/Server/SyncEngineRequest.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 // The CSyncEngineRequest class is used to store and monitor a request which
       
    15 // has, or will be, sent to the Background Sync Engine.
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalComponent
       
    22 */
       
    23 
       
    24 #include "Phonebook.h"
       
    25 #include "PhonebookManager.h"
       
    26 #include "SyncContactICCEntry.h"
       
    27 #include "phbksyncsvr.h"
       
    28 #include "phbksynclog.h"
       
    29 #include "SyncEngineSession.h"
       
    30 #include "SyncEngineServer.h"
       
    31 #include "phbksyncsess.h"
       
    32 #include "SyncEngineRequest.h"
       
    33 #include "common.h"
       
    34 
       
    35 
       
    36 /**
       
    37  *  Static factory method used to create a CSyncEngineRequest object.
       
    38  *
       
    39  *  @param aServer                Reference to the front-end server.
       
    40  *  @param aSyncEngine            Handle to the background sync engine.
       
    41  *  @param aPhonebookSyncRequest  Type of engine sync request.
       
    42  *  @param aPhonebookUid          UID of the phonebook being operated on.
       
    43  *  @param aContactId             Contact ID of the entry being modified.
       
    44  *  @param aClientSession         Pointer to the client session (if one exists).
       
    45  *  @param aClientMessage         Reference to the client message (if one exists).
       
    46  *
       
    47  *  @return  Returns the pointer to a newly created CSyncEngineRequest object.
       
    48  */
       
    49 CSyncEngineRequest*  CSyncEngineRequest::NewL(CPhoneBookServer& aServer,
       
    50 									  RSyncEngineSession& aSyncEngine,
       
    51 									  TPhonebookSyncRequest aPhonebookSyncRequest,
       
    52 									  TUid aPhonebookUid,
       
    53 									  TContactItemId aContactId,
       
    54 									  CPhoneBookSession* aClientSession,
       
    55 									  const RMessage2& aClientMessage)
       
    56 	{
       
    57 	CSyncEngineRequest*  engineRequest = new(ELeave) CSyncEngineRequest(aServer, aSyncEngine,
       
    58 																aPhonebookSyncRequest,
       
    59 																aPhonebookUid, aContactId,
       
    60 																aClientSession, aClientMessage);
       
    61 	CleanupStack::PushL(engineRequest);
       
    62 	engineRequest->ConstructL();
       
    63 	CleanupStack::Pop(engineRequest);
       
    64 
       
    65 	return engineRequest;
       
    66 	} // CActiveEngineRequest::NewL
       
    67 
       
    68 
       
    69 /**
       
    70  *  Private constructor for an engine request.
       
    71  *
       
    72  *  @param aServer                Reference to the front-end server.
       
    73  *  @param aSyncEngine            Handle to the background sync engine.
       
    74  *  @param aPhonebookSyncRequest  Type of engine sync request.
       
    75  *  @param aPhonebookUid          UID of the phonebook being operated on.
       
    76  *  @param aContactId             Contact ID of the entry being modified.
       
    77  *  @param aClientSession         Pointer to the client session (if one exists).
       
    78  *  @param aClientMessage         Reference to the client message (if one exists).
       
    79  */
       
    80 CSyncEngineRequest::CSyncEngineRequest(CPhoneBookServer& aServer,
       
    81 							   RSyncEngineSession& aSyncEngine,
       
    82 							   TPhonebookSyncRequest aPhonebookSyncRequest,
       
    83 							   TUid aPhonebookUid,
       
    84 							   TContactItemId aContactId,
       
    85 							   CPhoneBookSession* aClientSession,
       
    86 							   const RMessage2& aClientMessage)
       
    87   : CActive(EPriorityNormal),
       
    88     iServer(aServer),
       
    89     iSyncEngine(aSyncEngine),
       
    90     iPhonebookSyncRequest(aPhonebookSyncRequest),
       
    91     iPhonebookUid(aPhonebookUid),
       
    92     iContactId(aContactId),
       
    93     iClientSession(aClientSession),
       
    94     iClientMessage(aClientMessage),
       
    95     iWriteContactSlotNumber(-1),
       
    96     iWriteContactICCEntryBuf(NULL),
       
    97 	iWriteContactTemplateIdAndBufferSizePtr(NULL, 0),
       
    98 	iWriteContactSlotNumberPtr(NULL, 0),
       
    99 	iWriteContactPhoneBookIDPtr(NULL, 0),
       
   100 	iWriteContactICCEntryBufPtr(NULL, 0)
       
   101     {
       
   102 	// NOP
       
   103 	} // CSyncEngineRequest::CSyncEngineRequest
       
   104 
       
   105 
       
   106 /**
       
   107  *  Destructor for the engine request.
       
   108  */
       
   109 CSyncEngineRequest::~CSyncEngineRequest()
       
   110 	{
       
   111 	LOGACTIVE1(_L8("~CSyncEngineRequest()"));
       
   112 	Cancel();
       
   113 	
       
   114 	delete iWriteContactICCEntryBuf;
       
   115 	} // CSyncEngineRequest::~CSyncEngineRequest
       
   116 	
       
   117 
       
   118 /**
       
   119  *  Second phrase constructor for the engine request. The request will be initialised
       
   120  *  and any client side data will be stored for lating sending to the engine.
       
   121  */
       
   122 void CSyncEngineRequest::ConstructL()
       
   123 	{
       
   124 	LOGACTIVE1(_L8("CSyncEngineRequest::ConstructL()"));
       
   125 
       
   126 	//
       
   127 	// Read any data required into the engine request's buffers...
       
   128 	//
       
   129 	if (iPhonebookSyncRequest == ESyncWriteCntToICC)
       
   130 		{
       
   131 		TPckg<RPhoneBookSession::TTemplateAndBufferSize>  templateIdAndBufferSizePckg(iWriteContactTemplateIdAndBufferSize);
       
   132 		TPckg<TInt>  slotNumPckg(iWriteContactSlotNumber);
       
   133 
       
   134 		iClientMessage.ReadL(0, templateIdAndBufferSizePckg); 
       
   135 		iClientMessage.ReadL(1, slotNumPckg);
       
   136 
       
   137 		iWriteContactICCEntryBuf = CBufFlat::NewL(1);
       
   138 		iWriteContactICCEntryBuf->ResizeL(iWriteContactTemplateIdAndBufferSize.bufferSize); 
       
   139 		iWriteContactICCEntryBufPtr.Set(iWriteContactICCEntryBuf->Ptr(0));
       
   140 		iClientMessage.ReadL(3, iWriteContactICCEntryBufPtr);
       
   141 
       
   142 		//
       
   143 		// Call the engine's write contact function...
       
   144 		//		
       
   145 		iWriteContactTemplateIdAndBufferSizePtr.Set(reinterpret_cast<const TText8*>(&iWriteContactTemplateIdAndBufferSize),
       
   146 													sizeof(RPhoneBookSession::TTemplateAndBufferSize));
       
   147 		iWriteContactSlotNumberPtr.Set(reinterpret_cast<TText8*>(&iWriteContactSlotNumber),
       
   148 									   sizeof(iWriteContactSlotNumber), sizeof(iWriteContactSlotNumber));
       
   149 		iWriteContactPhoneBookIDPtr.Set(reinterpret_cast<TText8*>(&iPhonebookUid),
       
   150 										sizeof(iPhonebookUid), sizeof(iPhonebookUid));		
       
   151 		iWriteContactICCEntryBufPtr.Set(iWriteContactICCEntryBuf->Ptr(0));
       
   152 		}
       
   153 
       
   154 	CActiveScheduler::Add(this);
       
   155 	} // CSyncEngineRequest::ConstructL
       
   156 
       
   157 
       
   158 /**
       
   159  *  This function is used to issue a request to the Background Sync Engine via the
       
   160  *  Sync Engine Session. Once this function completes the request will be active and
       
   161  *  when the request is completed, RunL() will be called.
       
   162  */
       
   163 void CSyncEngineRequest::StartRequest()
       
   164 	{
       
   165 	LOGACTIVE1(_L8("CSyncEngineRequest::StartRequest()"));
       
   166 
       
   167 	if (iPhonebookSyncRequest == ESyncDoSynchronisation)
       
   168 		{
       
   169 		iSyncEngine.DoSynchronisation(iStatus, iPhonebookUid);
       
   170 		SetActive();
       
   171 		}
       
   172 	else if (iPhonebookSyncRequest == ESyncDeleteCntFromICC)
       
   173 		{
       
   174 		iSyncEngine.DeleteContact(iStatus, iPhonebookUid, iContactId);
       
   175 		SetActive();
       
   176 		}
       
   177 	else if (iPhonebookSyncRequest == ESyncWriteCntToICC)
       
   178 		{
       
   179 		iSyncEngine.WriteContact(iStatus,
       
   180 								 iWriteContactTemplateIdAndBufferSizePtr,
       
   181 								 iWriteContactSlotNumberPtr,
       
   182 								 iWriteContactPhoneBookIDPtr,
       
   183 								 iWriteContactICCEntryBufPtr);
       
   184 		SetActive();
       
   185 		}
       
   186 	else
       
   187 		{
       
   188 		PhBkSyncPanic(EPhBkSyncPanicInvalidEngineRequest);
       
   189 		}
       
   190 	} // CSyncEngineRequest::StartRequest
       
   191 
       
   192 
       
   193 /**
       
   194  *  Completes the clients request. This may involve performing additional
       
   195  *  actions such as informing the front-end server of status changes, or
       
   196  *  relaying information from the engine to the client.
       
   197  *
       
   198  *  The client request will be completed (if one exists) and this request
       
   199  *  will be completed (e.g. removed from the queue and deleted).
       
   200  */
       
   201 void CSyncEngineRequest::CompleteClientRequestL()
       
   202 	{
       
   203 	LOGACTIVE1(_L8("CSyncEngineRequest::CompleteClientRequestL()"));
       
   204 
       
   205 	//
       
   206 	// Perform any special tasks for this request...
       
   207 	//
       
   208 	if (iPhonebookSyncRequest == ESyncDoSynchronisation)
       
   209 		{
       
   210 		//
       
   211 		// Let the server know that the cache state has changed...
       
   212 		//
       
   213 		iServer.CompleteNotifyStateChange(iPhonebookUid);
       
   214 		}
       
   215 	else if (iStatus.Int() == KErrNone  &&
       
   216 			 iPhonebookSyncRequest == ESyncWriteCntToICC)
       
   217 		{
       
   218 		//
       
   219 		// Pass back the slot number and phonebook which may have been
       
   220 		// updated (in the cases where the slot or phonebook was not)
       
   221 		// specified.
       
   222 		//
       
   223 		TPckg<TInt>  slotNumPckg(iWriteContactSlotNumber);
       
   224 		TPckg<TUid>  phonebookUidPckg(iPhonebookUid);
       
   225 
       
   226 		iClientMessage.WriteL(1, slotNumPckg);
       
   227 		iClientMessage.WriteL(2, phonebookUidPckg);
       
   228 		}
       
   229 
       
   230 	//
       
   231 	// Complete the client's request (if one exists)...
       
   232 	//
       
   233 	if (iClientSession != NULL)
       
   234 		{
       
   235 		iClientSession->CompleteRequest(iClientMessage, iStatus.Int());
       
   236 		}
       
   237 
       
   238 	//
       
   239 	// Remove this request from the request queue and start the next request...
       
   240 	//
       
   241 	iServer.CompleteEngineRequest(*this);
       
   242 	
       
   243 	//
       
   244 	// Delete this request...
       
   245 	//
       
   246 	delete this;
       
   247 	} // CSyncEngineRequest::CompleteClientRequestL
       
   248 
       
   249 
       
   250 /**
       
   251  *  Standard Active Object RunL() method. This completes the client request.
       
   252  */
       
   253 void CSyncEngineRequest::RunL()
       
   254 	{
       
   255 	LOGACTIVE1(_L8("CSyncEngineRequest::RunL()"));
       
   256 
       
   257 	CompleteClientRequestL();
       
   258 	} // CSyncEngineRequest::RunL
       
   259 
       
   260 
       
   261 /** 
       
   262  *  Standard Active Object DoCancel method. This is called when the Cancel()
       
   263  *  method is called on an active request. It cancels the pending request
       
   264  *  with the Background Sync Engine.
       
   265  */
       
   266 void CSyncEngineRequest::DoCancel()
       
   267 	{
       
   268 	if (iPhonebookSyncRequest == ESyncDoSynchronisation)
       
   269 		{
       
   270 		iSyncEngine.DoSynchronisationCancel(iPhonebookUid);
       
   271 		}
       
   272 	else if (iPhonebookSyncRequest == ESyncDeleteCntFromICC)
       
   273 		{
       
   274 		iSyncEngine.DeleteContactCancel(iPhonebookUid);
       
   275 		}
       
   276 	else if (iPhonebookSyncRequest == ESyncWriteCntToICC)
       
   277 		{
       
   278 		iSyncEngine.WriteContactCancel(iPhonebookUid);
       
   279 		}
       
   280 	else
       
   281 		{
       
   282 		iStatus = KErrCancel;
       
   283 		}
       
   284 	} // CSyncEngineRequest::DoCancel
       
   285 
       
   286 
       
   287 /**
       
   288  *  Standard Active Object RunError() method. This is called if the RunL()
       
   289  *  method leaves. Hopefully this should not happen.
       
   290  *
       
   291  *  @param aError  Leave code from the RunL().
       
   292  *
       
   293  *  @return KErrNone (although the code will panic first).
       
   294  */
       
   295 TInt CSyncEngineRequest::RunError(TInt aError)
       
   296 	{
       
   297 #ifdef _DEBUG
       
   298 	LOGACTIVE2(_L8("CSyncEngineRequest::RunError(%d)"), aError);
       
   299 #else
       
   300 	(void) aError;
       
   301 #endif
       
   302 
       
   303 	PhBkSyncPanic(EPhBkSyncPanicUnexpectedLeave);
       
   304 
       
   305 	return KErrNone;	
       
   306 	} // CSyncEngineRequest::RunError
       
   307