contextframework/cfwplugins/PSStateSourcePlugIn/src/psstatesourceplugin.cpp
changeset 0 2e3d3ce01487
child 18 0818dd463d41
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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:  CPSStateSourcePlugIn class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #define __INCLUDE_CAPABILITY_NAMES__
       
    20 
       
    21 #include <cfcontextinterface.h>
       
    22 #include <cfcontextsourcesettingarray.h>
       
    23 #include <e32capability.h>
       
    24 
       
    25 #include "psstatesourceappstatedomainpskeys.h"
       
    26 #include "psstatesourceplugin.h"
       
    27 #include "psstatemapper.h"
       
    28 #include "psstatemapperint.h"
       
    29 #include "psstatemappertext.h"
       
    30 
       
    31 const TUint KLimit = 0xFFFFFFFF;
       
    32 _LIT (KAttrMapper, "mapper" );
       
    33 _LIT (KAttrCategory, "category" );
       
    34 _LIT (KAttrKey, "key" );
       
    35 _LIT (KAttrCapability, "capability" );
       
    36 _LIT (KAttrSource, "source" );
       
    37 _LIT (KAttrType, "type" );
       
    38 _LIT (KAttrPassThrough, "passthrough" );
       
    39 
       
    40 
       
    41 _LIT (KValueInt, "int" );
       
    42 _LIT (KValueText, "text" );
       
    43 
       
    44 _LIT (KValueTrue, "true" );
       
    45 
       
    46 _LIT (KTagIntMapping, "IntMapping");
       
    47 _LIT (KTagTextMapping, "TextMapping");
       
    48 
       
    49 _LIT (KAttributePSValue, "psvalue");
       
    50 _LIT (KAttributeContextValue, "contextvalue");
       
    51 
       
    52 
       
    53 //namespace PSSOURCE
       
    54 //    {
       
    55 //    // Panic category
       
    56 //    _LIT( KPanicCat, "PSStateSrc" );
       
    57 //    
       
    58 //    // Panic codes
       
    59 //    enum TPanicReason
       
    60 //        {
       
    61 //        EInvalidPSKey
       
    62 //        };
       
    63 //        
       
    64 //    // Local panic function
       
    65 //    LOCAL_C void Panic( TInt aCode )
       
    66 //        {
       
    67 //        User::Panic( KPanicCat, aCode );
       
    68 //        }
       
    69 //    }
       
    70 //
       
    71 //using namespace PSSOURCE;
       
    72 
       
    73 CPSStateSourcePlugIn* CPSStateSourcePlugIn::NewL( 
       
    74     TContextSourceInitParams* aParams )
       
    75     {
       
    76     CPSStateSourcePlugIn* self = CPSStateSourcePlugIn::NewLC( aParams );
       
    77     CleanupStack::Pop();
       
    78     
       
    79     return self;
       
    80     }
       
    81   
       
    82 CPSStateSourcePlugIn* CPSStateSourcePlugIn::NewLC( 
       
    83 TContextSourceInitParams* aParams )
       
    84     {
       
    85     CPSStateSourcePlugIn* self =
       
    86         new( ELeave ) CPSStateSourcePlugIn( aParams );
       
    87     CleanupStack::PushL( self );
       
    88     
       
    89     return self;
       
    90     }
       
    91   
       
    92 // Destructor
       
    93 CPSStateSourcePlugIn::~CPSStateSourcePlugIn()
       
    94     {
       
    95     delete iSettings;
       
    96     iMappers.ResetAndDestroy();
       
    97     }
       
    98 
       
    99 CPSStateSourcePlugIn::CPSStateSourcePlugIn(
       
   100     TContextSourceInitParams* aParams ):
       
   101     CCFContextSourcePlugIn( aParams )
       
   102     {
       
   103     }
       
   104 
       
   105 // METHODS
       
   106 
       
   107 
       
   108 
       
   109 //-----------------------------------------------------------------------------
       
   110 // CPSStateSourcePlugIn::HandleSettingL
       
   111 //-----------------------------------------------------------------------------
       
   112 //
       
   113 void CPSStateSourcePlugIn::HandleSettingL(
       
   114     CCFContextSourceSettingArray* aSettingList )
       
   115 	{
       
   116     
       
   117     iSettings = aSettingList;
       
   118     
       
   119     for (TInt i = 0; i < iSettings->Count(); i++ )
       
   120     	{
       
   121 		CPSStateMapper* mapper = NULL;
       
   122     	
       
   123     	const RKeyValueArray& attributes = iSettings->Item( i ).Attributes();
       
   124     	// Check application and view names (also default values)
       
   125         for( TInt j = 0; j < attributes.Count(); j++ )
       
   126             {
       
   127             TPtrC key( attributes[ j ]->Key() );
       
   128             TPtrC value( attributes[ j ]->Value() );
       
   129 
       
   130             if( key.CompareF( KAttrMapper ) == KErrNone )
       
   131                 {
       
   132                 if (value.CompareF( KValueInt) == KErrNone )
       
   133                 	{
       
   134                 	CPSStateMapperInt* intmapper 
       
   135                 		= CPSStateMapperInt::NewL( iCF );
       
   136                 	mapper = (CPSStateMapper*) intmapper;
       
   137                 	}
       
   138                 else if (value.CompareF( KValueText) == KErrNone )
       
   139                 	{
       
   140                 	CPSStateMapperText* textmapper 
       
   141                 		= CPSStateMapperText::NewL( iCF );
       
   142                 	mapper = (CPSStateMapper*) textmapper;
       
   143                 	}
       
   144                 } 
       
   145             } 
       
   146 
       
   147 		// Leave from here if PS state mapper is NULL.This may happen if none of the attributes' key value matches with the state mapper 
       
   148 		if ( mapper == NULL)
       
   149 			User::LeaveIfError(KErrArgument);
       
   150         
       
   151         for( TInt j = 0; j < attributes.Count(); j++ )
       
   152             {
       
   153             TPtrC key( attributes[ j ]->Key() );
       
   154             TPtrC value( attributes[ j ]->Value() );
       
   155 
       
   156             if( key.CompareF( KAttrCategory ) == KErrNone )
       
   157                 {
       
   158                	TUint32 category;
       
   159                 TInt err = ConvertHexToTUint( value, category );
       
   160 				mapper->SetPSCategory(TUid::Uid(category));                
       
   161                 }
       
   162             else if( key.CompareF( KAttrKey ) == KErrNone )
       
   163                 {
       
   164                 TUint32 key;
       
   165                 TInt err = ConvertHexToTUint( value, key );
       
   166 				mapper->SetPSKey(key);                
       
   167                 }
       
   168             else if( key.CompareF( KAttrCapability ) == KErrNone )
       
   169                 {
       
   170                 TSecurityPolicy policy;
       
   171 				ConvertDescToSecurityPolicy( value, policy ); 
       
   172 				mapper->SetSecurityPolicy( policy );                
       
   173                 }
       
   174             else if( key.CompareF( KAttrSource ) == KErrNone )
       
   175                 {
       
   176 				mapper->SetSourceL(value);
       
   177                 }
       
   178             else if( key.CompareF( KAttrType ) == KErrNone )
       
   179                 {
       
   180 				mapper->SetTypeL(value);
       
   181                 }
       
   182             else if( key.CompareF( KAttrPassThrough ) == KErrNone )
       
   183                 {
       
   184                 if (value.CompareF( KValueTrue ) == KErrNone )
       
   185                 	{
       
   186                 	mapper->SetPassThrough(ETrue);
       
   187                 	} else 
       
   188                 	{
       
   189                 	mapper->SetPassThrough(EFalse);
       
   190                 	}	
       
   191                 }
       
   192             }
       
   193         
       
   194         const RSettingParameterArray& parameters 
       
   195         	= iSettings->Item( i ).Parameters();
       
   196         
       
   197         switch (mapper->MapperType())
       
   198         	{
       
   199         	case CPSStateMapper::EPSIntMapper:
       
   200         		{
       
   201         		ParseIntMapperParamsL(*mapper, parameters);
       
   202         		break;
       
   203         		}
       
   204             case CPSStateMapper::EPSTextMapper:
       
   205             	{
       
   206             	ParseTextMapperParamsL(*mapper, parameters);
       
   207             	break;
       
   208             	}
       
   209         	}
       
   210 
       
   211         iMappers.AppendL( mapper );    
       
   212     	}
       
   213 	}
       
   214 
       
   215 
       
   216 void CPSStateSourcePlugIn::ParseIntMapperParamsL(CPSStateMapper& aMapper,
       
   217 	const RSettingParameterArray& aParameters)
       
   218 	{
       
   219 	CPSStateMapperInt* intmapper = (CPSStateMapperInt*) &aMapper;
       
   220     
       
   221     for( TInt j = 0; j < aParameters.Count(); ++j )
       
   222         {
       
   223         if ( aParameters[ j ]->Name() != KTagIntMapping )
       
   224             {
       
   225             continue;
       
   226             }
       
   227 
       
   228         TPtrC psValueDesc( KNullDesC );
       
   229         TPtrC contextValue( KNullDesC );
       
   230         const RKeyValueArray& paramAttr = aParameters[ j ]->Attributes();
       
   231         for( TInt k = 0; k < paramAttr.Count(); ++k )
       
   232             {
       
   233             TPtrC key( paramAttr[ k ]->Key() );
       
   234             TPtrC value( paramAttr[ k ]->Value() );
       
   235             
       
   236             if( key.CompareF( KAttributePSValue ) == KErrNone )
       
   237                 {
       
   238                 psValueDesc.Set( value );
       
   239                 }
       
   240             
       
   241             else if( key.CompareF( KAttributeContextValue ) == KErrNone )
       
   242                 {
       
   243                 contextValue.Set( value );
       
   244                 }
       
   245             }
       
   246 
       
   247 		TInt psValue;
       
   248 		
       
   249 		TLex lex( psValueDesc );
       
   250 		lex.Val(psValue);
       
   251 
       
   252 		intmapper->AddMappingL(psValue, contextValue);
       
   253         }
       
   254 	}
       
   255 
       
   256 
       
   257 void CPSStateSourcePlugIn::ParseTextMapperParamsL(CPSStateMapper& aMapper,
       
   258 	const RSettingParameterArray& aParameters)
       
   259 	{
       
   260 	CPSStateMapperText* textmapper = (CPSStateMapperText*) &aMapper;
       
   261     
       
   262     for( TInt j = 0; j < aParameters.Count(); ++j )
       
   263         {
       
   264         if ( aParameters[ j ]->Name() != KTagTextMapping )
       
   265             {
       
   266             continue; 
       
   267             }
       
   268 
       
   269         TPtrC psValueDesc( KNullDesC );
       
   270         TPtrC contextValue( KNullDesC );
       
   271         const RKeyValueArray& paramAttr = aParameters[ j ]->Attributes();
       
   272         for( TInt k = 0; k < paramAttr.Count(); ++k )
       
   273             {
       
   274             TPtrC key( paramAttr[ k ]->Key() );
       
   275             TPtrC value( paramAttr[ k ]->Value() );
       
   276             
       
   277             if( key.CompareF( KAttributePSValue ) == KErrNone )
       
   278                 {
       
   279                 psValueDesc.Set( value );
       
   280                 }
       
   281             
       
   282             else if( key.CompareF( KAttributeContextValue ) == KErrNone )
       
   283                 {
       
   284                 contextValue.Set( value );
       
   285                 }
       
   286             }
       
   287 
       
   288 		textmapper->AddMappingL(psValueDesc, contextValue);
       
   289         }
       
   290 	}
       
   291 
       
   292 //-----------------------------------------------------------------------------
       
   293 // CPSStateSourcePlugIn::DefineContextsL
       
   294 //-----------------------------------------------------------------------------
       
   295 //
       
   296 void CPSStateSourcePlugIn::DefineContextsL()
       
   297     {
       
   298     for (TInt i = 0; i < iMappers.Count(); i++)
       
   299     	{
       
   300     	iMappers[i]->Define();	
       
   301     	}
       
   302     }
       
   303     
       
   304 //-----------------------------------------------------------------------------
       
   305 // CPSStateSourcePlugIn::InitializeL
       
   306 //-----------------------------------------------------------------------------
       
   307 //
       
   308 void CPSStateSourcePlugIn::InitializeL()
       
   309     {
       
   310     for (TInt i = 0; i < iMappers.Count(); i++)
       
   311     	{
       
   312     	iMappers[i]->InitializeL();	
       
   313     	}
       
   314     }
       
   315 
       
   316     
       
   317 //-----------------------------------------------------------------------------
       
   318 // CPSStateSourcePlugIn::ConvertHexToTUint
       
   319 //-----------------------------------------------------------------------------
       
   320 //
       
   321 TInt CPSStateSourcePlugIn::ConvertHexToTUint( const TDesC& aString, 
       
   322     TUint32& aValue )
       
   323     {
       
   324     TInt err = KErrNone;
       
   325 
       
   326     TLex lex( aString );
       
   327 
       
   328     // Get rid of '0x'
       
   329     lex.Inc( 2 );
       
   330     if( lex.BoundedVal( aValue, EHex, KLimit ) != KErrNone )
       
   331         {
       
   332         err = KErrNotFound;
       
   333         }
       
   334 
       
   335     return err;
       
   336     }
       
   337 
       
   338 
       
   339 //-----------------------------------------------------------------------------
       
   340 // CPSStateSourcePlugIn::ConvertDescToSecurityPolicy
       
   341 //-----------------------------------------------------------------------------
       
   342 //
       
   343 void CPSStateSourcePlugIn::ConvertDescToSecurityPolicy( const TDesC& aString, 
       
   344     TSecurityPolicy& aSecurityPolicy )
       
   345 	{
       
   346 	TCapability capabilities[7] = 
       
   347 		{
       
   348 		ECapability_None,
       
   349 		ECapability_None,
       
   350 		ECapability_None,
       
   351 		ECapability_None,
       
   352 		ECapability_None,
       
   353 		ECapability_None,
       
   354 		ECapability_None
       
   355 		};
       
   356 		
       
   357 	TLex lexer( aString );
       
   358 	TInt tokenLength = 0;	
       
   359 	TInt i = 0;
       
   360 	do  {
       
   361 		lexer.SkipSpace();
       
   362 		lexer.Mark();
       
   363 		lexer.SkipCharacters();
       
   364 	
       
   365 		tokenLength = lexer.TokenLength();
       
   366 		if (tokenLength != 0)
       
   367 			{
       
   368 			TPtrC token = lexer.MarkedToken();
       
   369 			if ( i < 7 )
       
   370 				{
       
   371 				capabilities[i++] = ConvertToCapabilityNumber( token );
       
   372 				}
       
   373 			else
       
   374 				{
       
   375 				// TSecurityPolicy can hold maximum of 7 capabilities
       
   376 				// if more are given, we make a security policy
       
   377 				// that always fails.
       
   378 				capabilities[0] = ECapability_Denied;
       
   379 				}
       
   380 			}
       
   381 		}
       
   382 	while( tokenLength != 0 );
       
   383 			
       
   384 	TSecurityPolicy policy  (
       
   385 		capabilities[0],
       
   386 		capabilities[1],
       
   387 		capabilities[2],
       
   388 		capabilities[3],
       
   389 		capabilities[4],
       
   390 		capabilities[5],
       
   391 		capabilities[6] );
       
   392 		
       
   393 	aSecurityPolicy = policy;
       
   394 	}
       
   395 
       
   396 //-----------------------------------------------------------------------------
       
   397 // CPSStateSourcePlugIn::ConvertDescToSecurityPolicy
       
   398 //-----------------------------------------------------------------------------
       
   399 //
       
   400 TCapability CPSStateSourcePlugIn::ConvertToCapabilityNumber( 
       
   401 const TDesC& aString )
       
   402 	{
       
   403 	for( TInt i = 0; i < ECapability_Limit; i++ )
       
   404 		{
       
   405 		const TPtrC8 capName8 
       
   406 			= TPtrC8( (const unsigned char*) CapabilityNames[i] );
       
   407 		TBuf<20> capName;
       
   408 		capName.Copy( capName8 );
       
   409 		
       
   410 		if ( aString.CompareF( capName ) == KErrNone )
       
   411 			{
       
   412 			return (TCapability) i;
       
   413 			}
       
   414 		}
       
   415 	
       
   416 	// In the case the capability is not found from the list
       
   417 	// a capability that always fail is returned.
       
   418 
       
   419 	return ECapability_Denied;
       
   420 	}
       
   421