contextframework/cfw/src/basicoperationsplugin/cfcontextoperationutils.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:  CFContextOperationUtils class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <gmxmlelement.h>
       
    22 #include <gmxmltext.h>
       
    23 #include <f32file.h> // For delimiter definitions.
       
    24 
       
    25 #include "cfcontextoperationutils.h"
       
    26 #include "cfbasicoptrace.h"
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT( KScriptContextRefName,            "contextRef"            );
       
    30 
       
    31 _LIT( KScriptCepInt,                    "cep:int"               );
       
    32 _LIT( KScriptInt,                       "int"                   );
       
    33 _LIT( KScriptCepString,                 "cep:string"            );
       
    34 _LIT( KScriptString,                    "string"                );
       
    35 _LIT( KScriptCepFloat,                  "cep:double"            );
       
    36 _LIT( KScriptFloat,                     "double"                );
       
    37 _LIT( KScriptCepPosition,               "cep:position"          );
       
    38 _LIT( KScriptCepDate,                   "cep:date"              );
       
    39 _LIT( KScriptDate,                      "date"                  );
       
    40 
       
    41 _LIT( KScriptSourceAttribute,           "source"                );
       
    42 _LIT( KScriptTypeAttribute,             "type"                  );
       
    43 _LIT( KScriptValueAttribute,            "value"                 );
       
    44 _LIT( KScriptEvaluationDelayAttribute,  "evaluationDelay"       );
       
    45 
       
    46 // ======== MEMBER FUNCTIONS ========
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CFContextOperationUtils::ParseTwoComparisonArgs
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 TBool CFContextOperationUtils::ParseTwoComparisonArgs( CMDXMLNode& aNode,
       
    53     TPtrC& aContextSource,
       
    54     TPtrC& aContextType,
       
    55     CCFContextOperation::TCmpType& aCompareType,
       
    56     TPtrC& aCompareValue,
       
    57     TInt& aContextLevelDelay )
       
    58     {
       
    59     FUNC_LOG;
       
    60 
       
    61     TPtrC contextValue;
       
    62     TBool contextRefOK( EFalse );
       
    63     TBool comparisonTypeValueOK( EFalse );
       
    64     TBool argsOK( ETrue ); // Will be set to false if unknown nodes detected.
       
    65     CMDXMLNode* child = aNode.FirstChild();
       
    66     while ( child )
       
    67         {
       
    68         if ( child->NodeType() == CMDXMLNode::EElementNode )
       
    69             {
       
    70             if ( child->NodeName().CompareF( KScriptContextRefName ) == 0 )
       
    71                 {
       
    72                 if ( !contextRefOK )
       
    73                     {
       
    74                     contextRefOK = CFContextOperationUtils::ParseContextRef(
       
    75                             *child,
       
    76                             aContextSource,
       
    77                             aContextType,
       
    78                             contextValue,
       
    79                             aContextLevelDelay );
       
    80                     if ( !contextRefOK )
       
    81                         {
       
    82                         argsOK = EFalse;
       
    83                         break;
       
    84                         }
       
    85                     }
       
    86                 else
       
    87                     {
       
    88                     INFO( "CFContextOperationUtils::ParseTwoComparisonArgs - Redefinition not allowed, context ref already defined" );
       
    89                     argsOK = EFalse;
       
    90                     break;
       
    91                     }
       
    92                 }
       
    93             else
       
    94                 {
       
    95                 if ( !comparisonTypeValueOK )
       
    96                     {
       
    97                     comparisonTypeValueOK
       
    98                         = CFContextOperationUtils::ParseComparisonTypeValue(
       
    99                                 *child,
       
   100                                 aCompareType,
       
   101                                 aCompareValue );
       
   102                     if ( !comparisonTypeValueOK )
       
   103                         {
       
   104                         argsOK = EFalse;
       
   105                         break;
       
   106                         }
       
   107                     }
       
   108                 else
       
   109                     {
       
   110                     INFO( "CFContextOperationUtils::ParseTwoComparisonArgs - Redefinition not allowed, comparison type value already defined" );
       
   111                     argsOK = EFalse;
       
   112                     break;
       
   113                     }
       
   114                 }
       
   115             }
       
   116         else if ( child->NodeType() != CMDXMLNode::ECommentNode )
       
   117             {
       
   118             TPtrC nodeName( child->NodeName() );
       
   119             INFO_1( "CFContextOperationUtils::ParseTwoComparisonArgs - Unsupported node [%S]",
       
   120                     &nodeName );
       
   121             argsOK = EFalse;
       
   122             break;
       
   123             }
       
   124         child = child->NextSibling();
       
   125         }
       
   126 
       
   127     TBool parsed( EFalse );
       
   128     if ( argsOK && contextRefOK )
       
   129         {
       
   130         if ( ( comparisonTypeValueOK && !contextValue.Length() )
       
   131             || ( !comparisonTypeValueOK && contextValue.Length() ) )
       
   132             {
       
   133             if ( !comparisonTypeValueOK )
       
   134                 {
       
   135                 aCompareType = CCFContextOperation::EStringCmp;
       
   136                 aCompareValue.Set( contextValue );
       
   137                 }
       
   138             parsed = ETrue;
       
   139             }
       
   140         else
       
   141             {
       
   142             if ( !comparisonTypeValueOK )
       
   143                 {
       
   144                 INFO( "CFContextOperationUtils::ParseTwoComparisonArgs - Value to compare the context with not defined" );
       
   145                 }
       
   146             else
       
   147                 {
       
   148                 INFO( "CFContextOperationUtils::ParseTwoComparisonArgs - Ambiguous, value to compare the context with defined twice" );
       
   149                 }
       
   150             }
       
   151         }
       
   152 
       
   153     return parsed;
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CFContextOperationUtils::ParseContextRef
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 TBool CFContextOperationUtils::ParseContextRef( CMDXMLNode& aNode,
       
   161     TPtrC& aContextSource,
       
   162     TPtrC& aContextType,
       
   163     TPtrC& aContextValue,
       
   164     TInt& aContextLevelDelay )
       
   165     {
       
   166     FUNC_LOG;
       
   167 
       
   168     if ( aNode.NodeName().CompareF( KScriptContextRefName ) != 0
       
   169         || aNode.NodeType() != CMDXMLNode::EElementNode )
       
   170         {
       
   171         return EFalse; // Cannot parse context ref from the given node.
       
   172         }
       
   173 
       
   174     TBool contextRefOK( EFalse );
       
   175     CMDXMLElement& element = static_cast< CMDXMLElement& >( aNode );
       
   176     TPtrC evaluationDelay;
       
   177     if ( KErrNone == element.GetAttribute( KScriptSourceAttribute,
       
   178             aContextSource ) )
       
   179         {
       
   180         if ( KErrNone == element.GetAttribute( KScriptTypeAttribute,
       
   181                 aContextType ) )
       
   182             {
       
   183             if ( aContextSource.Length() && aContextType.Length()
       
   184                 && !element.HasChildNodes() )
       
   185                 {
       
   186                 contextRefOK = ETrue;
       
   187                 TInt numAttributes( 2 ); // Two attributes read so far.
       
   188                 TPtrC evaluationDelay;
       
   189                 // Extract context level evaluation delay if defined.
       
   190                 if ( KErrNone == element.GetAttribute(
       
   191                         KScriptEvaluationDelayAttribute,
       
   192                         evaluationDelay ) )
       
   193                     {
       
   194                     ++numAttributes;
       
   195                     if ( evaluationDelay.Length() )
       
   196                         {
       
   197                         // Convert delay to integer.
       
   198                         TLex parseInt( evaluationDelay );
       
   199                         parseInt.SkipSpace();
       
   200                         TInt contextLevelDelay( 0 );
       
   201                         TInt err = parseInt.Val( contextLevelDelay );
       
   202                         if ( err != KErrNone || contextLevelDelay < 0  )
       
   203                             {
       
   204                             // Evaluation delay defined but invalid.
       
   205                             contextRefOK = EFalse;
       
   206                             }
       
   207                         else if ( contextLevelDelay > 0 )
       
   208                             {
       
   209                             aContextLevelDelay = contextLevelDelay;
       
   210                             }
       
   211                         }
       
   212                     }
       
   213                 // Extract context value if defined.
       
   214                 if ( KErrNone != element.GetAttribute( KScriptValueAttribute,
       
   215                         aContextValue ) )
       
   216                     {
       
   217                     // Ensure empty value when it is not defined.
       
   218                     aContextValue.Set( 0, 0 );
       
   219                     }
       
   220                 else
       
   221                     {
       
   222                     ++numAttributes;
       
   223                     }
       
   224 
       
   225                 if ( numAttributes != element.NumAttributes() )
       
   226                     {
       
   227                     INFO( "CFContextOperationUtils::ParseContextRef - Unsupported amount of attributes" );
       
   228                     contextRefOK = EFalse; // Unsupported contextRef.
       
   229                     }
       
   230                 }
       
   231             else
       
   232                 {
       
   233                 INFO( "CFContextOperationUtils::ParseContextRef - Unsupported context ref" );
       
   234                 }
       
   235             }
       
   236         else
       
   237             {
       
   238             INFO( "CFContextOperationUtils::ParseContextRef - Getting type failed" );
       
   239             }
       
   240         }
       
   241     else
       
   242         {
       
   243         INFO( "CFContextOperationUtils::ParseContextRef - Getting source failed" );
       
   244         }
       
   245 
       
   246     return contextRefOK;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CFContextOperationUtils::ParseComparisonTypeValue
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 TBool CFContextOperationUtils::ParseComparisonTypeValue( CMDXMLNode& aNode,
       
   254     CCFContextOperation::TCmpType& aCompareType,
       
   255     TPtrC& aCompareValue )
       
   256     {
       
   257     FUNC_LOG;
       
   258 
       
   259     TBool parsed( EFalse );
       
   260     if ( aNode.NodeType() == CMDXMLNode::EElementNode )
       
   261         {
       
   262         aCompareType
       
   263             = CFContextOperationUtils::ComparisonType( aNode.NodeName() );
       
   264 
       
   265         if ( aCompareType != CCFContextOperation::EInvalidCmpType )
       
   266             {
       
   267             // Parse comparison value from first text node child.
       
   268             CMDXMLNode* child = aNode.FirstChild();
       
   269             while ( child )
       
   270                 {
       
   271                 // Allow one text node and multiple comments, nothing else.
       
   272                 if ( child->NodeType() == CMDXMLNode::ETextNode )
       
   273                     {
       
   274                     if ( parsed )
       
   275                         {
       
   276                         TPtrC nodeName( child->NodeName() );
       
   277                         INFO_1( "CFContextOperationUtils::ParseComparisonTypeValue - One definition is allowed. [%S] is not supported",
       
   278                                 &nodeName );
       
   279                         // Already parsed, flag inability to parse.
       
   280                         parsed = EFalse;
       
   281                         break;
       
   282                         }
       
   283                     CMDXMLText* text = static_cast< CMDXMLText* >( child );
       
   284                     aCompareValue.Set( text->Data() );
       
   285                     parsed = ETrue;
       
   286                     }
       
   287                 else if ( child->NodeType() != CMDXMLNode::ECommentNode )
       
   288                     {
       
   289                     TPtrC nodeName( child->NodeName() );
       
   290                     INFO_1( "CFContextOperationUtils::ParseComparisonTypeValue - Unsupported node [%S]",
       
   291                             &nodeName );
       
   292                     parsed = EFalse;
       
   293                     break;
       
   294                     }
       
   295                 child = child->NextSibling();
       
   296                 }
       
   297             }
       
   298         else
       
   299             {
       
   300             INFO( "CFContextOperationUtils::ParseComparisonTypeValue - Invalid comparison type" );
       
   301             }
       
   302         }
       
   303     else
       
   304         {
       
   305         TPtrC nodeName( aNode.NodeName() );
       
   306         INFO_1( "CFContextOperationUtils::ParseComparisonTypeValue - Unsupported node [%S]",
       
   307                 &nodeName );
       
   308         }
       
   309 
       
   310     return parsed;
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CFContextOperationUtils::ComparisonType
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 CCFContextOperation::TCmpType CFContextOperationUtils::ComparisonType(
       
   318     const TDesC& aComparisonType )
       
   319     {
       
   320     FUNC_LOG;
       
   321 
       
   322     CCFContextOperation::TCmpType ret( CCFContextOperation::EInvalidCmpType );
       
   323 
       
   324     if ( aComparisonType.CompareF( KScriptCepInt ) == 0  ||
       
   325         aComparisonType.CompareF( KScriptInt ) == 0 )
       
   326         {
       
   327         ret = CCFContextOperation::EIntCmp;
       
   328         }
       
   329     else if ( aComparisonType.CompareF( KScriptCepDate ) == 0 ||
       
   330         aComparisonType.CompareF( KScriptDate ) == 0 )
       
   331         {
       
   332         ret = CCFContextOperation::ETimeCmp;
       
   333         }
       
   334     else if ( aComparisonType.CompareF( KScriptCepPosition ) == 0 )
       
   335         {
       
   336         ret = CCFContextOperation::EPositionCmp;
       
   337         }
       
   338     else if ( aComparisonType.CompareF( KScriptCepString ) == 0 ||
       
   339         aComparisonType.CompareF( KScriptString ) == 0 )
       
   340         {
       
   341         ret = CCFContextOperation::EStringCmp;
       
   342         }
       
   343     else if ( aComparisonType.CompareF( KScriptCepFloat ) == 0 ||
       
   344         aComparisonType.CompareF( KScriptFloat ) == 0 )
       
   345         {
       
   346         ret = CCFContextOperation::EFloatCmp;
       
   347         }
       
   348 
       
   349     return ret;
       
   350     }
       
   351 
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // CFContextOperationUtils::StringToTimeL
       
   355 // ---------------------------------------------------------------------------
       
   356 //
       
   357 TTime CFContextOperationUtils::StringToTimeL( const TDesC& aString )
       
   358     {
       
   359     FUNC_LOG;
       
   360 
       
   361     TTime val;
       
   362     TInt err = val.Set( aString );
       
   363     if ( err != KErrNone )
       
   364         {
       
   365         ERROR_1( err, "Cannot interpret string as TTime. Value: %S", &aString );
       
   366         User::Leave( err );
       
   367         }
       
   368 
       
   369     return val;
       
   370     }
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // CFContextOperationUtils::StringToRealL
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 TReal CFContextOperationUtils::StringToRealL( const TDesC& aString )
       
   377     {
       
   378     FUNC_LOG;
       
   379 
       
   380     TLex lexer( aString );
       
   381     TInt err( KErrNone );
       
   382     TReal val( 0 );
       
   383     TChar decPoint( KExtDelimiter ); // '.'
       
   384 
       
   385     // Parse float from string; note that decimal point delimiter must be '.'
       
   386     err = lexer.Val( val, decPoint );
       
   387     if ( err != KErrNone )
       
   388         {
       
   389         ERROR_1( err, "Cannot interpret string as float. Value: %S", &aString );
       
   390         User::Leave( err );
       
   391         }
       
   392 
       
   393     return val;
       
   394     }
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // CFContextOperationUtils::PositionToRealsL
       
   398 // Converts a string containing latitude and longitude values into two real
       
   399 // values passed by reference.
       
   400 // ---------------------------------------------------------------------------
       
   401 //
       
   402 void CFContextOperationUtils::PositionToRealsL( const TDesC& aString,
       
   403     TReal& aLatitude,
       
   404     TReal& aLongitude )
       
   405     {
       
   406     FUNC_LOG;
       
   407 
       
   408     TLex lexer( aString );
       
   409     TBuf< KMaxName > latDes( KErrNone );
       
   410     TBuf< KMaxName > lonDes( KErrNone );
       
   411     TChar posDelimiter( KDriveDelimiter ); // ':'
       
   412 
       
   413     // Get the latitude to descriptor.
       
   414     while ( lexer.Peek() != posDelimiter && // Delimiter char not found.
       
   415             !lexer.Eos()                 )  // End of string not reached.
       
   416         {
       
   417         latDes.Append( lexer.Get() );
       
   418         }
       
   419 
       
   420     if ( lexer.Eos()                 || // Eos already.
       
   421          lexer.Peek() != posDelimiter ) // Delimiter not found.
       
   422         {
       
   423         ERROR_GEN_1( "Cannot interpret position as floats. String: %S", &aString );
       
   424         User::Leave( KErrBadDescriptor );
       
   425         }
       
   426 
       
   427     // Skip over delimiter.
       
   428     lexer.Inc();
       
   429 
       
   430     // Get the longitude to descriptor.
       
   431     while ( !lexer.Eos() )
       
   432         {
       
   433         latDes.Append( lexer.Get() );
       
   434         }
       
   435     // Parse latitude and longitude into given reals.
       
   436     aLatitude =  StringToRealL( latDes );
       
   437     aLongitude = StringToRealL( lonDes );
       
   438     }
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // CFContextOperationUtils::StringToIntL
       
   442 // ---------------------------------------------------------------------------
       
   443 //
       
   444 TInt CFContextOperationUtils::StringToIntL( const TDesC& aString )
       
   445     {
       
   446     FUNC_LOG;
       
   447 
       
   448     TLex lexer( aString );
       
   449     TInt val( KErrNone );
       
   450     TInt err = lexer.Val( val );
       
   451     if ( err != KErrNone )
       
   452         {
       
   453         ERROR_1( err, "Cannot interpret string as int. Value: %S", &aString );
       
   454         User::Leave( err );
       
   455         }
       
   456     return val;
       
   457     }