// -----------------------------------------------------------------------------
// CPIMAgnToDoAdapter::ReadDateFieldsL
// (other items were commented in a header)
// -----------------------------------------------------------------------------
//
void CPIMAgnToDoAdapter::ReadDateFieldsL(MPIMItemData& aData, CCalEntry& aEntry)
{
    JELOG2(EPim);
    TTime nullTime = Time::NullTTime();
    // The Agenda todo entry end field is the due date
    TTime due(aEntry.EndTimeL().TimeLocalL());
    if (due != nullTime)
    {
        // Set due to the PIM API specific due date, in this case, the start of date
        // Note that PIM API uses times as UTC times so the due date must be in
        // correct format. Previously requested as local time -> do not change
        TPIMDate pimDueDate(StartOfDay(due));
        // Date must be converted UTC time because acquired as local above
        ConvertTimeL(pimDueDate, EPIMDateUTC);
        TPIMFieldData dueFieldData(EPIMToDoDue, KPIMAttrNone, pimDueDate);
        aData.AddValueL(dueFieldData);

        // Get alarm. Ownership is transferred to the caller. Alarm cannot be set
        // if the due date is not set because the calculation is done as an offset
        // from the ToDo due date.
        CCalAlarm* calAlarm = aEntry.AlarmL();
        if (calAlarm)
        {
            TTimeIntervalMinutes nativeValue = calAlarm->TimeOffset();
            // The alarm is not needed anymore so it can be deleted
            delete calAlarm;
            calAlarm = NULL;
            // Change the time to the start of the due date
            TTime startOfDayLocal(StartOfDay(due));
            // Calculate the difference from the start of due date and start time including
            // the original alarm offset which was previously read
            TTimeIntervalMinutes temp(0);
            User::LeaveIfError(startOfDayLocal.MinutesFrom(due, temp));
            // Since it is not possible to substract TTimeIntervalMinutes
            // from TTime (probably a Symbian error), the difference has
            // to be calculated using the following way...
            TInt alarm = (nativeValue.Int() + temp.Int()) * KPIMSecondsInMinute;
            // Add alarm value to the item
            TPIMFieldData fieldData(EPIMToDoExtAlarm, EPIMFieldInt,
                                    KPIMAttrNone, alarm);
            // Add value to the PIM item data
            aData.AddValueL(fieldData);
        }
    }

    // Completion date. If the item has a completion date, the item is then completed
    // and completed flag is set to true in PIM API. Null time if not crossed out.
    TTime completed = aEntry.CompletedTimeL().TimeUtcL();
    if (completed != nullTime)
    {
        TPIMFieldData dateData(EPIMToDoCompletionDate, KPIMAttrNone, completed);
        aData.AddValueL(dateData);
        // Note that boolean and integer fields must be identified in the constructor
        TPIMFieldData flag(EPIMToDoCompleted, EPIMFieldBoolean, KPIMAttrNone,
                           ETrue);
        aData.AddValueL(flag);
    }
}
// -----------------------------------------------------------------------------
// CPIMAgnEventAdapter::ReadAlarmFromAgnL
// Reads alarm offset from the native Calendar entry. In case of Anniversary,
// the offset is calculated from the midnight since native Calendar supports
// only dates in these types of entries
// -----------------------------------------------------------------------------
//
void CPIMAgnEventAdapter::ReadAlarmFromAgnL(MPIMEventItem& aItem,
        CCalEntry& aEntry)
{
    JELOG2(EPim);
    CCalAlarm* calAlarm = aEntry.AlarmL();
    // The previous function call returns NULL if there is no alarm
    // set in the item. The ownership is transferred to the caller
    // if the alarm values has been added to the item.
    if (calAlarm)
    {
        TTimeIntervalMinutes nativeValue = calAlarm->TimeOffset();
        // The alarm is not needed anymore so it can be deleted
        delete calAlarm;
        calAlarm = NULL;
        //            nativeValue.Int() );
        // Convert the alarm value based on the start time of the entry
        CCalEntry::TType entryType = aEntry.EntryTypeL();
        // Events (memos) and anniversaries do not have time in the native
        // side, therefore alarm field in those entries need to be calculated
        // from the end of the day
        if (entryType == CCalEntry::EAnniv)
        {
            TTime start(aEntry.StartTimeL().TimeLocalL());
            // Change the time to the end of the start date
            TTime startOfDayLocal(start);
            startOfDayLocal = StartOfDay(startOfDayLocal);
            // Calculate the difference from end of day and start time including
            // the original alarm offset which was previously read
            TTimeIntervalMinutes temp(0);
            User::LeaveIfError(startOfDayLocal.MinutesFrom(start, temp));
            // Since it is not possible to substract TTimeIntervalMinutes
            // from TTime (probably a Symbian error), the difference has
            // to be calculated using the following way...
            nativeValue = nativeValue.Int() + temp.Int();
        }
        TInt alarmValue = nativeValue.Int() * KPIMSecondsInMinute;
        //            alarmValue );
        // Add alarm value to the item
        TPIMFieldData fieldData(EPIMEventAlarm, EPIMFieldInt, KPIMAttrNone,
                                alarmValue);
        aItem.ItemData().AddValueL(fieldData);
    }
}
TVerdict CTestCalInterimApiRichAlarmFormatStep::doTestStepL()
	{
	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
	CleanupStack::PushL(scheduler);  
	CActiveScheduler::Install(scheduler);

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

	OpenSessionFileL();
	iEntryView = CCalEntryView::NewL(*iSession,*alarmCallback);
	
	CActiveScheduler::Add(alarmCallback);
	CActiveScheduler::Start();
	
	//build the CCalDataExchange object and import
	CCalDataExchange* dataExchange = CCalDataExchange::NewL(*iSession);
	CleanupStack::PushL(dataExchange); 

	//IMPORT the calendar info from a known file
	RFs fs;
	fs.Connect();
	CleanupClosePushL(fs);
	RFile inFile;
	CleanupClosePushL(inFile);
	TInt errR = inFile.Open(fs, GetFullFileName(KEntryImportFile), EFileRead);
	RFileReadStream readStream(inFile);
	CleanupClosePushL(readStream);
	
	RPointerArray<CCalEntry> secondEntryArray;
	CleanupStack::PushL(TCleanupItem(CloseAndDeleteRPtrArrayEntries, &secondEntryArray));
	secondEntryArray.Reset();
	
	INFO_PRINTF1(KImporting);
	TInt index = 0;
	for (index = 0; index < KNumEntriesInFile; index++)
		{
		dataExchange->ImportL(KUidVCalendar, readStream, secondEntryArray);
		}
	
	CleanupStack::Pop(&secondEntryArray);
	CleanupStack::PopAndDestroy(&readStream);
	CleanupStack::PopAndDestroy(&inFile);
	CleanupStack::PushL(TCleanupItem(CloseAndDeleteRPtrArrayEntries, &secondEntryArray));

	SetTestStepResult(CompareAlarmDataL(&secondEntryArray));
	
	//Test for importing from a badly formatted file
	RFile badInFile;
	CleanupClosePushL(badInFile);
	TInt baddErr = badInFile.Open(fs, GetFullFileName(KEntryBadImportFile), EFileRead);
	RFileReadStream badStream(badInFile);
	CleanupClosePushL(badStream);
	
	RPointerArray<CCalEntry> badEntryArray;
	CleanupStack::PushL(TCleanupItem(CloseAndDeleteRPtrArrayEntries, &badEntryArray));
	badEntryArray.Reset();
	
	INFO_PRINTF1(KImporting);
	TInt i = 0;
	for (i = 0; i < 1; i++)
		{
		dataExchange->ImportL(KUidVCalendar, badStream, badEntryArray);
		}
	CCalEntry* pEntry = badEntryArray[0];
	CCalAlarm* pAlarm = pEntry->AlarmL();
	if (pAlarm)
		{
		CleanupStack::PushL(pAlarm);
		CCalContent* pContent = pAlarm->AlarmAction();
		if (pContent != NULL)
			{
			ERR_PRINTF1(KUnexpectedAlarmAction);
			SetTestStepResult(EFail);
			}
		delete pContent;
		CleanupStack::PopAndDestroy(pAlarm);
		}
	CleanupStack::PopAndDestroy(&badEntryArray);
	CleanupStack::PopAndDestroy(&badStream);
	CleanupStack::PopAndDestroy(&badInFile);

	
	CleanupStack::PopAndDestroy(5 , scheduler);

	return TestStepResult();
	}
