|
1 /* |
|
2 * Copyright (c) 2007-2009 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 history manager class implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "emailtrace.h" |
|
19 #include "cesmrlocationhistorymanager.h" |
|
20 |
|
21 #include <centralrepository.h> |
|
22 #include <s32mem.h> |
|
23 |
|
24 #include "mruiprivatecrkeys.h" |
|
25 #include "cesmrlocationhistoryitemfactory.h" |
|
26 #include "cesmrlocationhistoryitem.h" |
|
27 |
|
28 // ======== MEMBER FUNCTIONS ======== |
|
29 |
|
30 // ----------------------------------------------------------------------------- |
|
31 // CESMRLocationHistoryManager::NewL |
|
32 // ----------------------------------------------------------------------------- |
|
33 // |
|
34 CESMRLocationHistoryManager* CESMRLocationHistoryManager::NewL() |
|
35 { |
|
36 FUNC_LOG; |
|
37 CESMRLocationHistoryManager* object = CESMRLocationHistoryManager::NewLC(); |
|
38 CleanupStack::Pop ( object ); |
|
39 return object; |
|
40 } |
|
41 |
|
42 // ----------------------------------------------------------------------------- |
|
43 // CESMRLocationHistoryManager::NewLC |
|
44 // ----------------------------------------------------------------------------- |
|
45 // |
|
46 CESMRLocationHistoryManager* CESMRLocationHistoryManager::NewLC() |
|
47 { |
|
48 FUNC_LOG; |
|
49 CESMRLocationHistoryManager* object = new (ELeave) CESMRLocationHistoryManager(); |
|
50 CleanupStack::PushL( object ); |
|
51 object->ConstructL(); |
|
52 return object; |
|
53 } |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // CESMRLocationHistoryManager::~CESMRLocationHistoryManager |
|
57 // ----------------------------------------------------------------------------- |
|
58 // |
|
59 CESMRLocationHistoryManager::~CESMRLocationHistoryManager( ) |
|
60 { |
|
61 FUNC_LOG; |
|
62 delete iFactory; |
|
63 if ( iNotifyHandler ) |
|
64 { |
|
65 iNotifyHandler->StopListening(); |
|
66 } |
|
67 delete iNotifyHandler; |
|
68 delete iCRSession; |
|
69 |
|
70 iOrder.Reset(); |
|
71 iHistoryList.ResetAndDestroy(); |
|
72 } |
|
73 |
|
74 // ----------------------------------------------------------------------------- |
|
75 // CESMRLocationHistoryManager::UpdateLocationHistoryL |
|
76 // ----------------------------------------------------------------------------- |
|
77 // |
|
78 void CESMRLocationHistoryManager::UpdateLocationHistoryL( const MESMRLocationHistoryItem* aItem ) |
|
79 { |
|
80 FUNC_LOG; |
|
81 // Update order list |
|
82 TBool itemWriteNeeded( ETrue ); |
|
83 TInt index(-1); |
|
84 for( TInt i=0; i<iHistoryList.Count(); i++ ) |
|
85 { |
|
86 // check if the history item already exists or there is one with identical address |
|
87 if(( aItem->Id() == iHistoryList[i]->Id() ) || |
|
88 ( aItem->Address().Compare( iHistoryList[i]->Address() ) == 0 )) |
|
89 { |
|
90 index = i; |
|
91 for( TInt j=0; j<iOrder.Count(); j++ ) |
|
92 { |
|
93 if( i == iOrder[j] ) |
|
94 { |
|
95 itemWriteNeeded = EFalse; |
|
96 iOrder.Remove( j ); |
|
97 iOrder.Insert( index, 0 ); |
|
98 // If the received item is new, delete it as it's not used |
|
99 if( aItem->Id() != iHistoryList[i]->Id() ) |
|
100 { |
|
101 delete aItem; |
|
102 } |
|
103 break; |
|
104 } |
|
105 } |
|
106 } |
|
107 if( !itemWriteNeeded ) |
|
108 { |
|
109 break; |
|
110 } |
|
111 } |
|
112 |
|
113 iNotifyHandler->StopListening(); |
|
114 if( index == -1 ) |
|
115 { |
|
116 // new history item |
|
117 if( iOrder.Count() == iMaxCount ) |
|
118 { |
|
119 // max amount of history already stored, replace the oldest item |
|
120 index = iOrder[iOrder.Count() - 1]; |
|
121 MESMRLocationHistoryItem* item = iHistoryList[index]; |
|
122 iHistoryList.Remove( index ); |
|
123 delete item; |
|
124 iHistoryList.Insert( aItem, index ); |
|
125 iOrder.Remove( iOrder.Count() - 1 ); |
|
126 iOrder.Insert( index, 0 ); |
|
127 } |
|
128 else |
|
129 { |
|
130 // add new history item |
|
131 index = iOrder.Count(); |
|
132 iOrder.Insert( index, 0 ); |
|
133 iHistoryList.Append( aItem ); |
|
134 // update current item count cenrep key |
|
135 User::LeaveIfError( iCRSession->Set( KESMRUILocationHistoryItemCount, iOrder.Count() )); |
|
136 } |
|
137 } |
|
138 |
|
139 // update order cenrep key |
|
140 HBufC8* orderBuf = HBufC8::NewLC( NCentralRepositoryConstants::KMaxBinaryLength ); |
|
141 TPtr8 orderPtr( orderBuf->Des() ); |
|
142 RDesWriteStream orderStream( orderPtr ); |
|
143 orderStream.PushL(); |
|
144 for( TInt i=0; i<iOrder.Count(); i++ ) |
|
145 { |
|
146 orderStream.WriteUint16L( iOrder[i] ); |
|
147 } |
|
148 orderStream.CommitL(); |
|
149 CleanupStack::PopAndDestroy(); // codescanner::cleanup |
|
150 User::LeaveIfError( iCRSession->Set( KESMRUILocationHistoryItemOrder, orderPtr )); |
|
151 CleanupStack::PopAndDestroy( orderBuf ); |
|
152 |
|
153 // updated changed location cenrep key |
|
154 if( itemWriteNeeded ) |
|
155 { |
|
156 HBufC8* buf = HBufC8::NewLC( NCentralRepositoryConstants::KMaxBinaryLength ); |
|
157 TPtr8 bufPtr = buf->Des(); |
|
158 RDesWriteStream stream( bufPtr ); |
|
159 stream.PushL(); |
|
160 |
|
161 stream.WriteUint16L( aItem->Address().Length() ); |
|
162 if( aItem->Address().Length() != 0 ) |
|
163 { |
|
164 stream.WriteL( aItem->Address() ); |
|
165 } |
|
166 stream.WriteUint16L( aItem->Url().Length() ); |
|
167 if( aItem->Url().Length() != 0 ) |
|
168 { |
|
169 stream.WriteL( aItem->Url() ); |
|
170 } |
|
171 |
|
172 stream.CommitL(); |
|
173 CleanupStack::PopAndDestroy(); // codescanner::cleanup |
|
174 |
|
175 TUint32 key = KESMRUILocationHistoryItemFirstInt; |
|
176 |
|
177 key = key + (( index + 1 ) * KESMRUILocationHistoryItemIndexMask ); |
|
178 TInt err = iCRSession->Set( key, bufPtr ); |
|
179 if( err == KErrNotFound ) |
|
180 { |
|
181 err = iCRSession->Create( key, bufPtr ); |
|
182 } |
|
183 User::LeaveIfError( err ); |
|
184 CleanupStack::PopAndDestroy( buf ); |
|
185 } |
|
186 |
|
187 iNotifyHandler->StartListeningL(); |
|
188 } |
|
189 |
|
190 // ----------------------------------------------------------------------------- |
|
191 // CESMRLocationHistoryManager::LocationHistoryItemL |
|
192 // ----------------------------------------------------------------------------- |
|
193 // |
|
194 const MESMRLocationHistoryItem& |
|
195 CESMRLocationHistoryManager::LocationHistoryItemL( TInt aIndex ) |
|
196 { |
|
197 FUNC_LOG; |
|
198 if(( aIndex < 0 ) || ( aIndex >= iOrder.Count() )) |
|
199 { |
|
200 User::Leave( KErrNotFound ); |
|
201 } |
|
202 |
|
203 if( iOrder.Count() != iHistoryList.Count() ) |
|
204 { |
|
205 ReadHistoryDataL(); |
|
206 } |
|
207 |
|
208 return *iHistoryList[iOrder[aIndex]]; |
|
209 } |
|
210 |
|
211 // ----------------------------------------------------------------------------- |
|
212 // CESMRLocationHistoryManager::ItemCount |
|
213 // ----------------------------------------------------------------------------- |
|
214 // |
|
215 TUint CESMRLocationHistoryManager::ItemCount() |
|
216 { |
|
217 FUNC_LOG; |
|
218 return iOrder.Count(); |
|
219 } |
|
220 |
|
221 // ----------------------------------------------------------------------------- |
|
222 // CESMRLocationHistoryManager::LocationHistoryItemFactory |
|
223 // ----------------------------------------------------------------------------- |
|
224 // |
|
225 MESMRLocationHistoryItem* CESMRLocationHistoryManager::CreateLocationHistoryItemL( |
|
226 const TDesC& aAddress, |
|
227 const TDesC& aUrl ) |
|
228 { |
|
229 FUNC_LOG; |
|
230 return iFactory->CreateLocationHistoryItemL( aAddress, aUrl ); |
|
231 } |
|
232 |
|
233 // ----------------------------------------------------------------------------- |
|
234 // CESMRLocationHistoryManager::CESMRLocationHistoryManager |
|
235 // ----------------------------------------------------------------------------- |
|
236 // |
|
237 CESMRLocationHistoryManager::CESMRLocationHistoryManager() : |
|
238 iMaxCount( 0 ) |
|
239 { |
|
240 FUNC_LOG; |
|
241 } |
|
242 |
|
243 void CESMRLocationHistoryManager::HandleNotifyGeneric( TUint32 /*aId*/ ) |
|
244 { |
|
245 FUNC_LOG; |
|
246 /* One or more of the keys in KCRUidESMRUIPreviousLocations has changed. |
|
247 * Update all needed keys data by reading them. In reality this case is very |
|
248 * rare, so performance is not seen as a problem here. |
|
249 */ |
|
250 TRAP_IGNORE( ReadOrderDataL() ); |
|
251 TRAP_IGNORE( ReadHistoryDataL() ); |
|
252 } |
|
253 |
|
254 // ----------------------------------------------------------------------------- |
|
255 // CESMRLocationHistoryManager::ConstructL |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 void CESMRLocationHistoryManager::ConstructL() |
|
259 { |
|
260 FUNC_LOG; |
|
261 iFactory = CESMRLocationHistoryItemFactory::NewL(); |
|
262 iCRSession = CRepository::NewL( KCRUidESMRUIPreviousLocations ); |
|
263 iNotifyHandler = CCenRepNotifyHandler::NewL( *this, *iCRSession ); |
|
264 iNotifyHandler->StartListeningL(); |
|
265 |
|
266 User::LeaveIfError( iCRSession->Get( KESMRUILocationHistoryItemMaxCount, iMaxCount )); |
|
267 |
|
268 TInt currentCount = 0; |
|
269 User::LeaveIfError( iCRSession->Get( KESMRUILocationHistoryItemCount, currentCount )); |
|
270 |
|
271 if( currentCount > 0 ) |
|
272 { |
|
273 ReadOrderDataL(); |
|
274 } |
|
275 } |
|
276 |
|
277 // ----------------------------------------------------------------------------- |
|
278 // CESMRLocationHistoryManager::ReadOrderDataL |
|
279 // ----------------------------------------------------------------------------- |
|
280 // |
|
281 void CESMRLocationHistoryManager::ReadOrderDataL() |
|
282 { |
|
283 FUNC_LOG; |
|
284 TInt currentCount = 0; |
|
285 User::LeaveIfError( iCRSession->Get( KESMRUILocationHistoryItemCount, currentCount )); |
|
286 |
|
287 iOrder.Reset(); |
|
288 iOrder.ReserveL( currentCount ); |
|
289 HBufC8* orderBuf = HBufC8::NewLC( NCentralRepositoryConstants::KMaxBinaryLength ); |
|
290 TPtr8 orderPtr( orderBuf->Des() ); |
|
291 |
|
292 User::LeaveIfError( iCRSession->Get( KESMRUILocationHistoryItemOrder, orderPtr )); |
|
293 RDesReadStream orderStream( orderPtr ); |
|
294 orderStream.PushL(); |
|
295 |
|
296 TUint pos( 0 ); |
|
297 for( TInt i=0; i<currentCount; i++ ) |
|
298 { |
|
299 User::LeaveIfError( pos = orderStream.ReadUint16L() ); |
|
300 iOrder.Append( pos ); |
|
301 } |
|
302 CleanupStack::PopAndDestroy(); // codescanner::cleanup |
|
303 CleanupStack::PopAndDestroy( orderBuf ); |
|
304 } |
|
305 |
|
306 // ----------------------------------------------------------------------------- |
|
307 // CESMRLocationHistoryManager::ReadHistoryDataL |
|
308 // ----------------------------------------------------------------------------- |
|
309 // |
|
310 void CESMRLocationHistoryManager::ReadHistoryDataL() |
|
311 { |
|
312 FUNC_LOG; |
|
313 TInt currentCount = iOrder.Count(); |
|
314 |
|
315 iHistoryList.ResetAndDestroy(); |
|
316 iHistoryList.ReserveL( iMaxCount ); |
|
317 TUint32 key = KESMRUILocationHistoryItemFirstInt; |
|
318 for( TInt i=0; i<currentCount; i++ ) |
|
319 { |
|
320 HBufC8* buf = HBufC8::NewLC( NCentralRepositoryConstants::KMaxBinaryLength ); |
|
321 TPtr8 ptr( buf->Des() ); |
|
322 key += KESMRUILocationHistoryItemIndexMask; |
|
323 User::LeaveIfError( iCRSession->Get( key, ptr )); |
|
324 if( buf != 0 ) |
|
325 { |
|
326 RDesReadStream stream( ptr ); |
|
327 stream.PushL(); |
|
328 |
|
329 TInt addressLength = stream.ReadUint16L(); |
|
330 HBufC* address = HBufC::NewLC( addressLength ); |
|
331 TPtr addressPtr = address->Des(); |
|
332 if( addressLength != 0 ) |
|
333 { |
|
334 stream.ReadL( addressPtr, addressLength ); |
|
335 } |
|
336 |
|
337 TInt urlLength = stream.ReadUint16L(); |
|
338 HBufC* url = HBufC::NewLC( urlLength ); |
|
339 TPtr urlPtr = url->Des(); |
|
340 if( urlLength != 0 ) |
|
341 { |
|
342 stream.ReadL( urlPtr, urlLength ); |
|
343 } |
|
344 |
|
345 MESMRLocationHistoryItem* item = |
|
346 iFactory->CreateLocationHistoryItemL( addressPtr, urlPtr ); |
|
347 iHistoryList.Append( item ); |
|
348 |
|
349 CleanupStack::PopAndDestroy( url ); |
|
350 CleanupStack::PopAndDestroy( address ); |
|
351 CleanupStack::PopAndDestroy(); // codescanner::cleanup |
|
352 } |
|
353 CleanupStack::PopAndDestroy( buf ); |
|
354 } |
|
355 } |
|
356 |
|
357 // EOF |
|
358 |