javaextensions/pim/versit/src.s60/cpimcalendarconverter.cpp
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Converts vCalendars PIM <-> Versit
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // CLASS HEADER
       
    20 #include "cpimcalendarconverter.h"
       
    21 
       
    22 // INTERNAL INCLUDES
       
    23 #include "pimcommon.h" // tpimfield
       
    24 #include "pimpanics.h"
       
    25 #include "pimversit.h"
       
    26 #include "cpimitem.h"
       
    27 #include "cpimeventitem.h"
       
    28 #include "cpimtodoitem.h"
       
    29 #include "cpimtodopropertyconverter.h"
       
    30 #include "cpimeventpropertyconverter.h"
       
    31 #include "fs_methodcall.h"
       
    32 #include "logger.h"
       
    33 #include "cleanupresetanddestroy.h"
       
    34 
       
    35 // EXTERNAL INCLUDES
       
    36 #include <vcal.h>
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CPIMCalendarConverter::NewL
       
    42 // Two-phased constructor.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CPIMCalendarConverter* CPIMCalendarConverter::NewL(
       
    46     const CPIMEventValidator& aEventValidator,
       
    47     const CPIMToDoValidator& aToDoValidator,
       
    48     java::util::FunctionServer* aFuncServer)
       
    49 {
       
    50     JELOG2(EPim);
       
    51     CPIMCalendarConverter* self = new(ELeave) CPIMCalendarConverter(
       
    52         aEventValidator, aToDoValidator, aFuncServer);
       
    53 
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop(self);
       
    57 
       
    58     return self;
       
    59 }
       
    60 
       
    61 // Destructor
       
    62 CPIMCalendarConverter::~CPIMCalendarConverter()
       
    63 {
       
    64     JELOG2(EPim);
       
    65     delete iToDoConverter;
       
    66     delete iEventConverter;
       
    67 }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CPIMCalendarConverter::ItemToStreamL
       
    71 // Converts Event/ToDo Items to vCards.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 void CPIMCalendarConverter::ItemToStreamL(RWriteStream& aStream, // RWriteStream to write the vCard to.
       
    75         const CPIMItem& aItem, // CPIMItem which is to be converted.
       
    76         const Versit::TVersitCharSet aCharset) // charSet used in versit objects
       
    77 {
       
    78     JELOG2(EPim);
       
    79     CParserVCal* calParser = CParserVCal::NewL();
       
    80     CleanupStack::PushL(calParser);
       
    81     calParser->SetDefaultCharSet(aCharset);
       
    82 
       
    83     CParserVCalEntity* parser = CParserVCalEntity::NewL();
       
    84     // NOTE: parser is not put into the cleanup stack since AddEntityL
       
    85     // pushes it right from the start. So it MUST not be pushed here
       
    86     // to avoid duplicate cleanup stack pointers
       
    87     calParser->AddEntityL(parser);
       
    88     // No need to pop here
       
    89 
       
    90     CArrayFix<TPIMField>* fields = aItem.GetFieldsL();
       
    91     CleanupStack::PushL(fields);
       
    92     TInt fieldCount = fields->Count();
       
    93     switch (aItem.ItemType())
       
    94     {
       
    95     case EPIMEventList:
       
    96     {
       
    97         TPtrC vEvent(KVersitVarTokenVEVENT);
       
    98         parser->SetEntityNameL(vEvent);
       
    99         for (TInt i = 0; i < fieldCount; i++)
       
   100         {
       
   101             TPIMEventField eventField =
       
   102                 static_cast<TPIMEventField>(fields->At(i));
       
   103             CallMethodL(iEventConverter,
       
   104                         &CPIMEventPropertyConverter::ConvertEventFieldL, aItem,
       
   105                         *parser, eventField, iFuncServer);
       
   106         }
       
   107         // Priority is a mandatory property for vCals so add it here...
       
   108         // Since event items do not have priority field, use unspecified
       
   109         // priority value which is zero
       
   110         CParserPropertyValue* value = new(ELeave) CParserPropertyValueInt(0);
       
   111         // value is pushed to the cleanup stack in AddPropertyToParserL
       
   112         // and parser takes the ownership of the added value
       
   113         iEventConverter->AddPropertyToParserL(value, KVersitTokenPRIORITY(),
       
   114                                               *parser);
       
   115         // Add repeat rule vCal property if the item is repeating
       
   116         const CPIMEventItem& eventItem =
       
   117             static_cast<const CPIMEventItem&>(aItem);
       
   118         if (eventItem.IsRepeating())
       
   119         {
       
   120             iEventConverter->ConvertRepeatRuleL(eventItem.GetRepeat(), *parser);
       
   121         }
       
   122         break;
       
   123     }
       
   124     case EPIMToDoList:
       
   125     {
       
   126         // Priority field neesds to be added if not present in the item
       
   127         if (aItem.CountValuesL(EPIMToDoCompleted) == 0)
       
   128         {
       
   129             CParserPropertyValue* propertyValue =
       
   130                 CParserPropertyValueHBufC::NewL(KPIMStatusUncompleted);
       
   131             // value is pushed to the cleanup stack in AddPropertyToParserL
       
   132             // and parser takes the ownership of the added value
       
   133             iToDoConverter->AddPropertyToParserL(propertyValue,
       
   134                                                  KVersitTokenSTATUS(), *parser);
       
   135         }
       
   136         // Convert other fields
       
   137         TPtrC vToDo(KVersitVarTokenVTODO);
       
   138         parser->SetEntityNameL(vToDo);
       
   139         for (TInt i = 0; i < fieldCount; i++)
       
   140         {
       
   141             TPIMToDoField todoField =
       
   142                 static_cast<TPIMToDoField>(fields->At(i));
       
   143             CallMethodL(iToDoConverter,
       
   144                         &CPIMToDoPropertyConverter::ConvertToDoFieldL, aItem, *parser,
       
   145                         todoField, iFuncServer);
       
   146         }
       
   147         break;
       
   148     }
       
   149     default:
       
   150     {
       
   151         User::Leave(KErrArgument);
       
   152     }
       
   153     }
       
   154     calParser->ExternalizeL(aStream);
       
   155     aStream.CommitL();
       
   156     CleanupStack::PopAndDestroy(2, calParser);  // fields, parser
       
   157 }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CPIMCalendarConverter::StreamToItemL
       
   161 // Converts vCards to Event Items
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 void CPIMCalendarConverter::StreamToItemL(RReadStream& aStream, // RReadStream to be converted.
       
   165         RPointerArray<CPIMItem>& aItemArray, Versit::TVersitCharSet aCharset)
       
   166 {
       
   167     JELOG2(EPim);
       
   168 
       
   169 
       
   170     CParserVCal* parser = CParserVCal::NewL();
       
   171     CleanupStack::PushL(parser);
       
   172     parser->SetDefaultCharSet(aCharset);
       
   173     parser->InternalizeL(aStream);
       
   174 
       
   175     CArrayPtr<CVersitParser>* eventArray = parser->EntityL(
       
   176                                                KVersitVarTokenVEVENT, EFalse);
       
   177     CleanupStack::PushL(eventArray);
       
   178     CArrayPtr<CVersitParser>* todoArray = parser->EntityL(KVersitVarTokenVTODO,
       
   179                                           EFalse);
       
   180     CleanupStack::PushL(todoArray);
       
   181 
       
   182     if (eventArray)
       
   183     {
       
   184         TInt eventCount = eventArray->Count();
       
   185         for (TInt i = 0; i < eventCount; i++)
       
   186         {
       
   187             CParserVCalEntity* entity =
       
   188                 static_cast<CParserVCalEntity*>(eventArray->At(i));
       
   189             ParserToEventL(*entity, aItemArray);
       
   190         }
       
   191     }
       
   192 
       
   193     if (todoArray)
       
   194     {
       
   195         TInt todoCount = todoArray->Count();
       
   196         for (TInt i = 0; i < todoCount; i++)
       
   197         {
       
   198             CParserVCalEntity* entity =
       
   199                 static_cast<CParserVCalEntity*>(todoArray->At(i));
       
   200             ParserToToDoL(*entity, aItemArray);
       
   201         }
       
   202     }
       
   203     CleanupStack::PopAndDestroy(3, parser); // parser, eventArray, todoArray
       
   204 
       
   205 
       
   206 }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CPIMCalendarConverter::CPIMCalendarConverter
       
   210 // C++ default constructor can NOT contain any code, that
       
   211 // might leave.
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 CPIMCalendarConverter::CPIMCalendarConverter(
       
   215     const CPIMEventValidator& aEventValidator,
       
   216     const CPIMToDoValidator& aToDoValidator,
       
   217     java::util::FunctionServer* aFuncServer) :
       
   218         iEventValidator(aEventValidator), iToDoValidator(aToDoValidator),
       
   219         iFuncServer(aFuncServer)
       
   220 {
       
   221     JELOG2(EPim);
       
   222 }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CPIMCalendarConverter::ConstructL
       
   226 // Symbian 2nd phase constructor can leave.
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void CPIMCalendarConverter::ConstructL()
       
   230 {
       
   231     JELOG2(EPim);
       
   232     iToDoConverter = CPIMToDoPropertyConverter::NewL();
       
   233     iEventConverter = CPIMEventPropertyConverter::NewL();
       
   234 }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CPIMCalendarConverter::ParserToEventL
       
   238 // Converts vEvents to Event Items
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CPIMCalendarConverter::ParserToEventL(CParserVCalEntity& aParser,
       
   242         RPointerArray<CPIMItem>& aItemArray)
       
   243 {
       
   244     JELOG2(EPim);
       
   245     CleanupResetAndDestroyPushL(aItemArray);
       
   246     CPIMEventItem* item = CPIMEventItem::NewLC(iEventValidator);
       
   247     TPIMDate alarm(TInt64(0));
       
   248     // We don't take the ownership of the propertyArray, so the properties
       
   249     // are deleted when the parser is
       
   250     CArrayPtr<CParserProperty>* propertyArray = aParser.ArrayOfProperties(
       
   251                 EFalse);
       
   252     if (!propertyArray)
       
   253     {
       
   254         User::Leave(KErrCorrupt);
       
   255     }
       
   256     TInt propertyCount = propertyArray->Count();
       
   257     for (TInt i = 0; i < propertyCount; i++)
       
   258     {
       
   259         iEventConverter->ConvertPropertyL(*(propertyArray->At(i)), *item, alarm);
       
   260     }
       
   261     if (alarm.Int64() != TInt64(0))
       
   262     {
       
   263         // Count the alarm value
       
   264         if (item->CountValuesL(EPIMEventStart) > 0)
       
   265         {
       
   266             const TPIMDate startDate = item->GetDateL(EPIMEventStart, 0);
       
   267             TTimeIntervalSeconds interval(0);
       
   268             User::LeaveIfError(startDate.SecondsFrom(alarm, interval));
       
   269             item->addInt(EPIMEventAlarm, KPIMAttrNone, interval.Int());
       
   270         }
       
   271     }
       
   272     User::LeaveIfError(aItemArray.Append(item));
       
   273     CleanupStack::Pop(item); // item
       
   274     CleanupStack::Pop(&aItemArray);
       
   275 }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CPIMCalendarConverter::ParserToToDoL
       
   279 // Converts vTodos to Todo Items
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CPIMCalendarConverter::ParserToToDoL(CParserVCalEntity& aParser,
       
   283         RPointerArray<CPIMItem>& aItemArray)
       
   284 {
       
   285     JELOG2(EPim);
       
   286     CleanupResetAndDestroyPushL(aItemArray);
       
   287     CPIMToDoItem* item = CPIMToDoItem::NewLC(iToDoValidator);
       
   288     TPIMDate alarm(TInt64(0));
       
   289     // We don't take the ownership of the propertyArray, so the properties
       
   290     // are deleted when the parser is
       
   291     CArrayPtr<CParserProperty>* propertyArray = aParser.ArrayOfProperties(
       
   292                 EFalse);
       
   293     if (!propertyArray)
       
   294     {
       
   295         User::Leave(KErrCorrupt);
       
   296     }
       
   297     TInt propertyCount = propertyArray->Count();
       
   298     for (TInt i = 0; i < propertyCount; i++)
       
   299     {
       
   300         iToDoConverter->ConvertPropertyL(*(propertyArray->At(i)), *item, alarm);
       
   301     }
       
   302     if (alarm.Int64() != TInt64(0))
       
   303     {
       
   304         // Count alarm value if possible
       
   305         if (item->CountValuesL(EPIMToDoDue) > 0)
       
   306         {
       
   307             const TPIMDate dueDate = item->GetDateL(EPIMToDoDue, 0);
       
   308             TTimeIntervalSeconds alarmOffset(0);
       
   309             User::LeaveIfError(dueDate.SecondsFrom(alarm, alarmOffset));
       
   310             item->AddIntL(EPIMToDoExtAlarm, KPIMAttrNone, alarmOffset.Int());
       
   311         }
       
   312     }
       
   313     if (item->CountValuesL(EPIMToDoCompletionDate) == 0)
       
   314     {
       
   315         item->AddBooleanL(EPIMToDoCompleted, KPIMAttrNone, EFalse);
       
   316     }
       
   317     else
       
   318     {
       
   319         item->AddBooleanL(EPIMToDoCompleted, KPIMAttrNone, ETrue);
       
   320     }
       
   321     User::LeaveIfError(aItemArray.Append(item));
       
   322     CleanupStack::Pop(item); // item
       
   323     CleanupStack::Pop(&aItemArray);
       
   324 }
       
   325 
       
   326 //  End of File