|
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 "cesmrurlparserpluginimpl.h" |
|
21 #include "cmrurlparserextension.h" |
|
22 |
|
23 #include <EPos_CPosLandmark.h> |
|
24 #include <EPos_CPosLandmarkEncoder.h> |
|
25 #include <EPos_CPosLandmarkParser.h> |
|
26 #include <EPos_CPosLmOperation.h> |
|
27 #include <lbsposition.h> |
|
28 #include <escapeutils.h> |
|
29 #include <finditemengine.h> |
|
30 #include <calentry.h> |
|
31 |
|
32 #include "emailtrace.h" |
|
33 |
|
34 namespace |
|
35 { |
|
36 _LIT8( KLandmarkUrlMimeType, "maps.ovi.com" ); |
|
37 } |
|
38 |
|
39 // ======== MEMBER FUNCTIONS ======== |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // CESMRUrlParserPluginImpl::CESMRUrlParserPluginImpl |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 CESMRUrlParserPluginImpl::CESMRUrlParserPluginImpl() |
|
46 { |
|
47 FUNC_LOG; |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // CESMRUrlParserPluginImpl::NewL |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CESMRUrlParserPluginImpl* CESMRUrlParserPluginImpl::NewL() |
|
55 { |
|
56 FUNC_LOG; |
|
57 |
|
58 CESMRUrlParserPluginImpl* self = CESMRUrlParserPluginImpl::NewLC(); |
|
59 CleanupStack::Pop( self ); |
|
60 return self; |
|
61 } |
|
62 |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // CESMRUrlParserPluginImpl::NewLC |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 CESMRUrlParserPluginImpl* CESMRUrlParserPluginImpl::NewLC() |
|
69 { |
|
70 FUNC_LOG; |
|
71 |
|
72 CESMRUrlParserPluginImpl* self = new( ELeave ) CESMRUrlParserPluginImpl; |
|
73 CleanupStack::PushL( self ); |
|
74 return self; |
|
75 } |
|
76 |
|
77 |
|
78 // --------------------------------------------------------------------------- |
|
79 // CESMRUrlParserPluginImpl::~CESMRUrlParserPluginImpl |
|
80 // --------------------------------------------------------------------------- |
|
81 // |
|
82 CESMRUrlParserPluginImpl::~CESMRUrlParserPluginImpl() |
|
83 { |
|
84 FUNC_LOG; |
|
85 |
|
86 delete iParser; |
|
87 delete iEncoder; |
|
88 delete iExtension; |
|
89 |
|
90 ReleaseLandmarkResources(); |
|
91 } |
|
92 |
|
93 // --------------------------------------------------------------------------- |
|
94 // CESMRUrlParserPluginImpl::FindLocationUrl |
|
95 // Finds location URL from given text input |
|
96 // --------------------------------------------------------------------------- |
|
97 // |
|
98 TInt CESMRUrlParserPluginImpl::FindLocationUrl( |
|
99 const TDesC& aText, |
|
100 TPtrC& aUrl ) |
|
101 |
|
102 { |
|
103 FUNC_LOG; |
|
104 |
|
105 TInt pos(0); |
|
106 TRAPD( error, DoFindLocationUrlL( aText, pos, aUrl ) ); |
|
107 if ( error != KErrNone ) |
|
108 { |
|
109 return error; |
|
110 } |
|
111 else |
|
112 { |
|
113 return pos; |
|
114 } |
|
115 } |
|
116 |
|
117 // --------------------------------------------------------------------------- |
|
118 // CESMRUrlParserPluginImpl::CreateUrlFromLandmarkL |
|
119 // Creates location URL from landmark object |
|
120 // --------------------------------------------------------------------------- |
|
121 // |
|
122 HBufC* CESMRUrlParserPluginImpl::CreateUrlFromLandmarkL( |
|
123 const CPosLandmark& aLandmark ) |
|
124 { |
|
125 FUNC_LOG; |
|
126 |
|
127 CPosLandmarkEncoder& encoder = InitializeEncoderL(); |
|
128 CBufBase* buffer = encoder.SetUseOutputBufferL(); |
|
129 CleanupStack::PushL( buffer ); |
|
130 encoder.AddLandmarkL( aLandmark ); |
|
131 CPosLmOperation* operation = encoder.FinalizeEncodingL(); |
|
132 ExecuteAndDeleteLD( operation ); |
|
133 |
|
134 // Convert URL to Unicode and escape encode non-ASCII characters |
|
135 HBufC* unicode = EscapeUtils::ConvertToUnicodeFromUtf8L( buffer->Ptr( 0 ) ); |
|
136 CleanupStack::PushL( unicode ); |
|
137 HBufC* url = EscapeUtils::EscapeEncodeL( *unicode, EscapeUtils::EEscapeNormal ); |
|
138 |
|
139 CleanupStack::PopAndDestroy( 2, buffer ); // unicode |
|
140 |
|
141 //Transfer ownership of url |
|
142 return url; |
|
143 } |
|
144 |
|
145 |
|
146 // --------------------------------------------------------------------------- |
|
147 // CESMRUrlParserPluginImpl::CreateLandmarkFromUrlL |
|
148 // Creates landmark object from location URL |
|
149 // --------------------------------------------------------------------------- |
|
150 // |
|
151 CPosLandmark* CESMRUrlParserPluginImpl::CreateLandmarkFromUrlL( |
|
152 const TDesC& aUrl ) |
|
153 { |
|
154 FUNC_LOG; |
|
155 |
|
156 CPosLandmark* landmark = NULL; |
|
157 |
|
158 // Try to create landmark using landmark parser |
|
159 TRAPD( error, landmark = CreateLandmarkFromUrlInternalL( aUrl ) ); |
|
160 |
|
161 if ( error ) |
|
162 { |
|
163 // Try extension |
|
164 landmark = ExtensionL().CreateLandmarkFromUrlL( aUrl ); |
|
165 } |
|
166 |
|
167 //transfer ownership |
|
168 return landmark; |
|
169 } |
|
170 |
|
171 // ---------------------------------------------------------------------------- |
|
172 // CESMRUrlParserPluginImpl::CreateGeoValueLC |
|
173 // Converts URL to Calendar GEO property |
|
174 // ---------------------------------------------------------------------------- |
|
175 // |
|
176 CCalGeoValue* CESMRUrlParserPluginImpl::CreateGeoValueLC( const TDesC& aUrl ) |
|
177 { |
|
178 FUNC_LOG; |
|
179 |
|
180 // Convert URL to landmark |
|
181 CPosLandmark* landmark = CreateLandmarkFromUrlL( aUrl ); |
|
182 CleanupStack::PushL( landmark ); |
|
183 |
|
184 // Get position data from landmark |
|
185 TLocality position; |
|
186 User::LeaveIfError( landmark->GetPosition( position ) ); |
|
187 CleanupStack::PopAndDestroy( landmark ); |
|
188 |
|
189 // Convert position data to GEO value |
|
190 CCalGeoValue* geoVal = CCalGeoValue::NewL(); |
|
191 CleanupStack::PushL( geoVal ); |
|
192 geoVal->SetLatLongL( position.Latitude(), position.Longitude() ); |
|
193 |
|
194 return geoVal; |
|
195 } |
|
196 |
|
197 // ---------------------------------------------------------------------------- |
|
198 // CESMRUrlParserPluginImpl::InitializeParserL |
|
199 // ---------------------------------------------------------------------------- |
|
200 // |
|
201 CPosLandmarkParser& CESMRUrlParserPluginImpl::InitializeParserL() |
|
202 { |
|
203 FUNC_LOG; |
|
204 |
|
205 if ( !iParser ) |
|
206 { |
|
207 iParser = CPosLandmarkParser::NewL( KLandmarkUrlMimeType ); |
|
208 } |
|
209 |
|
210 return *iParser; |
|
211 } |
|
212 |
|
213 // ---------------------------------------------------------------------------- |
|
214 // CESMRUrlParserPluginImpl::InitializeEncoderL |
|
215 // ---------------------------------------------------------------------------- |
|
216 // |
|
217 CPosLandmarkEncoder& CESMRUrlParserPluginImpl::InitializeEncoderL() |
|
218 { |
|
219 FUNC_LOG; |
|
220 |
|
221 delete iEncoder; |
|
222 iEncoder = NULL; |
|
223 iEncoder = CPosLandmarkEncoder::NewL( KLandmarkUrlMimeType ); |
|
224 return *iEncoder; |
|
225 } |
|
226 |
|
227 // --------------------------------------------------------------------------- |
|
228 // CESMRUrlParserPluginImpl::DoFindLocationUrlL |
|
229 // Finds location URL from given text input |
|
230 // --------------------------------------------------------------------------- |
|
231 // |
|
232 |
|
233 void CESMRUrlParserPluginImpl::DoFindLocationUrlL( |
|
234 const TDesC& aText, |
|
235 TInt& aPos, |
|
236 TPtrC& aUrl ) |
|
237 { |
|
238 FUNC_LOG; |
|
239 |
|
240 aPos = KErrNotFound; |
|
241 aUrl.Set( KNullDesC ); |
|
242 |
|
243 // Search URL from given text |
|
244 CFindItemEngine* itemEngine = CFindItemEngine::NewL( |
|
245 aText, |
|
246 CFindItemEngine::EFindItemSearchURLBin ); |
|
247 CleanupStack::PushL (itemEngine ); |
|
248 |
|
249 // For each found item |
|
250 CFindItemEngine::SFoundItem item; |
|
251 itemEngine->Item( item ); |
|
252 |
|
253 TInt itemCount = itemEngine->ItemCount(); |
|
254 for ( TInt i = 0; i < itemCount; ++i ) |
|
255 { |
|
256 TPtrC url = aText.Mid( item.iStartPos, item.iLength ); |
|
257 |
|
258 CPosLandmark* landmark = NULL; |
|
259 TRAPD( error, landmark = CreateLandmarkFromUrlL( url ); ) |
|
260 delete landmark; |
|
261 |
|
262 if ( !error ) // Location url found |
|
263 { |
|
264 aPos = item.iStartPos; |
|
265 aUrl.Set( url ); |
|
266 // Stop iteration |
|
267 break; |
|
268 } |
|
269 else |
|
270 { |
|
271 itemEngine->NextItem( item ); |
|
272 } |
|
273 } |
|
274 |
|
275 CleanupStack::PopAndDestroy( itemEngine ); |
|
276 |
|
277 if ( aPos < 0 ) |
|
278 { |
|
279 ExtensionL().FindLocationUrlL( aText, aUrl, aPos ); |
|
280 } |
|
281 } |
|
282 |
|
283 // --------------------------------------------------------------------------- |
|
284 // CESMRUrlParserPluginImpl::ExtensionL |
|
285 // --------------------------------------------------------------------------- |
|
286 // |
|
287 CMRUrlParserExtension& CESMRUrlParserPluginImpl::ExtensionL() |
|
288 { |
|
289 FUNC_LOG; |
|
290 |
|
291 if ( !iExtension ) |
|
292 { |
|
293 iExtension = CMRUrlParserExtension::NewL(); |
|
294 } |
|
295 |
|
296 return *iExtension; |
|
297 } |
|
298 |
|
299 // --------------------------------------------------------------------------- |
|
300 // CESMRUrlParserPluginImpl::CreateLandmarkFromUrlInternalL |
|
301 // Creates landmark object from location URL |
|
302 // --------------------------------------------------------------------------- |
|
303 // |
|
304 CPosLandmark* CESMRUrlParserPluginImpl::CreateLandmarkFromUrlInternalL( |
|
305 const TDesC& aUrl ) |
|
306 { |
|
307 FUNC_LOG; |
|
308 |
|
309 // Decode URL and convert it to UTF-8 format |
|
310 HBufC* decodedUrl = EscapeUtils::EscapeDecodeL( aUrl ); |
|
311 CleanupStack::PushL( decodedUrl ); |
|
312 HBufC8* url = EscapeUtils::ConvertFromUnicodeToUtf8L( *decodedUrl ); |
|
313 CleanupStack::PopAndDestroy( decodedUrl ); |
|
314 CleanupStack::PushL( url ); |
|
315 |
|
316 // Create landmark using correct parser |
|
317 CPosLandmarkParser& parser = InitializeParserL(); |
|
318 parser.SetInputBuffer( *url ); |
|
319 CPosLmOperation* operation = parser.ParseContentL(); |
|
320 ExecuteAndDeleteLD( operation ); |
|
321 CPosLandmark* landmark = parser.LandmarkLC(); |
|
322 CleanupStack::Pop( landmark ); |
|
323 CleanupStack::PopAndDestroy( url ); |
|
324 |
|
325 //transfer ownership |
|
326 return landmark; |
|
327 } |
|
328 |
|
329 //EOF |
|
330 |