pimprotocols/phonebooksync/Server/SyncEngineClient.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2002-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 // Implementation of the Background Sync Engine client interface.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include "SyncEngineClient.h"
       
    24 #include "common.h"
       
    25 #include "Phonebook.h"
       
    26 #include "PhonebookManager.h"
       
    27 #include "SyncContactICCEntry.h"
       
    28 #include "SyncEngineServer.h"
       
    29 #include "phbksynclog.h"
       
    30 
       
    31 
       
    32 /**
       
    33  *  Size of the Background Sync Engine thread stack size.
       
    34  */
       
    35 const TInt KSyncEngineStackSize(0x3000);
       
    36 
       
    37 
       
    38 /**
       
    39  *  Performs all server initialisation, in particular creation of the
       
    40  *  scheduler object, the enigne server and then runs the scheduler
       
    41  *  (which starts the engine server).
       
    42  *
       
    43  *  @param aPhoneBookManager  Reference to the phonebook manager class used
       
    44  *                            by the front-end server.
       
    45  */
       
    46 static void RunEngineL(CPhoneBookManager& aPhoneBookManager)
       
    47 	{
       
    48 	LOGENGINE1(_L8("RunEngineL()"));
       
    49 
       
    50 	//
       
    51 	// Create a new Sync Engine Server...
       
    52 	//
       
    53 	CSyncEngineServer*  engine = CSyncEngineServer::NewL(aPhoneBookManager);
       
    54 	CleanupStack::PushL(engine);
       
    55 	
       
    56 	//
       
    57 	// Initialisation complete, now signal the client thread...
       
    58 	//
       
    59 	LOGENGINE1(_L8("RunEngineL(): Meeting Rendezvous..."));
       
    60 	RThread::Rendezvous(KErrNone);
       
    61 
       
    62 	//
       
    63 	// Run the server...
       
    64 	//
       
    65 	LOGENGINE1(_L8("RunEngineL(): Starting server..."));
       
    66 	CSyncEngineScheduler::Start();
       
    67 	
       
    68 	CleanupStack::PopAndDestroy(engine);
       
    69 	} // RunEngineL
       
    70 
       
    71 
       
    72 /**
       
    73  *  Thread function for starting the engine. This function basically just
       
    74  *  installs an Active Scheduler, a clean up stack and then calls the
       
    75  *  RunEngineL() leaving function.
       
    76  *
       
    77  *  @param aPtr  TAny pointer to the phonebook manager class used
       
    78  *               by the front-end server.
       
    79  */
       
    80 static TInt RunEngineThread(TAny* aPtr)
       
    81 	{
       
    82 	CPhoneBookManager*  phonebookManager = static_cast<CPhoneBookManager*>(aPtr);
       
    83 
       
    84 	LOGENGINE1(_L8("RunEngineThread()"));
       
    85 
       
    86 	//
       
    87 	// Create an Active Scheduler...
       
    88 	//
       
    89 	CActiveScheduler*  scheduler = new CActiveScheduler();
       
    90 	if (scheduler == NULL)
       
    91 		{
       
    92 		return KErrNoMemory;
       
    93 		}
       
    94 	CActiveScheduler::Install(scheduler);
       
    95 
       
    96 	//
       
    97 	// Create a clean up stack...
       
    98 	//
       
    99 	CTrapCleanup*  cleanup = CTrapCleanup::New();
       
   100 	
       
   101 	TRAPD(leaveCode, RunEngineL(*phonebookManager));
       
   102 
       
   103 	delete cleanup;	
       
   104 	delete scheduler;
       
   105 
       
   106 	return leaveCode;
       
   107 	} // RunEngineThread
       
   108 
       
   109 
       
   110 /**
       
   111  *  Start the engine thread.
       
   112  */
       
   113 TInt RSyncEngineSession::StartPhBkSyncEngine(CPhoneBookManager* aPhoneBookManager)
       
   114 	{
       
   115 	LOGENGINE1(_L8("RSyncEngineSession::StartPhBkSyncEngine()"));
       
   116 
       
   117 	//
       
   118 	// Create the Sync Engine Thread (using the same heap as this thread) at
       
   119 	// EPriorityAbsoluteBackground priority, ...
       
   120 	//
       
   121 	TInt  ret;
       
   122 	
       
   123 	ret = iEngineThread.Create(PHBKSYNC_ENGINE_NAME, RunEngineThread,
       
   124 	                           KSyncEngineStackSize, NULL, aPhoneBookManager);
       
   125 	if (ret != KErrNone)
       
   126 		{
       
   127 		return ret;
       
   128 		}
       
   129 
       
   130 	iEngineThread.SetPriority(EPriorityAbsoluteBackground);
       
   131 		
       
   132 	//
       
   133 	// Logon to the engine thread. This will be used later to ensure the
       
   134 	// thread shuts down when closed.
       
   135 	//
       
   136 	iEngineThread.Logon(iEngineThreadStatus);
       
   137 	if (iEngineThreadStatus != KRequestPending)
       
   138 		{
       
   139 		iEngineThread.Kill(0);
       
   140 		iEngineThread.Close();
       
   141 
       
   142 		User::WaitForRequest(iEngineThreadStatus);
       
   143 
       
   144 		return iEngineThreadStatus.Int();
       
   145 		}
       
   146 
       
   147 	//
       
   148 	// Rendezvous with the engine or abort the engine start...
       
   149 	//
       
   150 	TRequestStatus  rendezvousStatus;
       
   151 
       
   152 	iEngineThread.Rendezvous(rendezvousStatus);
       
   153 	if (rendezvousStatus != KRequestPending)
       
   154 		{
       
   155 		iEngineThread.Kill(0);
       
   156 		iEngineThread.Close();
       
   157 
       
   158 		User::WaitForRequest(rendezvousStatus);
       
   159 		User::WaitForRequest(iEngineThreadStatus);
       
   160 
       
   161 		return rendezvousStatus.Int();
       
   162 		}
       
   163 
       
   164 	//
       
   165 	// The thread is ready to go...
       
   166 	//
       
   167 	iEngineThread.Resume();
       
   168 	User::WaitForRequest(rendezvousStatus);
       
   169 	if (rendezvousStatus != KErrNone)
       
   170 		{
       
   171 		iEngineThread.Kill(0);
       
   172 		iEngineThread.Close();
       
   173 
       
   174 		User::WaitForRequest(iEngineThreadStatus);
       
   175 
       
   176 		return rendezvousStatus.Int();
       
   177 		}
       
   178 
       
   179 	return KErrNone;
       
   180 	} // RSyncEngineSession::StartPhBkSyncEngine
       
   181 
       
   182 
       
   183 /**
       
   184  *  Standard constructor.
       
   185  */
       
   186 RSyncEngineSession::RSyncEngineSession()
       
   187 	{
       
   188 	iEngineThread.SetHandle(0);
       
   189 	} // RSyncEngineSession::RSyncEngineSession
       
   190 
       
   191 
       
   192 /**
       
   193  *  Connects the client to the Phonebook Synchronizer Server. 
       
   194  *
       
   195  *  This must be used before any of the other methods listed in 
       
   196  *  this API section. The first client to call this method will 
       
   197  *  cause the initiation of the Phonebook Synchronizer Server 
       
   198  *  within its own executable process.
       
   199  *
       
   200  *  @param aPhoneBookManager  Pointer to the phonebook manager class used
       
   201  *                            by the front-end server.
       
   202  *
       
   203  *  @return KErrNone if successful, a system-wide error code if not. 
       
   204  *
       
   205  *  @capability None
       
   206  */
       
   207 TInt RSyncEngineSession::Connect(CPhoneBookManager* aPhoneBookManager)
       
   208 	{
       
   209 	LOGENGINE1(_L8("RSyncEngineSession::Connect()"));
       
   210 	
       
   211 	//
       
   212 	// Create a session with the server, but if it doesn't exist then start it and
       
   213 	// then create a session.
       
   214 	//
       
   215 	TInt  result = CreateSession(PHBKSYNC_ENGINE_NAME, TVersion());
       
   216 	if (result == KErrNotFound  ||  result == KErrServerTerminated)
       
   217 		{
       
   218 		result = StartPhBkSyncEngine(aPhoneBookManager);
       
   219 		
       
   220 		if(result == KErrNone)
       
   221 			{
       
   222 			result = CreateSession(PHBKSYNC_ENGINE_NAME, TVersion());
       
   223 			}
       
   224 		}	
       
   225 		
       
   226 	//
       
   227 	// If the creation of the session fails then close it to clean up
       
   228 	// any allocated member data or running threads...
       
   229 	//
       
   230 	if (result != KErrNone)
       
   231 		{
       
   232 		Close();
       
   233 		}
       
   234 
       
   235 	return result;
       
   236 	} // RSyncEngineSession::Connect
       
   237 
       
   238 
       
   239 /**
       
   240  *  Closes the client's session with the Phonebook Synchronizer Server. 
       
   241  *
       
   242  *  @capability None
       
   243  */
       
   244 void RSyncEngineSession::Close()
       
   245 	{
       
   246 	LOGENGINE1(_L8("RSyncEngineSession::Close()"));
       
   247 
       
   248 	RSessionBase::Close();
       
   249 
       
   250 	//
       
   251 	// Ensure the engine thread shuts down before we return...
       
   252 	//
       
   253 	if (iEngineThread.Handle() != 0)
       
   254 		{
       
   255 		User::WaitForRequest(iEngineThreadStatus);
       
   256 		iEngineThread.Close();
       
   257 		}
       
   258 	} // RSyncEngineSession::Close
       
   259 
       
   260 
       
   261 /**
       
   262  *  Instructs the engine to start a manual synchronisation of the ICC
       
   263  *  phonebook specified by the aPhonebookUid parameter.
       
   264  *
       
   265  *  @param aReqStatus     On completion, KErrNone if successful, a
       
   266  *                        system-wide error code if not. 
       
   267  *  @param aPhonebookUid  TUid of the ICC phonebook to be synchronised.
       
   268  *
       
   269  *  @capability ReadUserData
       
   270  *  @capability WriteUserData
       
   271  */
       
   272 void RSyncEngineSession::DoSynchronisation(TRequestStatus& aReqStatus,
       
   273 										   TUid aPhonebookUid)
       
   274 	{
       
   275 	LOGENGINE2(_L8("RSyncEngineSession::DoSynchronisation(): 0x%08x"),
       
   276 			   aPhonebookUid);
       
   277 	
       
   278 	SendReceive(ESyncDoSynchronisation, TIpcArgs(aPhonebookUid.iUid), aReqStatus);
       
   279 	} // RSyncEngineSession::DoSynchronisation
       
   280 
       
   281 
       
   282 /**
       
   283  *  Cancels an existing Syncronisation request.
       
   284  *
       
   285  *  @param aPhonebookUid  TUid of the ICC phonebook to cancel.
       
   286  *
       
   287  *  @capability ReadUserData
       
   288  *  @capability WriteUserData
       
   289  */
       
   290 void RSyncEngineSession::DoSynchronisationCancel(TUid aPhonebookUid)
       
   291 	{
       
   292 	LOGENGINE2(_L8("RSyncEngineSession::DoSynchronisationCancel(): 0x%08x"),
       
   293 			   aPhonebookUid);
       
   294 
       
   295 	SendReceive(ESyncDoSynchronisationCancel, TIpcArgs(aPhonebookUid.iUid));
       
   296 	} // RSyncEngineSession::DoSynchronisationCancel
       
   297 
       
   298 
       
   299 /**
       
   300  *  Write the contact item to an ICC phonebook. The parameters for this
       
   301  *  function are left in the original packaged state from the front-end
       
   302  *  server's client.
       
   303  *
       
   304  *  @param aReqStatus                   On completion, KErrNone if successful,
       
   305  *										a system-wide error code if not.
       
   306  *  @param aTemplateIdAndBufferSizePtr  Packaged pointer to the template and
       
   307  *                                      buffer size.
       
   308  *  @param aSlotNumberPtr  				Packaged pointer to the slot number.
       
   309  *  @param aPhoneBookUidPtr    			Packaged pointer to the phonebook UID.
       
   310  *  @param aICCEntryBufPtr    			Packaged pointer to the ICC Entry.
       
   311  *
       
   312  *  @capability WriteUserData
       
   313  */
       
   314 void RSyncEngineSession::WriteContact(TRequestStatus& aReqStatus,
       
   315 									  TPtrC8& aTemplateIdAndBufferSizePtr,
       
   316 									  TPtr8& aSlotNumberPtr,
       
   317 									  TPtr8& aPhoneBookUidPtr,
       
   318 									  TPtr8& aICCEntryBufPtr)
       
   319 	{
       
   320 	LOGENGINE1(_L8("RSyncEngineSession::WriteContact()"));
       
   321 
       
   322 	TIpcArgs  args(&aTemplateIdAndBufferSizePtr, &aSlotNumberPtr,
       
   323 	               &aPhoneBookUidPtr, &aICCEntryBufPtr);
       
   324 
       
   325 	SendReceive(ESyncWriteCntToICC, args, aReqStatus);
       
   326 	} // RSyncEngineSession::WriteContact
       
   327 
       
   328 
       
   329 /**
       
   330  *  Cancels an existing WriteContact request.
       
   331  *
       
   332  *  @param aPhonebookUid  TUid of the ICC phonebook to cancel.
       
   333  *
       
   334  *  @capability WriteUserData
       
   335  */
       
   336 void RSyncEngineSession::WriteContactCancel(TUid aPhonebookUid)
       
   337 	{
       
   338 	LOGENGINE2(_L8("RSyncEngineSession::WriteContactCancel(): 0x%08x"),
       
   339 			   aPhonebookUid);
       
   340 
       
   341 	SendReceive(ESyncWriteCntToICCCancel, TIpcArgs(aPhonebookUid.iUid));
       
   342 	} // RSyncEngineSession::WriteContactCancel
       
   343 
       
   344 
       
   345 /**
       
   346  *  Deletes a contact item from the ICC.
       
   347  *
       
   348  *  @param aReqStatus     On completion, KErrNone if successful, a system-wide
       
   349  *					      error code if not.
       
   350  *  @param aPhonebookUid  UID of the ICC phonebook to delete from.
       
   351  *  @param aId            The contact item to delete. 
       
   352  *
       
   353  *  @capability WriteUserData
       
   354  */
       
   355 void RSyncEngineSession::DeleteContact(TRequestStatus& aReqStatus,
       
   356 									   TUid aPhonebookUid,
       
   357 									   TContactItemId aId)
       
   358 	{
       
   359 	LOGENGINE3(_L8("RSyncEngineSession::DeleteContact(): 0x%08x"),
       
   360 			   aPhonebookUid, aId);
       
   361 
       
   362 	SendReceive(ESyncDeleteCntFromICC, TIpcArgs(aPhonebookUid.iUid, aId),
       
   363 				aReqStatus);
       
   364 	} // RSyncEngineSession::DeleteContact
       
   365 
       
   366 
       
   367 /**
       
   368  *  Cancels an existing WriteContact request.
       
   369  *
       
   370  *  @param aPhonebookUid  TUid of the ICC phonebook to cancel.
       
   371  *
       
   372  *  @capability WriteUserData
       
   373  */
       
   374 void RSyncEngineSession::DeleteContactCancel(TUid aPhonebookUid)
       
   375 	{
       
   376 	LOGENGINE2(_L8("RSyncEngineSession::DeleteContactCancel(): 0x%08x"),
       
   377 			   aPhonebookUid);
       
   378 
       
   379 	SendReceive(ESyncDeleteCntFromICCCancel, TIpcArgs(aPhonebookUid.iUid));
       
   380 	} // RSyncEngineSession::DeleteContactCancel
       
   381