javaextensions/pim/versit/src.s60/cpimcardconverter.cpp
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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 vCards PIM <-> Versit
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cpimcardconverter.h"
       
    21 #include "pimcommon.h" // tpimfield
       
    22 #include "pimpanics.h"
       
    23 #include "cpimitem.h"
       
    24 #include "cpimcontactitem.h"
       
    25 #include "cpimcontactvalidator.h"
       
    26 #include "cpimcardpropertyconverter.h"
       
    27 #include "fs_methodcall.h"
       
    28 #include "logger.h"
       
    29 #include "cleanupresetanddestroy.h"
       
    30 #include <vcard.h>
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CPIMCardConverter::NewL
       
    36 // Two-phased constructor.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CPIMCardConverter* CPIMCardConverter::NewL(
       
    40     const CPIMContactValidator& aContactValidator,
       
    41     java::util::FunctionServer* aFuncServer) // validator used for contacts
       
    42 {
       
    43     JELOG2(EPim);
       
    44     CPIMCardConverter* self =
       
    45         new(ELeave) CPIMCardConverter(aContactValidator, aFuncServer);
       
    46 
       
    47     CleanupStack::PushL(self);
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop(self);
       
    50 
       
    51     return self;
       
    52 }
       
    53 
       
    54 // Destructor
       
    55 CPIMCardConverter::~CPIMCardConverter()
       
    56 {
       
    57     JELOG2(EPim);
       
    58     delete iPropertyConverter;
       
    59 }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CPIMCardConverter::ItemToStreamL
       
    63 // Converts Contact Items to vCards.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CPIMCardConverter::ItemToStreamL(RWriteStream& aStream, // RWriteStream to write the vCard to.
       
    67                                       const CPIMItem& aItem, // CPIMItem which is to be converted.
       
    68                                       Versit::TVersitCharSet aCharset) // defines charset used in vcards
       
    69 {
       
    70     JELOG2(EPim);
       
    71     CParserVCard* parser = CParserVCard::NewL();
       
    72     CleanupStack::PushL(parser);
       
    73     parser->SetDefaultCharSet(aCharset);
       
    74     CArrayFix<TPIMField>* fields = aItem.GetFieldsL();
       
    75     CleanupStack::PushL(fields);
       
    76     TInt fieldCount = fields->Count();
       
    77 
       
    78     for (TInt i = 0; i < fieldCount; i++)
       
    79     {
       
    80         TPIMContactField contactField = (TPIMContactField)(fields->At(i));
       
    81         CallMethodL(iPropertyConverter,
       
    82                     &CPIMCardPropertyConverter::ConvertFieldL, aItem, *parser,
       
    83                     contactField, iFuncServer);
       
    84     }
       
    85     parser->ExternalizeL(aStream);
       
    86     aStream.CommitL();
       
    87     CleanupStack::PopAndDestroy(2, parser); // fields, parser
       
    88 }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CPIMCardConverter::StreamToItemL
       
    92 // Converts vCards to Contact Items
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CPIMCardConverter::StreamToItemL(RReadStream& aStream, // RReadStream to be converted.
       
    96                                       RPointerArray<CPIMItem>& aItemArray, Versit::TVersitCharSet aCharset)
       
    97 {
       
    98     JELOG2(EPim);
       
    99     CParserVCard* parser = CParserVCard::NewL();
       
   100     CleanupStack::PushL(parser);
       
   101     parser->SetDefaultCharSet(aCharset);
       
   102     parser->InternalizeL(aStream);
       
   103 
       
   104     CArrayPtrFlat<CParserVCard>* parserArray = new(ELeave) CArrayPtrFlat<
       
   105     CParserVCard> (1);
       
   106     CleanupStack::PushL(parserArray);
       
   107     parserArray->AppendL(parser);
       
   108 
       
   109     // parserArray may grow inside the loop, therefore the Count() -method
       
   110     // is always called.
       
   111     for (TInt i = 0; i < parserArray->Count(); i++)
       
   112     {
       
   113         ParserToItemL(*(parserArray->At(i)), aItemArray, *parserArray);
       
   114     }
       
   115 
       
   116     if (aItemArray.Count() == 0)
       
   117     {
       
   118         User::Leave(KErrCorrupt);
       
   119     }
       
   120 
       
   121     CleanupStack::PopAndDestroy(2, parser); // parserArray, parser
       
   122 }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CPIMCardConverter::ParserToItemL
       
   126 // Converts vCards to Contact Items
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CPIMCardConverter::ParserToItemL(CParserVCard& aParser, // parser to be converted
       
   130                                       RPointerArray<CPIMItem>& aItemArray, // items are added here
       
   131                                       CArrayPtrFlat<CParserVCard>& aParserArray) // contained vCards here
       
   132 {
       
   133     JELOG2(EPim);
       
   134     CleanupResetAndDestroyPushL(aItemArray);
       
   135     CPIMContactItem* item = CPIMContactItem::NewLC(iContactValidator);
       
   136     // We don't take the ownership of the propertyArray, so the properties
       
   137     // are deleted when the parser is
       
   138     CArrayPtr<CParserProperty>* propertyArray = aParser.ArrayOfProperties(
       
   139                 EFalse);
       
   140     if (propertyArray)
       
   141     {
       
   142         TInt propertyCount = propertyArray->Count();
       
   143         for (TInt i = 0; i < propertyCount; i++)
       
   144         {
       
   145             CParserVCard* containedCard = iPropertyConverter->ConvertPropertyL(
       
   146                                               *(propertyArray->At(i)), *item);
       
   147             if (containedCard)
       
   148             {
       
   149                 aParserArray.AppendL(containedCard);
       
   150             }
       
   151         }
       
   152         User::LeaveIfError(aItemArray.Append(item));
       
   153         CleanupStack::Pop(item); // item
       
   154     }
       
   155     else
       
   156     {
       
   157         // no properties, no item
       
   158         CleanupStack::PopAndDestroy(item);
       
   159     }
       
   160     // Let's see if there are any subentities to be converted as well
       
   161     CArrayPtr<CVersitParser>* entityArray = aParser.ArrayOfEntities(EFalse);
       
   162     if (entityArray)
       
   163     {
       
   164         TInt entityCount = entityArray->Count();
       
   165         for (TInt i = 0; i < entityCount; i++)
       
   166         {
       
   167             CParserVCard* subParser =
       
   168                 static_cast<CParserVCard*>(entityArray->At(i));
       
   169             aParserArray.AppendL(subParser);
       
   170         }
       
   171     }
       
   172     CleanupStack::Pop(&aItemArray);
       
   173 }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CPIMCardConverter::CPIMCardConverter
       
   177 // C++ default constructor can NOT contain any code, that
       
   178 // might leave.
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 CPIMCardConverter::CPIMCardConverter(
       
   182     const CPIMContactValidator& aContactValidator,
       
   183     java::util::FunctionServer* aFuncServer) :
       
   184         iContactValidator(aContactValidator), iFuncServer(aFuncServer)
       
   185 {
       
   186     JELOG2(EPim);
       
   187 }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CPIMCardConverter::ConstructL
       
   191 // Symbian 2nd phase constructor can leave.
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 void CPIMCardConverter::ConstructL()
       
   195 {
       
   196     JELOG2(EPim);
       
   197     iPropertyConverter = CPIMCardPropertyConverter::NewL(iContactValidator);
       
   198 }
       
   199 
       
   200 //  End of File