|
1 /* |
|
2 * Copyright (c) 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: Location API category manager |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INTERNAL INCLUDES |
|
20 #include "clapicategorymanager.h" |
|
21 #include "mlapilandmarkstore.h" |
|
22 #include "mlapilmdatabaseeventnotifier.h" |
|
23 #include "logger.h" |
|
24 // EXTERNAL INCLUDES |
|
25 #include <EPos_CPosLmCategoryManager.h> |
|
26 #include <EPos_CPosLandmarkDatabase.h> |
|
27 |
|
28 // UNNAMED LOCAL NAMESPACE |
|
29 namespace |
|
30 { |
|
31 // Minimum usage of the native landmark database. If the usage drops |
|
32 // below this level, the database will be compacted to avoid that the |
|
33 // Landmark Server would do that which eventually locks the database |
|
34 const TReal KLAPIMinCompactUsage = 0.7; |
|
35 } |
|
36 |
|
37 // --------------------------------------------------------------------------- |
|
38 // CLAPICategoryManager::NewL |
|
39 // --------------------------------------------------------------------------- |
|
40 // |
|
41 CLAPICategoryManager* CLAPICategoryManager::NewL(const TCtorParams& aParams) |
|
42 { |
|
43 JELOG2(EJavaLocation); |
|
44 CLAPICategoryManager* self = CLAPICategoryManager::NewLC(aParams); |
|
45 CleanupStack::Pop(self); |
|
46 return self; |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------------------------- |
|
50 // CLAPICategoryManager::NewLC |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 CLAPICategoryManager* CLAPICategoryManager::NewLC(const TCtorParams& aParams) |
|
54 { |
|
55 JELOG2(EJavaLocation); |
|
56 CLAPICategoryManager* self = new(ELeave) CLAPICategoryManager(aParams); |
|
57 CleanupStack::PushL(self); |
|
58 self->ConstructL(); |
|
59 return self; |
|
60 } |
|
61 |
|
62 // --------------------------------------------------------------------------- |
|
63 // CLAPICategoryManager::~CLAPICategoryManager |
|
64 // --------------------------------------------------------------------------- |
|
65 // |
|
66 CLAPICategoryManager::~CLAPICategoryManager() |
|
67 { |
|
68 JELOG2(EJavaLocation); |
|
69 if (iEventNotifier) |
|
70 { |
|
71 iEventNotifier->RemoveObserver(this); |
|
72 } |
|
73 iCategoryIds.Close(); |
|
74 delete iCategoryNames; |
|
75 delete iCategoryManager; |
|
76 } |
|
77 |
|
78 // --------------------------------------------------------------------------- |
|
79 // CLAPICategoryManager::ListCategoriesL |
|
80 // --------------------------------------------------------------------------- |
|
81 // |
|
82 const CDesCArray& CLAPICategoryManager::ListCategoriesL() |
|
83 { |
|
84 JELOG2(EJavaLocation); |
|
85 // Check if Event listening is supported so it is not necessary to |
|
86 // refresh the whole list of categories in the database. Note that |
|
87 // if the amount of categories is huge, this operation may take some |
|
88 // time so it is always better to track database events if possible |
|
89 if (!iEventNotifier || !iEventNotifier->IsEventListeningSupported()) |
|
90 { |
|
91 RefreshCategoryListL(); |
|
92 } |
|
93 // Return a reference to the current list of categories |
|
94 return *iCategoryNames; |
|
95 } |
|
96 |
|
97 // --------------------------------------------------------------------------- |
|
98 // CLAPICategoryManager::AddCategoryL |
|
99 // --------------------------------------------------------------------------- |
|
100 // |
|
101 void CLAPICategoryManager::AddCategoryL(const TDesC& aName) |
|
102 { |
|
103 JELOG2(EJavaLocation); |
|
104 CPosLandmarkCategory* category = CPosLandmarkCategory::NewLC(); |
|
105 // The native Landmarks API supports only specific length category names |
|
106 // There is no other option but to truncat the rest of the name off |
|
107 TPtrC name = aName.Left(KPosLmMaxCategoryNameLength); |
|
108 category->SetCategoryNameL(name); |
|
109 // Manager does not take the ownership |
|
110 TPosLmItemId id = iCategoryManager->AddCategoryL(*category); |
|
111 CleanupStack::PopAndDestroy(category); |
|
112 // Store the information about the category if it was successfully added |
|
113 if (id != KPosLmNullItemId) |
|
114 { |
|
115 // Store the new id and category name |
|
116 TInt position = iCategoryNames->InsertIsqL(name, ECmpCollated); |
|
117 // Insert the element to the same place in the id array |
|
118 TInt error = iCategoryIds.Insert(id, position); |
|
119 if (error != KErrNone) |
|
120 { |
|
121 // The arrays must be in sync so remove the added id |
|
122 iCategoryNames->Delete(position); |
|
123 User::Leave(error); |
|
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 // --------------------------------------------------------------------------- |
|
129 // CLAPICategoryManager::RemoveCategoryL |
|
130 // --------------------------------------------------------------------------- |
|
131 // |
|
132 void CLAPICategoryManager::RemoveCategoryL(const TDesC& aName) |
|
133 { |
|
134 JELOG2(EJavaLocation); |
|
135 // Remove the category if it exists. If not, this function call is |
|
136 // silently discareded as specified in the Location API specification |
|
137 TInt index = FindCategory(aName); |
|
138 if (index != KErrNotFound) |
|
139 { |
|
140 ExecuteAndDeleteLD(iCategoryManager->RemoveCategoryL( |
|
141 iCategoryIds[index])); |
|
142 // Remove the category from the list |
|
143 iCategoryIds.Remove(index); |
|
144 iCategoryNames->Delete(index); |
|
145 } |
|
146 } |
|
147 |
|
148 // --------------------------------------------------------------------------- |
|
149 // CLAPICategoryManager::AddLandmarkToCategoryL |
|
150 // --------------------------------------------------------------------------- |
|
151 // |
|
152 void CLAPICategoryManager::AddLandmarkToCategoryL(MLAPILandmark& aLandmark, |
|
153 const TDesC& aCategoryName) |
|
154 { |
|
155 JELOG2(EJavaLocation); |
|
156 TInt index = FindCategory(aCategoryName); |
|
157 // Leave if the requested category was not found |
|
158 __ASSERT_ALWAYS(index != KErrNotFound, User::Leave(KErrArgument)); |
|
159 TLAPIItemId categoryId = iCategoryIds[index]; |
|
160 |
|
161 // Load the landmark information from the database and update categories |
|
162 CPosLandmark* landmark = iLandmarkDatabase.ReadLandmarkLC(aLandmark.Id()); |
|
163 landmark->AddCategoryL(categoryId); |
|
164 |
|
165 // Currently, the passed landmark does not have to know to which categories |
|
166 // it belongs to so there is no need to update the information in the |
|
167 // landmark. This will change in Location API v2.0 |
|
168 |
|
169 // Update the existing landmark into the database. |
|
170 iLandmarkDatabase.UpdateLandmarkL(*landmark); |
|
171 CleanupStack::PopAndDestroy(landmark); |
|
172 } |
|
173 |
|
174 // --------------------------------------------------------------------------- |
|
175 // CLAPICategoryManager::RemoveLandmarkFromCategoryL |
|
176 // --------------------------------------------------------------------------- |
|
177 // |
|
178 void CLAPICategoryManager::RemoveLandmarkFromCategoryL( |
|
179 MLAPILandmark& aLandmark, const TDesC& aCategoryName) |
|
180 { |
|
181 JELOG2(EJavaLocation); |
|
182 TInt index = FindCategory(aCategoryName); |
|
183 // The function call is silently discarded if the specified category |
|
184 // does not exist in the landmark store |
|
185 if (index != KErrNotFound) |
|
186 { |
|
187 TLAPIItemId categoryId = iCategoryIds[index]; |
|
188 // Load the landmark information from the database and update categories |
|
189 CPosLandmark* lm = iLandmarkDatabase.ReadLandmarkLC(aLandmark.Id()); |
|
190 lm->RemoveCategory(categoryId); |
|
191 // Update the existing landmark into the database. |
|
192 iLandmarkDatabase.UpdateLandmarkL(*lm); |
|
193 CleanupStack::PopAndDestroy(lm); |
|
194 |
|
195 // Currently, the passed landmark does not have to know to which |
|
196 // categories it belongs to so there is no need to update the information |
|
197 // in the landmark. This will change in Location API v2.0 |
|
198 } |
|
199 } |
|
200 |
|
201 // --------------------------------------------------------------------------- |
|
202 // CLAPICategoryManager::NotifyEvent |
|
203 // --------------------------------------------------------------------------- |
|
204 // |
|
205 void CLAPICategoryManager::NotifyEvent(TPosLmEventType& aEventType, |
|
206 TPosLmItemId& aItemId) |
|
207 { |
|
208 JELOG2(EJavaLocation); |
|
209 // Handle the change. The error must be ignored because there is no way |
|
210 // to recover from out of memory situation |
|
211 TRAP_IGNORE(HandleCategoryChangeL(aEventType, aItemId)); |
|
212 } |
|
213 |
|
214 // --------------------------------------------------------------------------- |
|
215 // CLAPICategoryManager::ConstructL |
|
216 // --------------------------------------------------------------------------- |
|
217 // |
|
218 void CLAPICategoryManager::ConstructL() |
|
219 { |
|
220 JELOG2(EJavaLocation); |
|
221 iCategoryManager = CPosLmCategoryManager::NewL(iLandmarkDatabase); |
|
222 iCategoryNames = new(ELeave) CDesCArrayFlat(5); |
|
223 // Refresh the category list for the first time here. Note that this |
|
224 // is called only here if the event notifier supports listening changes |
|
225 RefreshCategoryListL(); |
|
226 // Start listening category changes if it is supported by the event notifier |
|
227 if (iEventNotifier && iEventNotifier->IsEventListeningSupported()) |
|
228 { |
|
229 iEventNotifier->AddObserverL(this); |
|
230 } |
|
231 } |
|
232 |
|
233 // --------------------------------------------------------------------------- |
|
234 // CLAPICategoryManager::CLAPICategoryManager |
|
235 // --------------------------------------------------------------------------- |
|
236 // |
|
237 CLAPICategoryManager::CLAPICategoryManager(const TCtorParams& aParams) : |
|
238 iLandmarkDatabase(*(aParams.iLandmarkDatabase)), iEventNotifier( |
|
239 aParams.iEventNotifier) |
|
240 { |
|
241 JELOG2(EJavaLocation); |
|
242 iLandmarkDatabasePtr = aParams.iLandmarkDatabase; |
|
243 } |
|
244 |
|
245 // --------------------------------------------------------------------------- |
|
246 // CLAPICategoryManager::RefreshCategoryListL |
|
247 // --------------------------------------------------------------------------- |
|
248 // |
|
249 void CLAPICategoryManager::RefreshCategoryListL() |
|
250 { |
|
251 JELOG2(EJavaLocation); |
|
252 // Reset any existing category information |
|
253 iCategoryIds.Reset(); |
|
254 iCategoryNames->Reset(); |
|
255 // Initialize the list of current categories. The order is handled internally |
|
256 CPosLmItemIterator* iterator = iCategoryManager->CategoryIteratorL(); |
|
257 CleanupStack::PushL(iterator); |
|
258 // Do not iterate categories if there is nothing to iterate |
|
259 if (iterator->NumOfItemsL() == 0) |
|
260 { |
|
261 CleanupStack::PopAndDestroy(iterator); |
|
262 return; |
|
263 } |
|
264 RArray<TPosLmItemId> ids; |
|
265 CleanupClosePushL(ids); |
|
266 // Get the prepared category identifiers from the iterator |
|
267 iterator->GetItemIdsL(ids, 0, iterator->NumOfItemsL()); |
|
268 |
|
269 // Read all available categories |
|
270 TInt itemCount = ids.Count(); |
|
271 for (TInt i = 0; i < itemCount; i++) |
|
272 { |
|
273 TPosLmItemId id = ids[i]; |
|
274 // Read category information from the category manager |
|
275 DoReadCategoryL(id); |
|
276 } |
|
277 CleanupStack::PopAndDestroy(2, iterator); // The id array is the second |
|
278 } |
|
279 |
|
280 // --------------------------------------------------------------------------- |
|
281 // CLAPICategoryManager::DoReadCategoryL |
|
282 // --------------------------------------------------------------------------- |
|
283 // |
|
284 void CLAPICategoryManager::DoReadCategoryL(TPosLmItemId& aItemId) |
|
285 { |
|
286 JELOG2(EJavaLocation); |
|
287 // Read category information from the category manager |
|
288 CPosLandmarkCategory* cat = iCategoryManager->ReadCategoryLC(aItemId); |
|
289 // Get the information from the category |
|
290 TPtrC namePtr; |
|
291 User::LeaveIfError(cat->GetCategoryName(namePtr)); |
|
292 // Add the category to the name array and update the id array also |
|
293 TInt position = iCategoryNames->InsertIsqL(namePtr, ECmpCollated); |
|
294 // Insert new id or append if the array |
|
295 TInt error = iCategoryIds.Insert(aItemId, position); |
|
296 if (error != KErrNone) |
|
297 { |
|
298 // The arrays must be in sync so remove the added id |
|
299 iCategoryNames->Delete(position); |
|
300 User::Leave(error); |
|
301 } |
|
302 CleanupStack::PopAndDestroy(cat); |
|
303 } |
|
304 |
|
305 // --------------------------------------------------------------------------- |
|
306 // CLAPICategoryManager::FindCategory |
|
307 // --------------------------------------------------------------------------- |
|
308 // |
|
309 TInt CLAPICategoryManager::FindCategory(const TDesC& aName) const |
|
310 { |
|
311 JELOG2(EJavaLocation); |
|
312 TInt pos = 0; |
|
313 TInt found = iCategoryNames->FindIsq(aName, pos, ECmpCollated); |
|
314 // Return the index of the category name. KErrNotFound if the category |
|
315 // was not found from the category names list |
|
316 return found == 0 ? pos : KErrNotFound; |
|
317 } |
|
318 |
|
319 void CLAPICategoryManager::CompactIfNeededL() |
|
320 { |
|
321 JELOG2(EJavaLocation); |
|
322 |
|
323 LOG(EJavaLocation, EInfo, "CLAPICategoryManager::CompactIfNeededL "); |
|
324 |
|
325 // Check if the database has been closed |
|
326 __ASSERT_ALWAYS(iLandmarkDatabasePtr, User::Leave(KErrSessionClosed)); |
|
327 |
|
328 CPosLandmarkDatabase::TSize databaseSize = iLandmarkDatabasePtr->SizeL(); |
|
329 |
|
330 // the minimum compact limit. This prevents that Landmarks Server will |
|
331 // not do this operation and lock the database |
|
332 if (databaseSize.iUsage < KLAPIMinCompactUsage) |
|
333 { |
|
334 ExecuteAndDeleteLD(iLandmarkDatabasePtr->CompactL()); |
|
335 } |
|
336 } |
|
337 |
|
338 // --------------------------------------------------------------------------- |
|
339 // CLAPICategoryManager::HandleCategoryChangeL |
|
340 // --------------------------------------------------------------------------- |
|
341 // |
|
342 void CLAPICategoryManager::HandleCategoryChangeL(TPosLmEventType& aEventType, |
|
343 TPosLmItemId& aItemId) |
|
344 { |
|
345 JELOG2(EJavaLocation); |
|
346 LOG1(EJavaLocation, EInfo, |
|
347 "CLAPICategoryManager::HandleCategoryChangeL - event %d", |
|
348 aEventType); |
|
349 // Check the type of the event and perform the correct operation |
|
350 switch (aEventType) |
|
351 { |
|
352 case EPosLmEventCategoryUnknownChanges: |
|
353 { |
|
354 // Unknown changes. This may occur when multiple categories are |
|
355 // deleted at once. The whole list needs to be refreshed |
|
356 RefreshCategoryListL(); |
|
357 break; |
|
358 } |
|
359 case EPosLmEventCategoryCreated: // Fallthrough |
|
360 case EPosLmEventCategoryUpdated: // Fallthrough |
|
361 case EPosLmEventCategoryDeleted: |
|
362 { |
|
363 // Update and delete events require deleting the existing category |
|
364 if (aEventType != EPosLmEventCategoryCreated) |
|
365 { |
|
366 TInt index = iCategoryIds.Find(aItemId); |
|
367 if (index != KErrNotFound) |
|
368 { |
|
369 iCategoryIds.Remove(index); |
|
370 iCategoryNames->Delete(index); |
|
371 } |
|
372 } |
|
373 |
|
374 // Read the newly added category from the database and add it to the |
|
375 // category name and identifier list. Delete event will not add new |
|
376 if (aEventType != EPosLmEventCategoryDeleted) |
|
377 { |
|
378 DoReadCategoryL(aItemId); |
|
379 } |
|
380 |
|
381 if (aEventType == EPosLmEventCategoryDeleted) |
|
382 { |
|
383 // Compact the store if needed |
|
384 CompactIfNeededL(); |
|
385 } |
|
386 break; |
|
387 } |
|
388 default: |
|
389 { |
|
390 // Other events are simply ignored |
|
391 break; |
|
392 } |
|
393 } |
|
394 } |
|
395 |
|
396 // End of file |