pimappservices/calendar/client/src/calclient.cpp
changeset 0 f979ecb2b13e
child 19 1984aceb8774
child 45 b6db4fd4947b
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "calclient.h"
       
    17 
       
    18 #include <s32mem.h>
       
    19 
       
    20 #include "agmallocator.h"
       
    21 #include "agmattachment.h"
       
    22 #include "agmentry.h"
       
    23 #include "agmtlsproxy.h"
       
    24 #include "agmtzrules.h"
       
    25 #include "agmrptdef.h"
       
    26 #include "agmcategory.h"
       
    27 #include "agmcontent.h"
       
    28 #include "agmserv.h"
       
    29 #include "agssignal.h"
       
    30 #include "calattachmentmanagerimpl.h"
       
    31 #include "calinstance.h"
       
    32 #include "calentryimpl.h"
       
    33 #include "agmsortcriteria.h"
       
    34 #include "calsessionimpl.h"
       
    35 #include "calcalendarinfoimpl.h"
       
    36 #include "agmfilechangenotification.h"
       
    37 
       
    38 const TUint KDefaultMessageSlots = 3;
       
    39 
       
    40 _LIT(KAgendaMutex, "AgnMutex");
       
    41 
       
    42 //
       
    43 // RAgendaServ
       
    44 //
       
    45 void RAgendaServ::AddSessionL(const CCalSessionImpl& aSession, TInt& aSessionId)
       
    46 	{
       
    47 	__ASSERT_DEBUG(FindSession(aSession) == -1, User::Invariant());
       
    48 	iCalSessions.AppendL(&aSession);
       
    49 	aSessionId = ++iLastSessionId;
       
    50 	}
       
    51 
       
    52 TBool RAgendaServ::RemoveSession(const CCalSessionImpl& aSession)
       
    53 	{
       
    54 	TInt pos = FindSession(aSession);
       
    55 	if (pos != KErrNotFound)
       
    56 	    {
       
    57 	    iCalSessions.Remove(pos);
       
    58 	    }
       
    59 	return (iCalSessions.Count()>0)?ETrue:EFalse;
       
    60 	}
       
    61 
       
    62 TInt RAgendaServ::FindSession(const CCalSessionImpl& aSession)
       
    63 	{
       
    64 	const TInt count = iCalSessions.Count();
       
    65 	for(TInt ii=0; ii<count; ++ii)
       
    66 		{
       
    67 		if(&aSession == iCalSessions[ii])
       
    68 			{
       
    69 			return ii;
       
    70 			}
       
    71 		}
       
    72 	return -1;
       
    73 	}
       
    74 
       
    75 void RAgendaServ::IncreaseRef()
       
    76 	{
       
    77 	iReferenceCount++;
       
    78 	}
       
    79 
       
    80 TInt RAgendaServ::DecreaseRef()
       
    81 	{
       
    82 	iReferenceCount--;
       
    83 	__ASSERT_ALWAYS( iReferenceCount >= 0, User::Invariant());
       
    84 	return iReferenceCount;
       
    85 	}
       
    86 
       
    87 RAgendaServ* RAgendaServ::NewL()
       
    88 /** Constructs a new RAgendaServ, initialising all member data to NULL.
       
    89 
       
    90 @return A pointer to the client interface to the agenda server. */
       
    91 	{
       
    92 	RAgendaServ* self = new (ELeave) RAgendaServ();
       
    93 	
       
    94 	TRAPD(err, self->ConstructL());
       
    95 	if(err != KErrNone)
       
    96 	    {
       
    97 	    delete self;
       
    98 	    User::Leave(err);
       
    99 	    }
       
   100 	
       
   101 	return self;
       
   102 	}
       
   103 
       
   104  // not derived from CBase, so have to set member data to NULL
       
   105 RAgendaServ::RAgendaServ()
       
   106 	: RSessionBase(), iBuffer(NULL),iLastSessionId(0)
       
   107 	{
       
   108 	}
       
   109 
       
   110 void RAgendaServ::ConstructL()
       
   111 	{
       
   112 	iReferenceCount = 0;
       
   113 	iSimpleEntryAllocator = 0;
       
   114 	iSimpleEntryAllocator = new (ELeave) CAgnSimpleEntryAllocator;
       
   115 	CAgnTlsProxy::CreateL(CAgnTlsProxy::TAgnTlsTzRulesType_Client);
       
   116     iBuffer = CBufFlat::NewL(KInitialBufferSize);
       
   117     iBuffer->ExpandL(0, KInitialBufferSize);
       
   118 	}
       
   119 
       
   120 RAgendaServ::~RAgendaServ()
       
   121 /** The destructor frees all resources owned by the server, prior to its destruction. */
       
   122 	{
       
   123 	__ASSERT_ALWAYS( iReferenceCount == 0, User::Invariant());
       
   124 	delete iBuffer;
       
   125 	CAgnTlsProxy::Release(NULL);	
       
   126 	delete iSimpleEntryAllocator;
       
   127 	iCalSessions.Close();
       
   128 	}
       
   129 
       
   130 // Connect to the server
       
   131 
       
   132 //
       
   133 // CAgnServer
       
   134 //
       
   135 
       
   136 // Static function to create a new thread or process, depending upon the
       
   137 // variant. For marm builds, the AGSVEXE.EXE program is run, which 
       
   138 // creates the server objects in a new process
       
   139 
       
   140 TInt RAgendaServ::StartServer()
       
   141 	{
       
   142 	TRequestStatus stat;
       
   143 	TAgnSignal signal(stat);
       
   144 
       
   145 	
       
   146 	RProcess server;
       
   147 	// Locate the server on all drives (Y-A, then Z)
       
   148 
       
   149 	TInt ret = server.Create(KAgendaServerExe, signal.Command(), EOwnerThread);
       
   150 	if (ret!=KErrNone)
       
   151 		{
       
   152 		return ret;
       
   153 		}
       
   154 	server.SetPriority(EPriorityForeground);
       
   155 
       
   156 	server.Rendezvous(stat);
       
   157 	
       
   158 	if (stat!=KRequestPending)
       
   159 		{
       
   160 		server.Kill(0);		// abort startup
       
   161 		}
       
   162 	else
       
   163 		{
       
   164 		server.Resume();	// logon OK - start the server
       
   165 		}
       
   166 
       
   167 	
       
   168 	User::WaitForRequest(stat);
       
   169 	server.Close();
       
   170 	return stat.Int();
       
   171 	}
       
   172 
       
   173 TInt RAgendaServ::Connect()
       
   174 /** Connects the client process to the agenda server. 
       
   175 
       
   176 This function must be called before the server can be used.
       
   177 
       
   178 @return An error code. KErrNone if successful. */
       
   179 	{
       
   180 	RMutex mutex;
       
   181 	TInt ret = mutex.CreateGlobal(KAgendaMutex);
       
   182 	if(ret != KErrNone && ret != KErrAlreadyExists)
       
   183 		{
       
   184 		return ret;
       
   185 		}
       
   186 	ret = mutex.OpenGlobal(KAgendaMutex);	
       
   187 	if (ret != KErrNone && ret != KErrAlreadyExists)
       
   188 		{
       
   189 		return ret;
       
   190 		}
       
   191 	mutex.Wait();
       
   192 	TInt r = DoConnect();
       
   193 	if (r==KErrNotFound || r==KErrServerTerminated)
       
   194 		{
       
   195 		r=StartServer();
       
   196 		if(r==KErrNone || r==KErrAlreadyExists)
       
   197 			{
       
   198 			r=DoConnect();
       
   199 			}
       
   200 		}
       
   201 	mutex.Signal();
       
   202 	mutex.Close();
       
   203 	return r;
       
   204 	}
       
   205 	
       
   206 TInt RAgendaServ::DoConnect()
       
   207 	{
       
   208 	return CreateSession(KAgendaServerName,Version(), KDefaultMessageSlots);
       
   209 	}
       
   210 
       
   211 
       
   212 // Return the client side version no.
       
   213 TVersion RAgendaServ::Version() const
       
   214 	{
       
   215 	return (TVersion(KAgnServerMajorVersion,KAgnServerMinorVersion,KAgnServerBuildVersion));
       
   216 	}
       
   217 
       
   218 
       
   219 
       
   220 //
       
   221 // Server Utility Functions
       
   222 
       
   223 void RAgendaServ::Panic(TAgnClientPanic aPanic) const
       
   224 	{
       
   225 	_LIT(KAgendaClient,"AgendaClient");
       
   226 	User::Panic(KAgendaClient, aPanic);
       
   227 	}
       
   228 
       
   229 /** 
       
   230 Checks the size of the incoming buffer, and if it is larger
       
   231 than the size of the buffer already defined, allocates enough
       
   232 space in aBuffer, and transfers the data from the server
       
   233 
       
   234 @capability ReadUserData
       
   235 */
       
   236 CBufFlat* RAgendaServ::CreateTransmitBufferL(TInt aBufferSize) const
       
   237 	{
       
   238 	CBufFlat* buffer = NULL;
       
   239 	
       
   240 	if (aBufferSize > KInitialBufferSize)
       
   241 		{
       
   242 		// Create a buffer with granularity of aBufferSize
       
   243 		buffer = CBufFlat::NewL(aBufferSize);
       
   244 		CleanupStack::PushL(buffer);
       
   245 
       
   246 		// Extends the size of the bufferr by the required size
       
   247 		buffer->ExpandL(0,aBufferSize);
       
   248 
       
   249 		// Transfer the data from the server
       
   250 		TPtr8 bufptr = buffer->Ptr(0);
       
   251 		TIpcArgs args(&bufptr);
       
   252 		User::LeaveIfError(SendReceive(ETransmitBuffer,args));
       
   253 		CleanupStack::Pop(buffer);
       
   254 		}
       
   255 	return buffer;
       
   256 	}
       
   257 
       
   258 
       
   259 void RAgendaServ::ExternalizeFilterL(const TAgnFilter& aFilter, RWriteStream& aStream) const
       
   260 	{
       
   261 	aFilter.ExternalizeL(aStream);
       
   262 	}
       
   263 
       
   264 
       
   265 CAgnSimpleEntry* RAgendaServ::InternalizeSimpleEntryL(RReadStream& aStream) const
       
   266 	{
       
   267 	CCalEntry::TType entryType = static_cast<CCalEntry::TType>( aStream.ReadUint32L() );
       
   268 		
       
   269 	CAgnSimpleEntry* entry = iSimpleEntryAllocator->CreateSimpleEntryL(entryType);
       
   270 	//to cleanup CAgnSortEntry pointer, if a leave happens during internalize.
       
   271 	TCleanSimpleEntry simpleEntryCleanupItem(*entry, *iSimpleEntryAllocator);
       
   272 	CleanupStack::PushL(TCleanupItem(TCleanSimpleEntry::DestroySimpleEntry,&simpleEntryCleanupItem));
       
   273 	entry->InternalizeL(aStream, ETrue);
       
   274     CleanupStack::Pop(&simpleEntryCleanupItem);
       
   275 	
       
   276 	return ( entry );
       
   277 	}
       
   278 
       
   279 /**
       
   280 @capability None
       
   281 */
       
   282 void RAgendaServ::OpenAgendaL(const CCalSessionImpl& aSession, const TDesC& aFileName, CalCommon::TCalFileVersionSupport& aStatus, TInt64& aFileId, TCalCollectionId& aCallectionId)
       
   283 	{
       
   284     CBufFlat* streamBuffer = CBufFlat::NewL(aFileName.Size()+ sizeof(TUint8));
       
   285     CleanupStack::PushL(streamBuffer);
       
   286     RBufWriteStream bufStream;
       
   287     bufStream.Open(*streamBuffer);
       
   288     CleanupClosePushL(bufStream);
       
   289     bufStream << aFileName;
       
   290     bufStream.WriteInt32L(aSession.SessionId());
       
   291     CleanupStack::PopAndDestroy(&bufStream);
       
   292     TPtr8 readPtr = streamBuffer->Ptr(0);
       
   293     TPckgBuf<CalCommon::TCalFileVersionSupport> statusPckg(aStatus);
       
   294     TPckgBuf<TInt64> fileId(aFileId);
       
   295     TPckgBuf<TCalCollectionId> shortFileId(aCallectionId);
       
   296     TIpcArgs args(&readPtr, &statusPckg, &fileId, &shortFileId);  
       
   297     TInt err = SendReceive(EOpenAgenda,args);
       
   298     CleanupStack::PopAndDestroy(streamBuffer);
       
   299     if(err != KErrAlreadyExists)
       
   300         {
       
   301         aStatus = statusPckg();
       
   302         User::LeaveIfError(err);
       
   303         aFileId = fileId();
       
   304         aCallectionId = shortFileId();
       
   305         }
       
   306 	}
       
   307 	
       
   308 void RAgendaServ::WaitUntilLoadedL(TCalCollectionId aCallectionId)
       
   309 	{
       
   310 	TIpcArgs args(0, aCallectionId);//0 argument means not to report the progess
       
   311 	TRequestStatus status = KRequestPending;
       
   312 	SendReceive(EStartBuildIndex, args, status);
       
   313 	User::WaitForRequest(status);
       
   314 	User::LeaveIfError(status.Int());
       
   315 	}
       
   316 
       
   317 /** 
       
   318 Closes the file that is open in the current agenda server session. 
       
   319 @capability None
       
   320 */
       
   321 void RAgendaServ::CloseAgenda(TInt aSessionId)
       
   322 	{
       
   323 	TIpcArgs args(aSessionId);
       
   324 	SendReceive(ECloseAgenda, args);
       
   325 	}
       
   326 
       
   327 /** 
       
   328 Fetches a complete entry from the server
       
   329 @capability ReadUserData
       
   330 */
       
   331 CAgnEntry* RAgendaServ::FetchEntryL(TAgnEntryId& aId, TInt64 aFileId) const
       
   332 	{
       
   333 	TPckg<TAgnEntryId> entryId(aId);
       
   334 	TPckgBuf<TInt> size;
       
   335 	TPtr8 ptr = iBuffer->Ptr(0);
       
   336 
       
   337 	if (aId.IsNullId())
       
   338 		{
       
   339 		return NULL;
       
   340 		}
       
   341 	TPckgBuf<TInt64> fileId(aFileId);
       
   342 	TIpcArgs args(&ptr, &entryId, &size, &fileId);
       
   343 	User::LeaveIfError(SendReceive(EFetchEntry,args));
       
   344 
       
   345 	return ReadEntryFromServerSessionL(size());	
       
   346 	}
       
   347 
       
   348 /** Fetches a complete entry from the server
       
   349 @capability ReadUserData
       
   350 */
       
   351 CAgnEntry* RAgendaServ::FetchEntryL(TCalLocalUid aId, TInt64 aFileId) const
       
   352 	{
       
   353 	TPckg<TCalLocalUid> entryId(aId);
       
   354 	TPckgBuf<TInt> size;
       
   355 	TPtr8 ptr = iBuffer->Ptr(0);
       
   356 	TPckgBuf<TInt64> fileId(aFileId);
       
   357 	TIpcArgs args(&ptr, &entryId, &size, &fileId);
       
   358 	User::LeaveIfError(SendReceive(EFetchEntryByUID,args));
       
   359 	return ReadEntryFromServerSessionL(size());
       
   360 	}
       
   361 
       
   362 
       
   363 /* Internalize agenda entry from buffer 
       
   364 
       
   365 @capability ReadUserData
       
   366 @param aBufferSize size of buffer 
       
   367 @return agenda entry internalized from buffer
       
   368 */
       
   369 CAgnEntry* RAgendaServ::ReadEntryFromServerSessionL(TInt aBufferSize) const
       
   370 	{
       
   371 	if (aBufferSize==0) // if size = 0, there entry has already been deleted
       
   372 		{
       
   373 		return NULL;
       
   374 		}
       
   375 
       
   376 	RBufReadStream readStream;
       
   377 	// Create a transfer buffer, and set where to read the data from
       
   378 	CBufFlat* buffer = CreateTransmitBufferL(aBufferSize);
       
   379 	if (buffer)
       
   380 		{
       
   381 		readStream.Open(*buffer);
       
   382 		}
       
   383 	else
       
   384 		{
       
   385 		readStream.Open(*iBuffer);
       
   386 		}
       
   387 	CleanupStack::PushL(buffer);
       
   388 
       
   389 	CAgnEntry* entry = CreateEntryFromBufferStreamL(readStream);
       
   390 	readStream.Close();
       
   391 	CleanupStack::PopAndDestroy(buffer);
       
   392 
       
   393 	return entry;
       
   394 	}
       
   395 
       
   396 CAgnEntry* RAgendaServ::CreateEntryFromBufferStreamL(RReadStream& aReadStream) const
       
   397 	{
       
   398 	CCalEntry::TType entryType = (CCalEntry::TType)aReadStream.ReadUint32L();
       
   399 	
       
   400 	CAgnEntry* entry = CAgnEntry::NewL(entryType);
       
   401 	CleanupStack::PushL(entry);
       
   402 	entry->InternalizeFromBufferL(aReadStream);
       
   403 	CleanupStack::Pop(entry);
       
   404 	return entry;
       
   405 	}
       
   406 
       
   407 /** 
       
   408 Get a lite entry from the server
       
   409 (Lite-entries are just time information, with no text, etc)
       
   410 @capability ReadUserData
       
   411 */
       
   412 CAgnSimpleEntry* RAgendaServ::GetSimpleEntryL(const TAgnEntryId& aId, TCalCollectionId aCallectionId) const
       
   413 	{
       
   414 	TPckgBuf<TInt> size;
       
   415 
       
   416 	TPtr8 ptr = iBuffer->Ptr(0);
       
   417 
       
   418 	if (aId.IsNullId())
       
   419 		{
       
   420 		return NULL;
       
   421 		}
       
   422 	CBufFlat* streamBuffer = CBufFlat::NewL(sizeof(TAgnInstance));
       
   423 	CleanupStack::PushL(streamBuffer);
       
   424 	RBufWriteStream bufStream;
       
   425 	bufStream.Open(*streamBuffer);
       
   426 	CleanupClosePushL(bufStream);
       
   427 
       
   428 	bufStream << aId;
       
   429 	CleanupStack::PopAndDestroy(&bufStream);
       
   430 	TPtr8 readPtr = streamBuffer->Ptr(0);
       
   431 	TIpcArgs args(&ptr, &readPtr, &size, aCallectionId);
       
   432 	User::LeaveIfError(SendReceive(EFetchSimpleEntry,args));
       
   433 	CleanupStack::PopAndDestroy(streamBuffer);
       
   434 	if (size() == 0)
       
   435 		{
       
   436 		User::Leave(KErrNotFound);
       
   437 		}
       
   438 
       
   439 	RBufReadStream readStream;
       
   440 
       
   441 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
   442 	if (buffer)
       
   443 		{
       
   444 		readStream.Open(*buffer);
       
   445 		}
       
   446 	else
       
   447 		{
       
   448 		readStream.Open(*iBuffer);
       
   449 		}
       
   450 
       
   451 	CleanupStack::PushL(buffer);
       
   452 
       
   453 	CAgnSimpleEntry* entry = NULL;
       
   454 
       
   455 	if (readStream.ReadUint8L())
       
   456 		{
       
   457 		entry = InternalizeSimpleEntryL(readStream);
       
   458 		entry->SetCollectionId(aCallectionId);
       
   459 		}
       
   460 	
       
   461 	readStream.Close();
       
   462 	
       
   463 	CleanupStack::PopAndDestroy(buffer);
       
   464 	
       
   465 	// Don't leave here for a NULL entry could be a race with an entry delete
       
   466 	return entry;
       
   467 	}
       
   468 
       
   469 /** 
       
   470 Get a lite entry from the server
       
   471 (Lite-entries are just time information, with no text, etc)
       
   472 @capability ReadUserData
       
   473 */
       
   474 CAgnSimpleEntry* RAgendaServ::GetSimpleEntryL(TCalCollectionId aCallectionId, TUint32 aId) const
       
   475 	{
       
   476 //	TPckg<TAgnEntryId> entryId(aId);
       
   477 	TPckgBuf<TInt> size;
       
   478 
       
   479 	TPtr8 ptr = iBuffer->Ptr(0);
       
   480 	if (aId == 0)
       
   481 		{
       
   482 		return NULL;
       
   483 		}
       
   484 	CBufFlat* streamBuffer = CBufFlat::NewL(sizeof(TUint8)+sizeof(TUint32));
       
   485 	CleanupStack::PushL(streamBuffer);
       
   486 	RBufWriteStream bufStream;
       
   487 	bufStream.Open(*streamBuffer);
       
   488 	CleanupClosePushL(bufStream);
       
   489 
       
   490 	bufStream << aId;
       
   491 	CleanupStack::PopAndDestroy(&bufStream);
       
   492 	TPtr8 readPtr = streamBuffer->Ptr(0);
       
   493 	TIpcArgs args(&ptr, &readPtr, &size, aCallectionId);
       
   494 	User::LeaveIfError(SendReceive(EFetchSimpleEntryByUID,args));
       
   495 	CleanupStack::PopAndDestroy(streamBuffer);
       
   496 	if (size() == 0)
       
   497 		{
       
   498 		User::Leave(KErrNotFound);
       
   499 		}
       
   500 
       
   501 	RBufReadStream readStream;
       
   502 
       
   503 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
   504 	if (buffer)
       
   505 		{
       
   506 		readStream.Open(*buffer);
       
   507 		}
       
   508 	else
       
   509 		{
       
   510 		readStream.Open(*iBuffer);
       
   511 		}
       
   512 
       
   513 	CleanupStack::PushL(buffer);
       
   514 
       
   515 	CAgnSimpleEntry* entry = NULL;
       
   516 
       
   517 	if (readStream.ReadUint8L())
       
   518 		{
       
   519 		entry = InternalizeSimpleEntryL(readStream);
       
   520 		entry->SetCollectionId(aCallectionId);
       
   521 		}
       
   522 	
       
   523 	readStream.Close();
       
   524 	
       
   525 	CleanupStack::PopAndDestroy(buffer);
       
   526 	
       
   527 	// Don't leave here for a NULL entry could be a race with an entry delete
       
   528 	return entry;
       
   529 	}
       
   530 	
       
   531 /** Get an array of lite entries from the server
       
   532 
       
   533 On return the instance ids and lite entries will have corresponding indices in the arrays 
       
   534 (Lite-entries are just time information, with no text, etc)
       
   535 
       
   536 @param aIds The instance ids of the lite entries to fetch
       
   537 @param aLiteEntries on return will contain the fetched lite entries.
       
   538 
       
   539 @capability ReadUserData
       
   540 */
       
   541 void RAgendaServ::GetSimpleEntryL(const RArray<TAgnInstance>& aEntryIds, RPointerArray<CAgnSimpleEntry>& aLiteEntries) const
       
   542 	{
       
   543 	// only fetches KMaxNumToFetch at a time from the server
       
   544 	const TInt KMaxNumToFetch(32);
       
   545 	const TInt KBufferSize = (KMaxNumToFetch + 1) * sizeof(TUint32); // enough room to write KNumToFetch entries plus a count
       
   546 	
       
   547 	const TInt KEntryIdCount(aEntryIds.Count());
       
   548 	for (TInt entriesFetchedSoFar(0) ; entriesFetchedSoFar < KEntryIdCount ;)
       
   549 		{
       
   550 		CBufFlat* instanceIdBuffer = CBufFlat::NewL(KBufferSize);
       
   551 		CleanupStack::PushL(instanceIdBuffer);
       
   552 		RBufWriteStream bufStream;
       
   553 		bufStream.Open(*instanceIdBuffer);
       
   554 			
       
   555 		// Externalize up to KMaxNumToFetch instance ids
       
   556 		// KNumToFetch will be the total number of fetched TAgnLiteEntrys fetched this time
       
   557 		const TInt KNumToFetch = ((KEntryIdCount - entriesFetchedSoFar) < KMaxNumToFetch) ? KEntryIdCount : entriesFetchedSoFar + KMaxNumToFetch;
       
   558 		bufStream.WriteUint32L(KNumToFetch - entriesFetchedSoFar);
       
   559 		for (; entriesFetchedSoFar < KNumToFetch ; ++entriesFetchedSoFar)
       
   560 			{
       
   561 			bufStream << aEntryIds[entriesFetchedSoFar];
       
   562 			}
       
   563 			
       
   564 		bufStream.CommitL();
       
   565 		bufStream.Close();
       
   566 		TPtr8 entryIds = instanceIdBuffer->Ptr(0);
       
   567 		
       
   568 		TPckgBuf<TInt> size;
       
   569 		TPtr8 ptr = iBuffer->Ptr(0);
       
   570 		
       
   571 		// call the server
       
   572 		TIpcArgs args(&ptr, &entryIds, &size);
       
   573 		User::LeaveIfError(SendReceive(EFetchSimpleEntries, args));
       
   574 			
       
   575 		// open the read stream
       
   576 		RBufReadStream readStream;
       
   577 		CBufFlat* liteEntryBuffer = CreateTransmitBufferL(size());
       
   578 		if (liteEntryBuffer)
       
   579 			{
       
   580 			readStream.Open(*liteEntryBuffer);
       
   581 			}
       
   582 		else
       
   583 			{
       
   584 			readStream.Open(*iBuffer);
       
   585 			}
       
   586 		CleanupStack::PushL(liteEntryBuffer);
       
   587 
       
   588 		// Read the lite entries from the buffer 
       
   589 		const TCalCollectionId KLiteEntryCount(readStream.ReadUint8L());
       
   590 		for (TInt i(0) ; i < KLiteEntryCount ; ++i)
       
   591 			{
       
   592 			if (readStream.ReadUint8L())
       
   593 				{
       
   594 				CAgnSimpleEntry* ptr = InternalizeSimpleEntryL(readStream);
       
   595 				ptr->SetCollectionId(readStream.ReadUint8L());
       
   596 				TInt err = aLiteEntries.Append(ptr);
       
   597 				if (err != KErrNone)
       
   598 					{
       
   599 					iSimpleEntryAllocator->Delete(ptr);
       
   600 					User::Leave(err);
       
   601 					}
       
   602 				}
       
   603 			}
       
   604 		
       
   605 		readStream.Close();
       
   606 		
       
   607 		CleanupStack::PopAndDestroy(liteEntryBuffer);
       
   608 		CleanupStack::PopAndDestroy(instanceIdBuffer);
       
   609 		}
       
   610 	}
       
   611 
       
   612 /** Get an array of lite entries from the server
       
   613 
       
   614 On return the instance ids and lite entries will have corresponding indices in the arrays 
       
   615 (Lite-entries are just time information, with no text, etc)
       
   616 
       
   617 @param aUid The UID of the lite entries to fetch
       
   618 @param aLiteEntries on return will contain the fetched lite entries.
       
   619 
       
   620 @capability ReadUserData
       
   621 */
       
   622 void RAgendaServ::FetchSimpleEntriesByGuidL(const TDesC8& aUid, RPointerArray<CAgnSimpleEntry>& aLiteEntries, RArray<TInt> aFileIds) const
       
   623 	{
       
   624 	const TInt countFile = aFileIds.Count();
       
   625 	const TInt lenUid = aUid.Length();
       
   626 	
       
   627 	CBufFlat* inBuffer = CBufFlat::NewL(sizeof(countFile*8) + lenUid);
       
   628 	CleanupStack::PushL(inBuffer);
       
   629 	RBufWriteStream bufStream;
       
   630 	CleanupClosePushL(bufStream);
       
   631 	bufStream.Open(*inBuffer);
       
   632 	bufStream.WriteInt16L(lenUid);
       
   633 	bufStream.WriteL(aUid, lenUid);
       
   634 	bufStream.WriteInt16L(countFile);
       
   635 	for(TInt ii=0; ii<countFile;++ii)
       
   636 		{
       
   637 		bufStream.WriteUint8L(aFileIds[ii]);
       
   638 		}
       
   639 	CleanupStack::PopAndDestroy(&bufStream);
       
   640 	
       
   641 	TPtr8 paramsPtr = inBuffer->Ptr(0);
       
   642 
       
   643 	TPckgBuf<TInt> size(0);
       
   644 	TPtr8 ptr = iBuffer->Ptr(0);
       
   645 	TIpcArgs args(&ptr, &size, &paramsPtr);
       
   646 	User::LeaveIfError(SendReceive(EFetchSimpleEntriesByGuid, args));
       
   647 	CleanupStack::PopAndDestroy(inBuffer);
       
   648 	// open the read stream
       
   649 	RBufReadStream readStream;
       
   650 	CBufFlat* liteEntryBuffer = CreateTransmitBufferL(size());
       
   651 	if (liteEntryBuffer)
       
   652 		{
       
   653 		readStream.Open(*liteEntryBuffer);
       
   654 		}
       
   655 	else
       
   656 		{
       
   657 		readStream.Open(*iBuffer);
       
   658 		}
       
   659 	CleanupStack::PushL(liteEntryBuffer);
       
   660 
       
   661 	// Read the lite entries from the buffer 
       
   662 	const TUint32 KLiteEntryCount(readStream.ReadUint32L());
       
   663 	for (TInt i(0) ; i < KLiteEntryCount ; ++i)
       
   664 		{
       
   665 		CAgnSimpleEntry* ptr = InternalizeSimpleEntryL(readStream);
       
   666 		ptr->SetCollectionId(readStream.ReadUint8L());
       
   667 		TInt err = aLiteEntries.Append(ptr);
       
   668 		if (err != KErrNone)
       
   669 			{
       
   670 			iSimpleEntryAllocator->Delete(ptr);
       
   671 			User::Leave(err);
       
   672 			}
       
   673 		}
       
   674 	
       
   675 	readStream.Close();
       
   676 	
       
   677 	CleanupStack::PopAndDestroy(liteEntryBuffer);
       
   678 	}
       
   679 
       
   680 /** Updates an existing entry on the server
       
   681 @param aEntry
       
   682 @param aTodoPosition
       
   683 @capability WriteUserData
       
   684 @capability ReadUserData
       
   685 @leave
       
   686 @internalComponent
       
   687 */
       
   688 void RAgendaServ::UpdateEntryL(CAgnEntry& aEntry, TBool aDeleteChildren, TCalCollectionId aCallectionId)
       
   689 	{
       
   690 	/*
       
   691 	Check that either (or both) the instance or the unique id of the entry
       
   692 	passed is not null, otherwise, the update will not occur because the server
       
   693 	will not know which of its entries it should update.
       
   694 	*/
       
   695 	__ASSERT_ALWAYS( !(aEntry.EntryId().IsNullId() && aEntry.LocalUid() == 0 && aCallectionId == NULL), User::Leave(KErrNotFound));
       
   696 	
       
   697 	aEntry.VerifyBeforeStoreL();
       
   698 	
       
   699 	RBufWriteStream bufStream;
       
   700 
       
   701 	bufStream.Open(*iBuffer);
       
   702 
       
   703 	bufStream.WriteUint32L(aEntry.Type());
       
   704 	bufStream.WriteUint8L(aDeleteChildren);
       
   705 
       
   706 	// Set regardless.
       
   707 	aEntry.SetLastModifiedDate();
       
   708 
       
   709 	// Externalize the entry details to the buffer
       
   710 	// Any rich text components will be stored in the textStream buffer
       
   711 	aEntry.ExternalizeToBufferL(bufStream);
       
   712 	bufStream.CommitL();
       
   713 	bufStream.Close();
       
   714 
       
   715 	// Get the sizes of the buffers
       
   716 	TInt size(iBuffer->Size());
       
   717 	
       
   718 	// Get the pointer of the buffers
       
   719 	TPtr8 ptr = iBuffer->Ptr(0);
       
   720 	
       
   721 	// The entry id	
       
   722 	TPckgBuf<TAgnEntryId> entryId;
       
   723 	TIpcArgs args(size, &ptr, &entryId, aCallectionId);
       
   724 	
       
   725 	User::LeaveIfError(SendReceive(EUpdateEntry, args));	
       
   726 	aEntry.SetEntryId(entryId());	
       
   727 	}
       
   728 
       
   729 /** 
       
   730 Add a new enty to the server
       
   731 @capability WriteUserData
       
   732 @capability ReadUserData
       
   733 */
       
   734 TAgnEntryId RAgendaServ::AddEntryL(CAgnEntry& aEntry, TInt64 aFileId)
       
   735 	{
       
   736 	aEntry.VerifyBeforeStoreL();
       
   737 	
       
   738 	RBufWriteStream bufStream;
       
   739 
       
   740 	bufStream.Open(*iBuffer);
       
   741 	
       
   742 	bufStream.WriteUint32L(aEntry.Type());
       
   743 
       
   744 	aEntry.ExternalizeToBufferL(bufStream);
       
   745 
       
   746 	bufStream.CommitL();
       
   747 	bufStream.Close();
       
   748 
       
   749 	// Get the sizes of the buffers
       
   750 	TInt size = iBuffer->Size();
       
   751 	
       
   752 	// Get the pointer of the buffers
       
   753 	TPtr8 ptr = iBuffer->Ptr(0);
       
   754 	
       
   755 	// The entry Id and unique Id
       
   756 	TPckgBuf<TAgnEntryParameters> entryParams;
       
   757 	TPckgBuf<TInt64> fileId(aFileId);
       
   758 	TIpcArgs args(size, &ptr, &entryParams, &fileId);	
       
   759 	
       
   760 	User::LeaveIfError(SendReceive(EAddEntry, args));
       
   761 	
       
   762 	aEntry.SetEntryId(entryParams().iEntryId);
       
   763 	aEntry.SetLocalUid(entryParams().iLocalId);
       
   764 	if (entryParams().iRecurrenceId != Time::NullTTime())
       
   765 		{
       
   766 		TAgnCalendarTime agnRecId;
       
   767 		if (aEntry.TimeMode() == MAgnCalendarTimeMode::EFloating)
       
   768 			{
       
   769 			agnRecId.SetFloatingL(entryParams().iRecurrenceId);
       
   770 			}
       
   771 		else
       
   772 			{
       
   773 			agnRecId.SetLocalL(entryParams().iRecurrenceId);
       
   774 			}
       
   775 		aEntry.UpdateRecurrenceIdL(agnRecId);
       
   776 		}
       
   777 	aEntry.SetLastModifiedDateUtc(entryParams().iLastModifiedDateUtc);
       
   778 	if (aEntry.TimeMode() != MAgnCalendarTimeMode::EFloating &&	aEntry.RptDef() != NULL	)
       
   779 		{
       
   780 		CAgnTzRules* tzRules = aEntry.RptDef()->AgnTzRules();
       
   781 		if(tzRules != NULL)
       
   782 			{
       
   783 			tzRules->SetTzZoneStreamId(entryParams().iTzStreamId);
       
   784 			tzRules->SetSystemTzRule(entryParams().iSystemTzRule);
       
   785 			}
       
   786 		}
       
   787 	
       
   788 
       
   789 	SendFileAttachmentHandlesL(aEntry, aFileId);
       
   790 
       
   791 	return entryParams().iEntryId;
       
   792 	}
       
   793 
       
   794 /**
       
   795 For all attachments on this entry, find out whether they are file attachments.
       
   796 If it contains a file handle, then it must be transferred to the server through a separate IPC call (TransferFileHandleToServerL).
       
   797 If it contains binary data, then a file handle must be fetched from the server, to which the binary data 
       
   798 will be written directly (WriteBinaryDataToNewAttachmentFileL).
       
   799 */
       
   800 void RAgendaServ::SendFileAttachmentHandlesL(CAgnEntry& aEntry, TInt64 aFileId)
       
   801 	{
       
   802 	const TInt KAttachmentCount = aEntry.AttachmentCount();
       
   803 	for (TUint32 i = 0; i < KAttachmentCount; i++)
       
   804 		{
       
   805 		CAgnAttachment& attach = aEntry.Attachment(i);
       
   806 		if (attach.Type() == CCalContent::EDispositionInline)
       
   807 			{
       
   808 			// This is a file attachment
       
   809 			CAgnAttachmentFile* fileAttach = static_cast<CAgnAttachmentFile*>(&attach);
       
   810 			if (fileAttach->IsFileHandleSet())
       
   811 				{
       
   812 				// This is a file attachment with a file handle
       
   813 				TransferFileHandleToServerL(aEntry.LocalUid(), i, *fileAttach, aFileId);
       
   814 				}
       
   815 			else if (fileAttach->Uid() == 0 && fileAttach->Content().Length() > 0)
       
   816 				{
       
   817 				// This is a new file attachment (uid==0) with binary data
       
   818 				WriteBinaryDataToNewAttachmentFileL(aEntry, i, aFileId);
       
   819 				}
       
   820 			}
       
   821 		}
       
   822 	}
       
   823 
       
   824 /**
       
   825 Store a new attachment with a file handle. The file handle is transferred to the Calendar server, which moves the file to 
       
   826 the Calendar store.
       
   827 On the server-side, this will also update the entry containing this attachment with the new attachment metadata.
       
   828 @capability WriteUserData
       
   829 */
       
   830 void RAgendaServ::TransferFileHandleToServerL(TCalLocalUid aLocalUid, TInt aAttachmentIndex, CAgnAttachmentFile& aFileAttachment, TInt64 aFileId)
       
   831 	{
       
   832 	TAttachmentData attachData;
       
   833 	attachData.iLocalUid = aLocalUid;
       
   834 	attachData.iAttachmentIndex = aAttachmentIndex;
       
   835 	TPckg<TCalAttachmentUid> attachUid(0);
       
   836 	
       
   837 	CBufFlat* paramsBuffer = CBufFlat::NewL(sizeof(TInt64) + sizeof(TAttachmentData));
       
   838 	CleanupStack::PushL(paramsBuffer);
       
   839 	RBufWriteStream bufStream;
       
   840 	CleanupClosePushL(bufStream);
       
   841 	bufStream.Open(*paramsBuffer);
       
   842 	bufStream.WriteUint32L(attachData.iLocalUid);
       
   843 	bufStream.WriteInt16L(attachData.iAttachmentIndex);
       
   844 	bufStream << aFileId;
       
   845 	CleanupStack::PopAndDestroy(&bufStream);
       
   846 	 	
       
   847 	TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
   848 	TIpcArgs args(&paramsPtr, &attachUid);
       
   849 	// Detach file handle from attachment, if TransferToServer or SendReceive(ETransferAttachmentFileToServer) fail,
       
   850 	// then the fileHandle must not be deleted.
       
   851 	RFile& fileHandle = const_cast<RFile&>(aFileAttachment.FileHandle()); 
       
   852 	CleanupClosePushL(fileHandle);
       
   853 
       
   854 	// note that the following line will panic if the RFs that contains 
       
   855 	// this file handle is not shared by calling RFs::ShareProtected()
       
   856 	User::LeaveIfError(aFileAttachment.FileHandle().TransferToServer(args, KTransferAttachmentToSrvFsArgIndex, KTransferAttachmentToSrvFileArgIndex));
       
   857 	User::LeaveIfError(SendReceive(ETransferAttachmentFileToServer, args));//replace the file in the same drive
       
   858 	CleanupStack::PopAndDestroy(&fileHandle);
       
   859 	if(attachUid() == 0)
       
   860 		{//When the Drive is different from the default one, the server adopt the handle and will move the attachment file after the file handle is closed.
       
   861 		User::LeaveIfError(SendReceive(EMoveFileToServer, args));
       
   862 		}
       
   863 	CleanupStack::PopAndDestroy(paramsBuffer);
       
   864 	aFileAttachment.SetHasFileHandle(EFalse);
       
   865 	aFileAttachment.SetUid(attachUid());
       
   866 	}
       
   867 
       
   868 /**
       
   869 Store a new attachment from binary data. A new file handle is created on the Calendar server, and this is transferred
       
   870 to the client side so that the binary data can be written directly.
       
   871 On the server-side, this will also update the entry containing this attachment with the new attachment metadata.
       
   872 @capability WriteUserData
       
   873 */
       
   874 void RAgendaServ::WriteBinaryDataToNewAttachmentFileL(CAgnEntry& aEntry, TInt aAttachmentIndex, TInt64 aFileId)
       
   875 	{
       
   876 	CAgnAttachmentFile* attachment = static_cast<CAgnAttachmentFile*>(&aEntry.Attachment(aAttachmentIndex));
       
   877 
       
   878 	// package up data required to identify this attachment on server side.
       
   879 	TAttachmentData data;
       
   880 	data.iLocalUid = aEntry.LocalUid();
       
   881 	data.iAttachmentIndex = aAttachmentIndex;
       
   882 	if (attachment->FileName().Length() > 0)
       
   883 		{
       
   884 		data.iDrive = attachment->FileName().Left(1);
       
   885 		}
       
   886 	else
       
   887 		{
       
   888 		data.iDrive = KNullDesC;
       
   889 		}
       
   890 
       
   891 	TPckg<TAttachmentData> pckgData(data);
       
   892 	TPckg<TInt> pckgSubSessionHandle(0);  // initialise	
       
   893 	TPckgBuf<TInt64> fileId(aFileId);
       
   894 	
       
   895 	TIpcArgs args(&pckgData, &pckgSubSessionHandle, &fileId);
       
   896 	
       
   897 	TInt dataSize = attachment->Content().Length();
       
   898 	args.Set(3, dataSize);
       
   899 
       
   900 	// call server side to create new file and update entry
       
   901 	const TInt KSessionHandle = SendReceive(ETransferFileToClientToWrite, args);
       
   902 	User::LeaveIfError(KSessionHandle);
       
   903 
       
   904 	// fetch file handle and write binary data to it
       
   905 	RFile newFileHandle;
       
   906 	CleanupClosePushL(newFileHandle);
       
   907 	User::LeaveIfError(newFileHandle.AdoptFromServer(KSessionHandle, pckgSubSessionHandle()));
       
   908 	User::LeaveIfError(newFileHandle.Write(attachment->Content()));
       
   909 	CleanupStack::PopAndDestroy(&newFileHandle); // newFileHandle.Close();
       
   910 	}
       
   911 	
       
   912 /**
       
   913 Fetch file handle for a file attachment.
       
   914 @capability ReadUserData
       
   915 */
       
   916 void RAgendaServ::FetchFileHandleL(RFile& aFileHandle, TCalAttachmentUid aAttachmentUid, TInt64 aFileId)
       
   917 	{
       
   918 	TPckg<TInt> pckgSubSessionHandle(0);  // initialise
       
   919 	TPckgBuf<TInt64> fileId(aFileId);
       
   920 	TIpcArgs args(aAttachmentUid, &pckgSubSessionHandle, &fileId);
       
   921 
       
   922 	TInt sessionHandle = SendReceive(ETransferAttachmentFileToClient, args);
       
   923 	User::LeaveIfError(sessionHandle);
       
   924 	User::LeaveIfError(aFileHandle.AdoptFromServer(sessionHandle, pckgSubSessionHandle()));
       
   925 	}
       
   926 
       
   927 /**
       
   928 Fetch all file attachments in the order specified and add them to the iterator.
       
   929 @capability ReadUserData
       
   930 */
       
   931 void RAgendaServ::FetchAttachmentsL(CCalAttachmentIteratorImpl& aIteratorImpl, CCalAttachmentManager::TSortOrder aSortOrder, TInt64 aFileId)
       
   932 	{
       
   933 	TPtr8 bufptr = iBuffer->Ptr(0);
       
   934 	TPckg<TInt> size(0);
       
   935 	TPckgBuf<TInt64> fileId(aFileId);
       
   936 	// make server request
       
   937 	TIpcArgs args(&bufptr, &size, aSortOrder, &fileId);
       
   938 	User::LeaveIfError(SendReceive(EFetchSortedAttachments, args));
       
   939 	
       
   940 	RBufReadStream readStream;
       
   941 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
   942 	if (buffer)
       
   943 		{
       
   944 		readStream.Open(*buffer);
       
   945 		}
       
   946 	else
       
   947 		{
       
   948 		readStream.Open(*iBuffer);
       
   949 		}
       
   950 	
       
   951 	CleanupStack::PushL(buffer);
       
   952 	// internalize array from readStream
       
   953 	const TInt KCount = readStream.ReadUint32L();
       
   954 
       
   955 	TCalAttachmentUid attachUid;
       
   956 	for (TInt i = 0; i < KCount; ++i)
       
   957 		{
       
   958 		readStream >> attachUid;
       
   959 		aIteratorImpl.AppendL(attachUid);
       
   960 		}
       
   961 	
       
   962 	readStream.Close();
       
   963 	CleanupStack::PopAndDestroy(buffer);
       
   964 	}
       
   965 
       
   966 /**
       
   967 Fetch local UIDs of all entries that reference the attachment specified.
       
   968 @capability ReadUserData
       
   969 */
       
   970 void RAgendaServ::EntriesReferencingFileAttachmentL(RArray<TCalLocalUid>& aUids, const CAgnAttachment& aAttachment, TInt64 aFileId)
       
   971 	{
       
   972 	if (aAttachment.Uid() && aAttachment.Type() == CCalContent::EDispositionInline)
       
   973 		{
       
   974 		TPtr8 bufptr = iBuffer->Ptr(0);
       
   975 		TPckg<TInt> size(0);
       
   976 		TPckgBuf<TInt64> fileId(aFileId);
       
   977 		TIpcArgs args(&bufptr, &size, aAttachment.Uid(), &fileId);
       
   978 		User::LeaveIfError(SendReceive(EEntriesWithAttachment, args));
       
   979 		
       
   980 		RBufReadStream readStream;
       
   981 		CBufFlat* buffer = CreateTransmitBufferL(size());
       
   982 		if (buffer)
       
   983 			{
       
   984 			readStream.Open(*buffer);
       
   985 			}
       
   986 		else
       
   987 			{
       
   988 			readStream.Open(*iBuffer);
       
   989 			}
       
   990 		CleanupStack::PushL(buffer);
       
   991 
       
   992 		// internalize local UIDs from readStream
       
   993 		const TInt KCount = readStream.ReadUint32L();
       
   994 		TCalLocalUid id;
       
   995 		for (TInt i = 0; i < KCount; ++i)
       
   996 			{
       
   997 			readStream >> id;
       
   998 			aUids.AppendL(id);
       
   999 			}
       
  1000 
       
  1001 		readStream.Close();
       
  1002 		CleanupStack::PopAndDestroy(buffer);
       
  1003 		}
       
  1004 	}
       
  1005 
       
  1006 /**
       
  1007 Fetch an attachment by its attachment UID. Note attachment UIDs are not exposed to clients, and are just used internally.
       
  1008 @capability ReadUserData
       
  1009 */
       
  1010 CAgnAttachment* RAgendaServ::FetchAttachmentByIdL(TCalAttachmentUid aAttachUid, TInt64 aFileId)
       
  1011 	{
       
  1012 	if (aAttachUid == 0)
       
  1013 		{
       
  1014 		return NULL;
       
  1015 		}
       
  1016 	
       
  1017 	TPckgBuf<TInt> size(0);
       
  1018 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1019 
       
  1020 	TPckgBuf<TInt64> fileId(aFileId);
       
  1021 	TIpcArgs args(&ptr, &size, aAttachUid, &fileId);
       
  1022 	User::LeaveIfError(SendReceive(EFetchAttachmentById, args));
       
  1023 
       
  1024 	if (size() == 0) // if size = 0, there entry has already been deleted
       
  1025 		{
       
  1026 		return NULL;
       
  1027 		}
       
  1028 
       
  1029 	RBufReadStream readStream;
       
  1030 	// Create a transfer buffer, and set where to read the data from
       
  1031 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1032 	if (buffer)
       
  1033 		{
       
  1034 		readStream.Open(*buffer);
       
  1035 		}
       
  1036 	else
       
  1037 		{
       
  1038 		readStream.Open(*iBuffer);
       
  1039 		}
       
  1040 	CleanupStack::PushL(buffer);
       
  1041 
       
  1042 	CAgnAttachment* attachment = AttachmentFactory::NewAttachmentL(readStream);
       
  1043 
       
  1044 	readStream.Close();
       
  1045 	
       
  1046 	CleanupStack::PopAndDestroy(buffer);
       
  1047 	return attachment;
       
  1048 	}
       
  1049 
       
  1050 /** Finds previous instances in the calendar store
       
  1051 
       
  1052 @param aParams The filter settings to use when fidning the previous instances
       
  1053 @param aInstances The previous instances.  Note that the instances will be inserted at the begining of the array
       
  1054  
       
  1055 @capability ReadUserData
       
  1056 */
       
  1057 void RAgendaServ::PreviousInstancesL(const RArray<TInt64> aFileIds, CArrayFix<TAgnInstance>& aInstances, TFindInstanceParams& aParams) const
       
  1058 	{
       
  1059 	const TInt count = aFileIds.Count();
       
  1060 	CBufFlat* paramsBuffer = CBufFlat::NewL(sizeof(TFindInstanceParams) + count*64);
       
  1061 	CleanupStack::PushL(paramsBuffer);
       
  1062 	RBufWriteStream bufStream;
       
  1063 	CleanupClosePushL(bufStream);
       
  1064 	bufStream.Open(*paramsBuffer);
       
  1065 	aParams.ExternalizeL(bufStream);
       
  1066 	bufStream.WriteInt16L(count);
       
  1067 	for(TInt ii=0; ii<count;++ii)
       
  1068 		{
       
  1069 		bufStream << aFileIds[ii];
       
  1070 		}
       
  1071 	CleanupStack::PopAndDestroy(&bufStream);
       
  1072 	
       
  1073 	TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
  1074 	TInt paramsSize = paramsBuffer->Size();
       
  1075 	
       
  1076 	TPckgBuf<TInt> size(0);
       
  1077 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1078 	
       
  1079 	TIpcArgs args(&ptr, &size, &paramsPtr, paramsSize);
       
  1080 	
       
  1081 	User::LeaveIfError(SendReceive(EPreviousInstances, args));
       
  1082 	
       
  1083 	CleanupStack::PopAndDestroy(paramsBuffer);
       
  1084 	
       
  1085 	RBufReadStream readStream;
       
  1086 	CleanupClosePushL(readStream);
       
  1087 	
       
  1088 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1089 	if (buffer)
       
  1090 		{
       
  1091 		readStream.Open(*buffer);
       
  1092 		CleanupStack::PushL(buffer);
       
  1093 		}
       
  1094 	else
       
  1095 		{
       
  1096 		readStream.Open(*iBuffer);
       
  1097 		}
       
  1098 	
       
  1099 	const TInt KCount(readStream.ReadUint32L());
       
  1100 
       
  1101 	for (TInt i(0) ; i < KCount; ++i)
       
  1102 		{
       
  1103 		TAgnInstance instance;
       
  1104 		readStream >> instance;
       
  1105 		aInstances.InsertL(i,instance);
       
  1106 		}
       
  1107 
       
  1108 	if (buffer)
       
  1109 		{
       
  1110 		CleanupStack::PopAndDestroy(buffer);
       
  1111 		}
       
  1112 	CleanupStack::PopAndDestroy(&readStream);
       
  1113 	}
       
  1114 
       
  1115 /** Finds the next instances in the calendar store
       
  1116 
       
  1117 @param aParams The filter settings to use when finding the next instances
       
  1118 @param aInstances The next instances.  Note that the instances will be appended to the end of the array
       
  1119  
       
  1120 @capability ReadUserData
       
  1121 */
       
  1122 void RAgendaServ::NextInstancesL(const RArray<TInt64> aFileIds, CArrayFix<TAgnInstance>& aInstances, const TFindInstanceParams& aParams) const
       
  1123 	{
       
  1124 	// Write the find instance setting out to the buffer
       
  1125 	
       
  1126 	CBufFlat* paramsBuffer = CBufFlat::NewL(sizeof(TFindInstanceParams));
       
  1127 	CleanupStack::PushL(paramsBuffer);
       
  1128 	RBufWriteStream bufStream;
       
  1129 	CleanupClosePushL(bufStream);
       
  1130 	bufStream.Open(*paramsBuffer);
       
  1131 	aParams.ExternalizeL(bufStream);
       
  1132 	bufStream.WriteInt16L(aFileIds.Count());
       
  1133 	for(TInt ii=0; ii<aFileIds.Count();++ii)
       
  1134 		{
       
  1135 		bufStream << aFileIds[ii];
       
  1136 		}
       
  1137 	CleanupStack::PopAndDestroy(&bufStream);
       
  1138 	
       
  1139 	TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
  1140 	
       
  1141 	TPckgBuf<TInt> size(0);
       
  1142 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1143 	
       
  1144 	TIpcArgs args(&ptr, &size, &paramsPtr);
       
  1145 	
       
  1146 	User::LeaveIfError(SendReceive(ENextInstances, args));
       
  1147 	
       
  1148 	CleanupStack::PopAndDestroy(paramsBuffer);
       
  1149 	
       
  1150 	RBufReadStream readStream;
       
  1151 	CleanupClosePushL(readStream);
       
  1152 	
       
  1153 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1154 	if (buffer)
       
  1155 		{
       
  1156 		readStream.Open(*buffer);
       
  1157 		CleanupStack::PushL(buffer);
       
  1158 		}
       
  1159 	else
       
  1160 		{
       
  1161 		readStream.Open(*iBuffer);
       
  1162 		}
       
  1163 	
       
  1164 	const TInt KCount(readStream.ReadUint32L());
       
  1165 	
       
  1166 	for (TInt i(0) ; i < KCount; ++i)
       
  1167 		{
       
  1168 		TAgnInstance instance;
       
  1169 		readStream >> instance;
       
  1170 		aInstances.AppendL(instance);
       
  1171 		}
       
  1172 
       
  1173 	if (buffer)
       
  1174 		{
       
  1175 		CleanupStack::PopAndDestroy(buffer);
       
  1176 		}
       
  1177 	
       
  1178 	CleanupStack::PopAndDestroy(&readStream);
       
  1179 	}
       
  1180 
       
  1181 // Entry Iterator Calls
       
  1182 
       
  1183 /** 
       
  1184 Creates an iterator for iterating though all the entries in the file. 
       
  1185 
       
  1186 This may be used for activities such as merging or finding. The function returns 
       
  1187 EFalse if no entries are available. If any entries are available, the iterator 
       
  1188 is set to the first one.
       
  1189 
       
  1190 Although this API does not have a trailing L it can leave
       
  1191 
       
  1192 @capability None
       
  1193 @return ETrue if there are entries in the file. EFalse if there are none. 
       
  1194 */
       
  1195 TBool RAgendaServ::CreateEntryIteratorL(TInt64 aFileId)
       
  1196 	{
       
  1197 	// Creates a server side entry iterator
       
  1198 	TPckgBuf<TInt64> fileId(aFileId);
       
  1199 	TPckgBuf<TBool> ok;
       
  1200 	TIpcArgs args(&fileId, &ok);
       
  1201 	User::LeaveIfError(SendReceive(ECreateEntryIterator,args));
       
  1202 	return ok();
       
  1203 	}
       
  1204 
       
  1205 /** 
       
  1206 Moves the entry iterator to the next entry. 
       
  1207 
       
  1208 Returns EFalse if no more entries exist.
       
  1209 
       
  1210 Although this API does not have a trailing L it can leave
       
  1211 
       
  1212 @capability None
       
  1213 @return ETrue if there is are more entries. EFalse if there are no more entries. 
       
  1214 */
       
  1215 TBool RAgendaServ::EntryIteratorNextL(TInt64 aFileId)
       
  1216 	{
       
  1217 	TPckgBuf<TInt64> fileId(aFileId);
       
  1218 	TPckgBuf<TBool> ok;
       
  1219 	TIpcArgs args(&fileId, &ok);
       
  1220 	User::LeaveIfError(SendReceive(EEntryIteratorNext,args));
       
  1221 	return ok();
       
  1222 	}
       
  1223 
       
  1224 /** 
       
  1225 Retrieves the ID for the entry at the current iterator position. 
       
  1226 
       
  1227 The ID can be used to retrieve the entry using CAgnEntryModel::FetchEntryL().
       
  1228 
       
  1229 @capability None
       
  1230 @return The ID for the entry at the current iterator position 
       
  1231 */
       
  1232 CAgnEntry* RAgendaServ::FetchEntryByIteratorL(TInt64 aFileId)
       
  1233 	{
       
  1234 	TPckgBuf<TInt64> fileId(aFileId);
       
  1235 
       
  1236 	TPckgBuf<TInt> size;
       
  1237 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1238 	TIpcArgs args(&ptr, &size, &fileId);
       
  1239 	User::LeaveIfError(SendReceive(EEntryIteratorPosition,args));
       
  1240 
       
  1241 	return ReadEntryFromServerSessionL(size());	
       
  1242 	}
       
  1243 
       
  1244 /** 
       
  1245 Place the uids of entries that have a last changed data greater than aDate and which meet the selection
       
  1246 criteria specified in aFilter into the aUids array.
       
  1247 @capability None
       
  1248 */
       
  1249 void RAgendaServ::GetEntryUidsSinceDateL(RArray<TCalLocalUid>& aUids, const TTime& aTime, TInt64 aFileId)
       
  1250 	{
       
  1251 	// package up filter size and filter 
       
  1252 	TPckgBuf<TTime> time(aTime);
       
  1253 
       
  1254 	TPtr8 bufptr = iBuffer->Ptr(0);
       
  1255 	TPckg<TInt> size(0);
       
  1256 	TPckgBuf<TInt64> fileId(aFileId);
       
  1257 	// make server request
       
  1258 	TIpcArgs args(&bufptr, &size, &time, &fileId);
       
  1259 	User::LeaveIfError(SendReceive(EGetEntryUidsSinceDate, args));
       
  1260 
       
  1261 	RBufReadStream readStream;
       
  1262 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1263 	if (buffer)
       
  1264 		{
       
  1265 		readStream.Open(*buffer);
       
  1266 		}
       
  1267 	else
       
  1268 		{
       
  1269 		readStream.Open(*iBuffer);
       
  1270 		}
       
  1271 	CleanupStack::PushL(buffer);
       
  1272 
       
  1273 	// internalize carrayfix from readStream
       
  1274 	const TInt KCount = readStream.ReadUint32L();
       
  1275 	TCalLocalUid id;
       
  1276 	for (TInt i = 0; i < KCount; i++)
       
  1277 		{
       
  1278 		readStream >> id;
       
  1279 		aUids.AppendL(id);
       
  1280 		}
       
  1281 
       
  1282 	readStream.Close();
       
  1283 	CleanupStack::PopAndDestroy(buffer);
       
  1284 	}
       
  1285 
       
  1286 /** 
       
  1287 Restore the notes text from the server
       
  1288 @capability ReadUserData
       
  1289 */
       
  1290 HBufC* RAgendaServ::RestoreTextL(TStreamId aId, TInt64 aFileId)
       
  1291 	{
       
  1292 	__ASSERT_ALWAYS( (aId != KNullStreamId), Panic(EStreamIdError));
       
  1293 
       
  1294 	TPckgBuf<TInt> size(0);
       
  1295 	TPckg<TStreamId> textId(aId);
       
  1296 
       
  1297 	TPtr8 ptr(iBuffer->Ptr(0));
       
  1298 	TPckgBuf<TInt64> fileId(aFileId);
       
  1299 	TIpcArgs args(&ptr, &textId, &size, &fileId);
       
  1300 	User::LeaveIfError(SendReceive(ERestoreText, args ));
       
  1301 	RBufReadStream readStream;
       
  1302 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1303 	if (buffer)
       
  1304 		{
       
  1305 		readStream.Open(*buffer);
       
  1306 		}
       
  1307 	else
       
  1308 		{
       
  1309 		readStream.Open(*iBuffer);
       
  1310 		}
       
  1311 	CleanupStack::PushL(buffer);
       
  1312 
       
  1313 	TInt textLength = readStream.ReadUint32L();
       
  1314 	HBufC* text = HBufC::NewL(readStream, textLength);
       
  1315 		
       
  1316 	CleanupStack::PopAndDestroy(buffer);
       
  1317 	return text;
       
  1318 	}
       
  1319 
       
  1320 /** 
       
  1321 @capability ReadUserData
       
  1322 */
       
  1323 CAgnContent* RAgendaServ::RestoreAlarmActionL(TStreamId aId, TInt64 aFileId)
       
  1324 	{
       
  1325 	__ASSERT_ALWAYS( (aId != KNullStreamId), Panic(EStreamIdError));
       
  1326 
       
  1327 	TPckgBuf<TInt> size(0);
       
  1328 	TPckg<TStreamId> id(aId);
       
  1329 	TPckgBuf<TInt64> fileId(aFileId);
       
  1330 	
       
  1331 	TPtr8 ptr(iBuffer->Ptr(0));
       
  1332 	TIpcArgs args(&ptr, &id, &size, &fileId);
       
  1333 	User::LeaveIfError(SendReceive(ERestoreAlarmAction, args ));
       
  1334 	RBufReadStream readStream;
       
  1335 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1336 	if (buffer)
       
  1337 		{
       
  1338 		readStream.Open(*buffer);
       
  1339 		}
       
  1340 	else
       
  1341 		{
       
  1342 		readStream.Open(*iBuffer);
       
  1343 		}
       
  1344 	CleanupStack::PushL(buffer);
       
  1345 
       
  1346 	CAgnContent* alarmAction = new (ELeave) CAgnContent;
       
  1347 	CleanupStack::PushL(alarmAction);
       
  1348 	readStream >> *alarmAction;
       
  1349 	CleanupStack::Pop(alarmAction);
       
  1350 
       
  1351 	CleanupStack::PopAndDestroy(buffer);
       
  1352 	
       
  1353 	return alarmAction;
       
  1354 	}
       
  1355 
       
  1356 
       
  1357 /**
       
  1358 Gets the number of allocated cells in the server space, debug only.
       
  1359 @capability None
       
  1360 */	
       
  1361 TInt RAgendaServ::_DebugRequestAllocatedCellsL()
       
  1362 	{
       
  1363 	TPckg<TInt> numCells(0);	
       
  1364 	TIpcArgs args(&numCells);
       
  1365  	User::LeaveIfError(SendReceive(EAgnResourceCount, args));
       
  1366  	return numCells();
       
  1367 	}
       
  1368 
       
  1369 /**
       
  1370 Gets the number of allocated cells in the server space, debug only.
       
  1371 @capability None
       
  1372 */	
       
  1373 TInt RAgendaServ::_DebugRequestAllocatedHeapSizeL()
       
  1374 	{
       
  1375 	TPckg<TInt> heapSize(0);	
       
  1376 	TIpcArgs args(&heapSize);
       
  1377  	User::LeaveIfError(SendReceive(EAgnHeapSizeCount, args));
       
  1378  	return heapSize();
       
  1379 	}
       
  1380 
       
  1381 
       
  1382 /**
       
  1383 Set heap allocation failure according the arguments, debug only.
       
  1384 @param	aHeapAllocationFailureFlag	heap allocation failure flags (RAllocator::TAllocFail)
       
  1385 @param	aCount	Fail at allocation number aCount
       
  1386 @capability None
       
  1387 */	
       
  1388 void RAgendaServ::_DebugSetHeapFailL(TInt aHeapAllocationFailureFlag, TInt aCount)
       
  1389 	{
       
  1390 	TIpcArgs args(aHeapAllocationFailureFlag, aCount);
       
  1391 	User::LeaveIfError(SendReceive(EAgnSetHeapFailure, args));
       
  1392 	}
       
  1393 
       
  1394 //
       
  1395 //	Category access
       
  1396 //
       
  1397 //
       
  1398 
       
  1399 /** 
       
  1400 Start building the Indexes asynchronously.
       
  1401 */
       
  1402 void RAgendaServ::StartBuildIndex(TBool aNeedProgressReport, TRequestStatus& aStatus, TCalCollectionId aCallectionId)
       
  1403 	{
       
  1404 	TIpcArgs args(aNeedProgressReport, aCallectionId);
       
  1405  	SendReceive(EStartBuildIndex, args, aStatus);
       
  1406 	}
       
  1407 
       
  1408 
       
  1409 /** 
       
  1410 Start tidying by ToDo list asynchronously.
       
  1411 @capability WriteUserData
       
  1412 @capability ReadUserData
       
  1413 */
       
  1414 void RAgendaServ::StartTidyByDateL(TBool aReportProgress, TRequestStatus& aStatus,
       
  1415 								   const TAgnFilter& aFilter,
       
  1416 								   const TTime& aTodaysDate,
       
  1417 								   const CalCommon::TCalTimeRange& aCalTimeRange,
       
  1418 								   TCalCollectionId aCallectionId)
       
  1419 	{
       
  1420 	// Externalize parameters
       
  1421 	RBufWriteStream bufStream;
       
  1422 	bufStream.Open(*iBuffer);
       
  1423 	aFilter.ExternalizeL(bufStream);
       
  1424 	TPckgBuf<TTime> timePckgBuf;
       
  1425 	timePckgBuf = aTodaysDate;
       
  1426 	bufStream.WriteL(timePckgBuf);
       
  1427 	timePckgBuf = aCalTimeRange.StartTime().TimeLocalL();
       
  1428 	bufStream.WriteL(timePckgBuf);
       
  1429 	timePckgBuf = aCalTimeRange.EndTime().TimeLocalL();
       
  1430 	bufStream.WriteL(timePckgBuf);
       
  1431 	bufStream.CommitL();
       
  1432 	bufStream.Close();
       
  1433 	TPtr8 parameterPtr = iBuffer->Ptr(0);
       
  1434 	
       
  1435 	// Pass the parameters to the agenda model server but don't start the
       
  1436 	// tidy operation.
       
  1437 	TIpcArgs args(&parameterPtr, aCallectionId);
       
  1438  	SendReceive(ETidyByDateReadParams, args);
       
  1439 
       
  1440 	// Start the tidy operation and notify us asynchronously when it is
       
  1441 	// complete
       
  1442 	TIpcArgs args1(aReportProgress, aCallectionId);
       
  1443  	SendReceive(ETidyByDateStart, args1, aStatus);
       
  1444 	}
       
  1445 
       
  1446 void RAgendaServ::RequestProgress(TRequestStatus& aStatus, TCalCollectionId aCallectionId)
       
  1447 	{
       
  1448 	TIpcArgs args(aCallectionId);
       
  1449  	SendReceive(ERequestProgress, args, aStatus);
       
  1450 	}
       
  1451 /** 
       
  1452 returns the number of categories in the category list
       
  1453 @capability None
       
  1454 */
       
  1455 TInt RAgendaServ::CategoryCountL(TInt64 aFileId)
       
  1456 	{
       
  1457 	TPckgBuf<TInt> count;
       
  1458 	TPckgBuf<TInt64> fileId(aFileId);
       
  1459 	TIpcArgs args(&count, &fileId);
       
  1460 	User::LeaveIfError(SendReceive(EGetCategoryListCount, args));
       
  1461 	
       
  1462 	return count();
       
  1463 	}
       
  1464 
       
  1465 /** 
       
  1466 returns a category created from the category at aIndex in the category list
       
  1467 @capability ReadUserData
       
  1468 @param aIndex, this is the index of the category in the category list
       
  1469 */
       
  1470 CAgnCategory* RAgendaServ::CategoryL(TInt aIndex, TInt64 aFileId)
       
  1471 	{
       
  1472 	TPtr8 ptr(iBuffer->Ptr(0));
       
  1473 	TPckgBuf<TInt> size;
       
  1474 	TPckgBuf<TInt64> fileId(aFileId);
       
  1475 	TIpcArgs args(&ptr, aIndex, &size, &fileId);
       
  1476 	User::LeaveIfError(SendReceive(EGetCategoryListItem, args));
       
  1477 
       
  1478 	RBufReadStream readStream;
       
  1479 	// Create a transfer buffer, and set where to read the data from
       
  1480 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1481 	if (buffer)
       
  1482 		{
       
  1483 		readStream.Open(*buffer);
       
  1484 		}
       
  1485 	else
       
  1486 		{
       
  1487 		readStream.Open(*iBuffer);
       
  1488 		}
       
  1489 	CleanupStack::PushL(buffer);
       
  1490 
       
  1491 	CAgnCategory* category = CAgnCategory::NewL(readStream);
       
  1492 	CleanupStack::PopAndDestroy(buffer);
       
  1493 	
       
  1494 	return category;
       
  1495 	}
       
  1496 
       
  1497 /** 
       
  1498 adds a category to the list 
       
  1499 @capability WriteUserData
       
  1500 @param aName, this is the name of the new category that we want to add to the category list
       
  1501 */
       
  1502 void RAgendaServ::AddCategoryToListL(const TDesC& aName, TInt64 aFileId)
       
  1503 	{
       
  1504 	TPckgBuf<TInt64> fileId(aFileId);
       
  1505 	TIpcArgs args(&aName, &fileId);
       
  1506 	User::LeaveIfError(SendReceive(EAddCategoryToList, args));
       
  1507 	}
       
  1508 
       
  1509 /** 
       
  1510 package up step size and category
       
  1511 @capability None
       
  1512 */
       
  1513 void RAgendaServ::FilterCategoryL(CArrayFixSeg<TAgnEntryId>& aEntries, TInt64 aFileId)
       
  1514 	{
       
  1515 	TPckgBuf<TInt> size(0);
       
  1516 	TPtr8 ptr(iBuffer->Ptr(0));
       
  1517 	TPckgBuf<TInt64> fileId(aFileId);
       
  1518 	
       
  1519 	TIpcArgs args(&ptr, &size, &fileId);
       
  1520 	User::LeaveIfError(SendReceive(ECategoryFilter, args));
       
  1521 
       
  1522 	RBufReadStream readStream;
       
  1523 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1524 	if (buffer)
       
  1525 		{
       
  1526 		readStream.Open(*buffer);								
       
  1527 		}
       
  1528 	else
       
  1529 		{
       
  1530 		readStream.Open(*iBuffer);
       
  1531 		}
       
  1532 	CleanupStack::PushL(buffer);
       
  1533 
       
  1534 	const TInt numberEntries = readStream.ReadInt16L();
       
  1535 	for(TInt a = 0; a < numberEntries; ++a)
       
  1536 		{
       
  1537 		TAgnEntryId entry;
       
  1538 		entry.InternalizeL(readStream);
       
  1539 		aEntries.AppendL(entry);
       
  1540 		}
       
  1541 
       
  1542 	CleanupStack::PopAndDestroy(buffer);
       
  1543 	}
       
  1544 
       
  1545 /**
       
  1546 @capability WriteUserData
       
  1547 */
       
  1548 void RAgendaServ::StartCategoryTaskL(TBool aReportProgress, TRequestStatus& aStatus, const CAgnCategory& aCategory, CCalAsyncTaskManager::TAsyncAction aTask, TCalCollectionId aCallectionId)
       
  1549 	{
       
  1550 	// Create a write stream for this buffer
       
  1551 	RBufWriteStream bufStream;
       
  1552 	bufStream.Open(*iBuffer);
       
  1553 	CleanupClosePushL(bufStream);
       
  1554 	aCategory.ExternalizeL(bufStream);
       
  1555 
       
  1556 	bufStream.CommitL();
       
  1557 	CleanupStack::Pop(&bufStream);
       
  1558 	TPtr8 ptr(iBuffer->Ptr(0));
       
  1559 	TIpcArgs args(&ptr, iBuffer->Size(), aTask, aCallectionId);
       
  1560 	User::LeaveIfError(SendReceive(ECategoryStart, args));
       
  1561 	TIpcArgs args1(aReportProgress, aTask, aCallectionId);
       
  1562 	SendReceive(ECategoryStartAsyn, args1, aStatus);
       
  1563 	}
       
  1564 
       
  1565 
       
  1566 void RAgendaServ::FilterCategorySynchronouslyL(const CAgnCategory& aCategory, TCalCollectionId aCallectionId)
       
  1567 	{
       
  1568 	RBufWriteStream bufStream;
       
  1569 	bufStream.Open(*iBuffer);
       
  1570 	CleanupClosePushL(bufStream);
       
  1571 	aCategory.ExternalizeL(bufStream);
       
  1572 
       
  1573 	bufStream.CommitL();
       
  1574 	CleanupStack::Pop(&bufStream);
       
  1575 	
       
  1576 	TPtr8 ptr(iBuffer->Ptr(0));
       
  1577 	TIpcArgs args(&ptr, iBuffer->Size(), CCalAsyncTaskManager::EFilterCategory, aCallectionId);
       
  1578 	User::LeaveIfError(SendReceive(ECategoryStart, args));
       
  1579 	TRequestStatus status = KRequestPending;
       
  1580 	TIpcArgs args1(EFalse, CCalAsyncTaskManager::EFilterCategory, aCallectionId);
       
  1581 	SendReceive(ECategoryStartAsyn, args1, status);
       
  1582 	User::WaitForRequest(status);
       
  1583 	User::LeaveIfError(status.Int());
       
  1584 	}
       
  1585 
       
  1586 /**
       
  1587 @capability None
       
  1588 */
       
  1589 void RAgendaServ::CancelTask(TInt64 aFileId)
       
  1590 	{
       
  1591 	TPckgBuf<TInt64> fileId(aFileId);
       
  1592 	TIpcArgs args(&fileId);
       
  1593 	SendReceive(ECancelTask, args);
       
  1594 	}
       
  1595 
       
  1596 /**
       
  1597 @capability WriteUserData
       
  1598 */
       
  1599 void RAgendaServ::CreateAgendaFileL(const TDesC& aFileName)
       
  1600 	{
       
  1601 	TIpcArgs args(&aFileName);
       
  1602 	User::LeaveIfError(SendReceive(ECreateAgendaFile,args));
       
  1603 	}
       
  1604 
       
  1605 /**
       
  1606 @capability WriteUserData
       
  1607 */
       
  1608 void RAgendaServ::DeleteAgendaFileL(const TDesC& aFileName, TInt aSessionId)
       
  1609 	{
       
  1610 	TIpcArgs args(&aFileName,aSessionId);
       
  1611 	User::LeaveIfError(SendReceive(EDeleteAgendaFile,args));
       
  1612 	}
       
  1613 
       
  1614 CDesCArray* RAgendaServ::ListAgendaFilesL()
       
  1615 	{
       
  1616 	// Get a list of file names held by the server 
       
  1617 	// currently setup extractor
       
  1618 	// Argumnents: 0 : Data buffer 
       
  1619 	//			   1 : Size of buffer
       
  1620 
       
  1621 	//create the buffer
       
  1622 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1623 
       
  1624 	TPckgBuf<TInt> size(0);
       
  1625 	TIpcArgs args(&ptr, &size);
       
  1626 	User::LeaveIfError(SendReceive(EGetListFileNames,args));
       
  1627 	RBufReadStream readStream;
       
  1628 
       
  1629 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1630 	if (buffer)
       
  1631 		{
       
  1632 		readStream.Open(*buffer);
       
  1633 		}
       
  1634 	else 
       
  1635 		{
       
  1636 		readStream.Open(*iBuffer);
       
  1637 		}
       
  1638 	CleanupStack::PushL(buffer);
       
  1639 
       
  1640 	const TInt count = readStream.ReadUint8L();
       
  1641 	CDesCArray* fileNames=NULL;
       
  1642 	if(count!=0)
       
  1643 		{
       
  1644 		fileNames = new(ELeave) CDesCArrayFlat(count);
       
  1645 		CleanupStack::PushL(fileNames);
       
  1646 		for (TInt i=0; i<count; ++i)
       
  1647 			{
       
  1648 			TFileName fileName;
       
  1649 			TInt length=readStream.ReadInt8L();		
       
  1650 			readStream.ReadL(fileName,length);
       
  1651 			fileNames->AppendL(fileName);
       
  1652 			}
       
  1653 
       
  1654 		CleanupStack::Pop(fileNames);
       
  1655 		}
       
  1656 	readStream.Close();
       
  1657 
       
  1658 	CleanupStack::PopAndDestroy(buffer);
       
  1659 	return fileNames;
       
  1660 	}
       
  1661 
       
  1662 TBool RAgendaServ::AgendaFileExistsL(const TDesC& aFileName)
       
  1663 	{
       
  1664 	TPckgBuf<TBool> isFileThere;
       
  1665 	TIpcArgs args(&isFileThere,&aFileName);
       
  1666 	User::LeaveIfError(SendReceive(EAgendaFileExists,args));
       
  1667 	return isFileThere();
       
  1668 	}
       
  1669 
       
  1670 TInt RAgendaServ::SetUpdateAlarm(TBool aUpdateAlarm, TInt64 aFileId)
       
  1671 	{
       
  1672 	TPckgBuf<TInt64> fileId(aFileId);
       
  1673 	TIpcArgs args(aUpdateAlarm, &fileId);
       
  1674 	return SendReceive(ESetUpdateAlarm,args); 
       
  1675 	}
       
  1676 
       
  1677 void RAgendaServ::SetEnablePubSubNotificationsL(TBool aEnablePubSubNotification, TInt64 aFileId)
       
  1678 	{
       
  1679 	TPckgBuf<TInt64> fileId(aFileId);
       
  1680 	TIpcArgs args(aEnablePubSubNotification, &fileId);
       
  1681 	User::LeaveIfError(SendReceive(ESetEnablePubSubNotification,args));
       
  1682 	}
       
  1683 
       
  1684 void RAgendaServ::SetChangeNotificationParametersL(MCalChangeCallBack2::TChangeEntryType aEntryType, TBool aIncludeUndatedTodos, TTime aFilterStartTimeUtc, TTime aFilterEndTimeUtc, TInt64 aFileId)
       
  1685     {
       
  1686 	CBufFlat* paramsBuffer = CBufFlat::NewL(sizeof(TInt64) + sizeof(TBool) + sizeof(MCalChangeCallBack2::TChangeEntryType));
       
  1687 	CleanupStack::PushL(paramsBuffer);
       
  1688 	RBufWriteStream bufStream;
       
  1689 	CleanupClosePushL(bufStream);
       
  1690 	bufStream.Open(*paramsBuffer);
       
  1691 	bufStream.WriteInt16L(aEntryType);
       
  1692 	bufStream.WriteInt16L(aIncludeUndatedTodos);
       
  1693 	bufStream << aFileId;
       
  1694 	CleanupStack::PopAndDestroy(&bufStream);
       
  1695 	
       
  1696 	TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
  1697 
       
  1698 	TPckg<TTime> start(aFilterStartTimeUtc);
       
  1699 	TPckg<TTime> end(aFilterEndTimeUtc);
       
  1700 	TIpcArgs args(&start, &end, &paramsPtr);
       
  1701     User::LeaveIfError(SendReceive(ERequestChangeNotificationParameters, args));
       
  1702 	CleanupStack::PopAndDestroy(paramsBuffer);
       
  1703     }
       
  1704 
       
  1705 void RAgendaServ::RequestChangeNotification(TRequestStatus& aStatus, TInt aSessionId, TUint8 aNotificationType)
       
  1706 	{
       
  1707 	TIpcArgs args(aSessionId, aNotificationType);
       
  1708 	SendReceive(ERequestChangeNotification, args, aStatus);
       
  1709 	}
       
  1710 
       
  1711 void RAgendaServ::CancelChangeNotification(TInt aSessionId, TUint8 aNotificationType)
       
  1712 	{
       
  1713 	TIpcArgs args(aSessionId, aNotificationType);
       
  1714     SendReceive(ECancelChangeNotification, args);
       
  1715 	}
       
  1716 
       
  1717 // Prevent notification of changes made to the agenda model from this
       
  1718 // client from being broadcast to other clients.
       
  1719 void RAgendaServ::DisableChangeBroadcast(TInt64 aFileId)
       
  1720 	{
       
  1721 	TPckgBuf<TInt64> fileId(aFileId);
       
  1722 	TIpcArgs args(&fileId);
       
  1723     SendReceive(EDisableChangeBroadcast, args);	
       
  1724 	}
       
  1725 
       
  1726 // Allow notification of changes made to the agenda model from this
       
  1727 // client from being broadcast to other clients.
       
  1728 void RAgendaServ::EnableChangeBroadcast(TInt64 aFileId)
       
  1729 	{
       
  1730 	TPckgBuf<TInt64> fileId(aFileId);
       
  1731 	TIpcArgs args(&fileId);
       
  1732     SendReceive(EEnableChangeBroadcast, args);
       
  1733 	}
       
  1734 
       
  1735 void RAgendaServ::GetFileChangesSinceLastNotificationL(RPointerArray<CAgnFileChangeInfo>& aCalFileChangeInfo, TInt aSessionId)
       
  1736     {
       
  1737     TPtr8 ptr = iBuffer->Ptr(0);
       
  1738     TPckgBuf<TInt> size(0);
       
  1739     TIpcArgs args(&ptr, &size, aSessionId);
       
  1740     User::LeaveIfError(SendReceive(EGetFileChangesSinceLastNotification,args));
       
  1741     RBufReadStream readStream;
       
  1742     CleanupClosePushL(readStream);
       
  1743     CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1744     if (buffer)
       
  1745         {
       
  1746         readStream.Open(*buffer);
       
  1747         }
       
  1748     else
       
  1749         {
       
  1750         readStream.Open(*iBuffer);
       
  1751         }
       
  1752     CleanupStack::PushL(buffer);
       
  1753     const TInt count = readStream.ReadInt32L();
       
  1754     aCalFileChangeInfo.ReserveL(aCalFileChangeInfo.Count() + count);
       
  1755     for (TInt ii = 0; ii < count; ++ii)
       
  1756         {       
       
  1757         CAgnFileChangeInfo* info = CAgnFileChangeInfo::NewL(readStream);
       
  1758         aCalFileChangeInfo.Append(info);
       
  1759         }
       
  1760 
       
  1761     CleanupStack::PopAndDestroy(buffer);
       
  1762     CleanupStack::PopAndDestroy(&readStream);
       
  1763     }
       
  1764 
       
  1765 /**
       
  1766 @capability ReadUserData
       
  1767 */
       
  1768 void RAgendaServ::GetChangesSinceLastNotificationL(RArray<TCalChangeEntry>& aChangeItems, TCalCollectionId aCollectionId, TCalFileId& aFileId )
       
  1769 	{
       
  1770 	TPckgBuf<TCalFileId> fileId(aFileId);
       
  1771 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1772 	TPckgBuf<TInt> size(0);
       
  1773 	TIpcArgs args(&ptr, &size, aCollectionId, &fileId);
       
  1774 	User::LeaveIfError(SendReceive(EGetChangesSinceLastNotification,args));
       
  1775 	RBufReadStream readStream;
       
  1776     aFileId = fileId();
       
  1777   	const TInt KBufferSize = size();
       
  1778 	CBufFlat* buffer = CreateTransmitBufferL(KBufferSize);
       
  1779 	if (buffer)
       
  1780 		{
       
  1781 		readStream.Open(*buffer);
       
  1782 		}
       
  1783 	else
       
  1784 		{
       
  1785 		readStream.Open(*iBuffer);
       
  1786 		}
       
  1787 
       
  1788 	CleanupStack::PushL(buffer);
       
  1789 	
       
  1790 	// The buffer contains a number of change item (6 bytes in size) and nothing else.
       
  1791 	// Check that the buffer is divisible by the size of a change item.
       
  1792 	const TInt KSizeOfChangeItem = 6;
       
  1793 	if (KBufferSize % KSizeOfChangeItem == 0)
       
  1794 		{
       
  1795 		const TInt KItems = KBufferSize / KSizeOfChangeItem;
       
  1796 
       
  1797 		TCalChangeEntry changeItem;
       
  1798 		for (TInt count=0; count < KItems; count++)
       
  1799 			{
       
  1800 			changeItem.iEntryId = readStream.ReadUint32L();
       
  1801 			changeItem.iChangeType = (MCalChangeCallBack2::TChangeType)readStream.ReadUint8L();
       
  1802 			CCalEntry::TType type = static_cast<CCalEntry::TType>(readStream.ReadUint8L());
       
  1803 			if (changeItem.iChangeType == MCalChangeCallBack2::EChangeUndefined)
       
  1804 				{
       
  1805 				changeItem.iEntryType = MCalChangeCallBack2::EChangeEntryAll;
       
  1806 				}
       
  1807 			else
       
  1808 				{
       
  1809 				if (type == CCalEntry::ETodo)
       
  1810 					{
       
  1811 					changeItem.iEntryType = MCalChangeCallBack2::EChangeEntryTodo;
       
  1812 					}
       
  1813 				else
       
  1814 					{
       
  1815 					changeItem.iEntryType = MCalChangeCallBack2::EChangeEntryEvent;
       
  1816 					}
       
  1817 				}
       
  1818 			aChangeItems.AppendL(changeItem);
       
  1819 			}
       
  1820 		}
       
  1821 	else
       
  1822 		{
       
  1823 		// If the buffer is corrupt, return a dummy item to say that something has changed
       
  1824 		TCalChangeEntry changeItem;
       
  1825 		changeItem.iEntryId = 0;
       
  1826 		changeItem.iChangeType = MCalChangeCallBack2::EChangeUndefined;
       
  1827 		changeItem.iEntryType = MCalChangeCallBack2::EChangeEntryAll;
       
  1828 		aChangeItems.AppendL(changeItem);
       
  1829 		}
       
  1830 
       
  1831 	readStream.Close();
       
  1832 
       
  1833 	CleanupStack::PopAndDestroy(buffer);
       
  1834 	}
       
  1835 
       
  1836 /**
       
  1837 @capability ReadUserData
       
  1838 */
       
  1839 HBufC8* RAgendaServ::GetPropertyValueL(const TDesC& aFilename, TStreamId aStreamId) const
       
  1840     {
       
  1841     TPtr8 bufptr = iBuffer->Ptr(0);
       
  1842     TPckg<TInt> size(0);
       
  1843     TPckgC<TStreamId> streamId(aStreamId);
       
  1844     
       
  1845     // make server request
       
  1846     TIpcArgs args(&bufptr, &size, &aFilename, &streamId);
       
  1847     
       
  1848     User::LeaveIfError(SendReceive(EGetPropertyValue, args));
       
  1849 
       
  1850     RBufReadStream readStream;
       
  1851     CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1852     if (buffer)
       
  1853         {
       
  1854         readStream.Open(*buffer);
       
  1855         }
       
  1856     else
       
  1857         {
       
  1858         readStream.Open(*iBuffer);
       
  1859         }
       
  1860     CleanupStack::PushL(buffer);
       
  1861     
       
  1862     // internalize calendar info from readStream
       
  1863     HBufC8* value = HBufC8::NewL(readStream, KMaxTInt);
       
  1864 
       
  1865     readStream.Close();
       
  1866     CleanupStack::PopAndDestroy(buffer);
       
  1867     
       
  1868     return value;
       
  1869     }
       
  1870 
       
  1871 /**
       
  1872 @capability WriteUserData
       
  1873 */
       
  1874 void RAgendaServ::SetCalendarInfoL(const TDesC& aFilename, const CCalCalendarInfoImpl& aCalendarInfoImpl) const
       
  1875     {
       
  1876     CBufFlat* paramsBuffer = CBufFlat::NewL(100);
       
  1877     CleanupStack::PushL(paramsBuffer);
       
  1878     RBufWriteStream bufStream;
       
  1879     CleanupClosePushL(bufStream);
       
  1880     bufStream.Open(*paramsBuffer);
       
  1881     aCalendarInfoImpl.IpcExternalizeL(bufStream);
       
  1882     CleanupStack::PopAndDestroy(&bufStream);
       
  1883     
       
  1884     TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
  1885 
       
  1886     TIpcArgs args(&aFilename, &paramsPtr);
       
  1887     TInt error = SendReceive(ESetCalendarInfo, args);
       
  1888     User::LeaveIfError(error);
       
  1889     CleanupStack::PopAndDestroy(paramsBuffer);
       
  1890     }
       
  1891 
       
  1892 /**
       
  1893 @capability ReadUserData
       
  1894 */
       
  1895 CCalCalendarInfoImpl* RAgendaServ::GetCalendarInfoLC(const TDesC& aFilename, CCalSessionImpl& aSessionImpl) const
       
  1896     {
       
  1897     TPtr8 bufptr = iBuffer->Ptr(0);
       
  1898     TPckg<TInt> size(0);
       
  1899     
       
  1900     // make server request
       
  1901     TIpcArgs args(&bufptr, &size, &aFilename);
       
  1902     
       
  1903     User::LeaveIfError(SendReceive(EGetCalendarInfo, args));
       
  1904 
       
  1905     RBufReadStream readStream;
       
  1906     CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1907     if (buffer)
       
  1908         {
       
  1909         readStream.Open(*buffer);
       
  1910         }
       
  1911     else
       
  1912         {
       
  1913         readStream.Open(*iBuffer);
       
  1914         }
       
  1915     CleanupStack::PushL(buffer);
       
  1916     
       
  1917     // internalize calendar info from readStream
       
  1918     CCalCalendarInfoImpl* info(CCalCalendarInfoImpl::NewL(aSessionImpl));
       
  1919     CleanupStack::PushL(info);
       
  1920     info->IpcInternalizeL(readStream);
       
  1921 
       
  1922     readStream.Close();
       
  1923     CleanupStack::Pop(info);
       
  1924     CleanupStack::PopAndDestroy(buffer);
       
  1925     CleanupStack::PushL(info);
       
  1926     return info;
       
  1927     }
       
  1928 
       
  1929 /**
       
  1930 @capability WriteUserData
       
  1931 */
       
  1932 void RAgendaServ::DeleteEntryL(TAgnEntryId aEntryId, TCalCollectionId aCallectionId)
       
  1933 	{
       
  1934 	TPckg<TAgnEntryId> entryId(aEntryId);
       
  1935 	TIpcArgs args(&entryId, aCallectionId);
       
  1936 	User::LeaveIfError(SendReceive(EDeleteEntry,args));
       
  1937 	}
       
  1938 
       
  1939 /**
       
  1940 @capability ReadUserData
       
  1941 */
       
  1942 void RAgendaServ::FetchEntryByGuidL(const TDesC8& aGuid, RPointerArray<CAgnEntry>& aList, TInt64 aFileId) const
       
  1943 	{
       
  1944 	// Get all entries which have the same GUID 
       
  1945 	// currently setup extractor
       
  1946 	// Argumnents: 0 : Data buffer 
       
  1947 	//			   1 : Size of buffer
       
  1948 	//			   2 : GUID
       
  1949 
       
  1950 	//create the buffer
       
  1951 	TPtr8 ptr = iBuffer->Ptr(0);
       
  1952 
       
  1953 	TPckgBuf<TInt> size(0);
       
  1954 	TPckgBuf<TInt64> fileId(aFileId);
       
  1955 	TIpcArgs args(&ptr, &size, &aGuid, &fileId);
       
  1956 	User::LeaveIfError(SendReceive(EFetchEntryByGuid,args));
       
  1957 	RBufReadStream readStream;
       
  1958 
       
  1959 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  1960 	if (buffer)
       
  1961 		{
       
  1962 		readStream.Open(*buffer);
       
  1963 		}
       
  1964 	else 
       
  1965 		{
       
  1966 		readStream.Open(*iBuffer);
       
  1967 		}
       
  1968 	CleanupStack::PushL(buffer);
       
  1969 
       
  1970 	const TInt KCount = readStream.ReadUint32L();
       
  1971 	
       
  1972 	for (TInt i = 0; i < KCount; ++i)
       
  1973 		{
       
  1974 		CAgnEntry* entry = CreateEntryFromBufferStreamL(readStream);
       
  1975 		CleanupStack::PushL(entry);
       
  1976 		aList.AppendL(entry);
       
  1977 		CleanupStack::Pop(entry);
       
  1978 		}
       
  1979 
       
  1980 	readStream.Close();
       
  1981 
       
  1982 	CleanupStack::PopAndDestroy(buffer);
       
  1983 	}
       
  1984 
       
  1985 /**
       
  1986 @capability WriteUserData
       
  1987 */
       
  1988 void RAgendaServ::DeleteEntryL(const RArray<TCalLocalUid>& aIds, TCalCollectionId aCallectionId)
       
  1989 	{
       
  1990 	// Create a write stream for this buffer
       
  1991 	RBufWriteStream writeStream;
       
  1992 	writeStream.Open(*iBuffer);
       
  1993 	const TInt KCount = aIds.Count();
       
  1994 	writeStream.WriteUint32L(KCount);
       
  1995 		for (TInt ii = 0; ii < KCount; ++ii)
       
  1996 		{
       
  1997 		writeStream.WriteUint32L(aIds[ii]);
       
  1998 		}
       
  1999 
       
  2000 	writeStream.CommitL();
       
  2001 	writeStream.Close();
       
  2002 	
       
  2003 	TInt size(iBuffer->Size());
       
  2004 	TPtr8 ptr = iBuffer->Ptr(0);
       
  2005 	
       
  2006 	TIpcArgs args(size, &ptr, aCallectionId);
       
  2007 	User::LeaveIfError(SendReceive(EDeleteEntriesByLocalUid,args));
       
  2008 	}
       
  2009 
       
  2010 /**
       
  2011 @capability WriteUserData
       
  2012 */
       
  2013 void RAgendaServ::DeleteEntryByGuidL(const TDesC8& aGuid, TBool aCommitAndNotify, TInt64 aFileId) 
       
  2014 	{
       
  2015 	TPckgBuf<TInt64> fileId(aFileId);
       
  2016 	TIpcArgs args(&aGuid, aCommitAndNotify, &fileId);
       
  2017 	User::LeaveIfError(SendReceive(EDeleteEntryByGuid, args));
       
  2018 	}
       
  2019 
       
  2020 /**
       
  2021 @capability ReadUserData
       
  2022 */
       
  2023 void RAgendaServ::FindInstancesL(RArray<TInt64>& aFileIds, CArrayFix<TAgnInstance>& aMatchedInstanceList, const TFindInstanceParams& aParams)
       
  2024 	{
       
  2025 	// Write the find instance setting out to the buffer
       
  2026 	CBufFlat* paramsBuffer = CBufFlat::NewL(sizeof(TFindInstanceParams));
       
  2027 	CleanupStack::PushL(paramsBuffer);
       
  2028 	RBufWriteStream bufStream;
       
  2029 	CleanupClosePushL(bufStream);
       
  2030 	bufStream.Open(*paramsBuffer);
       
  2031 	aParams.ExternalizeL(bufStream);
       
  2032 	bufStream.WriteInt16L(aFileIds.Count());
       
  2033 	for(TInt ii=0; ii<aFileIds.Count();++ii)
       
  2034 		{
       
  2035 		bufStream << aFileIds[ii];
       
  2036 		}
       
  2037 	CleanupStack::PopAndDestroy(&bufStream);
       
  2038 	
       
  2039 	TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
  2040 	
       
  2041 	TPckgBuf<TInt> size(0);
       
  2042 	TPtr8 ptr = iBuffer->Ptr(0);
       
  2043 	
       
  2044 	TIpcArgs args(&ptr, &size, &paramsPtr);
       
  2045 	User::LeaveIfError(SendReceive(EFindInstances,args));
       
  2046 	
       
  2047 	CleanupStack::PopAndDestroy(paramsBuffer);
       
  2048 	
       
  2049 	RBufReadStream readStream;
       
  2050 
       
  2051 	CBufFlat* buffer = CreateTransmitBufferL(size());
       
  2052 	if (buffer)
       
  2053 		{
       
  2054 		readStream.Open(*buffer);
       
  2055 		}
       
  2056 	else
       
  2057 		{
       
  2058 		readStream.Open(*iBuffer);
       
  2059 		}
       
  2060 
       
  2061 	if (buffer)
       
  2062 		{
       
  2063 		CleanupStack::PushL(buffer);
       
  2064 		}
       
  2065 
       
  2066 	TInt items = readStream.ReadUint32L();
       
  2067 
       
  2068 	for (TInt count=0; count<items; count++)
       
  2069 		{
       
  2070 		TAgnInstance aInstance;
       
  2071 		readStream >> aInstance;
       
  2072 
       
  2073 		aMatchedInstanceList.AppendL(aInstance);
       
  2074 		}
       
  2075 
       
  2076 	readStream.Close();
       
  2077 
       
  2078 	if (buffer)
       
  2079 		{
       
  2080 		CleanupStack::PopAndDestroy(buffer);
       
  2081 		}
       
  2082 	}
       
  2083 
       
  2084 /**
       
  2085 @capability WriteUserData
       
  2086 */
       
  2087 void RAgendaServ::CommitL(TCalCollectionId aCallectionId)
       
  2088 	{
       
  2089 	TIpcArgs args(aCallectionId);
       
  2090 	User::LeaveIfError(SendReceive(ECommit, args));
       
  2091 	}
       
  2092 /**
       
  2093 @capability WriteUserData
       
  2094 */
       
  2095 void RAgendaServ::Rollback(TCalCollectionId aCallectionId)
       
  2096 	{
       
  2097 	TIpcArgs args(aCallectionId);
       
  2098 	SendReceive(ERollback, args);
       
  2099 	}
       
  2100 /** Get the last modified time for tz rules from time zone server
       
  2101 	It is called when the client getting the last modified date for an entry since the 
       
  2102 	last modified time of an entry is not updated when tz database is updated.
       
  2103 @capability none
       
  2104 */
       
  2105 TTime RAgendaServ::TzRulesLastModifiedDateL(TInt64 aFileId)
       
  2106 	{
       
  2107 	TPckgBuf<TTime> time;
       
  2108 	TPckgBuf<TInt64> fileId(aFileId);
       
  2109 	TIpcArgs args(&time, &fileId);
       
  2110 	User::LeaveIfError(SendReceive(ETzDbChangedTime, args));
       
  2111 	return time();
       
  2112 	}
       
  2113 
       
  2114 #ifdef SYMBIAN_CALENDAR_ENHANCEDSEARCHANDSORT
       
  2115 /**
       
  2116  * Create instance iterator with given find instance settings and sort
       
  2117  * criteria
       
  2118 @capability ReadUserData
       
  2119 */
       
  2120 TInt RAgendaServ::CreateInstanceIteratorL(const TFindInstanceParams& aFindParams, const CCalSortCriteria& aSortCriteria, RArray<TInt64>& aFileIds)
       
  2121 	{
       
  2122 	//Store find instance parameter into buffer
       
  2123 	CBufFlat* paramsBuffer = CBufFlat::NewL(sizeof(TFindInstanceParams));
       
  2124 	CleanupStack::PushL(paramsBuffer);
       
  2125 	RBufWriteStream bufStream;
       
  2126 	CleanupClosePushL(bufStream);
       
  2127 	bufStream.Open(*paramsBuffer);
       
  2128 	aFindParams.ExternalizeL(bufStream);
       
  2129 	bufStream.CommitL();
       
  2130 	CleanupStack::PopAndDestroy(&bufStream);
       
  2131 	
       
  2132 	TPtr8 paramsPtr = paramsBuffer->Ptr(0);
       
  2133 	
       
  2134 	//Store sort criteria into buffer
       
  2135 	RBufWriteStream sortCriteriaBufStream;
       
  2136 	sortCriteriaBufStream.Open(*iBuffer);
       
  2137 	CleanupClosePushL(sortCriteriaBufStream);
       
  2138 	aSortCriteria.SortCriteria().ExternalizeL(sortCriteriaBufStream);
       
  2139 	sortCriteriaBufStream.WriteInt16L(aFileIds.Count());
       
  2140 	const TInt count = aFileIds.Count();
       
  2141 	for(TInt ii=0; ii<count; ++ii)
       
  2142 		{
       
  2143 		sortCriteriaBufStream << aFileIds[ii];
       
  2144 		}
       
  2145 
       
  2146     sortCriteriaBufStream.CommitL();
       
  2147 	CleanupStack::PopAndDestroy(&sortCriteriaBufStream);
       
  2148 
       
  2149 	// Get the sizes of the buffers
       
  2150 	TPtr8 ptr = iBuffer->Ptr(0);
       
  2151 	
       
  2152 	TPckgBuf<TInt> iteratorId(KErrNotFound);
       
  2153 	TIpcArgs args(&paramsPtr, &ptr, &iteratorId);
       
  2154 	
       
  2155 	User::LeaveIfError(SendReceive(EInstanceIteratorCreate, args));
       
  2156 	
       
  2157 	CleanupStack::PopAndDestroy(paramsBuffer);
       
  2158 	
       
  2159 	return iteratorId();
       
  2160 	}
       
  2161 #endif
       
  2162 
       
  2163 /**
       
  2164  * Destroy instance iterator with specified iterator id
       
  2165 @capability ReadUserData
       
  2166 */
       
  2167 void RAgendaServ::DestroyInstanceIterator(TInt aIteratorId)
       
  2168 	{
       
  2169 	TIpcArgs args(aIteratorId);
       
  2170 	
       
  2171 	//It's not neccessary/possible to check if the operation complete successfully
       
  2172 	//because the function may be called at application exiting time.
       
  2173 	Send(EInstanceIteratorDestroy, args);
       
  2174 	}
       
  2175 	
       
  2176 /**
       
  2177  * Fetch the available instances start from specified index 
       
  2178 @capability ReadUserData
       
  2179 */
       
  2180 void RAgendaServ::InstanceIteratorNextL(TInt aIteratorId, CArrayFix<TAgnInstance>& aInstances, TInt aIndex) const
       
  2181 	{
       
  2182 	TPckgBuf<TInt> size(0);
       
  2183 	TPtr8 ptr = iBuffer->Ptr(0);
       
  2184 	
       
  2185 	TIpcArgs args(&ptr, &size, aIteratorId, aIndex);
       
  2186 	User::LeaveIfError(SendReceive(EInstanceIteratorNext, args));
       
  2187 
       
  2188 	LoadingInstancesFromBufferL(aInstances, size());
       
  2189 	}
       
  2190 	
       
  2191 /**
       
  2192  * Fetch the previous instances which index are before given index  
       
  2193 @capability ReadUserData
       
  2194 */
       
  2195 void RAgendaServ::InstanceIteratorPreviousL(TInt aIteratorId, CArrayFix<TAgnInstance>& aInstances, TInt aIndex) const
       
  2196 	{
       
  2197 	TPckgBuf<TInt> size(0);
       
  2198 	TPtr8 ptr = iBuffer->Ptr(0);
       
  2199 	
       
  2200 	TIpcArgs args(&ptr, &size, aIteratorId, aIndex);
       
  2201 	User::LeaveIfError(SendReceive(EInstanceIteratorPrevious, args));
       
  2202 	
       
  2203 	LoadingInstancesFromBufferL(aInstances, size());
       
  2204 	}
       
  2205 	
       
  2206 void RAgendaServ::LoadingInstancesFromBufferL(CArrayFix<TAgnInstance>& aInstances, TInt aBufSize) const
       
  2207 	{
       
  2208 	RBufReadStream readStream;
       
  2209 	CleanupClosePushL(readStream);
       
  2210 	
       
  2211 	CBufFlat* buffer = CreateTransmitBufferL(aBufSize);
       
  2212 	if (buffer)
       
  2213 		{
       
  2214 		readStream.Open(*buffer);
       
  2215 		CleanupStack::PushL(buffer);
       
  2216 		}
       
  2217 	else
       
  2218 		{
       
  2219 		readStream.Open(*iBuffer);
       
  2220 		}
       
  2221 	
       
  2222 	const TInt KCount(readStream.ReadUint32L());
       
  2223 	
       
  2224 	for (TInt i(0) ; i < KCount; ++i)
       
  2225 		{
       
  2226 		TAgnInstance instanceId;
       
  2227 		instanceId.InternalizeL(readStream);
       
  2228 		aInstances.AppendL(instanceId);
       
  2229 		}
       
  2230 
       
  2231 	if (buffer)
       
  2232 		{
       
  2233 		CleanupStack::PopAndDestroy(buffer);
       
  2234 		}
       
  2235 	
       
  2236 	CleanupStack::PopAndDestroy(&readStream);
       
  2237 	}
       
  2238 
       
  2239 /**
       
  2240  * Get the number of instances stored in an iterator
       
  2241 @capability ReadUserData
       
  2242 */
       
  2243 TInt RAgendaServ::InstanceIteratorCountL(TInt aIteratorId) const
       
  2244 	{
       
  2245 	TPckgBuf<TInt> count(0);
       
  2246 	TIpcArgs args(aIteratorId, &count);
       
  2247 	User::LeaveIfError(SendReceive(EInstanceIteratorCount, args));
       
  2248 	return count();
       
  2249 	}
       
  2250 
       
  2251 /**
       
  2252  * Find the index of the given instance in the iterator. 
       
  2253 @capability ReadUserData
       
  2254 */
       
  2255 TInt RAgendaServ::InstanceIteratorLocateIndexL(TInt aIteratorId, TAgnInstance& aInstanceId)
       
  2256 	{
       
  2257 	TPckgBuf<TInt> index(0);
       
  2258 	TPckg<TAgnInstance> instanceId(aInstanceId);	
       
  2259 	TIpcArgs args(aIteratorId, &instanceId, &index);
       
  2260 	User::LeaveIfError(SendReceive(EInstanceIteratorLocateIndex, args));
       
  2261 	return index();
       
  2262 	}
       
  2263 
       
  2264 void RAgendaServ::DeleteSimpleEntry(CAgnSimpleEntry* aSimpleEntry)
       
  2265 /** Deletes a sort entry from the sort entry allocator (a specialised memory handler 
       
  2266 for sort entry objects). 
       
  2267 
       
  2268 A sort entry is an entry in an agenda index. The function frees the sort entry object 
       
  2269 and puts it on the free list.
       
  2270 
       
  2271 @param aSimpleEntry Pointer to the simple entry to delete. */
       
  2272 	{
       
  2273 	iSimpleEntryAllocator->Delete(aSimpleEntry);
       
  2274 	}
       
  2275 
       
  2276 CAgnSimpleEntry* RAgendaServ::CreateDumySimpleEntryL()
       
  2277 	{
       
  2278 	return iSimpleEntryAllocator->CreateSimpleEntryL(CCalEntry::EAppt);
       
  2279 	}
       
  2280 
       
  2281 CAgnSimpleEntryAllocator& RAgendaServ::SimpleEntryAllocator() const
       
  2282 	{
       
  2283 	return *iSimpleEntryAllocator;
       
  2284 	}
       
  2285 
       
  2286 /**
       
  2287 @capability none
       
  2288 */
       
  2289 void RAgendaServ::__dbgClearTzClientCacheL(TBool aRestartCaching)
       
  2290 	{
       
  2291 	CAgnTlsProxy* agntlsproxy = CAgnTlsProxy::CreateL(CAgnTlsProxy::TAgnTlsTzRulesType_Client);
       
  2292 	agntlsproxy->__dbgClearTzClientCacheL(aRestartCaching);
       
  2293 	CAgnTlsProxy::Release(agntlsproxy);
       
  2294 	}
       
  2295 
       
  2296 
       
  2297 //Cleanup Callback function which deletes CAgnSortEntry * using CAgnSorEntryAllocator.
       
  2298 void TCleanSimpleEntry::DestroySimpleEntry(TAny* aPtr)
       
  2299 	{
       
  2300 	TCleanSimpleEntry* cleanSimpleEntry = static_cast<TCleanSimpleEntry*> (aPtr);
       
  2301 	cleanSimpleEntry->GetSimpleEntryAllocator().Delete(cleanSimpleEntry->GetSimpleEntry());
       
  2302 	}
       
  2303 
       
  2304 TCleanSimpleEntry::TCleanSimpleEntry(CAgnSimpleEntry& aSimpleEntry, CAgnSimpleEntryAllocator& aAlloc)
       
  2305 				: iSimpleEntry(aSimpleEntry),iAlloc(aAlloc)
       
  2306 	{ 
       
  2307 	}
       
  2308 
       
  2309 CAgnSimpleEntry* TCleanSimpleEntry::GetSimpleEntry()
       
  2310 	{
       
  2311 	return &iSimpleEntry;	
       
  2312 	}
       
  2313 
       
  2314 CAgnSimpleEntryAllocator& TCleanSimpleEntry::GetSimpleEntryAllocator()
       
  2315 	{
       
  2316 	return iAlloc;	
       
  2317 	}
       
  2318 
       
  2319