Example Application Guide

 

geoprofilesengine.cpp

00001 /*
00002 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
00003 * All rights reserved.
00004 * This component and the accompanying materials are made available
00005 * under the terms of the License "Eclipse Public License v1.0"
00006 * which accompanies this distribution, and is available
00007 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 *
00009 * Initial Contributors:
00010 * Nokia Corporation - initial contribution.
00011 *
00012 * Contributors:
00013 *
00014 * Description:  Engine for Geoprofiles application
00015 *
00016 */
00017 
00018 
00019 // SYSTEM INCLUDES
00020 #include <lbtgeocircle.h> 
00021 #include <lbttriggerconditionarea.h> 
00022 #include <lbttriggerfilterbyattribute.h> 
00023 #include <lbtlisttriggeroptions.h>
00024 #include <lbtstartuptrigger.h>
00025 #include <lbttriggerinfo.h>
00026 #include <lbttriggerentry.h>
00027 #include <lbtgeocell.h>
00028 #include <s32mem.h>
00029 #include <LbsPosition.h>
00030 #include <aknwaitdialog.h>
00031 #include <geoprofilesui.rsg>
00032 
00033 // USER INCLUDES
00034 #include "geoprofilesengine.h"
00035 #include "geoprofilesuid.hrh"
00036 #include "geoprofile.h"
00037 #include "geoprofilesdebug.h"
00038 
00039 // CONSTANT DEFINTION
00040 _LIT( KMyGeoProfile, "GeoProfiles" );
00041 
00042 //Set executable file name of the trigger handling process.
00043 _LIT( KTriggerHandlingProcessName, "geoprofileshandler.exe" );
00044 _LIT_SECURE_ID( triggerHandlingProcessSID,  KGeoProfilesHandlerUid ); 
00045 _LIT_SECURE_ID( managerProcessSID,  KGeoProfilesUiUid ); 
00046 
00047 // ---------------------------------------------------------------------------
00048 // CGeoProfilesEngine::CGeoProfilesEngine
00049 // ---------------------------------------------------------------------------
00050 //
00051 CGeoProfilesEngine::CGeoProfilesEngine( MGeoProfilesEngineObserver&   aObserver )
00052     :CActive( EPriorityStandard ),
00053     iObserver( aObserver ),
00054     iEngineState( CGeoProfilesEngine::ENoAction ) 
00055     {    
00056     }
00057     
00058 // ---------------------------------------------------------------------------
00059 // CGeoProfilesEngine::~CGeoProfilesEngine
00060 // ---------------------------------------------------------------------------
00061 // 
00062 CGeoProfilesEngine::~CGeoProfilesEngine()
00063     {
00064     GEOPROFILEDEBUG("+ CGeoProfilesEngine::~CGeoProfilesEngine" )
00065         
00066     Cancel();
00067 
00068     // Close the notifier handle
00069     iNotifier->Cancel();
00070     delete iNotifier;
00071      
00072     delete iStartupTrigger;       
00073     
00074     // Close the server handles
00075     iLbt.Close();
00076     iLbtNotifier.Close();
00077     iLbtServer.Close();
00078     
00079     GEOPROFILEDEBUG("- CGeoProfilesEngine::~CGeoProfilesEngine" )
00080     }
00081     
00082 // ---------------------------------------------------------------------------
00083 // CGeoProfilesEngine::NewL
00084 // ---------------------------------------------------------------------------
00085 // 
00086 CGeoProfilesEngine* CGeoProfilesEngine::NewL( MGeoProfilesEngineObserver&   aObserver )
00087     {
00088     GEOPROFILEDEBUG("+ CGeoProfilesEngine::NewL" )
00089     
00090     CGeoProfilesEngine* self = new ( ELeave ) CGeoProfilesEngine( aObserver );
00091     CleanupStack::PushL( self );
00092     self->ConstructL();
00093     CleanupStack::Pop( self );
00094     
00095     GEOPROFILEDEBUG("- CGeoProfilesEngine::NewL" )
00096     return self;                                                                  
00097     }
00098     
00099 // ---------------------------------------------------------------------------
00100 // CGeoProfilesEngine::ConstructL
00101 // ---------------------------------------------------------------------------
00102 //
00103 void CGeoProfilesEngine::ConstructL()
00104     {
00105     GEOPROFILEDEBUG("- CGeoProfilesEngine::ConstructL" )
00106     
00107     // Open the LBT server handle
00108     iLbtServer.Connect();
00109     
00110     // Open the session handle
00111     iLbt.Open( iLbtServer );
00112       
00113     // Open the session for notifiers
00114     iLbtNotifier.Open( iLbtServer );
00115         
00116     // Open the Trigger Notifier handle
00117     iNotifier = CLbtTriggerChangeEventNotifier::NewL( iLbtNotifier, *this );
00118     iNotifier->Start();
00119     
00120     CActiveScheduler::Add( this );
00121     
00122     GEOPROFILEDEBUG("- CGeoProfilesEngine::ConstructL" )  
00123     }
00124 
00125 // ---------------------------------------------------------------------------
00126 // CGeoProfilesEngine::RunL
00127 // ---------------------------------------------------------------------------
00128 //
00129 void CGeoProfilesEngine::RunL()
00130     {
00131     GEOPROFILEDEBUG1("+ CGeoProfilesEngine::RunL %d", iStatus.Int())
00132     
00133     if ( iStatus.Int() != KErrNone )
00134         {
00135         iEngineState = ENoAction;
00136         
00137         // Dismiss the Wait dialog if it is Running
00138         DismissWaitDialogL();
00139         return;
00140         }
00141                 
00142     switch ( iEngineState )
00143         {
00144         case ECreateProfile:
00145             {
00146             // The profile has been successfully created.
00147             delete iStartupTrigger;
00148             iStartupTrigger = NULL;            
00149             iEngineState = ENoAction;
00150             
00151             // Dismiss the Wait Dialog
00152             DismissWaitDialogL();
00153             
00154             break;    
00155             }
00156         case EDeleteProfile:
00157             {
00158             // Delet the Trigger
00159             iLbt.DeleteTriggerL( iTriggerId );
00160             iEngineState = ENoAction;
00161            
00162             // Dismiss the Wait Dialog
00163             DismissWaitDialogL();
00164             break;    
00165             }
00166         case EUpdateProfile:
00167             {
00168             // Update the Profile
00169             DoUpdateProfileL( *iProfile );
00170             iProfile = NULL;
00171             
00172             iEngineState = ENoAction;
00173             // Dismiss the Wait Dialog
00174             DismissWaitDialogL();            
00175                 
00176             }
00177         default:
00178             {
00179             iEngineState = ENoAction;
00180             // Dismiss the Wait Dialog
00181             DismissWaitDialogL();  
00182             break;                
00183             }
00184         }    
00185     GEOPROFILEDEBUG("- CGeoProfilesEngine::RunL")
00186     }
00187     
00188 
00189 // ---------------------------------------------------------------------------
00190 // CGeoProfilesEngine::DoCancel
00191 // ---------------------------------------------------------------------------
00192 //
00193 void CGeoProfilesEngine::DoCancel()
00194     {
00195     GEOPROFILEDEBUG("+ CGeoProfilesEngine::DoCancel")
00196     
00197     iLbt.CancelAll();        
00198     
00199     DismissWaitDialogL();
00200     
00201     GEOPROFILEDEBUG("- CGeoProfilesEngine::DoCancel")
00202     }
00203 
00204 // ---------------------------------------------------------------------------
00205 // void CGeoProfilesEngine::AddProfileL
00206 // ---------------------------------------------------------------------------
00207 //    
00208 void CGeoProfilesEngine::AddProfileL( CGeoProfile&      aProfile )
00209     {    
00210     GEOPROFILEDEBUG("+ CGeoProfilesEngine::AddProfileL")
00211     
00212     if ( aProfile.GeoPositionType() == CGeoProfile::EUndefined )
00213         {
00214         User::Leave( KErrArgument );
00215         }
00216         
00217     if ( IsActive())
00218         {
00219         User::Leave( KErrInUse );
00220         }
00221         
00222     CLbtTriggerConditionArea* condition = NULL;
00223     
00224     // Currently only co-ordinates are supported
00225     if( Math::IsNaN( aProfile.GeoPositionInfo().Latitude() ) || 
00226         Math::IsNaN( aProfile.GeoPositionInfo().Longitude()))
00227         {
00228         User::Leave( KErrArgument );
00229         }
00230     TInt radius = aProfile.GeoProfileRadius(); 
00231     if ( !Math::IsNaN( aProfile.GeoPositionInfo().HorizontalAccuracy()))
00232         {
00233         radius += aProfile.GeoPositionInfo().HorizontalAccuracy();
00234         }
00235         
00236     //Define the triggering area   
00237     CLbtGeoCircle * trigArea = CLbtGeoCircle ::NewLC( aProfile.GeoPositionInfo(),
00238                                                       radius );
00239     //Construct trigger condition.
00240     
00241     CLbtTriggerConditionArea::TDirection triggerType = CLbtTriggerConditionArea::EFireOnEnter;
00242     if (aProfile.TriggerType() == CGeoProfile::EExitTrigger )
00243         {
00244         triggerType = CLbtTriggerConditionArea::EFireOnExit;
00245         }
00246     // Currently only Entry type Triggers are suppored
00247     condition = CLbtTriggerConditionArea::NewL( trigArea, 
00248                                                 triggerType );
00249     
00250     //ownership of trigArea is transferred.                                                 
00251     CleanupStack::Pop( trigArea );
00252     
00253     // Load Command line arguments
00254     HBufC* cmdLineArg = aProfile.PartialExternalizeLC();
00255     
00256     // Create the startup trigger
00257     iStartupTrigger = CLbtStartupTrigger::NewL( aProfile.GeoPlaceName(),
00258                                                 CLbtTriggerEntry::EStateEnabled,
00259                                                 CRequestorBase::ERequestorService,
00260                                                 CRequestorBase::EFormatApplication,
00261                                                 KMyGeoProfile,
00262                                                 managerProcessSID,
00263                                                 condition,
00264                                                 KTriggerHandlingProcessName,
00265                                                 triggerHandlingProcessSID,
00266                                                 *cmdLineArg );    
00267     CleanupStack::PopAndDestroy( cmdLineArg ); 
00268     
00269                                                     
00270     iTriggerId = 0xFFFFFFFF;
00271     iEngineState = ECreateProfile;
00272     
00273     // Create the trigger 
00274     iLbt.CreateTrigger( *iStartupTrigger, iTriggerId, ETrue, iStatus );
00275     SetActive();
00276     
00277     // Launch the Wait Dialog
00278     LaunchWaitDialogL( R_LBL_GEOPROFILES_WAITDLG_CREATE );
00279     
00280     GEOPROFILEDEBUG("- CGeoProfilesEngine::AddProfileL")
00281     }
00282 
00283 // ---------------------------------------------------------------------------
00284 // void CGeoProfilesEngine::RemoveProfileL
00285 // ---------------------------------------------------------------------------
00286 //
00287 void CGeoProfilesEngine::UpdateProfileL( CGeoProfile&      aProfile )
00288     { 
00289     GEOPROFILEDEBUG("+ CGeoProfilesEngine::UpdateProfileL")
00290 
00291     if ( IsActive())
00292         {
00293         User::Leave( KErrInUse );
00294         }
00295                     
00296     iEngineState = EUpdateProfile;    
00297     iProfile = &aProfile;
00298     
00299     SetActive();
00300     TRequestStatus* status = &iStatus;
00301     User::RequestComplete( status, KErrNone );
00302     
00303     // Launch the Wait Dialog
00304     LaunchWaitDialogL( R_LBL_GEOPROFILES_WAITDLG_UPDATE );    
00305     
00306     GEOPROFILEDEBUG("- CGeoProfilesEngine::UpdateProfileL")             
00307     }
00308     
00309 // ---------------------------------------------------------------------------
00310 // void CGeoProfilesEngine::RemoveProfileL
00311 // ---------------------------------------------------------------------------
00312 //
00313 void CGeoProfilesEngine::RemoveProfileL( CGeoProfile&      aProfile )
00314     {
00315     GEOPROFILEDEBUG("+ CGeoProfilesEngine::RemoveProfileL")
00316 
00317     if ( IsActive())
00318         {
00319         User::Leave( KErrInUse );
00320         }
00321             
00322     iEngineState = EDeleteProfile;    
00323     iTriggerId = aProfile.TriggerId();
00324     
00325     SetActive();
00326     TRequestStatus* status = &iStatus;
00327     User::RequestComplete( status, KErrNone );
00328     
00329     // Launch the Wait Dialog
00330     LaunchWaitDialogL( R_LBL_GEOPROFILES_WAITDLG_DELETE );
00331     
00332     GEOPROFILEDEBUG("- CGeoProfilesEngine::RemoveProfileL")
00333     }
00334     
00335 // ---------------------------------------------------------------------------
00336 // void CGeoProfilesEngine::ListProfilesL
00337 // ---------------------------------------------------------------------------
00338 //
00339 void CGeoProfilesEngine::ListProfilesL( RPointerArray< CGeoProfile>&   aGeoProfiles )
00340     {
00341     GEOPROFILEDEBUG("+ CGeoProfilesEngine::ListProfilesL")
00342     
00343     // Clean up the existing profiles
00344     aGeoProfiles.ResetAndDestroy();
00345     
00346     // Fetch the individual list of triggers
00347     RArray < TLbtTriggerId > aTriggerIdList;    
00348     iLbt.ListTriggerIdsL( aTriggerIdList );
00349     
00350     // Load the individual profiles
00351     for ( TInt i = 0; i < aTriggerIdList.Count(); i++ )
00352         {
00353         CGeoProfile* profile = CGeoProfile::NewL();
00354         CleanupStack::PushL( profile );
00355         
00356         // Fetch the trigger associated with the Id.
00357         CLbtTriggerInfo* trigger = iLbt.GetTriggerLC( aTriggerIdList[i]);
00358         CLbtTriggerEntry* triggerEntry = trigger->TriggerEntry();
00359         
00360         // Set the Location Information
00361         CLbtTriggerConditionArea* triggArea =  
00362                     static_cast< CLbtTriggerConditionArea*>( triggerEntry->GetCondition());
00363                     
00364         // Set the Trigger Type
00365         if ( triggArea->Direction() == CLbtTriggerConditionArea::EFireOnEnter )
00366             {
00367             profile->SetTriggerType( CGeoProfile::EEntryTrigger );
00368             }
00369         else
00370             {
00371             profile->SetTriggerType( CGeoProfile::EExitTrigger );   
00372             }
00373             
00374         CLbtGeoAreaBase* triggGeoArea = 
00375                     static_cast< CLbtGeoAreaBase*>( triggArea->TriggerArea());
00376         
00377         // Set Coordinate Information            
00378         if ( triggGeoArea->Type() == CLbtGeoAreaBase::ECircle )
00379             {
00380             CLbtGeoCircle* loc = static_cast<CLbtGeoCircle*>( triggArea->TriggerArea() );
00381             TLocality locality;
00382             locality.SetCoordinate( loc->Center().Latitude(),
00383                                     loc->Center().Longitude());
00384             profile->SetGeoPositionInfoL( triggerEntry->Name(), locality );           
00385             }
00386         // Copy the Trigger Id
00387         profile->SetTriggerId( triggerEntry->Id());
00388         
00389         // Load all other information
00390         CLbtStartupTrigger* startupTrigger = static_cast<CLbtStartupTrigger*>( triggerEntry );
00391         TPtrC cmdLine = startupTrigger->CommandLine();           
00392         // Internalize the contents
00393         profile->PartialInternalizeL( cmdLine );
00394         CleanupStack::PopAndDestroy( trigger );
00395         
00396         // Append the profile structure to the array
00397         if ( aGeoProfiles.Append( profile ))
00398             {
00399             CleanupStack::PopAndDestroy( profile );    
00400             }
00401         else
00402             {
00403             CleanupStack::Pop( profile );    
00404             }
00405         }
00406     aTriggerIdList.Reset();
00407     aTriggerIdList.Close();
00408     
00409     GEOPROFILEDEBUG("- CGeoProfilesEngine::ListProfilesL") 
00410     }
00411 
00412 // ---------------------------------------------------------------------------
00413 // void CGeoProfilesEngine::TriggerChangedL
00414 // ---------------------------------------------------------------------------
00415 //    
00416 void CGeoProfilesEngine::TriggerChangedL( const TLbtTriggerChangeEvent& /* aEvent*/ )
00417     {
00418     iObserver.ProfileStatusChangedL();   
00419     }
00420 
00421 // ---------------------------------------------------------------------------
00422 // void CGeoProfilesEngine::LaunchWaitDialogL
00423 // ---------------------------------------------------------------------------
00424 //     
00425 void CGeoProfilesEngine::LaunchWaitDialogL(  TInt aResource )
00426     {
00427     GEOPROFILEDEBUG("+ CGeoProfilesEngine::LaunchWaitDialogL") 
00428     
00429     iWaitDialog = new ( ELeave ) CAknWaitDialog (( REINTERPRET_CAST( CEikDialog**, &iWaitDialog )), ETrue );          
00430     iWaitDialog->ExecuteLD( aResource );
00431     iWaitDialog = NULL;
00432     
00433     GEOPROFILEDEBUG("+ CGeoProfilesEngine::LaunchWaitDialogL") 
00434     }
00435 
00436 // ---------------------------------------------------------------------------
00437 // void CGeoProfilesEngine::DismissWaitDialogL
00438 // ---------------------------------------------------------------------------
00439 // 
00440 void CGeoProfilesEngine::DismissWaitDialogL()
00441     {
00442     GEOPROFILEDEBUG("+ CGeoProfilesEngine::DismissWaitDialogL") 
00443     
00444     if ( iWaitDialog )
00445         {               
00446         TRAPD( err, iWaitDialog->ProcessFinishedL() );
00447         if ( err )
00448                 {
00449                     delete iWaitDialog;
00450             iWaitDialog = NULL;          
00451                 }               
00452         }
00453     GEOPROFILEDEBUG("+ CGeoProfilesEngine::DismissWaitDialogL")  
00454     }
00455     
00456 // ---------------------------------------------------------------------------
00457 // void CGeoProfilesEngine::DoUpdateProfileL
00458 // ---------------------------------------------------------------------------
00459 //     
00460 void CGeoProfilesEngine::DoUpdateProfileL(  CGeoProfile&      aProfile )
00461     {
00462     GEOPROFILEDEBUG("+ CGeoProfilesEngine::DoUpdateProfileL") 
00463       
00464     if ( aProfile.GeoPositionType() == CGeoProfile::EUndefined )
00465         {
00466         User::Leave( KErrArgument );
00467         }
00468             
00469     CLbtTriggerInfo* trigger = iLbt.GetTriggerLC( aProfile.TriggerId());
00470     
00471     CLbtStartupTrigger* startupTrigger = 
00472         static_cast< CLbtStartupTrigger* >( trigger->TriggerEntry() );
00473         
00474     // Copy the Profile Name
00475     startupTrigger->SetNameL( aProfile.GeoPlaceName());
00476     
00477     // Copy the co-ordinates and radius  
00478     if( Math::IsNaN( aProfile.GeoPositionInfo().Latitude() ) || 
00479         Math::IsNaN( aProfile.GeoPositionInfo().Longitude()))
00480         {
00481         User::Leave( KErrArgument );
00482         }
00483     TInt radius = aProfile.GeoProfileRadius(); 
00484     if ( !Math::IsNaN( aProfile.GeoPositionInfo().HorizontalAccuracy()))
00485         {
00486         radius += aProfile.GeoPositionInfo().HorizontalAccuracy();
00487         }
00488      
00489     // Remove the Initial condition area
00490     startupTrigger->SetCondition( NULL );
00491                     
00492     // Define the triggering area   
00493     CLbtGeoCircle * trigArea = CLbtGeoCircle ::NewLC( aProfile.GeoPositionInfo(), radius );
00494     
00495     CLbtTriggerConditionArea::TDirection triggerType = CLbtTriggerConditionArea::EFireOnEnter;
00496     if (aProfile.TriggerType() == CGeoProfile::EExitTrigger )
00497         {
00498         triggerType = CLbtTriggerConditionArea::EFireOnExit;
00499         }
00500     CLbtTriggerConditionArea* condition = CLbtTriggerConditionArea::NewL( trigArea, 
00501                                                                           triggerType );
00502     
00503     //ownership of trigArea is transferred.                                                 
00504     CleanupStack::Pop( trigArea );
00505     
00506     // Set the New condition    
00507     startupTrigger->SetCondition( condition );
00508     
00509     
00510     // Set the Command line arguments
00511     HBufC* cmdLineArg = aProfile.PartialExternalizeLC();        
00512     startupTrigger->SetCommandLineL( *cmdLineArg );
00513         
00514     // Update the Trigger
00515     TLbtTriggerAttributeFieldsMask mask = 
00516             CLbtTriggerEntry::EAttributeName | 
00517             CLbtTriggerEntry::EAttributeCondition | 
00518             CLbtTriggerEntry::EAttributeStartUpCommandLine;
00519          
00520     iLbt.UpdateTriggerL( *startupTrigger, mask, ELbtTrue );     
00521     CleanupStack::PopAndDestroy( cmdLineArg );        
00522     CleanupStack::PopAndDestroy( trigger );
00523     
00524     GEOPROFILEDEBUG("- CGeoProfilesEngine::DoUpdateProfileL")        
00525     }

© Nokia 2009

Back to top