locationmanager/ReverseGeocode/src/internalreversegeocode.cpp
changeset 36 aa5a574040a4
child 40 910a23996aa0
child 45 a93990e5815e
equal deleted inserted replaced
35:f727727520eb 36:aa5a574040a4
       
     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     delete iXmlHandler;
       
   198     delete iClientEngine;
       
   199     delete iAddressInfo;
       
   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     iAuthCode.Copy( KAuthenticationToken );
       
   216     iRefURL.Copy( KRefererURL );
       
   217         
       
   218 
       
   219     GetLanguageForTheRequest( iLang );
       
   220         
       
   221     //Form the request URI
       
   222     iQueryString.Format( KRequestFormat, &iAuthCode, &iRefURL, latitude, longitude, &iLang );
       
   223     TInt err = KErrNone;
       
   224     TRAP(err, iClientEngine->IssueHTTPGetL( iQueryString, aOption ));
       
   225         
       
   226     if ( iTimer && iTimer->IsActive() )
       
   227         {
       
   228         iTimer->Cancel();
       
   229         }
       
   230     if(err == KErrNone || err == KErrNotSupported)
       
   231         {
       
   232         // connection is closed because of data usage is set to manual
       
   233         iStartTimerFlag = EFalse;
       
   234         }
       
   235     LOG("CInternalReverseGeocode::GetAddressByCoordinateL ,end");
       
   236     }
       
   237 
       
   238 
       
   239 // ----------------------------------------------------------------------------
       
   240 // CInternalReverseGeocode::ConstructL()
       
   241 // second phase construction.
       
   242 // ---------------------------------------------------------------------------- 
       
   243 void CInternalReverseGeocode::ConstructL()
       
   244     {
       
   245     LOG( "CInternalReverseGeocode::ConstructL,begin" );
       
   246     iXMLBuf = HBufC8::NewL( KMaxResponseLength );
       
   247     
       
   248     //Address Info
       
   249     iAddressInfo = CInternalAddressInfo::NewL();
       
   250 
       
   251     iXmlHandler = CXmlHandler::NewL( *this, iAddressInfo );
       
   252         
       
   253     iClientEngine = CClientEngine::NewL( *this );
       
   254         
       
   255     iTimer = CConnectionTimerHandler::NewL(*this);
       
   256 
       
   257     LOG( "CInternalReverseGeocode::ConstructL,end" );		
       
   258     }
       
   259 
       
   260 
       
   261 // ----------------------------------------------------------------------------
       
   262 // CInternalReverseGeocode::OnParseCompletedL()
       
   263 // callback which notifys the completion of parsing.
       
   264 // ----------------------------------------------------------------------------
       
   265 void CInternalReverseGeocode::OnParseCompletedL( TInt aError, MAddressInfo& aLocationInfo )
       
   266     {
       
   267     LOG( "CInternalReverseGeocode::OnParseCompletedL,begin" );
       
   268     ARG_USED(aLocationInfo);
       
   269     iStartTimerFlag = ETrue;
       
   270     iObserver.ReverseGeocodeComplete( aError, *iAddressInfo );
       
   271     if(iStartTimerFlag)
       
   272         {
       
   273         StartTimer();
       
   274         }
       
   275     LOG( "CInternalReverseGeocode::OnParseCompletedL,end" );
       
   276     }
       
   277 
       
   278 // ----------------------------------------------------------------------------
       
   279 // CInternalReverseGeocode::ClientEvent()
       
   280 // callback which notifys progess of HTTP request
       
   281 // ---------------------------------------------------------------------------- 
       
   282 void CInternalReverseGeocode::ClientEvent( const THttpStatus& aEvent )
       
   283     {
       
   284     LOG( "CInternalReverseGeocode::ClientEvent,begin" );
       
   285     TInt err;
       
   286     //Have a Switch here
       
   287     switch( aEvent )
       
   288         {
       
   289         case EHttpTxCompleted:
       
   290             //Reading is done
       
   291             //Parse the String and get the restults
       
   292             if( iXmlHandler )
       
   293             {
       
   294              TRAP_IGNORE( iXmlHandler->StartParsingL( iXMLBuf ) );
       
   295             }
       
   296             break;
       
   297         //All these cases will in turn lead to
       
   298         //generic failure due to connection/Tx related problems    
       
   299         case EHttpConnectionFailure:
       
   300             // May happen if Socket connection fails
       
   301             err = KErrCouldNotConnect;
       
   302             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   303             break;
       
   304         case EHttpTxFailed:
       
   305         case EHttpMhfRunError:
       
   306             // May happen if Socket connection fails
       
   307             // Complete the RGC with generic error.
       
   308             err = KErrGeneral;
       
   309             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   310             break;
       
   311         case EHttpTxCancelled:
       
   312             //On Cancellation of request.
       
   313             err = KErrCancel;
       
   314             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   315             break;
       
   316         case EHttpAuthFailed:
       
   317             //On Cancellation of request.
       
   318             err = KErrPermissionDenied;
       
   319             iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   320             break;
       
   321         }
       
   322 	LOG( "CInternalReverseGeocode::ClientEvent,end" );
       
   323     }
       
   324 
       
   325 
       
   326 // ----------------------------------------------------------------------------
       
   327 // CInternalReverseGeocode::ClientBodyReceived()
       
   328 // callback through which the HTTP body data is recieved. 
       
   329 // ---------------------------------------------------------------------------- 
       
   330 void CInternalReverseGeocode::ClientBodyReceived( const TDesC8& aBodyData )
       
   331     {
       
   332     LOG( "CInternalReverseGeocode::ClientBodyReceived" );
       
   333     //Dump the contents here
       
   334     TPtr8 ptr = iXMLBuf->Des();
       
   335     ptr.Zero();
       
   336     ptr.Append( aBodyData );
       
   337     }
       
   338 
       
   339 
       
   340 // ----------------------------------------------------------------------------
       
   341 // CInternalReverseGeocode::GetLanguageForTheRequest()
       
   342 // Gets the appropriate language based on the Phone language setting 
       
   343 // ----------------------------------------------------------------------------
       
   344 void CInternalReverseGeocode::GetLanguageForTheRequest( TDes8& aLanguage )
       
   345     {
       
   346      LOG( "CInternalReverseGeocode::GetLanguageForTheRequest,begin" );    
       
   347 	//get the current phone langauge  
       
   348 	TInt phoneLangIndex = User::Language();
       
   349 
       
   350 	// Get the converted language 
       
   351 	if (  phoneLangIndex < sizeof( Marc_Table ) / sizeof( Marc_Table[ 0 ]  ) )
       
   352 	    {
       
   353 	    aLanguage = (const TUint8*) Marc_Table[ phoneLangIndex ];
       
   354 	    }
       
   355 	else
       
   356 	    {
       
   357 	     //By default language will be Eng
       
   358 	    aLanguage.Copy( KDefaultLanguage );
       
   359 	    }
       
   360 	LOG( "CInternalReverseGeocode::GetLanguageForTheRequest,begin" );
       
   361     }
       
   362 
       
   363 
       
   364 // ----------------------------------------------------------------------------
       
   365 // CInternalReverseGeocode::CloseConnection()
       
   366 // Closes the http connection and notifies the observer
       
   367 // ----------------------------------------------------------------------------
       
   368 void CInternalReverseGeocode::CloseConnection()
       
   369     {
       
   370     LOG( "CInternalReverseGeocode::CloseConnection ,begin" );
       
   371     if(iClientEngine)
       
   372         {
       
   373         iClientEngine->CloseConnection();
       
   374         LOG( "Connection closed\n" );
       
   375         TInt err = KErrCouldNotConnect;
       
   376         iObserver.ReverseGeocodeComplete( err , *iAddressInfo );
       
   377         }
       
   378 	LOG( "CInternalReverseGeocode::CloseConnection,end" );
       
   379     }
       
   380 
       
   381 // ----------------------------------------------------------------------------
       
   382 // CInternalReverseGeocode::StartTimer()
       
   383 // starts the timer
       
   384 // ----------------------------------------------------------------------------
       
   385 void CInternalReverseGeocode::StartTimer()
       
   386     {
       
   387     LOG( "CInternalReverseGeocode::StartTimer ,begin" );
       
   388     if(iTimer)
       
   389         {
       
   390         iTimer->StartTimer( KInterval);
       
   391         LOG( "Timer started" );
       
   392         }
       
   393     LOG( "CInternalReverseGeocode::StartTimer,end" );
       
   394     }
       
   395 
       
   396 // ----------------------------------------------------------------------------
       
   397 // CInternalReverseGeocode::HandleTimedoutEvent()
       
   398 // Closes the connection once it times out
       
   399 // ----------------------------------------------------------------------------
       
   400 void CInternalReverseGeocode::HandleTimedoutEvent(TInt aErrorCode)
       
   401     {
       
   402     LOG( "CInternalReverseGeocode::HandleTimedoutEvent" );
       
   403     ARG_USED(aErrorCode);
       
   404     CloseConnection();
       
   405     }
       
   406 
       
   407 // ----------------------------------------------------------------------------
       
   408 // CInternalReverseGeocode::SilentConnectionAllowed()
       
   409 // Checks if a silent connection is allowed
       
   410 // ---------------------------------------------------------------------------- 
       
   411 TBool CInternalReverseGeocode::SilentConnectionAllowed()
       
   412     {
       
   413     LOG( "CInternalReverseGeocode::SilentConnectionAllowed ,begin" );
       
   414     TBool retVal = EFalse;
       
   415     if(iClientEngine)
       
   416         {
       
   417         retVal = iClientEngine->SilentConnectionAllowed();
       
   418         }
       
   419     LOG1("Silent connection allowed ,end- %d", (TInt)retVal);
       
   420     return retVal;
       
   421     }
       
   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