mapnavproviderrefapp/src/mnrpgeocodingservice.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 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:  CMnrpGeocodingService class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 
       
    21 #include <aknnotewrappers.h>
       
    22 
       
    23 #include <lbsposition.h>
       
    24 
       
    25 #include <EPos_CPosLandmark.h>
       
    26 #include <epos_poslandmarkserialization.h>
       
    27 
       
    28 #include <mngeocodingservicebase.h>
       
    29 
       
    30 #include "debug.h"
       
    31 
       
    32 #include "mnrpappserverback.h"
       
    33 #include "mnrpappuiback.h"
       
    34 #include "mnrpdocumentback.h"
       
    35 #include "mnrpengine.h"
       
    36 
       
    37 #include <mnrefproviderback.rsg>
       
    38 #include "mnrpgeocodingservice.h"
       
    39 
       
    40 // ======== LOCAL FUNCTIONS ========
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 //  Cleanup item for RPointerArray<CPosLandmark>
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 void CleanupLandmarksArray( TAny* aArray )
       
    47     {
       
    48     ( static_cast<RPointerArray<CPosLandmark>*>( aArray ) )->ResetAndDestroy();
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 //  Cleanup item for moving application fore/background
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CleanupForeground( TAny* aAppUi )
       
    56     {
       
    57     ( static_cast<CMnrpAppUiBack*>( aAppUi ) )->MoveBackground();
       
    58     }
       
    59 
       
    60 // ======== MEMBER FUNCTIONS ========
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CMnrpGeocodingService::CMnrpGeocodingService( CMnrpEngine& aEngine )
       
    66  :  iEngine( aEngine )
       
    67     {
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CMnrpGeocodingService* CMnrpGeocodingService::NewL( CMnrpEngine& aEngine )
       
    74     {
       
    75     CMnrpGeocodingService* self = new (ELeave) CMnrpGeocodingService( aEngine );
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop( self );
       
    79     return self;
       
    80     }
       
    81     
       
    82 // ---------------------------------------------------------------------------
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 CMnrpGeocodingService::~CMnrpGeocodingService()
       
    86     {
       
    87     delete iProcessor;
       
    88     delete iLandmark;
       
    89     delete iPlainAddress;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 void CMnrpGeocodingService::ConstructL()
       
    96     {
       
    97     BaseConstructL();
       
    98     
       
    99     iProcessor = CIdle::NewL( CActive::EPriorityIdle );
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CMnrpGeocodingService::LeaveIfBusyL()
       
   106     {
       
   107     if ( iProcessor->IsActive() )
       
   108         {
       
   109         User::Leave( KErrInUse );
       
   110         }
       
   111     }
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CMnrpGeocodingService::StartProcessingL()
       
   117     {
       
   118     iProcessor->Start( TCallBack( CMnrpGeocodingService::GeocodingProcessingCallBack, this ) );
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 TInt CMnrpGeocodingService::GeocodingProcessingCallBack( TAny* aPtr )
       
   125     {
       
   126     CMnrpGeocodingService* self =    
       
   127         static_cast<CMnrpGeocodingService*> ( aPtr );
       
   128         
       
   129     self->HandleGeocodingCompleted();
       
   130     return EFalse;
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CMnrpGeocodingService::HandleGeocodingCompleted()
       
   137     {
       
   138     TInt err = KErrNone;
       
   139     TRAP( err, FinishProcessingL() );
       
   140     if ( err )
       
   141         {
       
   142         CompleteRequest( err );
       
   143         }
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CMnrpGeocodingService::FinishProcessingL()
       
   150     {
       
   151     LOG1("MnRefProvider: FinishProcessingL in, cursvc %d", iCurrentService);
       
   152 
       
   153     TLocality loc;
       
   154     CPosLandmark* lm = NULL;
       
   155     
       
   156     switch ( iCurrentService )
       
   157         {
       
   158         case ECoordToAddr:
       
   159                 
       
   160             if ( iDialogAllowed )
       
   161                 {
       
   162                 lm = LetUserSelectBestMatchL( *iLandmark );
       
   163                 CleanupStack::PushL( lm );
       
   164                 }
       
   165             else
       
   166                 {
       
   167                 // search and return just best match - closest POI
       
   168                 if ( iLandmark->GetPosition( loc ) != KErrNone )
       
   169                     {
       
   170                     User::Leave( KErrArgument );
       
   171                     }
       
   172                 lm = iEngine.CoordToAddressLC( loc );
       
   173                 }
       
   174             break;
       
   175 
       
   176         case EAddrToCoord:
       
   177             lm = iEngine.AddressToCoordLC( *iLandmark );
       
   178             break;
       
   179 
       
   180         case EPlainToCoord:
       
   181             lm = iEngine.AddressToCoordLC( *iPlainAddress );
       
   182             break;
       
   183             
       
   184         default:            
       
   185             User::Leave( KErrGeneral );
       
   186         }
       
   187 
       
   188     iCurrentService = ENone;    
       
   189     
       
   190     CompleteGeocodingRequestL( *lm );
       
   191     CleanupStack::PopAndDestroy( lm );
       
   192 
       
   193     LOG("MnRefProvider: FinishProcessingL out");
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 CPosLandmark* CMnrpGeocodingService::LetUserSelectBestMatchL( const CPosLandmark& aReference )
       
   200     {
       
   201     // 1. Find best matches
       
   202     
       
   203     RPointerArray<CPosLandmark> matches;
       
   204     CleanupStack::PushL( TCleanupItem( CleanupLandmarksArray, &matches ) );
       
   205     
       
   206     TLocality location;
       
   207     if ( aReference.GetPosition( location ) != KErrNone )
       
   208         {
       
   209         User::Leave( KErrArgument );
       
   210         }
       
   211     
       
   212     const TInt KMaxmMatches = 3;
       
   213     iEngine.BestCoordToAddressMatchesL( location, matches, KMaxmMatches );
       
   214     
       
   215     if ( matches.Count() < 1 )
       
   216         {
       
   217         User::Leave( KErrNotFound ); // cannot find any match
       
   218         }
       
   219     
       
   220     // 2. Start selection dialog
       
   221     
       
   222     TInt result = 1, selected = 0;
       
   223     if ( matches.Count() > 1 )
       
   224         {
       
   225         // prepare dialog
       
   226         const TInt KGranularity = 8;
       
   227         CDesCArraySeg *textArray = new (ELeave) CDesCArraySeg( KGranularity );
       
   228         CleanupStack::PushL( textArray );
       
   229         
       
   230         for ( TInt i = 0; i < matches.Count(); i++ )
       
   231             {
       
   232             HBufC* buf = HBufC::NewLC( KPosLmMaxTextFieldLength );
       
   233             TPtrC ptr( buf->Des() );
       
   234             
       
   235             matches[i]->GetPositionField( EPositionFieldLocationName, ptr );
       
   236             textArray->AppendL( ptr );
       
   237 
       
   238             CleanupStack::PopAndDestroy( buf );
       
   239             }
       
   240         
       
   241         // bring application foreground
       
   242         CMnrpAppUiBack* ui = (CMnrpAppUiBack*) CEikonEnv::Static()->AppUi();
       
   243         CleanupStack::PushL( TCleanupItem( CleanupForeground, ui ) );
       
   244         ui->MoveForeground();
       
   245 
       
   246         CAknListQueryDialog* dialog = new (ELeave) CAknListQueryDialog( &selected );
       
   247         dialog->PrepareLC( R_MNREFGEOCODER_LIST_QUERY );
       
   248 
       
   249         dialog->SetItemTextArray( textArray );
       
   250         dialog->SetOwnershipType( ELbmOwnsItemArray );
       
   251 
       
   252         // pop up dialog
       
   253         result = dialog->RunLD();
       
   254 
       
   255         CleanupStack::PopAndDestroy(); // move background
       
   256         CleanupStack::Pop( textArray );
       
   257         }
       
   258 
       
   259     // 3. Return result
       
   260     CPosLandmark* lm = NULL;
       
   261     if ( result )
       
   262         {
       
   263         lm = matches[selected];
       
   264         matches.Remove( selected );
       
   265         }
       
   266     else
       
   267         {
       
   268         User::Leave( KErrCancel ); 
       
   269         // user cancelled geocoding selection dialog
       
   270         // it automatically cancels geocoding request
       
   271         }
       
   272 
       
   273     CleanupStack::PopAndDestroy(); // matches
       
   274     return lm;
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // From class CMnGeocodingServiceBase
       
   279 // ---------------------------------------------------------------------------
       
   280 //
       
   281 void CMnrpGeocodingService::HandleFindAddressByCoordinateL(
       
   282     const CPosLandmark& aLandmark)
       
   283     {
       
   284     LeaveIfBusyL();
       
   285 
       
   286     delete iLandmark;
       
   287     iLandmark = NULL;
       
   288     iLandmark = CPosLandmark::NewL(aLandmark);
       
   289     
       
   290     iCurrentService = ECoordToAddr;
       
   291     iDialogAllowed = !( Options() & CMnGeocoder::EOptionDialogDisabled );
       
   292     StartProcessingL();
       
   293     }
       
   294         
       
   295 // ---------------------------------------------------------------------------
       
   296 // From class CMnGeocodingServiceBase
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void CMnrpGeocodingService::HandleFindCoordinateByAddressL(
       
   300     const CPosLandmark& aLandmark)
       
   301     {
       
   302     LeaveIfBusyL();
       
   303 
       
   304     delete iLandmark;
       
   305     iLandmark = NULL;
       
   306     iLandmark = CPosLandmark::NewL( aLandmark );
       
   307     
       
   308     iCurrentService = EAddrToCoord;
       
   309     iDialogAllowed = !( Options() & CMnGeocoder::EOptionDialogDisabled );
       
   310     StartProcessingL();
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // From class CMnGeocodingServiceBase
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 void CMnrpGeocodingService::HandleFindCoordinateByAddressL(
       
   318     const TDesC& aAddress)
       
   319     {
       
   320     LeaveIfBusyL();
       
   321 
       
   322     delete iPlainAddress;
       
   323     iPlainAddress = NULL;
       
   324     iPlainAddress = aAddress.AllocL();
       
   325     
       
   326     delete iLandmark;
       
   327     iLandmark = NULL;
       
   328     iLandmark = CPosLandmark::NewL();
       
   329     
       
   330     iCurrentService = EPlainToCoord;
       
   331     iDialogAllowed = !( Options() & CMnGeocoder::EOptionDialogDisabled );
       
   332     StartProcessingL();
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // From class CMnServiceBase
       
   337 // ---------------------------------------------------------------------------
       
   338 //
       
   339 void CMnrpGeocodingService::DoCancel()
       
   340     {
       
   341     iProcessor->Cancel();
       
   342     iCurrentService = ENone;
       
   343     }