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