// -----------------------------------------------------------------------------
// CopyExceptionDatesToPimL
// Copies Agenda exception dates to a PIM API repeat rule.
// Returns: void
// -----------------------------------------------------------------------------
//
void CopyExceptionDatesToPimL(const CCalEntry& aAgnEntry,
                              MPIMRepeatRuleData& aPimRepeatRuleData)
{
    JELOG2(EPim);
    RArray<TCalTime> agnExceptionDates;
    aAgnEntry.GetExceptionDatesL(agnExceptionDates);
    CleanupClosePushL(agnExceptionDates);
    const TInt numExceptions = agnExceptionDates.Count();
    for (TInt i = 0; i < numExceptions; i++)
    {
        TCalTime agnExceptionDate = agnExceptionDates[i];
        TTime pimExceptionDate = agnExceptionDate.TimeUtcL();
        aPimRepeatRuleData.AddExceptDateL(pimExceptionDate);
    }

    CleanupStack::PopAndDestroy(); // agnExceptionDates close
}
TVerdict CTestCalInterimApiDEF064928Step::doTestStepL()
	{
	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
	CleanupStack::PushL(scheduler);  
	CActiveScheduler::Install(scheduler);

	iSession  = CCalSession::NewL();
	
	CTestCalInterimApiCallbackForAlarms* alarmCallback = CTestCalInterimApiCallbackForAlarms::NewL(this);
	CleanupStack::PushL(alarmCallback); 

	OpenSessionFileL();
	iEntryView = CCalEntryView::NewL(*iSession,*alarmCallback);
	
	SetTestStepResult(EFail);
	
	CActiveScheduler::Add(alarmCallback);
	CActiveScheduler::Start();
	
	TUint seqNo = 1;
	HBufC8* pGUID1= KAlarmExportTestGUID1().AllocLC();
	CCalEntry* entry1 = CCalEntry::NewL(CCalEntry::EAppt, pGUID1,
	                                    CCalEntry::EMethodAdd, seqNo);
	CleanupStack::Pop(pGUID1); // entry takes ownership
	CleanupStack::PushL(entry1);                                   

	TDateTime entryStart1(2006, EJuly, 6, 15, 0, 0, 0);
	TCalTime  calStart1;
	calStart1.SetTimeLocalL(entryStart1);
	TDateTime entryStop1(2006, EJuly, 6, 16, 0, 0, 0);
	TCalTime calStop1;
	calStop1.SetTimeLocalL(entryStop1);
	entry1->SetStartAndEndTimeL(calStart1, calStop1);

	TCalRRule* rpt = new (ELeave) TCalRRule(TCalRRule::EDaily);
	CleanupStack::PushL(rpt); 

	rpt->SetInterval(1);
	TCalTime startTime;
	startTime.SetTimeLocalL(TDateTime(2006, EJuly, 6, 0, 0, 0, 0));
	rpt->SetDtStart(startTime);
	TCalTime endTime;
	endTime.SetTimeLocalL(TDateTime(2006, EJuly, 10, 0, 0, 0, 0));
	rpt->SetUntil(endTime);
	entry1->SetRRuleL(*rpt); // doesn't take ownership

	CleanupStack::PopAndDestroy(rpt); 
	
	SaveToAgendaL(entry1, EFalse);

	seqNo++;
	HBufC8* pGUID2= KAlarmExportTestGUID2().AllocLC();
	TDateTime entryTime2(2006, EJuly, 9, 15, 0, 0, 0);
	TCalTime  calTime2;
	calTime2.SetTimeLocalL(entryTime2);
	CCalEntry* entry2 = CCalEntry::NewL(CCalEntry::EAppt, pGUID2,
	                                    CCalEntry::EMethodAdd, seqNo, calTime2, 
	                                    CalCommon::EThisOnly);
	CleanupStack::Pop(pGUID2); // entry takes ownership
	CleanupStack::PushL(entry2);                                   

	CreateExceptionDateL(entry2, entry1);

	RArray<TCalTime> exDateList1;
	CleanupClosePushL(exDateList1);
	entry1->GetExceptionDatesL(exDateList1);
	TESTL(exDateList1[0].TimeLocalL() == calTime2.TimeLocalL());
	CleanupStack::PopAndDestroy();

	//build the CCalDataExchange object and Export
	RFs   fs;
	fs.Connect();
	CleanupClosePushL(fs);
	
	CCalDataExchange* dataExchange = CCalDataExchange::NewL(*iSession);
	CleanupStack::PushL(dataExchange); 
	RFile outFile;
	CleanupClosePushL(outFile);
	TInt err = outFile.Replace(fs, KEntryExportFile, EFileWrite);
	RFileWriteStream writeStream(outFile);
	CleanupClosePushL(writeStream);
	
	INFO_PRINTF1(KExporting);
	RPointerArray<CCalEntry> firstEntryArray;
	CleanupStack::PushL(TCleanupItem(CloseAndDeleteRPtrArrayEntries, &firstEntryArray));
	iEntryView->FetchL(entry1->UidL(), firstEntryArray);
		
	dataExchange->ExportL(KUidVCalendar, writeStream, firstEntryArray);
	
	writeStream.CommitL();
	
	// check that the exception date was stored correctly
	CCalEntry *fetchedEntry = firstEntryArray[0];
	RArray<TCalTime> exDateList2;
	fetchedEntry->GetExceptionDatesL(exDateList2);
	CleanupClosePushL(exDateList2);
	TESTL(exDateList2[0].TimeLocalL() == calTime2.TimeLocalL());
	
	CleanupStack::PopAndDestroy();
	CleanupStack::PopAndDestroy();
	CleanupStack::PopAndDestroy(&writeStream); // writeStream.Close()
	CleanupStack::PopAndDestroy(&outFile); // outFile.Close()
	
	//Internalize the entries from the file we just wrote
	RFile inFile;
	CleanupClosePushL(inFile);
	TInt errR = inFile.Open(fs, KEntryExportFile, EFileRead);
	RFileReadStream readStream(inFile);
	CleanupClosePushL(readStream);
	
	RPointerArray<CCalEntry> secondEntryArray;
	CleanupStack::PushL(TCleanupItem(CloseAndDeleteRPtrArrayEntries, &secondEntryArray));
	
	INFO_PRINTF1(KImporting);
	dataExchange->ImportL(KUidVCalendar, readStream, secondEntryArray);
	
	CleanupStack::Pop(&secondEntryArray);
	CleanupStack::PopAndDestroy(&readStream);
	CleanupStack::PopAndDestroy(&inFile);
	CleanupStack::PushL(TCleanupItem(CloseAndDeleteRPtrArrayEntries, &secondEntryArray));
	
	// check the imported alarm
	TESTL(secondEntryArray.Count() == 1);
	CCalEntry* entry = secondEntryArray[0];
	RArray<TCalTime> importedExDate;
	entry->GetExceptionDatesL(importedExDate);
	TESTL(importedExDate.Count() == 1);
	TESTL(importedExDate[0].TimeLocalL() == calTime2.TimeLocalL());

	CleanupStack::PopAndDestroy(7, scheduler);
	SetTestStepResult(EPass);
	return TestStepResult();
	}