pimappservices/calendar/client/src/calinstanceviewimpl.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "calclient.h"
       
    17 #include "agmentry.h"
       
    18 #include "agmutil.h"
       
    19 #include "calcommonimpl.h"
       
    20 #include "calentryimpl.h"
       
    21 #include <calinstance.h>
       
    22 #include <calsession.h>
       
    23 #include "calsessionimpl.h"
       
    24 #include "agmdebug.h"
       
    25 #include "agmdate.h"
       
    26 #include "calinstanceiteratorall.h"
       
    27 #include "calinstanceiteratoruid.h"
       
    28 #include "agmallocator.h"
       
    29 #ifdef SYMBIAN_CALENDAR_ENHANCEDSEARCHANDSORT
       
    30 #include "calsortedinstanceiterator.h"
       
    31 #endif
       
    32 #include <s32strm.h>
       
    33 
       
    34 #ifdef SYMBIAN_SKIPPED_CALENDAR_ALARMS
       
    35 #include <asshdalarm.h>
       
    36 #endif
       
    37 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    38 #include <asshdalarmcal.h>
       
    39 #endif //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    40 
       
    41 CCalInstanceViewImpl* CCalInstanceViewImpl::NewL(CCalSession& aSession, MCalProgressCallBack* aProgressCallBack)
       
    42 	{
       
    43 	CCalInstanceViewImpl* self = new (ELeave) CCalInstanceViewImpl(aSession.Impl().Server());
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL(aProgressCallBack, aSession);
       
    46 	CleanupStack::Pop(self);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 CCalInstanceViewImpl::CCalInstanceViewImpl(RAgendaServ&	aAgendaServer)
       
    51 	:iAgendaServer(aAgendaServer)
       
    52 	{
       
    53 	}
       
    54 
       
    55 void CCalInstanceViewImpl::ConstructL(MCalProgressCallBack* aProgressCallBack, CCalSession& aSession)
       
    56 	{
       
    57 	iCalSessions.AppendL(&(aSession.Impl()));
       
    58 	aSession.Impl().IncrementReferenceCount();
       
    59 	if (aProgressCallBack)
       
    60 		{
       
    61 		iCalSessions[0]->AsyncTaskManager().StartBuildIndexL(*aProgressCallBack);	
       
    62 		}
       
    63 	else if (! iCalSessions[0]->AsyncTaskManager().IsBusy() || iCalSessions[0]->AsyncTaskManager().BuildingIndex())
       
    64 		{//If it is not busy or it is building index, we will ask the server to build index. Otherwise, e.g. the server is doing filtering category, we will do nothing because index must have been built already.
       
    65 		iAgendaServer.WaitUntilLoadedL(iCalSessions[0]->CollectionId());
       
    66 		}
       
    67 	}
       
    68 
       
    69 CCalInstanceViewImpl* CCalInstanceViewImpl::NewL(RPointerArray<CCalSession>& aSessions, MCalProgressCallBack* aProgressCallBack)
       
    70 	{
       
    71 	CCalInstanceViewImpl* self = new (ELeave) CCalInstanceViewImpl(aSessions[0]->Impl().Server());
       
    72 	CleanupStack::PushL(self);
       
    73 	self->ConstructL(aSessions, aProgressCallBack);
       
    74 	CleanupStack::Pop(self);
       
    75 	return self;
       
    76 	}
       
    77 void CCalInstanceViewImpl::ConstructL(RPointerArray<CCalSession>& aSessions, MCalProgressCallBack* aProgressCallBack)
       
    78 	{
       
    79 	TInt count = aSessions.Count();
       
    80 	__ASSERT_ALWAYS(count >0, User::Leave(KErrNotFound));
       
    81 	
       
    82 	for (TInt i=0; i<count; ++i)
       
    83 		{
       
    84 		aSessions[i]->Impl().Server();
       
    85 		__ASSERT_ALWAYS(&(aSessions[i]->Impl().Server())==&iAgendaServer, CalUtils::Panic(EInvalidServerHandle));
       
    86 		iCalSessions.AppendL(&(aSessions[i]->Impl()));
       
    87 		iCalSessions[i]->IncrementReferenceCount();
       
    88 	
       
    89 		if (!aProgressCallBack && (! iCalSessions[i]->AsyncTaskManager().IsBusy() || iCalSessions[i]->AsyncTaskManager().BuildingIndex()))
       
    90 			{
       
    91 			iAgendaServer.WaitUntilLoadedL(iCalSessions[i]->CollectionId());
       
    92 			}
       
    93 		}
       
    94 	if (aProgressCallBack)
       
    95 		{
       
    96 		if(count == 1)
       
    97 			{
       
    98 			iCalSessions[0]->AsyncTaskManager().StartBuildIndexL(*aProgressCallBack);
       
    99 			}
       
   100 		else
       
   101 			{
       
   102 			iCurrentFileToOpen = 0;
       
   103 			iCalProgressCallBack = aProgressCallBack;
       
   104 			iCalSessions[iCurrentFileToOpen]->AsyncTaskManager().StartBuildIndexL(*this);
       
   105 			}
       
   106 		}
       
   107 	}
       
   108 
       
   109 void CCalInstanceViewImpl::Progress(TInt aPercentageCompleted)
       
   110 	{
       
   111 	iCalProgressCallBack->Progress((aPercentageCompleted + iCurrentFileToOpen*100)/iCalSessions.Count());
       
   112 	}
       
   113 
       
   114 void CCalInstanceViewImpl::Completed(TInt aError)
       
   115 	{
       
   116 	//if current file is not the last one, start to build the next file
       
   117 	//otherwise complete aProgressCallBack  
       
   118 	if(++iCurrentFileToOpen < iCalSessions.Count() && aError == KErrNone)
       
   119 		{
       
   120 		TRAPD(err, iCalSessions[iCurrentFileToOpen]->AsyncTaskManager().StartBuildIndexL(*this));
       
   121 		if(err!= KErrNone)
       
   122 			{
       
   123 			iCalProgressCallBack->Completed(err);
       
   124 			}
       
   125 		}
       
   126 	else
       
   127 		{
       
   128 		iCalProgressCallBack->Completed(aError);
       
   129 		}
       
   130 	}
       
   131 
       
   132 TBool CCalInstanceViewImpl::NotifyProgress()
       
   133 	{
       
   134 	//get from aProgressCallBack
       
   135 	return iCalProgressCallBack->NotifyProgress();
       
   136 	}
       
   137 
       
   138 CCalSessionImpl& CCalInstanceViewImpl::CalSessionImpl() const
       
   139 	{//This method is not needed after multiple calendar support. However, we have to provide CCalSessionImpl for BC reason (CCalInstanceIterator::iSessionImpl)
       
   140 	return *iCalSessions[0];
       
   141 	}
       
   142 
       
   143 CCalInstanceViewImpl::~CCalInstanceViewImpl()
       
   144 	{
       
   145 	const TInt count = iCalSessions.Count();
       
   146 	for(TInt ii =0; ii< count; ++ii)
       
   147 		{
       
   148 		if(iCalSessions[ii]->AsyncTaskManager().BuildingIndex())
       
   149 			{
       
   150 			iCalSessions[ii]->AsyncTaskManager().CancelAsynTask();
       
   151 			}
       
   152 		iCalSessions[ii]->DecrementReferenceCount();
       
   153 		}
       
   154 
       
   155 	iCalSessions.Close();
       
   156 	}
       
   157 	
       
   158 void CCalInstanceViewImpl::FindInstanceL(RPointerArray<CCalInstance>& aInstanceList, 
       
   159 										 CalCommon::TCalViewFilter aCalViewFilter, 
       
   160 										 const CalCommon::TCalTimeRange& aTimeRange) const
       
   161 	{
       
   162 	CCalInstanceView::TCalSearchParams params(KNullDesC(), CalCommon::EFoldedTextSearch);
       
   163 	FindInstanceL(aInstanceList, aCalViewFilter, aTimeRange, params);
       
   164 	}
       
   165 
       
   166 void CCalInstanceViewImpl::DeleteL(CCalInstance* aInstance, CalCommon::TRecurrenceRange aWhichInstances)
       
   167 	{
       
   168 	__ASSERT_ALWAYS(aInstance, CalUtils::Panic(ENullInstanceParameter));
       
   169 	
       
   170 	CAgnEntry* entryToDelete = aInstance->Entry().Impl()->GetFullEntryL();
       
   171 	if (entryToDelete)
       
   172 		{
       
   173 		TAgnCalendarTime instanceTime = CalUtils::TCalTimeToTAgnCalendarTimeL(aInstance->Time());
       
   174 		DeleteInstanceL(*entryToDelete, instanceTime, aWhichInstances);
       
   175 		}
       
   176 	
       
   177 	delete aInstance;
       
   178 	}
       
   179 
       
   180 void CCalInstanceViewImpl::DeleteInstanceL(CAgnEntry& aEntry, const TAgnCalendarTime& aInstanceTime, CalCommon::TRecurrenceRange aWhichInstances)
       
   181 	{
       
   182 	if (aEntry.EntryId().IsNullId() || (aEntry.RptDef() && !aEntry.RptDef()->IsAnInstanceL(aInstanceTime.LocalL())))
       
   183 		{
       
   184 		#if defined (__CAL_INSTANCE_LOGGING__) || (__CAL_VERBOSE_LOGGING__)
       
   185 			{
       
   186 			if(aEntry.EntryId().IsNullId())
       
   187 				{
       
   188 				_DBGLOG_INSTANCE(AgmDebug::DebugLog("Panic: EAgmErrInvalidInstance - Attempting to delete an instance where the entry Id is NULL");)	
       
   189 				}
       
   190 			else if (aEntry.RptDef() && !aEntry.RptDef()->IsAnInstanceL(aInstanceTime.LocalL()))
       
   191 				{
       
   192 				_DBGLOG_INSTANCE(AgmDebug::DebugLog("Panic: EAgmErrInvalidInstance - Attempting to delete an instance which is NOT part of the repeat definition");)
       
   193 				
       
   194 				TBuf<KMinTTimeStrLength> instTimeBuf;
       
   195 				AgmDebug::TTimeStrL(aInstanceTime.LocalL(), instTimeBuf);
       
   196 				_DBGLOG_INSTANCE(AgmDebug::DebugLog("Panic: EAgmErrInvalidInstance - Attempting to delete an instance which is NOT part of the repeat definition. Instance local time = %S", &instTimeBuf);)
       
   197 				}
       
   198 			
       
   199 			_DBGLOG_BASIC(AgmDebug::DebugLog("Panic: EAgmErrInvalidInstance - Attempting to delete an invalid instance");)
       
   200 			}
       
   201 		#endif
       
   202 		DBG_PANIC(EAgmErrInvalidInstance);
       
   203 		return;
       
   204 		}
       
   205 		
       
   206 	TCalLocalUid uniqId = aEntry.LocalUid();
       
   207 	CAgnRptDef* rptDef = NULL;
       
   208 	TBool isRepeating = EFalse;
       
   209 	if (aEntry.RptDef())
       
   210 		{
       
   211 		isRepeating = ETrue;
       
   212 		rptDef = CAgnRptDef::NewL(aEntry);
       
   213 		CleanupStack::PushL(rptDef);
       
   214 		rptDef->CopyL(*aEntry.RptDef());
       
   215 		}
       
   216 	const TAgnCalendarTime& KStart = aEntry.StartTime();
       
   217 	const TAgnCalendarTime& KEnd = aEntry.EndTime();
       
   218 
       
   219 	TRAPD(ret,DoDeleteInstanceL(aEntry, aInstanceTime, aWhichInstances));
       
   220 	if (ret == KErrNone)
       
   221 		{
       
   222 		TRAP(ret, iAgendaServer.CommitL(aEntry.CollectionId()));
       
   223 		if (ret != KErrNone)
       
   224 			{
       
   225 			iAgendaServer.Rollback(aEntry.CollectionId());
       
   226 			}
       
   227 		}
       
   228 	if (ret != KErrNone)
       
   229 		{
       
   230 		// restore start date, end date and repeat definition
       
   231 		aEntry.SetStartAndEndTimeL(KStart, KEnd);
       
   232 
       
   233 		if (isRepeating)
       
   234 			{
       
   235 			aEntry.SetRptDefL(*rptDef);
       
   236 			CleanupStack::PopAndDestroy();
       
   237 			}
       
   238 		User::Leave(ret);
       
   239 		}
       
   240 	if (isRepeating)
       
   241 		CleanupStack::PopAndDestroy();
       
   242 	}
       
   243 /*
       
   244 @capability WriteUserData
       
   245 @capability ReadUserData
       
   246 */
       
   247 void CCalInstanceViewImpl::DoDeleteInstanceL(CAgnEntry& aEntry, const TAgnCalendarTime& aInstanceTime, CalCommon::TRecurrenceRange aWhichInstances)
       
   248 	{
       
   249 	if (aEntry.RptDef())
       
   250 		{
       
   251 		switch(aWhichInstances)
       
   252 			{
       
   253 		case CalCommon::EThisOnly:
       
   254 			{
       
   255 			// if adding exception to the entry would result in all its instances
       
   256 			// being excepted then delete from the store, otherwise add the exception and 
       
   257 			// update it in the store
       
   258 			if ( ! aEntry.HasChildren() && aEntry.RptDef()->Exceptions() && 
       
   259 				 aEntry.RptDef()->InstanceCountL() == aEntry.RptDef()->Exceptions()->Count()+1)
       
   260 				{
       
   261 				iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   262 				}
       
   263 			else
       
   264 				{
       
   265 				aEntry.RptDef()->AddExceptionL(aInstanceTime);
       
   266 				iAgendaServer.UpdateEntryL(aEntry, EFalse, aEntry.CollectionId());
       
   267 				}
       
   268 			break;
       
   269 			}
       
   270 
       
   271 		case CalCommon::EThisAndAll:
       
   272 			{
       
   273 			iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   274 			break;
       
   275 			}
       
   276 
       
   277 		case CalCommon::EThisAndFuture:
       
   278 			{
       
   279 			TTime previousInstance;
       
   280 			if ( ! aEntry.RptDef()->NudgePreviousUnexceptedInstanceL(aInstanceTime.LocalL(),previousInstance))
       
   281 				{
       
   282 				iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   283 				}
       
   284 			else
       
   285 				{
       
   286 				TAgnCalendarTime prevInst;
       
   287 				if (aEntry.TimeMode() == MAgnCalendarTimeMode::EFloating)
       
   288 					{
       
   289 					prevInst.SetFloatingL(previousInstance);
       
   290 					}
       
   291 				else
       
   292 					{
       
   293 					prevInst.SetLocalL(previousInstance);
       
   294 					}
       
   295 				if (aEntry.RptDef()->RRule())
       
   296 					{
       
   297 					aEntry.RptDef()->SetUntilTime(prevInst);
       
   298 					}
       
   299 				aEntry.RptDef()->PruneExceptionsL();
       
   300 				if (aEntry.RptDef()->InstanceCountL()==1)	 // see if there's just one instance left
       
   301 					{	
       
   302 					// if that one instance is an exception then delete the entry 		
       
   303 					if (aEntry.RptDef()->Exceptions())
       
   304 						{
       
   305 						iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   306 						}
       
   307 					// otherwise make the entry non-repeating
       
   308 					else
       
   309 						{
       
   310 						aEntry.ClearRepeat();
       
   311 						iAgendaServer.UpdateEntryL(aEntry, EFalse, aEntry.CollectionId());
       
   312 						}
       
   313 					}
       
   314 				else
       
   315 					iAgendaServer.UpdateEntryL(aEntry, EFalse, aEntry.CollectionId());
       
   316 				}
       
   317 			break;
       
   318 			}
       
   319 		case CalCommon::EThisAndPrior: 
       
   320 			{
       
   321 			TTime nextInstance;
       
   322 			if (!aEntry.RptDef()->NudgeNextInstanceL(aInstanceTime.LocalL(),nextInstance, ETrue))
       
   323 				iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   324 			else
       
   325 				{
       
   326 				aEntry.MoveStartTimeLocalL(nextInstance);
       
   327 				aEntry.RptDef()->PruneExceptionsL();
       
   328 				if (aEntry.RptDef()->InstanceCountL()==1)	 // see if there's just one instance left
       
   329 					{	
       
   330 					// if that one instance is an exception then delete the entry 		
       
   331 					if (aEntry.RptDef()->Exceptions())
       
   332 						{
       
   333 						iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   334 						}
       
   335 					// otherwise make the entry non-repeating
       
   336 					else
       
   337 						{
       
   338 						aEntry.ClearRepeat();
       
   339 						iAgendaServer.UpdateEntryL(aEntry, EFalse, aEntry.CollectionId());
       
   340 						}
       
   341 					}
       
   342 				else
       
   343 					iAgendaServer.UpdateEntryL(aEntry, EFalse, aEntry.CollectionId());
       
   344 				}
       
   345 			break;
       
   346 			}
       
   347 		default: 
       
   348 			{
       
   349 			_DBGLOG_BASIC(AgmDebug::DebugLog("Panic: EAgmErrInvalidRecurrenceRange - Invalid Recurrence Range");)
       
   350 			DBG_PANIC(EAgmErrInvalidRecurrenceRange);
       
   351 			}
       
   352 			}
       
   353 		}
       
   354 	else // the entry isn't repeating
       
   355 		iAgendaServer.DeleteEntryL(aEntry.EntryId(), aEntry.CollectionId());
       
   356 
       
   357 	}
       
   358 
       
   359 // This function creates CCalLiteEntries from a list of entry ids
       
   360 void CCalInstanceViewImpl::FetchLiteEntriesFromIdsL(RArray<TAgnInstance>& aEntryIds, RPointerArray<CCalLiteEntry>& aCalLiteEntries, TLinearOrder<CCalLiteEntry>* aOrder) const
       
   361 	{
       
   362 	RPointerArray<CAgnSimpleEntry> simpleEntries;
       
   363 	CleanupClosePushL(simpleEntries);
       
   364 	//for cleanup of the sortentries in the array if a leave happens, CleanupClosePushL is not enough.
       
   365 	//Cleanup function needs access to non-static member iCalSession so that it can use
       
   366 	//AgnEntryModel::DeleteLiteEntry to cleanup sortentries.
       
   367 	TCleanSimpleEntryArray cleanupSimpleEntryArray(simpleEntries, iAgendaServer);
       
   368 	CleanupStack::PushL(TCleanupItem(DestroySimpleEntryArray, &cleanupSimpleEntryArray));
       
   369 	
       
   370 	// first fetch the entries as CAgnSimpleEntries
       
   371 	iAgendaServer.GetSimpleEntryL(aEntryIds, simpleEntries);
       
   372 	__ASSERT_ALWAYS(aEntryIds.Count() == simpleEntries.Count(), User::Leave(KErrNotFound));
       
   373 
       
   374 	// create CCalLiteEntries from the CAgnSimpleEntries
       
   375 	// entries must be added to the CCalLiteEntry array in the same order they appeared in the other arrays
       
   376 	while (simpleEntries.Count() > 0)
       
   377 		{
       
   378 		CCalLiteEntry* calLiteEntry = CCalLiteEntry::NewL(*simpleEntries[0], iAgendaServer);
       
   379 		calLiteEntry->IncrementRefCount();
       
   380 		simpleEntries.Remove(0); // CCalLiteEntry has taken ownership of CAgnSimpleEntry
       
   381 		TInt err = 0;
       
   382 		if(aOrder)
       
   383 			{
       
   384 			err = aCalLiteEntries.InsertInOrder(calLiteEntry, *aOrder);	
       
   385 			}
       
   386 		else
       
   387 			{
       
   388 			err = aCalLiteEntries.Append(calLiteEntry);	
       
   389 			}
       
   390 
       
   391 		if (err != KErrNone)
       
   392 			{
       
   393 			calLiteEntry->DecrementRefCount(); // deletes calLiteEntry
       
   394 			User::Leave(err);
       
   395 			}
       
   396 		}
       
   397 	
       
   398 	CleanupStack::Pop(&cleanupSimpleEntryArray);
       
   399 	CleanupStack::PopAndDestroy(&simpleEntries); // Close()
       
   400 	}
       
   401 
       
   402 CArrayFixSeg<TAgnInstance>* CCalInstanceViewImpl::FindInstanceIdL(const CCalFindInstanceSettings& aCalFindInstanceSettings) const
       
   403 	{
       
   404 	aCalFindInstanceSettings.Filter();
       
   405 	const TInt KCalMaxSearchStringLength = 32;
       
   406 	if (aCalFindInstanceSettings.Text() != KNullDesC && aCalFindInstanceSettings.Text().Length() > KCalMaxSearchStringLength)
       
   407 		{
       
   408 		User::Leave(KErrArgument);
       
   409 		}
       
   410 	
       
   411 	
       
   412 	TAgnFilter agnFilter(aCalFindInstanceSettings.Filter(), aCalFindInstanceSettings.Behaviour());
       
   413 	
       
   414 	TFindInstanceParams params;
       
   415 	params.iFilter = agnFilter;
       
   416 	params.iUndatedTodoTimeLocal.HomeTime();
       
   417 	params.iRangeStart = CalUtils::TCalTimeToTAgnCalendarTimeL(aCalFindInstanceSettings.TimeRange().StartTime());
       
   418 	params.iRangeEnd = CalUtils::TCalTimeToTAgnCalendarTimeL(aCalFindInstanceSettings.TimeRange().EndTime());
       
   419 	params.iSearchString.Copy(aCalFindInstanceSettings.Text());
       
   420 	
       
   421 	CArrayFixSeg<TAgnInstance>* instances = new (ELeave) CArrayFixSeg<TAgnInstance>(16);
       
   422  	CleanupStack::PushL(instances);
       
   423  
       
   424 	// Find the instances
       
   425  	RArray<TInt64> fileIds;
       
   426  	GetFileIdLC(fileIds);
       
   427   	iAgendaServer.FindInstancesL(fileIds,*instances, params);
       
   428  	CleanupStack::PopAndDestroy(&fileIds);
       
   429  	CleanupStack::Pop(instances);
       
   430  	return instances;
       
   431  	}
       
   432 
       
   433 CCalSessionImpl* CCalInstanceViewImpl::FindSession(TCalCollectionId aCallectionId) const
       
   434 	{
       
   435 	const TInt count = iCalSessions.Count();
       
   436 	for(TInt ii = 0; ii< count; ++ii)
       
   437 		{
       
   438 		if(iCalSessions[ii]->CollectionId() == aCallectionId)
       
   439 			{
       
   440 			return iCalSessions[ii];
       
   441 			}
       
   442 		}
       
   443 	return NULL;
       
   444 	}
       
   445  	
       
   446  CCalInstance* CCalInstanceViewImpl::CreateInstanceFromLiteEntryL(CCalLiteEntry& aLiteEntry, TAgnInstanceId& aInstanceId, TBool aCheckInstanceTime) const
       
   447  	{
       
   448  	CCalSessionImpl* session = FindSession(aLiteEntry.LiteEntry().CollectionId());
       
   449 	__ASSERT_ALWAYS(session, User::Leave(KErrNotFound));
       
   450  	CCalEntryImpl* calEntryImpl = CCalEntryImpl::NewL(aLiteEntry, *session);
       
   451 	CleanupStack::PushL(calEntryImpl);
       
   452 	CCalEntry* calEntry = CCalEntry::NewL(calEntryImpl);
       
   453 	CleanupStack::Pop(calEntryImpl);
       
   454 	
       
   455 	CleanupStack::PushL(calEntry);
       
   456 	TCalTime instanceTime = CalUtils::TAgnCalendarTimeToTCalTimeL(aInstanceId.Date());
       
   457 
       
   458 	CCalInstance* calInstance = NULL;
       
   459 	TBool instanceTimeChanged = EFalse;
       
   460 
       
   461 	if (aCheckInstanceTime)
       
   462 		{// When instances are fetched using iterator, the instance ids is cached at the begining when the iterator is created. Therefore, the entry might has been changed by the time the instance is returned to the user.
       
   463 		const CAgnSimpleEntry& KSimpleEntry = aLiteEntry.LiteEntry();
       
   464  		const CAgnRptDef* KRepeating = KSimpleEntry.RptDef();
       
   465  		if(KRepeating)
       
   466 	 		{
       
   467 	 		instanceTimeChanged = ! KRepeating->IsAnInstanceL(instanceTime.TimeLocalL());	
       
   468 	 		}
       
   469 	 	else //compare the instance time with the entry's start\end time depends on it is a todo or not
       
   470 	 		{	
       
   471 	 		instanceTimeChanged = KSimpleEntry.EntryTime() != aInstanceId.Date();
       
   472 	 		}
       
   473  		}
       
   474 
       
   475 	if (instanceTimeChanged)
       
   476 		{
       
   477 		User::Leave(KErrNotFound);	
       
   478 		}
       
   479 	else
       
   480 		{
       
   481 		calInstance= CCalInstance::NewL(calEntry, instanceTime);
       
   482 		CleanupStack::Pop(calEntry);
       
   483 		}
       
   484 	return 	calInstance;	
       
   485  	}
       
   486  TInt CCalInstanceViewImpl::CompareInstanceId(const TAgnInstance& aLeft, const TAgnInstance& aRight)
       
   487  //
       
   488  // Compare two entries - used when sorting entries in the array
       
   489  //
       
   490  	{
       
   491 
       
   492  	if ( aLeft.iId.Value() == aRight.iId.Value() &&  aLeft.iCollectionId == aRight.iCollectionId)
       
   493  		{
       
   494  		return 0;
       
   495  		}
       
   496 
       
   497  	if(aLeft.iId.Value() < aRight.iId.Value())
       
   498  		{
       
   499  		return -1;
       
   500  		}
       
   501  	else if(aLeft.iId.Value() > aRight.iId.Value())
       
   502  		{
       
   503  		return 1;
       
   504  		}
       
   505  	else
       
   506  		{
       
   507  		return aLeft.iCollectionId  < aRight.iCollectionId ? -1 : 1;
       
   508  		}
       
   509   	}
       
   510 
       
   511 void CCalInstanceViewImpl::FindInstanceL(RPointerArray<CCalInstance>& aMatchedInstanceList, 
       
   512 										 CalCommon::TCalViewFilter aCalViewFilter, 
       
   513 										 const CalCommon::TCalTimeRange& aTimeRange, 
       
   514 										 const CCalInstanceView::TCalSearchParams& aSearchParams) const
       
   515 	{
       
   516  	CCalFindInstanceSettings* findSetting = CCalFindInstanceSettings::NewL(aCalViewFilter, aTimeRange);
       
   517  	CleanupStack::PushL(findSetting);
       
   518  	findSetting->SetTextSearchL(aSearchParams);
       
   519  	CArrayFixSeg<TAgnInstance>* instances = FindInstanceIdL(*findSetting);
       
   520  	CleanupStack::PushL(instances);
       
   521  	FindInstancesFromIdsL(instances, aMatchedInstanceList); 	
       
   522 	CleanupStack::PopAndDestroy(2, findSetting);//findSetting, instances
       
   523 	}
       
   524 
       
   525 void CCalInstanceViewImpl::FindInstancesFromIdsL(CArrayFixSeg<TAgnInstance>* aInstanceIds, RPointerArray<CCalInstance>& aInstanceArray) const
       
   526 	{
       
   527 	if (aInstanceIds->Count() > 0)
       
   528 		{
       
   529 		RArray<TAgnInstance> liteEntryIdsToFetch; // array of entry IDs
       
   530 		CleanupClosePushL(liteEntryIdsToFetch);
       
   531 	
       
   532 		// Create a list of all entry IDs in the set of instances
       
   533 		const TInt KInstancesCount(aInstanceIds->Count());
       
   534 		TLinearOrder<TAgnInstance> order(CompareInstanceId);
       
   535 		for (TInt i(0); i < KInstancesCount ; ++i)
       
   536 			{
       
   537 			TInt err = liteEntryIdsToFetch.InsertInOrder((*aInstanceIds)[i], order); // doesn't allow repeats
       
   538 			if (err != KErrAlreadyExists)
       
   539 				{
       
   540 				User::LeaveIfError(err);
       
   541 				}
       
   542 			}
       
   543 		
       
   544 		RPointerArray<CCalLiteEntry> calLiteEntries; // array of light entries
       
   545 		CleanupStack::PushL(TCleanupItem(CleanCalLiteEntries, &calLiteEntries));
       
   546 		
       
   547 		// Fetch the light entry for each entry ID
       
   548 		FetchLiteEntriesFromIdsL(liteEntryIdsToFetch, calLiteEntries, NULL);
       
   549 		const TInt KCalEntryCount = calLiteEntries.Count();
       
   550 		
       
   551 		// array of light entries must match array of entry IDs
       
   552 		__ASSERT_ALWAYS(liteEntryIdsToFetch.Count() == KCalEntryCount, User::Leave(KErrNotFound));
       
   553 		
       
   554 		for (TInt i(0); i < KInstancesCount ; ++i)
       
   555 	 		{
       
   556 	 		// for each instance, find the index of the entry id
       
   557 	 		TInt index = liteEntryIdsToFetch.FindInOrder((*aInstanceIds)[i], order);
       
   558 	  		User::LeaveIfError(index);
       
   559 			
       
   560  			// Use the index to find the CCalLiteEntry for this instance and create the CCalInstance from it
       
   561 			CCalInstance* calInstance = CreateInstanceFromLiteEntryL(*calLiteEntries[index], (*aInstanceIds)[i].iId, EFalse);
       
   562 			CleanupStack::PushL(calInstance);
       
   563 			aInstanceArray.AppendL(calInstance);
       
   564 			CleanupStack::Pop(calInstance);
       
   565 			}	
       
   566 	
       
   567 		CleanupStack::PopAndDestroy(&calLiteEntries);
       
   568 		CleanupStack::PopAndDestroy(&liteEntryIdsToFetch);
       
   569  		}
       
   570 	}
       
   571 
       
   572 /**
       
   573 */
       
   574 CCalInstanceIterator* CCalInstanceViewImpl::FindInstanceL(const CCalFindInstanceSettings& aSettings) const
       
   575 	{
       
   576 #ifdef SYMBIAN_CALENDAR_ENHANCEDSEARCHANDSORT
       
   577 	if(DefaultSortOnly(aSettings))
       
   578 		{
       
   579 		return CCalInstanceIteratorAll::NewL(aSettings, *this);
       
   580 		}
       
   581 	else
       
   582 		{
       
   583 		return CCalSortedInstanceIterator::NewL(aSettings, *this);
       
   584 		}
       
   585 #else
       
   586 	return CCalInstanceIteratorAll::NewL(aSettings, *this);
       
   587 #endif
       
   588 	}
       
   589 
       
   590 CCalInstanceIterator* CCalInstanceViewImpl::FindInstanceL(const CCalFindInstanceSettings& aSettings, TCalLocalUid aLocalUid, const TCalTime& aStartInstanceTime) const
       
   591 	{
       
   592 	__ASSERT_ALWAYS(iCalSessions.Count()==1, CalUtils::Panic(EAmbiguousInstance));
       
   593 	return DoFindInstanceL(aSettings, aLocalUid, aStartInstanceTime, iCalSessions[0]->CollectionId());
       
   594 	}
       
   595 
       
   596 CCalInstanceIterator* CCalInstanceViewImpl::FindInstanceL(const CCalFindInstanceSettings& aSettings, TCalInstanceId& aInstanceId) const
       
   597 	{
       
   598 	return DoFindInstanceL(aSettings, aInstanceId.iEntryLocalId, aInstanceId.iInstanceTime, aInstanceId.iCollectionId);
       
   599 	}
       
   600 
       
   601 CCalInstanceIterator* CCalInstanceViewImpl::DoFindInstanceL(const CCalFindInstanceSettings& aSettings, TCalLocalUid aLocalUid, const TCalTime& aStartInstanceTime, TUint8 aShortFileId) const
       
   602 	{
       
   603 #ifdef SYMBIAN_CALENDAR_ENHANCEDSEARCHANDSORT
       
   604 	if(DefaultSortOnly(aSettings))
       
   605 		{
       
   606 		return CCalInstanceIteratorAll::NewL(aSettings, *this, aLocalUid, aStartInstanceTime, aShortFileId);
       
   607 		}
       
   608 	else
       
   609 		{
       
   610 		return CCalSortedInstanceIterator::NewL(aSettings, *this, aLocalUid, aStartInstanceTime, aShortFileId);
       
   611 		}
       
   612 #else
       
   613 	return CCalInstanceIteratorAll::NewL(aSettings, *this, aLocalUid, aStartInstanceTime, aShortFileId);	
       
   614 #endif
       
   615 	}
       
   616 
       
   617 
       
   618 #ifdef SYMBIAN_CALENDAR_ENHANCEDSEARCHANDSORT
       
   619 TBool CCalInstanceViewImpl::DefaultSortOnly(const CCalFindInstanceSettings& aSettings)
       
   620 	{
       
   621 	//Use the default sort if no sort criteria is found.
       
   622 	return (0 == aSettings.SortCriteria().Count());
       
   623 	}
       
   624 #else
       
   625 TBool CCalInstanceViewImpl::DefaultSortOnly(const CCalFindInstanceSettings& /*aSettings*/)
       
   626 	{
       
   627 	return ETrue;
       
   628 	}
       
   629 #endif
       
   630 
       
   631 CCalInstanceIterator* CCalInstanceViewImpl::FindInstanceByUidL(const TDesC8& aUid, const TCalTime& aInstanceTime) const
       
   632 	{
       
   633 	__ASSERT_ALWAYS(iCalSessions.Count()==1, CalUtils::Panic(EAmbiguousInstance));
       
   634 	return CCalInstanceIteratorUid::NewL(*this, aUid, aInstanceTime, iCalSessions[0]->CollectionId());
       
   635 	}
       
   636 
       
   637 CCalInstanceIterator* CCalInstanceViewImpl::FindInstanceByUidL(TCalCollectionId aCollectionId, const TDesC8& aUid, const TCalTime& aInstanceTime) const
       
   638 	{
       
   639 	return CCalInstanceIteratorUid::NewL(*this, aUid, aInstanceTime, aCollectionId);
       
   640 	}
       
   641 
       
   642 CCalInstance* CCalInstanceViewImpl::FindInstanceL(TCalLocalUid aLocalId, const TCalTime& aInstanceTime) const
       
   643 	{
       
   644 	__ASSERT_ALWAYS(iCalSessions.Count()==1, CalUtils::Panic(EAmbiguousInstance));
       
   645 	return DoFindInstanceL(aLocalId, aInstanceTime, iCalSessions[0]->CollectionId());
       
   646 	}
       
   647 
       
   648 CCalInstance* CCalInstanceViewImpl::DoFindInstanceL(TCalLocalUid aLocalId, const TCalTime& aInstanceTime, TUint8 aShortFileId) const
       
   649 	{
       
   650 	CAgnSimpleEntry* simpleEntry = iAgendaServer.GetSimpleEntryL(aShortFileId, aLocalId);
       
   651  	TCleanSimpleEntry cleanSimpleEntry(*simpleEntry, iAgendaServer.SimpleEntryAllocator());
       
   652 	TCleanupItem cleanupItem(TCleanSimpleEntry::DestroySimpleEntry, &cleanSimpleEntry);
       
   653 	CleanupStack::PushL(cleanupItem);
       
   654 	
       
   655 	// Leave if the entry cannot be found
       
   656 	__ASSERT_ALWAYS(simpleEntry, User::Leave(KErrNotFound));
       
   657 	
       
   658 	if (!IsValidInstanceL(*simpleEntry, aInstanceTime))
       
   659 		{
       
   660 		User::Leave(KErrNotFound);
       
   661 		}
       
   662 	
       
   663 	TCalTime instanceTime(aInstanceTime);
       
   664 	
       
   665 	if ( ( !simpleEntry->EndTime().IsSet() ) && ( simpleEntry->CompletedDateUtc() == Time::NullTTime() ) )
       
   666 		{
       
   667 		// if the entry is an undated todo then we should use 'now' as the instance time
       
   668 		TTime now;
       
   669 		now.HomeTime();
       
   670 		instanceTime.SetTimeLocalL(now);
       
   671 		}
       
   672 
       
   673 	CCalLiteEntry* calLiteEntry = CCalLiteEntry::NewL(*simpleEntry, iAgendaServer);
       
   674 	CleanupStack::Pop(); // simpleEntry
       
   675 	
       
   676 	calLiteEntry->IncrementRefCount();
       
   677 	
       
   678 	CCalEntryImpl* calEntryImpl(NULL);
       
   679 	CCalSessionImpl* session = FindSession(calLiteEntry->LiteEntry().CollectionId());
       
   680 	__ASSERT_ALWAYS(session, User::Leave(KErrNotFound));
       
   681 	TRAPD(err, calEntryImpl = CCalEntryImpl::NewL(*calLiteEntry, *session) );
       
   682 	
       
   683 	calLiteEntry->DecrementRefCount();
       
   684 	
       
   685 	if (err != KErrNone)
       
   686 		{
       
   687 		User::Leave(err);
       
   688 		}
       
   689 	
       
   690 	CleanupStack::PushL(calEntryImpl);
       
   691 	CCalEntry* calEntry = CCalEntry::NewL(calEntryImpl);
       
   692 	CleanupStack::Pop(calEntryImpl);
       
   693 	CleanupStack::PushL(calEntry);
       
   694 	CCalInstance* instance = CCalInstance::NewL(calEntry, instanceTime);
       
   695 	CleanupStack::Pop(calEntry);
       
   696 	
       
   697 	return instance;
       
   698 	}
       
   699 
       
   700 CCalInstance* CCalInstanceViewImpl::FindInstanceL(TCalInstanceId& aInstanceId) const
       
   701 	{
       
   702 	return DoFindInstanceL(aInstanceId.iEntryLocalId,aInstanceId.iInstanceTime, aInstanceId.iCollectionId);
       
   703 	}
       
   704 
       
   705 #ifdef SYMBIAN_SKIPPED_CALENDAR_ALARMS
       
   706 void CCalInstanceViewImpl::FindAlarmedInstanceL(RPointerArray<CCalInstance>& aAlarmedInstanceList,    
       
   707 						  						TASShdAlarmedInstanceParams aAlarmedInstanceParams) const
       
   708 	{
       
   709 	TAgnFilter filter(CalCommon::EIncludeAll, CalCommon::EFoldedTextSearch);
       
   710 	filter.SetAlarmedInstanceSearch(ETrue);
       
   711 	filter.SetFloatingInstanceOnly(aAlarmedInstanceParams.iTimeType == EFloating);
       
   712 	
       
   713 	TFindInstanceParams findInstanceParams;
       
   714  	findInstanceParams.iFilter = filter;
       
   715  	
       
   716  	TAgnCalendarTime searchFromTime;
       
   717  	searchFromTime.SetLocalL(aAlarmedInstanceParams.iLocalStartTime);
       
   718  	findInstanceParams.iRangeStart = searchFromTime;
       
   719  	findInstanceParams.iRangeEnd.SetLocalL(aAlarmedInstanceParams.iLocalEndTime); 	
       
   720  	
       
   721  	const TInt KGranularity(5);
       
   722  	CArrayFixSeg<TAgnInstance>* instances = new(ELeave) CArrayFixSeg<TAgnInstance>(KGranularity);
       
   723  	CleanupStack::PushL(instances);
       
   724  	RArray<TInt64> fileIds;
       
   725  	GetFileIdLC(fileIds);
       
   726 	iAgendaServer.FindInstancesL(fileIds, *instances, findInstanceParams);
       
   727 	CleanupStack::PopAndDestroy(&fileIds);
       
   728 	FindInstancesFromIdsL(instances, aAlarmedInstanceList);
       
   729 	
       
   730 	CleanupStack::PopAndDestroy(instances);
       
   731 	}
       
   732 #endif
       
   733 void CCalInstanceViewImpl::GetFileIdLC(RArray<TInt64>& aFiles)const
       
   734 	{
       
   735 	CleanupClosePushL(aFiles);
       
   736 	const TInt count = iCalSessions.Count();
       
   737 	for (TInt ii=0; ii<count; ++ii)
       
   738 		{
       
   739 		aFiles.AppendL(iCalSessions[ii]->FileId());
       
   740 		}
       
   741 	}
       
   742 
       
   743 void CCalInstanceViewImpl::GetShortFileIdLC(RArray<TInt>& aFiles)const
       
   744 	{
       
   745 	CleanupClosePushL(aFiles);
       
   746 	const TInt count = iCalSessions.Count();
       
   747 	for (TInt ii=0; ii<count; ++ii)
       
   748 		{
       
   749 		User::LeaveIfError(aFiles.InsertInOrder(iCalSessions[ii]->CollectionId()));
       
   750 		}
       
   751 	}
       
   752 
       
   753 TCalTime CCalInstanceViewImpl::NextInstanceL(CalCommon::TCalViewFilter aCalViewFilter, const TCalTime& aStartDate) const
       
   754  	{
       
   755  	TFindInstanceParams findInstanceParams;
       
   756  	findInstanceParams.iFilter = TAgnFilter(aCalViewFilter, CalCommon::EFoldedTextSearch);
       
   757  	
       
   758  	TAgnCalendarTime searchFromTime;
       
   759  	searchFromTime.SetLocalL(AgnDateTime::ResetToMidnight(aStartDate.TimeLocalL()) + TTimeIntervalDays(1));
       
   760  	findInstanceParams.iInstance.iId.SetDate(searchFromTime); 
       
   761  	findInstanceParams.iInstance.iCollectionId = 0;
       
   762  	findInstanceParams.iUndatedTodoTimeLocal.HomeTime();
       
   763  	findInstanceParams.iRangeStart = searchFromTime;
       
   764  	findInstanceParams.iRangeEnd.SetUtcL(AgnDateTime::MaxDate());
       
   765  	findInstanceParams.iNumInstances = 1;
       
   766  	
       
   767  	CArrayFixSeg<TAgnInstance>* instances = new(ELeave) CArrayFixSeg<TAgnInstance>(1);
       
   768  	CleanupStack::PushL(instances);
       
   769 	
       
   770  	RArray<TInt64> fileIds;
       
   771  	GetFileIdLC(fileIds);
       
   772 	iAgendaServer.NextInstancesL(fileIds, *instances, findInstanceParams);
       
   773 	CleanupStack::PopAndDestroy(&fileIds);
       
   774 	TCalTime returnTime;
       
   775 	if (instances->Count() > 0)
       
   776 		{
       
   777 		returnTime = CalUtils::TAgnCalendarTimeToTCalTimeL((*instances)[0].iId.Date());
       
   778 		}
       
   779 	
       
   780 	CleanupStack::PopAndDestroy(instances);
       
   781 	
       
   782  	return returnTime;
       
   783 	}
       
   784 
       
   785 TCalTime CCalInstanceViewImpl::PreviousInstanceL(CalCommon::TCalViewFilter aCalViewFilter, const TCalTime& aStartDate) const
       
   786  	{
       
   787  	TFindInstanceParams findInstanceParams;
       
   788  	findInstanceParams.iFilter = TAgnFilter(aCalViewFilter, CalCommon::EFoldedTextSearch);
       
   789  	
       
   790  	TAgnCalendarTime searchFromTime;
       
   791  	// take off a microsecond because we don't want to get the instance that is at midnight on the day
       
   792  	searchFromTime.SetLocalL(AgnDateTime::ResetToMidnight(aStartDate.TimeLocalL()));
       
   793  	findInstanceParams.iInstance.iId.SetDate(searchFromTime); 
       
   794  	findInstanceParams.iInstance.iCollectionId = 0; 
       
   795  	findInstanceParams.iUndatedTodoTimeLocal.HomeTime();
       
   796  	findInstanceParams.iRangeStart.SetUtcL(AgnDateTime::MinDate());
       
   797  	findInstanceParams.iRangeEnd = searchFromTime;
       
   798  	findInstanceParams.iNumInstances = 1;
       
   799  	
       
   800  	CArrayFixSeg<TAgnInstance>* instances = new(ELeave) CArrayFixSeg<TAgnInstance>(1);
       
   801  	CleanupStack::PushL(instances);
       
   802  	RArray<TInt64> fileIds;
       
   803  	GetFileIdLC(fileIds);
       
   804  	iAgendaServer.PreviousInstancesL(fileIds, *instances, findInstanceParams);
       
   805 	CleanupStack::PopAndDestroy(&fileIds);
       
   806 	
       
   807 	TCalTime returnTime;
       
   808 	if (instances->Count() > 0)
       
   809 		{		
       
   810 		returnTime = CalUtils::TAgnCalendarTimeToTCalTimeL((*instances)[0].iId.Date());
       
   811 		}
       
   812 	
       
   813 	CleanupStack::PopAndDestroy(instances);
       
   814 	
       
   815  	return returnTime;
       
   816 	}
       
   817 
       
   818 /** A utility method to create an instance from an instance id
       
   819 
       
   820 @param aInstanceId the instance id to create an instance from
       
   821 @return The instance
       
   822 
       
   823 @internalComponent
       
   824 */
       
   825 CCalInstance* CCalInstanceViewImpl::CreateInstanceL(const TAgnInstance& aInstance) const
       
   826 	{
       
   827 	CAgnSimpleEntry* simpleEntry = iAgendaServer.GetSimpleEntryL(aInstance.iId, aInstance.iCollectionId);
       
   828 	TCleanSimpleEntry simpleEntryCleanupItem(*simpleEntry, iAgendaServer.SimpleEntryAllocator());
       
   829   	CleanupStack::PushL(TCleanupItem(TCleanSimpleEntry::DestroySimpleEntry, &simpleEntryCleanupItem));
       
   830 	CCalLiteEntry* liteEntry = CCalLiteEntry::NewL(*simpleEntry, iAgendaServer);
       
   831 	CleanupStack::Pop(); // &simpleEntry
       
   832 	CleanupStack::PushL(liteEntry);
       
   833 	CCalSessionImpl* session = FindSession(liteEntry->LiteEntry().CollectionId());
       
   834 	__ASSERT_ALWAYS(session, User::Leave(KErrNotFound));
       
   835 	CCalEntryImpl* entryImpl = CCalEntryImpl::NewL(*liteEntry, *session);
       
   836 	CleanupStack::Pop(liteEntry);
       
   837 	CleanupStack::PushL(entryImpl);
       
   838 	CCalEntry* entry = CCalEntry::NewL(entryImpl);
       
   839 	CleanupStack::Pop(entryImpl);
       
   840 	CleanupStack::PushL(entry);
       
   841 	CCalInstance* instance = CCalInstance::NewL(entry, CalUtils::TAgnCalendarTimeToTCalTimeL(aInstance.iId.Date()));
       
   842 	CleanupStack::Pop(entry);
       
   843 	return instance;
       
   844 	}
       
   845 
       
   846 /** A utility method to create an CCalInstance from its entry and instance time
       
   847 
       
   848 @param aEntry The entry that the instance belongs to
       
   849 @param aInstanceTime The instance time of the instance
       
   850 @return A CCalInstance object 
       
   851 
       
   852 @internalComponent
       
   853 */
       
   854 CCalInstance* CCalInstanceViewImpl::CreateInstanceL(CCalLiteEntry& aLiteEntry, const TCalTime& aInstanceTime) const
       
   855 	{	
       
   856 	CCalSessionImpl* session = FindSession(aLiteEntry.LiteEntry().CollectionId());
       
   857 	__ASSERT_ALWAYS(session, User::Leave(KErrNotFound));
       
   858 	CCalEntryImpl* calEntryImpl = CCalEntryImpl::NewL(aLiteEntry, *session);
       
   859 	CleanupStack::PushL(calEntryImpl);
       
   860 	CCalEntry* calEntry = CCalEntry::NewL(calEntryImpl);
       
   861 	CleanupStack::Pop(calEntryImpl);
       
   862 	CleanupStack::PushL(calEntry);
       
   863 	CCalInstance* instance = CCalInstance::NewL(calEntry, aInstanceTime);
       
   864 	CleanupStack::Pop(calEntry);
       
   865 	return instance;
       
   866 	}
       
   867 
       
   868 
       
   869 /** A utility method to find if an instance time is valid
       
   870 
       
   871 @param aEntry The entry that the instance belongs to
       
   872 @param aInstanceTime The instance time of the instance
       
   873 @return ETrue if the instance time is valid.  EFalse of the instance is invalid.
       
   874 
       
   875 @internalComponent
       
   876 */
       
   877 TBool CCalInstanceViewImpl::IsValidInstanceL(const CAgnSimpleEntry& aEntry, const TCalTime& aInstanceTime) const
       
   878 	{
       
   879 	TBool instanceExists(EFalse);
       
   880 	const CAgnRptDef* rptDef(aEntry.RptDef());
       
   881 	
       
   882 	if (rptDef)
       
   883 		{
       
   884 		if (rptDef->IsAnUnexceptedInstanceL(aInstanceTime.TimeLocalL()))
       
   885 			{
       
   886 			instanceExists = ETrue;
       
   887 			}
       
   888 		}
       
   889 	else
       
   890 		{
       
   891 		if (aEntry.Type() == CCalEntry::ETodo)
       
   892 			{
       
   893 			if (aEntry.CompletedDateUtc() != Time::NullTTime())
       
   894 				{
       
   895 				if (aEntry.CompletedDateUtc() == aInstanceTime.TimeUtcL())
       
   896 					{
       
   897 					// it is a completed todo and the completed date is correct
       
   898 					instanceExists = ETrue;
       
   899 					}
       
   900 				}
       
   901 			else if ( !aEntry.EndTime().IsSet() || ( aEntry.EndTime().LocalL() == aInstanceTime.TimeLocalL() ) )
       
   902 				{
       
   903 				// it is an undated todo or the instance time is correct
       
   904 				instanceExists = ETrue;
       
   905 				}
       
   906 			}
       
   907 		else
       
   908 			{
       
   909 			if (aEntry.StartTime().LocalL() == aInstanceTime.TimeLocalL())
       
   910 				{
       
   911 				instanceExists = ETrue;
       
   912 				}
       
   913 			}
       
   914 		}
       
   915 	
       
   916 	return instanceExists;
       
   917 	}
       
   918 
       
   919 
       
   920 //Cleanup Callback function which deletes CAgnSimpleEntries using CCalSessionImpl. 
       
   921 void CCalInstanceViewImpl::DestroySimpleEntryArray(TAny* aPtr)
       
   922     {
       
   923 	TCleanSimpleEntryArray* ptrCleanup = static_cast<TCleanSimpleEntryArray*> (aPtr);
       
   924 	const TInt KCount = ptrCleanup->GetCount();
       
   925 	TInt loop;
       
   926 	for(loop = 0;loop < KCount;++loop)
       
   927 		{
       
   928 		ptrCleanup->GetAgendaServ().DeleteSimpleEntry(ptrCleanup->GetSimpleEntryAt(loop));
       
   929 		}
       
   930 	ptrCleanup->CloseArray();
       
   931     }
       
   932 
       
   933 void CCalInstanceViewImpl::CleanCalLiteEntries(TAny* aPtr)
       
   934 	{
       
   935 	if (aPtr)
       
   936 		{
       
   937 		RPointerArray<CCalLiteEntry>* calLiteEntries = static_cast<RPointerArray<CCalLiteEntry>*>(aPtr);
       
   938 		const TInt count = calLiteEntries->Count();
       
   939 		for (TInt i = count - 1; i >= 0; --i)
       
   940 			{
       
   941 			(*calLiteEntries)[i]->DecrementRefCount(); // removes reference and deletes if this is the last one
       
   942 			calLiteEntries->Remove(i);
       
   943 			}
       
   944 		calLiteEntries->Reset();
       
   945 		}
       
   946 	}
       
   947 
       
   948 RAgendaServ& CCalInstanceViewImpl::GetServ() const
       
   949 	{
       
   950 	return iAgendaServer;
       
   951 	}
       
   952 	
       
   953 void CCalInstanceViewImpl::IncrementsessionReferenceCount()
       
   954 	{
       
   955 	const TInt count = iCalSessions.Count();
       
   956 	for(TInt ii=0; ii< count; ++ii)
       
   957 		{
       
   958 		iCalSessions[ii]->IncrementReferenceCount();
       
   959 		}
       
   960 	}
       
   961 void CCalInstanceViewImpl::DecrementSessionReferenceCount()
       
   962 	{
       
   963 	const TInt count = iCalSessions.Count();
       
   964 	for(TInt ii =0; ii< count; ++ii)
       
   965 		{
       
   966 		iCalSessions[ii]->DecrementReferenceCount();
       
   967 		}
       
   968 	}
       
   969 TCleanSimpleEntryArray::TCleanSimpleEntryArray(RPointerArray<CAgnSimpleEntry>& aSimpleEntryArray, RAgendaServ& aAgendaServ)
       
   970 	: iSimpleEntryArray(aSimpleEntryArray), iAgendaServer(aAgendaServ)
       
   971 	{
       
   972 	}
       
   973 
       
   974 CAgnSimpleEntry* TCleanSimpleEntryArray::GetSimpleEntryAt(TInt aPos)
       
   975 	{
       
   976 	return iSimpleEntryArray[aPos];
       
   977 	}
       
   978 
       
   979 RAgendaServ& TCleanSimpleEntryArray::GetAgendaServ()
       
   980 	{
       
   981 	return iAgendaServer;
       
   982 	}
       
   983 
       
   984 TInt TCleanSimpleEntryArray::GetCount() const
       
   985 	{
       
   986 	return iSimpleEntryArray.Count();
       
   987 	}
       
   988 	
       
   989 void TCleanSimpleEntryArray::CloseArray()
       
   990 	{
       
   991 	iSimpleEntryArray.Close();
       
   992 	}
       
   993