locationtriggering/ltcontextsourceplugin/src/lbttriggeradapter.cpp
changeset 0 667063e416a2
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:  Location Trigger Creation and monitoring
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "lbttriggeradapter.h"
       
    20 #include "lbtcontextsourceplugindebug.h"
       
    21 #include "lbtcontextsourcepluginconst.hrh"
       
    22 
       
    23 
       
    24 // ---------------------------------------------------------------------------
       
    25 // CLocationTriggerAdapter::CLocationTriggerAdapter
       
    26 // ---------------------------------------------------------------------------
       
    27 //
       
    28 CLocationTriggerAdapter::CLocationTriggerAdapter( MLBTContextTriggerObserver& aObserver ) :
       
    29 CActive( EPriorityStandard ),iNotifier(NULL),
       
    30 iObserver( aObserver )
       
    31     {
       
    32     LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter()");
       
    33     CActiveScheduler::Add(this);
       
    34     }
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CLocationTriggerAdapter::~CLocationTriggerAdapter
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CLocationTriggerAdapter::~CLocationTriggerAdapter()
       
    41     {
       
    42     CActive::Cancel();
       
    43     
       
    44     LBTCONTEXTSOURCEPLUGINDEBUG( "~CLocationTriggerAdapter()");
       
    45     
       
    46     LBTCONTEXTSOURCEPLUGINDEBUG( "Closing Lbt");
       
    47     iLbt.Close();
       
    48     
       
    49     LBTCONTEXTSOURCEPLUGINDEBUG( "Closing Lbt Notifier");
       
    50     iLbtNotifier.Close();
       
    51     
       
    52     LBTCONTEXTSOURCEPLUGINDEBUG( "Closing Lbt Server");
       
    53     iLbtServ.Close();
       
    54     
       
    55     if( iNotifier )
       
    56         {
       
    57         delete iNotifier;
       
    58         iNotifier = NULL;
       
    59         }
       
    60     
       
    61     LBTCONTEXTSOURCEPLUGINDEBUG( "~CLocationTriggerAdapter : Out");
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // CLocationTriggerAdapter::NewLC
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CLocationTriggerAdapter* CLocationTriggerAdapter::NewLC( MLBTContextTriggerObserver& aObserver )
       
    69     {
       
    70     CLocationTriggerAdapter* self = new (ELeave) CLocationTriggerAdapter( aObserver );
       
    71     CleanupStack::PushL(self);
       
    72     self->ConstructL();
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CLocationTriggerAdapter::NewL
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CLocationTriggerAdapter* CLocationTriggerAdapter::NewL( MLBTContextTriggerObserver& aObserver )
       
    81     {
       
    82     CLocationTriggerAdapter* self = CLocationTriggerAdapter::NewLC( aObserver );
       
    83     CleanupStack::Pop(self);
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CLocationTriggerAdapter::ConstructL
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CLocationTriggerAdapter::ConstructL()
       
    92     {
       
    93     LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::ConstructL() : In");
       
    94     TInt ret = iLbtServ.Connect();
       
    95 
       
    96     if( ret == KErrNone ) 
       
    97         {
       
    98         LBTCONTEXTSOURCEPLUGINDEBUG( "Opening LBT connection");
       
    99         User::LeaveIfError(iLbt.Open(iLbtServ));
       
   100         User::LeaveIfError(iLbtNotifier.Open(iLbtServ));
       
   101         
       
   102         LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::ConstructL() : Out");
       
   103         }
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // CLocationTriggerAdapter::CreateContextTriggerL
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CLocationTriggerAdapter::CreateContextTriggerL( TTriggerNode& aTriggerNode )
       
   111     {
       
   112     LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::CreateContextTriggerL - IN" );
       
   113     iTriggerList.iTriggerId = KLbtNullTriggerId;
       
   114     iTriggerList.contextName.Set( aTriggerNode.GetContextSource() );
       
   115     TPtrC contextSource = aTriggerNode.GetContextSource();
       
   116     LBTCONTEXTSOURCEPLUGINDEBUG1( "GetContextSource = %S", &contextSource );
       
   117     // Get the trigger name
       
   118     TPtrC triggerName;
       
   119     if( !( aTriggerNode.GetTriggerName( triggerName ) ) )
       
   120         {
       
   121         iTriggerList.triggerName.Set( triggerName );
       
   122         LBTCONTEXTSOURCEPLUGINDEBUG1( "GetTriggerName = %S", &triggerName );
       
   123         if ( IsActive() )
       
   124             {
       
   125             User::Leave( KErrInUse );
       
   126             }
       
   127         // Get the latitude & longitude
       
   128         TReal latitude;
       
   129         TReal longitude;
       
   130         if( !( aTriggerNode.GetLatitude( latitude ) ) && 
       
   131                 !( aTriggerNode.GetLongitude( longitude ) ) )
       
   132             {
       
   133             LBTCONTEXTSOURCEPLUGINDEBUG1( "GetLatitude = %f", latitude );
       
   134             LBTCONTEXTSOURCEPLUGINDEBUG1( "GetLongitude = %f", longitude );
       
   135             TCoordinate centre( latitude,longitude ); 
       
   136             // Get the region
       
   137             TPtrC region;
       
   138             if( !( aTriggerNode.GetRegion( region ) ) )
       
   139                 {
       
   140                 LBTCONTEXTSOURCEPLUGINDEBUG1( "GetRegion = %S", &region );
       
   141                 // Get the radius
       
   142                 TReal radius;
       
   143                 if( !( aTriggerNode.GetRadius( radius ) ) )
       
   144                     {
       
   145                     LBTCONTEXTSOURCEPLUGINDEBUG1( "GetRadius = %f", radius );
       
   146                     CLbtGeoCircle* trigArea = CLbtGeoCircle::NewL( 
       
   147                                         centre,radius );
       
   148                                             
       
   149                     CleanupStack::PushL( trigArea );
       
   150 
       
   151                     CLbtTriggerConditionArea* cond = 
       
   152                         CLbtTriggerConditionArea::NewL( 
       
   153                         trigArea,
       
   154                         aTriggerNode.GetDirection() );
       
   155                     
       
   156                     LBTCONTEXTSOURCEPLUGINDEBUG1( "GetDirection = %d", aTriggerNode.GetDirection() );
       
   157                         
       
   158                     
       
   159                     CleanupStack::PushL( cond );
       
   160                     
       
   161                     TUid managerUi;
       
   162                     managerUi = TUid::Uid( KLBTContextSourcePluginDllUid );
       
   163                     
       
   164                     CLbtSessionTrigger* trig = CLbtSessionTrigger::NewL( 
       
   165                             triggerName,               
       
   166                             aTriggerNode.GetCurrentState(),
       
   167                             CRequestorBase::ERequestorService,         
       
   168                             CRequestorBase::EFormatApplication,         
       
   169                             KMyRequestor,                               
       
   170                             managerUi,                                   
       
   171                             cond                                       
       
   172                         );  
       
   173                     LBTCONTEXTSOURCEPLUGINDEBUG1( "GetCurrentState = %d", aTriggerNode.GetCurrentState() );
       
   174                         
       
   175                     CleanupStack::PushL( trig );
       
   176                     
       
   177                     iLbt.CreateTrigger( *trig, iTriggerList.iTriggerId, ETrue, iStatus );
       
   178                     
       
   179                     SetActive();
       
   180                     
       
   181                     CleanupStack::PopAndDestroy( trig );
       
   182                     CleanupStack::Pop( cond );
       
   183                     CleanupStack::Pop( trigArea ); 
       
   184                     }
       
   185                 else
       
   186                     {
       
   187                     User::Leave(KErrGeneral);
       
   188                     }
       
   189                 }
       
   190             else
       
   191                 {
       
   192                 User::Leave(KErrGeneral);
       
   193                 }
       
   194             }
       
   195         else
       
   196             {
       
   197             User::Leave(KErrGeneral);
       
   198             }
       
   199         }
       
   200     else  
       
   201 	    {      
       
   202         User::Leave(KErrGeneral);
       
   203 		}
       
   204     LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::CreateContextTriggerL - OUT" ); 
       
   205    }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // CLocationTriggerAdapter::TriggerFiredL
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CLocationTriggerAdapter::TriggerFiredL( const TLbtTriggerFireInfo& /*aFireInfo*/ )
       
   212   {
       
   213   LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::TriggerFiredL() : In");
       
   214   
       
   215   RArray < TLbtTriggerFireInfo > triggerInfoList;
       
   216   iLbt.GetFiredTriggersL(triggerInfoList);
       
   217   CleanupClosePushL( triggerInfoList );
       
   218   
       
   219   for( int i=0; i<triggerInfoList.Count(); i++)
       
   220       {
       
   221       LBTCONTEXTSOURCEPLUGINDEBUG1( "Trigger Fired, Trigger id : %d", triggerInfoList[i].iTriggerId );
       
   222       TLbtTriggerId firedTrigger = triggerInfoList[i].iTriggerId;
       
   223       iObserver.ContextTriggerFiredL( firedTrigger );
       
   224       }
       
   225 
       
   226   CleanupStack::PopAndDestroy( &triggerInfoList );
       
   227   LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::TriggerFiredL() : Out");
       
   228   }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // CLocationTriggerAdapter::DeleteAllTriggersL
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void CLocationTriggerAdapter::DeleteAllTriggersL()
       
   235     {
       
   236     iLbt.DeleteTriggersL( NULL );
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // CLocationTriggerAdapter::RunL
       
   241 // ---------------------------------------------------------------------------
       
   242 //
       
   243 void CLocationTriggerAdapter::RunL()
       
   244     {
       
   245     LBTCONTEXTSOURCEPLUGINDEBUG( "CLocationTriggerAdapter::RunL() : In");
       
   246     if(iStatus.Int()!=KErrNone)
       
   247         {       
       
   248         LBTCONTEXTSOURCEPLUGINDEBUG( "Trigger Create FAILED");
       
   249         // Trigger not created properly
       
   250         iTriggerList.iTriggerId = KLbtNullTriggerId;                
       
   251         User::Leave( iStatus.Int() );
       
   252         return;
       
   253         }
       
   254              
       
   255     if( !iNotifier )
       
   256         {
       
   257         LBTCONTEXTSOURCEPLUGINDEBUG( "Starting LBT Notifier");
       
   258         iNotifier = CLbtTriggerFiringEventNotifier::NewL( iLbtNotifier,*this );
       
   259         iNotifier->Start();
       
   260         }
       
   261     
       
   262     // If trigger created successfully, call define contexts
       
   263     LBTCONTEXTSOURCEPLUGINDEBUG1( "Trigger Create Successful, Trigger ID : %d",iTriggerList.iTriggerId);
       
   264     iObserver.TriggerCreatedL( iTriggerList.iTriggerId , 
       
   265                                iTriggerList.contextName , 
       
   266                                iTriggerList.triggerName );
       
   267     }
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // CLocationTriggerAdapter::RunError
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 TInt CLocationTriggerAdapter::RunError( TInt aError )
       
   274     {
       
   275     if( aError != KErrNone )
       
   276         {
       
   277         LBTCONTEXTSOURCEPLUGINDEBUG1( "Trigger Create FAILED with error : %d",aError);
       
   278         //if error, notify it to the engine.      
       
   279         iObserver.NotifyError( aError );
       
   280         return aError;
       
   281         }
       
   282     return KErrNone;
       
   283     }
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // CLocationTriggerAdapter::DoCancel
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 void CLocationTriggerAdapter::DoCancel()
       
   290     {    
       
   291     iLbt.CancelAll();
       
   292 
       
   293     }