|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : LandmarksPositionRequest.cpp 00004 * Part of : Landmark Reference Application 00005 * Interface : - 00006 * Description : Implements the CLandmarksPositionRequest class 00007 * Version : 1.0 00008 * 00009 * Copyright (c) 2004-2006 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================== 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <aknwaitdialog.h> 00018 #include <coemain.h> 00019 #include <aknnotewrappers.h> 00020 00021 #include <LbsPositionInfo.h> 00022 00023 #include <LmRefApp.rsg> 00024 #include "LandmarksUtils.h" 00025 #include "LandmarksPositionRequest.h" 00026 #include "LandmarksOperationObserver.h" 00027 00028 // ============================ MEMBER FUNCTIONS =============================== 00029 00030 // ----------------------------------------------------------------------------- 00031 // CLandmarksPositionRequest::CLandmarksPositionRequest 00032 // C++ constructor can NOT contain any code, that 00033 // might leave. 00034 // ----------------------------------------------------------------------------- 00035 // 00036 CLandmarksPositionRequest::CLandmarksPositionRequest( 00037 MLandmarksOperationObserver* aObserver) 00038 : 00039 CActive(CActive::EPriorityStandard), 00040 iObserver(aObserver), 00041 iState(EIdle) 00042 { 00043 CActiveScheduler::Add(this); 00044 } 00045 00046 // ----------------------------------------------------------------------------- 00047 // CLandmarksPositionRequest::ConstructL 00048 // Symbian 2nd phase constructor can leave. 00049 // ----------------------------------------------------------------------------- 00050 // 00051 void CLandmarksPositionRequest::ConstructL(const TDesC& aAppName) 00052 { 00053 iAppName = aAppName.AllocL(); 00054 InitializeL(); 00055 } 00056 00057 // ----------------------------------------------------------------------------- 00058 // CLandmarksPositionRequest::NewL 00059 // Two-phased constructor. 00060 // ----------------------------------------------------------------------------- 00061 // 00062 CLandmarksPositionRequest* CLandmarksPositionRequest::NewL( 00063 const TDesC& aAppName, 00064 MLandmarksOperationObserver* aObserver) 00065 { 00066 CLandmarksPositionRequest* self = 00067 new (ELeave) CLandmarksPositionRequest(aObserver); 00068 CleanupStack::PushL(self); 00069 self->ConstructL(aAppName); 00070 CleanupStack::Pop(self); 00071 return self; 00072 } 00073 00074 // Destructor 00075 CLandmarksPositionRequest::~CLandmarksPositionRequest() 00076 { 00077 Cancel(); 00078 iPositioner.Close(); 00079 iLocationServer.Close(); 00080 DismissWaitNote(); 00081 delete iAppName; 00082 } 00083 00084 // ----------------------------------------------------------------------------- 00085 // CLandmarksPositionRequest::FetchPositionInfoL 00086 // 00087 // (other items were commented in a header). 00088 // ----------------------------------------------------------------------------- 00089 // 00090 void CLandmarksPositionRequest::FetchPositionInfoL( 00091 const TDesC& aMessage, 00092 TPositionInfo& aPositionInfo) 00093 { 00094 Cancel(); 00095 iPositionInfo = &aPositionInfo; 00096 00097 // Initialize wait note 00098 CAknWaitDialog* waitNote = new (ELeave) CAknWaitDialog( 00099 reinterpret_cast <CEikDialog**> (&iWaitNote), ETrue); // show immedaitely 00100 CleanupStack::PushL(waitNote); 00101 waitNote->SetCallback(this); 00102 waitNote->SetTextL(aMessage); 00103 waitNote->PrepareLC(R_LMREFAPP_ACQUIRE_LOCATION_WAIT_NOTE); 00104 CleanupStack::Pop(waitNote); // waitNote will delete itself 00105 iWaitNote = waitNote; 00106 00107 // Request location 00108 iState = EAcquiringDefaultLocation; 00109 ExecuteNextStep(); 00110 00111 // Launch wait note 00112 iWaitNote->RunLD(); 00113 } 00114 00115 // ----------------------------------------------------------------------------- 00116 // CLandmarksPositionRequest::DismissWaitNote 00117 // 00118 // (other items were commented in a header). 00119 // ----------------------------------------------------------------------------- 00120 // 00121 void CLandmarksPositionRequest::DismissWaitNote() 00122 { 00123 if (iWaitNote) 00124 { 00125 TInt err; 00126 TRAP(err, iWaitNote->ProcessFinishedL()); 00127 iWaitNote = NULL; 00128 } 00129 } 00130 00131 // ----------------------------------------------------------------------------- 00132 // CLandmarksPositionRequest::DoCancel 00133 // 00134 // (other items were commented in a header). 00135 // ----------------------------------------------------------------------------- 00136 // 00137 void CLandmarksPositionRequest::DoCancel() 00138 { 00139 // Cancel location request 00140 if (iState == EAcquiringDefaultLocation) 00141 { 00142 iPositioner.CancelRequest(EPositionerNotifyPositionUpdate); 00143 } 00144 else if (iState == EAcquiringLastKnownLocation) 00145 { 00146 iPositioner.CancelRequest(EPositionerGetLastKnownPosition); 00147 } 00148 iState = EIdle; 00149 DismissWaitNote(); 00150 } 00151 00152 // ----------------------------------------------------------------------------- 00153 // CLandmarksPositionRequest::RunL 00154 // 00155 // (other items were commented in a header). 00156 // ----------------------------------------------------------------------------- 00157 // 00158 void CLandmarksPositionRequest::RunL() 00159 { 00160 if (iStatus == KErrNone) 00161 { 00162 // Location retrieved. Operation finished. 00163 NotifyOperationCompleteL(KErrNone); 00164 } 00165 else // iStatus != KErrNone 00166 { 00167 if (iState == EAcquiringDefaultLocation) 00168 { 00169 // Fetching current location failed. Try last known location. 00170 iState = EAcquiringLastKnownLocation; 00171 ExecuteNextStep(); 00172 } 00173 else if (iState == EAcquiringLastKnownLocation) 00174 { 00175 // Fetching last known location failed. 00176 NotifyOperationCompleteL(iStatus.Int()); 00177 } 00178 } 00179 } 00180 00181 // ----------------------------------------------------------------------------- 00182 // CLandmarksPositionRequest::RunError 00183 // 00184 // (other items were commented in a header). 00185 // ----------------------------------------------------------------------------- 00186 // 00187 TInt CLandmarksPositionRequest::RunError(TInt /*aError*/) 00188 { 00189 // Ignore possible errors. 00190 return KErrNone; 00191 } 00192 00193 // ----------------------------------------------------------------------------- 00194 // CLandmarksPositionRequest::DialogDismissedL 00195 // 00196 // (other items were commented in a header). 00197 // ----------------------------------------------------------------------------- 00198 // 00199 void CLandmarksPositionRequest::DialogDismissedL(TInt aButtonId) 00200 { 00201 if (aButtonId == EAknSoftkeyCancel) 00202 { 00203 // Dialog is already dismissed by framework 00204 iWaitNote = NULL; 00205 00206 Cancel(); 00207 } 00208 } 00209 00210 void CLandmarksPositionRequest::ShowErrorL(TInt aError) 00211 { 00212 HBufC* format = CCoeEnv::Static()->AllocReadResourceAsDes16LC( 00213 R_LMREFAPP_ACQUIRING_LOC_ERROR); 00214 00215 // some extra characters needed for error code 00216 const TInt KErrorCodeTextLength = 10; 00217 HBufC* message = HBufC::NewLC( 00218 format->Length() + KErrorCodeTextLength); 00219 message->Des().Format(*format, aError); 00220 00221 CAknInformationNote* informationNote = 00222 new (ELeave) CAknInformationNote(ETrue); 00223 informationNote->ExecuteLD(*message); 00224 CleanupStack::PopAndDestroy(2, format); 00225 } 00226 00227 // ----------------------------------------------------------------------------- 00228 // CLandmarksPositionRequest::ExecuteNextStep 00229 // 00230 // (other items were commented in a header). 00231 // ----------------------------------------------------------------------------- 00232 // 00233 void CLandmarksPositionRequest::ExecuteNextStep() 00234 { 00235 iStatus = KRequestPending; 00236 00237 switch (iState) 00238 { 00239 case EAcquiringDefaultLocation: 00240 iPositioner.NotifyPositionUpdate(*iPositionInfo, iStatus); 00241 break; 00242 case EAcquiringLastKnownLocation: 00243 iPositioner.GetLastKnownPosition(*iPositionInfo, iStatus); 00244 break; 00245 default: 00246 // this case should never happen, see RunL 00247 LandmarksUtils::Panic(KErrGeneral); 00248 break; 00249 } 00250 00251 SetActive(); 00252 } 00253 00254 // ----------------------------------------------------------------------------- 00255 // CLandmarksPositionRequest::NotifyOperationCompleteL 00256 // 00257 // (other items were commented in a header). 00258 // ----------------------------------------------------------------------------- 00259 // 00260 void CLandmarksPositionRequest::NotifyOperationCompleteL(TInt aError) 00261 { 00262 DismissWaitNote(); 00263 iState = EIdle; 00264 00265 // Notify user if location couldn't be fetched. 00266 if (aError) 00267 { 00268 ShowErrorL(aError); 00269 } 00270 00271 // Notify Observer 00272 iObserver->NotifyOperationProgressL( 00273 EAcquiringLocation, KOperationReady, aError); 00274 } 00275 00276 // ----------------------------------------------------------------------------- 00277 // CLandmarksPositionRequest::InitializeL() 00278 // 00279 // (other items were commented in a header). 00280 // ----------------------------------------------------------------------------- 00281 // 00282 void CLandmarksPositionRequest::InitializeL() 00283 { 00284 // Connect to the location server 00285 User::LeaveIfError(iLocationServer.Connect()); 00286 00287 // Open the default positioner 00288 User::LeaveIfError(iPositioner.Open(iLocationServer)); 00289 00290 // Set this application as location requestor 00291 User::LeaveIfError(iPositioner.SetRequestor( 00292 CRequestor::ERequestorService, 00293 CRequestor::EFormatApplication, 00294 *iAppName)); 00295 00296 // Set maximum allowed time for a location request 00297 const TInt K30Seconds = 30000000; 00298 TTimeIntervalMicroSeconds timeOut(K30Seconds); 00299 TPositionUpdateOptions updateOptions; 00300 updateOptions.SetUpdateTimeOut(timeOut); 00301 User::LeaveIfError(iPositioner.SetUpdateOptions(updateOptions)); 00302 } 00303 00304 // End of File 00305
|
© Nokia 2006 |