javaextensions/pim/versit/src.s60/cpimversit.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:  Handles conversions between PIM Item <-> versit format
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cpimmanager.h"
       
    21 #include "cpimversit.h"
       
    22 #include "cpimcardconverter.h"
       
    23 #include "cpimcalendarconverter.h"
       
    24 #include "cpimitem.h"
       
    25 #include "cleanupresetanddestroy.h"
       
    26 #include "logger.h"
       
    27 #include <s32mem.h> // RBufWriteStream
       
    28 #include <vtoken.h>
       
    29 #include <vcard.h>
       
    30 #include <vcal.h>
       
    31 
       
    32 // CONSTANTS
       
    33 _LIT(KPIMSlash, "/");
       
    34 const TInt KPIMBufferGranularity = 40;
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CPIMVersit::NewL
       
    40 // Two-phased constructor.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CPIMVersit* CPIMVersit::NewL(CPIMManager* aManager)
       
    44 {
       
    45     JELOG2(EPim);
       
    46     CPIMVersit* self = new(ELeave) CPIMVersit(aManager);
       
    47 
       
    48     CleanupStack::PushL(self);
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop(self);
       
    51 
       
    52     return self;
       
    53 }
       
    54 
       
    55 // Destructor
       
    56 CPIMVersit::~CPIMVersit()
       
    57 {
       
    58     JELOG2(EPim);
       
    59     delete iCardConverter;
       
    60     delete iCalendarConverter;
       
    61     delete iVCardFormats;
       
    62     delete iVCalendarFormats;
       
    63 }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CPIMVersit::ItemToStringL
       
    67 // Converts a PIM Item to a Versit object.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 HBufC8* CPIMVersit::ItemToStringL(const CPIMItem& aItem,
       
    71                                   Versit::TVersitCharSet aCharset)
       
    72 {
       
    73     JELOG2(EPim);
       
    74     CBufSeg* buffer = CBufSeg::NewL(KPIMBufferGranularity);
       
    75     CleanupStack::PushL(buffer);
       
    76     RBufWriteStream stream(*buffer, 0);
       
    77     stream.PushL();
       
    78     switch (aItem.ItemType())
       
    79     {
       
    80     case EPIMContactList:
       
    81     {
       
    82         iCardConverter->ItemToStreamL(stream, aItem, aCharset);
       
    83         break;
       
    84     }
       
    85     case EPIMToDoList:
       
    86     case EPIMEventList:
       
    87     {
       
    88         iCalendarConverter->ItemToStreamL(stream, aItem, aCharset);
       
    89         break;
       
    90     }
       
    91     default:
       
    92     {
       
    93         User::Leave(KErrGeneral);
       
    94     }
       
    95     }
       
    96     TInt bufferSize = buffer->Size();
       
    97     HBufC8* retVal = HBufC8::NewL(bufferSize);
       
    98     TPtr8 retDes(retVal->Des());
       
    99     buffer->Read(0, retDes, bufferSize);
       
   100     stream.Pop();
       
   101     CleanupStack::PopAndDestroy(buffer);
       
   102     return retVal;
       
   103 }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CPIMVersit::StringToItemL
       
   107 // Converts a Versit object to a PIM Item.
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 RPointerArray<CPIMItem>* CPIMVersit::StringToItemL(const TDesC8& aString,
       
   111         Versit::TVersitCharSet aCharset)
       
   112 {
       
   113     JELOG2(EPim);
       
   114     RPointerArray<CPIMItem>* itemArray =
       
   115         new(ELeave) RPointerArray<CPIMItem> (1);
       
   116     CleanupStack::PushL(itemArray);
       
   117     CleanupResetAndDestroyPushL(*itemArray);
       
   118 
       
   119     // figure out the type of the item
       
   120     // is it a vCard
       
   121     HBufC8* vCardBegin = Combine3DesC8L(KVersitTokenBEGIN(),
       
   122                                         KVersitTokenColon(), KVersitTokenVCARD());
       
   123 
       
   124     TInt isVCard = aString.Find(*vCardBegin);
       
   125     delete vCardBegin;
       
   126     if (isVCard == 0)
       
   127     {
       
   128         // this is a vCard
       
   129         StringToContactL(aString, *itemArray, aCharset);
       
   130         CleanupStack::Pop(2); // itemArray reset and destroy, itemArray object
       
   131         return itemArray;
       
   132     }
       
   133 
       
   134     HBufC8* vCalendarBegin = Combine3DesC8L(KVersitTokenBEGIN(),
       
   135                                             KVersitTokenColon(), KVersitTokenVCALENDAR());
       
   136 
       
   137     TInt isVCalendar = aString.Find(*vCalendarBegin);
       
   138     delete vCalendarBegin;
       
   139     if (isVCalendar == 0)
       
   140     {
       
   141         // this is a vCalendar
       
   142 
       
   143         RDesReadStream stream(aString);
       
   144 
       
   145         iCalendarConverter->StreamToItemL(stream, *itemArray, aCharset);
       
   146         CleanupStack::Pop(2); // itemArray reset and restroy, itemArray object
       
   147         return itemArray;
       
   148     }
       
   149     User::Leave(KErrNotSupported);
       
   150     return itemArray; // not really, just to avoid warning
       
   151 }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CPIMVersit::SupportedSerialFormatsL
       
   155 // Returns an array of supported formats for a specific pim list type.
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 const CDesCArray& CPIMVersit::SupportedSerialFormatsL(TPIMListType aPimListType) const
       
   159 {
       
   160     JELOG2(EPim);
       
   161     CDesCArray* retVal = NULL;
       
   162     switch (aPimListType)
       
   163     {
       
   164     case EPIMContactList:
       
   165     {
       
   166         retVal = iVCardFormats;
       
   167         break;
       
   168     }
       
   169     case EPIMEventList:
       
   170     case EPIMToDoList:
       
   171     {
       
   172         retVal = iVCalendarFormats;
       
   173         break;
       
   174     }
       
   175     default:
       
   176     {
       
   177         User::Leave(KErrArgument);
       
   178     }
       
   179     }
       
   180     return *retVal;
       
   181 }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CPIMVersit::CPIMVersit
       
   185 // C++ default constructor can NOT contain any code, that
       
   186 // might leave.
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 //CPIMVersit::CPIMVersit(
       
   190 //    const CPIMContactValidator& aContactValidator, // validates contacts
       
   191 //    const CPIMEventValidator& aEventValidator, // validates events
       
   192 //    const CPIMToDoValidator& aToDoValidator,  // validates todos
       
   193 //    java::util::FunctionServer* aFuncServer )
       
   194 //    : iContactValidator( aContactValidator),
       
   195 //      iEventValidator( aEventValidator ),
       
   196 //      iToDoValidator( aToDoValidator ),
       
   197 //      iFuncServer(aFuncServer)
       
   198 //    {
       
   199 //    }
       
   200 // -----------------------------------------------------------------------------
       
   201 // CPIMVersit::CPIMVersit
       
   202 // C++ default constructor can NOT contain any code, that
       
   203 // might leave.
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 CPIMVersit::CPIMVersit(CPIMManager* aManager) :
       
   207         iContactValidator(aManager->ContactValidator()), iEventValidator(
       
   208             aManager->EventValidator()), iToDoValidator(aManager->ToDoValidator()),
       
   209         iFuncServer(aManager)
       
   210 {
       
   211     JELOG2(EPim);
       
   212 }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CPIMVersit::ConstructL
       
   216 // Symbian 2nd phase constructor can leave.
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 void CPIMVersit::ConstructL()
       
   220 {
       
   221     JELOG2(EPim);
       
   222     iVCardFormats = new(ELeave) CDesC16ArrayFlat(1);
       
   223     HBufC* vCardFormat = Combine3DesCLC(KVersitVarTokenVCARD(), KPIMSlash(),
       
   224                                         KVersitTokenVCardVersionNo());
       
   225     iVCardFormats->AppendL(*vCardFormat);
       
   226     CleanupStack::PopAndDestroy(vCardFormat); // vCardFormat
       
   227 
       
   228     iVCalendarFormats = new(ELeave) CDesC16ArrayFlat(1);
       
   229     HBufC* vCalendarFormat = Combine3DesCLC(KVersitVarTokenVCALENDAR(),
       
   230                                             KPIMSlash(), KVersitTokenVCalVersionNo());
       
   231     iVCalendarFormats->AppendL(*vCalendarFormat);
       
   232     CleanupStack::PopAndDestroy(vCalendarFormat); // vCalendarFormat
       
   233 
       
   234     iCardConverter = CPIMCardConverter::NewL(iContactValidator, iFuncServer);
       
   235     iCalendarConverter = CPIMCalendarConverter::NewL(iEventValidator,
       
   236                          iToDoValidator, iFuncServer);
       
   237 }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CPIMVersit::StringToContact
       
   241 // Symbian 2nd phase constructor can leave.
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CPIMVersit::StringToContactL(const TDesC8& aString, // contains a vCard
       
   245                                   RPointerArray<CPIMItem>& aItemArray, // items are stored here
       
   246                                   Versit::TVersitCharSet aCharset) // defines the used character set
       
   247 {
       
   248     JELOG2(EPim);
       
   249     // vCard specification says that END:VCARD is optional.
       
   250     // However, Symbian Versit parser chokes without it. So,
       
   251     // we have to check if one exists
       
   252     HBufC8* vCardEnd = HBufC8::NewL(KVersitTokenCRLF().Length()
       
   253                                     + KVersitTokenEND().Length() + KVersitTokenVCARD().Length()
       
   254                                     + KVersitTokenColon().Length());
       
   255     CleanupStack::PushL(vCardEnd);
       
   256     TPtr8 vCardEndDes = vCardEnd->Des();
       
   257     vCardEndDes = KVersitTokenCRLF();
       
   258     vCardEndDes.Append(KVersitTokenEND());
       
   259     vCardEndDes.Append(KVersitTokenColon());
       
   260     vCardEndDes.Append(KVersitTokenVCARD());
       
   261     TInt endExist = aString.Find(*vCardEnd);
       
   262     if (endExist != KErrNotFound)
       
   263     {
       
   264         RDesReadStream stream(aString);
       
   265         iCardConverter->StreamToItemL(stream, aItemArray, aCharset);
       
   266     }
       
   267     else
       
   268     {
       
   269         HBufC8* vCard =
       
   270             Combine3DesC8L(aString, vCardEndDes, KVersitLineBreak());
       
   271         CleanupStack::PushL(vCard);
       
   272         RDesReadStream stream(*vCard);
       
   273         iCardConverter->StreamToItemL(stream, aItemArray, aCharset);
       
   274         CleanupStack::PopAndDestroy(vCard); // vCard
       
   275     }
       
   276     CleanupStack::PopAndDestroy(vCardEnd); // vCardEnd
       
   277 
       
   278 }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CPIMVersit::Combine3DesC8L
       
   282 // Combines 3 8-bit descriptors into one
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 HBufC8* CPIMVersit::Combine3DesC8L(const TDesC8& aDesC1, const TDesC8& aDesC2,
       
   286                                    const TDesC8& aDesC3)
       
   287 {
       
   288     JELOG2(EPim);
       
   289     HBufC8* retVal = HBufC8::NewL(aDesC1.Length() + aDesC2.Length()
       
   290                                   + aDesC3.Length());
       
   291     TPtr8 retValDes = retVal->Des();
       
   292     retValDes = aDesC1;
       
   293     retValDes.Append(aDesC2);
       
   294     retValDes.Append(aDesC3);
       
   295     return retVal;
       
   296 }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CPIMVersit::Combine3DesCLC
       
   300 // Combines 3 16-bit descriptors into one, which is left in cleanup stack
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 HBufC* CPIMVersit::Combine3DesCLC(const TDesC& aDesC1, const TDesC& aDesC2,
       
   304                                   const TDesC& aDesC3)
       
   305 {
       
   306     JELOG2(EPim);
       
   307     HBufC* retVal = HBufC::NewL(aDesC1.Length() + aDesC2.Length()
       
   308                                 + aDesC3.Length());
       
   309     TPtr retValDes = retVal->Des();
       
   310     retValDes = aDesC1;
       
   311     retValDes.Append(aDesC2);
       
   312     retValDes.Append(aDesC3);
       
   313     CleanupStack::PushL(retVal);
       
   314     return retVal;
       
   315 }
       
   316 
       
   317 //  End of File