omads/omadsextensions/adapters/contactsgroup/src/contactsgrpconverter.cpp
branchRCL_3
changeset 52 4f0867e42d62
parent 51 8e7494275d3a
child 56 3e6957da2ff8
equal deleted inserted replaced
51:8e7494275d3a 52:4f0867e42d62
     1 /*
       
     2 * Copyright (c) 2008-2010 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:  Contacts Group object conversion routines
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include <utf.h>
       
    21 #include <cntdef.h>
       
    22 #include <cntitem.h>
       
    23 #include <cntfldst.h>
       
    24 #include <vcard.h>
       
    25 #include <vprop.h> 
       
    26 #include <vutil.h>
       
    27 #include <s32mem.h>
       
    28 
       
    29 #include "contactsgrpconverter.h"
       
    30 #include "logger.h"
       
    31 
       
    32 // CONSTANTS
       
    33 /*
       
    34 vCard format example:
       
    35 BEGIN:VCARD
       
    36 VERSION:2.1
       
    37 FN:GroupName
       
    38 X-CNTGRP:4;14;54
       
    39 END:VCARD
       
    40 */
       
    41 
       
    42 // Group label property
       
    43 _LIT8(KCntGrpFN, "FN");
       
    44 // Group contacts propetry
       
    45 _LIT8(KCntGrpXCntGrp, "X-CNTGRP");
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CContactsGrpConverter::NewL
       
    49 // Two-phased constructor.
       
    50 // -----------------------------------------------------------------------------
       
    51 CContactsGrpConverter* CContactsGrpConverter::NewL()
       
    52     {
       
    53     CContactsGrpConverter* self = CContactsGrpConverter::NewLC();
       
    54     CleanupStack::Pop( self );
       
    55     return self;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CContactsGrpConverter::NewLC
       
    60 // Two-phased constructor.
       
    61 // -----------------------------------------------------------------------------
       
    62 CContactsGrpConverter* CContactsGrpConverter::NewLC()
       
    63     {
       
    64     TRACE_FUNC_ENTRY;
       
    65     CContactsGrpConverter* self = new (ELeave) CContactsGrpConverter();
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     TRACE_FUNC_EXIT;
       
    69     return self;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CContactsGrpConverter::CContactsGrpConverter
       
    74 // C++ default constructor can NOT contain any code, that might leave
       
    75 // -----------------------------------------------------------------------------
       
    76 CContactsGrpConverter::CContactsGrpConverter()
       
    77     {
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CContactsGrpConverter::ConstructL
       
    82 // Symbian 2nd phase constructor can leave.
       
    83 // -----------------------------------------------------------------------------
       
    84 void CContactsGrpConverter::ConstructL()
       
    85     {
       
    86     ResetL();
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CContactsGrpConverter::~CContactsGrpConverter
       
    91 // Destructor.
       
    92 // -----------------------------------------------------------------------------
       
    93 CContactsGrpConverter::~CContactsGrpConverter()
       
    94     {
       
    95     TRACE_FUNC_ENTRY;
       
    96     delete iContactsIdArray;
       
    97     delete iGroupLabel;
       
    98     
       
    99     TRACE_FUNC_EXIT;
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CContactsGrpConverter::GroupLabel
       
   104 // returns group label
       
   105 // -----------------------------------------------------------------------------
       
   106 const TDesC& CContactsGrpConverter::GroupLabel() const
       
   107     {
       
   108     return *iGroupLabel;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CContactsGrpConverter::ItemsContained
       
   113 // returns contact items belonging to this group
       
   114 // -----------------------------------------------------------------------------
       
   115 const CContactIdArray& CContactsGrpConverter::ItemsContained() const
       
   116     {
       
   117     return *iContactsIdArray;
       
   118     }
       
   119     
       
   120 // -----------------------------------------------------------------------------
       
   121 // CContactsGrpConverter::ImportDbItemL
       
   122 // Imports group item
       
   123 // -----------------------------------------------------------------------------
       
   124 void CContactsGrpConverter::ImportDbItemL( CContactGroup& aItem )
       
   125     {
       
   126     TRACE_FUNC_ENTRY;
       
   127     if ( aItem.Type() != KUidContactGroup )
       
   128         {
       
   129         User::Leave( KErrNotSupported );
       
   130         }
       
   131     ResetL();
       
   132     
       
   133     if ( aItem.HasItemLabelField() )
       
   134         {
       
   135         TPtrC label = aItem.GetGroupLabelL();
       
   136         delete iGroupLabel;
       
   137         iGroupLabel = NULL;
       
   138         iGroupLabel = label.AllocL();
       
   139         }
       
   140     else
       
   141         {
       
   142         delete iGroupLabel;
       
   143         iGroupLabel = NULL;
       
   144         iGroupLabel = KNullDesC().AllocL();
       
   145         }
       
   146     
       
   147     // this is NULL, if no items
       
   148     CContactIdArray* tempIdArray = aItem.ItemsContainedLC();
       
   149     if ( tempIdArray )
       
   150         {
       
   151         delete iContactsIdArray;
       
   152         iContactsIdArray = tempIdArray;
       
   153         CleanupStack::Pop( tempIdArray );
       
   154         }
       
   155     
       
   156     LOGGER_WRITE_1("GroupName: %S", iGroupLabel);
       
   157     if ( iContactsIdArray )
       
   158         {
       
   159         LOGGER_WRITE_1("contacts count: %d", iContactsIdArray->Count() );
       
   160         }
       
   161     TRACE_FUNC_EXIT;
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CContactsGrpConverter::ExportVCardL
       
   166 // Export contact group as vCard
       
   167 // -----------------------------------------------------------------------------
       
   168 void CContactsGrpConverter::ExportVCardL( CBufBase& aBuffer )
       
   169     {
       
   170     TRACE_FUNC_ENTRY;
       
   171     RBufWriteStream stream( aBuffer, 0);
       
   172     CleanupClosePushL( stream );
       
   173     ExportVCardL( stream );
       
   174     CleanupStack::PopAndDestroy( &stream );
       
   175     TRACE_FUNC_EXIT;
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CContactsGrpConverter::ExportL
       
   180 // Exports contact group as to vCard
       
   181 // -----------------------------------------------------------------------------
       
   182 void CContactsGrpConverter::ExportVCardL( RWriteStream& aWriteStream )
       
   183     {
       
   184     TRACE_FUNC_ENTRY;
       
   185     CParserVCard* vCardParser = CParserVCard::NewL();
       
   186     CleanupStack::PushL( vCardParser );
       
   187     
       
   188     // add group name
       
   189     CParserPropertyValue* FNvalue = CParserPropertyValueHBufC::NewL( *iGroupLabel );
       
   190     // Associate the property params and property value with the FN property
       
   191     CParserProperty* FNproperty=CParserProperty::NewL( *FNvalue, KCntGrpFN, NULL );
       
   192     // Finally add the FN property to the vCard using the parser object.
       
   193     vCardParser->AddPropertyL( FNproperty );
       
   194     
       
   195     // add group contacts
       
   196     const TInt KDefaultGranularity = 5;
       
   197     CDesCArrayFlat* flatArray = new (ELeave) CDesCArrayFlat(KDefaultGranularity);
       
   198     CleanupStack::PushL( flatArray );
       
   199     TInt count = iContactsIdArray->Count();
       
   200     const TInt KMaxIdLength = 10;
       
   201     TBuf<KMaxIdLength> idnum;
       
   202     for ( TInt i=0; i<count; i++ )
       
   203         {
       
   204         const TContactItemId id = (*iContactsIdArray)[i];
       
   205         idnum.Zero();
       
   206         idnum.AppendNum( id );
       
   207         flatArray->AppendL( idnum );
       
   208         }
       
   209     CParserPropertyValueCDesCArray* grPropertyValueArray =
       
   210         new (ELeave) CParserPropertyValueCDesCArray( flatArray );
       
   211     CleanupStack::Pop( flatArray );
       
   212     
       
   213     CParserProperty* grProperty = 
       
   214         CParserProperty::NewL( *grPropertyValueArray, KCntGrpXCntGrp, NULL );
       
   215     vCardParser->AddPropertyL( grProperty );
       
   216     
       
   217     // Set default sharacterset
       
   218     vCardParser->SetDefaultCharSet( Versit::EUTF8CharSet );
       
   219     
       
   220     vCardParser->ExternalizeL( aWriteStream );
       
   221     
       
   222     CleanupStack::PopAndDestroy( vCardParser );
       
   223     TRACE_FUNC_EXIT;
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CContactsGrpConverter::ImportVCardL
       
   228 // Import vCard item
       
   229 // -----------------------------------------------------------------------------
       
   230 void CContactsGrpConverter::ImportVCardL( const TDesC8& aBuffer )
       
   231     {
       
   232     TRACE_FUNC_ENTRY;
       
   233     ResetL();
       
   234     CParserVCard* vCardParser = CParserVCard::NewL();
       
   235     vCardParser->SetDefaultCharSet( Versit::EUTF8CharSet );
       
   236     CleanupStack::PushL( vCardParser );
       
   237     
       
   238     RDesReadStream stream( aBuffer );
       
   239     vCardParser->InternalizeL( stream ); // To import a vCard.
       
   240     stream.Close(); // Close the file stream once the import is done.
       
   241     
       
   242     CArrayPtr<CParserProperty>* properties = vCardParser->ArrayOfProperties( EFalse );
       
   243     LOGGER_WRITE_1("properties count: %d", properties->Count());
       
   244     for (TInt i=0; i<properties->Count(); i++)
       
   245         {
       
   246         const CParserProperty* property = properties->At(i);
       
   247         // FN
       
   248         if ( property->Name() == KCntGrpFN )
       
   249             {
       
   250             LOGGER_WRITE("group name found");
       
   251             delete iGroupLabel;
       
   252             iGroupLabel = NULL;
       
   253             CParserPropertyValue* pvalue = property->Value();
       
   254             TPtrC val = (static_cast<CParserPropertyValueHBufC*> (pvalue))->Value();
       
   255             iGroupLabel = val.AllocL();
       
   256             LOGGER_WRITE_1("iGroupLabel: %S", iGroupLabel);
       
   257             }
       
   258         // X-CNTGRP
       
   259         else if ( property->Name() == KCntGrpXCntGrp )
       
   260             {
       
   261             LOGGER_WRITE("group contacts found");
       
   262             CParserPropertyValue* pvalue = property->Value();
       
   263             TPtrC val = (static_cast<CParserPropertyValueHBufC*> (pvalue))->Value();
       
   264             SetContactItemsL( val );
       
   265             }
       
   266         else
       
   267             {
       
   268             // skip other properties
       
   269             }
       
   270         }
       
   271     CleanupStack::PopAndDestroy( vCardParser );
       
   272     TRACE_FUNC_EXIT;
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // CContactsGrpConverter::ResetL
       
   277 // Reset all data
       
   278 // -----------------------------------------------------------------------------
       
   279 void CContactsGrpConverter::ResetL()
       
   280     {
       
   281     delete iGroupLabel;
       
   282     iGroupLabel = NULL;
       
   283     iGroupLabel = KNullDesC().AllocL();
       
   284     if ( iContactsIdArray )
       
   285         {
       
   286         iContactsIdArray->Reset();
       
   287         }
       
   288     else
       
   289         {
       
   290         iContactsIdArray = CContactIdArray::NewL();
       
   291         }
       
   292     }
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // CContactsGrpConverter::SetContactItemsL
       
   296 // Parses contact items from aValue
       
   297 // -----------------------------------------------------------------------------
       
   298 void CContactsGrpConverter::SetContactItemsL( const TDesC& aValue )
       
   299     {
       
   300     TRACE_FUNC_ENTRY;
       
   301     const TInt KDefaultGranularity = 5;
       
   302     RArray<TPtrC> arr(KDefaultGranularity);
       
   303     CleanupClosePushL( arr );
       
   304     const TChar separator(';');
       
   305     SplitL( aValue, separator, arr );
       
   306     if ( iContactsIdArray )
       
   307         {
       
   308         iContactsIdArray->Reset();
       
   309         }
       
   310     else
       
   311         {
       
   312         iContactsIdArray = CContactIdArray::NewL();
       
   313         }
       
   314     for (TInt i=0; i<arr.Count(); i++ )
       
   315         {
       
   316         TLex lex( arr[i] );
       
   317         TInt32 id;
       
   318         TInt err = lex.Val( id );
       
   319         if ( err )
       
   320             {
       
   321             LOGGER_WRITE_1("lex.val returned err: %d, leaving with KErrCorrupt", err);
       
   322             iContactsIdArray->Reset();
       
   323             User::Leave( KErrCorrupt );
       
   324             }
       
   325         LOGGER_WRITE_1("add: %d", id);
       
   326         iContactsIdArray->AddL( id );
       
   327         }
       
   328     CleanupStack::PopAndDestroy( &arr );
       
   329     TRACE_FUNC_EXIT;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CContactsGrpConverter::SplitL
       
   334 // Function splits string (eg "name1, name2, name3") into substrings.
       
   335 // -----------------------------------------------------------------------------
       
   336 void CContactsGrpConverter::SplitL(const TDesC& aText, const TChar aSeparator, 
       
   337                     RArray<TPtrC>& aArray)
       
   338     {
       
   339     TRACE_FUNC_ENTRY;
       
   340     TPtrC ptr;
       
   341     ptr.Set(aText);
       
   342 
       
   343     for (;;)
       
   344         {
       
   345         TInt pos=ptr.Locate(aSeparator);
       
   346         if (pos==KErrNotFound)
       
   347             {
       
   348             TrimAll( ptr );
       
   349             if ( ptr.Length() > 0 )
       
   350                 {
       
   351                 aArray.AppendL(ptr);
       
   352                 }
       
   353             break;
       
   354             }
       
   355 
       
   356         TPtrC subStr=ptr.Left(pos); // get pos characters starting from position 0
       
   357         TrimAll( subStr );
       
   358         if ( subStr.Length() > 0 )
       
   359             {
       
   360             aArray.AppendL(subStr);
       
   361             }
       
   362 
       
   363         if ( !(ptr.Length()>pos+1) )
       
   364             {
       
   365             break;
       
   366             }
       
   367             
       
   368         ptr.Set( ptr.Mid(pos+1) ); // get all characters starting from position pos+1
       
   369         }
       
   370     TRACE_FUNC_EXIT;
       
   371     }
       
   372 
       
   373 // -----------------------------------------------------------------------------
       
   374 // CContactsGrpConverter::TrimAll
       
   375 // Trims all whitespaces from beginning and end of string
       
   376 // -----------------------------------------------------------------------------
       
   377 void CContactsGrpConverter::TrimAll( TPtrC& aValue )
       
   378     {
       
   379     TRACE_FUNC_ENTRY;
       
   380     TInt valueStart(0);
       
   381     while ( valueStart < aValue.Length() 
       
   382             && (aValue[valueStart] == ' '
       
   383             || aValue[valueStart] == '\r'
       
   384             || aValue[valueStart] == '\n') )
       
   385         {
       
   386         valueStart++;
       
   387         }
       
   388     TInt valueEnd(aValue.Length()-1);
       
   389     while ( valueEnd > 0 
       
   390             && (aValue[valueEnd] == ' '
       
   391             || aValue[valueEnd] == '\r'
       
   392             || aValue[valueEnd] == '\n') )
       
   393         {
       
   394         valueEnd--;
       
   395         }
       
   396     valueEnd++;
       
   397     aValue.Set( aValue.Mid(valueStart, valueEnd - valueStart));
       
   398     TRACE_FUNC_EXIT;
       
   399     }
       
   400