contextframework/cfw/src/basicoperationsplugin/cfclausenode.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2007-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:  CCFClauseNode class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include "cfclausenode.h"
       
    22 #include "cfscriptaction.h"
       
    23 #include "cfnotifyaction.h"
       
    24 #include "cfpublishcontextaction.h"
       
    25 #include "cfsourcecommandaction.h"
       
    26 #include "cfbasicoptrace.h"
       
    27 
       
    28 #include <cfcontextinterface.h>
       
    29 #include <cfcontextobject.h>
       
    30 #include <cfkeyvaluepair.h>
       
    31 #include <cfcontextsourcecommand.h>
       
    32 #include <cfcontextsourcecommandparameter.h>
       
    33 #include <gmxmlnode.h>
       
    34 #include <gmxmlelement.h>
       
    35 
       
    36 // CONSTANTS
       
    37 _LIT( KScriptActionsName,               "actions"               );
       
    38 
       
    39 _LIT( KScriptPublishContextName,        "publishContext"        );
       
    40 _LIT( KScriptDefineName,                "define"                );
       
    41 _LIT( KScriptTrueName,                  "true"                  );
       
    42 _LIT( KScriptContextRefName,            "contextRef"            );
       
    43 _LIT( KScriptSourceAttribute,           "source"                );
       
    44 _LIT( KScriptTypeAttribute,             "type"                  );
       
    45 _LIT( KScriptValueAttribute,            "value"                 );
       
    46 _LIT( KScriptSourceCommandActionName,   "sourceCommand"         );
       
    47 
       
    48 // ======== LOCAL FUNCTIONS ========
       
    49 
       
    50 // Cleans up RKeyValueArray instance.
       
    51 LOCAL_C void CleanupKeyValueArray( TAny* aArray )
       
    52     {
       
    53     static_cast< RKeyValueArray* >( aArray )->ResetAndDestroy();
       
    54     }
       
    55 
       
    56 // Cleanup item for RKeyValueArray object.
       
    57 LOCAL_C void CleanupResetAndDestroyPushL( RKeyValueArray& aArray )
       
    58     {
       
    59     TCleanupItem item( CleanupKeyValueArray, &aArray );
       
    60     CleanupStack::PushL( item );
       
    61     }
       
    62 
       
    63 
       
    64 // ======== MEMBER FUNCTIONS ========
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CCFClauseNode::CCFClauseNode
       
    68 // C++ default constructor can NOT contain any code, that might leave.
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CCFClauseNode::CCFClauseNode( MCFOperationServices& aServices,
       
    72     CCFOperationNode* aParent )
       
    73     : CCFOperationNode( aServices, aParent )
       
    74     {
       
    75     FUNC_LOG;
       
    76     }
       
    77 
       
    78 
       
    79 // Destructor
       
    80 CCFClauseNode::~CCFClauseNode()
       
    81     {
       
    82     FUNC_LOG;
       
    83     }
       
    84 
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CCFClauseNode::ParseActionsL
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CCFClauseNode::ParseActionsL( CMDXMLNode& aNode,
       
    91     RPointerArray< CCFScriptAction >& aActions )
       
    92     {
       
    93     FUNC_LOG;
       
    94 
       
    95     if ( aNode.NodeName().CompareF( KScriptActionsName ) != 0 )
       
    96         {
       
    97         TPtrC nodeName( aNode.NodeName() );
       
    98         ERROR_GEN_1( "CCFClauseNode::ParseActionsL - Unknown actions name [%S]",
       
    99                 &nodeName );
       
   100         User::Leave( KErrNotSupported );
       
   101         }
       
   102 
       
   103     // Parse actions.
       
   104     CMDXMLNode* actionNode = aNode.FirstChild();
       
   105     while ( actionNode )
       
   106         {
       
   107         if ( actionNode->NodeType() == CMDXMLNode::EElementNode )
       
   108             {
       
   109             if ( actionNode->NodeName().CompareF( 
       
   110                     KScriptPublishContextName ) == 0 )
       
   111                 {
       
   112                 CMDXMLNode* publishNode = actionNode->FirstChild();
       
   113                 while ( publishNode )
       
   114                     {
       
   115                     if ( publishNode->NodeType() == CMDXMLNode::EElementNode
       
   116                         && publishNode->NodeName().CompareF(
       
   117                                 KScriptContextRefName ) == 0 )
       
   118                         {
       
   119                         CCFScriptAction* action
       
   120                             = ParsePublishContextActionL( *publishNode );
       
   121                         if ( action )
       
   122                             {
       
   123                             CleanupStack::PushL( action );  // CLEANUP<< action
       
   124                             aActions.AppendL( action );
       
   125                             CleanupStack::Pop( action );    // CLEANUP>> action
       
   126                             }
       
   127                         else
       
   128                             {
       
   129                             TPtrC nodeName( publishNode->NodeName() );
       
   130                             ERROR_GEN_1( "CCFClauseNode::ParseActionsL - Skipping publish context action node [%S]",
       
   131                                     &nodeName );
       
   132                             }
       
   133                         }
       
   134                     else if ( publishNode->NodeType()
       
   135                         != CMDXMLNode::ECommentNode )
       
   136                         {
       
   137                         TPtrC nodeName( publishNode->NodeName() );
       
   138                         ERROR_GEN_1( "CCFClauseNode::ParseActionsL - Unknown publish context action node [%S]",
       
   139                                 &nodeName );
       
   140                         User::Leave( KErrNotSupported );
       
   141                         }
       
   142                     publishNode = publishNode->NextSibling();
       
   143                     }
       
   144                 }
       
   145             else
       
   146                 {
       
   147                 CCFScriptAction* action = NULL;
       
   148                 if ( actionNode->NodeName().CompareF(
       
   149                         KScriptSourceCommandActionName ) == 0 )
       
   150                     {
       
   151                     action = ParseSourceCommandActionL( *actionNode );
       
   152                     }
       
   153                 else
       
   154                     {
       
   155                     action = ParseNotifyActionL( *actionNode );
       
   156                     }
       
   157 
       
   158                 if ( action )
       
   159                     {
       
   160                     CleanupStack::PushL( action );          // CLEANUP<< action
       
   161                     aActions.AppendL( action );
       
   162                     CleanupStack::Pop( action );            // CLEANUP>> action
       
   163                     }
       
   164                 else
       
   165                     {
       
   166                     TPtrC nodeName( actionNode->NodeName() );
       
   167                     ERROR_GEN_1( "CCFClauseNode::ParseActionsL - Skipping action node [%S]",
       
   168                             &nodeName );
       
   169                     }
       
   170                 }
       
   171             }
       
   172         actionNode = actionNode->NextSibling();
       
   173         }
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CCFClauseNode::ParseNotifyActionL
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 CCFScriptAction* CCFClauseNode::ParseNotifyActionL( CMDXMLNode& aNode )
       
   181     {
       
   182     FUNC_LOG;
       
   183 
       
   184     // Notify action.
       
   185     // below                                    // CLEANUP<< notifyAction
       
   186     HBufC* notifyAction = aNode.NodeName().AllocLC();
       
   187 
       
   188     INFO_1( "Reading action definition for [%S]", &( *notifyAction ) );
       
   189 
       
   190     RKeyValueArray keysAndValues;
       
   191     CleanupResetAndDestroyPushL( keysAndValues ); // CLEANUP<< keysAndValues
       
   192     if ( aNode.NodeType() == CMDXMLNode::EElementNode )
       
   193         {
       
   194         // Set attributes for the action.
       
   195         CMDXMLElement& element = static_cast< CMDXMLElement& >( aNode );
       
   196         TInt numAttributes = element.NumAttributes();
       
   197         TPtrC attributeName( KNullDesC );
       
   198         TPtrC attributeValue( KNullDesC );
       
   199         for ( TInt i = 0; i < numAttributes; ++i )
       
   200             {
       
   201             if ( KErrNone == element.AttributeDetails(
       
   202                     i, attributeName, attributeValue ) )
       
   203                 {
       
   204                 INFO_2( "Found action attribute: '%S'='%S'",
       
   205                     &attributeName,
       
   206                     &attributeValue );
       
   207 
       
   208                 // below                        // CLEANUP<< keyValue
       
   209                 CCFKeyValuePair* keyValue = CCFKeyValuePair::NewLC(
       
   210                     attributeName,
       
   211                     attributeValue );
       
   212                 keysAndValues.AppendL( keyValue );
       
   213                 CleanupStack::Pop( keyValue );  // CLEANUP>> keyValue
       
   214                 }
       
   215             }
       
   216         }
       
   217 
       
   218     CCFScriptAction* action = CCFNotifyAction::NewL( iServices,
       
   219             *notifyAction,
       
   220             keysAndValues );
       
   221 
       
   222     INFO_1( "Created Notify Action for [%S]", &( *notifyAction ) );
       
   223 
       
   224     CleanupStack::PopAndDestroy( &keysAndValues );// CLEANUP>> keysAndValues
       
   225     CleanupStack::PopAndDestroy( notifyAction );// CLEANUP>> notifyAction
       
   226 
       
   227     return action;
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CCFClauseNode::ParsePublishContextActionL
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 CCFScriptAction* CCFClauseNode::ParsePublishContextActionL( CMDXMLNode& aNode )
       
   235     {
       
   236     FUNC_LOG;
       
   237 
       
   238     CCFScriptAction* action = NULL;
       
   239     CMDXMLNode* parentNode = aNode.ParentNode();
       
   240     if ( parentNode
       
   241         && parentNode->NodeName().CompareF( KScriptPublishContextName ) == 0
       
   242         && aNode.NodeName().CompareF( KScriptContextRefName ) == 0 )
       
   243         {
       
   244         // Publish context action.
       
   245         if ( aNode.NodeType() != CMDXMLNode::EElementNode )
       
   246             {
       
   247             TPtrC nodeName( aNode.NodeName() );
       
   248             ERROR_GEN_1( "CCFClauseNode::ParsePublishContextActionL - Unknown publish context action node [%S]",
       
   249                     &nodeName );
       
   250             User::Leave( KErrNotSupported );
       
   251             }
       
   252 
       
   253         TPtrC contextSource;
       
   254         TPtrC contextType;
       
   255         TPtrC contextValue;
       
   256         CMDXMLElement& element = static_cast< CMDXMLElement& >( aNode );
       
   257         element.GetAttribute( KScriptSourceAttribute, contextSource );
       
   258         element.GetAttribute( KScriptTypeAttribute, contextType );
       
   259         element.GetAttribute( KScriptValueAttribute, contextValue );
       
   260         if ( contextSource.Length() == 0 || contextType.Length() == 0
       
   261             || contextValue.Length() == 0 )
       
   262             {
       
   263             ERROR_GEN( "CCFClauseNode::ParsePublishContextActionL - Publish context action contextRef element is missing attributes" );
       
   264             User::Leave( KErrNotSupported );
       
   265             }
       
   266 
       
   267         CCFContextObject* co = CCFContextObject::NewLC();   // CLEANUP<< co
       
   268         co->SetSourceL( contextSource );
       
   269         co->SetTypeL( contextType );
       
   270         co->SetValueL( contextValue );
       
   271         co->SetTimestampToHomeTime();
       
   272         
       
   273         // Check from the parent node '<publishContext>' if there are attributes
       
   274         // set for 'define'
       
   275         TBool autoDefine( EFalse );
       
   276         if( parentNode->NodeType() == CMDXMLNode::EElementNode )
       
   277             {
       
   278             // Check if the attribute is 'define'
       
   279             CMDXMLElement& parentElement = static_cast< CMDXMLElement& >( *parentNode );
       
   280             TInt err( KErrNone );
       
   281             TPtrC name( KNullDesC );
       
   282             TPtrC value( KNullDesC );
       
   283             TInt numOfAttribs( parentElement.NumAttributes() );
       
   284             for( TInt i = 0; i < numOfAttribs; i++ )
       
   285                 {
       
   286                 err = parentElement.AttributeDetails( i, name, value );
       
   287                 if( err == KErrNone )
       
   288                     {
       
   289                     // Check for 'define' attribute
       
   290                     if( name.CompareF( KScriptDefineName ) == KErrNone )
       
   291                         {
       
   292                         // Check if the value is 'true'
       
   293                         if( value.CompareF( KScriptTrueName ) == KErrNone )
       
   294                             {
       
   295                             autoDefine = ETrue;
       
   296                             }
       
   297                         }
       
   298                     }
       
   299                 }
       
   300             }
       
   301         
       
   302         // Create a new action object giving it context object ownership.
       
   303         action = CCFPublishContextAction::NewL( iServices, co, autoDefine );
       
   304         CleanupStack::Pop( co );                            // CLEANUP>> co
       
   305 
       
   306         INFO_4( "Created PublishContext Action for [%S: %S: %S], Auto define: [%d]",
       
   307                 &contextSource, &contextType, &contextValue, autoDefine );
       
   308         }
       
   309     else
       
   310         {
       
   311         TPtrC nodeName( aNode.NodeName() );
       
   312         if ( parentNode )
       
   313             {
       
   314             nodeName.Set( parentNode->NodeName() );
       
   315             }
       
   316         ERROR_GEN_1( "CCFClauseNode::ParsePublishContextActionL - Unknown publish context action node [%S]",
       
   317                 &nodeName );
       
   318         }
       
   319 
       
   320     return action;
       
   321     }
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // CCFClauseNode::ParseSourceCommandActionL
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 CCFScriptAction* CCFClauseNode::ParseSourceCommandActionL( CMDXMLNode& aNode )
       
   328     {
       
   329     FUNC_LOG;
       
   330 
       
   331     TPtrC commandNodeName( aNode.NodeName() );
       
   332     if ( commandNodeName.CompareF( KScriptSourceCommandActionName ) != 0 )
       
   333         {
       
   334         ERROR_GEN_1( "CCFClauseNode::ParseSourceCommandActionL - Unknown source command action node [%S]",
       
   335                 &commandNodeName );
       
   336         return NULL;
       
   337         }
       
   338 
       
   339     INFO_1( "Reading action definition for [%S]", &commandNodeName );
       
   340 
       
   341     RKeyValueArray keysAndValues;
       
   342     CleanupResetAndDestroyPushL( keysAndValues );   // CLEANUP<< keysAndValues
       
   343 
       
   344     GetAttributesL( aNode, keysAndValues );
       
   345 
       
   346     // below                                        // CLEANUP<< command
       
   347     CCFContextSourceCommand* command = CCFContextSourceCommand::NewLC();
       
   348     command->SetNameL( commandNodeName );
       
   349     command->AddAttributesL( keysAndValues );
       
   350 
       
   351     // Get command parameters.
       
   352     CMDXMLNode* paramNode = aNode.FirstChild();
       
   353     while ( paramNode )
       
   354         {
       
   355         TPtrC paramNodeName( paramNode->NodeName() );
       
   356         if ( paramNode->NodeType() == CMDXMLNode::EElementNode )
       
   357             {
       
   358             GetAttributesL( *paramNode, keysAndValues );
       
   359 
       
   360             // below                                // CLEANUP<< param
       
   361             CCFContextSourceCommandParameter* param
       
   362                 = CCFContextSourceCommandParameter::NewLC();
       
   363             param->SetNameL( paramNodeName );
       
   364             param->AddAttributesL( keysAndValues );
       
   365 
       
   366             GetNestedSourceCommandParametersL( *paramNode, *param );
       
   367 
       
   368             command->AddParameterL( param );
       
   369 
       
   370             CleanupStack::Pop( param );             // CLEANUP>> param
       
   371             }
       
   372         else if ( paramNode->NodeType() != CMDXMLNode::ECommentNode )
       
   373             {
       
   374             ERROR_GEN_1( "CCFClauseNode::ParseSourceCommandActionL - Unsupported node type [%S]",
       
   375                     &paramNodeName );
       
   376             User::Leave( KErrNotSupported );
       
   377             }
       
   378         paramNode = paramNode->NextSibling();
       
   379         }
       
   380 
       
   381     // Create command's sender identity.
       
   382     TCFSourceCommandSenderId senderId;
       
   383     RThread thread;
       
   384     senderId.iSender = thread.SecureId();
       
   385     thread.Close();
       
   386     senderId.iScriptId = iServices.ScriptId();
       
   387     command->SetSender( senderId );
       
   388 
       
   389     CCFSourceCommandAction* action = CCFSourceCommandAction::NewL( iServices,
       
   390             command );
       
   391 
       
   392     CleanupStack::Pop( command );                   // CLEANUP>> command
       
   393     CleanupStack::PopAndDestroy( &keysAndValues );  // CLEANUP>> keysAndValues
       
   394 
       
   395     INFO_1( "Created Source Command Action for [%S]", &commandNodeName );
       
   396 
       
   397     return action;
       
   398     }
       
   399 
       
   400 // -----------------------------------------------------------------------------
       
   401 // CCFClauseNode::GetAttributesL
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void CCFClauseNode::GetAttributesL( CMDXMLNode& aNode,
       
   405     RKeyValueArray& aAttributes )
       
   406     {
       
   407     FUNC_LOG;
       
   408 
       
   409     if ( aNode.NodeType() == CMDXMLNode::EElementNode )
       
   410         {
       
   411         CMDXMLElement& element = static_cast< CMDXMLElement& >( aNode );
       
   412         TInt numAttributes = element.NumAttributes();
       
   413         TPtrC attributeName( KNullDesC );
       
   414         TPtrC attributeValue( KNullDesC );
       
   415         for ( TInt i = 0; i < numAttributes; ++i )
       
   416             {
       
   417             if ( KErrNone == element.AttributeDetails(
       
   418                     i, attributeName, attributeValue ) )
       
   419                 {
       
   420                 INFO_2( "Found action attribute: '%S'='%S'",
       
   421                     &attributeName,
       
   422                     &attributeValue );
       
   423 
       
   424                 // below                        // CLEANUP<< keyValue
       
   425                 CCFKeyValuePair* keyValue = CCFKeyValuePair::NewLC(
       
   426                     attributeName,
       
   427                     attributeValue );
       
   428                 aAttributes.AppendL( keyValue );
       
   429                 CleanupStack::Pop( keyValue );  // CLEANUP>> keyValue
       
   430                 }
       
   431             }
       
   432         }
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CCFClauseNode::GetNestedSourceCommandParametersL
       
   437 // -----------------------------------------------------------------------------
       
   438 //
       
   439 void CCFClauseNode::GetNestedSourceCommandParametersL( CMDXMLNode& aNode,
       
   440     CCFContextSourceCommandParameter& aParent )
       
   441     {
       
   442     FUNC_LOG;
       
   443 
       
   444     RKeyValueArray keysAndValues;
       
   445     CleanupResetAndDestroyPushL( keysAndValues );   // CLEANUP<< keysAndValues
       
   446 
       
   447     CMDXMLNode* nestedNode = aNode.FirstChild();
       
   448     while ( nestedNode )
       
   449         {
       
   450         TPtrC nestedNodeName( nestedNode->NodeName() );
       
   451         if ( nestedNode->NodeType() == CMDXMLNode::EElementNode )
       
   452             {
       
   453             GetAttributesL( *nestedNode, keysAndValues );
       
   454 
       
   455             // below                                // CLEANUP<< parameter
       
   456             CCFContextSourceCommandParameter* parameter
       
   457                 = CCFContextSourceCommandParameter::NewLC();
       
   458             parameter->SetNameL( nestedNodeName );
       
   459             parameter->AddAttributesL( keysAndValues );
       
   460 
       
   461             aParent.AddParameterL( parameter );
       
   462 
       
   463             CleanupStack::Pop( parameter );         // CLEANUP>> parameter
       
   464 
       
   465             GetNestedSourceCommandParametersL( *nestedNode, *parameter );
       
   466             }
       
   467         else if ( nestedNode->NodeType() != CMDXMLNode::ECommentNode )
       
   468             {
       
   469             ERROR_GEN_1( "CCFClauseNode::GetNestedSourceCommandParameterL - Unsupported node type [%S]",
       
   470                     &nestedNodeName );
       
   471             User::Leave( KErrNotSupported );
       
   472             }
       
   473         nestedNode = nestedNode->NextSibling();
       
   474         }
       
   475 
       
   476     CleanupStack::PopAndDestroy( &keysAndValues );  // CLEANUP>> keysAndValues
       
   477     }