mapnavproviderrefapp/src/mnrppositionrequest.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2006 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:  CMnrpPositionRequest class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <AknWaitDialog.h>
       
    20 #include <coemain.h>
       
    21 #include <aknnotewrappers.h>
       
    22 
       
    23 #include <lbspositioninfo.h>
       
    24 
       
    25 #include "mnrppositionrequest.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CMnrpPositionRequest::CMnrpPositionRequest(
       
    33     MMnrpPositionObserver& aObserver )
       
    34     : 
       
    35     CActive( CActive::EPriorityStandard ), 
       
    36     iObserver( aObserver ),
       
    37     iState( EIdle )
       
    38     {
       
    39     TRealX nan;
       
    40     nan.SetNaN();
       
    41     iSpeed = iHeading = iCourse = iMagneticHeading = iMagneticCourse = nan;
       
    42     
       
    43     CActiveScheduler::Add( this );
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CMnrpPositionRequest::ConstructL( const TDesC& aRequestorName )
       
    50     {
       
    51     const TInt KBufSize = 8192;
       
    52     iGenericPosition = HPositionGenericInfo::NewL( KBufSize );
       
    53     
       
    54     // Connect to the location server
       
    55     User::LeaveIfError( iLocationServer.Connect() );
       
    56 
       
    57     // Open the default positioner
       
    58     User::LeaveIfError( iPositioner.Open( iLocationServer ) );
       
    59 
       
    60     // Set this application as location requestor
       
    61     User::LeaveIfError( iPositioner.SetRequestor(
       
    62         CRequestor::ERequestorService,
       
    63         CRequestor::EFormatApplication,
       
    64         aRequestorName ) );
       
    65 
       
    66     // Set default options
       
    67     TPositionUpdateOptions updateOptions;
       
    68 
       
    69     const TInt K30Seconds = 30000000;
       
    70     TTimeIntervalMicroSeconds timeOut( K30Seconds );
       
    71 
       
    72     updateOptions.SetUpdateTimeOut( timeOut );
       
    73     SetOptionsL( updateOptions );
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CMnrpPositionRequest* CMnrpPositionRequest::NewL(
       
    80     const TDesC& aAppName, 
       
    81     MMnrpPositionObserver& aObserver )
       
    82     {
       
    83     CMnrpPositionRequest* self =
       
    84         new (ELeave) CMnrpPositionRequest( aObserver );
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL( aAppName );
       
    87     CleanupStack::Pop( self );
       
    88     return self;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 EXPORT_C CMnrpPositionRequest::~CMnrpPositionRequest()
       
    95     {
       
    96     Cancel();
       
    97     iPositioner.Close();
       
    98     iLocationServer.Close();
       
    99     delete iGenericPosition;
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C void CMnrpPositionRequest::FetchNewPosition( TBool aLastKnownLocationAllowed )
       
   106     {
       
   107     Cancel();
       
   108     // Request location
       
   109     iState = EAcquiringDefaultLocation;
       
   110     iLastKnownLocationAllowed = aLastKnownLocationAllowed;
       
   111     ExecuteNextStep();
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 EXPORT_C void CMnrpPositionRequest::GetPosition( TPositionInfo& aPositionInfo )
       
   118     {
       
   119     aPositionInfo = iPositionInfo;
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 EXPORT_C TReal32 CMnrpPositionRequest::Speed() 
       
   126     { 
       
   127     return iSpeed;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C TReal32 CMnrpPositionRequest::Heading() 
       
   134     { 
       
   135     return iHeading; 
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 EXPORT_C TReal32 CMnrpPositionRequest::MagneticHeading() 
       
   142     { 
       
   143     return iMagneticHeading; 
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 EXPORT_C TReal32 CMnrpPositionRequest::Course() 
       
   150     {
       
   151     return iCourse;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C TReal32 CMnrpPositionRequest::MagneticCourse()
       
   158     { 
       
   159     return iMagneticCourse;
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CMnrpPositionRequest::DoCancel()
       
   166     {
       
   167     // Cancel location request
       
   168     if (iState == EAcquiringDefaultLocation)
       
   169         {
       
   170         iPositioner.CancelRequest( EPositionerNotifyPositionUpdate );
       
   171         }
       
   172     else if (iState == EAcquiringLastKnownLocation)
       
   173         {
       
   174         iPositioner.CancelRequest( EPositionerGetLastKnownPosition );
       
   175         }
       
   176     iState = EIdle;
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CMnrpPositionRequest::RunL()
       
   183     {
       
   184     if ( iStatus != KErrNone &&
       
   185          iState == EAcquiringDefaultLocation && 
       
   186          iLastKnownLocationAllowed )
       
   187         {
       
   188         // Fetching current location failed. Try last known location.
       
   189         iState = EAcquiringLastKnownLocation;
       
   190         ExecuteNextStep();
       
   191         }
       
   192     else
       
   193         {
       
   194         // Operation finished, notify observer
       
   195         iState = EIdle;
       
   196         SaveContainer( *iGenericPosition );
       
   197         iObserver.HandlePositionRequestCompletedL( iStatus.Int() );
       
   198         }
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 TInt CMnrpPositionRequest::RunError(TInt /*aError*/)
       
   205     {
       
   206     // Ignore possible errors.
       
   207     return KErrNone;
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CMnrpPositionRequest::ExecuteNextStep()
       
   214     {
       
   215     iStatus = KRequestPending;
       
   216 
       
   217     switch (iState)
       
   218         {
       
   219         case EAcquiringDefaultLocation:
       
   220             InitContainer( *iGenericPosition );
       
   221             iPositioner.NotifyPositionUpdate( *iGenericPosition, iStatus);
       
   222             break;
       
   223         case EAcquiringLastKnownLocation:
       
   224             iPositioner.GetLastKnownPosition( iPositionInfo, iStatus );
       
   225             break;
       
   226         default:    
       
   227             // this case should never happen, see RunL
       
   228             return;
       
   229         }
       
   230 
       
   231     SetActive();
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 EXPORT_C void CMnrpPositionRequest::SetOptionsL( const TPositionUpdateOptionsBase& aOptions )
       
   238     {
       
   239     User::LeaveIfError( iPositioner.SetUpdateOptions( aOptions ) );
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CMnrpPositionRequest::InitContainer( HPositionGenericInfo& aGenericInfo )
       
   246     {
       
   247     aGenericInfo.ClearPositionData();
       
   248     
       
   249     aGenericInfo.SetRequestedField( EPositionFieldHorizontalSpeed );
       
   250     aGenericInfo.SetRequestedField( EPositionFieldTrueCourse );
       
   251     aGenericInfo.SetRequestedField( EPositionFieldMagneticCourse );
       
   252     aGenericInfo.SetRequestedField( EPositionFieldHeading );
       
   253     aGenericInfo.SetRequestedField( EPositionFieldMagneticHeading );
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 void CMnrpPositionRequest::SaveContainer( const HPositionGenericInfo& aGenericInfo )
       
   260     {
       
   261     TRealX nan;
       
   262     nan.SetNaN();
       
   263     iSpeed = iHeading = iCourse = iMagneticHeading = iMagneticCourse = nan;
       
   264     
       
   265     aGenericInfo.GetValue( EPositionFieldHorizontalSpeed, iSpeed );
       
   266     aGenericInfo.GetValue( EPositionFieldTrueCourse, iCourse );
       
   267     aGenericInfo.GetValue( EPositionFieldMagneticCourse, iMagneticCourse );
       
   268     aGenericInfo.GetValue( EPositionFieldHeading, iHeading );
       
   269     aGenericInfo.GetValue( EPositionFieldMagneticHeading, iMagneticHeading );
       
   270     
       
   271     iPositionInfo = *iGenericPosition;
       
   272     }