Example Application Guide

 

geoplaceselector.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:  Geo Profile Place selector
00015 *
00016 */
00017 
00018 
00019 // SYSTEM INCLUDES
00020 #include <BADESCA.H>
00021 #include <stringloader.h>
00022 #include <aknlistquerydialog.h>
00023 #include <aknpopupheadingpane.h> 
00024 #include <geoprofilesui.rsg>
00025 #include <clmklandmarkselectordlg.h> 
00026 #include <tlmkitemiddbcombiinfo.h>
00027 #include <aknwaitdialog.h>
00028 #include <AknQueryDialog.h>
00029 #include <aknnotewrappers.h> 
00030 
00031 // USER INCLUDES
00032 #include "geoplaceselector.h"
00033 #include "geoprofile.h"
00034 #include "geoprofiles.hrh"
00035 
00036 // CONSTANT DEFINTION
00037 _LIT( KTab, "\t" );
00038 
00039 // ---------------------------------------------------------------------------
00040 // CGeoPlaceSelector::CGeoPlaceSelector
00041 // ---------------------------------------------------------------------------
00042 //
00043 CGeoPlaceSelector::CGeoPlaceSelector()
00044     :CActive( EPriorityStandard )
00045     {    
00046     }
00047     
00048 // ---------------------------------------------------------------------------
00049 // CGeoPlaceSelector::~CGeoPlaceSelector
00050 // ---------------------------------------------------------------------------
00051 // 
00052 CGeoPlaceSelector::~CGeoPlaceSelector()
00053     {
00054     // Cancel any outstanding request
00055     Cancel();
00056     
00057     iPositioner.Close();
00058     iServer.Close();
00059     
00060     delete iPlacePopupTitle;
00061     }
00062     
00063 // ---------------------------------------------------------------------------
00064 // CGeoPlaceSelector::NewL
00065 // ---------------------------------------------------------------------------
00066 // 
00067 CGeoPlaceSelector* CGeoPlaceSelector::NewLC()
00068     {
00069     CGeoPlaceSelector* self = new ( ELeave ) CGeoPlaceSelector();
00070     CleanupStack::PushL( self );
00071     self->ConstructL();
00072     return self;                                                                  
00073     }
00074     
00075 // ---------------------------------------------------------------------------
00076 // CGeoPlaceSelector::ConstructL
00077 // ---------------------------------------------------------------------------
00078 //
00079 void CGeoPlaceSelector::ConstructL()
00080     {
00081     // Allocate the Title text
00082     iPlacePopupTitle = NULL;
00083     
00084     iMapsAvailable = EFalse;
00085     
00086     User::LeaveIfError( iServer.Connect());
00087     User::LeaveIfError( iPositioner.Open( iServer ));
00088     
00089     CActiveScheduler::Add( this );
00090     }
00091 
00092 // ---------------------------------------------------------------------------
00093 // void CGeoPlaceSelector::SelectGeoPlaceL
00094 // ---------------------------------------------------------------------------
00095 //
00096 TInt CGeoPlaceSelector::SelectGeoPlaceL( TInt&            aPlaceType,
00097                                          TLocality&       aLocality,
00098                                          TPtr&            aPlaceName )
00099     {   
00100     TInt retVal = KErrCancel;
00101     retVal = DisplayPlacePopUpL();
00102     switch ( retVal )
00103         {
00104         case EPlaceCurrentLocation:
00105             {
00106             retVal = SelectPlaceFromCurrentLocationL( aLocality );
00107             aPlaceType = CGeoProfile::ECoordinate;
00108             break;    
00109             }
00110         case EPlaceNetworkCell:
00111             {
00112             break;              
00113             }
00114         case EPlaceLandmark:
00115             {
00116             retVal = SelectPlaceFromLandmarkL( aLocality, aPlaceName );
00117             aPlaceType = CGeoProfile::ECoordinate;
00118             }
00119         default:
00120             {
00121             break;
00122             }
00123         }
00124     
00125     if ( !retVal )
00126         {
00127         HBufC* title = StringLoader::LoadLC( R_LBL_GEOPROFILES_LOCATIONNAME );
00128         
00129         // Ask the User to enter a suitable name
00130         CAknTextQueryDialog* nameQuery = CAknTextQueryDialog::NewL( aPlaceName );
00131         retVal = !nameQuery->ExecuteLD( R_GEOPROFILES_NAME_QUERYDIALOG, title->Des());
00132         CleanupStack::PopAndDestroy( title );     
00133         }          
00134     return retVal;
00135     }
00136 
00137 // ---------------------------------------------------------------------------
00138 // void CGeoPlaceSelector::RunL
00139 // ---------------------------------------------------------------------------
00140 //
00141 void CGeoPlaceSelector::RunL()
00142     {
00143     iLocAcqState = iStatus.Int();
00144     DismissWaitDialog();
00145     }
00146 
00147 // ---------------------------------------------------------------------------
00148 // void CGeoPlaceSelector::DoCancel
00149 // ---------------------------------------------------------------------------
00150 //
00151 void CGeoPlaceSelector::DoCancel()
00152     {
00153     iPositioner.CancelRequest( EPositionerNotifyPositionUpdate );
00154     iLocAcqState = KErrNotFound;
00155     DismissWaitDialog();       
00156     }
00157 // ---------------------------------------------------------------------------
00158 // TInt CGeoPlaceSelector::DisplayPlacePopUp
00159 // ---------------------------------------------------------------------------
00160 //
00161 TInt CGeoPlaceSelector::DisplayPlacePopUpL()
00162     {
00163     TInt retVal = KErrCancel;
00164        
00165     // Index for profile selection
00166     TInt index = 0;
00167         
00168     // Load the Title text
00169     HBufC* title = StringLoader::LoadLC( R_LBL_GEOPROFILES_PLACE );
00170         
00171     // Create the list Query Dialog
00172     CAknListQueryDialog* placeQuery = new( ELeave ) CAknListQueryDialog( &index );
00173     placeQuery->PrepareLC( R_GEOPROFILES_LIST_QUERY );
00174           
00175     // Assign Title Text
00176     CAknPopupHeadingPane *  heading = placeQuery->QueryHeading();
00177     if( heading )
00178         {
00179         heading->SetTextL( *title );
00180         }
00181        
00182     // Descriptor array
00183     CDesCArrayFlat* array = new ( ELeave ) CDesCArrayFlat( 4 );
00184     CleanupStack::PushL( array );
00185     
00186     HBufC* placeName = HBufC::NewLC( 256 );
00187     TPtr placeNamePtr = placeName->Des();
00188    
00189     // Append the Current Place Label
00190     HBufC* currentplace = StringLoader::LoadLC( R_LBL_GEOPROFILES_CURRENTPLACE );
00191     placeNamePtr.Zero();
00192     placeNamePtr.Append( KTab );
00193     placeNamePtr.Append( currentplace->Des());
00194     array->AppendL( *placeName );    
00195     CleanupStack::PopAndDestroy( currentplace );   
00196     
00197     // Append the Landmark Label
00198     HBufC* landmark = StringLoader::LoadLC( R_LBL_GEOPROFILES_LANDMARKS );
00199     placeNamePtr.Zero();
00200     placeNamePtr.Append( KTab );
00201     placeNamePtr.Append( landmark->Des());
00202     array->AppendL( *placeName );
00203     CleanupStack::PopAndDestroy( landmark );
00204                 
00205     // Cleanup the profile Name buffer
00206     CleanupStack::PopAndDestroy( placeName );
00207     
00208     // Assign the Item text array
00209     placeQuery->SetItemTextArray( array );
00210     placeQuery->SetOwnershipType(  ELbmOwnsItemArray );
00211     CleanupStack::Pop( array );
00212       
00213     if ( placeQuery->RunLD())
00214         {
00215         if ( index == 3 && !iMapsAvailable )
00216             {
00217             retVal = index + 1;
00218             }
00219         else
00220             {
00221             retVal = index;    
00222             }        
00223         }
00224         
00225     CleanupStack::PopAndDestroy( title );
00226     return retVal;    
00227 
00228     }
00229 
00230 // ---------------------------------------------------------------------------
00231 // void CGeoPlaceSelector::SelectPlaceFromLandmarkL
00232 // ---------------------------------------------------------------------------
00233 //
00234 TInt CGeoPlaceSelector::SelectPlaceFromLandmarkL( TLocality&    aLocality,
00235                                                   TPtr&         aPlaceName )
00236     {
00237     TInt retVal = KErrCancel;
00238     
00239     TLmkItemIdDbCombiInfo lmkInfo;
00240     
00241     // Create a Landmarks object
00242     CLmkLandmarkSelectorDlg* dlg =  CLmkLandmarkSelectorDlg::NewL();
00243     if ( dlg->ExecuteLD( lmkInfo ) )
00244         {        
00245         CPosLandmarkDatabase* lmkDb = lmkInfo.GetLmDb();
00246         CleanupStack::PushL( lmkDb );
00247         CPosLandmark* lmk = lmkDb->ReadLandmarkLC( lmkInfo.GetItemId() );
00248         
00249         //retrieve lat long info
00250         TLocality locality;
00251         lmk->GetPosition( locality );
00252         if( !Math::IsNaN( locality.Latitude() ) && 
00253             !Math::IsNaN( locality.Longitude()))
00254             {
00255             aLocality = locality;
00256             
00257             // Store the name if it exists
00258             TPtrC landmarkName;
00259             lmk->GetLandmarkName( landmarkName );
00260             
00261             if ( landmarkName.Length() && landmarkName.Length() < KGeoPlaceNameMaxLength )
00262                 {
00263                 aPlaceName.Copy( landmarkName );
00264                 }
00265             retVal = KErrNone;
00266             }
00267         else
00268             {
00269             User::Leave( KErrNotFound );    
00270             }
00271         CleanupStack::PopAndDestroy( 2, lmkDb );       
00272         }
00273     return retVal;
00274     }
00275 
00276 // ---------------------------------------------------------------------------
00277 // void CGeoPlaceSelector::SelectPlaceFromCurrentLocationL
00278 // ---------------------------------------------------------------------------
00279 //
00280 TInt CGeoPlaceSelector::SelectPlaceFromCurrentLocationL( TLocality&     aLocality )
00281     {
00282     if ( IsActive())
00283         {
00284         User::Leave( KErrInUse );
00285         }
00286         
00287     // Initialize the server options
00288     _LIT(KRequestorName, "Geo Profiles ");    
00289         User::LeaveIfError( iPositioner.SetRequestor( 
00290                             CRequestor::ERequestorService,
00291                             CRequestor::EFormatApplication,
00292                             KRequestorName ));
00293                             
00294     // Issue a Location Request
00295     TPositionInfo posInfo;
00296     iPositioner.NotifyPositionUpdate( posInfo, iStatus );
00297     SetActive();    
00298     
00299     iLocAcqState = KErrNotFound;
00300     iDialogState = KErrNone;
00301     
00302     // Launch the Wait Dialog which will block the thread
00303     LaunchWaitDialogL();
00304     
00305     if( iLocAcqState )
00306         {
00307         // If Location Acquistion Failed, leave with the corresponding error code.
00308         User::Leave( iLocAcqState );
00309         }
00310         
00311     if ( !iDialogState )
00312         {
00313         // Now Copy the Location Information
00314         TPosition position;
00315         posInfo.GetPosition( position );
00316         
00317         if( !Math::IsNaN( position.Latitude() ) && 
00318             !Math::IsNaN( position.Longitude()))
00319             {
00320             aLocality = position;
00321             }
00322         else
00323             {
00324             User::Leave( KErrNotFound );   
00325             }                
00326         }                            
00327     return iDialogState;
00328     }
00329     
00330 // ---------------------------------------------------------------------------
00331 // void CGeoPlaceSelector::LaunchWaitDialogL
00332 // ---------------------------------------------------------------------------
00333 //     
00334 void CGeoPlaceSelector::LaunchWaitDialogL()
00335     {
00336     iWaitDialog = new ( ELeave ) CAknWaitDialog (( REINTERPRET_CAST( CEikDialog**, &iWaitDialog )), ETrue );          
00337     if( !( iWaitDialog->ExecuteLD( R_LBL_GEOPROFILES_WAITDLG_CURRENTLOCATION )))
00338         {
00339         iWaitDialog = NULL;
00340         iDialogState = KErrAbort;
00341         Cancel();    
00342         }
00343     iWaitDialog = NULL;
00344     }
00345 
00346 // ---------------------------------------------------------------------------
00347 // void CGeoPlaceSelector::DismissWaitDialogL
00348 // ---------------------------------------------------------------------------
00349 // 
00350 void CGeoPlaceSelector::DismissWaitDialog()
00351     {
00352     if ( iWaitDialog )
00353         {               
00354         TRAPD( err, iWaitDialog->ProcessFinishedL());
00355         if ( err )
00356                 {
00357                     delete iWaitDialog;                      
00358                 }
00359             iWaitDialog = NULL;
00360         } 
00361     }

© Nokia 2009

Back to top