|
1 /* |
|
2 * Copyright (c) 2008-2008 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: Parser class for parsing landmarks to url and vice versa |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "emailtrace.h" |
|
21 #include <EPos_CPosLandmark.h> |
|
22 #include <ecom/implementationproxy.h> |
|
23 #include <barsread.h> |
|
24 #include <esmrurlparserplugindata.rsg> |
|
25 #include <data_caging_path_literals.hrh> |
|
26 #include <barsc.h> |
|
27 #include <f32file.h> |
|
28 #include <lbsposition.h> |
|
29 |
|
30 #include "cesmrurlparserpluginimpl.h" |
|
31 #include "esmrinternaluid.h" |
|
32 |
|
33 |
|
34 _LIT( KResourceName, "esmrurlparserplugindata.rsc" ); |
|
35 _LIT( KResourceFileLocFormat, "r%02d" ); |
|
36 _LIT( KResourceFormat, "rsc" ); |
|
37 |
|
38 const TInt KCoordinateMaxLength = 10; |
|
39 const TInt KNumberOfDecimals = 4; |
|
40 const TUint KDecimalSeparator = '.'; |
|
41 |
|
42 |
|
43 |
|
44 // ======== MEMBER FUNCTIONS ======== |
|
45 |
|
46 |
|
47 |
|
48 // --------------------------------------------------------------------------- |
|
49 // Non-leaving constructor |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 CESMRUrlParserPluginImpl::CESMRUrlParserPluginImpl() |
|
53 : iIsInitialized(EFalse) |
|
54 { |
|
55 FUNC_LOG; |
|
56 |
|
57 } |
|
58 |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // ConstructL for leaving construction |
|
62 // --------------------------------------------------------------------------- |
|
63 // |
|
64 void CESMRUrlParserPluginImpl::ConstructL()// codescanner::LFunctionCantLeave |
|
65 { |
|
66 FUNC_LOG; |
|
67 |
|
68 } |
|
69 |
|
70 |
|
71 // --------------------------------------------------------------------------- |
|
72 // Symbian style NewL constructor |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 CESMRUrlParserPluginImpl* CESMRUrlParserPluginImpl::NewL() |
|
76 { |
|
77 FUNC_LOG; |
|
78 CESMRUrlParserPluginImpl* self = CESMRUrlParserPluginImpl::NewLC(); |
|
79 CleanupStack::Pop( self ); |
|
80 return self; |
|
81 } |
|
82 |
|
83 |
|
84 // --------------------------------------------------------------------------- |
|
85 // Symbian style NewLC constructor |
|
86 // --------------------------------------------------------------------------- |
|
87 // |
|
88 CESMRUrlParserPluginImpl* CESMRUrlParserPluginImpl::NewLC() |
|
89 { |
|
90 FUNC_LOG; |
|
91 CESMRUrlParserPluginImpl* self = new( ELeave ) CESMRUrlParserPluginImpl; |
|
92 CleanupStack::PushL( self ); |
|
93 self->ConstructL(); |
|
94 return self; |
|
95 } |
|
96 |
|
97 |
|
98 // --------------------------------------------------------------------------- |
|
99 // Destructor |
|
100 // --------------------------------------------------------------------------- |
|
101 // |
|
102 CESMRUrlParserPluginImpl::~CESMRUrlParserPluginImpl() |
|
103 { |
|
104 FUNC_LOG; |
|
105 iFile.Close(); |
|
106 iFs.Close(); |
|
107 } |
|
108 |
|
109 // --------------------------------------------------------------------------- |
|
110 // Finds location URL from given text input |
|
111 // --------------------------------------------------------------------------- |
|
112 // |
|
113 TInt CESMRUrlParserPluginImpl::FindLocationUrl( const TDesC& aText, |
|
114 TPtrC& aUrl ) |
|
115 |
|
116 { |
|
117 FUNC_LOG; |
|
118 TInt pos(0); |
|
119 TRAPD( error, pos = DoFindLocationUrlL( aText, aUrl ) ); |
|
120 if ( error != KErrNone ) |
|
121 { |
|
122 return error; |
|
123 } |
|
124 else |
|
125 { |
|
126 return pos; |
|
127 } |
|
128 } |
|
129 |
|
130 // --------------------------------------------------------------------------- |
|
131 // Finds location URL from given text input |
|
132 // --------------------------------------------------------------------------- |
|
133 // |
|
134 TInt CESMRUrlParserPluginImpl::DoFindLocationUrlL( // codescanner::intleaves |
|
135 const TDesC& aText, |
|
136 TPtrC& aUrl ) |
|
137 { |
|
138 FUNC_LOG; |
|
139 //If this parserplugin instance is not yet initialized |
|
140 //init it now |
|
141 if( !iIsInitialized ) |
|
142 { |
|
143 InitializeL(); |
|
144 } |
|
145 |
|
146 //Read basestring from resource file |
|
147 HBufC* baseString = ReadResourceStringLC(R_QTN_LOCATION_URL); |
|
148 |
|
149 //Seach if base string is found from aText |
|
150 TInt urlLocation = aText.Find(*baseString); |
|
151 CleanupStack::PopAndDestroy(baseString); |
|
152 if( urlLocation < KErrNone ) |
|
153 { |
|
154 User::Leave(KErrNotFound); |
|
155 } |
|
156 |
|
157 //Take out unnecessary part before URL and search if endmark (whitespace) |
|
158 //is found. If endmark is not found, |
|
159 //then all the rest of descriptor is part of URL |
|
160 HBufC* urlAndAfterBuf = aText.Mid( urlLocation ).AllocLC(); |
|
161 TInt urlAndAfterBufLength = urlAndAfterBuf->Length(); |
|
162 TInt urlEndmarkLocation = KErrNotFound; |
|
163 |
|
164 for( TInt i = 0; i < urlAndAfterBufLength; i++) |
|
165 { |
|
166 if( TChar( (*urlAndAfterBuf)[i] ).IsSpace() ) |
|
167 { |
|
168 urlEndmarkLocation = i; |
|
169 break; |
|
170 } |
|
171 } |
|
172 |
|
173 if( urlEndmarkLocation == KErrNotFound ) |
|
174 { |
|
175 urlEndmarkLocation = urlAndAfterBufLength; |
|
176 } |
|
177 |
|
178 //Take out part from beginning of URL to endmark |
|
179 HBufC* urlToEndMark = urlAndAfterBuf->Left( urlEndmarkLocation ).AllocLC(); |
|
180 |
|
181 //Now we should have only URL left, check with "sieve" that it is about in |
|
182 //right format |
|
183 HBufC* sieve = ReadResourceStringLC(R_QTN_LOCATION_SIEVE_URL); |
|
184 TInt sievedStartPoint = urlToEndMark->Match( *sieve ); |
|
185 |
|
186 CleanupStack::PopAndDestroy(sieve); |
|
187 CleanupStack::PopAndDestroy(urlToEndMark); |
|
188 CleanupStack::PopAndDestroy(urlAndAfterBuf); |
|
189 |
|
190 if( sievedStartPoint == KErrNotFound ) |
|
191 { |
|
192 User::Leave(KErrNotFound); |
|
193 } |
|
194 |
|
195 //Check that parameters are in right format |
|
196 TPtrC latValue; |
|
197 TPtrC lonValue; |
|
198 |
|
199 GetCoordinateParamValuesL( aText.Mid(urlLocation,urlEndmarkLocation), |
|
200 latValue, lonValue ); |
|
201 CheckCoordinateParamL( latValue ); |
|
202 CheckCoordinateParamL( lonValue ); |
|
203 |
|
204 //Set aURL to correspond URL part of aText and return with url position |
|
205 aUrl.Set( aText.Mid(urlLocation,urlEndmarkLocation)); |
|
206 return urlLocation; |
|
207 } |
|
208 |
|
209 |
|
210 // --------------------------------------------------------------------------- |
|
211 // Creates location URL from landmark object |
|
212 // --------------------------------------------------------------------------- |
|
213 // |
|
214 HBufC* CESMRUrlParserPluginImpl::CreateUrlFromLandmarkL( |
|
215 const CPosLandmark& aLandmark ) |
|
216 { |
|
217 FUNC_LOG; |
|
218 //If this parserplugin instance is not yet initialized |
|
219 //init it now |
|
220 if( !iIsInitialized ) |
|
221 { |
|
222 InitializeL(); |
|
223 } |
|
224 |
|
225 //Take longitude and latitude out of landmark |
|
226 TLocality position; |
|
227 User::LeaveIfError( aLandmark.GetPosition( position ) ); |
|
228 TReal64 latitude = position.Latitude(); |
|
229 TReal64 longitude = position.Longitude(); |
|
230 |
|
231 //Define TReal format type |
|
232 TRealFormat format( KCoordinateMaxLength ); |
|
233 format.iType = KRealFormatFixed; |
|
234 format.iPlaces = KNumberOfDecimals; |
|
235 format.iPoint = TChar(KDecimalSeparator); |
|
236 |
|
237 //Read strings from resourcefile |
|
238 HBufC* baseUrl = ReadResourceStringLC( R_QTN_LOCATION_URL ); |
|
239 HBufC* lat = ReadResourceStringLC( R_QTN_LOCATION_URL_LATITUDE ); |
|
240 HBufC* separator = ReadResourceStringLC( R_QTN_LOCATION_URL_PARAM_SEPARATOR ); |
|
241 HBufC* lon = ReadResourceStringLC( R_QTN_LOCATION_URL_LONGITUDE ); |
|
242 |
|
243 //Concatenate all strings and coordinates |
|
244 HBufC* url = HBufC::NewL( baseUrl->Length() + lat->Length() + separator->Length() |
|
245 + lon->Length() + format.iWidth + format.iWidth ); |
|
246 TPtr pointer = url->Des(); |
|
247 pointer.Append( *baseUrl ); |
|
248 pointer.Append( *lat ); |
|
249 pointer.AppendNum( latitude, format ); |
|
250 pointer.Append( *separator ); |
|
251 pointer.Append( *lon ); |
|
252 pointer.AppendNum( longitude, format ); |
|
253 |
|
254 CleanupStack::PopAndDestroy( lon ); |
|
255 CleanupStack::PopAndDestroy( separator ); |
|
256 CleanupStack::PopAndDestroy( lat ); |
|
257 CleanupStack::PopAndDestroy( baseUrl ); |
|
258 |
|
259 //Transfer ownership of url |
|
260 return url; |
|
261 } |
|
262 |
|
263 |
|
264 // --------------------------------------------------------------------------- |
|
265 // Creates landmark object from location URL |
|
266 // --------------------------------------------------------------------------- |
|
267 // |
|
268 CPosLandmark* CESMRUrlParserPluginImpl::CreateLandmarkFromUrlL( |
|
269 const TDesC& aUrl ) |
|
270 { |
|
271 FUNC_LOG; |
|
272 //If this parserplugin instance is not yet initialized |
|
273 //init it now |
|
274 if( !iIsInitialized ) |
|
275 { |
|
276 InitializeL(); |
|
277 } |
|
278 |
|
279 //Read sieve from resourcefile and check if URL matches the sieveformat |
|
280 HBufC* sieve = ReadResourceStringLC( R_QTN_LOCATION_SIEVE_URL ); |
|
281 TInt matchPos = User::LeaveIfError( aUrl.Match( *sieve ) ); |
|
282 CleanupStack::PopAndDestroy( sieve ); |
|
283 if( matchPos != 0 ) |
|
284 { |
|
285 //URL was found but is not int the beginning of desc |
|
286 User::Leave( KErrArgument ); |
|
287 } |
|
288 |
|
289 //Parse actual coordinate values out of url |
|
290 TPtrC latValue; |
|
291 TPtrC lonValue; |
|
292 GetCoordinateParamValuesL( aUrl, latValue, lonValue ); |
|
293 |
|
294 //Check that parameters are in right format |
|
295 CheckCoordinateParamL( latValue ); |
|
296 CheckCoordinateParamL( lonValue ); |
|
297 |
|
298 //Convert parameters to TReal values |
|
299 TLex lexConverter( latValue ); |
|
300 TReal64 realLatitude; |
|
301 lexConverter.Val( realLatitude ); |
|
302 |
|
303 lexConverter.Assign( lonValue ); |
|
304 TReal64 realLongitude; |
|
305 lexConverter.Val( realLongitude ); |
|
306 |
|
307 //Create landmark with coordinatevalues |
|
308 CPosLandmark* landmark = CPosLandmark::NewLC(); |
|
309 TLocality position; |
|
310 position.SetCoordinate( realLatitude, realLongitude ); |
|
311 landmark->SetPositionL( position ); |
|
312 CleanupStack::Pop( landmark ); |
|
313 //transfer ownership |
|
314 return landmark; |
|
315 } |
|
316 |
|
317 // ---------------------------------------------------------------------------- |
|
318 // CESMRUrlParserPluginImpl::LocateResourceFile |
|
319 // |
|
320 // For locating resource file |
|
321 // ---------------------------------------------------------------------------- |
|
322 // |
|
323 TInt CESMRUrlParserPluginImpl::LocateResourceFile( |
|
324 const TDesC& aResource, |
|
325 const TDesC& aPath, |
|
326 TFileName &aResourceFile, |
|
327 RFs* aFs ) |
|
328 { |
|
329 FUNC_LOG; |
|
330 TFindFile resourceFile( *aFs ); |
|
331 TInt err = resourceFile.FindByDir( |
|
332 aResource, |
|
333 aPath ); |
|
334 |
|
335 if ( KErrNone == err ) |
|
336 { |
|
337 aResourceFile.Copy( resourceFile.File() ); |
|
338 } |
|
339 else |
|
340 { |
|
341 const TChar KFileFormatDelim( '.' ); |
|
342 TFileName locResourceFile; |
|
343 |
|
344 TInt pos = aResource.LocateReverse( KFileFormatDelim ); |
|
345 if ( pos != KErrNotFound ) |
|
346 { |
|
347 locResourceFile.Copy( aResource.Mid(0, pos + 1) ); |
|
348 |
|
349 TInt language( User::Language() ); |
|
350 locResourceFile.AppendFormat( KResourceFileLocFormat, language ); |
|
351 |
|
352 TFindFile resourceFile( *aFs ); |
|
353 err = resourceFile.FindByDir( |
|
354 locResourceFile, |
|
355 aPath ); |
|
356 |
|
357 if ( KErrNone == err ) |
|
358 { |
|
359 aResourceFile.Copy( resourceFile.File() ); |
|
360 aResourceFile.Replace( |
|
361 aResourceFile.Length() - KResourceFormat().Length(), |
|
362 KResourceFormat().Length(), |
|
363 KResourceFormat() ); |
|
364 } |
|
365 } |
|
366 } |
|
367 |
|
368 |
|
369 return err; |
|
370 } |
|
371 |
|
372 // ---------------------------------------------------------------------------- |
|
373 // CESMRUrlParserPluginImpl::InitializeL |
|
374 // |
|
375 // Initializes resources |
|
376 // ---------------------------------------------------------------------------- |
|
377 // |
|
378 void CESMRUrlParserPluginImpl::InitializeL() |
|
379 { |
|
380 FUNC_LOG; |
|
381 if( iIsInitialized ) |
|
382 { |
|
383 return; |
|
384 } |
|
385 //Connect to RFs session |
|
386 User::LeaveIfError( iFs.Connect( KFileServerDefaultMessageSlots ) ); |
|
387 |
|
388 //Find and open resource file containing URL strings |
|
389 TFileName fileName; |
|
390 User::LeaveIfError( LocateResourceFile( KResourceName, |
|
391 KDC_RESOURCE_FILES_DIR, |
|
392 fileName, |
|
393 &iFs ) ); |
|
394 iFile.OpenL( iFs, fileName ); |
|
395 iFile.ConfirmSignatureL(); |
|
396 iIsInitialized = ETrue; |
|
397 } |
|
398 |
|
399 // ---------------------------------------------------------------------------- |
|
400 // CESMRUrlParserPluginImpl::ReadResourceStringLC |
|
401 // Reads resource string from specified id |
|
402 // ---------------------------------------------------------------------------- |
|
403 // |
|
404 HBufC* CESMRUrlParserPluginImpl::ReadResourceStringLC( TInt aResourceId ) |
|
405 { |
|
406 FUNC_LOG; |
|
407 HBufC8* string = iFile.AllocReadLC(aResourceId); |
|
408 iReader.SetBuffer(string); |
|
409 HBufC* stringBuffer = iReader.ReadTPtrC().AllocL(); |
|
410 CleanupStack::PopAndDestroy( string ); |
|
411 CleanupStack::PushL( stringBuffer ); |
|
412 //stringBuffer ownership is transfered |
|
413 return stringBuffer; |
|
414 } |
|
415 |
|
416 // ---------------------------------------------------------------------------- |
|
417 // CESMRUrlParserPluginImpl::CheckCoordinateParam |
|
418 // |
|
419 // Checks if coordinate parameter (lon or lat) is in correct format |
|
420 // Leaves if not |
|
421 // Correct form is: |
|
422 // -minus sign allowed only in first position |
|
423 // -only one decimalseparator sign allowed |
|
424 // -only digits allowed |
|
425 // -aParam length not allowed to be zero |
|
426 // ---------------------------------------------------------------------------- |
|
427 // |
|
428 void CESMRUrlParserPluginImpl::CheckCoordinateParamL( const TDesC& aParam ) |
|
429 { |
|
430 FUNC_LOG; |
|
431 if( aParam.Length() == 0 ) |
|
432 { |
|
433 User::Leave( KErrArgument ); |
|
434 } |
|
435 |
|
436 //check that aParam contains only digits and only one decimalseparator |
|
437 //and minus sign is first, if it exists |
|
438 TChar character; |
|
439 TBool decimalSeparatorFound = EFalse; |
|
440 TLex lex; |
|
441 lex.Assign( aParam ); |
|
442 const TUint KMinusSign = '-'; |
|
443 |
|
444 for( TInt i = 0; i < aParam.Length(); i++) |
|
445 { |
|
446 character = lex.Get(); |
|
447 //checks if first character is minus sign and continues if it is |
|
448 if( i == 0 && (TUint)character == KMinusSign) |
|
449 { |
|
450 continue; |
|
451 } |
|
452 //check that only one decimalseparator exists |
|
453 if( (TUint)character == KDecimalSeparator ) |
|
454 { |
|
455 if( decimalSeparatorFound ) |
|
456 { |
|
457 User::Leave( KErrArgument ); |
|
458 } |
|
459 else |
|
460 { |
|
461 decimalSeparatorFound = ETrue; |
|
462 } |
|
463 } |
|
464 //check that character is either digit or decimalseparator |
|
465 if( !( character.IsDigit() ) && (TUint)character != KDecimalSeparator ) |
|
466 { |
|
467 User::Leave( KErrArgument ); |
|
468 } |
|
469 } |
|
470 } |
|
471 |
|
472 // ---------------------------------------------------------------------------- |
|
473 // CESMRUrlParserPluginImpl::GetCoordinateParamValuesL |
|
474 // Returns longitude and latitude if found correctly |
|
475 // ---------------------------------------------------------------------------- |
|
476 // |
|
477 void CESMRUrlParserPluginImpl::GetCoordinateParamValuesL( const TDesC& aUrl, |
|
478 TPtrC& aLatitude, |
|
479 TPtrC& aLongitude ) |
|
480 { |
|
481 FUNC_LOG; |
|
482 //Read latitude and longitude strings from resourcefile |
|
483 HBufC* lat = ReadResourceStringLC( R_QTN_LOCATION_URL_LATITUDE ); |
|
484 HBufC* lon = ReadResourceStringLC( R_QTN_LOCATION_URL_LONGITUDE ); |
|
485 HBufC* separator = ReadResourceStringLC( R_QTN_LOCATION_URL_PARAM_SEPARATOR ); |
|
486 |
|
487 //Find out if lat and lon params and separator exists in aUrl |
|
488 TInt latPos = aUrl.Find( *lat ); |
|
489 TInt lonPos = aUrl.Find( *lon ); |
|
490 TInt separatorPos = aUrl.Find( *separator ); |
|
491 if( latPos == KErrNotFound || lonPos == KErrNotFound |
|
492 || separatorPos == KErrNotFound ) |
|
493 { |
|
494 User::Leave( KErrNotFound ); |
|
495 } |
|
496 |
|
497 //takes from aUrl parts with actual coordinate data |
|
498 aLatitude.Set( aUrl.Mid( latPos + lat->Length(), separatorPos - latPos - lat->Length() ) ); |
|
499 aLongitude.Set( aUrl.Right( aUrl.Length() - separatorPos - 1 - lon->Length() ) ); |
|
500 |
|
501 CleanupStack::PopAndDestroy( separator ); |
|
502 CleanupStack::PopAndDestroy( lon ); |
|
503 CleanupStack::PopAndDestroy( lat ); |
|
504 } |
|
505 //EOF |
|
506 |