// -----------------------------------------------------------------------------
// CPIMAgnToDoAdapter::AddDefaultValuesToEntryL
// (other items were commented in a header)
// -----------------------------------------------------------------------------
//
void CPIMAgnToDoAdapter::AddDefaultValuesToEntryL(const MPIMItemData& aData,
        CCalEntry& aEntry) const
{
    JELOG2(EPim);
    // Calendar creates medium priority ToDos by default
    if (!aData.CountValues(EPIMToDoPriority))
    {
        aEntry.SetPriorityL(EPIMToDoNativePriorityMedium);
    }
    // Calendar uses private synchronization by default
    if (!aData.CountValues(EPIMToDoClass))
    {
        aEntry.SetReplicationStatusL(CCalEntry::EPrivate);
    }
    // Calendar does not support timed ToDo so the time is set to 00:00 o'clock
    if (!aData.CountValues(EPIMToDoDue))
    {
        TTime thisTime;
        thisTime.HomeTime();
        // Set time to calendar specific due time. Currently this is the start
        // of the date. Note that No conversion needed since time is local
        TCalTime calThisTime;
        // Set time as local time since acquired above as local time
        calThisTime.SetTimeLocalL(StartOfDay(thisTime));
        aEntry.SetStartAndEndTimeL(calThisTime, calThisTime);
    }
    if (!aData.CountValues(EPIMToDoCompletionDate) && !aData.CountValues(
                EPIMToDoCompleted))
    {
        aEntry.SetCompletedL(EFalse, aEntry.CompletedTimeL());
    }

}
// -----------------------------------------------------------------------------
// CPIMAgnEventAdapter::AddDefaultValuesToEntryL
// -----------------------------------------------------------------------------
//
void CPIMAgnEventAdapter::AddDefaultValuesToEntryL(const MPIMEventItem& aItem,
        CCalEntry& aEntry) const
{
    JELOG2(EPim);
    const MPIMItemData& data = aItem.ItemData();
    // Default calendar synchronisation is private
    if (data.CountValues(EPIMEventClass) == 0)
    {
        aEntry.SetReplicationStatusL(CCalEntry::EPrivate);
    }
}
// -----------------------------------------------------------------------------
// CPIMAgnEventAdapter::ConvertClassToAgnL
// Makes conversion from framework PIM item class to native entry class.
// -----------------------------------------------------------------------------
//
void CPIMAgnEventAdapter::ConvertClassToAgnL(const MPIMEventItem& aItem,
        CCalEntry& aEntry)
{
    JELOG2(EPim);
    CCalEntry::TReplicationStatus replicationStatus = CCalEntry::EPrivate;

    // There should and MUST be only one value in the
    // class field so get the value from the first index
    const TPIMFieldData& fieldData = aItem.ItemData().ValueL(EPIMEventClass, 0);
    TInt eClassValue = fieldData.IntegerValue();

    // Map pim class value to calendar entry value
    switch (eClassValue)
    {
    case EPIMEventClassPrivate:
    {
        replicationStatus = CCalEntry::EPrivate;
        break;
    }
    case EPIMEventClassConfidential:
    {
        replicationStatus = CCalEntry::ERestricted;
        break;
    }
    case EPIMEventClassPublic:
    {
        replicationStatus = CCalEntry::EOpen;
        break;
    }
    default:
    {
        User::Leave(KErrArgument);
        break;
    }
    }
    aEntry.SetReplicationStatusL(replicationStatus);
}
// -----------------------------------------------------------------------------
// CPIMAgnToDoAdapter::ConvertIntToAgnL
// Makes int conversion from framework PIM item data field to To-do item field.
// -----------------------------------------------------------------------------
//
void CPIMAgnToDoAdapter::ConvertIntToAgnL(TPIMToDoField aField, // Int field to be converted
        TInt aIndex, // Index of the date field
        CCalEntry& aEntry, // The Agenda model entry typecasted to a Todo item
        const MPIMItemData& aItemData) // The PIM item to read the field from
{
    JELOG2(EPim);
    const TPIMFieldData fieldData = aItemData.ValueL(aField, aIndex);
    switch (aField)
    {
    case EPIMToDoPriority:
    {
        TInt intField = fieldData.IntegerValue();

        if ((EPIMToDoPriorityHigh <= intField) && (intField
                < EPIMToDoPriorityMedium))
        {
            aEntry.SetPriorityL(EPIMToDoNativePriorityHigh);
        }
        else if ((EPIMToDoPriorityMedium <= intField) && (intField
                 < EPIMToDoPriorityLow))
        {
            aEntry.SetPriorityL(EPIMToDoNativePriorityMedium);
        }
        else if ((EPIMToDoPriorityLow <= intField) && (intField
                 <= EPIMToDoPriorityMaxValue))
        {
            aEntry.SetPriorityL(EPIMToDoNativePriorityLow);
        }
        else
        {
            // From requirement specification: Imported to-do items with
            // priority set to zero must be mapped to the native priority
            // value Medium.
            aEntry.SetPriorityL(EPIMToDoNativePriorityMedium);
        }
        break;
    }
    case EPIMToDoClass:
    {
        CCalEntry::TReplicationStatus replicationStatus = CCalEntry::EPrivate;

        // Single value assumed
        TInt classValue = fieldData.IntegerValue();

        switch (classValue)
        {
        case EPIMToDoClassPrivate:
        {
            replicationStatus = CCalEntry::EPrivate;
            break;
        }
        case EPIMToDoClassConfidential:
        {
            replicationStatus = CCalEntry::ERestricted;
            break;
        }
        case EPIMToDoClassPublic:
        {
            replicationStatus = CCalEntry::EOpen;
            break;
        }
        default:
        {
            User::Leave(KErrArgument);
            break;
        }
        }
        aEntry.SetReplicationStatusL(replicationStatus);
        break;
    }
    case EPIMToDoExtAlarm:
    {
        CCalAlarm* agnAlarm = CCalAlarm::NewL();
        CleanupStack::PushL(agnAlarm);
        agnAlarm->SetTimeOffset(AlarmOffsetL(aItemData, aEntry));
        aEntry.SetAlarmL(agnAlarm);
        CleanupStack::PopAndDestroy(agnAlarm);
        break;
    }
    default:
    {
        // Should not happen
        __ASSERT_DEBUG(EFalse, User::Invariant());
    }
    }
}