contentpublishingsrv/contentharvester/factorysettingsplugin/src/chfactorysettingsdomparser.cpp
changeset 93 82b66994846c
parent 92 782e3408c2ab
child 94 dbb8300717f7
equal deleted inserted replaced
92:782e3408c2ab 93:82b66994846c
     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:  factory settings for content harvester
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <driveinfo.h>
       
    22 #include <xmlengnodelist.h>
       
    23 #include <xmlengdomparser.h>
       
    24 #include <xmlengdocument.h>
       
    25 #include <utf.h>
       
    26 #include <liwservicehandler.h>
       
    27 #include "chfactorysettingsdomparser.h"
       
    28 #include "chfactorysettingsglobals.h"
       
    29 #include "chfactsetutils.h"
       
    30 #include "cpdebug.h"
       
    31 
       
    32 // CONSTANTS
       
    33 _LIT8( KMainDataItem8, "data" );
       
    34 _LIT8( KMainPublisherItem8, "publisher" );
       
    35 _LIT8( KType, "type" );
       
    36 _LIT8( KItem, "item" );
       
    37 _LIT8( KData, "data" );
       
    38 _LIT8( KAdd, "Add" );
       
    39 _LIT8( KDelete, "Delete" );
       
    40 _LIT8( KCpData, "cp_data" );
       
    41 _LIT8( KPublisher, "publisher" );
       
    42 
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CCHFactorySettingsDOMParser::CCHFactorySettingsDOMParser
       
    48 // C++ default constructor can NOT contain any code, that
       
    49 // might leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CCHFactorySettingsDOMParser::CCHFactorySettingsDOMParser( RFs& aFs ):
       
    53     iFs( aFs )
       
    54     {
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CCHFactorySettingsDOMParser::ConstructL
       
    59 // Symbian 2nd phase constructor can leave.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void CCHFactorySettingsDOMParser::ConstructL( MLiwInterface* aInterface )
       
    63     {
       
    64     iCPSInterface = aInterface;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CCHFactorySettingsDOMParser::NewL
       
    69 // Two-phased constructor.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CCHFactorySettingsDOMParser* CCHFactorySettingsDOMParser::NewL(
       
    73         MLiwInterface* aInterface, RFs& aFs )
       
    74     {
       
    75     CCHFactorySettingsDOMParser* self = NewLC( aInterface, aFs );
       
    76     CleanupStack::Pop( self );
       
    77     return self;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CCHFactorySettingsDOMParser::NewLC
       
    82 // Two-phased constructor.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CCHFactorySettingsDOMParser* CCHFactorySettingsDOMParser::NewLC(
       
    86         MLiwInterface* aInterface, RFs& aFs )
       
    87     {
       
    88     CCHFactorySettingsDOMParser* self = new( ELeave ) CCHFactorySettingsDOMParser( aFs );
       
    89     CleanupStack::PushL( self );
       
    90     self->ConstructL( aInterface );
       
    91     return self;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CCHFactorySettingsDOMParser::~CCHFactorySettingsDOMParser
       
    96 // Destructor
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 CCHFactorySettingsDOMParser::~CCHFactorySettingsDOMParser()
       
   100     {
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CCHFactorySettingsDOMParser::Restore
       
   105 // (other items were commented in a header).
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 TInt CCHFactorySettingsDOMParser::Restore( const TDesC& aStr, TBool aUnInstall )
       
   109     {
       
   110     TRAPD( err, ParseFactorySettingsFileL( aStr, aUnInstall ) );
       
   111     return err;
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 //  CCHFactorySettingsDOMParser::ParseFactorySettingsFileL
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CCHFactorySettingsDOMParser::ParseFactorySettingsFileL(
       
   119         const TDesC& aFilePath, TBool aUnInstall )
       
   120     {
       
   121     RXmlEngDOMImplementation domImpl;
       
   122     CleanupClosePushL( domImpl );
       
   123     domImpl.OpenL();
       
   124 
       
   125     RXmlEngDOMParser parser;
       
   126     CleanupClosePushL( parser );
       
   127     User::LeaveIfError( parser.Open( domImpl ) );
       
   128 
       
   129     RXmlEngDocument doc = parser.ParseFileL( aFilePath );
       
   130     CleanupClosePushL( doc );
       
   131 
       
   132     TXmlEngElement docElement = doc.AsElement(); // cast node to element node
       
   133 
       
   134     TXmlEngElement element = docElement.FirstChild().AsElement();
       
   135 
       
   136     RXmlEngNodeList<TXmlEngElement> elementList;
       
   137     CleanupClosePushL( elementList );
       
   138 
       
   139     element.GetChildElements( elementList );// to remove
       
   140 
       
   141     while( elementList.HasNext() )
       
   142         {
       
   143         element = elementList.Next();
       
   144         TPtrC8 typeIdentifier( element.Name() );
       
   145         if( !( typeIdentifier.CompareC( KMainDataItem8 ) ) ||
       
   146             !( typeIdentifier.CompareC( KMainPublisherItem8 ) ) )
       
   147             {
       
   148             CLiwGenericParamList* inparam = CLiwGenericParamList::NewLC();
       
   149             CLiwGenericParamList* outparam = CLiwGenericParamList::NewLC();
       
   150             CLiwDefaultMap* map = CLiwDefaultMap::NewLC();
       
   151 
       
   152             if( !( typeIdentifier.CompareC( KMainDataItem8 ) ) )
       
   153                 {
       
   154                 TLiwGenericParam item_type( KType, TLiwVariant( KCpData ));
       
   155                 inparam->AppendL( item_type );
       
   156                 }
       
   157             else
       
   158                 {
       
   159                 TLiwGenericParam item_type( KType, TLiwVariant( KPublisher ));
       
   160                 inparam->AppendL( item_type );
       
   161                 }
       
   162 
       
   163             if( aUnInstall )
       
   164                 {
       
   165                 // remove
       
   166                 RemoveItemL(element, map);
       
   167                 TLiwGenericParam item( KData, TLiwVariant( map ));
       
   168                 inparam->AppendL( item );
       
   169                 iCPSInterface->ExecuteCmdL( KDelete, *inparam, *outparam );
       
   170                 }
       
   171             else
       
   172                 {
       
   173                 // add
       
   174                 AddItemL( element, map, EMap );
       
   175                 TLiwGenericParam item( KItem, TLiwVariant( map ));
       
   176                 inparam->AppendL( item );
       
   177                 iCPSInterface->ExecuteCmdL( KAdd, *inparam, *outparam );
       
   178                 }
       
   179 
       
   180             CleanupStack::PopAndDestroy( map );
       
   181             CleanupStack::PopAndDestroy( outparam );
       
   182             CleanupStack::PopAndDestroy( inparam );
       
   183             }
       
   184         }
       
   185 
       
   186     CleanupStack::PopAndDestroy( &elementList );
       
   187     CleanupStack::PopAndDestroy( &doc );
       
   188     CleanupStack::PopAndDestroy( &parser );
       
   189     CleanupStack::PopAndDestroy( &domImpl );
       
   190 
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CCHFactorySettingsDOMParser::AddItemL
       
   195 // (other items were commented in a header).
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CCHFactorySettingsDOMParser::AddItemL( const TXmlEngElement& aNode,
       
   199     CLiwContainer* aContainer, TTypes aType )
       
   200     {
       
   201     RXmlEngNodeList<TXmlEngElement> children;
       
   202     CleanupClosePushL( children );
       
   203     aNode.GetChildElements( children );
       
   204 
       
   205     while( children.HasNext() )
       
   206         {
       
   207         TXmlEngElement node = children.Next();
       
   208         TPtrC8 typeIdentifier( node.Name() );
       
   209         if( !( typeIdentifier.CompareC( KDataItem8 ) ) )
       
   210             {
       
   211             switch ( aType )
       
   212                 {
       
   213                 case EMap:
       
   214                     ExtractAttributesAndAddL( node,
       
   215                         static_cast<CLiwDefaultMap*>(aContainer) );
       
   216                     break;
       
   217                 case EList:
       
   218                     ExtractAttributesAndAddL( node,
       
   219                         static_cast<CLiwDefaultList*>( aContainer ) );
       
   220                     break;
       
   221                 default:
       
   222                     {
       
   223                     User::Panic( _L("Wrong Type" ),1 );
       
   224                     }
       
   225                 }
       
   226             }
       
   227         }
       
   228 
       
   229     CleanupStack::PopAndDestroy( &children );
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CCHFactorySettingsDOMParser::RemoveItemL
       
   234 // (other items were commented in a header).
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CCHFactorySettingsDOMParser::RemoveItemL( const TXmlEngElement& aNode,
       
   238     CLiwDefaultMap* aMap )
       
   239     {
       
   240     CP_DEBUG(_L8("CCHFactorySettingsDOMParser::RemoveItemL" ));
       
   241     RXmlEngNodeList<TXmlEngElement> children;
       
   242     CleanupClosePushL( children );
       
   243 
       
   244     aNode.GetChildElements( children );
       
   245 
       
   246     while( children.HasNext() )
       
   247         {
       
   248         TXmlEngElement node = children.Next();
       
   249         TPtrC8 typeIdentifier( node.Name() );
       
   250         if( !( typeIdentifier.CompareC( KDataItem8 ) ) )
       
   251             {
       
   252             ExtractDeleteAttributesL( node, aMap );
       
   253             }
       
   254         }
       
   255     CleanupStack::PopAndDestroy( &children );
       
   256     }
       
   257 
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 //  CCHFactorySettingsDOMParser::ExtractDeleteAttributesL
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void CCHFactorySettingsDOMParser::ExtractDeleteAttributesL(
       
   264 		const TXmlEngElement& aNode,
       
   265 		CLiwDefaultMap* aMap )
       
   266     {
       
   267     CP_DEBUG(_L8("CCHFactorySettingsDOMParser::ExtractDeleteAttributesL" ));
       
   268 
       
   269     TPtrC8 key = aNode.AttributeValueL(KAttributeKey8);
       
   270     TPtrC8 value = aNode.AttributeValueL(KAttributeValue8);
       
   271     HBufC* type = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   272             aNode.AttributeValueL(KAttributeType8));
       
   273     CleanupStack::PushL( type );
       
   274 
       
   275     if ( key.Compare( KNullDesC8 ) != 0
       
   276         && type->Compare( KNullDesC ) != 0
       
   277     	&& value.Compare( KNullDesC8 ) != 0 )
       
   278 		{
       
   279 		HBufC* value16 = NULL;
       
   280 		switch ( ConvertToType( *type ) )
       
   281 			{
       
   282 			case EDescriptor8:
       
   283 				aMap->InsertL( key, value );
       
   284 				break;
       
   285 			case EDescriptor16:
       
   286 			    value16 = CnvUtfConverter::ConvertToUnicodeFromUtf8L( value );
       
   287 			    CleanupStack::PushL( value16 );
       
   288 				aMap->InsertL( key, *value16 );
       
   289 			    CleanupStack::PopAndDestroy( value16 );
       
   290 				break;
       
   291 			default:
       
   292 				{
       
   293 				//wrong type - ignore
       
   294 				}
       
   295 			}
       
   296 		}
       
   297     CleanupStack::PopAndDestroy( type );
       
   298     }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 //  CCHFactorySettingsDOMParser::ExtractAttributesAndAddL
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 void CCHFactorySettingsDOMParser::ExtractAttributesAndAddL(
       
   305 		const TXmlEngElement& aNode, CLiwDefaultMap* aMap )
       
   306     {
       
   307     CP_DEBUG(_L8("CCHFactorySettingsDOMParser::ExtractAttributesAndAddL" ));
       
   308     TPtrC8 key = aNode.AttributeValueL(KAttributeKey8);
       
   309     HBufC* type = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   310             aNode.AttributeValueL(KAttributeType8));
       
   311     CleanupStack::PushL( type );
       
   312 
       
   313     if ( key.Compare( KNullDesC8 ) != 0
       
   314         && type->Compare( KNullDesC ) != 0 )
       
   315 		{
       
   316 	    HBufC* value = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   317 	            aNode.AttributeValueL(KAttributeValue8));
       
   318 	    CleanupStack::PushL( value );
       
   319         TTypes typeCheck = ConvertToType( *type );
       
   320         //check if value exist
       
   321         //it is not needed in case of Map and List
       
   322         if ( value->Compare( KNullDesC ) != 0
       
   323         		|| typeCheck == EMap
       
   324         		|| typeCheck == EList )
       
   325             {
       
   326             HBufC8* bufV = NULL;
       
   327             CLiwDefaultMap* nestedMap = NULL;
       
   328             CLiwDefaultList* nestedList = NULL;
       
   329 
       
   330             switch ( typeCheck )
       
   331                 {
       
   332                 case EInteger:
       
   333                     aMap->InsertL( key, FactSetCHUtils::Str2Int32( *value ) );
       
   334                     break;
       
   335                 case EUInteger:
       
   336                 	aMap->InsertL( key, FactSetCHUtils::Str2Uint( *value ) );
       
   337                     break;
       
   338                 case EDescriptor8:
       
   339                     bufV = FactSetCHUtils::Str2Str8LC( *value );
       
   340                     aMap->InsertL( key, *bufV );
       
   341                     CleanupStack::PopAndDestroy( bufV );
       
   342                     break;
       
   343                 case EDescriptor16:
       
   344                 	aMap->InsertL( key, *value );
       
   345                     break;
       
   346                 case EMap:
       
   347                 	nestedMap = CLiwDefaultMap::NewLC();
       
   348                     AddItemL( aNode, nestedMap, EMap );
       
   349                     aMap->InsertL( key, nestedMap );
       
   350                     CleanupStack::PopAndDestroy( nestedMap );
       
   351                     break;
       
   352                 case EList:
       
   353                 	nestedList = CLiwDefaultList::NewLC();
       
   354                     AddItemL( aNode, nestedList, EList );
       
   355                     aMap->InsertL( key, nestedList );
       
   356                     CleanupStack::PopAndDestroy( nestedList );
       
   357                     break;
       
   358                 case EBool:
       
   359                 	aMap->InsertL( key, FactSetCHUtils::Str2Bool( *value ) );
       
   360                     break;
       
   361                 case EReal:
       
   362                 	aMap->InsertL( key, FactSetCHUtils::Str2Real( *value ) );
       
   363                     break;
       
   364                 case EUid:
       
   365                 	aMap->InsertL( key, FactSetCHUtils::Str2Uid( *value ) );
       
   366                     break;
       
   367                 default:
       
   368                     {
       
   369                     //wrong type - ignore
       
   370                     }
       
   371                 }
       
   372             }
       
   373         CleanupStack::PopAndDestroy( value );
       
   374 		}
       
   375     CleanupStack::PopAndDestroy( type );
       
   376     }
       
   377 
       
   378 
       
   379 // -----------------------------------------------------------------------------
       
   380 // CCHFactorySettingsDOMParser::ExtractAttributesAndAddL
       
   381 // -----------------------------------------------------------------------------
       
   382 //
       
   383 void CCHFactorySettingsDOMParser::ExtractAttributesAndAddL(
       
   384         const TXmlEngElement& aNode, CLiwDefaultList* aList )
       
   385     {
       
   386     CP_DEBUG( _L8( "CCHFactorySettingsDOMParser::ExtractAttributesAndAddL" ) );
       
   387 
       
   388     HBufC* type = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   389             aNode.AttributeValueL(KAttributeType8));
       
   390     CleanupStack::PushL( type );
       
   391 
       
   392     if( type->Compare( KNullDesC ) != 0 )
       
   393         {
       
   394         HBufC* value = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
   395                 aNode.AttributeValueL(KAttributeValue8));
       
   396         CleanupStack::PushL( value );
       
   397         TTypes typeCheck = ConvertToType( *type );
       
   398         //check if value exist
       
   399         //it is not needed in case of Map and List
       
   400         if( value->Compare( KNullDesC ) != 0
       
   401                 || typeCheck == EMap
       
   402                 || typeCheck == EList )
       
   403             {
       
   404             HBufC8* bufV = NULL;
       
   405             CLiwDefaultMap* nestedMap = NULL;
       
   406             CLiwDefaultList* nestedList = NULL;
       
   407 
       
   408             switch( typeCheck )
       
   409                 {
       
   410                 case EInteger:
       
   411                     aList->AppendL( FactSetCHUtils::Str2Int32( *value ) );
       
   412                     break;
       
   413                 case EUInteger:
       
   414                     aList->AppendL( FactSetCHUtils::Str2Uint( *value ) );
       
   415                     break;
       
   416                 case EDescriptor8:
       
   417                     bufV = FactSetCHUtils::Str2Str8LC( *value );
       
   418                     aList->AppendL( *bufV );
       
   419                     CleanupStack::PopAndDestroy( bufV );
       
   420                     break;
       
   421                 case EDescriptor16:
       
   422                     aList->AppendL( value );
       
   423                     break;
       
   424                 case EMap:
       
   425                     nestedMap = CLiwDefaultMap::NewLC();
       
   426                     AddItemL( aNode, nestedMap, EMap );
       
   427                     aList->AppendL( nestedMap );
       
   428                     CleanupStack::PopAndDestroy( nestedMap );
       
   429                     break;
       
   430                 case EList:
       
   431                     nestedList = CLiwDefaultList::NewLC();
       
   432                     AddItemL( aNode, nestedList, EList );
       
   433                     aList->AppendL( nestedList );
       
   434                     CleanupStack::PopAndDestroy( nestedList );
       
   435                     break;
       
   436                 case EBool:
       
   437                     aList->AppendL( FactSetCHUtils::Str2Bool( *value ) );
       
   438                     break;
       
   439                 case EReal:
       
   440                     aList->AppendL( FactSetCHUtils::Str2Real( *value ) );
       
   441                     break;
       
   442                 case EUid:
       
   443                     aList->AppendL( FactSetCHUtils::Str2Uid( *value ) );
       
   444                     break;
       
   445                 default:
       
   446                     {
       
   447                     //wrong type - ignore
       
   448                     }
       
   449                 }
       
   450             }
       
   451         CleanupStack::PopAndDestroy( value );
       
   452         }
       
   453     CleanupStack::PopAndDestroy( type );
       
   454     }
       
   455 
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // CCHFactorySettingsDOMParser::ConvertToType
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 CCHFactorySettingsDOMParser::TTypes CCHFactorySettingsDOMParser::ConvertToType(
       
   462     const TDesC& aType )
       
   463     {
       
   464     CP_DEBUG(_L8("CCHFactorySettingsDOMParser::ConvertToType" ));
       
   465     if ( !aType.Compare( KInteger ) )
       
   466         return EInteger;
       
   467     if ( !aType.Compare( KUInteger ) )
       
   468         return EUInteger;
       
   469     if ( !aType.Compare( KDescriptor8 ) )
       
   470         return EDescriptor8;
       
   471     if ( !aType.Compare( KDescriptor16 ) )
       
   472         return EDescriptor16;
       
   473     if ( !aType.Compare( KMap ) )
       
   474         return EMap;
       
   475     if ( !aType.Compare( KList ) )
       
   476         return EList;
       
   477     if ( !aType.Compare( KBool ) )
       
   478         return EBool;
       
   479     if ( !aType.Compare( KReal ) )
       
   480         return EReal;
       
   481     if ( !aType.Compare( KUid ) )
       
   482         return EUid;
       
   483     return EUndefined;
       
   484     }