locationsystemui/locationsysui/posindicator/posreversegeocodeplugin/src/posrevgeocodeplugin.cpp
branchRCL_3
changeset 44 2b4ea9893b66
equal deleted inserted replaced
42:02ba3f1733c6 44:2b4ea9893b66
       
     1 /*
       
     2 * Copyright (c) 2010 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 CPosRevGeoCodePlugin class.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "posrevgeocodeplugin.h"
       
    19 #include "posrevgeocodelogger.h"
       
    20 
       
    21 #include <lbsposition.h>
       
    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, "88dedc61fb43c924aa39a873dadd41f4" );
       
    28 //Static RefererURL which shows that request has been made from this app
       
    29 _LIT8( KRefererURL, "nokia_positioningIndicator" );
       
    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 KTimeOutInterval = 60000000;  // 60 seconds
       
   150 
       
   151 
       
   152 // ECOM implementation specifics
       
   153 static const TImplementationProxy implTable[] =
       
   154     {
       
   155     IMPLEMENTATION_PROXY_ENTRY( 0x2002E693, CPosRevGeoCodePlugin::NewL )
       
   156     }; 
       
   157 
       
   158 
       
   159 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   160     {
       
   161     aTableCount = sizeof(implTable) / sizeof(TImplementationProxy);
       
   162 
       
   163     return implTable;
       
   164     }
       
   165 
       
   166 //------------------------------------------------------------------------------
       
   167 // CPosRevGeoCodePlugin::NewL
       
   168 //------------------------------------------------------------------------------
       
   169 CPosRevGeoCodePlugin* CPosRevGeoCodePlugin::NewL( MPosReverseGeocodeObserver& aObserver )
       
   170     {
       
   171     FUNC("CPosRevGeoCodePlugin::NewL");
       
   172     CPosRevGeoCodePlugin *self = new (ELeave) CPosRevGeoCodePlugin( aObserver );
       
   173     CleanupStack::PushL( self );
       
   174     self->ConstructL();
       
   175     CleanupStack::Pop( self );
       
   176     return self;
       
   177     }
       
   178 
       
   179 //------------------------------------------------------------------------------
       
   180 // CPosRevGeoCodePlugin::CPosRevGeoCodePlugin
       
   181 //------------------------------------------------------------------------------
       
   182 CPosRevGeoCodePlugin::CPosRevGeoCodePlugin( MPosReverseGeocodeObserver& aObserver ):
       
   183 	 																					iObserver( aObserver ),
       
   184 	 																					iHTTPClientEngine ( NULL ),
       
   185                                             iXmlParser ( NULL ),                                           
       
   186                                             iConnectionTimer( NULL ),
       
   187                                             iXMLBuf ( NULL )        
       
   188     {
       
   189     FUNC("CPosRevGeoCodePlugin::CPosRevGeoCodePlugin");
       
   190     }
       
   191 
       
   192 //------------------------------------------------------------------------------
       
   193 // CPosRevGeoCodePlugin::~CPosRevGeoCodePlugin
       
   194 //------------------------------------------------------------------------------
       
   195 CPosRevGeoCodePlugin::~CPosRevGeoCodePlugin()
       
   196     {
       
   197     FUNC("CPosRevGeoCodePlugin::~CPosRevGeoCodePlugin");
       
   198      
       
   199     delete iConnectionTimer;
       
   200     iConnectionTimer = NULL;
       
   201     
       
   202     delete iXMLBuf;
       
   203     iXMLBuf = NULL;
       
   204     
       
   205     delete iXmlParser;
       
   206     iXmlParser = NULL;
       
   207     
       
   208     delete iHTTPClientEngine;
       
   209     iHTTPClientEngine = NULL;
       
   210     FUNC("out>>CPosRevGeoCodePlugin");
       
   211     }
       
   212 
       
   213 //------------------------------------------------------------------------------
       
   214 // CPosRevGeoCodePlugin::GetAddressByCoordinateL
       
   215 //------------------------------------------------------------------------------
       
   216 void CPosRevGeoCodePlugin::GetAddressByCoordinateL( CPosLandmark& aLandmark )
       
   217     {
       
   218     FUNC("CPosRevGeoCodePlugin::GetAddressByCoordinateL");
       
   219     // Store the landmark object.
       
   220     iClientLandmark = &aLandmark;
       
   221     
       
   222     TLocality locality;
       
   223     User::LeaveIfError( aLandmark.GetPosition( locality ) );
       
   224     TReal64 latitude = locality.Latitude();
       
   225     TReal64 longitude = locality.Longitude();
       
   226     
       
   227     iAuthCode.Copy( KAuthenticationToken );
       
   228     iRefURL.Copy( KRefererURL );
       
   229     
       
   230     GetLanguageForTheRequest( iLang );
       
   231     
       
   232     //Form the request URI
       
   233     iQueryString.Format( KRequestFormat, &iAuthCode, &iRefURL, latitude, longitude, &iLang );
       
   234     
       
   235     iHTTPClientEngine->IssueHTTPGetL( iQueryString );
       
   236     
       
   237     if ( iConnectionTimer && iConnectionTimer->IsActive() )
       
   238         {
       
   239         iConnectionTimer->Cancel();
       
   240         }
       
   241     }
       
   242 
       
   243 //------------------------------------------------------------------------------
       
   244 // CPosRevGeoCodePlugin::Cancel()
       
   245 //------------------------------------------------------------------------------
       
   246 void CPosRevGeoCodePlugin::Cancel()
       
   247     {
       
   248     if( iHTTPClientEngine->IsActive() )
       
   249         {
       
   250         iHTTPClientEngine->CancelTransaction();
       
   251         }
       
   252     }
       
   253 
       
   254 //------------------------------------------------------------------------------
       
   255 // CPosRevGeoCodePlugin::ConstructL
       
   256 //------------------------------------------------------------------------------
       
   257 void CPosRevGeoCodePlugin::ConstructL()
       
   258     {
       
   259     FUNC("CPosRevGeoCodePlugin::ConstructL");
       
   260     iXMLBuf = HBufC8::NewL( KMaxResponseLength );
       
   261 
       
   262     iXmlParser = CPosRevGeoCodeXmlParser::NewL( *this );
       
   263     
       
   264     iHTTPClientEngine = CPosRevGeoCodeHTTPClientEngine::NewL( *this );
       
   265     
       
   266     iConnectionTimer = CPosRevGeoCodeConnectionTimer::NewL(*this);
       
   267     }
       
   268 
       
   269 //------------------------------------------------------------------------------
       
   270 // CPosRevGeoCodePlugin::OnParseCompletedL
       
   271 //------------------------------------------------------------------------------
       
   272 void CPosRevGeoCodePlugin::OnParseCompletedL( TInt aError )
       
   273     {
       
   274     FUNC("CPosRevGeoCodePlugin::OnParseCompletedL");
       
   275     StartTimer();
       
   276     iObserver.ReverseGeocodeComplete( aError );
       
   277     }
       
   278 
       
   279 //------------------------------------------------------------------------------
       
   280 // CPosRevGeoCodePlugin::ClientEvent
       
   281 //------------------------------------------------------------------------------
       
   282 void CPosRevGeoCodePlugin::ClientEvent( const THttpStatus& aEvent )
       
   283     {
       
   284     FUNC("CPosRevGeoCodePlugin::ClientEvent");
       
   285     TInt error;
       
   286     switch( aEvent )
       
   287         {
       
   288         case EHttpTxCompleted:
       
   289             //Reading is done
       
   290             //Parse the String and get the restults
       
   291             if( iXmlParser )
       
   292             {
       
   293              TRAP_IGNORE( iXmlParser->StartParsingL( iXMLBuf,iClientLandmark ) );
       
   294             }
       
   295             break;
       
   296         //All these cases will in turn lead to
       
   297         //generic failure due to connection/Tx related problems    
       
   298         case EHttpConnectionFailure:
       
   299             // May happen if Socket connection fails
       
   300             error = KErrCouldNotConnect;
       
   301             iObserver.ReverseGeocodeComplete( error );
       
   302             break;
       
   303         case EHttpTxFailed:
       
   304         case EHttpMhfRunError:
       
   305             // May happen if Socket connection fails
       
   306             // Complete the RGC with generic error.
       
   307             error = KErrGeneral;
       
   308             iObserver.ReverseGeocodeComplete( error );
       
   309             break;
       
   310         case EHttpTxCancelled:
       
   311             //On Cancellation of request.
       
   312             error = KErrCancel;
       
   313             iObserver.ReverseGeocodeComplete( error );
       
   314             break;
       
   315         case EHttpAuthFailed:
       
   316             //On authentication failed.
       
   317             error = KErrPermissionDenied;
       
   318             iObserver.ReverseGeocodeComplete( error );
       
   319             break;
       
   320         }
       
   321     }
       
   322 
       
   323 //------------------------------------------------------------------------------
       
   324 // CPosRevGeoCodePlugin::ClientBodyReceived
       
   325 //------------------------------------------------------------------------------
       
   326 void CPosRevGeoCodePlugin::ClientBodyReceived( const TDesC8& aBodyData )
       
   327     {
       
   328     FUNC("CPosRevGeoCodePlugin::ClientBodyReceived");
       
   329     //Dump the contents here
       
   330     TPtr8 ptr = iXMLBuf->Des();
       
   331     ptr.Zero();
       
   332     ptr.Append( aBodyData );
       
   333     }
       
   334 
       
   335 //------------------------------------------------------------------------------
       
   336 // CPosRevGeoCodePlugin::GetLanguageForTheRequest
       
   337 //------------------------------------------------------------------------------
       
   338 void CPosRevGeoCodePlugin::GetLanguageForTheRequest( TDes8& aLanguage )
       
   339     {
       
   340     FUNC("CPosRevGeoCodePlugin::GetLanguageForTheRequest");
       
   341     // Get the current phone langauge  
       
   342     TInt phoneLangIndex = User::Language();
       
   343 
       
   344     // Get the converted language 
       
   345     if (  phoneLangIndex < sizeof( Marc_Table ) / sizeof( Marc_Table[ 0 ]  ) )
       
   346         {
       
   347         aLanguage = (const TUint8*) Marc_Table[ phoneLangIndex ];
       
   348         }
       
   349     else
       
   350         {
       
   351          //By default language will be Eng
       
   352         aLanguage.Copy( KDefaultLanguage );
       
   353         }
       
   354     }
       
   355 
       
   356 //------------------------------------------------------------------------------
       
   357 // CPosRevGeoCodePlugin::CloseConnection
       
   358 //------------------------------------------------------------------------------
       
   359 void CPosRevGeoCodePlugin::CloseConnection()
       
   360     {
       
   361     FUNC("CPosRevGeoCodePlugin::CloseConnection");
       
   362     if( iHTTPClientEngine )
       
   363         {
       
   364         iHTTPClientEngine->CloseConnection();
       
   365         }
       
   366     }
       
   367 
       
   368 //------------------------------------------------------------------------------
       
   369 // CPosRevGeoCodePlugin::StartTimer
       
   370 //------------------------------------------------------------------------------
       
   371 void CPosRevGeoCodePlugin::StartTimer()
       
   372     {
       
   373     FUNC("CPosRevGeoCodePlugin::StartTimer");
       
   374     if( iConnectionTimer )
       
   375         {
       
   376         iConnectionTimer->StartTimer( KTimeOutInterval );
       
   377         }
       
   378     }
       
   379 
       
   380 //------------------------------------------------------------------------------
       
   381 // CPosRevGeoCodePlugin::HandleTimedoutEvent
       
   382 //------------------------------------------------------------------------------
       
   383 void CPosRevGeoCodePlugin::HandleTimedoutEvent(TInt /*aErrorCode*/)
       
   384     {
       
   385     FUNC("CPosRevGeoCodePlugin::HandleTimedoutEvent");
       
   386     CloseConnection();
       
   387     }
       
   388 
       
   389 //end of file