// -----------------------------------------------------------------------------
// CPIMAgnEventAdapter::ReadRepeatFromAgnL
// Reads entry's repeat details and converts them into PIM repeat rule.
// -----------------------------------------------------------------------------
//
void CPIMAgnEventAdapter::ReadRepeatFromAgnL(MPIMEventItem& aItem,
        CCalEntry& aEntry)
{
    JELOG2(EPim);
    MPIMRepeatRuleData* repeatRuleData = aItem.GetRepeat();

    TCalRRule agnRRule;
    if (aEntry.GetRRuleL(agnRRule))
    {
        repeatRuleData->clear();

        PIMRepeatRuleConverter::ConvertSupportedRepeatToPIML(*repeatRuleData,
                agnRRule);

        CopyExceptionDatesToPimL(aEntry, *repeatRuleData);

        // Anniv does not need to have an end date, so clear that field to be sure
        if (aEntry.EntryTypeL() == CCalEntry::EAnniv)
        {
            repeatRuleData->ClearFieldL(EPIMRepeatRuleEnd);
        }

        aItem.SetRepeating(ETrue);
    }
    else
    {
        repeatRuleData->clear();
        aItem.SetRepeating(EFalse);
    }
}
// -----------------------------------------------------------------------------
// CPIMAgnApptAdapter::ReadAgnToItemL
// Reads an Agenda Model Appointment item and converts it to framework PIM item.
// -----------------------------------------------------------------------------
//
void CPIMAgnApptAdapter::ReadAgnToItemL(MPIMEventItem& aItem, CCalEntry& aEntry)
{
    JELOG2(EPim);
    ReadFieldsFromAgnL(aItem, aEntry, KPIMSupportedApptFields,
                       KPIMSupportedApptFieldsCount);

    TCalRRule tempRepeat;
    if (aEntry.GetRRuleL(tempRepeat))
    {
        ReadRepeatFromAgnL(aItem, aEntry);
        aItem.GetRepeat()->BackupRepeatL();
    }
}
void CBadRRule::CreateToDoEntryL()
{
	RPointerArray<CCalEntry> entryarr ;	
	CleanupResetAndDestroyPushL(entryarr);	

	
	TInt num ;

	HBufC8* guid = KGUID1().AllocLC(); // ownership of guid gets transferred
	CCalEntry *entry = CCalEntry::NewL(CCalEntry::ETodo, guid, CCalEntry::EMethodNone, 0) ;
	CleanupStack::Pop(guid);	
	CleanupStack::PushL(entry) ;

	TCalTime startTime ;
	TCalTime endTime ;	
	startTime.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 8, 10, 30, 0, 0))) ;
	endTime.SetTimeUtcL(startTime.TimeUtcL() + TTimeIntervalDays(1)) ;
			
	entry->SetStartAndEndTimeL(startTime, endTime) ;

	TCalTime endTime1 ;	
	endTime1.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 23, 10, 5, 0, 0))) ;		


	TCalRRule rrule(TCalRRule::EWeekly) ;
	
	rrule.SetDtStart(startTime);
	rrule.SetUntil(endTime1);
	rrule.SetInterval(1);	
	
	RArray<TDay> days;
	CleanupClosePushL(days);	
	days.AppendL(ETuesday);
	rrule.SetByDay(days);
	CleanupStack::PopAndDestroy(&days);

	entry->SetRRuleL(rrule) ; 
	// both entry and rrule start time should be aligned to the first instance
	TCalRRule setRRule ;
	entry->GetRRuleL(setRRule) ;
	
	test(setRRule.DtStart().TimeUtcL() == rrule.DtStart().TimeUtcL()) ;
	entryarr.AppendL(entry) ;
	CleanupStack::Pop(entry);		

	iCalEntryView->StoreL(entryarr, num) ;	
	entryarr.ResetAndDestroy();

	TCalTime recId;
	recId.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 9, 10, 30, 0, 0))); // creating recurrence id

	guid = KGUID1().AllocLC(); // ownership of guid gets transferred
	entry = CCalEntry::NewL(CCalEntry::ETodo, guid, CCalEntry::EMethodNone, 0, recId, CalCommon::EThisOnly);
	CleanupStack::Pop(guid);			
	CleanupStack::PushL(entry) ;	

	TCalTime startTime3 ;
	TCalTime endTime3 ;	
	startTime3.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 7, 10, 2, 0, 0))) ;
	endTime3.SetTimeUtcL(startTime3.TimeUtcL() + TTimeIntervalHours(1)) ;
	entry->SetStartAndEndTimeL(startTime3, endTime3) ;
	
	entryarr.AppendL(entry) ;
	CleanupStack::Pop(entry) ;
	
	iCalEntryView->StoreL(entryarr, num) ;		
	CleanupStack::PopAndDestroy(&entryarr);
	
	RArray<TTime> insTimes ;

	insTimes.Append(TTime(TDateTime(2006, EJanuary, 7, 11, 2, 0, 0))) ;
	insTimes.Append(TTime(TDateTime(2006, EJanuary, 16, 10, 30, 0, 0))) ;

	CleanupClosePushL(insTimes) ;
	CheckInstancesL(insTimes) ; 
	CleanupStack::PopAndDestroy(&insTimes) ;
			// time portion of repeat's end eliminates the third instance

}
void CBadRRule::CreateParentL()
{
	RPointerArray<CCalEntry> entryarr ;	
	CleanupResetAndDestroyPushL(entryarr);	

	TInt num ;

	HBufC8* guid = KGUID().AllocLC(); // ownership of guid gets transferred
	CCalEntry *entry = CCalEntry::NewL(CCalEntry::EEvent, guid, CCalEntry::EMethodNone, 0) ;
	CleanupStack::Pop(guid);	
	CleanupStack::PushL(entry) ;

	TCalTime startTime ;
	TCalTime endTime ;	
	startTime.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 7, 10, 1, 0, 0))) ;
	endTime.SetTimeUtcL(startTime.TimeUtcL() + TTimeIntervalHours(1)) ;
			
	entry->SetStartAndEndTimeL(startTime, endTime) ;

	TCalTime endTime1;
	endTime1.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 23, 10, 5, 0, 0))) ;		


	TCalRRule rrule(TCalRRule::EWeekly) ;
	
	rrule.SetDtStart(startTime);
	rrule.SetUntil(endTime1);
	rrule.SetInterval(1);	
	
	RArray<TDay> days;
	CleanupClosePushL(days);	
	days.AppendL(ETuesday);
	rrule.SetByDay(days);
	CleanupStack::PopAndDestroy(&days);

	entry->SetRRuleL(rrule) ; 
	TCalRRule setRRule ;
	entry->GetRRuleL(setRRule) ;
	
	test(setRRule.DtStart().TimeUtcL() == rrule.DtStart().TimeUtcL()) ;
	entryarr.AppendL(entry) ;
	CleanupStack::Pop(entry);		

	iCalEntryView->StoreL(entryarr, num) ;	
	entryarr.ResetAndDestroy() ;

	iCalEntryView->FetchL(KGUID(), entryarr);
	TCalRRule savedRRule ;
	entryarr[0]->GetRRuleL(savedRRule) ;
	
	TCalTime matchTime ; /* Jan 10, 2006 is the first Tuesday */
	matchTime.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 9, 10, 1, 0, 0))) ;
	//When the entry is stored, the repeat start time is adjusted so that it fits the rule (10th January is Tuesday
	test(savedRRule.DtStart().TimeUtcL() == matchTime.TimeUtcL()) ;
	test(entryarr[0]->StartTimeL().TimeUtcL() == (matchTime.TimeUtcL())) ;
	
	
	CleanupStack::PopAndDestroy(&entryarr) ;

	RArray<TTime> insTimes ;	
	insTimes.Append(TTime(TDateTime(2006, EJanuary, 9, 10, 1, 0, 0))) ;
	insTimes.Append(TTime(TDateTime(2006, EJanuary, 16, 10, 1, 0, 0))) ;
	insTimes.Append(TTime(TDateTime(2006, EJanuary, 23, 10, 1, 0, 0))) ;
	
	CleanupClosePushL(insTimes) ;
	CheckInstancesL(insTimes) ;
	CleanupStack::PopAndDestroy(&insTimes) ;
}