locationlandmarksrefappfors60/Src/LandmarksPositionRequest.cpp
changeset 0 522cd55cc3d7
child 34 8173571d354e
equal deleted inserted replaced
-1:000000000000 0:522cd55cc3d7
       
     1 /*
       
     2 * Copyright (c) 2004-2007 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:  Implements the CLandmarksPositionRequest class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <AknWaitDialog.h>
       
    21 #include <coemain.h>
       
    22 #include <aknnotewrappers.h>
       
    23 
       
    24 #include <lbspositioninfo.h>
       
    25 
       
    26 #include <LmRefApp.rsg>
       
    27 #include "LandmarksUtils.h"
       
    28 #include "LandmarksPositionRequest.h"
       
    29 #include "LandmarksOperationObserver.h"
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CLandmarksPositionRequest::CLandmarksPositionRequest(
       
    37     MLandmarksOperationObserver* aObserver)
       
    38     : 
       
    39     CActive(CActive::EPriorityStandard), 
       
    40     iObserver(aObserver),
       
    41     iState(EIdle)
       
    42     {
       
    43     CActiveScheduler::Add(this);
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CLandmarksPositionRequest::ConstructL(const TDesC& aAppName)
       
    50     {
       
    51     iAppName = aAppName.AllocL();
       
    52     InitializeL();
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CLandmarksPositionRequest* CLandmarksPositionRequest::NewL(
       
    59     const TDesC& aAppName,
       
    60     MLandmarksOperationObserver* aObserver)
       
    61     {
       
    62     CLandmarksPositionRequest* self =
       
    63         new (ELeave) CLandmarksPositionRequest(aObserver);
       
    64     CleanupStack::PushL(self);
       
    65     self->ConstructL(aAppName);
       
    66     CleanupStack::Pop(self);
       
    67     return self;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CLandmarksPositionRequest::~CLandmarksPositionRequest()
       
    74     {
       
    75     Cancel();
       
    76     iPositioner.Close();
       
    77     iLocationServer.Close();
       
    78     DismissWaitNote();
       
    79     delete iAppName;
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CLandmarksPositionRequest::FetchPositionInfoL(
       
    86     const TDesC& aMessage,
       
    87     TPositionInfo& aPositionInfo)
       
    88     {
       
    89     Cancel();
       
    90     iPositionInfo = &aPositionInfo;
       
    91 
       
    92     // Initialize wait note
       
    93     CAknWaitDialog* waitNote = new (ELeave) CAknWaitDialog(
       
    94         reinterpret_cast <CEikDialog**> (&iWaitNote), ETrue); // show immedaitely
       
    95     CleanupStack::PushL(waitNote);
       
    96     waitNote->SetCallback(this);
       
    97     waitNote->SetTextL(aMessage);
       
    98     waitNote->PrepareLC(R_LMREFAPP_ACQUIRE_LOCATION_WAIT_NOTE);
       
    99     CleanupStack::Pop(waitNote); // waitNote will delete itself
       
   100     iWaitNote = waitNote;
       
   101 
       
   102     // Request location
       
   103     iState = EAcquiringDefaultLocation;
       
   104     ExecuteNextStep();
       
   105 
       
   106     // Launch wait note
       
   107     iWaitNote->RunLD();
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CLandmarksPositionRequest::DismissWaitNote()
       
   114     {
       
   115     if (iWaitNote)
       
   116         {
       
   117         TInt err;
       
   118         TRAP(err, iWaitNote->ProcessFinishedL());
       
   119         iWaitNote = NULL;
       
   120         }
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void CLandmarksPositionRequest::DoCancel()
       
   127     {
       
   128     // Cancel location request
       
   129     if (iState == EAcquiringDefaultLocation)
       
   130         {
       
   131         iPositioner.CancelRequest(EPositionerNotifyPositionUpdate);
       
   132         }
       
   133     else if (iState == EAcquiringLastKnownLocation)
       
   134         {
       
   135         iPositioner.CancelRequest(EPositionerGetLastKnownPosition);
       
   136         }
       
   137     iState = EIdle;
       
   138     DismissWaitNote();
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void CLandmarksPositionRequest::RunL()
       
   145     {
       
   146     if (iStatus == KErrNone)
       
   147         {
       
   148         // Location retrieved. Operation finished.
       
   149 
       
   150         if ( iState == EAcquiringLastKnownLocation )
       
   151             {
       
   152             // inform user that it was last known location
       
   153             HBufC* msg = CCoeEnv::Static()->AllocReadResourceLC( R_LMREFAPP_LAST_LOCATION_USED_INFO );
       
   154             LandmarksUtils::ErrorNoteL( *msg );
       
   155             CleanupStack::PopAndDestroy(msg);
       
   156             }
       
   157 
       
   158         NotifyOperationCompleteL(KErrNone);
       
   159         }
       
   160     else // iStatus != KErrNone
       
   161         {
       
   162         if (iState == EAcquiringDefaultLocation)
       
   163             {
       
   164             // Fetching current location failed. Try last known location.
       
   165             iState = EAcquiringLastKnownLocation;
       
   166             ExecuteNextStep();
       
   167             }
       
   168         else if (iState == EAcquiringLastKnownLocation)
       
   169             {
       
   170             // Fetching last known location failed. 
       
   171             NotifyOperationCompleteL(iStatus.Int());
       
   172             }
       
   173         }
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 TInt CLandmarksPositionRequest::RunError(TInt /*aError*/)
       
   180     {
       
   181     // Ignore possible errors.
       
   182     return KErrNone;
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CLandmarksPositionRequest::DialogDismissedL(TInt aButtonId)
       
   189     {
       
   190     if (aButtonId == EAknSoftkeyCancel)
       
   191         {
       
   192         // Dialog is already dismissed by framework
       
   193         iWaitNote = NULL;
       
   194 
       
   195         Cancel();
       
   196         }
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CLandmarksPositionRequest::ShowErrorL(TInt aError)
       
   203     {
       
   204     HBufC* format = CCoeEnv::Static()->AllocReadResourceAsDes16LC(
       
   205         R_LMREFAPP_ACQUIRING_LOC_ERROR);
       
   206     
       
   207     // some extra characters needed for error code
       
   208     const TInt KErrorCodeTextLength = 10;
       
   209     HBufC* message = HBufC::NewLC(
       
   210         format->Length() + KErrorCodeTextLength);
       
   211     message->Des().Format(*format, aError);
       
   212 
       
   213     CAknInformationNote* informationNote = 
       
   214         new (ELeave) CAknInformationNote(ETrue);
       
   215     informationNote->ExecuteLD(*message);
       
   216     CleanupStack::PopAndDestroy(2, format);
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CLandmarksPositionRequest::ExecuteNextStep()
       
   223     {
       
   224     iStatus = KRequestPending;
       
   225 
       
   226     switch (iState)
       
   227         {
       
   228         case EAcquiringDefaultLocation:
       
   229             iPositioner.NotifyPositionUpdate(*iPositionInfo, iStatus);
       
   230             break;
       
   231         case EAcquiringLastKnownLocation:
       
   232             iPositioner.GetLastKnownPosition(*iPositionInfo, iStatus);
       
   233             break;
       
   234         default:    
       
   235             // this case should never happen, see RunL
       
   236             LandmarksUtils::Panic(KErrGeneral);
       
   237             break;
       
   238         }
       
   239 
       
   240     SetActive();
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CLandmarksPositionRequest::NotifyOperationCompleteL(TInt aError)
       
   247     {
       
   248     DismissWaitNote();
       
   249     iState = EIdle;
       
   250 
       
   251     // Notify user if location couldn't be fetched.
       
   252     if (aError) 
       
   253         {
       
   254         ShowErrorL(aError);
       
   255         }
       
   256 
       
   257     // Notify Observer
       
   258     iObserver->NotifyOperationProgressL(
       
   259         EAcquiringLocation, KOperationReady, aError);
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CLandmarksPositionRequest::InitializeL()
       
   266     {
       
   267     // Connect to the location server
       
   268     User::LeaveIfError(iLocationServer.Connect());
       
   269 
       
   270     // Open the default positioner
       
   271     User::LeaveIfError(iPositioner.Open(iLocationServer));
       
   272 
       
   273     // Set this application as location requestor
       
   274     User::LeaveIfError(iPositioner.SetRequestor(
       
   275         CRequestor::ERequestorService,
       
   276         CRequestor::EFormatApplication,
       
   277         *iAppName));
       
   278 
       
   279     // Set maximum allowed time for a location request
       
   280     const TInt K30Seconds = 30000000;
       
   281     TTimeIntervalMicroSeconds timeOut(K30Seconds);
       
   282     TPositionUpdateOptions updateOptions;
       
   283     updateOptions.SetUpdateTimeOut(timeOut);
       
   284     User::LeaveIfError(iPositioner.SetUpdateOptions(updateOptions));
       
   285     }
       
   286 
       
   287