void CCalAlarmAttachTest::TestUpdateEntryHavingAlarmcontentL()
    {
    test.Printf(_L("Test updating an entry having AlarmContent \n"));
    
    //Create a Calendar entry with Alarm Content
    RPointerArray<CCalEntry> entries;
    CleanupResetAndDestroyPushL(entries);
    HBufC8* guid = KGUID1().AllocLC();
    CCalEntry* calentry = CCalEntry::NewL(CCalEntry::EEvent, guid, CCalEntry::EMethodNone, 0);
    CleanupStack::Pop(guid);
    CleanupStack::PushL(calentry);
    
    TCalTime calTime1;
    calTime1.SetTimeUtcL(TDateTime(2007,EFebruary,15, 13, 30, 0, 0));
    TCalTime calTime2;
    calTime2.SetTimeUtcL(TDateTime(2007,EFebruary,15, 14, 30, 0, 0));
    calentry->SetStartAndEndTimeL(calTime1, calTime2);
    calentry->SetSummaryL(_L("Test for Alarms"));
    CCalAlarm* alarm = CCalAlarm::NewL();
    CleanupStack::PushL(alarm);
    alarm->SetTimeOffset(1);
        
    CCalContent* almContent = CCalContent::NewL();
    CleanupStack::PushL(almContent);
    // Add content and mimetype for the alarm.
    HBufC8* content = KContent().AllocLC();
    HBufC8* mimetype = KMimeType().AllocLC();
        
    // Takes ownership of content and mimetype.
    almContent->SetContentL(content, mimetype, CCalContent::EDispositionInline);
    alarm->SetAlarmAction(almContent); // Takes ownership of almContent.
        
    calentry->SetAlarmL(alarm);
        
    CleanupStack::Pop(mimetype);
    CleanupStack::Pop(content);    
    CleanupStack::Pop(almContent); 
    CleanupStack::PopAndDestroy(alarm);

    TCalRRule rptRule;
    rptRule.SetDtStart( calTime1 );
    rptRule.SetType( TCalRRule::EYearly );
    rptRule.SetCount(5);  
    calentry->SetRRuleL(rptRule);

    entries.AppendL(calentry);
   
    // Store entry with Alarm content
    TInt entriesStored = 0;
    iTestLib->SynCGetEntryViewL().StoreL(entries, entriesStored);   
    CleanupStack::Pop(calentry);
    CleanupStack::PopAndDestroy(&entries);
    
    //Fetch stored entry and modify alarm time.
    CleanupResetAndDestroyPushL(entries);
    iTestLib->SynCGetEntryViewL().FetchL(KGUID1(), entries);
    CCalEntry* entry = entries[0];
    alarm = entry->AlarmL();
    CleanupStack::PushL(alarm);
    alarm->SetTimeOffset(1);
    entry->SetAlarmL(alarm);
    CleanupStack::PopAndDestroy(alarm);

    //Update the entry with changes.
    iTestLib->SynCGetEntryViewL().UpdateL(entries, entriesStored); 
    CleanupStack::PopAndDestroy(&entries);

    //Fetch updated entry and check if Alarm content is intact.
    CleanupResetAndDestroyPushL(entries);
    iTestLib->SynCGetEntryViewL().FetchL(KGUID1(), entries);
    TestAlarmL(entries[0], KContent(), KMimeType());
    CleanupStack::PopAndDestroy(&entries);
    }