locationtriggering/ltcontextsourceplugin/src/lbtcontextsourceplugin.cpp
changeset 0 667063e416a2
child 7 19bff11d6c18
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2009 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:  CLBTContextSourcePlugin DLL source
       
    15 *
       
    16 */
       
    17 
       
    18 //  Include Files  
       
    19 
       
    20 #define __INCLUDE_CAPABILITY_NAMES__
       
    21 #include <e32capability.h>
       
    22 
       
    23 #include "lbtcontextsourceplugin.h"	// CLBTContextSourcePlugin
       
    24 #include "lbtcontextsourceplugindebug.h"
       
    25 
       
    26 
       
    27 // constants
       
    28 _LIT (KTriggerName, "name");
       
    29 _LIT (KTriggerLocation, "TriggerLocation");
       
    30 _LIT (KLatitude, "latitude");
       
    31 _LIT (KLongitude, "longitude");
       
    32 _LIT (KRadius, "radius");
       
    33 _LIT (KRegion, "region");
       
    34 _LIT (KSource, "setName");
       
    35 _LIT (KDirection, "Direction");
       
    36 _LIT (KCurrentState, "CurrentState");
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CLBTContextSourcePlugin::NewLC
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CLBTContextSourcePlugin* CLBTContextSourcePlugin::NewLC( TContextSourceInitParams* aParams )
       
    43     {
       
    44     CLBTContextSourcePlugin* self = new (ELeave) CLBTContextSourcePlugin( aParams );
       
    45     CleanupStack::PushL(self);
       
    46     self->ConstructL();
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CLBTContextSourcePlugin::NewL
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CLBTContextSourcePlugin* CLBTContextSourcePlugin::NewL(TContextSourceInitParams* aParams)
       
    55     {
       
    56     CLBTContextSourcePlugin* self = CLBTContextSourcePlugin::NewLC( aParams );
       
    57     CleanupStack::Pop(self);
       
    58     return self;
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CLBTContextSourcePlugin::ConstructL
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 void CLBTContextSourcePlugin::ConstructL()
       
    66     {
       
    67     
       
    68     // Start listening to trigger fire events
       
    69         iLocationTrigger = CLocationTriggerAdapter::NewL(*this);
       
    70         
       
    71     // Start monitoring Management UI events
       
    72     iLbtManagerObserver = CLocationTriggerManager::NewL();
       
    73     iLbtManagerObserver->StartManagerObserver();
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CLBTContextSourcePlugin::CLBTContextSourcePlugin
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CLBTContextSourcePlugin::CLBTContextSourcePlugin( 
       
    81 TContextSourceInitParams* aParams ):
       
    82 CCFContextSourcePlugIn( aParams ),
       
    83 iTriggersCreated(FALSE)
       
    84     {
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CLBTContextSourcePlugin::~CLBTContextSourcePlugin
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 CLBTContextSourcePlugin::~CLBTContextSourcePlugin()
       
    92     {
       
    93     iTriggerList.ResetAndDestroy();
       
    94     iTriggerList.Close();
       
    95     
       
    96     iTriggerNodeList.ResetAndDestroy();
       
    97     iTriggerNodeList.Close();
       
    98     
       
    99     if( iLocationTrigger )
       
   100         {
       
   101         delete iLocationTrigger;
       
   102         iLocationTrigger = NULL;
       
   103         }
       
   104     
       
   105     if( iSettingList )
       
   106         {
       
   107         delete iSettingList;
       
   108         iSettingList = NULL;
       
   109         }
       
   110     
       
   111     if( iLbtManagerObserver )
       
   112         {
       
   113         delete iLbtManagerObserver;
       
   114         iLbtManagerObserver = NULL;
       
   115         }
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CLBTContextSourcePlugin::HandleSettingL
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CLBTContextSourcePlugin::HandleSettingL(
       
   123         CCFContextSourceSettingArray* aSettingList )
       
   124     {
       
   125     
       
   126     // Cancel all active requests if any
       
   127     if (iLocationTrigger->IsActive())
       
   128         {
       
   129         iLocationTrigger->Cancel();
       
   130         }
       
   131     
       
   132     if( !iSettingList )
       
   133         {
       
   134         delete iSettingList;
       
   135         iSettingList = NULL;
       
   136         }
       
   137     // take ownership of this object
       
   138     iSettingList = aSettingList;
       
   139     
       
   140     // Clear contents of previous valid trigger specifications
       
   141     iTriggerList.ResetAndDestroy();
       
   142     iTriggerList.Close();
       
   143     
       
   144     // Clear contents of array of created triggers
       
   145     iTriggerNodeList.ResetAndDestroy();
       
   146     iTriggerNodeList.Close();
       
   147 
       
   148     // Read from XML file(s) and save onto array
       
   149     for ( TInt i = 0; i < aSettingList->Count(); i++ )
       
   150         {
       
   151         const CCFContextSourceSetting& item = aSettingList->Item( i );
       
   152 	TInt error = KErrNone;
       
   153         TRAP( error, ParseTriggerInformationL( item ) );
       
   154         LBTCONTEXTSOURCEPLUGINDEBUG1( " error on ParseTriggerInformationL %d", error );
       
   155         }
       
   156 
       
   157     if( iTriggerNodeList.Count()>0 )
       
   158         {
       
   159             iCurrentTriggerIndex = 0;
       
   160             TInt err= KErrNone;
       
   161             TRAP(err, CreateTriggersL( ));
       
   162             while(err != KErrNone)
       
   163                 {
       
   164                 iCurrentTriggerIndex++;
       
   165                 if( iCurrentTriggerIndex <= iTriggerNodeList.Count()-1 )
       
   166                     {
       
   167                     TRAP( err, CreateTriggersL( ) );
       
   168                     }
       
   169                 }
       
   170         }
       
   171 
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // CLBTContextSourcePlugin::ParseTriggerInformationL
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CLBTContextSourcePlugin::ParseTriggerInformationL( const CCFContextSourceSetting& aItem )
       
   179     {
       
   180     // Flages used to check if the tags are present & also to check repetition of tags
       
   181     TBool triggerNameSet = EFalse;
       
   182     TBool latitudeSet = EFalse;
       
   183     TBool longitudeSet = EFalse;
       
   184     TBool regionSet = EFalse;
       
   185     TBool radiusSet = EFalse;
       
   186     TBool contextSourceSet = EFalse;
       
   187     TBool directionSet = EFalse;
       
   188     TBool currentStateSet = EFalse;
       
   189     
       
   190     
       
   191     LBTCONTEXTSOURCEPLUGINDEBUG( "CLBTContextSourcePlugin::ParseTriggerInformationL : IN " );
       
   192     const RSettingParameterArray& parameters 
       
   193                             = aItem.Parameters();
       
   194     
       
   195     // Add check for improper syntax of XML file
       
   196     if ( aItem.Name() != KTriggerLocation )     
       
   197     {
       
   198     User::Leave( KErrGeneral );
       
   199     }
       
   200     
       
   201     TTriggerNode *triggerNode = new(ELeave) TTriggerNode();
       
   202     
       
   203     
       
   204     const RKeyValueArray& paramAttr = aItem.Attributes();
       
   205     for( TInt k = 0; k < paramAttr.Count(); ++k )
       
   206         {
       
   207             {
       
   208             TPtrC key( paramAttr[k]->Key() );
       
   209             TPtrC value( paramAttr[ k ]->Value() );
       
   210 
       
   211             if ( ! (key.Compare( KTriggerName ))) 
       
   212                 {
       
   213                 // INdicate that the name tag has been parsed
       
   214                 if( !triggerNameSet )
       
   215                     {
       
   216                     triggerNameSet = ETrue;
       
   217                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetTriggerNameL = %S", &value);
       
   218                     triggerNode->SetTriggerNameL( value );
       
   219                     }
       
   220                 }
       
   221             else if(!(key.CompareF( KLatitude ) ))
       
   222                 {
       
   223                 // Indicate that the latitude tag has been parsed
       
   224                 if( !latitudeSet )
       
   225                     {
       
   226                     latitudeSet = ETrue;
       
   227                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetLatitudeL = %S", &value);
       
   228                     triggerNode->SetLatitudeL( value );
       
   229                     }
       
   230                 }
       
   231             
       
   232             else if( !(key.CompareF( KLongitude ) ))
       
   233                 {
       
   234                 // Indicate that the longitude tag has been parsed
       
   235                 if( !longitudeSet )
       
   236                     {
       
   237                     longitudeSet = ETrue;
       
   238                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetLongitudeL = %S", &value);
       
   239                     triggerNode->SetLongitudeL( value );
       
   240                     }
       
   241                 }
       
   242             else if (!(key.CompareF( KRegion ) ))
       
   243                 {
       
   244                 // Indicate that the region tag has been parsed
       
   245                 if( !regionSet )
       
   246                     {
       
   247                     regionSet = ETrue;
       
   248                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetRegionL = %S", &value);
       
   249                     triggerNode->SetRegionL( value );
       
   250                     }
       
   251                 }
       
   252             
       
   253             else if ( !(key.CompareF( KRadius ) ))
       
   254                 {
       
   255                 // Indicate that the radius tag has been parsed
       
   256                 if( !radiusSet )
       
   257                     {
       
   258                     radiusSet = ETrue;
       
   259                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetRadiusL = %S", &value);
       
   260                     triggerNode->SetRadiusL( value );
       
   261                     }
       
   262                 }
       
   263             else if ( !(key.CompareF( KSource ) ) )
       
   264                 {
       
   265                 // Indicate that the contextsource tag has been parsed
       
   266                 if( !contextSourceSet )
       
   267                     {
       
   268                     contextSourceSet = ETrue;
       
   269                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetContextSource = %S", &value);
       
   270                     triggerNode->SetContextSource( value );
       
   271                     }
       
   272                 }
       
   273             else if ( !(key.CompareF( KDirection ) ) )
       
   274                 {
       
   275                 // Indicate that the direction tag has been parsed
       
   276                 if( !directionSet )
       
   277                     {
       
   278                     directionSet = ETrue;
       
   279                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetDirection = %S", &value); 
       
   280                     triggerNode->SetDirectionL( value );
       
   281                     }
       
   282                 }
       
   283             else if ( !(key.CompareF(KCurrentState) ) )
       
   284                 {
       
   285                 // Indicate that the current state tag has been parsed
       
   286                 if( !currentStateSet )
       
   287                     {
       
   288                     currentStateSet = ETrue;
       
   289                     LBTCONTEXTSOURCEPLUGINDEBUG1( "SetCurrentState = %S", &value);    
       
   290                     triggerNode->SetCurrentStateL( value );
       
   291                     }
       
   292                 }
       
   293             }
       
   294         }
       
   295     // Check if mandatory tags are present before appending to the trigger node list
       
   296     if( triggerNameSet && latitudeSet && longitudeSet && regionSet && radiusSet )
       
   297         {
       
   298         // Check if optional tags are present,if not set default values
       
   299         if( !contextSourceSet )
       
   300             {
       
   301             TPtrC defaultContextSource( KDefaultContextSourceName );
       
   302             triggerNode->SetContextSource( defaultContextSource );
       
   303             }
       
   304         if( !directionSet )
       
   305             {
       
   306             TPtrC defaultdirection( KNullDesC );
       
   307             triggerNode->SetDirectionL( defaultdirection );
       
   308             }
       
   309         if( !currentStateSet )
       
   310             {
       
   311             TPtrC defaultCurrentState ( KNullDesC );
       
   312             triggerNode->SetCurrentStateL( defaultCurrentState );
       
   313             }
       
   314         // Add to triggernode list
       
   315         iTriggerNodeList.Append( triggerNode );
       
   316         }
       
   317     else
       
   318         {
       
   319         // delete triggerNode
       
   320         delete triggerNode;
       
   321         User::Leave( KErrGeneral );
       
   322         }
       
   323     
       
   324     LBTCONTEXTSOURCEPLUGINDEBUG( "CLBTContextSourcePlugin::ParseTriggerInformationL : OUT " );
       
   325 
       
   326     }
       
   327 
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CLBTContextSourcePlugin::CreateTriggersL
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 void CLBTContextSourcePlugin::CreateTriggersL( )
       
   334     {
       
   335     if( iCurrentTriggerIndex<iTriggerNodeList.Count())
       
   336         {
       
   337         iLocationTrigger->CreateContextTriggerL( *iTriggerNodeList[iCurrentTriggerIndex] );
       
   338         }
       
   339     }
       
   340 
       
   341 // ---------------------------------------------------------------------------
       
   342 // CLBTContextSourcePlugin::InitializeL
       
   343 // ---------------------------------------------------------------------------
       
   344 //
       
   345 void CLBTContextSourcePlugin::InitializeL()
       
   346     {
       
   347     
       
   348     }
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 // CLBTContextSourcePlugin::DefineContextsL
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 void CLBTContextSourcePlugin::DefineContextsL()
       
   355     {
       
   356     if(iTriggersCreated)
       
   357         {
       
   358             DefineTriggerContexts();
       
   359         }
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 // CLBTContextSourcePlugin::DefineTriggerContextsL
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 void CLBTContextSourcePlugin::DefineTriggerContexts( )
       
   367     {
       
   368     TSecurityPolicy policy( ECapabilityLocalServices );
       
   369 
       
   370     for(int i=0; i<iTriggerList.Count() ; i++)
       
   371         {
       
   372         TInt err = iCF.DefineContext( iTriggerList[i]->contextName, KContextType, policy);
       
   373         LBTCONTEXTSOURCEPLUGINDEBUG1( "Defining ContextSource = %S", &iTriggerList[i]->contextName);
       
   374         }
       
   375     }
       
   376 
       
   377 // ---------------------------------------------------------------------------
       
   378 // CLBTContextSourcePlugin::ContextTriggerFiredL
       
   379 // ---------------------------------------------------------------------------
       
   380 //
       
   381 void CLBTContextSourcePlugin::ContextTriggerFiredL( TLbtTriggerId aTriggerId )
       
   382     {
       
   383     LBTCONTEXTSOURCEPLUGINDEBUG( "ContextTriggerFiredL :IN");
       
   384     TPtrC contextSource(KNullDesC);
       
   385     TPtrC contextName(KNullDesC);
       
   386     
       
   387     for(int i=0; i<iTriggerList.Count(); i++)
       
   388         {
       
   389         if (iTriggerList[i]->iTriggerId == aTriggerId)
       
   390             {
       
   391             contextSource.Set(iTriggerList[i]->contextName);
       
   392             contextName.Set(iTriggerList[i]->triggerName);
       
   393             break;
       
   394             }
       
   395         }
       
   396         
       
   397     CCFContextObject* context = CCFContextObject::NewLC();
       
   398 
       
   399     context->SetSourceL( contextSource );
       
   400     context->SetTypeL( KContextType );
       
   401     context->SetValueL( contextName );   
       
   402                     
       
   403     // Publish new context value
       
   404     RThread thread;
       
   405     CleanupClosePushL( thread );
       
   406     iCF.PublishContext( *context, thread );
       
   407     LBTCONTEXTSOURCEPLUGINDEBUG1( "Publishing Context = %S", &contextSource);
       
   408     LBTCONTEXTSOURCEPLUGINDEBUG1( "Context Value = %S", &contextName);
       
   409     thread.Close();
       
   410     
       
   411     CleanupStack::Pop( &thread );
       
   412     CleanupStack::PopAndDestroy( context );
       
   413     LBTCONTEXTSOURCEPLUGINDEBUG( "ContextTriggerFiredL :OUT");
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------------------------
       
   417 // CLBTContextSourcePlugin::TriggerCreatedL
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 void CLBTContextSourcePlugin::TriggerCreatedL( TLbtTriggerId& aTrigId , 
       
   421                                                 TPtrC& aContextName, 
       
   422                                                 TPtrC& aTriggerName )
       
   423     {
       
   424     TriggerList* newTrigger;
       
   425     newTrigger = new (ELeave) TriggerList;
       
   426     CleanupStack::PushL(newTrigger);
       
   427     newTrigger->iTriggerId = aTrigId;
       
   428     newTrigger->contextName.Set( aContextName );
       
   429     newTrigger->triggerName.Set( aTriggerName );
       
   430         
       
   431     iTriggerList.Append(newTrigger);
       
   432     iCurrentTriggerIndex++;
       
   433     if( iCurrentTriggerIndex <= iTriggerNodeList.Count()-1 )
       
   434         {            
       
   435         TInt err = KErrNone;
       
   436         TRAP(err, CreateTriggersL( ));
       
   437         while(err != KErrNone)
       
   438             {
       
   439             iCurrentTriggerIndex++;
       
   440             if( iCurrentTriggerIndex <= iTriggerNodeList.Count()-1 )
       
   441                 {
       
   442                 TRAP( err, CreateTriggersL() );
       
   443                 }
       
   444             }
       
   445         }
       
   446     else
       
   447         {
       
   448         iTriggersCreated = TRUE;
       
   449         DefineContextsL();
       
   450         }
       
   451     CleanupStack::Pop(newTrigger);
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CLBTContextSourcePlugin::NotifyError
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 void CLBTContextSourcePlugin::NotifyError( TInt /*aError*/ )
       
   459     {
       
   460     }
       
   461 
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // CLBTContextSourcePlugin::Extension
       
   465 // ---------------------------------------------------------------------------
       
   466 //
       
   467 TAny* CLBTContextSourcePlugin::Extension( const TUid& aExtensionUid ) const
       
   468     {
       
   469     // Check the extension
       
   470     if ( aExtensionUid == KCFContextSourceInterfaceUid )
       
   471             {
       
   472             return ( ( MCFContextSource* ) this );
       
   473             }
       
   474         else if ( aExtensionUid == KCFContextSourceUpgradeUid )
       
   475             {
       
   476             return ( ( MCFContextSourceUpgrade* ) this );
       
   477             }
       
   478         else
       
   479             {
       
   480             return NULL;
       
   481             }
       
   482     }
       
   483     
       
   484 // ---------------------------------------------------------------------------
       
   485 // CLBTContextSourcePlugin::UpdateSettingsL
       
   486 // ---------------------------------------------------------------------------
       
   487 //
       
   488 void CLBTContextSourcePlugin::UpdateSettingsL( CCFContextSourceSettingArray* aSettingList )
       
   489     {
       
   490         LBTCONTEXTSOURCEPLUGINDEBUG( "UpdateSettingsL() : IN");
       
   491         
       
   492         // Reset list index number to zero.
       
   493         iCurrentTriggerIndex = 0;
       
   494         // Stop monitoring the triggers to avoid a call back for this
       
   495         // delete
       
   496         iLbtManagerObserver->Cancel();
       
   497         
       
   498         // Delete all existing triggers created by the plugin
       
   499         LBTCONTEXTSOURCEPLUGINDEBUG( "DeleteAllTriggersL");
       
   500         iTriggersCreated = FALSE;
       
   501         TRAPD(error, iLocationTrigger->DeleteAllTriggersL() ); 
       
   502         // CHeck if the delete is leaving because there is no triggers 
       
   503         // to be created(i.e KErrNotFound),if yes then go ahead & create 
       
   504         // the triggers
       
   505         if( !error || error == KErrNotFound )
       
   506             {
       
   507             // start observer
       
   508             iLbtManagerObserver->StartManagerObserver();
       
   509             // Read updated setting files and re-create triggers
       
   510             HandleSettingL( aSettingList );
       
   511             }
       
   512         LBTCONTEXTSOURCEPLUGINDEBUG( "UpdateSettingsL() : OUT");
       
   513     }
       
   514 
       
   515 // ---------------------------------------------------------------------------
       
   516 // CLBTContextSourcePlugin::CheckValidity
       
   517 // ---------------------------------------------------------------------------
       
   518 //
       
   519 TInt CLBTContextSourcePlugin::CheckValidity( RThread& aClientThread, 
       
   520         const CCFContextSourceSettingArray& aSettingList )
       
   521     {
       
   522     TBool validFile = EFalse;
       
   523     
       
   524     // Check capabilities of client application
       
   525     if ( !aClientThread.HasCapability( ECapabilityLocation ) )
       
   526         {
       
   527         return KErrPermissionDenied;
       
   528         }
       
   529     
       
   530     // Check that the file contains atleast a single valid node,else
       
   531     // leave with KErrNotSupported
       
   532     for ( TInt i = 0; i < aSettingList.Count(); i++ )
       
   533         {
       
   534         const CCFContextSourceSetting& item = aSettingList.Item( i );
       
   535 
       
   536         const RSettingParameterArray& parameters 
       
   537                                 = item.Parameters();
       
   538         
       
   539         // Add check for improper syntax of XML file
       
   540         if ( item.Name() != KTriggerLocation )     
       
   541             {
       
   542             validFile = EFalse; 
       
   543             }
       
   544         else
       
   545             {
       
   546             validFile = ETrue;
       
   547             break;
       
   548             }
       
   549         }
       
   550     
       
   551     if ( validFile )
       
   552         {
       
   553         LBTCONTEXTSOURCEPLUGINDEBUG( "CheckValidity() : OUT , returning KErrNone");
       
   554         return KErrNone;
       
   555         }
       
   556     else
       
   557         {
       
   558         LBTCONTEXTSOURCEPLUGINDEBUG( "CheckValidity() : OUT , returning KErrNotSupported" );
       
   559         return KErrNotSupported;
       
   560         }
       
   561     }