messagingfw/msgsrvnstore/server/src/MSVSESS.CPP
changeset 0 8e480a14352b
child 6 fe71b07a6401
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1998-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 #ifdef _DEBUG
       
    17 #undef _NO_SERVER_LOGGING_
       
    18 #endif
       
    19 
       
    20 
       
    21 
       
    22 #include <e32std.h>
       
    23 #include <s32mem.h>
       
    24 #include <tmsvsystemprogress.h>
       
    25 
       
    26 #include "MSVSTD.H"
       
    27 #include "MSVIPC.H"
       
    28 #include "MSVREG.H"
       
    29 #include "MSVREGIP.H"
       
    30 #include "MSVIDS.H"
       
    31 #include "MSVUIDS.H"
       
    32 #include "MSVENTRY.H"
       
    33 #include "MSERVER.H"
       
    34 #include "MSVSERV.H"
       
    35 #include "MSVLOPS.H"
       
    36 #include "MSVROPS.H"
       
    37 #include "MSVUTILS.H"
       
    38 #include "MTSR.H"
       
    39 #include "MSVPANIC.H"
       
    40 #include "CMsvChangeBuffer.h"
       
    41 #include "MsvSecurityCapabilitySet.h"
       
    42 #include "cmsvcopystoreoperation.h"
       
    43 #include "cmsvdeletestoreoperation.h"
       
    44 #include "MSVAPI.H"
       
    45 #include "msvindexadapter.h"
       
    46 #include "msvcacheentry.h"
       
    47 #include "msvsearchsortcacheentry.h"
       
    48 #include "msvsearchsortcachemanager.h"
       
    49 #include "msvsearchsortdeltacache.h"
       
    50 #include <msvsearchsortquery.h>
       
    51 #include <msvsearchsortoperation.h>
       
    52 
       
    53 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
    54 #include <u32std.h>
       
    55 #include "cmsvconverterwaiter.h"
       
    56 #endif
       
    57 
       
    58 const TInt KMsvOperationIndexGranularity=8;
       
    59 const TInt KMsvOperationDataArrayGranularity=4;
       
    60 const TInt KMsvMtmRefCountArrayGranularity=4;
       
    61 
       
    62 _LIT(KMsvClientPanicString, "MSGS Client");
       
    63 
       
    64 //**********************************
       
    65 // TMtmGroupRefCount
       
    66 //**********************************
       
    67 
       
    68 TMtmGroupRefCount::TMtmGroupRefCount(TUid aMtmTypeUid)
       
    69 : iMtmTypeUid(aMtmTypeUid), iRefCount(0)
       
    70 	{}
       
    71 
       
    72 //**********************************
       
    73 // CMsvOperationData
       
    74 //**********************************
       
    75 
       
    76 CMsvOperationData::CMsvOperationData(TMsvOp aOpId)
       
    77 : iOpId(aOpId)
       
    78 	{
       
    79 	__DECLARE_NAME(_S("CMsvOperationData"));
       
    80 	}
       
    81 
       
    82 CMsvOperationData::~CMsvOperationData()
       
    83 	{
       
    84 	delete iBuffer;
       
    85 	}
       
    86 
       
    87 //**********************************
       
    88 // CMsvServerSession
       
    89 //**********************************
       
    90 
       
    91 CMsvServerSession::CMsvServerSession(CMsvServer& aServer)
       
    92 :   CSession2(),
       
    93 	iMsvServer(aServer),
       
    94 	iOperations(KMsvOperationIndexGranularity),
       
    95 	iMtmGroupRefCountArray(KMsvMtmRefCountArrayGranularity),
       
    96 	iOperationDataArray(KMsvOperationDataArrayGranularity),
       
    97 	iReceiveEntryEvents(ETrue)
       
    98 	{
       
    99 	__DECLARE_NAME(_S("CMsvServerSession"));
       
   100 	iSessionId = iMsvServer.SessionId();
       
   101 	}
       
   102 
       
   103 CMsvServerSession* CMsvServerSession::NewL(CMsvServer& aServer, const RMessage2 &aMessage)
       
   104 	{
       
   105 	CMsvServerSession* self = new(ELeave) CMsvServerSession(aServer);
       
   106 	CleanupStack::PushL(self);
       
   107 	self->ConstructL(aMessage);
       
   108 	CleanupStack::Pop();
       
   109 	return self;
       
   110 	}
       
   111 
       
   112 
       
   113 void CMsvServerSession::ConstructL(const RMessage2 &aMessage)
       
   114 	{
       
   115 	iChangeBuffer = CMsvChangeBuffer::NewL();
       
   116 	iBuffer = HBufC8::NewL(KMsvSessionBufferLength);
       
   117 	iChildrenSelection = new(ELeave) CArrayPtrSeg<const TMsvEntry>(16);
       
   118 	iChildrenSelectionIds = new(ELeave) CMsvEntrySelection;
       
   119 	
       
   120 	// if the index is already loaded, tell the client
       
   121 	if (iMsvServer.HasContext() && iMsvServer.Context().State()==TMsvIndexLoadProgress::EIndexComplete)
       
   122 		{
       
   123 		TMsvNotifBuffer buffer;
       
   124 		TMsvPackedChangeNotification package(buffer);
       
   125 		package.Pack(EMsvIndexLoaded, 0, 0, 0);
       
   126 		NotifyChangedL(buffer);
       
   127 
       
   128 		// Send disk status notifications
       
   129 		if (iMsvServer.StartupState() != EMsvNullNotification)
       
   130 			{
       
   131 			package.Pack(iMsvServer.StartupState(), KMsvNullIndexEntryId, iMsvServer.Context().Config().iDrive, 0);
       
   132 			NotifyChangedL(buffer);
       
   133 			}
       
   134 	
       
   135 	#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   136 			// if we have unsupported drives, notify the client
       
   137 		if(FoundUnSupportedDrives())
       
   138 			{
       
   139 			package.Pack(EMsvMessageStoreNotSupported, 0, 0, 0);
       
   140 			NotifyChangedL(buffer);
       
   141 			}
       
   142 	#endif
       
   143 		}
       
   144 
       
   145 	RThread thread;
       
   146 	aMessage.Client(thread);
       
   147 	iProcessName = thread.Name();
       
   148 	thread.Close();
       
   149 	}
       
   150 
       
   151 
       
   152 CMsvServerSession::~CMsvServerSession()
       
   153 	{
       
   154 	// releasing locks
       
   155 	__ASSERT_DEBUG(iLockedStores.Count()==0, PanicClient(iQueuedMessage, EMsvLockedStoresOnExit));
       
   156 	TInt count = iLockedStores.Count();
       
   157 	while (count--)
       
   158 		{
       
   159 		iMsvServer.IndexAdapter().ReleaseStore(iLockedStores.At(count)); // ignore any errors
       
   160 		}
       
   161 
       
   162 
       
   163 	// deleting outstanding operations
       
   164 	__ASSERT_DEBUG(iOperations.Count()==0, PanicClient(iQueuedMessage, EMsvOutstandingOperationsOnExit));
       
   165 	count=iOperations.Count();
       
   166 	while (count--)
       
   167 		{
       
   168 		delete iOperations.At(count); // operation are deleted last in first out order
       
   169 		iOperations.Delete(count);
       
   170 		}
       
   171 
       
   172 	iOperationDataArray.ResetAndDestroy();
       
   173 	iMtmGroupRefCountArray.Reset();
       
   174 
       
   175 	// Set request queued flag to false so we're not notified to close the session
       
   176 	iRequestQueued = EFalse;
       
   177 
       
   178 	iMsvServer.ClosingSession(iSessionId);
       
   179 
       
   180 	delete iChildrenSelection;
       
   181 	delete iChildrenSelectionIds;
       
   182 	if(iChangeBuffer != NULL)
       
   183 		{
       
   184 		delete iChangeBuffer;
       
   185 		}
       
   186 	if(iBuffer != NULL)
       
   187 		{
       
   188 		delete iBuffer;
       
   189 		
       
   190 		}
       
   191 	}
       
   192 
       
   193 void CMsvServerSession::ServiceL(const RMessage2& aMessage)
       
   194 //
       
   195 //
       
   196 //
       
   197 	{
       
   198 	if ((!iMsvServer.HasContext() || iMsvServer.Context().State()!=TMsvIndexLoadProgress::EIndexComplete) && aMessage.Function()!=EMsvNotifySessionEvent && aMessage.Function()!=EMsvCancelSessionEventNotification)
       
   199 		aMessage.Complete(KErrNotReady);
       
   200 	else
       
   201 		{
       
   202 		TRAPD(error, DoServiceL(aMessage));
       
   203 		if (error)
       
   204 			aMessage.Complete(error);
       
   205 		}
       
   206 	}
       
   207 
       
   208 void CMsvServerSession::DoServiceL(const RMessage2& aMessage)
       
   209 //
       
   210 // service a client request; test the opcode and then do appropriate servicing
       
   211 //
       
   212 	{
       
   213 	// all functions called should ensure that all synchronous messages have been completed
       
   214 	switch (aMessage.Function())
       
   215 		{
       
   216 		case EMsvNotifySessionEvent:
       
   217 			QueueSessionEventRequestL(aMessage);
       
   218 			break;
       
   219 		case EMsvCancelSessionEventNotification:
       
   220 			CancelSessionEventRequest(aMessage);
       
   221 			break;
       
   222 		case EMsvOperationData:
       
   223 			CopyOperationDataL(aMessage);
       
   224 			break;
       
   225 		case EMsvCommandData:
       
   226 			CopyCommandDataL(aMessage);
       
   227 			break;
       
   228 		case EMsvCreateEntry:
       
   229 			CreateEntryL(aMessage);
       
   230 			break;
       
   231 		case EMsvGetEntry:
       
   232 			GetEntryL(aMessage);
       
   233 			break;
       
   234 		case EMsvChangeEntry:
       
   235 			ChangeEntryL(aMessage);
       
   236 			break;
       
   237 		case EMsvGetChildren:
       
   238 			GetChildrenL(aMessage);
       
   239 			break;
       
   240 		case EMsvGetRemainingChildren:
       
   241 			GetRemainingChildrenL(aMessage);
       
   242 			break;
       
   243 		case EMsvDeleteEntries:
       
   244 			DeleteEntriesL(aMessage);
       
   245 			break;
       
   246 		case EMsvLockEntry:
       
   247 			LockEntryL(aMessage);
       
   248 			break;
       
   249 		case EMsvReleaseEntry:
       
   250 			ReleaseEntryL(aMessage);
       
   251 			break;
       
   252 		case EMsvReadStore:
       
   253 			ReadStoreL(aMessage);
       
   254 			break;
       
   255 		case EMsvLockStore:
       
   256 			LockStoreL(aMessage);
       
   257 			break;
       
   258 		case EMsvReleaseStore:
       
   259 			ReleaseStoreL(aMessage);
       
   260 			break;
       
   261 		case EMsvCancelOperation:
       
   262 			CancelOperationL(aMessage);
       
   263 			break;
       
   264 		case EMsvOperationCompletion:
       
   265 			OperationCompletionL(aMessage);
       
   266 			break;
       
   267 		case EMsvOperationProgress:
       
   268 			OperationProgressL(aMessage);
       
   269 			break;
       
   270 		case EMsvOperationSystemProgress:
       
   271 			OperationSystemProgressL(aMessage);
       
   272 			break;
       
   273 		case EMsvOperationMtm:
       
   274 			OperationMtmL(aMessage);
       
   275 			break;
       
   276 		case EMsvMoveEntries:
       
   277 			MoveEntriesL(aMessage);
       
   278 			break;
       
   279 		case EMsvCopyEntries:
       
   280 			CopyEntriesL(aMessage);
       
   281 			break;
       
   282 		case EMsvMtmCommand:
       
   283 			TransferCommandL(aMessage);
       
   284 			break;
       
   285 		case EMsvFillRegisteredMtmDllArray:
       
   286 			FillRegisteredMtmDllArray(aMessage);
       
   287 			break;
       
   288 		case EMsvInstallMtmGroup:
       
   289 			MtmGroupL(aMessage, ETrue);
       
   290 			break;
       
   291 		case EMsvDeInstallMtmGroup:
       
   292 			MtmGroupL(aMessage, EFalse);
       
   293 			break;
       
   294 		case EMsvUseMtmGroup:
       
   295 			UseMtmGroup(aMessage);
       
   296 			break;
       
   297 		case EMsvReleaseMtmGroup:
       
   298 			ReleaseMtmGroup(aMessage);
       
   299 			break;
       
   300 		case EMsvGetMtmGroupData:
       
   301 			GetMtmGroupDataL(aMessage);
       
   302 			break;
       
   303 		case EMsvGetMtmRequiredCapabilities:
       
   304 			GetMtmRequiredCapabilitiesL(aMessage);
       
   305 			break;
       
   306 		case EMsvCloseServer:
       
   307 			iMsvServer.CloseServer(aMessage);
       
   308 			break;
       
   309 		case EMsvStopService:
       
   310 			iMsvServer.StopServiceL(aMessage);
       
   311 			break;
       
   312 		case EMsvServiceActive:
       
   313 			iMsvServer.ServiceActive(aMessage);
       
   314 			break;
       
   315 		case EMsvServiceProgress:
       
   316 			ServiceProgressL(aMessage);
       
   317 			break;
       
   318 		case EMsvRemoveEntry:
       
   319 			RemoveEntry(aMessage);
       
   320 			break;
       
   321 		case EMsvGetMessageDirectory:
       
   322 			MessageFolderL(aMessage);
       
   323 			break;
       
   324 		case EMsvSlotAvailable:
       
   325 			if (iOperations.Count()<KMsvMaxSlotsAvailable)
       
   326 				aMessage.Complete(KErrNone);
       
   327 			else
       
   328 				aMessage.Complete(KErrServerBusy);
       
   329 			break;
       
   330 		case EMsvSetSessionAsObserver:
       
   331 			iObserverOnly=ETrue;
       
   332 			aMessage.Complete(KErrNone);
       
   333 			break;
       
   334 		case EMsvSetFailure:
       
   335 			iMsvServer.SetFailure((TMsvFailure)aMessage.Int0(), aMessage.Int1(), aMessage.Int2(), aMessage.Int3());
       
   336 			aMessage.Complete(KErrNone);
       
   337 			break;
       
   338 		case EMsvChangeAttributes:
       
   339 			ChangeAttributesL(aMessage);
       
   340 			break;
       
   341 		case EMsvGetChildIds:
       
   342 			GetChildIdsL(aMessage);
       
   343 			break;
       
   344 		case EMsvChangeDrive:
       
   345 			ChangeDriveL(aMessage);
       
   346 			break;
       
   347 		case EMsvOutstandingOperations:
       
   348 			OutstandingOperationsL(aMessage);
       
   349 			break;
       
   350 		case EMsvGetNotifySequence:
       
   351 			GetNotifySequenceL(aMessage);
       
   352 			break;
       
   353 		case EMsvSetReceiveEntyEvents:
       
   354 			SetReceiveEntryEvents(aMessage);
       
   355 			break;
       
   356 		case EMsvDecStoreReaderCount:
       
   357 			DecStoreReaderCountL(aMessage);
       
   358 			break;
       
   359 		case EMsvGetMessageDrive:
       
   360 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   361 			MessageDrive(aMessage);
       
   362 #else
       
   363 			aMessage.Complete(iMsvServer.Context().Config().iDrive);
       
   364 #endif
       
   365 			break;
       
   366 		case EMsvCreateAttachmentForWrite:
       
   367 			CreateAttachmentForWriteL(aMessage);
       
   368 			break;
       
   369 		case EMsvReplaceAttachmentForWrite:
       
   370 			ReplaceAttachmentForWriteL(aMessage);
       
   371 			break;
       
   372 		case EMsvOpenAttachment:
       
   373 			OpenAttachmentL(aMessage);
       
   374 			break;
       
   375 		case EMsvOpenAttachmentForWrite:
       
   376 			OpenAttachmentForWriteL(aMessage);
       
   377 			break;
       
   378 		case EMsvDeleteAttachment:
       
   379 			DeleteAttachmentL(aMessage);
       
   380 			break;
       
   381 		case EMsvRenameAttachment:
       
   382 			RenameAttachmentL(aMessage);
       
   383 			break;
       
   384 		case EMsvFileExists :
       
   385 			FileExistsL(aMessage);
       
   386 			break;
       
   387 		case EMsvGetAttachmentFilePath:
       
   388 			GetAttachmentFilePathL(aMessage);
       
   389 			break;
       
   390 		case EMsvOpenFileStoreForRead:
       
   391 			OpenFileStoreForReadL(aMessage);
       
   392 			break;
       
   393 		case EMsvOpenTempStoreFile:
       
   394 			OpenTempStoreFileL(aMessage);
       
   395 			break;
       
   396 		case EMsvReplaceFileStore:
       
   397 			ReplaceFileStoreL(aMessage);
       
   398 			break;
       
   399 		case EMsvDeleteFileStore:
       
   400 			DeleteFileStoreL(aMessage);
       
   401 			break;
       
   402 		case EMsvFileStoreExists:
       
   403 			FileStoreExistsL(aMessage);
       
   404 			break;
       
   405 		case EMsvGetAndClearIndexCorruptFlag:
       
   406 			aMessage.Complete(iMsvServer.Context().GetAndClearIndexCorruptFlagL());
       
   407 			break;
       
   408 		case EMsvCopyStore:
       
   409 			CopyStoreL(aMessage);
       
   410 			break;
       
   411 		case EMsvDeleteStore:
       
   412 			DeleteStoreL(aMessage);
       
   413 			break;
       
   414 		case EMsvDriveContainsStore:
       
   415 			aMessage.Complete(MessageServer::DriveContainsStore(iMsvServer.FileSession(),aMessage.Int0()));
       
   416 			break;
       
   417 		case EMsvMessageStoreDrivePresent:
       
   418 			aMessage.Complete(MessageServer::IsMessageStoreDrivePresentL(iMsvServer.FileSession()));			
       
   419 			break;
       
   420 		case EMsvGetBodyTextFilePath:
       
   421 			 BodyTextFilePathL(aMessage);
       
   422 			 break;
       
   423 		case EMsvOpenTextFileForRead:
       
   424 			 OpenPlainTextFileL(aMessage);
       
   425 			 break;
       
   426 		case EMsvCreatePlainTextFile:
       
   427 			 CreatePlainTextFileL(aMessage);
       
   428 			 break;
       
   429 		case EMsvDeletePlainTextFile:
       
   430 			DeletePlainTextFileL(aMessage); 
       
   431 			break;
       
   432 		case EMsvReplacePlainTextFile:
       
   433 			ReplacePlainTextFileL(aMessage);
       
   434 			break;
       
   435 
       
   436 		case EMsvGetNonOperationMtmData:
       
   437 			{
       
   438 			GetNonOperationMtmDataL(aMessage);
       
   439 			break;
       
   440 			}
       
   441 		case EMsvSearchSortOperation: // On HEADER AND BODY
       
   442 			SearchSortOnHeaderAndBodytMsgL(aMessage);
       
   443 			break;
       
   444 	
       
   445 		case EMsvSearchSortOnIndexEntry: //On INDEX ENTRY.
       
   446 			SearchSortOnIndexEntryL(aMessage);
       
   447 			break;
       
   448 
       
   449 		case EMsvGetResult ://Index entry result.
       
   450 			SendSearchSortIndexEntryResultL(aMessage);
       
   451 			break;
       
   452 		
       
   453 		case EMsvGetIdsOrResult://header result.
       
   454 			SendResultOrIdsToSearchL(aMessage);
       
   455 			break;
       
   456 		
       
   457 		case EMsvGetResultCount:
       
   458 			GetSearchSortResultCountL(aMessage);
       
   459 			break;
       
   460 			
       
   461 		case EMsvGetNextId:
       
   462 			GetResultInIteratorL(aMessage);
       
   463 			break;
       
   464 		
       
   465 		case EMsvGetNextEntry:
       
   466 			GetNextEntryInIteratorL(aMessage);
       
   467 			break;
       
   468 		
       
   469 		case EMsvGetQueryId:
       
   470 			GetQueryIDL(aMessage);
       
   471 			break;
       
   472 	
       
   473 		case EMsvUnmarQueryId:
       
   474 			QueryUnMarkedL(aMessage);
       
   475 			break;
       
   476 
       
   477 		case EMsvIdWithSortFiled:
       
   478 			UpdateSearchSortCacheWithSortFiledL(aMessage);
       
   479 			break;
       
   480 	
       
   481 		case EMsvSearchSortQueryId:
       
   482 			GetResultForQueryIDL(aMessage);
       
   483 			break;
       
   484 
       
   485 		case EMsvQueryData:
       
   486 			CopyQueryDataL(aMessage);
       
   487 			break;
       
   488 			
       
   489 		case EMsvGetSearchSortProgress:
       
   490 			GetSearchSortProgressInfoL(aMessage);
       
   491 			break;
       
   492 			
       
   493 		case EMsvCancelSearchSortOp:
       
   494 			SearchSortOperationCancelL(aMessage);
       
   495 			break;
       
   496 			
       
   497 		case EMsvUpdateAndSort:
       
   498 			UpdateSearchSortResultsAndSortByEntryL(aMessage);
       
   499 			break;
       
   500 
       
   501 // Code changes for PREQ 557.	   
       
   502 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   503 		case EMsvGetChildrenALL:
       
   504 			GetChildrenAllL(aMessage);
       
   505 			break;
       
   506 		case EMsvGetChildIdsALL:
       
   507 			GetChildIdsAllL(aMessage);
       
   508 			break;
       
   509 		case EMsvGetCurrentDriveInfo:
       
   510 			CurrentDriveInfoL(aMessage);
       
   511 			break;
       
   512 		case EMsvGetDriveList:
       
   513 			DriveListL(aMessage);	   
       
   514 			break;
       
   515 		case EMsvGetAvailableDriveList:
       
   516 			AvailableDriveListL(aMessage);
       
   517 			break;
       
   518 		case EMsvAddDriveToDriveList:
       
   519 			AddDriveL(aMessage);
       
   520 			break;
       
   521 		case EMsvRemoveDriveFromDriveList:
       
   522 			RemoveDriveL(aMessage); 
       
   523 			break;
       
   524 		case EMsvUpdateDrivePriority:
       
   525 			UpdateDrivePriorityL(aMessage);
       
   526 			break;
       
   527 #if (defined SYMBIAN_MESSAGESTORE_UNIT_TESTCODE)
       
   528 		case EMsvResetRepository:
       
   529 			ResetRepositoryL(aMessage);
       
   530 			break;
       
   531 		case EMsvPrintCache:
       
   532 			PrintCache(aMessage);
       
   533 			break;
       
   534 #endif	  // #if (defined SYMBIAN_MESSAGESTORE_UNIT_TESTCODE)
       
   535 #endif	  // #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   536 
       
   537 
       
   538 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   539 		case EMsvCreateStore:
       
   540 			CreateHeaderTableL(aMessage);
       
   541 			break;
       
   542 		case EMsvCheckStoreExists:
       
   543 			DoesStoreExistsL(aMessage);
       
   544 			break;
       
   545 		case EMsvLastErrorMessage:
       
   546 			LastErrorMessageL(aMessage);
       
   547 			break;
       
   548 		case EMsvCreateHeaderEntry:
       
   549 			CreateHeaderEntryL(aMessage);
       
   550 			break;
       
   551 		case EMsvLoadHeaderEntry:
       
   552 			LoadHeaderEntryL(aMessage);
       
   553 			break;
       
   554 		case EMsvUpdateHeaderEntry:
       
   555 			UpdateHeaderEntryL(aMessage);
       
   556 			break;
       
   557 		case EMsvDeleteHeaderEntry:
       
   558 			DeleteHeaderEntryL(aMessage);
       
   559 			break;							  
       
   560 		case EMsvCheckAnyStoreExists:
       
   561 			DoesAnyStoreExists(aMessage);
       
   562 			break;
       
   563 		case EMsvCheckHeaderTableExist:
       
   564 			{
       
   565 			DoesHeaderTableExist(aMessage);
       
   566 			break;
       
   567 			}
       
   568 		case EMsvGetConvertibleDriveList:
       
   569 			GetConvertibleDriveListL(aMessage);
       
   570 			break;
       
   571 		case EMsvConvertMessageStore:
       
   572 			ConvertMessageStoreL(aMessage);
       
   573 			break;
       
   574 		case EMsvGetConversionStatus:
       
   575 			GetConversionStatus(aMessage);
       
   576 			break;
       
   577 		case EMsvCancelConversionRequest:
       
   578 			CancelConversionRequestL(aMessage);
       
   579 			break;
       
   580 #endif	  // #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   581 		default:
       
   582 			PanicClient(aMessage, EMsvBadRequest);
       
   583 			break;
       
   584 		}
       
   585 	}
       
   586 
       
   587 
       
   588 void CMsvServerSession::QueueSessionEventRequestL(const RMessage2 &aMessage)
       
   589 //
       
   590 // The client is requesting notifcation of any changes
       
   591 //
       
   592 	{
       
   593 	if (iRequestQueued)
       
   594 		{
       
   595 		PanicClient(aMessage, EMsvDuplicatedChangeRequest);
       
   596 		return;
       
   597 		}
       
   598 
       
   599 	if (iChangeBuffer->IsEmpty())
       
   600 		{
       
   601 		// queue this request until a change occurs
       
   602 		iQueuedMessage = aMessage;
       
   603 		iRequestQueued = ETrue;
       
   604 		}
       
   605 	else
       
   606 		{
       
   607 		// inform client of change which has been queued
       
   608 		SendNotificationL(aMessage);
       
   609 		}
       
   610 	}
       
   611 
       
   612 void CMsvServerSession::SendNotificationL(const RMessage2& aMessage)
       
   613 	{
       
   614 	// inform client of change which has been queued
       
   615 	TPckgBuf<TUint32> sequence;
       
   616 	aMessage.WriteL(0, iChangeBuffer->Next(sequence()));
       
   617 
       
   618 	// Tell the client the notification sequence number
       
   619 	aMessage.WriteL(1, sequence);
       
   620 
       
   621 #ifndef _NO_SERVER_LOGGING_
       
   622 	iMsvServer.Log(_L("Sending notification sequence %d"), sequence());
       
   623 #endif
       
   624 
       
   625 	aMessage.Complete(KErrNone);
       
   626 	iChangeBuffer->Out();
       
   627 	}
       
   628 
       
   629 void CMsvServerSession::CancelSessionEventRequest(const RMessage2 &aMessage)
       
   630 //
       
   631 // The client is cancelling the outstanding request
       
   632 //
       
   633 	{
       
   634 	if (iRequestQueued)
       
   635 		{
       
   636 		iQueuedMessage.Complete(KErrNone);
       
   637 		iRequestQueued = EFalse;
       
   638 		}
       
   639 	aMessage.Complete(KErrNone);
       
   640 	}
       
   641 
       
   642 
       
   643 
       
   644 void CMsvServerSession::ReadBufferL(const RMessage2& aMessage, TInt aParam, HBufC8*& aBuffer)
       
   645 //
       
   646 // Copies a buffer from the client
       
   647 // Only fails if there is not enough memory to increase the buffer size (if needed)
       
   648 //
       
   649 	{
       
   650 	TInt desLen = aMessage.GetDesLength(aParam);
       
   651 
       
   652 	if(desLen >= 0)
       
   653 		{
       
   654 		TBool alloced=EFalse;
       
   655 		HBufC8* localBuffer=NULL;
       
   656 		
       
   657 		if (aBuffer==NULL)
       
   658 			{
       
   659 			localBuffer = HBufC8::NewLC(desLen);
       
   660 			alloced=ETrue;
       
   661 			}
       
   662 		else if (desLen > aBuffer->Des().MaxLength())
       
   663 			{
       
   664 			// we have to increase the size of iBuffer
       
   665 			aBuffer->Des().SetLength(0); // to avoid copying the contents
       
   666 			localBuffer = aBuffer->ReAlloc(desLen);
       
   667 			if (localBuffer==NULL)
       
   668 				{
       
   669 				User::Leave(KErrNoMemory); // unable to create buffer large enough
       
   670 				}
       
   671 			}
       
   672 		
       
   673 		if(localBuffer)
       
   674 			{
       
   675 			TPtr8 desPtr = localBuffer->Des();
       
   676 			aMessage.ReadL(aParam, desPtr);
       
   677 				
       
   678 			if (alloced)
       
   679 				{
       
   680 				CleanupStack::Pop(); // localBuffer
       
   681 				}
       
   682 			
       
   683 			aBuffer = localBuffer;  
       
   684 			}
       
   685 		//use aBuffer
       
   686 		else
       
   687 			{
       
   688 			TPtr8 desPtr = aBuffer->Des();
       
   689 			aMessage.ReadL(aParam, desPtr); 
       
   690 			}
       
   691 		}
       
   692 	else
       
   693 		{
       
   694 		// desLen is negative leave with an error.
       
   695 		User::Leave(KErrArgument);
       
   696 		}
       
   697 	}
       
   698 
       
   699 
       
   700 void CMsvServerSession::WriteBufferL(const RMessage2& aMessage, TInt aParam)
       
   701 //
       
   702 // Copies the packed entry buffer from the client
       
   703 // Only fails if the client buffer is not large enough
       
   704 //
       
   705 	{
       
   706 	TInt desLen = aMessage.GetDesMaxLength(aParam);
       
   707 	if (desLen < iBuffer->Des().Length())
       
   708 		User::Leave(KErrOverflow);
       
   709 	aMessage.WriteL(aParam, iBuffer->Des());
       
   710 	}
       
   711 
       
   712 
       
   713 void CMsvServerSession::WriteL(const RMessage2& aMessage, TInt aParam, const TDesC8& aDes)
       
   714 //
       
   715 // Write a descriptor to the client thread; if unsuccessful,leave so that client can handle it.
       
   716 //
       
   717 	{
       
   718 	TInt error = aMessage.Write(aParam, aDes);
       
   719 	if (error)
       
   720 		{
       
   721 		User::Leave(error);
       
   722 		}
       
   723 	}
       
   724 
       
   725 
       
   726 void CMsvServerSession::ReadL(const RMessage2& aMessage, TInt aParam, TDes8& aDes)
       
   727 //
       
   728 // Reads a descriptor from the client thread; if unsuccessful,leave so that client can handle it
       
   729 //
       
   730 	{
       
   731 	TInt error = aMessage.Read(aParam, aDes);
       
   732 	if (error)
       
   733 		{
       
   734 		User::Leave(error);
       
   735 		}
       
   736 	}
       
   737 
       
   738 
       
   739 
       
   740 void CMsvServerSession::CopyOperationDataL(const RMessage2 &aMessage)
       
   741 //
       
   742 // Copies operation data from the client and stores for the asynchronous operation request
       
   743 //
       
   744 	{
       
   745 	TMsvOp operationId = aMessage.Int0();
       
   746 	CMsvOperationData* opData = new (ELeave) CMsvOperationData(operationId);
       
   747 	CleanupStack::PushL(opData);
       
   748 	ReadBufferL(aMessage, 1, opData->iBuffer);
       
   749 	iOperationDataArray.AppendL(opData);
       
   750 	CleanupStack::Pop(); // opData
       
   751 	aMessage.Complete(KErrNone);
       
   752 	}
       
   753 
       
   754 void CMsvServerSession::CopyCommandDataL(const RMessage2 &aMessage)
       
   755 //
       
   756 // Copies operation data from the client and stores for the asynchronous operation request
       
   757 //
       
   758 	{
       
   759 	TMsvOp operationId = aMessage.Int0();
       
   760 	// operation data
       
   761 	CMsvOperationData* opData = new (ELeave) CMsvOperationData(operationId);
       
   762 	CleanupStack::PushL(opData);
       
   763 	ReadBufferL(aMessage, 1, opData->iBuffer);
       
   764 
       
   765 	// This is to check if the buffer is a valid , before it is added to 
       
   766 	// the operationData array
       
   767 	TMsvPackedOperation packedOperation(opData->iBuffer);
       
   768 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
   769 	CleanupStack::PushL(selection);
       
   770 	TInt command, temp;
       
   771 	packedOperation.UnpackL(*selection, command, temp);
       
   772 	CleanupStack::PopAndDestroy(selection);
       
   773 
       
   774 	iOperationDataArray.AppendL(opData);
       
   775 	
       
   776 	// additional parameter
       
   777 	CMsvOperationData* opParam = new (ELeave) CMsvOperationData(operationId);
       
   778 	CleanupStack::PushL(opParam);
       
   779 	ReadBufferL(aMessage, 2, opParam->iBuffer);
       
   780 	iOperationDataArray.AppendL(opParam);
       
   781 	CleanupStack::Pop(2, opData); // opParam, opData
       
   782 	//
       
   783 	aMessage.Complete(KErrNone);
       
   784 	}
       
   785 
       
   786 
       
   787 HBufC8* CMsvServerSession::RecoverOperationData(TMsvOp aOpId)
       
   788 //
       
   789 // Returns the operation data previously copied from the client
       
   790 // NOTE the calling function is responsible for deleting the returned HBufC
       
   791 //
       
   792 	{
       
   793 	// find the data
       
   794 	TInt count = 0;
       
   795 	TInt totalCount=iOperationDataArray.Count();
       
   796 	for (; count<totalCount; count++)
       
   797 		{
       
   798 		if (iOperationDataArray.At(count)->OperationId()==aOpId)
       
   799 			break;
       
   800 		}
       
   801 	// Panic during debug if cannot find data
       
   802 	__ASSERT_DEBUG(count<iOperationDataArray.Count(), PanicServer(EMsvEntryDataNotFound));
       
   803 	// At runtime, return NULL.
       
   804 	if(count >= iOperationDataArray.Count())
       
   805 		{
       
   806 		return NULL;
       
   807 		}
       
   808 	// return the data to the caller, and delete the container object
       
   809 	HBufC8* opData = iOperationDataArray.At(count)->iBuffer;
       
   810 	iOperationDataArray.At(count)->iBuffer = NULL;
       
   811 	delete iOperationDataArray.At(count);
       
   812 	iOperationDataArray.Delete(count);
       
   813 	return opData;
       
   814 	}
       
   815 
       
   816 
       
   817 void CMsvServerSession::CreateEntryL(const RMessage2 &aMessage)
       
   818 //
       
   819 // Create a entry in the index
       
   820 //
       
   821 	{
       
   822 	// Recover the operation data
       
   823 	TMsvOp operationId = aMessage.Int0();
       
   824 	HBufC8* opData = RecoverOperationData(operationId);
       
   825 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
   826 	if(opData == NULL)
       
   827 		{
       
   828 		aMessage.Complete(KErrArgument);
       
   829 		return;
       
   830 		}
       
   831 	
       
   832 	CleanupStack::PushL(opData);
       
   833 	// Unpack the data
       
   834 	TMsvPackedEntry packedEntry(opData);
       
   835 	TMsvEntry entry;
       
   836 	packedEntry.UnpackEntry(entry);
       
   837 
       
   838 	// Check the content of the entry are conforming to policy
       
   839 	if (!MsvUtils::ValidEntry(entry, ETrue))
       
   840 		{
       
   841 		PanicClient(aMessage, EMsvIncorrectEntry);
       
   842 		User::Leave(KErrNotSupported);
       
   843 		}
       
   844 
       
   845 	// Check if the entry is local or the entry is a service
       
   846 	TBool local=ETrue;
       
   847 	// Police request - client must be able to create the entry.
       
   848 	iMsvServer.PoliceCreateEntryL(aMessage, entry, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CreateEntryL"));
       
   849 
       
   850 	// For purpose of creating a service entry, it is considered local as an MTM
       
   851 	// is not required to do the work.
       
   852 	if( entry.iType == KUidMsvServiceEntry )
       
   853 		local = ETrue;
       
   854 
       
   855 	// start the change for local or remote entries
       
   856 	if (local)
       
   857 		{
       
   858 		TSecureId ownerId = aMessage.Int1();
       
   859 
       
   860 		DoCreateLocalEntryL(entry, operationId, aMessage, ownerId);
       
   861 		}
       
   862 	else
       
   863 		DoCreateRemoteEntryL(entry, operationId, aMessage);
       
   864 
       
   865 	CleanupStack::PopAndDestroy(); // opData
       
   866 	}
       
   867 
       
   868 void CMsvServerSession::DoCreateLocalEntryL(const TMsvEntry& aEntry, TMsvOp aOpId, const RMessage2 &aMessage, TSecureId aOwnerId)
       
   869 //
       
   870 // Create a local entry in the index
       
   871 //
       
   872 	{
       
   873 	CMsvLocalCreateOperation* operation = new(ELeave) CMsvLocalCreateOperation(aMessage, aOpId, aEntry, iMsvServer);
       
   874 	CleanupStack::PushL(operation);
       
   875 	iOperations.AppendL(operation);
       
   876 	CleanupStack::Pop(operation);
       
   877 	operation->Start(aOwnerId);
       
   878 	}
       
   879 
       
   880 void CMsvServerSession::DoCreateRemoteEntryL(const TMsvEntry& aEntry, TMsvOp aOpId, const RMessage2 &aMessage)
       
   881 //
       
   882 // Create a remote entry in the index
       
   883 //
       
   884 	{
       
   885 	// make sure that the operation can be added to the list
       
   886 	iOperations.SetReserveL(iOperations.Count()+1);
       
   887 
       
   888 	// create the operation and pass it to the server for starting/queuing
       
   889 	CMsvMtmOperation* operation = CMsvMtmOperation::NewL(aMessage, aOpId, aEntry.iMtm, aEntry.iServiceId, iSessionId, iMsvServer);
       
   890 	CleanupStack::PushL(operation);
       
   891 	operation->CreateL(aEntry);
       
   892 	iMsvServer.StartOperationL(*operation, iSessionId, aMessage, ETrue);
       
   893 	iOperations.AppendL(operation); // will not fail - see start of function
       
   894 	CleanupStack::Pop(); // operation
       
   895 	}
       
   896 
       
   897 
       
   898 void CMsvServerSession::ChangeEntryL(const RMessage2 &aMessage)
       
   899 //
       
   900 // Changes the entry
       
   901 //
       
   902 	{
       
   903 	// Recover the operation data
       
   904 	TMsvOp operationId = aMessage.Int0();
       
   905 	HBufC8* opData = RecoverOperationData(operationId);
       
   906 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
   907 	if(opData == NULL)
       
   908 		{
       
   909 		aMessage.Complete(KErrArgument);
       
   910 		return;
       
   911 		}
       
   912 		
       
   913 	CleanupStack::PushL(opData);
       
   914 	// Unpack the data
       
   915 	TMsvPackedEntry packedEntry(opData);
       
   916 	TMsvEntry entry;
       
   917 	packedEntry.UnpackEntry(entry);
       
   918 
       
   919 	// Check the content of the entry are conforming to policy
       
   920 	__ASSERT_DEBUG(MsvUtils::ValidEntry(entry), PanicClient(aMessage, EMsvIncorrectEntry));
       
   921 	if (!MsvUtils::ValidEntry(entry))
       
   922 		User::Leave(KErrNotSupported);
       
   923 
       
   924 	// Check if the entry is local or the entry is a service
       
   925 	TBool local=ETrue;
       
   926 	// Police request - client must be able to modify the entry.
       
   927 	iMsvServer.PoliceModifyEntryL(aMessage, entry, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ChangeEntryL"));
       
   928 
       
   929 	// For purpose of changing a service entry, it is considered local as an MTM
       
   930 	// is not required to do the work.
       
   931 	if( entry.iType == KUidMsvServiceEntry )
       
   932 		local = ETrue;
       
   933 
       
   934 	// start the change for local or remote entries
       
   935 	if (local)
       
   936 		{
       
   937 		// Extract the owner ID from the message.
       
   938 		TSecureId ownerId = aMessage.Int1();
       
   939 
       
   940 		DoChangeLocalEntryL(entry, operationId, aMessage, ownerId);
       
   941 		}
       
   942 	else
       
   943 		DoChangeRemoteEntryL(entry, operationId, aMessage);
       
   944 
       
   945 	CleanupStack::PopAndDestroy(); // opData
       
   946 	}
       
   947 
       
   948 void CMsvServerSession::DoChangeLocalEntryL(const TMsvEntry& aEntry, TMsvOp aOpId, const RMessage2 &aMessage, TSecureId aOwnerId)
       
   949 //
       
   950 // Create a local entry in the index
       
   951 //
       
   952 	{
       
   953 	CMsvLocalChangeOperation* operation = new(ELeave) CMsvLocalChangeOperation(aMessage, aOpId, aEntry, iMsvServer);
       
   954 	CleanupStack::PushL(operation);
       
   955 	// Check the given owner ID - if NULL then this is not a forced update, but
       
   956 	// need to set the ID to that of the requesting process.
       
   957 	TBool forcedUpdate = (aOwnerId != aMessage.SecureId());
       
   958 
       
   959 	operation->StartL(aOwnerId, forcedUpdate);
       
   960 	iOperations.AppendL(operation);
       
   961 	CleanupStack::Pop(); //operation
       
   962 	}
       
   963 
       
   964 
       
   965 void CMsvServerSession::DoChangeRemoteEntryL(const TMsvEntry& aEntry, TMsvOp aOpId, const RMessage2 &aMessage)
       
   966 //
       
   967 // Change a entry under a remote service
       
   968 //
       
   969 	{
       
   970 	// make sure that the operation can be added to the list
       
   971 	iOperations.SetReserveL(iOperations.Count()+1);
       
   972 
       
   973 	// create the operation
       
   974 	CMsvMtmOperation* operation = CMsvMtmOperation::NewL(aMessage, aOpId, aEntry.iMtm, aEntry.iServiceId, iSessionId, iMsvServer);
       
   975 	CleanupStack::PushL(operation);
       
   976 	operation->ChangeL(aEntry);
       
   977 	iMsvServer.StartOperationL(*operation, iSessionId, aMessage, ETrue);
       
   978 	iOperations.AppendL(operation); // will not fail - see start of function
       
   979 	CleanupStack::Pop(); // operation
       
   980 	}
       
   981 
       
   982 
       
   983 void CMsvServerSession::PackEntryAndWriteBufferL(const RMessage2 &aMessage, const TInt aParam, const TMsvEntry& aEntry)
       
   984 //
       
   985 //
       
   986 //
       
   987 	{
       
   988 	// package the entry
       
   989 	TMsvPackedEntry packedEntry(iBuffer);
       
   990 	TInt error = packedEntry.PackEntry(aEntry);
       
   991 	while(error!=KErrNone)
       
   992 		{
       
   993 		// increase the size of the buffer and try again
       
   994 		iBuffer->Des().SetLength(0); // to avoid copying contents
       
   995 		iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
   996 		error = packedEntry.PackEntry(aEntry);
       
   997 		}
       
   998 	WriteBufferL(aMessage, aParam);
       
   999 	}
       
  1000 
       
  1001 void CMsvServerSession::GetEntryL(const RMessage2 &aMessage)
       
  1002 //
       
  1003 // Returns the index entry for the given id
       
  1004 //
       
  1005 	{
       
  1006 	// copy across the entry id to get
       
  1007 	TMsvId id = aMessage.Int0();
       
  1008 	TMsvEntry* entryPtr;
       
  1009 	TSecureId ownerId;
       
  1010 	TInt error = KErrNone;
       
  1011 	error = iMsvServer.IndexAdapter().GetEntry(id, entryPtr, ownerId);
       
  1012 	if (error==KErrNone)
       
  1013 		{
       
  1014 		// Police request - client must be able to read the entry.
       
  1015 		iMsvServer.PoliceReadEntryL(aMessage, ownerId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetEntryL"));
       
  1016 
       
  1017 		// get the owning service and write that back
       
  1018 		TPckgBuf<TMsvId> service;
       
  1019 		if (id==KMsvRootIndexEntryId)
       
  1020 			service()=KMsvRootIndexEntryId;
       
  1021 		else
       
  1022 			{
       
  1023 			iMsvServer.IndexAdapter().OwningService(id, service()); // error ignore as the entry exists
       
  1024 			}
       
  1025 
       
  1026 		WriteL(aMessage, 2, service);
       
  1027 		// write the entry back
       
  1028 		PackEntryAndWriteBufferL(aMessage, 1, *entryPtr);
       
  1029 		}
       
  1030 	aMessage.Complete(error);
       
  1031 	}
       
  1032 
       
  1033 void CMsvServerSession::OutstandingOperationsL(const RMessage2& aMessage)
       
  1034 	{
       
  1035 	TPckgBuf<TInt> outstanding;
       
  1036 	outstanding() = iMsvServer.OutstandingOperations() > 0;
       
  1037 	WriteL(aMessage, 0, outstanding);
       
  1038 	aMessage.Complete(KErrNone);
       
  1039 	}
       
  1040 
       
  1041 void CMsvServerSession::GetNotifySequenceL(const RMessage2& aMessage)
       
  1042 	{
       
  1043 	TPckgBuf<TUint32> sequence;
       
  1044 	sequence() = iChangeBuffer->NotifySequence();
       
  1045 	WriteL(aMessage, 0, sequence);
       
  1046 	aMessage.Complete(KErrNone);
       
  1047 	}
       
  1048 
       
  1049 void CMsvServerSession::GetChildrenL(const RMessage2 &aMessage)
       
  1050 //
       
  1051 // Gets the children of an entry
       
  1052 //
       
  1053 	{
       
  1054 	// reset
       
  1055 	iChildrenSelection->Reset();
       
  1056 	iChildrenSelectionIds->Reset();
       
  1057 
       
  1058 	// copy across the children details structure
       
  1059 	TPckgBuf<TMsvChildrenDetails> children;
       
  1060 	aMessage.ReadL(0, children);
       
  1061 
       
  1062 	// Check that the children details arguments are empty. Panic in debug mode but try to handle it gracefully in
       
  1063 	// release code.
       
  1064 	__ASSERT_DEBUG( children().iTotalNumberChildren==0 &&
       
  1065 					children().iNumberChildrenInArray==0, PanicServer(EMsvChildrenDetailsNotEmpty2));
       
  1066 
       
  1067 	if( children().iTotalNumberChildren != 0 || children().iNumberChildrenInArray != 0 )
       
  1068 		{
       
  1069 		aMessage.Complete(KErrArgument);
       
  1070 		return;
       
  1071 		}
       
  1072 		
       
  1073 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
  1074 	// Return error, if the passed parent-id 
       
  1075 	// is a non-current standard id.
       
  1076 	if( (IsStandardId(children().iParentId)) &&
       
  1077 		(KCurrentDriveId != GetDriveId(children().iParentId))
       
  1078 	  )
       
  1079 		{
       
  1080 		aMessage.Complete(KErrArgument);
       
  1081 		return;
       
  1082 		}	   
       
  1083 #endif
       
  1084 
       
  1085 	// copy across the sort order
       
  1086 	TPckgBuf<TMsvSelectionOrdering> order;
       
  1087 	aMessage.ReadL(1, order);
       
  1088 
       
  1089 	// get the children as a selection
       
  1090 	// Need to filter the list via the client secure ID if the client is not
       
  1091 	// trusted with Read User Data.
       
  1092 	TBool filterByOwnerId = !aMessage.HasCapability(ECapabilityReadUserData);
       
  1093 	iMsvServer.IndexAdapter().GetChildrenL(children().iParentId, *iChildrenSelection, order(), KUidMsvNullEntry, filterByOwnerId, aMessage.SecureId());
       
  1094 	// return number of children
       
  1095 	children().iTotalNumberChildren = iChildrenSelection->Count();
       
  1096 
       
  1097 	// package up the entries
       
  1098 	iBuffer->Des().SetMax();
       
  1099 	TMsvPackedEntryArray packedEntryArray(iBuffer);
       
  1100 	TInt count=0;
       
  1101 	TInt error=KErrNone;
       
  1102 	TInt totalCount=iChildrenSelection->Count();
       
  1103 	for (; count<totalCount; count++)
       
  1104 		{
       
  1105 		error = packedEntryArray.PackEntry(*iChildrenSelection->At(count));
       
  1106 		if (error)
       
  1107 			{
       
  1108 			children().iLastEntryInArray = count;
       
  1109 			break;
       
  1110 			}
       
  1111 		}
       
  1112 	// return number of children in the array
       
  1113 	children().iNumberChildrenInArray = count;
       
  1114 
       
  1115 	// Try to write the buffer to the client (if any entries are in the array)
       
  1116     if (children().iNumberChildrenInArray)
       
  1117     	{
       
  1118         TRAPD(bufferError, WriteL(aMessage, 2, iBuffer->Des()));
       
  1119         if(bufferError!=KErrNone)
       
  1120         	{
       
  1121         	// Writing data into client buffer is failed, we should not send incorrect count to client.
       
  1122 			error = bufferError;
       
  1123         	children().iNumberChildrenInArray = 0;
       
  1124         	children().iLastEntryInArray = 0;
       
  1125         	}
       
  1126         }
       
  1127 
       
  1128 	// write the children to client
       
  1129 	WriteL(aMessage, 0, children);
       
  1130 
       
  1131 	if (error==KErrNone)
       
  1132 		{
       
  1133 		// reset the member data
       
  1134 		iChildrenSelection->Reset();
       
  1135 		}
       
  1136 	else
       
  1137 		{
       
  1138 		// keep an list of the ids separatelyin case they are deleted by another client
       
  1139 		TInt totalCount=iChildrenSelection->Count();
       
  1140 		for (count=0; count<totalCount; count++)
       
  1141 			iChildrenSelectionIds->AppendL(iChildrenSelection->At(count)->Id());
       
  1142 		iChildrenDetails = children();
       
  1143 		}
       
  1144 
       
  1145 	// signal the client and finished with selection
       
  1146 	aMessage.Complete(error);
       
  1147 	}
       
  1148 
       
  1149 
       
  1150 
       
  1151 
       
  1152 void CMsvServerSession::GetRemainingChildrenL(const RMessage2 &aMessage)
       
  1153 //
       
  1154 // Gets the remaining children of an entry when the buffer is to small
       
  1155 //
       
  1156 	{
       
  1157 	// Check the arguments. Panic in debug mode but try to handle it gracefully in
       
  1158 	// release code.
       
  1159 	__ASSERT_DEBUG(iChildrenSelection->Count()!=0, PanicServer(EMsvChildrenSelectionNull));
       
  1160 	__ASSERT_DEBUG(iChildrenSelection->Count() == iChildrenSelectionIds->Count(), PanicServer(EMsvChildrenSelectionCountsMismatch));
       
  1161 
       
  1162 	if( (iChildrenSelection->Count()==0) || (iChildrenSelection->Count() != iChildrenSelectionIds->Count()) )
       
  1163 		{
       
  1164 		aMessage.Complete(KErrArgument);
       
  1165 		return;
       
  1166 		}
       
  1167 
       
  1168 	// copy across the children details structure
       
  1169 	TPckgBuf<TMsvChildrenDetails> pDetails;
       
  1170 	aMessage.ReadL(0, pDetails);
       
  1171 
       
  1172 	// Check the arguments. Panic in debug mode but try to handle it gracefully in
       
  1173 	// release code.
       
  1174 	__ASSERT_DEBUG( iChildrenDetails.iParentId == pDetails().iParentId &&
       
  1175 					iChildrenDetails.iTotalNumberChildren == pDetails().iTotalNumberChildren &&
       
  1176 					iChildrenDetails.iNumberChildrenInArray == pDetails().iNumberChildrenInArray, PanicServer(EMsvChildrenDetailsNotEmpty1));
       
  1177 
       
  1178 	if( iChildrenDetails.iParentId != pDetails().iParentId ||
       
  1179 		iChildrenDetails.iTotalNumberChildren != pDetails().iTotalNumberChildren ||
       
  1180 		iChildrenDetails.iNumberChildrenInArray != pDetails().iNumberChildrenInArray )
       
  1181 		{
       
  1182 		aMessage.Complete(KErrArgument);
       
  1183 		return;
       
  1184 		}
       
  1185 
       
  1186 	iChildrenDetails=pDetails();
       
  1187 	// Make a local copy of details, this needs to be used when the client buffer is not sufficient enough to
       
  1188 	// pack data.
       
  1189 	TMsvChildrenDetails localCopy = pDetails();
       
  1190 	
       
  1191 	// package up the entries
       
  1192 	iBuffer->Des().SetMax();
       
  1193 	TMsvPackedEntryArray packedEntryArray(iBuffer);
       
  1194 	TInt error=KErrNone;
       
  1195 	TInt count=iChildrenDetails.iLastEntryInArray;
       
  1196 	iChildrenDetails.iLastEntryInArray=-1;
       
  1197 
       
  1198 	TInt childCount=iChildrenSelection->Count();
       
  1199 	TInt missingCount = 0;
       
  1200 
       
  1201 	for (; count<childCount; count++)
       
  1202 		{
       
  1203 		// The iChildrenSelection contains pointers to TMsvEntry objects. If one of those
       
  1204 		// objects has been deleted then the pointer will no longer be valid so we need
       
  1205 		// to get the entry again using the ID from iChildrenSelectionIds.
       
  1206 		TMsvEntry* entryPtr;
       
  1207 		TInt err = KErrNone;
       
  1208 		err = iMsvServer.IndexAdapter().GetEntry(iChildrenSelectionIds->At(count), entryPtr);
       
  1209 		if (err != KErrNotFound)
       
  1210 			{
       
  1211 #ifndef _NO_SERVER_LOGGING_
       
  1212 			if (entryPtr != iChildrenSelection->At(count))
       
  1213 				{
       
  1214 				iMsvServer.Log(_L("GetRemainingChildrenL() - Ptr mismatch for id %d (%x %x)"), iChildrenSelectionIds->At(count), iChildrenSelection->At(count), entryPtr);
       
  1215 				}
       
  1216 #endif
       
  1217 
       
  1218 			error = packedEntryArray.PackEntry(*entryPtr);
       
  1219 
       
  1220 			if (error)
       
  1221 				{
       
  1222 				// the buffer is not large enough to pack all the entries
       
  1223 				iChildrenDetails.iLastEntryInArray = count;
       
  1224 				if ((count - missingCount - pDetails().iLastEntryInArray) == 0)
       
  1225 					{
       
  1226 					// Couldn't fit entry in buffer
       
  1227 					// Increase size of buffer - client will do this as well
       
  1228 					iBuffer->Des().SetLength(0);
       
  1229 					iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  1230 					}
       
  1231 				break;
       
  1232 
       
  1233 				}
       
  1234 			}
       
  1235 		else
       
  1236 			{
       
  1237 #ifndef _NO_SERVER_LOGGING_
       
  1238 			iMsvServer.Log(_L("GetRemainingChildrenL() - Ignore missing id %d"), iChildrenSelectionIds->At(count));
       
  1239 #endif
       
  1240 
       
  1241 			++missingCount;
       
  1242 			}
       
  1243 		}
       
  1244 
       
  1245 	// return number of children in the array
       
  1246 	iChildrenDetails.iNumberChildrenInArray = count - missingCount - pDetails().iLastEntryInArray;
       
  1247 
       
  1248 	// write the array to the client (if any entries are in the array)
       
  1249 	TRAPD(err, WriteBufferL(aMessage, 2));
       
  1250 	if(err)
       
  1251 		{
       
  1252 		if(err == KErrOverflow)
       
  1253 		 {
       
  1254 		 localCopy.iNumberChildrenInArray = 0;
       
  1255 		 iChildrenDetails = localCopy;
       
  1256 		 pDetails() = iChildrenDetails;
       
  1257 		 WriteL(aMessage, 0, pDetails);
       
  1258 		 User::Leave(KErrOverflow);
       
  1259 		 }
       
  1260 	 else
       
  1261 		 {
       
  1262 		 User::Leave(err);
       
  1263 		 }   
       
  1264 	 }   
       
  1265 			 
       
  1266 
       
  1267 	// write the details to client
       
  1268 	pDetails() = iChildrenDetails;
       
  1269 	WriteL(aMessage, 0, pDetails);
       
  1270 
       
  1271 	if (error==KErrNone)
       
  1272 		{
       
  1273 		// reset the member data
       
  1274 		iChildrenDetails = TMsvChildrenDetails();
       
  1275 		iChildrenSelection->Reset();
       
  1276 		iChildrenSelectionIds->Reset();
       
  1277 		}
       
  1278 
       
  1279 	// signal the client and finished with selection
       
  1280 	aMessage.Complete(error);
       
  1281 	}
       
  1282 
       
  1283 
       
  1284 void CMsvServerSession::DeleteEntriesL(const RMessage2 &aMessage)
       
  1285 //
       
  1286 // Deleting entries
       
  1287 //
       
  1288 	{
       
  1289 	// Recover the operation data
       
  1290 	TMsvOp operationId = aMessage.Int0();
       
  1291 	HBufC8* opData = RecoverOperationData(operationId);
       
  1292 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
  1293 	if(opData == NULL)
       
  1294 		{
       
  1295 		aMessage.Complete(KErrArgument);
       
  1296 		return;
       
  1297 		}
       
  1298 	
       
  1299 	CleanupStack::PushL(opData);
       
  1300 	// unpack the data
       
  1301 	TMsvPackedOperation packedOperation(opData);
       
  1302 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
  1303 	CleanupStack::PushL(selection);
       
  1304 	TInt temp1, temp2;
       
  1305 	packedOperation.UnpackL(*selection, temp1, temp2);
       
  1306 
       
  1307 /**
       
  1308 Added code to begin transaction for bulk deletion.
       
  1309 */
       
  1310 	iMsvServer.IndexAdapter().GetDbAdapter()->BeginTransactionL();
       
  1311 
       
  1312 	if (selection->Count())
       
  1313 		{
       
  1314 		// Check if the entries are local (inc services) or remote
       
  1315 		TBool local;
       
  1316 		TMsvEntry* entry=NULL;
       
  1317 		User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(selection->At(0), entry));
       
  1318 		// Police request - client must be able to modify the entry.
       
  1319 		iMsvServer.PoliceModifyEntryL(aMessage, *entry, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DeleteEntriesL"));
       
  1320 		// start the move for local or remote messages
       
  1321 		if (local || entry->iType==KUidMsvServiceEntry)
       
  1322 			{
       
  1323 			DoDeleteLocalEntriesL(selection, operationId, aMessage);
       
  1324 			TMsgType aType(EDeletedMsg);
       
  1325 			if(CMSvSearchSortCacheManager::Instance()->iManagerEntry != NULL)
       
  1326 				{
       
  1327 				if(CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count()>0)
       
  1328 					{
       
  1329 					TInt count=selection->Count();
       
  1330 					TInt i=0;
       
  1331 					while(i < count)
       
  1332 						{
       
  1333 						CMsvSearchSortDeltaCache::Instance()->EntryInDeltaCache(selection->At(i),aType);
       
  1334 						i++;	
       
  1335 						}
       
  1336 					}
       
  1337 				}
       
  1338 			}
       
  1339 		else
       
  1340 			{
       
  1341 			DoDeleteRemoteEntriesL(selection, operationId, aMessage);
       
  1342 			TMsgType aType(EDeletedMsg);
       
  1343 			if(CMSvSearchSortCacheManager::Instance()->iManagerEntry != NULL)
       
  1344 				{
       
  1345 				if(CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count()>0)
       
  1346 					{
       
  1347 					TInt count=selection->Count();
       
  1348 					TInt i=0;
       
  1349 					while(i < count)
       
  1350 						{
       
  1351 						CMsvSearchSortDeltaCache::Instance()->EntryInDeltaCache(selection->At(i),aType);
       
  1352 						i++;	
       
  1353 						}
       
  1354 					}
       
  1355 				}
       
  1356 			}
       
  1357 		}
       
  1358 	else
       
  1359 		{
       
  1360 		PanicClient(aMessage, EMsvNoEntriesInDeleteSelection);
       
  1361 		aMessage.Complete(KErrNotFound);
       
  1362 		}
       
  1363 
       
  1364 	CleanupStack::PopAndDestroy(); // opData
       
  1365 	}
       
  1366 
       
  1367 
       
  1368 void CMsvServerSession::DoDeleteLocalEntriesL(CMsvEntrySelection*& aSelection, TMsvOp aOpId, const RMessage2 &aMessage)
       
  1369 //
       
  1370 //
       
  1371 //
       
  1372 	{
       
  1373 	CMsvLocalDeleteOperation* operation = new(ELeave) CMsvLocalDeleteOperation(aMessage, aOpId, aSelection, iMsvServer);
       
  1374 	CleanupStack::Pop(); // selection
       
  1375 	CleanupStack::PushL(operation);
       
  1376 	operation->StartL();
       
  1377 	iOperations.AppendL(operation);
       
  1378 	CleanupStack::Pop(); // operation
       
  1379 	}
       
  1380 
       
  1381 
       
  1382 void CMsvServerSession::DoDeleteRemoteEntriesL(CMsvEntrySelection*& aSelection, TMsvOp aOpId, const RMessage2 &aMessage)
       
  1383 //
       
  1384 //
       
  1385 //
       
  1386 	{
       
  1387 	// make sure that the operation can be added to the list
       
  1388 	iOperations.SetReserveL(iOperations.Count()+1);
       
  1389 
       
  1390 	// find the first entry being acted on
       
  1391 	TMsvEntry* entry=NULL;
       
  1392 	User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(aSelection->At(0), entry));
       
  1393 	// create the operation
       
  1394 	CMsvMtmOperation* operation = CMsvMtmOperation::NewL(aMessage, aOpId, entry->iMtm, entry->iServiceId, iSessionId, iMsvServer);
       
  1395 	CleanupStack::Pop();
       
  1396 	CleanupStack::PushL(operation);
       
  1397 
       
  1398 	// set up the operation type and pass the data
       
  1399 	operation->DeleteAll(aSelection);
       
  1400 
       
  1401 	// either start the operation or queue it
       
  1402 	iMsvServer.StartOperationL(*operation, iSessionId, aMessage, ETrue);
       
  1403 	iOperations.AppendL(operation); // will not fail - see start of function
       
  1404 	CleanupStack::Pop(); // operation
       
  1405 	}
       
  1406 
       
  1407 
       
  1408 void CMsvServerSession::LockEntryL(const RMessage2 &aMessage)
       
  1409 //
       
  1410 // Locks an entry in the index
       
  1411 //
       
  1412 	{
       
  1413 	TMsvId id = aMessage.Int0();
       
  1414 	aMessage.Complete(iMsvServer.IndexAdapter().LockEntry(id));
       
  1415 	}
       
  1416 
       
  1417 
       
  1418 void CMsvServerSession::ReleaseEntryL(const RMessage2 &aMessage)
       
  1419 //
       
  1420 // Releases the lock on an entry in the index
       
  1421 //
       
  1422 	{
       
  1423 	TMsvId id = aMessage.Int0();
       
  1424 	aMessage.Complete(iMsvServer.IndexAdapter().ReleaseEntry(id));
       
  1425 	}
       
  1426 
       
  1427 void CMsvServerSession::ReadStoreL(const RMessage2 &aMessage)
       
  1428 //
       
  1429 // Can only read from a store that has nnot been locked
       
  1430 //
       
  1431 	{
       
  1432 	// Fail now if the index says it's not available
       
  1433 	User::LeaveIfError(iMsvServer.IndexAdapter().ErrorState());
       
  1434 	TMsvId id = aMessage.Int0();
       
  1435 
       
  1436 	// Police request - client must be able to read the entry.
       
  1437 	iMsvServer.PoliceReadEntryL(aMessage, id, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ReadStoreL"));
       
  1438 
       
  1439 	TBool locked;
       
  1440 	TInt error = KErrNone;
       
  1441 	error = iMsvServer.IndexAdapter().IsStoreLocked(id, locked);
       
  1442 	if (error==KErrNone && locked)
       
  1443 		error = KErrAccessDenied;
       
  1444 	if(error==KErrNone)
       
  1445 		{
       
  1446 		iMsvServer.IndexAdapter().IncStoreReaderCount(id);
       
  1447 		}
       
  1448 
       
  1449 	aMessage.Complete(error);
       
  1450 	}
       
  1451 
       
  1452 
       
  1453 void CMsvServerSession::LockStoreL(const RMessage2 &aMessage)
       
  1454 //
       
  1455 // Tries to locks the store
       
  1456 // Adds the id to the list of locked entries
       
  1457 //
       
  1458 	{
       
  1459 	// Fail now if the index says it's not available
       
  1460 	User::LeaveIfError(iMsvServer.IndexAdapter().ErrorState());
       
  1461 	TMsvId id = aMessage.Int0();
       
  1462 
       
  1463 	// Police request - client must be able to modify the entry.
       
  1464 	iMsvServer.PoliceModifyEntryL(aMessage, id, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::LockStoreL"));
       
  1465 
       
  1466 	iLockedStores.AppendL(id); // ensure that the entry cannot be locked but not added to the list
       
  1467 	TInt error = KErrNone;
       
  1468 	error = iMsvServer.IndexAdapter().LockStore(id);
       
  1469 	if (error)
       
  1470 		iLockedStores.Delete(iLockedStores.Count()-1);
       
  1471 
       
  1472 	aMessage.Complete(error);
       
  1473 	}
       
  1474 
       
  1475 
       
  1476 void CMsvServerSession::ReleaseStoreL(const RMessage2 &aMessage)
       
  1477 //
       
  1478 // Releases the lock on a store
       
  1479 // Removes the id from the list of locked entries
       
  1480 //
       
  1481 	{
       
  1482 	TMsvId id = aMessage.Int0();
       
  1483 
       
  1484 	// Police request - client must be able to modify the entry.
       
  1485 	iMsvServer.PoliceModifyEntryL(aMessage, id, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ReleaseStoreL"));
       
  1486 	TInt error = KErrNone;
       
  1487 	error = iMsvServer.IndexAdapter().ReleaseStore(id);
       
  1488 	TInt count = iLockedStores.Count();
       
  1489 	while (count--)
       
  1490 		{
       
  1491 		if (iLockedStores.At(count)==id)
       
  1492 			{
       
  1493 			iLockedStores.Delete(count);
       
  1494 			break;
       
  1495 			}
       
  1496 		}
       
  1497 
       
  1498 	__ASSERT_DEBUG(count>=0, PanicClient(aMessage, EMsvReleasingUnknownStore));
       
  1499 
       
  1500 	aMessage.Complete(error);
       
  1501 	}
       
  1502 
       
  1503 
       
  1504 void CMsvServerSession::DecStoreReaderCountL(const RMessage2 &aMessage)
       
  1505 //
       
  1506 // Decrements the reader count on a store
       
  1507 //
       
  1508 	{
       
  1509 	TMsvId id = aMessage.Int0();
       
  1510 
       
  1511 	// Police request - client must be able to read the entry.
       
  1512 	iMsvServer.PoliceReadEntryL(aMessage, id, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DecStoreReaderCountL"));
       
  1513 
       
  1514 	TInt error = KErrNone;
       
  1515 	error = iMsvServer.IndexAdapter().DecStoreReaderCount(id);
       
  1516 	aMessage.Complete(error);
       
  1517 	}
       
  1518 
       
  1519 void CMsvServerSession::NotifyChangedL(TMsvNotifBuffer& aChange, TBool aQueue)
       
  1520 //
       
  1521 // This session is being informed that a change has occured
       
  1522 // Will only leave if a write to a client fails
       
  1523 //
       
  1524 	{
       
  1525 	// queue the change in FIFO buffer
       
  1526 	iChangeBuffer->InL(aChange);
       
  1527 
       
  1528 #ifndef _NO_SERVER_LOGGING_
       
  1529 	iMsvServer.Log(_L("Queued notification sequence %d"), iChangeBuffer->NotifySequence());
       
  1530 #endif
       
  1531 
       
  1532 	// Only actually send the notification when we have a context
       
  1533 	if (iRequestQueued && !aQueue &&  iQueuedMessage.Handle())
       
  1534 		{
       
  1535 		// inform client of change
       
  1536 		SendNotificationL(iQueuedMessage);
       
  1537 		iRequestQueued = EFalse;
       
  1538 		}
       
  1539 	}
       
  1540 
       
  1541 
       
  1542 TBool CMsvServerSession::IsInUse(TUid aMtmTypeUid)
       
  1543 	{
       
  1544 	TBool isinuse=EFalse;
       
  1545 	TInt index=0;
       
  1546 	TInt count=iMtmGroupRefCountArray.Count();
       
  1547 	for (; (index<count) && !isinuse; index++)
       
  1548 		if (iMtmGroupRefCountArray[index].iMtmTypeUid==aMtmTypeUid)
       
  1549 			isinuse=ETrue;
       
  1550 	return isinuse;
       
  1551 	}
       
  1552 
       
  1553 void CMsvServerSession::CancelOperationL(const RMessage2 &aMessage)
       
  1554 //
       
  1555 // Cancels an operation
       
  1556 //
       
  1557 	{
       
  1558 	TMsvOp op = aMessage.Int0();
       
  1559 
       
  1560 	TInt error=KErrNotFound;
       
  1561 	for (TInt count=0; count<iOperations.Count(); count++)
       
  1562 		{
       
  1563 		if (iOperations.At(count)->Id()==op)
       
  1564 			{
       
  1565 			error=KErrNone;
       
  1566 
       
  1567 			iOperations.At(count)->Cancel();
       
  1568 
       
  1569 			if (iOperations.At(count)->State()!=EMsvOperationQueued)
       
  1570 				{
       
  1571 				TPtrC8 desPtr = iOperations.At(count)->Progress();
       
  1572 				if (desPtr.Length() > KMsvProgressBufferLength)
       
  1573 					{
       
  1574 					PanicClient(aMessage, EMsvProgressBufferExceeds256);
       
  1575 					error = KErrOverflow;
       
  1576 					}
       
  1577 				else
       
  1578 					{
       
  1579 					WriteL(aMessage, 1, desPtr);
       
  1580 					error = desPtr.Length();
       
  1581 					}
       
  1582 				}
       
  1583 			else
       
  1584 				{
       
  1585 				error=KErrNotReady;
       
  1586 				}
       
  1587 
       
  1588 			delete iOperations.At(count);
       
  1589 			iOperations.Delete(count);
       
  1590 			break;
       
  1591 			}
       
  1592 		}
       
  1593 
       
  1594 	aMessage.Complete(error);
       
  1595 	}
       
  1596 
       
  1597 
       
  1598 void CMsvServerSession::OperationCompletionL(const RMessage2 &aMessage)
       
  1599 //
       
  1600 // Gets the final progress of an operation and deletes all objects
       
  1601 //
       
  1602 	{
       
  1603 	TMsvOp op = aMessage.Int0();
       
  1604 
       
  1605 	TInt error=KErrNotFound;
       
  1606 	for (TInt count=0; count<iOperations.Count(); count++)
       
  1607 		{
       
  1608 		if (iOperations.At(count)->Id()==op)
       
  1609 			{
       
  1610 			__ASSERT_DEBUG(iOperations.At(count)->State() == EMsvOperationCompleted || iOperations.At(count)->State() == EMsvOperationFailed, PanicServer(EMsvOperationNotCompleted));
       
  1611 			if (iOperations.At(count)->State() == EMsvOperationFailed)
       
  1612 				{
       
  1613 				// there will be no progress available
       
  1614 				error=KErrNone;
       
  1615 				}
       
  1616 			else
       
  1617 				{
       
  1618 				TPtrC8 desPtr = iOperations.At(count)->Progress();
       
  1619 				if (desPtr.Length() > KMsvProgressBufferLength)
       
  1620 					{
       
  1621 					PanicClient(aMessage, EMsvProgressBufferExceeds256);
       
  1622 					error = KErrOverflow;
       
  1623 					}
       
  1624 				else
       
  1625 					{
       
  1626 					WriteL(aMessage, 1, desPtr);
       
  1627 					error = desPtr.Length();
       
  1628 					}
       
  1629 				}
       
  1630 			delete iOperations.At(count);
       
  1631 			iOperations.Delete(count);
       
  1632 			break;
       
  1633 			}
       
  1634 		}
       
  1635 
       
  1636 	aMessage.Complete(error);
       
  1637 	}
       
  1638 
       
  1639 
       
  1640 void CMsvServerSession::OperationProgressL(const RMessage2 &aMessage)
       
  1641 //
       
  1642 // Gets the progress of an operation
       
  1643 //
       
  1644 	{
       
  1645 	TMsvOp op = aMessage.Int0();
       
  1646 
       
  1647 	TInt error=KErrNotFound;
       
  1648 	for (TInt count=0; count<iOperations.Count(); count++)
       
  1649 		{
       
  1650 		if (iOperations.At(count)->Id()==op)
       
  1651 			{
       
  1652 			if (iOperations.At(count)->State() == EMsvOperationQueued)
       
  1653 				{
       
  1654 				// the operation has not started
       
  1655 				error = KErrNotReady;
       
  1656 				}
       
  1657 			else
       
  1658 				{
       
  1659 				// get the progress from the running operation
       
  1660 				TPtrC8 desPtr = iOperations.At(count)->Progress();
       
  1661 				if (desPtr.Length() > KMsvProgressBufferLength)
       
  1662 					{
       
  1663 					PanicClient(aMessage, EMsvProgressBufferExceeds256);
       
  1664 					error = KErrOverflow;
       
  1665 					}
       
  1666 				else
       
  1667 					{
       
  1668 					WriteL(aMessage, 1, desPtr);
       
  1669 					error = desPtr.Length();
       
  1670 					}
       
  1671 				break;
       
  1672 				}
       
  1673 			}
       
  1674 		}
       
  1675 	aMessage.Complete(error);
       
  1676 	}
       
  1677 
       
  1678 void CMsvServerSession::OperationSystemProgressL(const RMessage2 &aMessage)
       
  1679 //
       
  1680 // Get the progress of a system operation
       
  1681 //
       
  1682 	{
       
  1683 	TMsvOp op = aMessage.Int0();
       
  1684 	TInt error=KErrNotFound;
       
  1685 	for (TInt count=0; count<iOperations.Count(); count++)
       
  1686 		{
       
  1687 		if (iOperations.At(count)->Id()==op)
       
  1688 			{
       
  1689 			if (iOperations.At(count)->State() == EMsvOperationQueued)
       
  1690 				{
       
  1691 				// the operation has not started
       
  1692 				error = KErrNotReady;
       
  1693 				}
       
  1694 			else
       
  1695 				{
       
  1696 				// get the progress from the running operation
       
  1697 				TPckgBuf<TMsvSystemProgress> systemProgress;
       
  1698 				error = iOperations.At(count)->SystemProgress(systemProgress());
       
  1699 				WriteL(aMessage, 1, systemProgress);
       
  1700 				break;
       
  1701 				}
       
  1702 			}
       
  1703 		}
       
  1704 	aMessage.Complete(error);
       
  1705 	}
       
  1706 
       
  1707 void CMsvServerSession::OperationMtmL(const RMessage2 &aMessage)
       
  1708 //
       
  1709 // Returns the MTM which will perform the operation using the two entries
       
  1710 //
       
  1711 	{
       
  1712 	// read the two ids
       
  1713 	TMsvId id1 = aMessage.Int0();
       
  1714 	TMsvId id2 = aMessage.Int1();
       
  1715 
       
  1716 	// the uid to be writen back
       
  1717 	TPckgBuf<TUid> uid;
       
  1718 	TPckgBuf<TMsvId> service;
       
  1719 
       
  1720 	// get the first entries
       
  1721 	TBool local1;
       
  1722 	TMsvEntry* entry1=NULL;
       
  1723 	User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(id1, entry1));
       
  1724 	User::LeaveIfError(iMsvServer.IndexAdapter().IsLocal(id1, local1));
       
  1725 	if (id1==id2)
       
  1726 		{
       
  1727 		// single entry either under local or a remote service
       
  1728 		if (local1)
       
  1729 			{
       
  1730 			uid() = KUidMsvLocalServiceMtm;
       
  1731 			service() = KMsvLocalServiceIndexEntryId;
       
  1732 			}
       
  1733 		else
       
  1734 			{
       
  1735 			uid() = entry1->iMtm;
       
  1736 			service() = entry1->iServiceId;
       
  1737 			}
       
  1738 		}
       
  1739 	else
       
  1740 		{
       
  1741 		// get the second entry
       
  1742 		TBool local2;
       
  1743 		TMsvEntry* entry2=NULL;
       
  1744 		User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(id2, entry2));
       
  1745 		User::LeaveIfError(iMsvServer.IndexAdapter().IsLocal(id2, local2));
       
  1746 		// work out the responsible MTM
       
  1747 		if (local1 && local2)
       
  1748 			{
       
  1749 			// both entries under the local service
       
  1750 			uid() = KUidMsvLocalServiceMtm;
       
  1751 			service() = KMsvLocalServiceIndexEntryId;
       
  1752 			}
       
  1753 		else if (entry1->iMtm == entry2->iMtm)
       
  1754 			{
       
  1755 			__ASSERT_DEBUG(entry1->iMtm!=KUidMsvLocalServiceMtm, PanicServer(EMsvOperationMtm3));
       
  1756 			// both entries use the same MTM
       
  1757 			uid() = entry1->iMtm;
       
  1758 			service() = entry1->iServiceId;
       
  1759 			}
       
  1760 		else if (entry1->iMtm == KUidMsvLocalServiceMtm)
       
  1761 			{
       
  1762 			__ASSERT_DEBUG(entry2->iMtm!=KUidMsvLocalServiceMtm, PanicServer(EMsvOperationMtm1));
       
  1763 			// entry1 is a local entry - hence entry 2 must be MTM dependent
       
  1764 			uid() = entry2->iMtm;
       
  1765 			service() = entry2->iServiceId;
       
  1766 			}
       
  1767 		else
       
  1768 			{
       
  1769 #ifdef _DEBUG
       
  1770 			if (entry2->iMtm != KUidMsvLocalServiceMtm)
       
  1771 				{
       
  1772 				// Neither entry belongs to local mtm, check they have the same technology type
       
  1773 				__ASSERT_DEBUG(iMsvServer.Registry().TechnologyTypeUid(entry1->iMtm) == iMsvServer.Registry().TechnologyTypeUid(entry2->iMtm), PanicServer(EMsvOperationMtm2));
       
  1774 				}
       
  1775 #endif
       
  1776 
       
  1777 			// entry1 is not a local entry - assume entry 2 is local
       
  1778 			uid() = entry1->iMtm;
       
  1779 			service() = entry1->iServiceId;
       
  1780 			}
       
  1781 		}
       
  1782 
       
  1783 	WriteL(aMessage, 2, uid);
       
  1784 	WriteL(aMessage, 3, service);
       
  1785 	aMessage.Complete(KErrNone);
       
  1786 	}
       
  1787 
       
  1788 
       
  1789 
       
  1790 void CMsvServerSession::MoveEntriesL(const RMessage2 &aMessage)
       
  1791 //
       
  1792 // Moving entries
       
  1793 //
       
  1794 	{
       
  1795 	// Recover the operation data
       
  1796 	TMsvOp operationId = aMessage.Int0();
       
  1797 	HBufC8* opData = RecoverOperationData(operationId);
       
  1798 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
  1799 	if(opData == NULL)
       
  1800 		{
       
  1801 		aMessage.Complete(KErrArgument);
       
  1802 		return;
       
  1803 		}
       
  1804 	
       
  1805 	CleanupStack::PushL(opData);
       
  1806 	// unpack the data
       
  1807 	TMsvPackedOperation packedOperation(opData);
       
  1808 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
  1809 	CleanupStack::PushL(selection);
       
  1810 	TInt target, temp;
       
  1811 	packedOperation.UnpackL(*selection, target, temp);
       
  1812 
       
  1813 	TInt count = selection->Count();
       
  1814 	if ( count == 0)
       
  1815 		{
       
  1816 		PanicClient(aMessage, EMsvNoEntriesInMoveSelection);
       
  1817 		aMessage.Complete(KErrNotFound);
       
  1818 		return;
       
  1819 		}
       
  1820 
       
  1821 	// Find the first entry in the selection which exists
       
  1822 	TInt pos=-1;
       
  1823 	TMsvId source = KMsvNullIndexEntryIdValue;
       
  1824 	while (++pos<count)
       
  1825 		{
       
  1826 		TMsvEntry* entryPtr;
       
  1827 		TInt error = KErrNone;
       
  1828 		error = iMsvServer.IndexAdapter().GetEntry(selection->At(pos), entryPtr);
       
  1829 		if( error == KErrNone )
       
  1830 			{
       
  1831 			source = entryPtr->Parent();
       
  1832 			break;
       
  1833 			}
       
  1834 		}
       
  1835 	if (pos==count)
       
  1836 		pos = 0;
       
  1837 
       
  1838 	// Check if the target and source are local or remote
       
  1839 	TBool targetLocal=EFalse;
       
  1840 	TBool sourceLocal=EFalse;
       
  1841 	TBool hasCapability=EFalse;
       
  1842 	// Police request - client must be able to move the entries.
       
  1843 #if (defined SYMBIAN_USER_PROMPT_SERVICE)
       
  1844 	TRAPD(err, iMsvServer.PoliceMoveEntriesL(aMessage, *selection, target, source, targetLocal, sourceLocal, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::MoveEntriesL")));
       
  1845 	if(err == KErrNone)
       
  1846 		{
       
  1847 		hasCapability = ETrue;
       
  1848 		}
       
  1849 	else if((err != KErrNone) && (targetLocal && sourceLocal))
       
  1850 		{
       
  1851 		User::LeaveIfError(err);
       
  1852 		}
       
  1853 #else
       
  1854 	iMsvServer.PoliceMoveEntriesL(aMessage, *selection, target, source, targetLocal, sourceLocal, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::MoveEntriesL"));
       
  1855 #endif
       
  1856 	
       
  1857 	// start the move for local or remote messages
       
  1858 	if (targetLocal && sourceLocal)
       
  1859 		DoMoveLocalEntriesL(selection, target, operationId, aMessage);
       
  1860 	else
       
  1861 		DoMoveRemoteEntriesL(selection, target, operationId, aMessage, sourceLocal, targetLocal, hasCapability);
       
  1862 
       
  1863 	CleanupStack::PopAndDestroy(); // opData
       
  1864 	}
       
  1865 
       
  1866 void CMsvServerSession::DoMoveLocalEntriesL(CMsvEntrySelection*& aSelection, TMsvId aTarget, TMsvOp aOpId, const RMessage2 &aMessage)
       
  1867 //
       
  1868 // NOTE aSelection is the top item on the cleanupstack when the function is called
       
  1869 //
       
  1870 	{
       
  1871 	CMsvLocalMoveOperation* operation = new(ELeave) CMsvLocalMoveOperation(aMessage, aOpId, aSelection, aTarget, iMsvServer);
       
  1872 	CleanupStack::Pop(); // selection
       
  1873 	CleanupStack::PushL(operation);
       
  1874 	operation->StartL();
       
  1875 	iOperations.AppendL(operation);
       
  1876 	CleanupStack::Pop(); // operation
       
  1877 	}
       
  1878 
       
  1879 
       
  1880 void CMsvServerSession::DoMoveRemoteEntriesL(CMsvEntrySelection*& aSelection, TMsvId aTarget, TMsvOp aOpId, const RMessage2 &aMessage, TBool aSourceLocal, TBool aTargetLocal, TBool aHasCapability)
       
  1881 //
       
  1882 // NOTE aSelection is the top item on the cleanupstack when the function is called
       
  1883 //
       
  1884 	{
       
  1885 	// make sure that the operation can be added to the list
       
  1886 	iOperations.SetReserveL(iOperations.Count()+1);
       
  1887 
       
  1888 	TMsvEntry* entry=NULL;
       
  1889 	if (!aTargetLocal)
       
  1890 		{
       
  1891 		User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(aTarget, entry));
       
  1892 		}
       
  1893 	else
       
  1894 		{
       
  1895 		User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(aSelection->At(0), entry));
       
  1896 		}
       
  1897 
       
  1898 
       
  1899 	// create the operation
       
  1900 	CMsvMtmOperation* operation = CMsvMtmOperation::NewL(aMessage, aOpId, entry->iMtm, entry->iServiceId, iSessionId, iMsvServer);
       
  1901 	CleanupStack::Pop();
       
  1902 	CleanupStack::PushL(operation);
       
  1903 
       
  1904 	// set up the operation type and pass the data
       
  1905 	if (!(aSourceLocal || aTargetLocal))
       
  1906 		operation->MoveWithinService(aSelection, aTarget);
       
  1907 	else if (aSourceLocal)
       
  1908 		operation->MoveFromLocal(aSelection, aTarget);
       
  1909 	else
       
  1910 		operation->MoveToLocal(aSelection, aTarget);
       
  1911 
       
  1912 	// either start the operation or queue it
       
  1913 	iMsvServer.StartOperationL(*operation, iSessionId, aMessage, aHasCapability);
       
  1914 	iOperations.AppendL(operation); // will not fail - see start of function
       
  1915 	CleanupStack::Pop(); // operation
       
  1916 	}
       
  1917 
       
  1918 
       
  1919 void CMsvServerSession::CopyEntriesL(const RMessage2 &aMessage)
       
  1920 //
       
  1921 // Moving entries
       
  1922 //
       
  1923 	{
       
  1924 	// Recover the operation data
       
  1925 	TMsvOp operationId = aMessage.Int0();
       
  1926 	HBufC8* opData = RecoverOperationData(operationId);
       
  1927 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
  1928 	if(opData == NULL)
       
  1929 		{
       
  1930 		aMessage.Complete(KErrArgument);
       
  1931 		return;
       
  1932 		}
       
  1933 	
       
  1934 	CleanupStack::PushL(opData);
       
  1935 	// unpack the data
       
  1936 	TMsvPackedOperation packedOperation(opData);
       
  1937 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
  1938 	CleanupStack::PushL(selection);
       
  1939 	TInt target,temp;
       
  1940 	packedOperation.UnpackL(*selection, target, temp);
       
  1941 
       
  1942 	if (selection->Count() == 0)
       
  1943 		{
       
  1944 		PanicClient(aMessage, EMsvNoEntriesInCopySelection);
       
  1945 		aMessage.Complete(KErrNotFound);
       
  1946 		return;
       
  1947 		}
       
  1948 
       
  1949 	// Find the first entry in the selection which exists
       
  1950 	TInt pos=-1;
       
  1951 	TInt count=selection->Count();
       
  1952 	while (++pos<count)
       
  1953 		{
       
  1954 		TBool entryExsists = EFalse;
       
  1955 		entryExsists = iMsvServer.IndexAdapter().EntryExists(selection->At(pos));
       
  1956 		if (entryExsists)
       
  1957 			break;
       
  1958 		}
       
  1959 	if (pos==count)
       
  1960 		pos = 0;
       
  1961 
       
  1962 	// Check if the target and source are local or remote
       
  1963 	TBool targetLocal=EFalse;
       
  1964 	TBool sourceLocal=EFalse;
       
  1965 	TBool hasCapability = EFalse;
       
  1966 #if (defined SYMBIAN_USER_PROMPT_SERVICE)
       
  1967 	// Police request - client must be able to copy the entries.
       
  1968 	TRAPD(err, iMsvServer.PoliceCopyEntriesL(aMessage, *selection, target, targetLocal, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CopyEntriesL")));
       
  1969 	
       
  1970 	if(err == KErrNone)
       
  1971 		{
       
  1972 		hasCapability = ETrue;
       
  1973 		}
       
  1974 #else
       
  1975 	iMsvServer.PoliceCopyEntriesL(aMessage, *selection, target, targetLocal, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CopyEntriesL"));
       
  1976 #endif
       
  1977 	TInt error = KErrNone;
       
  1978 	// what happens about the rest? Inconsistent behaviour
       
  1979 	error = iMsvServer.IndexAdapter().IsLocal(selection->At(pos), sourceLocal);
       
  1980 	
       
  1981 #if (defined SYMBIAN_USER_PROMPT_SERVICE)   
       
  1982 	if((err != KErrNone) && (targetLocal && sourceLocal))
       
  1983 		{
       
  1984 		User::LeaveIfError(err);
       
  1985 		}
       
  1986 #endif
       
  1987 		
       
  1988 	if (error)
       
  1989 		{
       
  1990 		CleanupStack::PopAndDestroy(2); // selection & opData
       
  1991 		aMessage.Complete(error);
       
  1992 		return;
       
  1993 		}
       
  1994 
       
  1995 
       
  1996 	// start the move for local or remote messages
       
  1997 	if (targetLocal && sourceLocal)
       
  1998 		DoCopyLocalEntriesL(selection, target, operationId, aMessage);
       
  1999 	else
       
  2000 		DoCopyRemoteEntriesL(selection, target, operationId, aMessage, sourceLocal, targetLocal, hasCapability);
       
  2001 
       
  2002 	CleanupStack::PopAndDestroy(); // opData
       
  2003 	}
       
  2004 
       
  2005 void CMsvServerSession::DoCopyLocalEntriesL(CMsvEntrySelection*& aSelection, TMsvId aTarget, TMsvOp aOpId, const RMessage2 &aMessage)
       
  2006 //
       
  2007 // NOTE aSelection is the top item on the cleanupstack when the function is called
       
  2008 //
       
  2009 	{
       
  2010 	CMsvLocalCopyOperation* operation = new(ELeave) CMsvLocalCopyOperation(aMessage, aOpId, aSelection, aTarget, iMsvServer);
       
  2011 	CleanupStack::Pop(); // selection
       
  2012 	CleanupStack::PushL(operation);
       
  2013 
       
  2014 	operation->StartL();
       
  2015 
       
  2016 	iOperations.AppendL(operation);
       
  2017 	CleanupStack::Pop(); // operation
       
  2018 	}
       
  2019 
       
  2020 void CMsvServerSession::DoCopyRemoteEntriesL(CMsvEntrySelection*& aSelection, TMsvId aTarget, TMsvOp aOpId, const RMessage2 &aMessage, TBool aSourceLocal, TBool aTargetLocal, TBool aHasCapability)
       
  2021 //
       
  2022 // NOTE aSelection is the top item on the cleanupstack when the function is called
       
  2023 //
       
  2024 	{
       
  2025 	// make sure that the operation can be added to the list
       
  2026 	iOperations.SetReserveL(iOperations.Count()+1);
       
  2027 
       
  2028 	TMsvEntry* entry=NULL;
       
  2029 	if (!aTargetLocal)
       
  2030 		{
       
  2031 		User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(aTarget, entry));
       
  2032 		}
       
  2033 
       
  2034 	else
       
  2035 		{
       
  2036 		User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(aSelection->At(0), entry));
       
  2037 		}
       
  2038 
       
  2039 	// create the operation
       
  2040 	CMsvMtmOperation* operation = CMsvMtmOperation::NewL(aMessage, aOpId, entry->iMtm, entry->iServiceId, iSessionId, iMsvServer);
       
  2041 	CleanupStack::Pop(); // aSelection
       
  2042 	CleanupStack::PushL(operation);
       
  2043 
       
  2044 	// set up the operation type and pass the data
       
  2045 	if (!(aSourceLocal || aTargetLocal))
       
  2046 		operation->CopyWithinService(aSelection, aTarget);
       
  2047 	else if (aSourceLocal)
       
  2048 		operation->CopyFromLocal(aSelection, aTarget);
       
  2049 	else
       
  2050 		operation->CopyToLocal(aSelection, aTarget);
       
  2051 
       
  2052 	// either start the operation or queue it
       
  2053 	iMsvServer.StartOperationL(*operation, iSessionId, aMessage, aHasCapability);
       
  2054 	iOperations.AppendL(operation); // will not fail - see start of function
       
  2055 	CleanupStack::Pop(); // operation
       
  2056 	}
       
  2057 
       
  2058 
       
  2059 
       
  2060 void CMsvServerSession::TransferCommandL(const RMessage2 &aMessage)
       
  2061 //
       
  2062 //
       
  2063 //
       
  2064 	{
       
  2065 	// Recover the operation data and parameter
       
  2066 	TMsvOp operationId = aMessage.Int0();
       
  2067 	HBufC8* opData = RecoverOperationData(operationId);
       
  2068 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
  2069 	if(opData == NULL)
       
  2070 		{
       
  2071 		aMessage.Complete(KErrArgument);
       
  2072 		return;
       
  2073 		}
       
  2074 	
       
  2075 	HBufC8* opParam = RecoverOperationData(operationId);
       
  2076 	// Check for NULL data entry to be changed, must have been given incorrect id for argument.
       
  2077 	if(opParam == NULL)
       
  2078 		{
       
  2079 		delete opData;
       
  2080 		aMessage.Complete(KErrArgument);
       
  2081 		return;
       
  2082 		}
       
  2083 	
       
  2084 	CleanupStack::PushL(opData);
       
  2085 	CleanupStack::PushL(opParam);
       
  2086 
       
  2087 	// Unpack the data
       
  2088 	TMsvPackedOperation packedOperation(opData);
       
  2089 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
  2090 	CleanupStack::PushL(selection);
       
  2091 	TInt command, temp;
       
  2092 	packedOperation.UnpackL(*selection, command, temp);
       
  2093 
       
  2094 	// make sure that the operation can be added to the list
       
  2095 	iOperations.SetReserveL(iOperations.Count()+1);
       
  2096 
       
  2097 	// find the first entry being acted on
       
  2098 	TMsvEntry* entry=NULL;
       
  2099 	User::LeaveIfError(iMsvServer.IndexAdapter().GetEntry(selection->At(0), entry));
       
  2100 	TBool hasCapability = EFalse;
       
  2101 	// Police request - client must be able to access the MTM functionality.
       
  2102 #if (defined SYMBIAN_USER_PROMPT_SERVICE)
       
  2103 	TRAPD(err, iMsvServer.PoliceMtmTransferCommandL(aMessage, entry->iMtm, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::TransferCommandL")));
       
  2104 	
       
  2105 	if(err == KErrNone)
       
  2106 		{
       
  2107 		hasCapability = ETrue;
       
  2108 		}
       
  2109 	else if(err != KErrPermissionDenied)
       
  2110 		{
       
  2111 		User::LeaveIfError(err);
       
  2112 		}
       
  2113 #else
       
  2114 	iMsvServer.PoliceMtmTransferCommandL(aMessage, entry->iMtm, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::TransferCommandL"));
       
  2115 #endif
       
  2116 
       
  2117 	// create the operation
       
  2118 	CMsvMtmOperation* operation = CMsvMtmOperation::NewL(aMessage, operationId, entry->iMtm, entry->iServiceId, iSessionId, iMsvServer);
       
  2119 	CleanupStack::Pop(); // selection
       
  2120 	CleanupStack::Pop(); // opParam
       
  2121 	CleanupStack::PushL(operation);
       
  2122 
       
  2123 	// set up the operation type and pass the data
       
  2124 	operation->StartCommand(selection, command, opParam);
       
  2125 
       
  2126 	// either start the operation or queue it
       
  2127 	iMsvServer.StartOperationL(*operation, iSessionId, aMessage, hasCapability);
       
  2128 	iOperations.AppendL(operation); // will not fail - see start of function
       
  2129 	CleanupStack::Pop(); // operation
       
  2130 	CleanupStack::PopAndDestroy(); // opParam
       
  2131 	}
       
  2132 
       
  2133 
       
  2134 void CMsvServerSession::FillRegisteredMtmDllArray(const RMessage2 &aMessage)
       
  2135 	{
       
  2136 	TRAPD(error,DoFillRegisteredMtmDllArrayL(aMessage));
       
  2137 	aMessage.Complete(error);
       
  2138 	}
       
  2139 
       
  2140 void CMsvServerSession::DoFillRegisteredMtmDllArrayL(const RMessage2 &aMessage)
       
  2141 	{
       
  2142 	CRegisteredMtmDllArray* registeredMtmDllArray = new(ELeave) CRegisteredMtmDllArray;
       
  2143 	CleanupStack::PushL(registeredMtmDllArray);
       
  2144 
       
  2145 	TUid mtmdlltypeuid = {aMessage.Int0()};
       
  2146 	TInt error=iMsvServer.FillRegisteredMtmDllArray(mtmdlltypeuid,*registeredMtmDllArray);
       
  2147 	User::LeaveIfError(error);
       
  2148 
       
  2149 	TPtr8 bufferPtr = iBuffer->Des();
       
  2150 	bufferPtr.Zero();
       
  2151 	RDesWriteStream writeStream(bufferPtr);
       
  2152 	TMsvPackedRegisteredMtmDllArray::PackRegisteredMtmDllArrayL(writeStream, *registeredMtmDllArray);
       
  2153 	writeStream.CommitL();
       
  2154 
       
  2155 	WriteBufferL(aMessage, 1);
       
  2156 	CleanupStack::PopAndDestroy(); // registeredMtmDllArray
       
  2157 	}
       
  2158 
       
  2159 
       
  2160 void CMsvServerSession::MtmGroupL(const RMessage2 &aMessage, TBool aInstall)
       
  2161 	{
       
  2162 	TFullName fullname;
       
  2163 	TInt  desLen = aMessage.GetDesLengthL(0);
       
  2164 	fullname.SetLength(desLen);
       
  2165 	TInt error = aMessage.Read(0,fullname);
       
  2166 	if (error)
       
  2167 		{
       
  2168 		PanicClient(aMessage, EMsvBadDescriptorRead);
       
  2169 		User::Leave(error);
       
  2170 		}
       
  2171 	TUid mtmtypeuid=KNullUid;
       
  2172 	if (aInstall)
       
  2173 		{
       
  2174 		User::LeaveIfError(iMsvServer.InstallMtmGroup(fullname,mtmtypeuid));
       
  2175 		iMsvServer.NotifyChanged(EMsvMtmGroupInstalled,mtmtypeuid);
       
  2176 		}
       
  2177 	else
       
  2178 		{
       
  2179 		User::LeaveIfError(iMsvServer.DeInstallMtmGroup(fullname,mtmtypeuid));
       
  2180 		iMsvServer.NotifyChanged(EMsvMtmGroupDeInstalled,mtmtypeuid);
       
  2181 		}
       
  2182 	aMessage.Complete(KErrNone);
       
  2183 	}
       
  2184 
       
  2185 
       
  2186 void CMsvServerSession::UseMtmGroup(const RMessage2 &aMessage)
       
  2187 	{
       
  2188 	TUid mtmtypeuid = {aMessage.Int0()};
       
  2189 	TInt index=0;
       
  2190 	TInt count=iMtmGroupRefCountArray.Count();
       
  2191 	for (; (index<count) && (mtmtypeuid!=iMtmGroupRefCountArray[index].iMtmTypeUid); index++)
       
  2192 		{
       
  2193 		}
       
  2194 	TInt error=KErrNone;
       
  2195 	if (index==count)
       
  2196 		{
       
  2197 		TMtmGroupRefCount mtmgrouprefcount(mtmtypeuid);
       
  2198 		TRAP(error,iMtmGroupRefCountArray.AppendL(mtmgrouprefcount));
       
  2199 		}
       
  2200 	if (error==KErrNone)
       
  2201 		iMtmGroupRefCountArray[index].iRefCount++;
       
  2202 	aMessage.Complete(error);
       
  2203 	}
       
  2204 
       
  2205 void CMsvServerSession::ReleaseMtmGroup(const RMessage2 &aMessage)
       
  2206 	{
       
  2207 	TUid mtmtypeuid = {aMessage.Int0()};
       
  2208 	TInt index=0;
       
  2209 	TInt count=iMtmGroupRefCountArray.Count();
       
  2210 	for (; (index<count) && (mtmtypeuid!=iMtmGroupRefCountArray[index].iMtmTypeUid); index++)
       
  2211 		{
       
  2212 		}
       
  2213 	TInt error=KErrNone;
       
  2214 	if (index==count)
       
  2215 		error=KErrNotFound;
       
  2216 	else
       
  2217 		{
       
  2218 		iMtmGroupRefCountArray[index].iRefCount--;
       
  2219 		if (iMtmGroupRefCountArray[index].iRefCount==0)
       
  2220 			iMtmGroupRefCountArray.Delete(index);
       
  2221 		}
       
  2222 	aMessage.Complete(error);
       
  2223 	}
       
  2224 
       
  2225 void CMsvServerSession::GetMtmGroupDataL(const RMessage2 &aMessage)
       
  2226 	{
       
  2227 	TUid mtmtypeuid = {aMessage.Int0()};
       
  2228 	CMtmGroupData* mtmgroupdata=iMsvServer.GetMtmGroupDataL(mtmtypeuid);
       
  2229 	CleanupStack::PushL(mtmgroupdata);
       
  2230 	TPtr8 bufferPtr = iBuffer->Des();
       
  2231 	bufferPtr.Zero();
       
  2232 	RDesWriteStream writeStream(bufferPtr);
       
  2233 	writeStream << *mtmgroupdata;
       
  2234 	writeStream.CommitL();
       
  2235 
       
  2236 	WriteBufferL(aMessage, 1);
       
  2237 	CleanupStack::PopAndDestroy();
       
  2238 	aMessage.Complete(KErrNone);
       
  2239 	}
       
  2240 
       
  2241 
       
  2242 void CMsvServerSession::GetMtmRequiredCapabilitiesL(const RMessage2& aMessage)
       
  2243 	{
       
  2244 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  2245 	TCapabilitySet mtmRequiredCaps;
       
  2246 	iMsvServer.GetMtmRequiredCapabilitiesL(mtmTypeUid,mtmRequiredCaps);
       
  2247 	TPtr8 ptr = iBuffer->Des();
       
  2248 	ptr.Zero();
       
  2249 	RDesWriteStream writeStream(ptr);
       
  2250 	MsvSecurityCapabilitySetUtils::ExternalizeL(writeStream,mtmRequiredCaps);
       
  2251 	writeStream.CommitL();
       
  2252 	WriteBufferL(aMessage, 1);
       
  2253 	aMessage.Complete(KErrNone);
       
  2254 	}
       
  2255 
       
  2256 
       
  2257 void CMsvServerSession::PanicClient(const RMessage2& aMessage, TMsvClientPanic aPanic) const
       
  2258 //
       
  2259 // Panic the client
       
  2260 //
       
  2261 	{
       
  2262 	aMessage.Panic(KMsvClientPanicString, aPanic);
       
  2263 	}
       
  2264 
       
  2265 
       
  2266 void CMsvServerSession::StopOperations(TMsvId aServiceId)
       
  2267 //
       
  2268 // Cancels all operations which use the service (the service is being stopped)
       
  2269 //
       
  2270 	{
       
  2271 	TInt count=iOperations.Count();
       
  2272 	while (count--)
       
  2273 		{
       
  2274 		if (iOperations.At(count)->ServiceId() == aServiceId)
       
  2275 			{
       
  2276 			delete iOperations.At(count);
       
  2277 			iOperations.Delete(count);
       
  2278 			}
       
  2279 		}
       
  2280 	}
       
  2281 
       
  2282 
       
  2283 void CMsvServerSession::ServiceProgressL(const RMessage2 &aMessage)
       
  2284 //
       
  2285 // Returns the progress from the server MTM
       
  2286 //
       
  2287 	{
       
  2288 	TPtrC8 desPtr = iMsvServer.ServiceProgressL(aMessage.Int0());
       
  2289 	TInt desLength = aMessage.GetDesMaxLength(1);
       
  2290 	if (desLength < desPtr.Length())
       
  2291 		aMessage.Complete(KErrOverflow);
       
  2292 	else
       
  2293 		{
       
  2294 		WriteL(aMessage, 1, desPtr);
       
  2295 		aMessage.Complete(desPtr.Length());
       
  2296 		}
       
  2297 	}
       
  2298 
       
  2299 void CMsvServerSession::RemoveEntry(const RMessage2& aMessage)
       
  2300 //
       
  2301 // Removes the entry from the index. Client not interested in result.
       
  2302 //
       
  2303 	{
       
  2304 	TMsvId id = aMessage.Int0();
       
  2305 
       
  2306 	TBool local;
       
  2307 	// Police request - client must be able to modify the entry.
       
  2308 	TRAPD(error, iMsvServer.PoliceModifyEntryL(aMessage, id, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::RemoveEntry")));
       
  2309 
       
  2310 	if( error == KErrPermissionDenied )
       
  2311 		{
       
  2312 		// The request was denied - complete with this error code.
       
  2313 		aMessage.Complete(KErrPermissionDenied);
       
  2314 		return;
       
  2315 		}
       
  2316 	else if( error == KErrNone )
       
  2317 		{
       
  2318 		if (local)
       
  2319 			iMsvServer.RemoveEntry(id);
       
  2320 		else
       
  2321 			{
       
  2322 			TMsvId service;
       
  2323 			iMsvServer.IndexAdapter().OwningService(id, service); // error ignored
       
  2324 			if (service==id)
       
  2325 				iMsvServer.RemoveEntry(id);
       
  2326 			else
       
  2327 				PanicClient(aMessage, EMsvRemovingNonLocalEntry);
       
  2328 			}
       
  2329 		}
       
  2330 
       
  2331 	aMessage.Complete(KErrNone);
       
  2332 	}
       
  2333 
       
  2334 void CMsvServerSession::MessageFolderL(const RMessage2 &aMessage)
       
  2335 //
       
  2336 //
       
  2337 //
       
  2338 	{
       
  2339 	aMessage.WriteL(0, iMsvServer.Context().MessageFolder());
       
  2340 	aMessage.Complete(KErrNone);
       
  2341 	}
       
  2342 
       
  2343 void CMsvServerSession::ChangeAttributesL(const RMessage2& aMessage)
       
  2344 //
       
  2345 //
       
  2346 //
       
  2347 	{
       
  2348 	HBufC8* buffer = NULL;
       
  2349 	ReadBufferL(aMessage, 0, buffer);
       
  2350 	CleanupStack::PushL(buffer);
       
  2351 
       
  2352 	TMsvPackedOperation packedOperation(buffer);
       
  2353 
       
  2354 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
  2355 	CleanupStack::PushL(selection);
       
  2356 
       
  2357 	TInt setAttributes, clearAttributes;
       
  2358 	packedOperation.UnpackL(*selection, setAttributes, clearAttributes);
       
  2359 
       
  2360 	if (selection->Count() == 0)
       
  2361 		{
       
  2362 		PanicClient(aMessage, EMsvNoEntriesInChangeAttributesSelection);
       
  2363 		aMessage.Complete(KErrNotFound);
       
  2364 		return;
       
  2365 		}
       
  2366 
       
  2367 	// get a copy of the selection
       
  2368 	CMsvEntrySelection* changedEntries = selection->CopyL();
       
  2369 	CleanupStack::PushL(changedEntries);
       
  2370 
       
  2371 	// create a selection for the later notification
       
  2372 	CMsvEntrySelection* notify = new(ELeave)CMsvEntrySelection;
       
  2373 	CleanupStack::PushL(notify);
       
  2374 	notify->SetReserveL(selection->Count());
       
  2375 
       
  2376 	TInt error = KErrNone;
       
  2377 
       
  2378 	// lock all the selection
       
  2379 	TInt count1 = selection->Count();
       
  2380 	while (count1--)
       
  2381 		{
       
  2382 		error = iMsvServer.IndexAdapter().LockEntry(selection->At(count1));
       
  2383 		if (error)
       
  2384 			break;
       
  2385 		}
       
  2386 
       
  2387 	// change the attributes if all were locked
       
  2388 	if (error == KErrNone)
       
  2389 		{
       
  2390 		error = iMsvServer.IndexAdapter().ChangeAttributes(*changedEntries, setAttributes, clearAttributes);
       
  2391 		}
       
  2392 
       
  2393 
       
  2394 	// release all that were locked
       
  2395 	TInt count2 = selection->Count();
       
  2396 	while (--count2 > count1)
       
  2397 		{
       
  2398 		iMsvServer.IndexAdapter().ReleaseEntry(selection->At(count2));
       
  2399 		}
       
  2400 
       
  2401 
       
  2402 	if (error == KErrNone)
       
  2403 		{
       
  2404 		// Note: Code in the following loop shouldn't leave
       
  2405 		while(changedEntries->Count())
       
  2406 			{
       
  2407 			TMsvId id = changedEntries->At(0);
       
  2408 			changedEntries->Delete(0);
       
  2409 
       
  2410 			// Get the index entry associated with this entry - Shouldn't fail
       
  2411 		
       
  2412 			TInt err = KErrNone;
       
  2413 			CMsvCacheEntry* entry = NULL;
       
  2414 			err = iMsvServer.IndexAdapter().GetInternalEntry(id, entry);
       
  2415 			if (err != KErrNone)
       
  2416 				continue;
       
  2417 
       
  2418 			// Get the parent of this entry. It will be notified that its children have changed
       
  2419 			TMsvId parent = entry->Entry().Parent();
       
  2420 			notify->AppendL(id);
       
  2421 
       
  2422 			// Find other entries that have the same parent
       
  2423 			TInt count = changedEntries->Count();
       
  2424 			while(count--)
       
  2425 				{
       
  2426 				iMsvServer.IndexAdapter().GetInternalEntry(changedEntries->At(count), entry);
       
  2427 				if (entry && entry->Entry().Parent() == parent)
       
  2428 					{
       
  2429 					// Add entry to list - Shouldn't fail as we've already reserved space
       
  2430 					notify->AppendL(changedEntries->At(count));
       
  2431 					changedEntries->Delete(count);
       
  2432 					}
       
  2433 				}
       
  2434 
       
  2435 			// Do the notification
       
  2436 			iMsvServer.NotifyChanged(EMsvEntriesChanged, *notify, parent);
       
  2437 			notify->Reset();
       
  2438 			}
       
  2439 		}
       
  2440 
       
  2441 	aMessage.Complete(error);
       
  2442 	CleanupStack::PopAndDestroy(4); // notify, changedEntries, selection, buffer
       
  2443 	}
       
  2444 
       
  2445 void CMsvServerSession::GetChildIdsL(const RMessage2& aMessage)
       
  2446 	{
       
  2447 	TMsvId id = aMessage.Int1();
       
  2448 
       
  2449 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
  2450 	// Return error, if the passed parent-id 
       
  2451 	// is a non-current standard id.
       
  2452 	if( (IsStandardId(id)) &&
       
  2453 		(KCurrentDriveId != GetDriveId(id))
       
  2454 	  )
       
  2455 		{
       
  2456 		aMessage.Complete(KErrArgument);		
       
  2457 		return;
       
  2458 		}
       
  2459 #endif
       
  2460 	
       
  2461 	CMsvEntrySelection* selection = new(ELeave)CMsvEntrySelection;
       
  2462 	CleanupStack::PushL(selection);
       
  2463 
       
  2464 	HBufC8* buffer = NULL;
       
  2465 	ReadBufferL(aMessage, 0, buffer);
       
  2466 	CleanupStack::PushL(buffer);
       
  2467 
       
  2468 	CMsvEntryFilter* filter = CMsvEntryFilter::NewLC();
       
  2469 	TMsvPackedEntryFilter package(buffer);
       
  2470 	package.UnpackFilter(*filter);
       
  2471 
       
  2472 	// Need to filter the list via the client secure ID if the client is not
       
  2473 	// trusted with Read User Data.
       
  2474 	TBool filterByOwnerId = !aMessage.HasCapability(ECapabilityReadUserData);
       
  2475 
       
  2476 	iMsvServer.IndexAdapter().GetChildrenId(id, *filter, *selection, filterByOwnerId, aMessage.SecureId());
       
  2477 	TMsvPackedOperation op(iBuffer);
       
  2478 
       
  2479 	TInt error = op.Pack(*selection, 0, 0);
       
  2480 	while (error != KErrNone)
       
  2481 		{
       
  2482 		// increase the size of the buffer and try again
       
  2483 		iBuffer->Des().SetLength(0); // to avoid copying contents
       
  2484 		iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  2485 		error = op.Pack(*selection, 0, 0);
       
  2486 		}
       
  2487 
       
  2488 	WriteBufferL(aMessage, 0);
       
  2489 
       
  2490 	aMessage.Complete(KErrNone);
       
  2491 	CleanupStack::PopAndDestroy(3); // selection, buffer, filter
       
  2492 	}
       
  2493 
       
  2494 
       
  2495 
       
  2496 void CMsvServerSession::ChangeDriveL(const RMessage2 &aMessage)
       
  2497 //
       
  2498 // Change the drive containing the Message Store
       
  2499 //
       
  2500 	{
       
  2501 	// Not allowed to do this if operations are outstanding
       
  2502 	if (iMsvServer.OutstandingOperations() > 0)
       
  2503 		User::Leave(KErrServerBusy);
       
  2504 
       
  2505 	// make sure that the operation can be added to the list
       
  2506 	iOperations.SetReserveL(iOperations.Count()+1);
       
  2507 
       
  2508 	// Start the operation
       
  2509 	CMsvChangeDriveOperation* operation = new(ELeave)CMsvChangeDriveOperation(aMessage, aMessage.Int0(), aMessage.Int1(), iMsvServer);
       
  2510 	CleanupStack::PushL(operation);
       
  2511 	User::LeaveIfError(operation->Start());
       
  2512 
       
  2513 	iOperations.AppendL(operation);
       
  2514 	CleanupStack::Pop(); // operation
       
  2515 	}
       
  2516 
       
  2517 
       
  2518 
       
  2519 
       
  2520 void CMsvServerSession::CopyStoreL(const RMessage2 &aMessage)
       
  2521 //
       
  2522 // Copy the Message Store to another location
       
  2523 //
       
  2524 	{
       
  2525 	// Not allowed to do this if operations are outstanding
       
  2526 	if (iMsvServer.OutstandingOperations() > 0)
       
  2527 		User::Leave(KErrServerBusy);
       
  2528 
       
  2529 	// make sure that the operation can be added to the list
       
  2530 	iOperations.SetReserveL(iOperations.Count()+1);
       
  2531 
       
  2532 	// Start the operation
       
  2533 	CMsvCopyStoreOperation* operation = CMsvCopyStoreOperation::NewL(aMessage, iMsvServer);
       
  2534 	CleanupStack::PushL(operation);
       
  2535 	operation->StartL();
       
  2536 
       
  2537 	iOperations.AppendL(operation);
       
  2538 	CleanupStack::Pop(); // operation
       
  2539 	}
       
  2540 
       
  2541 void CMsvServerSession::DeleteStoreL(const RMessage2 &aMessage)
       
  2542 //
       
  2543 // Delete the Message Store from the specified location
       
  2544 //
       
  2545 	{
       
  2546 	// Not allowed to do this if operations are outstanding
       
  2547 	if (iMsvServer.OutstandingOperations() > 0)
       
  2548 		User::Leave(KErrServerBusy);
       
  2549 
       
  2550 	// make sure that the operation can be added to the list
       
  2551 	iOperations.SetReserveL(iOperations.Count()+1);
       
  2552 
       
  2553 	// Start the operation
       
  2554 	CMsvDeleteStoreOperation* operation = new(ELeave)CMsvDeleteStoreOperation(aMessage, iMsvServer);
       
  2555 	CleanupStack::PushL(operation);
       
  2556 	operation->StartL();
       
  2557 
       
  2558 	iOperations.AppendL(operation);
       
  2559 	CleanupStack::Pop(); // operation
       
  2560 	}
       
  2561 
       
  2562 
       
  2563 
       
  2564 void CMsvServerSession::SetReceiveEntryEvents(const RMessage2 &aMessage)
       
  2565 //
       
  2566 // Request/UnRequest Entry Events
       
  2567 //
       
  2568 	{
       
  2569 	iReceiveEntryEvents=aMessage.Int0();
       
  2570 	aMessage.Complete(KErrNone);
       
  2571 	}
       
  2572 
       
  2573 
       
  2574 void CMsvServerSession::CreateAttachmentForWriteL(const RMessage2 &aMessage)
       
  2575 	{
       
  2576 	// Get the Message Id from slot 0
       
  2577 	TMsvId msgId = aMessage.Int0();
       
  2578 
       
  2579 	// Police request - client must be able to modify the entry.
       
  2580 	TBool local;
       
  2581 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CreateAttachmentForWriteL"));
       
  2582 
       
  2583 	if( local )
       
  2584 		{
       
  2585 		// Update the owner ID of the entry. NOTE - doesn't matter if actually
       
  2586 		// attachment creation fails after this - safer that owner ID is updated
       
  2587 		// at very least.
       
  2588 		UpdateOwnerIdL(msgId, aMessage.SecureId());
       
  2589 		}
       
  2590 
       
  2591 	// Get the file name from slot 1
       
  2592 	TFileName fileName;
       
  2593 	aMessage.ReadL(1, fileName);
       
  2594 
       
  2595 	RFs fs;
       
  2596 	ConnectAndShareFileSessionLC(fs);
       
  2597 
       
  2598 	// Set the CMsvServer and file session
       
  2599 	TMsvServerStoreManager storemanager;
       
  2600 	storemanager.SetMsvServerAndFileSession(iMsvServer,fs);
       
  2601 
       
  2602 	RFile file;
       
  2603 	TBool fileNameChanged = storemanager.CreateAttachmentForWriteL(msgId, fileName, file);
       
  2604 	CleanupClosePushL(file);
       
  2605 
       
  2606 	// Write the sub session handle (RFile) in slot 2
       
  2607 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle());
       
  2608 	aMessage.WriteL(2,pckgSubSessionHandle);
       
  2609 
       
  2610 	// Write the file changed status in slot 3
       
  2611 	TPckg<TBool> pckgFileNameChanged(fileNameChanged);
       
  2612 	aMessage.WriteL(3,pckgFileNameChanged);
       
  2613 
       
  2614 	// Write back the file path to the client.
       
  2615 	TInt desLen = aMessage.GetDesMaxLength(1);
       
  2616 	if( desLen < fileName.Length() )
       
  2617 		User::Leave(KErrOverflow);
       
  2618 	aMessage.WriteL(1, fileName);
       
  2619 
       
  2620 	// Takes care of completing with session (RFs),
       
  2621 	// SendReceieve returns session handle at client side
       
  2622 	User::LeaveIfError(file.TransferToClient(aMessage,2));
       
  2623 
       
  2624 	CleanupStack::PopAndDestroy(2, &fs); // fs , file
       
  2625 	}
       
  2626 
       
  2627 void CMsvServerSession::ReplaceAttachmentForWriteL(const RMessage2 &aMessage)
       
  2628 	{
       
  2629 	// Get the Message Id from slot 0
       
  2630 	TMsvId msgId = aMessage.Int0();
       
  2631 
       
  2632 	// Police request - client must be able to modify the entry.
       
  2633 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ReplaceAttachmentForWriteL"));
       
  2634 
       
  2635 	// Get the file name from slot 1
       
  2636 	TFileName fileName;
       
  2637 	aMessage.ReadL(1, fileName);
       
  2638 
       
  2639 	RFs fs;
       
  2640 	ConnectAndShareFileSessionLC(fs);
       
  2641 
       
  2642 	// Set the CMsvServer and file session
       
  2643 	TMsvServerStoreManager storemanager;
       
  2644 	storemanager.SetMsvServerAndFileSession(iMsvServer,fs);
       
  2645 
       
  2646 	RFile file;
       
  2647 	storemanager.ReplaceAttachmentForWriteL(msgId, fileName, file);
       
  2648 	CleanupClosePushL(file);
       
  2649 
       
  2650 	// Write the sub session handle (RFile) in slot 2
       
  2651 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle());
       
  2652 	aMessage.WriteL(2,pckgSubSessionHandle);
       
  2653 
       
  2654 	// Write back the file path to the client.
       
  2655 	TInt desLen = aMessage.GetDesMaxLength(1);
       
  2656 	if( desLen < fileName.Length() )
       
  2657 		User::Leave(KErrOverflow);
       
  2658 	aMessage.WriteL(1, fileName);
       
  2659 
       
  2660 	// Takes care of completing with session (RFs),
       
  2661 	// SendReceieve returns session handle at client side
       
  2662 	User::LeaveIfError(file.TransferToClient(aMessage,2));
       
  2663 
       
  2664 	CleanupStack::PopAndDestroy(2, &fs); // fs , file
       
  2665 	}
       
  2666 
       
  2667 void CMsvServerSession::OpenAttachmentL(const RMessage2 &aMessage)
       
  2668 	{
       
  2669 	// Get the Message Id from slot 0
       
  2670 	TMsvId msgId = aMessage.Int0();
       
  2671 
       
  2672 	// Police request - client must be able to read the entry.
       
  2673 	iMsvServer.PoliceReadEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::OpenAttachmentL"));
       
  2674 
       
  2675 	// Get the file name from slot 1
       
  2676 	TFileName filePath;
       
  2677 	aMessage.ReadL(1, filePath);
       
  2678 
       
  2679 	RFs fs;
       
  2680 	ConnectAndShareFileSessionLC(fs);
       
  2681 
       
  2682 	// Set the CMsvServer and file session
       
  2683 	TMsvServerStoreManager storemanager;
       
  2684 	storemanager.SetMsvServerAndFileSession(iMsvServer,fs);
       
  2685 
       
  2686 	RFile file;
       
  2687 	storemanager.OpenAttachmentL(msgId, filePath, file);
       
  2688 	CleanupClosePushL(file);
       
  2689 
       
  2690 	// Write the sub session handle (RFile) in slot 2
       
  2691 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle());
       
  2692 	aMessage.WriteL(2,pckgSubSessionHandle);
       
  2693 
       
  2694 	// Takes care of completing with session (RFs),
       
  2695 	// SendReceieve returns session handle at client side
       
  2696 	User::LeaveIfError(file.TransferToClient(aMessage,2));
       
  2697 
       
  2698 	CleanupStack::PopAndDestroy(2,&fs); // fs, file
       
  2699 	}
       
  2700 
       
  2701 void CMsvServerSession::OpenAttachmentForWriteL(const RMessage2 &aMessage)
       
  2702 	{
       
  2703 	// Get the Message Id from slot 0
       
  2704 	TMsvId msgId = aMessage.Int0();
       
  2705 
       
  2706 	// Police request - client must be able to read the entry.
       
  2707 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::OpenAttachmentForWriteL"));
       
  2708 
       
  2709 	// Get the file name from slot 1
       
  2710 	TFileName filePath;
       
  2711 	aMessage.ReadL(1, filePath);
       
  2712 
       
  2713 	RFs fs;
       
  2714 	ConnectAndShareFileSessionLC(fs);
       
  2715 
       
  2716 	// Set the CMsvServer and file session
       
  2717 	TMsvServerStoreManager storemanager;
       
  2718 	storemanager.SetMsvServerAndFileSession(iMsvServer,fs);
       
  2719 
       
  2720 	RFile file;
       
  2721 	storemanager.OpenAttachmentForWriteL(msgId, filePath, file);
       
  2722 	CleanupClosePushL(file);
       
  2723 
       
  2724 	// Write the sub session handle (RFile) in slot 2
       
  2725 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle());
       
  2726 	aMessage.WriteL(2,pckgSubSessionHandle);
       
  2727 
       
  2728 	// Takes care of completing with session (RFs),
       
  2729 	// SendReceieve returns session handle at client side
       
  2730 	User::LeaveIfError(file.TransferToClient(aMessage,2));
       
  2731 
       
  2732 	CleanupStack::PopAndDestroy(2,&fs); // fs, file
       
  2733 	}
       
  2734 
       
  2735 void CMsvServerSession::DeleteAttachmentL(const RMessage2 &aMessage)
       
  2736 	{
       
  2737 	// Get the Message Id from slot 0
       
  2738 	TMsvId msgId = aMessage.Int0();
       
  2739 
       
  2740 	// Police request - client must be able to modify the entry.
       
  2741 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DeleteAttachmentL"));
       
  2742 
       
  2743 	// Get the file name from slot 1
       
  2744 	TFileName filePath;
       
  2745 	aMessage.ReadL(1, filePath);
       
  2746 
       
  2747 	// Set the CMsvServer and file session
       
  2748 	TMsvServerStoreManager storemanager;
       
  2749 	storemanager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession());
       
  2750 
       
  2751 	TInt error = storemanager.DeleteAttachment(msgId, filePath);
       
  2752 	aMessage.Complete(error);
       
  2753 	}
       
  2754 
       
  2755 void CMsvServerSession::RenameAttachmentL(const RMessage2 &aMessage)
       
  2756 	{
       
  2757 	// Get the Message Id from slot 0
       
  2758 	TMsvId msgId = aMessage.Int0();
       
  2759 
       
  2760 	// Police request - client must be able to modify the entry.
       
  2761 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::RenameAttachmentL"));
       
  2762 
       
  2763 	// Get the old file name from slot 1
       
  2764 	TFileName oldFileName;
       
  2765 	aMessage.ReadL(1, oldFileName);
       
  2766 
       
  2767 	// Get the new file name from slot 2
       
  2768 	TFileName newFileName;
       
  2769 	aMessage.ReadL(2, newFileName);
       
  2770 
       
  2771 	// Set the CMsvServer and file session
       
  2772 	TMsvServerStoreManager storemanager;
       
  2773 	storemanager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession());
       
  2774 
       
  2775 	TInt error = storemanager.RenameAttachment(msgId, oldFileName, newFileName);
       
  2776 	aMessage.Complete(error);
       
  2777 	}
       
  2778 
       
  2779 void CMsvServerSession::FileExistsL(const RMessage2 &aMessage)
       
  2780 	{
       
  2781 	// Get the file name from slot 10
       
  2782 	TFileName filePath;
       
  2783 	aMessage.ReadL(0, filePath);
       
  2784 
       
  2785 	// Set the CMsvServer and file session
       
  2786 	TMsvServerStoreManager storemanager;
       
  2787 	storemanager.SetMsvServerAndFileSession(iMsvServer, iMsvServer.FileSession());
       
  2788 
       
  2789 	TInt err = KErrNotFound;
       
  2790 	if( storemanager.FileExistsL(filePath) )
       
  2791 		{
       
  2792 		err = KErrNone;
       
  2793 		}
       
  2794 
       
  2795 	aMessage.Complete(err);
       
  2796 	}
       
  2797 
       
  2798 void CMsvServerSession::GetAttachmentFilePathL(const RMessage2& aMessage)
       
  2799 	{
       
  2800 	// Get the Message Id from slot 0
       
  2801 	TMsvId msgId = aMessage.Int0();
       
  2802 
       
  2803 	// Get the file path buffer from slot 1
       
  2804 	TFileName filePath;
       
  2805 	aMessage.ReadL(1, filePath);
       
  2806 
       
  2807 	// Set the CMsvServer and file session
       
  2808 	TMsvServerStoreManager storeManager;
       
  2809 	storeManager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession());
       
  2810 	storeManager.AttachmentFilePathL(msgId, filePath);
       
  2811 
       
  2812 	// Write the attachment file path back to slot 1
       
  2813 	TInt desLen = aMessage.GetDesMaxLength(1);
       
  2814 	if( desLen < filePath.Length() )
       
  2815 		{
       
  2816 		User::Leave(KErrOverflow);
       
  2817 		}
       
  2818 
       
  2819 	aMessage.WriteL(1, filePath);
       
  2820 	aMessage.Complete(KErrNone);
       
  2821 	}
       
  2822 
       
  2823 void CMsvServerSession::OpenFileStoreForReadL(const RMessage2 &aMessage)
       
  2824 	{
       
  2825 	// Get the Message Id from slot 0
       
  2826 	TMsvId msgId = aMessage.Int0();
       
  2827 
       
  2828 	// Police request - client must be able to read the entry.
       
  2829 	iMsvServer.PoliceReadEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::OpenFileStoreForReadL"));
       
  2830 
       
  2831 	RFs fs;
       
  2832 	ConnectAndShareFileSessionLC(fs);
       
  2833 
       
  2834 	// Set the CMsvServer and file session
       
  2835 	TMsvServerStoreManager storemanager;
       
  2836 	storemanager.SetMsvServerAndFileSession(iMsvServer,fs);
       
  2837 
       
  2838 	RFile file;
       
  2839 	User::LeaveIfError( storemanager.OpenFileStoreForRead(msgId,file));
       
  2840 	CleanupClosePushL(file);
       
  2841 
       
  2842 	// Write the sub session handle (RFile) in slot 1
       
  2843 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle());
       
  2844 	aMessage.WriteL(1,pckgSubSessionHandle);
       
  2845 
       
  2846 	// Takes care of completing with session (RFs),
       
  2847 	// SendReceieve returns session handle at client side
       
  2848 	User::LeaveIfError(file.TransferToClient(aMessage,1));
       
  2849 
       
  2850 	CleanupStack::PopAndDestroy(2,&fs); // fs, file
       
  2851 	}
       
  2852 
       
  2853 void CMsvServerSession::OpenTempStoreFileL(const RMessage2 &aMessage)
       
  2854 	{
       
  2855 	// Get the Message Id from slot 0
       
  2856 	TMsvId msgId = aMessage.Int0();
       
  2857 
       
  2858 	// Police request - client must be able to modify the entry.
       
  2859 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::OpenTempStoreFileL"));
       
  2860 
       
  2861 	RFs fs;
       
  2862 	ConnectAndShareFileSessionLC(fs);
       
  2863 
       
  2864 	// Set the CMsvServer and file session
       
  2865 	TMsvServerStoreManager storemanager;
       
  2866 	storemanager.SetMsvServerAndFileSession(iMsvServer,fs);
       
  2867 
       
  2868 	RFile file;
       
  2869 	storemanager.OpenTempStoreFileL(msgId,file);
       
  2870 	CleanupClosePushL(file);
       
  2871 
       
  2872 	// Write the sub session handle (RFile) in slot 1
       
  2873 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle());
       
  2874 	aMessage.WriteL(1,pckgSubSessionHandle);
       
  2875 
       
  2876 	// Takes care of completing with session (RFs),
       
  2877 	// SendReceieve returns session handle at client side
       
  2878 	User::LeaveIfError(file.TransferToClient(aMessage,1));
       
  2879 
       
  2880 	CleanupStack::PopAndDestroy(2,&fs); // fs, file
       
  2881 	}
       
  2882 
       
  2883 void CMsvServerSession::ReplaceFileStoreL(const RMessage2 &aMessage)
       
  2884 	{
       
  2885 	// Get the Message Id from slot 0
       
  2886 	TMsvId msgId = aMessage.Int0();
       
  2887 
       
  2888 	// Police request - client must be able to modify the entry.
       
  2889 	TBool local;
       
  2890 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ReplaceFileStoreL"));
       
  2891 
       
  2892 	if( local )
       
  2893 		{
       
  2894 		// Update the owner ID of the entry. NOTE - doesn't matter if actually
       
  2895 		// attachment creation fails after this - safer that owner ID is updated
       
  2896 		// at very least.
       
  2897 		UpdateOwnerIdL(msgId, aMessage.SecureId());
       
  2898 		}
       
  2899 
       
  2900 	// Set the CMsvServer and file session
       
  2901 	TMsvServerStoreManager storemanager;
       
  2902 	storemanager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession());
       
  2903 
       
  2904 	storemanager.ReplaceFileStoreL(msgId);
       
  2905 	aMessage.Complete(KErrNone);
       
  2906 	}
       
  2907 
       
  2908 void CMsvServerSession::DeleteFileStoreL(const RMessage2 &aMessage)
       
  2909 	{
       
  2910 	// Get the Message Id from slot 0
       
  2911 	TMsvId msgId = aMessage.Int0();
       
  2912 
       
  2913 	// Police request - client must be able to modify the entry.
       
  2914 	iMsvServer.PoliceModifyEntryL(aMessage, msgId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DeleteFileStoreL"));
       
  2915 
       
  2916 	// Set the CMsvServer and file session
       
  2917 	TMsvServerStoreManager storemanager;
       
  2918 	storemanager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession());
       
  2919 
       
  2920 	storemanager.DeleteFileStoreL(msgId);
       
  2921 	aMessage.Complete(KErrNone);
       
  2922 	}
       
  2923 
       
  2924 void CMsvServerSession::FileStoreExistsL(const RMessage2 &aMessage) const
       
  2925 	{
       
  2926 	// Get the Message Id from slot 0
       
  2927 	TMsvId msgId = aMessage.Int0();
       
  2928 
       
  2929 	// Set the CMsvServer and file session
       
  2930 	TMsvServerStoreManager storemanager;
       
  2931 	storemanager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession());
       
  2932 
       
  2933 	TInt storeExists = storemanager.FileStoreExistsL(msgId);
       
  2934 	aMessage.Complete(storeExists);
       
  2935 	}
       
  2936 
       
  2937 void CMsvServerSession::ConnectAndShareFileSessionLC(RFs& aFs )
       
  2938 	{
       
  2939 	User::LeaveIfError(aFs.Connect());
       
  2940 	CleanupClosePushL(aFs);
       
  2941 	User::LeaveIfError(aFs.ShareProtected());
       
  2942 	}
       
  2943 
       
  2944 void CMsvServerSession::UpdateOwnerIdL(TMsvId aId, TSecureId aOwnerId)
       
  2945 	{
       
  2946 	User::LeaveIfError(iMsvServer.IndexAdapter().LockEntry(aId));
       
  2947 	TMsvEntry* entry;
       
  2948 	iMsvServer.IndexAdapter().GetEntry(aId, entry); // error ignored
       
  2949 	User::LeaveIfError(iMsvServer.IndexAdapter().ChangeEntry(*entry, aOwnerId, ETrue));
       
  2950 	iMsvServer.IndexAdapter().ReleaseEntry(aId);		// error ignored
       
  2951 	}
       
  2952 
       
  2953 
       
  2954 /**
       
  2955 Gets the path for the file in message store for which the message Id is passed in RMessage2.
       
  2956 @param aMessage RMessage2 encapsulating client request and data.
       
  2957 @return none.
       
  2958 */
       
  2959 void CMsvServerSession::BodyTextFilePathL(const RMessage2 &aMessage)
       
  2960 	{
       
  2961 	// Get the Message Id from slot 0
       
  2962 	TMsvId bodyTextId = aMessage.Int0();
       
  2963 	
       
  2964 	// Get the file name from slot 1
       
  2965 	TFileName filePath;
       
  2966 	aMessage.ReadL(1, filePath);
       
  2967 		
       
  2968 	// Set the CMsvServer and file session 
       
  2969 	TMsvServerStoreManager storemanager; 
       
  2970 	storemanager.SetMsvServerAndFileSession(iMsvServer, iMsvServer.FileSession());
       
  2971 	
       
  2972 	storemanager.BodyTextFilePathL(bodyTextId, filePath);
       
  2973 	aMessage.WriteL(1, filePath);   
       
  2974 	aMessage.Complete(KErrNone);
       
  2975 	}
       
  2976 
       
  2977 
       
  2978 /**
       
  2979 Open the RFile located in filepath encapsulted in RMessage2 in Read mode.
       
  2980 @param aMessage RMessage2 encapsulating client request and data.
       
  2981 @return none.
       
  2982 */
       
  2983 void CMsvServerSession::OpenPlainTextFileL(const RMessage2 &aMessage)
       
  2984 	{
       
  2985 	TMsvId bodyTextId = aMessage.Int0();
       
  2986 	
       
  2987 	TFileName filePath;
       
  2988 	aMessage.ReadL(1, filePath);
       
  2989 	iMsvServer.PoliceReadEntryL(aMessage, bodyTextId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::OpenPlainTextFileL"));
       
  2990 	
       
  2991 	RFs fs;
       
  2992 	ConnectAndShareFileSessionLC(fs);
       
  2993 	RFile file;
       
  2994 	CleanupClosePushL(file);
       
  2995 		
       
  2996 	// Set the CMsvServer and file session 
       
  2997 	TMsvServerStoreManager storemanager;
       
  2998 	storemanager.SetMsvServerAndFileSession(iMsvServer, fs); 
       
  2999 	storemanager.OpenBodyTextFileForReadL(file, bodyTextId, filePath);
       
  3000 
       
  3001 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle()); 
       
  3002 	aMessage.WriteL(2,pckgSubSessionHandle);
       
  3003 	User::LeaveIfError(file.TransferToClient(aMessage, 2));
       
  3004 	CleanupStack::PopAndDestroy(2, &fs);
       
  3005 	}
       
  3006 
       
  3007 
       
  3008 /**
       
  3009 Creates the RFile in location specified by filepath encapsulted in RMessage2.
       
  3010 @param aMessage RMessage2 encapsulating client request and data.
       
  3011 @return none.
       
  3012 */  
       
  3013 void CMsvServerSession::CreatePlainTextFileL(const RMessage2 &aMessage)
       
  3014 	{
       
  3015 	TMsvId bodyTextId = aMessage.Int0();
       
  3016 	
       
  3017 	iMsvServer.PoliceModifyEntryL(aMessage, bodyTextId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CreatePlainTextFileL")); 
       
  3018 	
       
  3019 	RFs fs;
       
  3020 	ConnectAndShareFileSessionLC(fs);
       
  3021 	
       
  3022 	RFile file;
       
  3023 	CleanupClosePushL(file);
       
  3024 		
       
  3025 	// Set the CMsvServer and file session 
       
  3026 	TMsvServerStoreManager storemanager;
       
  3027 	storemanager.SetMsvServerAndFileSession(iMsvServer, fs); 
       
  3028 	storemanager.CreatePlainTextFileL(file, bodyTextId);
       
  3029 	
       
  3030 	TPckg<TInt> pckgSubSessionHandle(file.SubSessionHandle()); 
       
  3031 	aMessage.WriteL(1,pckgSubSessionHandle);
       
  3032 	User::LeaveIfError(file.TransferToClient(aMessage,1));
       
  3033 	CleanupStack::PopAndDestroy(2, &fs);
       
  3034 	}
       
  3035 
       
  3036 
       
  3037 /**
       
  3038 Deletes the RFile located in filepath encapsulted in RMessage2
       
  3039 @param aMessage RMessage2 encapsulating client request and data.
       
  3040 @return none.
       
  3041 */
       
  3042 void CMsvServerSession::DeletePlainTextFileL(const RMessage2& aMessage)
       
  3043 	{
       
  3044 	TMsvServerStoreManager storemanager;
       
  3045 	storemanager.SetMsvServerAndFileSession(iMsvServer, iMsvServer.FileSession()); 
       
  3046 	TMsvId bodyTextId = aMessage.Int0();
       
  3047 
       
  3048 	// Police request - client must be able to modify the entry.
       
  3049 	iMsvServer.PoliceModifyEntryL(aMessage, bodyTextId, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DeletePlainTextFileL"));
       
  3050 	storemanager.DeletePlainTextFileL(bodyTextId);
       
  3051 	}
       
  3052 
       
  3053 /**
       
  3054 Replace the RFile for the message Id encapsulted in RMessage2.
       
  3055 @param aMessage RMessage2 encapsulating client request and data.
       
  3056 @return none.
       
  3057 */  
       
  3058 void CMsvServerSession::ReplacePlainTextFileL(const RMessage2 &aMessage)
       
  3059 	{
       
  3060 	// Get the Message Id from slot 0
       
  3061 	TMsvId bodyTextId = aMessage.Int0();
       
  3062 	
       
  3063 	// Police request - client must be able to modify the entry.
       
  3064 	TBool local;
       
  3065 	iMsvServer.PoliceModifyEntryL(aMessage, bodyTextId, local, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ReplacePlainTextFileL")); 
       
  3066 
       
  3067 	if( local )
       
  3068 		{
       
  3069 		// Update the owner ID of the entry. 
       
  3070 		UpdateOwnerIdL(bodyTextId, aMessage.SecureId());
       
  3071 		}
       
  3072 
       
  3073 	// Set the CMsvServer and file session 
       
  3074 	TMsvServerStoreManager storemanager;
       
  3075 	storemanager.SetMsvServerAndFileSession(iMsvServer,iMsvServer.FileSession()); 
       
  3076 		
       
  3077 	storemanager.ReplacePlainTextFileL(bodyTextId);
       
  3078 	aMessage.Complete(KErrNone);
       
  3079 	}
       
  3080 
       
  3081 /**
       
  3082 Gets data from a server MTM that is not related to any operation being performed on
       
  3083 that MTM.
       
  3084 
       
  3085 @param aMessage Received message
       
  3086 */
       
  3087 void CMsvServerSession::GetNonOperationMtmDataL(const RMessage2 &aMessage)
       
  3088 	{
       
  3089 	TPtrC8 desPtr;
       
  3090 
       
  3091 	iMsvServer.GetNonOperationMtmDataL(static_cast<TMsvId>(aMessage.Int0()), static_cast<TNonOperationMtmDataType>(aMessage.Int1()), desPtr);
       
  3092 
       
  3093 	TInt desLength = aMessage.GetDesMaxLength(2);
       
  3094 	if (desLength < desPtr.Length())
       
  3095 		{
       
  3096 		aMessage.Complete(KErrOverflow);
       
  3097 		}
       
  3098 	else
       
  3099 		{
       
  3100 		WriteL(aMessage, 2, desPtr);
       
  3101 		aMessage.Complete(KErrNone);
       
  3102 		}
       
  3103 	}
       
  3104 
       
  3105 
       
  3106 /*****************************************
       
  3107  * SearchSortOnHeaderAndBodytMsgL() :
       
  3108  * Get the Query data from  client to make Search-Sort on HEADER abd BODY
       
  3109  * in meassage server.
       
  3110  * 
       
  3111  * @param aMessage Received message
       
  3112  ****************************************/ 
       
  3113 void CMsvServerSession::SearchSortOnHeaderAndBodytMsgL(const RMessage2& aMessage)
       
  3114 	{
       
  3115 	//Header and body.
       
  3116 	TMsvOp operationId = aMessage.Int0();
       
  3117 		
       
  3118 	HBufC8* buffer = NULL;
       
  3119 	ReadBufferL(aMessage, 1, buffer);
       
  3120 	CleanupStack::PushL(buffer);
       
  3121 
       
  3122 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::SearchSortOnHeaderAndBodytMsgL"));
       
  3123 	
       
  3124 	CMsvSearchSortQuery* query= CMsvSearchSortQuery::NewL() ;
       
  3125 	CleanupStack::PushL(query);
       
  3126 
       
  3127 	TMsvPackQuery packedQuery(buffer);
       
  3128 	packedQuery.UnpackQuery(query);
       
  3129 		
       
  3130 	TBool markQuery = aMessage.Int2();
       
  3131 	
       
  3132 	CMsvSearchSortCacheEntry *aEntry = CMsvSearchSortCacheEntry::CreateQueryEntryL(query, markQuery);
       
  3133 	CleanupStack::PushL(aEntry);
       
  3134 	
       
  3135 	TInt indexCount =CMSvSearchSortCacheManager::Instance()->QueryExists(*aEntry);
       
  3136 	//Check the query is exist or not.
       
  3137 	
       
  3138 	if(CMSvSearchSortCacheManager::Instance()->QueryExists(*aEntry)<0)
       
  3139 		{
       
  3140 	
       
  3141 		//NEW Query
       
  3142 		if(!aEntry->IsSortQuery())
       
  3143 			{
       
  3144 			CMSvSearchSortCacheManager::Instance()->TypeOfSearchQuery(*aEntry);
       
  3145 			}
       
  3146 		else
       
  3147 			{
       
  3148 			CMSvSearchSortCacheManager::Instance()->TypeOfSortQuery(*aEntry);
       
  3149 			}
       
  3150 		//Do process query.
       
  3151 		CMSvSearchSortCacheManager::Instance()->DoProcessQueryL(*aEntry);
       
  3152 		}
       
  3153 	else 
       
  3154 		{
       
  3155 		//Existing Query
       
  3156 		CMSvSearchSortCacheManager::Instance()->DoProcessQueryL(indexCount);
       
  3157 		}
       
  3158 	CleanupStack::Pop();//  aEntry
       
  3159 	CleanupStack::PopAndDestroy(2); //  query, buffer
       
  3160 	aMessage.Complete(KErrNone);
       
  3161 	}
       
  3162 	
       
  3163 
       
  3164 /*****************************************
       
  3165  * SearchSortOnIndexEntry() :
       
  3166  * Get the Query data from  client to make Search-Sort on INDEX ENTRY
       
  3167  * in meassage server.
       
  3168  * 
       
  3169  * @param aMessage Received message
       
  3170  ****************************************/ 
       
  3171 
       
  3172 void CMsvServerSession::SearchSortOnIndexEntryL(const RMessage2& aMessage)
       
  3173 	{
       
  3174 	//Search sort on index entry.
       
  3175 	// Recover the operation data
       
  3176 	TMsvOp operationId = aMessage.Int0();
       
  3177 	
       
  3178 	HBufC8* buffer = NULL;
       
  3179 	ReadBufferL(aMessage, 1, buffer);
       
  3180 	CleanupStack::PushL(buffer);
       
  3181 	
       
  3182 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::SearchSortOnIndexEntryL"));
       
  3183 
       
  3184 	CMsvSearchSortQuery* query= CMsvSearchSortQuery::NewL() ;
       
  3185 	CleanupStack::PushL(query);
       
  3186 
       
  3187 	TMsvPackQuery packedQuery(buffer);
       
  3188 	packedQuery.UnpackQuery(query);
       
  3189 
       
  3190 		
       
  3191 	TBool markQuery = aMessage.Int2();
       
  3192 	TInt  iteratorCount = aMessage.Int3();
       
  3193 	
       
  3194 	CMsvSearchSortCacheEntry *aEntry = CMsvSearchSortCacheEntry::CreateQueryEntryL(query, markQuery,iteratorCount);
       
  3195 	CleanupStack::PushL(aEntry);
       
  3196 	
       
  3197 	TInt indexCount =CMSvSearchSortCacheManager::Instance()->QueryExists(*aEntry);
       
  3198 	//Query Exist or not
       
  3199 	if(CMSvSearchSortCacheManager::Instance()->QueryExists(*aEntry)<0)
       
  3200 		{
       
  3201 		//NEW Query
       
  3202 		if(!aEntry->IsSortQuery())
       
  3203 			{
       
  3204 			CMSvSearchSortCacheManager::Instance()->TypeOfSearchQuery(*aEntry);
       
  3205 			}
       
  3206 		else
       
  3207 			{
       
  3208 			CMSvSearchSortCacheManager::Instance()->TypeOfSortQuery(*aEntry);
       
  3209 			}
       
  3210 		//Do process query.
       
  3211 		CMSvSearchSortCacheManager::Instance()->DoProcessQueryL(*aEntry,iteratorCount);
       
  3212 		}
       
  3213 	else 
       
  3214 		{
       
  3215 		//Existing Query
       
  3216 		CMSvSearchSortCacheManager::Instance()->DoProcessQueryL(indexCount);
       
  3217 		}
       
  3218 	
       
  3219 	CleanupStack::Pop();		// aEntry
       
  3220 	CleanupStack::PopAndDestroy(2, buffer); // buffer, CmsvSearchSortQuery
       
  3221 	aMessage.Complete(KErrNone);
       
  3222 	}
       
  3223 
       
  3224 void CMsvServerSession::SendSearchSortIndexEntryResultL(const RMessage2 &aMessage)
       
  3225 	{
       
  3226 	TBool resultType = aMessage.Int0();
       
  3227 	
       
  3228 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::SendSearchSortIndexEntryResultL"));
       
  3229 	//id's
       
  3230 	if(resultType) // EResultAsTMsvID
       
  3231 		{
       
  3232 		TMsvPackedIdOperation op(iBuffer);
       
  3233 		TInt error = op.Pack(CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray);
       
  3234 		while (error != KErrNone)
       
  3235 			{
       
  3236 			// increase the size of the buffer and try again
       
  3237 			iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3238 			iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3239 			error = op.Pack(CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray);
       
  3240 			}
       
  3241 		}
       
  3242 	else  // EResultAsTMsvEntry
       
  3243 		{
       
  3244 		//As of now it takes TMsvId.
       
  3245 		}
       
  3246 	WriteBufferL(aMessage, 1);
       
  3247 	CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray.Reset();
       
  3248 	CMSvSearchSortCacheManager::Instance()->iToFindResultAsIdArray.Reset();
       
  3249 	CMSvSearchSortCacheManager::Instance()->iMsvIdWithSortFieldArray.Reset();
       
  3250 	aMessage.Complete(KErrNone);
       
  3251 	}
       
  3252 
       
  3253 
       
  3254 
       
  3255 //sending resultas
       
  3256 void CMsvServerSession::SendResultOrIdsToSearchL(const RMessage2& aMessage)
       
  3257 	{
       
  3258 	// result type (is it a Final result, Partial Result or New query)
       
  3259 	TPckgBuf<TInt> resultType;
       
  3260 	TInt errorMem = KErrNone;
       
  3261 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::SendResultOrIdsToSearchL"));   
       
  3262 	//resuttype
       
  3263 	resultType = CMSvSearchSortCacheManager::Instance()->iReturnResultType;
       
  3264 	
       
  3265 	if(CMSvSearchSortCacheManager::Instance()->iReturnResultType == KMsvSearchSortQueryIdNotFound)
       
  3266 		{
       
  3267 		WriteL(aMessage, 1, resultType);
       
  3268 		}
       
  3269 	else
       
  3270 		{
       
  3271 		// need to send data to client according resultType 
       
  3272 		// result type is KFinalResult, KNewQuery or KPartilaResult
       
  3273 		TInt error = KErrNone;
       
  3274 		
       
  3275 		TMsvPackedIdOperation op(iBuffer);
       
  3276 		
       
  3277 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
  3278 		error = op.Pack(CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray);
       
  3279 		while (error != KErrNone)
       
  3280 			{
       
  3281 			// increase the size of the buffer and try again
       
  3282 			iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3283 			iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3284 			error = op.Pack(CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray);
       
  3285 			}
       
  3286 
       
  3287 #else   // else SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB	
       
  3288 	
       
  3289 		if(CMSvSearchSortCacheManager::Instance()->iReturnResultType == KFinalResult)
       
  3290 			{
       
  3291 			CMSvSearchSortCacheManager::Instance()->iSearchSortDeltaCache->iIsHeaderSearchEnabled = EFalse;
       
  3292 			error = op.Pack(CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray);
       
  3293 			
       
  3294 			while (error != KErrNone)
       
  3295 				{
       
  3296 				// increase the size of the buffer and try again
       
  3297 				iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3298 				iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3299 				error = op.Pack(CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray);
       
  3300 				}
       
  3301 			}
       
  3302 		else if(CMSvSearchSortCacheManager::Instance()->iReturnResultType == KNewQuery)
       
  3303 			{
       
  3304 			error = op.Pack(CMSvSearchSortCacheManager::Instance()->iToFindResultAsIdArray);
       
  3305 			while (error != KErrNone)
       
  3306 				{
       
  3307 				// increase the size of the buffer and try again
       
  3308 				iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3309 				iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3310 				error = op.Pack(CMSvSearchSortCacheManager::Instance()->iToFindResultAsIdArray);
       
  3311 				}
       
  3312 			}
       
  3313 		else // for KPartialResult
       
  3314 			{
       
  3315 			error = op.Pack(CMSvSearchSortCacheManager::Instance()->iDeltaCacheIdArray);
       
  3316 			while (error != KErrNone)
       
  3317 				{
       
  3318 				// increase the size of the buffer and try again
       
  3319 				iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3320 				iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3321 				error = op.Pack(CMSvSearchSortCacheManager::Instance()->iDeltaCacheIdArray);
       
  3322 				}
       
  3323 			}
       
  3324 #endif // SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB
       
  3325 			
       
  3326 		// send Id's in buffer
       
  3327 		TRAP(errorMem,WriteBufferL(aMessage,0));
       
  3328 			
       
  3329 		//send resultType to client, whether it's final result, partial result or newQuery.
       
  3330 		WriteL(aMessage,1,resultType);
       
  3331 		}
       
  3332 	
       
  3333 	//needs to reset RArray
       
  3334 	if(errorMem==KErrNone)
       
  3335 		{
       
  3336 		CMSvSearchSortCacheManager::Instance()->iFinalResultAsIdArray.Reset();
       
  3337 		CMSvSearchSortCacheManager::Instance()->iToFindResultAsIdArray.Reset();
       
  3338 		CMSvSearchSortCacheManager::Instance()->iMsvIdWithSortFieldArray.Reset();
       
  3339 		}
       
  3340 
       
  3341 	aMessage.Complete(errorMem);
       
  3342 	}
       
  3343 
       
  3344 void CMsvServerSession::GetSearchSortResultCountL(const RMessage2& aMessage)
       
  3345 	{
       
  3346 	TPckgBuf<TInt> resultCount;
       
  3347 	
       
  3348 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetSearchSortResultCountL"));  
       
  3349 		
       
  3350 	TInt  index = CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count();
       
  3351 	CMSvSearchSortCacheManager::Instance()->ResultCountL(index-1);
       
  3352 	
       
  3353 	resultCount() = CMSvSearchSortCacheManager::Instance()->iFinalResultCount;//Contain the result.
       
  3354 	WriteL(aMessage, 0, resultCount);
       
  3355 	CMSvSearchSortCacheManager::Instance()->iFinalResultCount = 0;
       
  3356 	aMessage.Complete(KErrNone);
       
  3357 	}
       
  3358 	
       
  3359 	
       
  3360 void CMsvServerSession::GetResultInIteratorL(const RMessage2& aMessage)
       
  3361 	{
       
  3362 	TPckgBuf<TMsvId> messageId;
       
  3363 	TPckgBuf<TInt> count;
       
  3364 	
       
  3365 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetResultInIteratorL"));   
       
  3366 		
       
  3367 	TInt  index = CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count();
       
  3368 	CMSvSearchSortCacheManager::Instance()->NextResultForInteraratorL(index-1);
       
  3369 	messageId() = CMSvSearchSortCacheManager::Instance()->iIteratorId;
       
  3370 	count() = CMSvSearchSortCacheManager::Instance()->iIteratorRemainingResultCount;
       
  3371 	
       
  3372 	//result as TMsvId
       
  3373 	WriteL(aMessage, 0, messageId);
       
  3374 	//for remaing count
       
  3375 	WriteL(aMessage, 1, count);
       
  3376 	
       
  3377 	CMSvSearchSortCacheManager::Instance()->iIteratorId = 0;
       
  3378 	aMessage.Complete(KErrNone);
       
  3379 	}
       
  3380 	
       
  3381 void CMsvServerSession::GetNextEntryInIteratorL(const RMessage2& aMessage)
       
  3382 	{
       
  3383 	//need this varible to send remaining count to client
       
  3384 	TPckgBuf<TInt> count;
       
  3385 	
       
  3386 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetNextEntryInIteratorL"));	
       
  3387 	
       
  3388 	TInt  index = CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count();
       
  3389 	CMSvSearchSortCacheManager::Instance()->NextResultForInteraratorL(index-1);
       
  3390 	
       
  3391 	// package the entry
       
  3392 	TMsvPackedEntry packedEntry(iBuffer);
       
  3393 	TInt error = packedEntry.PackEntry(CMSvSearchSortCacheManager::Instance()->iIteratorEntry);
       
  3394 	while(error!=KErrNone)
       
  3395 		{
       
  3396 		// increase the size of the buffer and try again
       
  3397 		iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3398 		iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3399 		error = packedEntry.PackEntry(CMSvSearchSortCacheManager::Instance()->iIteratorEntry);
       
  3400 		}
       
  3401 	//needs fill the remaing count
       
  3402 	count() =  CMSvSearchSortCacheManager::Instance()->iIteratorRemainingResultCount;
       
  3403 	
       
  3404 	//result as TMsvEntry
       
  3405 	WriteBufferL(aMessage, 0);
       
  3406 	//for remaing count
       
  3407 	WriteL(aMessage, 1, count);
       
  3408 	
       
  3409 	aMessage.Complete(KErrNone);
       
  3410 	}
       
  3411 	
       
  3412 
       
  3413 void CMsvServerSession::GetQueryIDL(const RMessage2& aMessage)
       
  3414 	{
       
  3415 	TPckgBuf<TInt> queryId;
       
  3416 	
       
  3417 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetQueryIDL"));	
       
  3418 	
       
  3419 	queryId() = CMSvSearchSortCacheManager::Instance()->QueryID();
       
  3420 	
       
  3421 	WriteL(aMessage, 0, queryId);
       
  3422 	aMessage.Complete(KErrNone);
       
  3423 	}
       
  3424 	
       
  3425 void CMsvServerSession::UpdateSearchSortCacheWithSortFiledL(const RMessage2& aMessage)
       
  3426 	{
       
  3427 	TInt  index = CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count();
       
  3428 	
       
  3429 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::UpdateSearchSortCacheWithSortFiledL"));	
       
  3430 	
       
  3431 	// get the length
       
  3432 	TInt desLen = aMessage.GetDesLength(1);
       
  3433 	if(desLen == 0)
       
  3434 		{
       
  3435 		aMessage.Complete(KErrArgument);
       
  3436 		return;
       
  3437 		}
       
  3438 
       
  3439 	// allocate buffer copy the data
       
  3440 	CBufFlat* buffer = CBufFlat::NewL(desLen);
       
  3441 	CleanupStack::PushL(buffer);
       
  3442 	buffer->ExpandL(0, desLen);
       
  3443 	RBufReadStream readStream(*buffer);
       
  3444 	CleanupClosePushL (readStream);
       
  3445 	TPtr8 desPtr = buffer->Ptr(0);
       
  3446 	
       
  3447 	TRAPD(err,  aMessage.ReadL(1, desPtr));
       
  3448 	User::LeaveIfError(err);
       
  3449 	// object of TMsvPackedIdAndMessagePart 
       
  3450 	TMsvPackedIdAndMessagePart packIdAndMessagePart;
       
  3451 	
       
  3452 	//extract TMsvId with Sort field
       
  3453 	packIdAndMessagePart.InternalizeL(readStream, CMSvSearchSortCacheManager::Instance()->iMsvIdWithSortFieldArray);
       
  3454 	
       
  3455 	//Manager class update the Db.
       
  3456 	CMSvSearchSortCacheManager::Instance()->StoreSortResultL(index,CMSvSearchSortCacheManager::Instance()->iMsvIdWithSortFieldArray);
       
  3457 	
       
  3458 	CleanupStack::PopAndDestroy(2, buffer);
       
  3459 	aMessage.Complete(KErrNone);
       
  3460 	}
       
  3461 	
       
  3462 void CMsvServerSession::QueryUnMarkedL(const RMessage2& aMessage)
       
  3463 	{
       
  3464 	
       
  3465 	TInt  queryID = aMessage.Int0();
       
  3466 	
       
  3467 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::PoliceSearchSortQueryReadRequestL"));  
       
  3468 	
       
  3469 	TInt index =  CMSvSearchSortCacheManager::Instance()->QueryExists(queryID);
       
  3470 	if(index>0)
       
  3471 		{
       
  3472 		CMSvSearchSortCacheManager::Instance()->UnMarkedQuery(index);
       
  3473 		}
       
  3474 	else
       
  3475 		{
       
  3476 		aMessage.Complete(KErrNotFound);	
       
  3477 		}   
       
  3478 	aMessage.Complete(KErrNone);
       
  3479 	}
       
  3480 
       
  3481 void CMsvServerSession::GetResultForQueryIDL(const RMessage2& aMessage)
       
  3482 	{
       
  3483 	TInt operationId = aMessage.Int0();
       
  3484 	TInt queryID = aMessage.Int1();
       
  3485 	
       
  3486 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetResultForQueryIDL"));   
       
  3487 	
       
  3488 	TInt index =  CMSvSearchSortCacheManager::Instance()->QueryExists(queryID);
       
  3489 	if(index>=0)
       
  3490 		{
       
  3491 		CMSvSearchSortCacheManager::Instance()->DoProcessQueryL(index);
       
  3492 		}
       
  3493 	else
       
  3494 		{
       
  3495 		//Invalid query id.
       
  3496 		CMSvSearchSortCacheManager::Instance()->iReturnResultType = KMsvSearchSortQueryIdNotFound;
       
  3497 		}
       
  3498 	
       
  3499 	aMessage.Complete(KErrNone);
       
  3500 	}
       
  3501 
       
  3502 
       
  3503 void CMsvServerSession::GetSearchSortProgressInfoL(const RMessage2 &aMessage)
       
  3504 	{
       
  3505 	TInt operationId = aMessage.Int0();
       
  3506 	TPckg<TInt> progress(KMsvSearchSortOpNone);
       
  3507 	
       
  3508 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetSearchSortProgressInfoL")); 
       
  3509 	
       
  3510 	progress() = CMSvSearchSortCacheManager::Instance()->ReturnProgressInfo();
       
  3511 	
       
  3512 	WriteL(aMessage, 1, progress);
       
  3513 	aMessage.Complete(KErrNone);
       
  3514 	}
       
  3515 	
       
  3516 void CMsvServerSession::SearchSortOperationCancelL(const RMessage2& aMessage)
       
  3517 	{
       
  3518 	TInt operationId = aMessage.Int0();
       
  3519 	
       
  3520 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::SearchSortOperationCancel"));  
       
  3521 	
       
  3522 	CMSvSearchSortCacheManager::Instance()->CancelSearchSortOperation();
       
  3523 	//cancel the current oeration and delete the query and correspnding entry from DB.
       
  3524 	aMessage.Complete(KErrNone);
       
  3525 	}
       
  3526 
       
  3527 //Update search sort results to DB, and sort by TMsvEntry fields invalid id's
       
  3528 void CMsvServerSession::UpdateSearchSortResultsAndSortByEntryL(const RMessage2& aMessage)
       
  3529 	{
       
  3530 	
       
  3531 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::UpdateSearchSortResultsAndSortByEntryL")); 
       
  3532 	
       
  3533 	TInt  index = CMSvSearchSortCacheManager::Instance()->iManagerEntry->Count();
       
  3534 
       
  3535 	HBufC8* buffer = NULL;
       
  3536 	ReadBufferL(aMessage, 1, buffer);
       
  3537 	CleanupStack::PushL(buffer);
       
  3538 
       
  3539 	// Unpack the entries from the buffer
       
  3540 	TMsvPackedIdOperation packedIdOperation(buffer);
       
  3541 	packedIdOperation.UnpackL(CMSvSearchSortCacheManager::Instance()->iUpdateIdsToCacheArray);
       
  3542 	
       
  3543 	// Update DB and sort by TMsvEnty field with existing partil query
       
  3544 	// Invalid ID need to delete.
       
  3545 	CMSvSearchSortCacheManager::Instance()->StoreResultL(index,CMSvSearchSortCacheManager::Instance()->iUpdateIdsToCacheArray);
       
  3546 	
       
  3547 	CleanupStack::PopAndDestroy(buffer); // buffer
       
  3548 	
       
  3549 	aMessage.Complete(KErrNone);
       
  3550 	}
       
  3551 
       
  3552 void CMsvServerSession::CopyQueryDataL(const RMessage2& aMessage)
       
  3553 	{
       
  3554 	TInt operationId = aMessage.Int0();
       
  3555 	TInt queryId = aMessage.Int1();
       
  3556 	
       
  3557 	iMsvServer.PoliceSearchSortQueryReadRequestL(aMessage,__PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CopyQueryDataL")); 
       
  3558 	
       
  3559 	CMsvSearchSortQuery* searchSortQuery= CMsvSearchSortQuery::NewL() ;
       
  3560 	CleanupStack::PushL(searchSortQuery);
       
  3561 	
       
  3562 	TInt index =  CMSvSearchSortCacheManager::Instance()->QueryExists(queryId);
       
  3563 	if(index>=0)
       
  3564 		{
       
  3565 		//This below function will return  TMsvSearchSortQuery object .
       
  3566 		CMSvSearchSortCacheManager::Instance()->RetrunQuery(index, searchSortQuery);
       
  3567 		}
       
  3568 	else
       
  3569 		{
       
  3570 		//Query id not found.
       
  3571 		CleanupStack::PopAndDestroy(searchSortQuery); // searchSortQuery
       
  3572 		aMessage.Complete(KErrNotFound);
       
  3573 		return;
       
  3574 		}
       
  3575 	
       
  3576 	TMsvPackQuery packedQuery(iBuffer);
       
  3577 	
       
  3578 	TInt error = KErrNone;
       
  3579 	error = packedQuery.PackQuery(searchSortQuery);
       
  3580 	
       
  3581 	WriteBufferL(aMessage, 2);
       
  3582 	CleanupStack::PopAndDestroy(searchSortQuery); // searchSortQuery
       
  3583 	aMessage.Complete(error);
       
  3584 	}
       
  3585 
       
  3586 
       
  3587 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
  3588 /**
       
  3589  * GetChildrenAllL()
       
  3590  *
       
  3591  * @param RMessage2.
       
  3592  *
       
  3593  * Code changes for PREQ 557.
       
  3594  * Service request EMsvGetChildrenALL.
       
  3595  * If the parent id is a standard folder the function fetches
       
  3596  * the child entries from all drives in the preferred drive list
       
  3597  * as maintained by server.
       
  3598  */
       
  3599  void CMsvServerSession::GetChildrenAllL(const RMessage2 &aMessage)
       
  3600 	{
       
  3601 	// Reset internal datastructure.
       
  3602 	iChildrenSelection->Reset();
       
  3603 	iChildrenSelectionIds->Reset();
       
  3604 
       
  3605 	// Copy across the children details structure.
       
  3606 	TPckgBuf<TMsvChildrenDetails> children;
       
  3607 	aMessage.ReadL(0, children);
       
  3608 
       
  3609 	// Check that the children details arguments are empty. 
       
  3610 	// Panic in debug mode but try to handle it gracefully 
       
  3611 	// in release code.
       
  3612 	__ASSERT_DEBUG( children().iTotalNumberChildren==0 &&
       
  3613 					children().iNumberChildrenInArray==0, PanicServer(EMsvChildrenDetailsNotEmpty2));
       
  3614 
       
  3615 	if( children().iTotalNumberChildren != 0 || children().iNumberChildrenInArray != 0 )
       
  3616 		{
       
  3617 		aMessage.Complete(KErrArgument);
       
  3618 		return;
       
  3619 		}
       
  3620 	
       
  3621 	// The function is allowed only for following standard 
       
  3622 	// folders of current drive:
       
  3623 	// Inbox, Outbox, Draft, Sent and Deleted.
       
  3624 	if( (KCurrentDriveId != GetDriveId(children().iParentId)) ||
       
  3625 		( KMsvLocalServiceIndexEntryId >= UnmaskTMsvId(children().iParentId) ||
       
  3626 		  KMsvUnknownServiceIndexEntryId <= UnmaskTMsvId(children().iParentId) )
       
  3627 	  )
       
  3628 		{
       
  3629 		aMessage.Complete(KErrArgument);
       
  3630 		return;
       
  3631 		}
       
  3632 		
       
  3633 	// Copy across the sort order.
       
  3634 	TPckgBuf<TMsvSelectionOrdering> order;
       
  3635 	aMessage.ReadL(1, order);
       
  3636 
       
  3637 	// Get the children as a selection. Need to 
       
  3638 	// filter the list via the client secure ID 
       
  3639 	// if the client is not trusted with Read User Data.
       
  3640 	TBool filterByOwnerId = !aMessage.HasCapability(ECapabilityReadUserData);
       
  3641 	
       
  3642 	iMsvServer.IndexAdapter().GetChildrenAllL(children().iParentId, *iChildrenSelection, order(), KUidMsvNullEntry, filterByOwnerId, aMessage.SecureId());
       
  3643 
       
  3644 	// Return number of children.
       
  3645 	children().iTotalNumberChildren = iChildrenSelection->Count();
       
  3646 
       
  3647 	// Package up the entries.
       
  3648 	iBuffer->Des().SetMax();
       
  3649 	TMsvPackedEntryArray packedEntryArray(iBuffer);
       
  3650 	
       
  3651 	TInt count=0;
       
  3652 	TInt error=KErrNone;
       
  3653 	TInt totalCount=iChildrenSelection->Count();
       
  3654 	for (; count<totalCount; count++)
       
  3655 		{
       
  3656 		error = packedEntryArray.PackEntry(*iChildrenSelection->At(count));
       
  3657 		if (error)
       
  3658 			{
       
  3659 			children().iLastEntryInArray = count;
       
  3660 			break;
       
  3661 			}
       
  3662 		}
       
  3663 	// return number of children in the array
       
  3664 	children().iNumberChildrenInArray = count;
       
  3665 
       
  3666 	// write the children to client
       
  3667 	WriteL(aMessage, 0, children);
       
  3668 	
       
  3669 	// write the array to the client (if any entries are in the array)
       
  3670 	if (children().iNumberChildrenInArray)
       
  3671 		WriteL(aMessage, 2, iBuffer->Des());
       
  3672 
       
  3673 	if (error==KErrNone)
       
  3674 		{
       
  3675 		// reset the member data
       
  3676 		iChildrenSelection->Reset();
       
  3677 		}
       
  3678 	else
       
  3679 		{
       
  3680 		// keep an list of the ids separatelyin case they are deleted by another client
       
  3681 		TInt totalCount=iChildrenSelection->Count();
       
  3682 		for (count=0; count<totalCount; count++)
       
  3683 			iChildrenSelectionIds->AppendL(iChildrenSelection->At(count)->Id());
       
  3684 		iChildrenDetails = children();
       
  3685 		}
       
  3686 
       
  3687 	// signal the client and finished with selection
       
  3688 	aMessage.Complete(error);
       
  3689 	}
       
  3690 
       
  3691 
       
  3692 
       
  3693 /** 
       
  3694  * GetChildIdsAll()
       
  3695  *
       
  3696  * Gets the children of a parent entry specified as first argument.
       
  3697  * If the passed parent is a standard folder the function will fetch 
       
  3698  * entries from all drives currently present in the preferred drive list.
       
  3699  */
       
  3700 void CMsvServerSession::GetChildIdsAllL(const RMessage2& aMessage)
       
  3701 	{
       
  3702 	TMsvId id = aMessage.Int1();
       
  3703 
       
  3704 	// The function is allowed only for following standard 
       
  3705 	// folders of current drive:
       
  3706 	// Inbox, Outbox, Draft, Sent and Deleted.
       
  3707 	if( (KCurrentDriveId != GetDriveId(id)) ||
       
  3708 		(KMsvLocalServiceIndexEntryId >= UnmaskTMsvId(id)) ||
       
  3709 		(KMsvUnknownServiceIndexEntryId <= UnmaskTMsvId(id))
       
  3710 	  )
       
  3711 		{
       
  3712 		aMessage.Complete(KErrArgument);
       
  3713 		return;
       
  3714 		}
       
  3715 
       
  3716 	CMsvEntrySelection* selection = new(ELeave)CMsvEntrySelection;
       
  3717 	CleanupStack::PushL(selection);
       
  3718 	HBufC8* buffer = NULL;
       
  3719 	ReadBufferL(aMessage, 0, buffer);
       
  3720 	CleanupStack::PushL(buffer);
       
  3721 
       
  3722 	CMsvEntryFilter* filter = CMsvEntryFilter::NewLC();
       
  3723 	TMsvPackedEntryFilter package(buffer);
       
  3724 	package.UnpackFilter(*filter);
       
  3725 
       
  3726 	// Need to filter the list via the client secure ID if the client is not
       
  3727 	// trusted with Read User Data.
       
  3728 	TBool filterByOwnerId = !aMessage.HasCapability(ECapabilityReadUserData);
       
  3729 
       
  3730 	TInt error = iMsvServer.IndexAdapter().GetChildrenIdAll(id, *filter, *selection, filterByOwnerId, aMessage.SecureId());
       
  3731 	if(KErrNone != error)
       
  3732 		{
       
  3733 		aMessage.Complete(error);
       
  3734 		CleanupStack::PopAndDestroy(3); // selection, buffer, filter
       
  3735 		}
       
  3736 		
       
  3737 	TMsvPackedOperation op(iBuffer);
       
  3738 
       
  3739 	error = op.Pack(*selection, 0, 0);
       
  3740 	while (error != KErrNone)
       
  3741 		{
       
  3742 		// increase the size of the buffer and try again
       
  3743 		iBuffer->Des().SetLength(0); // to avoid copying contents
       
  3744 		iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  3745 		error = op.Pack(*selection, 0, 0);
       
  3746 		}
       
  3747 
       
  3748 	WriteBufferL(aMessage, 0);
       
  3749 
       
  3750 	aMessage.Complete(KErrNone);
       
  3751 	CleanupStack::PopAndDestroy(3); // selection, buffer, filter
       
  3752 	}
       
  3753 	
       
  3754 	
       
  3755 
       
  3756 
       
  3757 /**
       
  3758  * MessageDrive()
       
  3759  *
       
  3760  * @param RMessage2.
       
  3761  *
       
  3762  * Code changes for PREQ 557.
       
  3763  * Service request EMsvGetMessageDrive.
       
  3764  */
       
  3765 void CMsvServerSession::MessageDrive(const RMessage2 &aMessage)
       
  3766 	{
       
  3767 	aMessage.Complete(CMsvPreferredDriveList::GetDriveList()->CurrentDriveNumber());		
       
  3768 	}
       
  3769 
       
  3770 
       
  3771 
       
  3772 
       
  3773 /**
       
  3774  * CurrentDriveInfoL()
       
  3775  *
       
  3776  * @param RMessage2.
       
  3777  *
       
  3778  * Code changes for PREQ 557.
       
  3779  * Service request EMsvGetCurrentDriveInfo.
       
  3780  */	 
       
  3781 void CMsvServerSession::CurrentDriveInfoL(const RMessage2 &aMessage)
       
  3782 	{
       
  3783 	TMsvPreferredDrive driveInfo;
       
  3784 	TUint currentDriveIndex = CMsvPreferredDriveList::GetDriveList()->CurrentDriveIndex();
       
  3785 	TRAPD(err, CMsvPreferredDriveList::GetDriveList()->DriveInfoL(currentDriveIndex, driveInfo));
       
  3786 	if(KErrNone == err)
       
  3787 		{
       
  3788 		TPckgBuf<TDriveNumber> driveNum;
       
  3789 		TPckgBuf<TUint> priority;
       
  3790 		
       
  3791 		driveNum() = driveInfo.driveNum;
       
  3792 		priority() = currentDriveIndex + 1;
       
  3793 		
       
  3794 		WriteL(aMessage, 0, driveNum);
       
  3795 		WriteL(aMessage, 1, priority);
       
  3796 		}
       
  3797 	aMessage.Complete(err);
       
  3798 	}
       
  3799 	
       
  3800 
       
  3801 
       
  3802 /**
       
  3803  * DriveListL()
       
  3804  *
       
  3805  * @param RMessage2.
       
  3806  *
       
  3807  * Code changes for PREQ 557.
       
  3808  * Service request EMsvGetDriveList.
       
  3809  */	 
       
  3810 void CMsvServerSession::DriveListL(const RMessage2 &aMessage)
       
  3811 	{
       
  3812 	CMsvPreferredDriveList *driveList = CMsvPreferredDriveList::GetDriveList();
       
  3813 	RArray<TDriveNumber> driveNumList;
       
  3814 	CleanupClosePushL(driveNumList);
       
  3815 	
       
  3816 	for(TInt index=0; index<driveList->Count(); index++)
       
  3817 		{
       
  3818 		driveNumList.AppendL((*driveList)[index].driveNum);	 
       
  3819 		}
       
  3820 	
       
  3821 	TMsvPackedDriveIdOperation driveOp(iBuffer);
       
  3822 	User::LeaveIfError(driveOp.Pack(driveNumList));
       
  3823 	WriteBufferL(aMessage, 0);
       
  3824 	
       
  3825 	CleanupStack::PopAndDestroy();	  // driveNumList
       
  3826 	aMessage.Complete(KErrNone);
       
  3827 	}
       
  3828 	
       
  3829 
       
  3830 
       
  3831 /**
       
  3832  * AvailableDriveListL()
       
  3833  *
       
  3834  * @param RMessage2.
       
  3835  *
       
  3836  * Code changes for PREQ 557.
       
  3837  * Service request EMsvGetAvailableDriveList.
       
  3838  */ 
       
  3839 void CMsvServerSession::AvailableDriveListL(const RMessage2 &aMessage)
       
  3840 	{
       
  3841 	CMsvPreferredDriveList *driveList = CMsvPreferredDriveList::GetDriveList();
       
  3842 	RArray<TDriveNumber> driveNumList;
       
  3843 	CleanupClosePushL(driveNumList);
       
  3844 	
       
  3845 	for(TInt index=0; index<driveList->Count(); index++)
       
  3846 		{
       
  3847 		if(EMsvMessageStoreAvailableStatus == (*driveList)[index].status)
       
  3848 			{
       
  3849 			driveNumList.AppendL((*driveList)[index].driveNum);	 
       
  3850 			}
       
  3851 		}
       
  3852 	
       
  3853 	TMsvPackedDriveIdOperation driveOp(iBuffer);
       
  3854 	User::LeaveIfError(driveOp.Pack(driveNumList));
       
  3855 	WriteBufferL(aMessage, 0);
       
  3856 	
       
  3857 	CleanupStack::PopAndDestroy();	  // driveNumList
       
  3858 	aMessage.Complete(KErrNone);
       
  3859 	}
       
  3860 	
       
  3861 	
       
  3862 	
       
  3863 
       
  3864 /**
       
  3865  * AddDriveL()
       
  3866  *
       
  3867  * @param RMessage2.
       
  3868  *
       
  3869  * Code changes for PREQ 557.
       
  3870  * Service request EMsvAddDriveToDriveList.
       
  3871  */	 
       
  3872 void CMsvServerSession::AddDriveL(const RMessage2 &aMessage)
       
  3873 	{
       
  3874 	TDriveNumber driveNumber = (TDriveNumber) aMessage.Int0();
       
  3875 	TUint priority = (TUint) aMessage.Int1();
       
  3876 
       
  3877 	TRAPD(err, iMsvServer.AddDriveToListL(driveNumber, priority, this));
       
  3878 	if(KErrNone == err)
       
  3879 		{
       
  3880 		TPckgBuf<TUint> priorityBuf;
       
  3881 		priorityBuf() = priority;
       
  3882 		WriteL(aMessage, 2, priorityBuf);
       
  3883 		}
       
  3884 	aMessage.Complete(err);
       
  3885 	}
       
  3886 	
       
  3887 	
       
  3888 
       
  3889 /**
       
  3890  * RemoveDriveL()
       
  3891  *
       
  3892  * @param RMessage2.
       
  3893  *
       
  3894  * Code changes for PREQ 557.
       
  3895  * Service request EMsvRemoveDriveFromDriveList.
       
  3896  */ 
       
  3897 void CMsvServerSession::RemoveDriveL(const RMessage2 &aMessage)
       
  3898 	{
       
  3899 	TDriveNumber driveNumber = (TDriveNumber) aMessage.Int0();
       
  3900 	TRAPD(err, iMsvServer.RemoveDriveFromListL(driveNumber));
       
  3901 	aMessage.Complete(err);
       
  3902 	}
       
  3903 	
       
  3904 	
       
  3905 
       
  3906 /**
       
  3907  * UpdateDrivePriorityL()
       
  3908  *
       
  3909  * @param RMessage2.
       
  3910  *
       
  3911  * Code changes for PREQ 557.
       
  3912  * Service request EMsvUpdateDrivePriority.
       
  3913  */	 
       
  3914 void CMsvServerSession::UpdateDrivePriorityL(const RMessage2 &aMessage)
       
  3915 	{
       
  3916 	TDriveNumber driveNumber = (TDriveNumber) aMessage.Int0();
       
  3917 	TUint priority = (TUint) aMessage.Int1();
       
  3918 
       
  3919 	TRAPD(err, iMsvServer.UpdateDrivePriorityL(driveNumber, priority));
       
  3920 	if(KErrNone == err)
       
  3921 		{
       
  3922 		TPckgBuf<TUint> priorityBuf;
       
  3923 		priorityBuf() = priority;
       
  3924 		WriteL(aMessage, 2, priorityBuf);
       
  3925 		}
       
  3926 	aMessage.Complete(err);
       
  3927 	}
       
  3928 	
       
  3929 	
       
  3930 /**
       
  3931  * ResetRepositoryL()
       
  3932  *
       
  3933  * @param RMessage2.
       
  3934  *
       
  3935  * Code changes for PREQ 557.
       
  3936  * Service request EMsvResetRepository.
       
  3937  */ 
       
  3938 #if (defined SYMBIAN_MESSAGESTORE_UNIT_TESTCODE)
       
  3939 void CMsvServerSession::ResetRepositoryL(const RMessage2 &aMessage)
       
  3940 	{
       
  3941 	TRAPD(err, iMsvServer.ResetRepositoryL());
       
  3942 	aMessage.Complete(err);
       
  3943 	}
       
  3944 
       
  3945 
       
  3946 void CMsvServerSession::PrintCache(const RMessage2 &aMessage)
       
  3947 	{
       
  3948 	#ifdef _DEBUG
       
  3949 		iMsvServer.Context().IndexAdapter()->PrintL();
       
  3950 	#endif
       
  3951 	aMessage.Complete(KErrNone);
       
  3952 	}
       
  3953 
       
  3954 #endif	  // #if (defined SYMBIAN_MESSAGESTORE_UNIT_TESTCODE)
       
  3955 
       
  3956 #endif		  // #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
  3957 
       
  3958 
       
  3959 
       
  3960 
       
  3961 
       
  3962 
       
  3963 
       
  3964 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
  3965 
       
  3966 /*
       
  3967  * CreateHeaderTableL()
       
  3968  *
       
  3969  * The function creates the header table in the message store.
       
  3970  */
       
  3971 void CMsvServerSession::CreateHeaderTableL(const RMessage2 &aMessage)
       
  3972 	{
       
  3973 	// Check for capability.
       
  3974 	if(!aMessage.HasCapability(ECapabilityWriteDeviceData))
       
  3975 		{
       
  3976 		// Client missing capabilities - emit diagnostics and leave...
       
  3977 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityWriteDeviceData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CreateHeaderTableL")));
       
  3978 		}
       
  3979 
       
  3980 	// Capability check is fine. Proceed.
       
  3981 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  3982 	ReadBufferL(aMessage, 1, iBuffer);
       
  3983 	
       
  3984 	// Read the header structure from the buffer.
       
  3985 	RPointerArray<CFieldPair> fieldDetails;
       
  3986 	CleanupClosePushL(fieldDetails);
       
  3987 	TMsvPackedHeaderStructure headerStruct(iBuffer);
       
  3988 			
       
  3989 	headerStruct.UnpackL(fieldDetails);
       
  3990 	
       
  3991 	// Create the header table and handle error.
       
  3992 	TRAPD(err, iMsvServer.MessageDBAdapter().CreateHeaderTableL(mtmTypeUid, fieldDetails, iLastDBErrorMessage));
       
  3993 	fieldDetails.ResetAndDestroy();
       
  3994 	CleanupStack::PopAndDestroy();		  // fieldDetails
       
  3995 	aMessage.Complete(err);
       
  3996 	}
       
  3997 
       
  3998 
       
  3999 
       
  4000 
       
  4001 
       
  4002 /*
       
  4003  * DoesStoreExistsL()
       
  4004  *
       
  4005  * The function checks if a header table for a MTM Id already exists in message store.
       
  4006  */
       
  4007 void CMsvServerSession::DoesStoreExistsL(const RMessage2 &aMessage)
       
  4008 	{
       
  4009 	// Check for capability.
       
  4010 	if(!aMessage.HasCapability(ECapabilityReadDeviceData))
       
  4011 		{
       
  4012 		// Client missing capabilities - emit diagnostics and leave...
       
  4013 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityReadDeviceData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DoesStoreExistsL")));
       
  4014 		}
       
  4015 
       
  4016 	// Capability check is fine. Proceed.
       
  4017 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  4018 	
       
  4019 	TBool isStoreExists = iMsvServer.MessageDBAdapter().IsHeaderTableExistsL(mtmTypeUid);
       
  4020 
       
  4021 	TPckgBuf<TBool> storeExists;
       
  4022 	storeExists() = isStoreExists;
       
  4023 	WriteL(aMessage, 1, storeExists);
       
  4024 	aMessage.Complete(KErrNone);
       
  4025 	}
       
  4026 	
       
  4027 	
       
  4028 
       
  4029 
       
  4030 
       
  4031 /*
       
  4032  * LastErrorMessageL()
       
  4033  *
       
  4034  * This function is mainly used after the header table creation.
       
  4035  * The function returns the error message text, if the header table
       
  4036  * creation fails.
       
  4037  */ 
       
  4038 void CMsvServerSession::LastErrorMessageL(const RMessage2 &aMessage)
       
  4039 	{
       
  4040 	TInt desLen = iLastDBErrorMessage.Size();   
       
  4041 	if (Align4(desLen+4) > aMessage.GetDesMaxLength(0))
       
  4042 		{
       
  4043 		User::Leave(KErrOverflow);
       
  4044 		}
       
  4045 	
       
  4046 	iBuffer->Des().SetLength(desLen+4);
       
  4047 	TInt* ptr = (TInt*) CONST_CAST(TUint8*, iBuffer->Ptr());
       
  4048 	*ptr++ = desLen;
       
  4049 	Mem::Copy((void*)ptr, iLastDBErrorMessage.Ptr(), desLen);
       
  4050 
       
  4051 	WriteBufferL(aMessage, 0);
       
  4052 	aMessage.Complete(KErrNone);
       
  4053 	}
       
  4054 
       
  4055 
       
  4056 
       
  4057 
       
  4058 
       
  4059 
       
  4060 /*
       
  4061  * CreateHeaderEntryL()
       
  4062  *
       
  4063  * This function creates a new header entry in the database.
       
  4064  */ 
       
  4065 void CMsvServerSession::CreateHeaderEntryL(const RMessage2 &aMessage)
       
  4066 	{
       
  4067 	// Check for capability.
       
  4068 	if(!aMessage.HasCapability(ECapabilityWriteUserData))
       
  4069 		{
       
  4070 		// Client missing capabilities - emit diagnostics and leave...
       
  4071 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityWriteUserData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::CreateHeaderEntryL")));
       
  4072 		}
       
  4073 
       
  4074 	// Capability check is fine. Proceed.
       
  4075 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  4076 	TMsvId entryId = aMessage.Int1();
       
  4077 	
       
  4078 	ReadBufferL(aMessage, 2, iBuffer);
       
  4079 		
       
  4080 	RPointerArray<CHeaderFields> fieldPairList;
       
  4081 	CleanupClosePushL(fieldPairList);
       
  4082 	
       
  4083 	TMsvPackedHeaderData headerData(iBuffer);
       
  4084 	headerData.UnpackL(fieldPairList);
       
  4085 	
       
  4086 	TRAPD(err, iMsvServer.MessageDBAdapter().CreateHeaderEntryL(mtmTypeUid, entryId, fieldPairList));
       
  4087 	fieldPairList.ResetAndDestroy();
       
  4088 	CleanupStack::PopAndDestroy();
       
  4089 	aMessage.Complete(err); 
       
  4090 	}
       
  4091 	
       
  4092 	
       
  4093 	
       
  4094 	
       
  4095 
       
  4096 
       
  4097 /*
       
  4098  * LoadHeaderEntryL()
       
  4099  *
       
  4100  * This function loads the header entry from the database
       
  4101  * and returns to the client.
       
  4102  */ 
       
  4103 void CMsvServerSession::LoadHeaderEntryL(const RMessage2 &aMessage)
       
  4104 	{
       
  4105 	// Check for capability.
       
  4106 	if(!aMessage.HasCapability(ECapabilityReadUserData))
       
  4107 		{
       
  4108 		// Client missing capabilities - emit diagnostics and leave...
       
  4109 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityReadUserData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::LoadHeaderEntryL")));
       
  4110 		}
       
  4111 
       
  4112 	// Capability check is fine. Proceed.
       
  4113 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  4114 	TMsvId entryId = aMessage.Int1();
       
  4115 	
       
  4116 	RPointerArray<CHeaderFields> fieldPairList;
       
  4117 	CleanupClosePushL(fieldPairList);
       
  4118 	
       
  4119 	TRAPD(err, iMsvServer.MessageDBAdapter().LoadHeaderEntryL(mtmTypeUid, entryId, fieldPairList));
       
  4120 	if(KErrNone == err)
       
  4121 		{
       
  4122 		TMsvPackedHeaderData headerData(iBuffer);
       
  4123 		err = headerData.Pack(fieldPairList);
       
  4124 		while (err != KErrNone)
       
  4125 			{
       
  4126 			// increase the size of the buffer and try again
       
  4127 			iBuffer->Des().SetLength(0); // to avoid copying contents
       
  4128 			iBuffer = iBuffer->ReAllocL(iBuffer->Des().MaxSize() + KMsvSessionBufferLength);
       
  4129 			err = headerData.Pack(fieldPairList);
       
  4130 			}
       
  4131 		WriteBufferL(aMessage, 2);
       
  4132 		}
       
  4133 	fieldPairList.ResetAndDestroy();
       
  4134 	CleanupStack::PopAndDestroy();
       
  4135 
       
  4136 	aMessage.Complete(err);
       
  4137 	}
       
  4138 	
       
  4139 	
       
  4140 	
       
  4141 	
       
  4142 	
       
  4143 	
       
  4144 /*
       
  4145  * DeleteHeaderEntryL()
       
  4146  *
       
  4147  * This function deletes the header entry from the database.
       
  4148  */	 
       
  4149 void CMsvServerSession::DeleteHeaderEntryL(const RMessage2 &aMessage)
       
  4150 	{
       
  4151 	// Check for capability.
       
  4152 	if(!aMessage.HasCapability(ECapabilityWriteUserData))
       
  4153 		{
       
  4154 		// Client missing capabilities - emit diagnostics and leave...
       
  4155 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityWriteUserData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::DeleteHeaderEntryL")));
       
  4156 		}
       
  4157 
       
  4158 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  4159 	TMsvId entryId = aMessage.Int1();
       
  4160 	
       
  4161 	TRAPD(err, iMsvServer.MessageDBAdapter().DeleteHeaderEntryL(mtmTypeUid, entryId));
       
  4162 	aMessage.Complete(err);
       
  4163 	}
       
  4164 
       
  4165 
       
  4166 
       
  4167 	
       
  4168 	
       
  4169 	
       
  4170 /*
       
  4171  * UpdateHeaderEntryL()
       
  4172  *
       
  4173  * This function udpates the header entry in the database.
       
  4174  */ 
       
  4175 void CMsvServerSession::UpdateHeaderEntryL(const RMessage2 &aMessage)
       
  4176 	{
       
  4177 	// Check for capability.
       
  4178 	if(!aMessage.HasCapability(ECapabilityWriteUserData))
       
  4179 		{
       
  4180 		// Client missing capabilities - emit diagnostics and leave...
       
  4181 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityWriteUserData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::UpdateHeaderEntryL")));
       
  4182 		}
       
  4183 
       
  4184 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  4185 	TMsvId entryId = aMessage.Int1();
       
  4186 	
       
  4187 	ReadBufferL(aMessage, 2, iBuffer);
       
  4188 		
       
  4189 	RPointerArray<CHeaderFields> fieldPairList;
       
  4190 	CleanupClosePushL(fieldPairList);
       
  4191 	
       
  4192 	TMsvPackedHeaderData headerData(iBuffer);
       
  4193 	headerData.UnpackL(fieldPairList);
       
  4194 	
       
  4195 	TRAPD(err, iMsvServer.MessageDBAdapter().UpdateHeaderEntryL(mtmTypeUid, entryId, fieldPairList));
       
  4196 	fieldPairList.ResetAndDestroy();
       
  4197 	CleanupStack::PopAndDestroy();
       
  4198 	aMessage.Complete(err);
       
  4199 	}
       
  4200 
       
  4201 
       
  4202 
       
  4203 
       
  4204 
       
  4205 
       
  4206 /*
       
  4207  * DoesAnyStoreExists()
       
  4208  *
       
  4209  * This function checks if the store (header/body) exists 
       
  4210  * for a give metadata entry.
       
  4211  */ 
       
  4212 void CMsvServerSession::DoesAnyStoreExists(const RMessage2 &aMessage)
       
  4213 	{
       
  4214 	TMsvId entryId = aMessage.Int0();
       
  4215 	TUid mtmTypeUid = TUid::Uid(aMessage.Int1());
       
  4216 	
       
  4217 	TBool isStoreExists = EFalse;
       
  4218 	TRAPD(err, isStoreExists = iMsvServer.MessageDBAdapter().DoesAnyStoreExistsL(entryId, mtmTypeUid));
       
  4219 	if(err)
       
  4220 		{
       
  4221 		aMessage.Complete(EFalse);
       
  4222 		}
       
  4223 	else
       
  4224 		{
       
  4225 		aMessage.Complete(isStoreExists);
       
  4226 		}
       
  4227 	}
       
  4228 
       
  4229 void CMsvServerSession::DoesHeaderTableExist(const RMessage2 aMessage)
       
  4230 	{
       
  4231 	TUid mtmTypeUid = TUid::Uid(aMessage.Int0());
       
  4232 	
       
  4233 	TBool doesHeaderExists = EFalse;
       
  4234 	TRAPD(err, doesHeaderExists = iMsvServer.MessageDBAdapter().IsHeaderTableExistsL(mtmTypeUid));
       
  4235 	if(err)
       
  4236 		{
       
  4237 		aMessage.Complete(EFalse);
       
  4238 		}
       
  4239 	else
       
  4240 		{
       
  4241 		aMessage.Complete(doesHeaderExists);
       
  4242 		}
       
  4243 	}
       
  4244 
       
  4245 /***************************************Converter API's***************************/
       
  4246 /*
       
  4247  GetConvertibleDriveListL()
       
  4248  Fetches a list of drives with message store unsupported	
       
  4249  
       
  4250  @param aMessage: RMessage2 representing client request and containing request data.
       
  4251  @return None
       
  4252  */
       
  4253 void CMsvServerSession::GetConvertibleDriveListL(const RMessage2 &aMessage)
       
  4254 	{
       
  4255 	// capability check
       
  4256 	if(!aMessage.HasCapability(ECapabilityReadDeviceData))
       
  4257 		{
       
  4258 		// Client missing capabilities - emit diagnostics and leave...
       
  4259 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityReadDeviceData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::GetConvertibleDriveListL")));
       
  4260 		}
       
  4261 	
       
  4262 	
       
  4263 	CMsvPreferredDriveList *driveList = CMsvPreferredDriveList::GetDriveList();
       
  4264 	RArray<TDriveNumber> driveNumList;
       
  4265 	CleanupClosePushL(driveNumList);
       
  4266 	
       
  4267 	for(TInt index=0; index<driveList->Count(); index++)
       
  4268 		{
       
  4269 		if(EMsvMessageStoreNotSupportedStatus == (*driveList)[index].status)
       
  4270 			{
       
  4271 			driveNumList.AppendL((*driveList)[index].driveNum);	 
       
  4272 			}
       
  4273 		}
       
  4274 	
       
  4275 	TMsvPackedDriveIdOperation driveOp(iBuffer);
       
  4276 	User::LeaveIfError(driveOp.Pack(driveNumList));
       
  4277 	WriteBufferL(aMessage, 0);
       
  4278 	
       
  4279 	CleanupStack::PopAndDestroy();	  // driveNumList
       
  4280 	aMessage.Complete(KErrNone);
       
  4281 	}
       
  4282 
       
  4283 /*
       
  4284  ConvertMessageStoreL()
       
  4285  Initiates message store conversion on a drive. if the conversion is active on a drive
       
  4286  than this request is queued and completed later.
       
  4287 
       
  4288  @param aMessage: RMessage2 representing client request and containing request data.
       
  4289  @return None
       
  4290  */
       
  4291 void CMsvServerSession::ConvertMessageStoreL(const RMessage2 &aMessage)
       
  4292 	{
       
  4293 	// capability check
       
  4294 	if(!aMessage.HasCapability(ECapabilityWriteDeviceData))
       
  4295 		{
       
  4296 		// Client missing capabilities - emit diagnostics and leave...
       
  4297 		User::LeaveIfError(PlatSec::CapabilityCheckFail(aMessage, ECapabilityWriteDeviceData, __PLATSEC_DIAGNOSTIC_STRING("Checked by CMsvServerSession::ConvertMessageStoreL")));
       
  4298 		}
       
  4299 	
       
  4300 	/*Instantiate the converter waiter thread. If it is already active, than the existing instance is returned
       
  4301 	  Else a new instance is created and returned. Singleton Pattern   */
       
  4302 	  
       
  4303 	iConverterWaiter=CMsvConverterWaiter::InstanceL(&iMsvServer);
       
  4304 		
       
  4305 	if(!iConverterWaiter->isRunningMessageStoreConverter()) 
       
  4306 		{
       
  4307 		iConverterWaiter->StartMessageStoreConversionL(aMessage,EFalse);
       
  4308 		}
       
  4309 	else 
       
  4310 		{
       
  4311 		// Queue the request if the drive is already not present
       
  4312 		iConverterWaiter->QueueConversionRequestL(aMessage);
       
  4313 		}
       
  4314 	}
       
  4315 
       
  4316 /*
       
  4317  CancelConversionRequestL()
       
  4318  Cancels conversion request for a drive.
       
  4319 
       
  4320  @param aMessage: RMessage2 representing client request and containing request data.
       
  4321  @return None
       
  4322  */
       
  4323 void CMsvServerSession::CancelConversionRequestL(const RMessage2 &aMessage)
       
  4324 	{
       
  4325 	if(iConverterWaiter)
       
  4326 		{
       
  4327 		iConverterWaiter->CancelConversionL(aMessage);
       
  4328 		}
       
  4329 	else
       
  4330 		{
       
  4331 		aMessage.Complete(KErrNotFound);
       
  4332 		}
       
  4333 	}
       
  4334 
       
  4335 /*
       
  4336  GetConversionStatus()
       
  4337  Gets the conversion status for a drive.
       
  4338  
       
  4339  @param aMessage: RMessage2 representing client request and containing request data.
       
  4340  @return None
       
  4341  */ 
       
  4342 void CMsvServerSession::GetConversionStatus(const RMessage2 &aMessage)
       
  4343 	{
       
  4344 	if(iConverterWaiter)
       
  4345 		{
       
  4346 		iConverterWaiter->GetConversionStatus(aMessage);
       
  4347 		}
       
  4348 	else
       
  4349 		{
       
  4350 		aMessage.Complete(KErrNotFound);
       
  4351 		}
       
  4352 	}
       
  4353 
       
  4354 TBool CMsvServerSession::FoundUnSupportedDrives()
       
  4355 	{
       
  4356 	CMsvPreferredDriveList *driveList = CMsvPreferredDriveList::GetDriveList();
       
  4357 	for(TInt index=0; index<driveList->Count(); index++)
       
  4358 		{
       
  4359 		if(EMsvMessageStoreNotSupportedStatus == (*driveList)[index].status)
       
  4360 			{
       
  4361 			return ETrue;
       
  4362 			}
       
  4363 		}
       
  4364 	return EFalse;
       
  4365 	
       
  4366 	}
       
  4367 #endif	  // #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
  4368 
       
  4369 
       
  4370 
       
  4371 
       
  4372 
       
  4373