locationmanager/ReverseGeocode/src/internalreversegeocode.cpp
branchRCL_3
changeset 57 2872ae438bf7
parent 53 29d87345eaeb
child 63 e538444823de
equal deleted inserted replaced
53:29d87345eaeb 57:2872ae438bf7
     1 /*
       
     2 * Copyright (c) 2006-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: Implementation of reversegeocodea achieved using HTTP get
       
    15 * request to the Maps server.
       
    16 */
       
    17 
       
    18 #include "internalreversegeocode.h"
       
    19 #include "internaladdressinfo.h"
       
    20 #include "locationmanagerdebug.h"
       
    21 
       
    22 
       
    23 //Maximum length for the response buffer size
       
    24 const int KMaxResponseLength = 2048;
       
    25 
       
    26 //Language option for the REST request
       
    27 _LIT8( KDefaultLanguage, "eng" );
       
    28 
       
    29 
       
    30 //Format of the HTTP request for the reverse geocode
       
    31 _LIT8( KRequestFormat, "http://loc.mobile.maps.svc.ovi.com/geocoder/rgc/1.0?n=10&lat=%f&long=%f&lg=%S&output=xml" );
       
    32 
       
    33 
       
    34 // http://www.loc.gov/marc/languages/
       
    35 // These are the nokia language id converted to MARC language strings.
       
    36  static const char Marc_Table[ 104 ][ 4 ] = {
       
    37         "   ", // dummy
       
    38         "ENG", // 1 English
       
    39         "FRE", // 2 French
       
    40         "GER", // 3 German
       
    41         "SPA", // 4 Spanish
       
    42         "ITA", // 5 Italian
       
    43         "SWE", // 6 Swedish
       
    44         "DAN", // 7 Danish
       
    45         "NOR", // 8 Norwegian
       
    46         "FIN", // 9 Finnish
       
    47         "010", // 10 American
       
    48         "011", // 11 Swiss French
       
    49         "GSW", // 12 Swiss German
       
    50         "POR", // 13 Portuguese
       
    51         "TUR", // 14 Turkish
       
    52         "ICE", // 15 Icelandic
       
    53         "RUS", // 16 Russian
       
    54         "HUN", // 17 Hungarian
       
    55         "DUT", // 18 Dutch
       
    56         "019", // 19 Flemish
       
    57         "020", // 20 Australian English
       
    58         "021", // 21 Belgian French
       
    59         "022", // 22 Austrian German
       
    60         "023", // 23 New Zealand English
       
    61         "FRE", // 24 International French
       
    62         "CZE", // 25 Czech
       
    63         "SLO", // 26 Slovak
       
    64         "POL", // 27 Polish
       
    65         "SLV", // 28 Slovenian
       
    66         "029", // 29 TaiwanChinese
       
    67         "CHT", // 30 HongKongChinese
       
    68         "CHI", // 31 PeoplesRepublicOfChina Chinese
       
    69         "JPN", // 32 Japanese
       
    70         "THA", // 33 Thai
       
    71         "AFR", // 34 Afrikaans
       
    72         "ALB", // 35 Albanian
       
    73         "AMH", // 36 Amharic
       
    74         "ARA", // 37 Arabic
       
    75         "ARM", // 38 Armenian
       
    76         "TGL", // 39 Tagalog
       
    77         "BEL", // 40 Belarusian
       
    78         "BEN", // 41 Bengali
       
    79         "BUL", // 42 Bulgarian
       
    80         "BUR", // 43 Burmese
       
    81         "CAT", // 44 Catalan
       
    82         "SCR", // 45 Croatian
       
    83         "046", // 46 Canadian English
       
    84         "ENG", // 47 International English
       
    85         "048", // 48 SouthAfrican English
       
    86         "EST", // 49 Estonian
       
    87         "PER", // 50 Persian (Farsi)
       
    88         "051", // 51 Canadian French
       
    89         "GAE", // 52 Scots Gaelic
       
    90         "GEO", // 53 Georgian
       
    91         "GRE", // 54 Greek
       
    92         "055", // 55 Cyprus Greek
       
    93         "GUJ", // 56 Gujarati
       
    94         "HEB", // 57 Hebrew
       
    95         "HIN", // 58 Hindi
       
    96         "IND", // 59 Bahasa indonesia
       
    97         "GLE", // 60 Irish
       
    98         "061", // 61 Swiss Italian
       
    99         "KAN", // 62 Kannada
       
   100         "KAZ", // 63 Kazakh
       
   101         "KHM", // 64 Khmer
       
   102         "KOR", // 65 Korean
       
   103         "LAO", // 66 Lao
       
   104         "LAV", // 67 Latvian
       
   105         "LIT", // 68 Lithuanian
       
   106         "MAC", // 69 Macedonian
       
   107         "070", // 70 Bahasa Malaysia
       
   108         "MAL", // 71 Malayalam
       
   109         "MAR", // 72 Marathi
       
   110         "MOL", // 73 Moldavian
       
   111         "MON", // 74 Mongolian
       
   112         "NNO", // 75 Norwegian Nynorsk
       
   113         "076", // 76 Brazilian Portuguese
       
   114         "PAN", // 77 Punjabi
       
   115         "RUM", // 78 Romanian
       
   116         "SCC", // 79 Serbian
       
   117         "SNH", // 80 Sinhalese
       
   118         "SOM", // 81 Somali
       
   119         "082", // 82 International Spanish
       
   120         "083", // 83 LatinAmerican Spanish
       
   121         "SWA", // 84 Swahili
       
   122         "085", // 85 Finland Swedish
       
   123         "TAJ", // 86 Tajik
       
   124         "TAM", // 87 Tamil
       
   125         "TEL", // 88 Telugu
       
   126         "TIB", // 89 Tibetan
       
   127         "TIR", // 90 Tigrinya
       
   128         "091", // 91 Cyprus Turkish
       
   129         "TUK", // 92 Turkmen
       
   130         "UKR", // 93 Ukrainian
       
   131         "URD", // 94 Urdu
       
   132         "UZB", // 95 Uzbek
       
   133         "VIE", // 96 Vietnamese
       
   134         "WEL", // 97 Welsh
       
   135         "ZUL", // 98 Zulu
       
   136         "UND", // 99 Other
       
   137         "UND", // 100 Undef
       
   138         "UND", // 101 Undef
       
   139         "BAQ", // 102 Basque
       
   140         "103", // 103 Galician
       
   141     };
       
   142 
       
   143  // Timer interval
       
   144  const TInt KInterval = 15000000;  // 15 seconds
       
   145 
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // CInternalReverseGeocode::NewL()
       
   149 // Factory function to create the instance
       
   150 // ----------------------------------------------------------------------------
       
   151 CInternalReverseGeocode* CInternalReverseGeocode::NewL( MReverseGeocodeObserver& aObserver )
       
   152     {
       
   153         LOG( "CInternalReverseGeocode::NewL,begin" );
       
   154         CInternalReverseGeocode *self = new (ELeave) CInternalReverseGeocode( aObserver );
       
   155         CleanupStack::PushL( self );
       
   156         self->ConstructL();
       
   157         
       
   158         CleanupStack::Pop( self );
       
   159         return self;
       
   160     }
       
   161 
       
   162 
       
   163 // ----------------------------------------------------------------------------
       
   164 // CInternalReverseGeocode::CInternalReverseGeocode()
       
   165 // Constructor
       
   166 // ---------------------------------------------------------------------------- 
       
   167 CInternalReverseGeocode::CInternalReverseGeocode( MReverseGeocodeObserver& aObserver ):
       
   168                             iXmlHandler ( NULL ),
       
   169                             iClientEngine ( NULL ),
       
   170                             iXMLBuf ( NULL ),
       
   171                             iObserver( aObserver ),
       
   172                             iTimer( NULL ),
       
   173                             iStartTimerFlag(EFalse)
       
   174     {
       
   175     }
       
   176 
       
   177 
       
   178 // ----------------------------------------------------------------------------
       
   179 // CInternalReverseGeocode::~CInternalReverseGeocode()
       
   180 // Destructor
       
   181 // ---------------------------------------------------------------------------- 
       
   182 CInternalReverseGeocode::~CInternalReverseGeocode()
       
   183     {
       
   184     LOG( "CInternalReverseGeocode::~CInternalReverseGeocode,begin" );
       
   185     if ( iTimer)
       
   186         {
       
   187         iTimer->Cancel();
       
   188         delete iTimer;
       
   189         iTimer = NULL;
       
   190         }
       
   191     delete iXMLBuf;
       
   192     iXMLBuf = NULL;
       
   193     delete iXmlHandler;
       
   194     iXmlHandler = NULL;
       
   195     delete iClientEngine;
       
   196     iClientEngine = NULL;
       
   197     delete iAddressInfo;
       
   198     iAddressInfo = NULL;
       
   199     
       
   200     LOG( "CInternalReverseGeocode::~CInternalReverseGeocode,end" );  
       
   201     }
       
   202 
       
   203 
       
   204 // ----------------------------------------------------------------------------
       
   205 // CInternalReverseGeocode::GetAddressByCoordinateL()
       
   206 // Gets the address for the given geo-coordinaates.
       
   207 // ----------------------------------------------------------------------------
       
   208 void CInternalReverseGeocode::GetAddressByCoordinateL( TLocality aLocality, 
       
   209                                                       const TConnectionOption aOption )
       
   210     {
       
   211     LOG("CInternalReverseGeocode::GetAddressByCoordinateL ,begin");
       
   212     TReal64 latitude = aLocality.Latitude();
       
   213     TReal64 longitude = aLocality.Longitude();
       
   214 
       
   215     GetLanguageForTheRequest( iLang );
       
   216         
       
   217     //Form the request URI
       
   218     iQueryString.Format( KRequestFormat, latitude, longitude, &iLang );
       
   219     TInt err = KErrNone;
       
   220     TRAP(err, iClientEngine->IssueHTTPGetL( iQueryString, aOption ));
       
   221         
       
   222     if ( iTimer && iTimer->IsActive() )
       
   223         {
       
   224         iTimer->Cancel();
       
   225         }
       
   226     if(err == KErrNone || err == KErrNotSupported)
       
   227         {
       
   228         // connection is closed because of data usage is set to manual
       
   229         iStartTimerFlag = EFalse;
       
   230         }
       
   231     LOG("CInternalReverseGeocode::GetAddressByCoordinateL ,end");
       
   232     }
       
   233 
       
   234 
       
   235 // ----------------------------------------------------------------------------
       
   236 // CInternalReverseGeocode::ConstructL()
       
   237 // second phase construction.
       
   238 // ---------------------------------------------------------------------------- 
       
   239 void CInternalReverseGeocode::ConstructL()
       
   240     {
       
   241     LOG( "CInternalReverseGeocode::ConstructL,begin" );
       
   242     iXMLBuf = HBufC8::NewL( KMaxResponseLength );
       
   243     
       
   244     //Address Info
       
   245     iAddressInfo = CInternalAddressInfo::NewL();
       
   246 
       
   247     iXmlHandler = CXmlHandler::NewL( *this, iAddressInfo );
       
   248         
       
   249     iClientEngine = CClientEngine::NewL( *this );
       
   250         
       
   251     iTimer = CConnectionTimerHandler::NewL(*this);
       
   252 
       
   253     LOG( "CInternalReverseGeocode::ConstructL,end" );		
       
   254     }
       
   255 
       
   256 
       
   257 // ----------------------------------------------------------------------------
       
   258 // CInternalReverseGeocode::OnParseCompletedL()
       
   259 // callback which notifys the completion of parsing.
       
   260 // ----------------------------------------------------------------------------
       
   261 void CInternalReverseGeocode::OnParseCompletedL( TInt aError, MAddressInfo& aLocationInfo )
       
   262     {
       
   263     LOG( "CInternalReverseGeocode::OnParseCompletedL,begin" );
       
   264     ARG_USED(aLocationInfo);
       
   265     iStartTimerFlag = ETrue;
       
   266     iObserver.ReverseGeocodeComplete( aError, *iAddressInfo );
       
   267     if(iStartTimerFlag)
       
   268         {
       
   269         StartTimer();
       
   270         }
       
   271     LOG( "CInternalReverseGeocode::OnParseCompletedL,end" );
       
   272     }
       
   273 
       
   274 // ----------------------------------------------------------------------------
       
   275 // CInternalReverseGeocode::ClientEvent()
       
   276 // callback which notifys progess of HTTP request
       
   277 // ---------------------------------------------------------------------------- 
       
   278 void CInternalReverseGeocode::ClientEvent( const THttpStatus& aEvent )
       
   279     {
       
   280     LOG1( "CInternalReverseGeocode::ClientEvent,begin. Err - %d", aEvent);
       
   281     TInt err = KErrNone;
       
   282     //Have a Switch here
       
   283     switch( aEvent )
       
   284         {
       
   285         case EHttpTxCompleted:
       
   286             //Reading is done
       
   287             //Parse the String and get the restults
       
   288             if( iXmlHandler )
       
   289             {
       
   290              TRAP_IGNORE( iXmlHandler->StartParsingL( iXMLBuf ) );
       
   291             }
       
   292             break;
       
   293         //All these cases will in turn lead to
       
   294         //generic failure due to connection/Tx related problems    
       
   295         case EHttpConnectionFailure:
       
   296             // May happen if Socket connection fails
       
   297             err = KErrCouldNotConnect;
       
   298             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   299             break;
       
   300         case EHttpTxFailed:
       
   301         case EHttpMhfRunError:
       
   302             // May happen if Socket connection fails
       
   303             // Complete the RGC with generic error.
       
   304             err = KErrGeneral;
       
   305             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   306             break;
       
   307         case EHttpTxCancelled:
       
   308             //On Cancellation of request.
       
   309             err = KErrCancel;
       
   310             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   311             break;
       
   312         case EHttpAuthFailed:
       
   313             //On Cancellation of request.
       
   314             err = KErrPermissionDenied;
       
   315             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   316             break;
       
   317         }
       
   318     if(err != KErrNone)
       
   319         {
       
   320         LOG("Error occur while getting data.");
       
   321         StartTimer();
       
   322         }
       
   323 	LOG( "CInternalReverseGeocode::ClientEvent,end" );
       
   324     }
       
   325 
       
   326 
       
   327 // ----------------------------------------------------------------------------
       
   328 // CInternalReverseGeocode::ClientBodyReceived()
       
   329 // callback through which the HTTP body data is recieved. 
       
   330 // ---------------------------------------------------------------------------- 
       
   331 void CInternalReverseGeocode::ClientBodyReceived( const TDesC8& aBodyData )
       
   332     {
       
   333     LOG( "CInternalReverseGeocode::ClientBodyReceived" );
       
   334     //Dump the contents here
       
   335     TPtr8 ptr = iXMLBuf->Des();
       
   336     ptr.Zero();
       
   337     ptr.Append( aBodyData );
       
   338     }
       
   339 
       
   340 
       
   341 // ----------------------------------------------------------------------------
       
   342 // CInternalReverseGeocode::GetLanguageForTheRequest()
       
   343 // Gets the appropriate language based on the Phone language setting 
       
   344 // ----------------------------------------------------------------------------
       
   345 void CInternalReverseGeocode::GetLanguageForTheRequest( TDes8& aLanguage )
       
   346     {
       
   347      LOG( "CInternalReverseGeocode::GetLanguageForTheRequest,begin" );    
       
   348 	//get the current phone langauge  
       
   349 	TInt phoneLangIndex = User::Language();
       
   350 
       
   351 	// Get the converted language 
       
   352 	if (  phoneLangIndex < sizeof( Marc_Table ) / sizeof( Marc_Table[ 0 ]  ) )
       
   353 	    {
       
   354 	    aLanguage = (const TUint8*) Marc_Table[ phoneLangIndex ];
       
   355 	    }
       
   356 	else
       
   357 	    {
       
   358 	     //By default language will be Eng
       
   359 	    aLanguage.Copy( KDefaultLanguage );
       
   360 	    }
       
   361 	LOG( "CInternalReverseGeocode::GetLanguageForTheRequest,begin" );
       
   362     }
       
   363 
       
   364 
       
   365 // ----------------------------------------------------------------------------
       
   366 // CInternalReverseGeocode::CloseConnection()
       
   367 // Closes the http connection and notifies the observer
       
   368 // ----------------------------------------------------------------------------
       
   369 void CInternalReverseGeocode::CloseConnection()
       
   370     {
       
   371     LOG( "CInternalReverseGeocode::CloseConnection ,begin" );
       
   372     if(iClientEngine)
       
   373         {
       
   374         iClientEngine->CloseConnection();
       
   375         LOG( "Connection closed\n" );
       
   376         TInt err = KErrCouldNotConnect;
       
   377         iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   378         }
       
   379 	LOG( "CInternalReverseGeocode::CloseConnection,end" );
       
   380     }
       
   381 
       
   382 // ----------------------------------------------------------------------------
       
   383 // CInternalReverseGeocode::StartTimer()
       
   384 // starts the timer
       
   385 // ----------------------------------------------------------------------------
       
   386 void CInternalReverseGeocode::StartTimer()
       
   387     {
       
   388     LOG( "CInternalReverseGeocode::StartTimer ,begin" );
       
   389     if(iTimer)
       
   390         {
       
   391         iTimer->StartTimer( KInterval);
       
   392         LOG( "Timer started" );
       
   393         }
       
   394     LOG( "CInternalReverseGeocode::StartTimer,end" );
       
   395     }
       
   396 
       
   397 // ----------------------------------------------------------------------------
       
   398 // CInternalReverseGeocode::HandleTimedoutEvent()
       
   399 // Closes the connection once it times out
       
   400 // ----------------------------------------------------------------------------
       
   401 void CInternalReverseGeocode::HandleTimedoutEvent(TInt aErrorCode)
       
   402     {
       
   403     LOG( "CInternalReverseGeocode::HandleTimedoutEvent" );
       
   404     ARG_USED(aErrorCode);
       
   405     CloseConnection();
       
   406     }
       
   407 
       
   408 // ----------------------------------------------------------------------------
       
   409 // CInternalReverseGeocode::SilentConnectionAllowed()
       
   410 // Checks if a silent connection is allowed
       
   411 // ---------------------------------------------------------------------------- 
       
   412 TBool CInternalReverseGeocode::SilentConnectionAllowed()
       
   413     {
       
   414     LOG( "CInternalReverseGeocode::SilentConnectionAllowed ,begin" );
       
   415     TBool retVal = EFalse;
       
   416     if(iClientEngine)
       
   417         {
       
   418         retVal = iClientEngine->SilentConnectionAllowed();
       
   419         }
       
   420     LOG1("Silent connection allowed ,end- %d", (TInt)retVal);
       
   421     return retVal;
       
   422     }
       
   423 
       
   424 // ----------------------------------------------------------------------------
       
   425 // CInternalReverseGeocode::GetCurrentRegisterNw()
       
   426 // ---------------------------------------------------------------------------- 
       
   427 RMobilePhone::TMobilePhoneNetworkInfoV2& CInternalReverseGeocode::GetCurrentRegisterNw()
       
   428     {
       
   429     LOG( "CInternalReverseGeocode::GetCurrentRegisterNw ,begin" );
       
   430     return iObserver.GetCurrentRegisterNw();
       
   431     }
       
   432 
       
   433 // ----------------------------------------------------------------------------
       
   434 // CInternalReverseGeocode::IsRegisteredAtHomeNetwork()
       
   435 // ---------------------------------------------------------------------------- 
       
   436 TBool CInternalReverseGeocode::IsRegisteredAtHomeNetwork()
       
   437     {
       
   438     LOG( "CInternalReverseGeocode::IsRegisteredAtHomeNetwork" );
       
   439     return iObserver.IsRegisteredAtHomeNetwork();
       
   440     }
       
   441 
       
   442 // ----------------------------------------------------------------------------
       
   443 // CInternalReverseGeocode::GetHomeNetworkInfo()
       
   444 // ----------------------------------------------------------------------------
       
   445 const RMobilePhone::TMobilePhoneNetworkInfoV1& 
       
   446         CInternalReverseGeocode::GetHomeNetworkInfo(TBool& aHomeNwInfoAvailableFlag)
       
   447     {
       
   448     LOG( "CInternalReverseGeocode::GetHomeNetworkInfo" );
       
   449     return iObserver.GetHomeNetworkInfo(aHomeNwInfoAvailableFlag);
       
   450     }
       
   451 
       
   452 
       
   453 //end of file
       
   454