plugin/poi/landmarks/provider/src/Remote.cpp
changeset 0 c316ab048e9d
equal deleted inserted replaced
-1:000000000000 0:c316ab048e9d
       
     1 /*
       
     2  * Name        : Remote.cpp
       
     3  * Description : 
       
     4  * Project     : This file is part of OpenMAR, an Open Mobile Augmented Reality browser
       
     5  * Website     : http://OpenMAR.org
       
     6  *
       
     7  * Copyright (c) 2010 David Caabeiro
       
     8  *
       
     9  * All rights reserved. This program and the accompanying materials are made available 
       
    10  * under the terms of the Eclipse Public License v1.0 which accompanies this 
       
    11  * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
       
    12  *
       
    13  */
       
    14 
       
    15 #include "Remote.h"
       
    16 
       
    17 #include "HttpClient.h"
       
    18 
       
    19 #include <AknUtils.h>
       
    20 #include <lbspositioninfo.h>
       
    21 #include <http/mhttpdatasupplier.h>
       
    22 #include <HttpErr.h>
       
    23 #include <utf.h>
       
    24 
       
    25 #include "Logger.h"
       
    26 
       
    27 CRemoteLoader* CRemoteLoader::NewL(MLandmarkLoaderObserver& aObserver, const TDesC8& aProviderUri)
       
    28 {
       
    29     CRemoteLoader* self = new(ELeave) CRemoteLoader(aObserver);
       
    30     CleanupStack::PushL(self);
       
    31     self->ConstructL(aProviderUri);
       
    32     CleanupStack::Pop(self);
       
    33 
       
    34     return self;
       
    35 }
       
    36 
       
    37 CRemoteLoader::~CRemoteLoader()
       
    38 {
       
    39     iBuffer.Close();
       
    40 
       
    41     delete iParser;
       
    42     delete iClient;
       
    43     delete iBaseUri;
       
    44 }
       
    45 
       
    46 CRemoteLoader::CRemoteLoader(MLandmarkLoaderObserver& aObserver)
       
    47     : CActive(CActive::EPriorityStandard), iObserver(aObserver)
       
    48 {
       
    49     CActiveScheduler::Add(this);
       
    50 }
       
    51 
       
    52 void CRemoteLoader::ConstructL(const TDesC8& aProviderUri)
       
    53 {
       
    54     LOGARG("[LANDMARKS] Creating remote landmark loader: %S", &aProviderUri);
       
    55 
       
    56     iBaseUri = aProviderUri.AllocL();
       
    57     iClient = CHttpClient::NewL(*this);
       
    58 
       
    59     _LIT8(KMimeType, "application/vnd.nokia.landmarkcollection+xml");
       
    60     iParser = CPosLandmarkParser::NewL(KMimeType);
       
    61 }
       
    62 
       
    63 void CRemoteLoader::RunL()
       
    64 {
       
    65     if (iStatus == KPosLmOperationNotComplete || iStatus == KErrNone)
       
    66     {
       
    67         CPosLandmark* landmark = iParser->LandmarkLC();
       
    68         iObserver.LandmarkLoaderItemCreatedL(*landmark);
       
    69         CleanupStack::PopAndDestroy(landmark);
       
    70     }
       
    71 
       
    72     if (iStatus == KPosLmOperationNotComplete)
       
    73     {
       
    74         iOperation->NextStep(iStatus, iProgress);
       
    75         SetActive();
       
    76     }
       
    77 }
       
    78 
       
    79 void CRemoteLoader::DoCancel()
       
    80 {
       
    81     // TODO: Add cancellation code
       
    82 }
       
    83 
       
    84 void CRemoteLoader::RequestL(const TCoordinate& aCoordinate, TReal32 aRadius)
       
    85 {
       
    86     TRealFormat realFormat;
       
    87     realFormat.iPoint  = TChar('.');    // Override TLocale::DecimalSeparator() as separator
       
    88     realFormat.iWidth  = 8;             // Set max width of number
       
    89     realFormat.iPlaces = 5;             // Set max width of decimal portion
       
    90 
       
    91     TBuf8<KDefaultRealWidth> latitude;
       
    92     latitude.Num(aCoordinate.Latitude(), realFormat);
       
    93 
       
    94     TBuf8<KDefaultRealWidth> longitude;
       
    95     longitude.Num(aCoordinate.Longitude(), realFormat);
       
    96 
       
    97     // Convert to integer
       
    98     TInt radius = static_cast<TInt>(aRadius);
       
    99 
       
   100     TBuf8<256> uri(*iBaseUri);
       
   101     _LIT8(KParam, "?lat=%S&lon=%S&radius=%d");
       
   102     uri.AppendFormat(KParam, &latitude, &longitude, radius);
       
   103 
       
   104     LOGARG("[LANDMARKS] Making http request %S", &uri);
       
   105 
       
   106     iClient->GetL(uri);
       
   107 }
       
   108 
       
   109 void CRemoteLoader::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   110 {
       
   111     switch (aEvent.iStatus)
       
   112     {
       
   113         case THTTPEvent::EGotResponseHeaders:
       
   114         {
       
   115             RHTTPResponse resp = aTransaction.Response();
       
   116             TInt status = resp.StatusCode();
       
   117 
       
   118             LOGARG("[LANDMARKS] HTTP response status %d", status);
       
   119 
       
   120             break;
       
   121         }
       
   122 
       
   123         case THTTPEvent::EGotResponseBodyData:
       
   124         {
       
   125             MHTTPDataSupplier* body = aTransaction.Response().Body();
       
   126 
       
   127             TPtrC8 dataChunk;
       
   128             body->GetNextDataPart(dataChunk);
       
   129 
       
   130             // Check if there is enough remaining space
       
   131             const TInt newLength = iBuffer.Length() + dataChunk.Length();
       
   132             if (iBuffer.MaxLength() < newLength)
       
   133                 iBuffer.ReAllocL(newLength);
       
   134             iBuffer.Append(dataChunk);
       
   135 
       
   136             body->ReleaseData();
       
   137 
       
   138             break;
       
   139         }
       
   140 
       
   141         case THTTPEvent::EResponseComplete:
       
   142             LOGTXT("[LANDMARKS] HTTP response complete");
       
   143 
       
   144             break ;
       
   145 
       
   146         case THTTPEvent::ESucceeded:
       
   147         {
       
   148             RHTTPResponse resp = aTransaction.Response();
       
   149             TInt status = resp.StatusCode();
       
   150 
       
   151             LOGARG("[LANDMARKS] HTTP response succeeded %d", status);
       
   152 
       
   153             aTransaction.Close();
       
   154 
       
   155             iObserver.LandmarkLoaderOpenedL(KErrNone);
       
   156             
       
   157             iParser->SetInputBuffer(iBuffer);
       
   158 
       
   159             iOperation = iParser->ParseContentL();
       
   160 
       
   161             iOperation->NextStep(iStatus, iProgress);
       
   162             SetActive();
       
   163 
       
   164             break;
       
   165         }
       
   166 
       
   167         case THTTPEvent::EFailed:
       
   168         {
       
   169             RHTTPResponse resp = aTransaction.Response();
       
   170             TInt status = resp.StatusCode();
       
   171 
       
   172             LOGARG("[LANDMARKS] HTTP response failed %d", status);
       
   173 
       
   174             aTransaction.Close();
       
   175 
       
   176             iObserver.LandmarkLoaderOpenedL(KErrCouldNotConnect);
       
   177             break;
       
   178         }
       
   179 
       
   180         default:
       
   181             LOGARG("[LANDMARKS] HTTP unknown event %d", aEvent.iStatus);
       
   182 
       
   183             aTransaction.Close();
       
   184 
       
   185             iObserver.LandmarkLoaderOpenedL(KErrUnknown);
       
   186             break;
       
   187     }
       
   188 }
       
   189 
       
   190 TInt CRemoteLoader::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   191 {
       
   192     iObserver.LandmarkLoaderOpenedL(aError);
       
   193 
       
   194     return KErrNone;
       
   195 }