|
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // CMMSACCOUNT.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 |
|
19 #include <centralrepository.h> |
|
20 #include <cmmsaccounts.h> |
|
21 #include <cmmssettings.h> |
|
22 #include <mmmssettingsobserver.h> |
|
23 |
|
24 |
|
25 const TUint32 KNullId = 0x00000000; |
|
26 const TUid KMMSRepositoryUid = {0x10202D4C}; |
|
27 const TInt KMaxSettingLength = 256; |
|
28 const TInt KMmsSettingsVersion = 1; |
|
29 const TUint32 KIncrementAccount = 0x00100000; |
|
30 const TInt KShiftAccountId = 20; |
|
31 const TUint32 KInitialiseSettings = 0x00000000; // Initial settings are in account 0 |
|
32 |
|
33 const TUint32 KAccountMask = 0x800FFFFF; |
|
34 const TUint32 KMaximumAccounts = 0x80000000; |
|
35 const TUint32 KDefaultAccountId = 0x80000003; |
|
36 |
|
37 |
|
38 |
|
39 /** |
|
40 Allocates and constructs an MMS account object. |
|
41 |
|
42 Initialises all member data to their default values. |
|
43 |
|
44 @return |
|
45 The newly constructed MMS account object. |
|
46 */ |
|
47 EXPORT_C CMmsAccounts* CMmsAccounts::NewL() |
|
48 { |
|
49 CMmsAccounts* self = CMmsAccounts::NewLC(); |
|
50 CleanupStack::Pop(self); |
|
51 return self; |
|
52 } |
|
53 |
|
54 /** |
|
55 Allocates and constructs an MMS account object. |
|
56 |
|
57 Initialises all member data to their default values. |
|
58 |
|
59 @return |
|
60 The newly constructed MMS account object. |
|
61 */ |
|
62 EXPORT_C CMmsAccounts* CMmsAccounts::NewLC() |
|
63 { |
|
64 CMmsAccounts* self = new (ELeave) CMmsAccounts(); |
|
65 CleanupStack::PushL(self); |
|
66 self->ConstructL(); |
|
67 return self; |
|
68 } |
|
69 |
|
70 /** |
|
71 Second phase construction. |
|
72 */ |
|
73 void CMmsAccounts::ConstructL() |
|
74 { |
|
75 iRepository = CRepository::NewL(KMMSRepositoryUid); |
|
76 } |
|
77 |
|
78 /** |
|
79 Constructor. |
|
80 */ |
|
81 CMmsAccounts::CMmsAccounts() |
|
82 : CActive(CActive::EPriorityStandard) |
|
83 { |
|
84 CActiveScheduler::Add(this); |
|
85 } |
|
86 |
|
87 /** |
|
88 Destructor. |
|
89 */ |
|
90 EXPORT_C CMmsAccounts::~CMmsAccounts() |
|
91 { |
|
92 Cancel(); |
|
93 iObservers.Close(); |
|
94 delete iRepository; |
|
95 } |
|
96 |
|
97 void CMmsAccounts::DoCancel() |
|
98 { |
|
99 // cancel request to the repository |
|
100 iRepository->NotifyCancel(KNullId, KNullId); |
|
101 } |
|
102 |
|
103 void CMmsAccounts::RunL() |
|
104 { |
|
105 // Notify all observers |
|
106 for(TInt i=iObservers.Count()-1; i>=0; --i) |
|
107 { |
|
108 iObservers[i]->HandleNotify(MMmsSettingsObserver::ESettingsUpdated); |
|
109 } |
|
110 |
|
111 // Re-issue notification request |
|
112 User::LeaveIfError(iRepository->NotifyRequest(KNullId, KNullId, iStatus)); |
|
113 SetActive(); |
|
114 } |
|
115 |
|
116 /** |
|
117 Registers a new MMS Settings observer. |
|
118 |
|
119 @param aObserver |
|
120 A reference to an observer to be registered for events |
|
121 */ |
|
122 EXPORT_C void CMmsAccounts::AddObserverL(MMmsSettingsObserver& aObserver) |
|
123 { |
|
124 iObservers.AppendL(&aObserver); |
|
125 |
|
126 if (IsActive() == EFalse) |
|
127 { |
|
128 // Issue notification request |
|
129 User::LeaveIfError(iRepository->NotifyRequest(KNullId, KNullId, iStatus)); |
|
130 SetActive(); |
|
131 } |
|
132 } |
|
133 |
|
134 /** |
|
135 Deregisters a previously registered observer. |
|
136 |
|
137 @param aObserver |
|
138 A reference to an observer to be unregistered for events |
|
139 */ |
|
140 EXPORT_C void CMmsAccounts::RemoveObserver(MMmsSettingsObserver& aObserver) |
|
141 { |
|
142 TInt i = iObservers.Find(&aObserver); |
|
143 __ASSERT_ALWAYS(i>=0, User::Invariant()); |
|
144 iObservers.Remove(i); |
|
145 |
|
146 if (iObservers.Count() <= 0) |
|
147 { |
|
148 Cancel(); |
|
149 } |
|
150 } |
|
151 |
|
152 /** |
|
153 Creates a MMS Account. |
|
154 |
|
155 @param aAccountName |
|
156 MMS account Name |
|
157 |
|
158 @param aSettings |
|
159 MMS Settings |
|
160 |
|
161 @return |
|
162 MMS Account Id |
|
163 */ |
|
164 EXPORT_C TMmsAccountId CMmsAccounts::CreateMMSAccountL(const TDesC& aAccountName, const CMmsSettings& aSettings) |
|
165 { |
|
166 // Get the number of accounts exist in CenRep |
|
167 RArray<TUint32> accountIds; |
|
168 CleanupClosePushL(accountIds); |
|
169 TInt error = iRepository->FindL(KNullId, static_cast<TUint32>(KAccountMask), accountIds); |
|
170 if (error != KErrNotFound) |
|
171 { |
|
172 User::LeaveIfError(error); |
|
173 } |
|
174 TInt accountsCount = accountIds.Count() - 1; // ignoring the default account |
|
175 CleanupStack::PopAndDestroy(&accountIds); |
|
176 |
|
177 if (accountsCount >= MaxMMSAccounts()) |
|
178 { |
|
179 // Can't create any more accounts |
|
180 User::Leave(KErrOverflow); |
|
181 } |
|
182 |
|
183 TUint32 accountId = 0; |
|
184 accountId = GetNextEmptyAccountSlotL(); |
|
185 DoSaveSettingsL(accountId, aSettings, aAccountName); |
|
186 |
|
187 TMmsAccountId account; |
|
188 account.iMmsAccountId = accountId >> KShiftAccountId; |
|
189 account.iMmsAccountName = aAccountName; |
|
190 return account; |
|
191 } |
|
192 |
|
193 TUint CMmsAccounts::GetNextEmptyAccountSlotL() |
|
194 { |
|
195 TUint32 accountId = KNullId; |
|
196 TBuf<KMmsAccountNameSize> accountName; |
|
197 TInt error = 0; |
|
198 TBool found = EFalse; |
|
199 TInt maximumAccount = MaxMMSAccounts(); |
|
200 |
|
201 for (TInt count = 0; count < maximumAccount; ++count) |
|
202 { |
|
203 accountId = accountId + KIncrementAccount; |
|
204 error = iRepository->Get(accountId + EMmsAccountName, accountName); |
|
205 if (error == KErrNotFound) |
|
206 { |
|
207 found = ETrue; |
|
208 break; |
|
209 } |
|
210 else |
|
211 { |
|
212 User::LeaveIfError(error); |
|
213 } |
|
214 } |
|
215 |
|
216 if (found == EFalse) |
|
217 { |
|
218 // No empty slot available |
|
219 User::Leave(KErrNotFound); |
|
220 } |
|
221 |
|
222 return accountId; |
|
223 } |
|
224 |
|
225 /** |
|
226 Maximum number of accounts that can be stored in the Central Repository |
|
227 |
|
228 @return |
|
229 The maximum number of accounts |
|
230 */ |
|
231 EXPORT_C TInt CMmsAccounts::MaxMMSAccounts() |
|
232 { |
|
233 TInt maxAccounts = 0; |
|
234 TInt error = iRepository->Get(KMaximumAccounts, maxAccounts); |
|
235 if(error != KErrNone) |
|
236 { |
|
237 return error; |
|
238 } |
|
239 return maxAccounts; |
|
240 } |
|
241 |
|
242 /** |
|
243 Gets a list of MMS account IDs stored in the Central Repository. |
|
244 |
|
245 Any existing entries in the array will be deleted. |
|
246 |
|
247 @param aAccountIds |
|
248 A list if MMS account Ids |
|
249 */ |
|
250 EXPORT_C void CMmsAccounts::GetMMSAccountsL(RArray<TMmsAccountId>& aAccountIds) const |
|
251 { |
|
252 aAccountIds.Reset(); |
|
253 RArray<TUint32> accountIds; |
|
254 CleanupClosePushL(accountIds); |
|
255 TInt error = iRepository->FindL(KNullId, static_cast<TUint32>(KAccountMask), accountIds); |
|
256 if (error != KErrNotFound) |
|
257 { |
|
258 User::LeaveIfError(error); |
|
259 } |
|
260 |
|
261 TMmsAccountId accountId; |
|
262 TInt count = accountIds.Count(); |
|
263 for(TInt i = 1; i < count; ++i) |
|
264 { |
|
265 TUint32 settingId = accountIds[i]; |
|
266 User::LeaveIfError(iRepository->Get(settingId + EMmsAccountName, accountId.iMmsAccountName)); |
|
267 accountId.iMmsAccountId = accountIds[i] >> KShiftAccountId; |
|
268 aAccountIds.AppendL(accountId); |
|
269 } |
|
270 |
|
271 CleanupStack::PopAndDestroy(&accountIds); |
|
272 } |
|
273 |
|
274 /** |
|
275 Deletes the specified MMS account. |
|
276 |
|
277 @param aAccountId |
|
278 MMS account Id |
|
279 */ |
|
280 EXPORT_C void CMmsAccounts::DeleteMMSAccountL(const TMmsAccountId& aAccountId) |
|
281 { |
|
282 TUint32 accountId = aAccountId.iMmsAccountId << KShiftAccountId; |
|
283 |
|
284 // Read in proxy and napId counts |
|
285 TInt proxyCount = 0; |
|
286 User::LeaveIfError(iRepository->Get(accountId + EMmsProxyCount, proxyCount)); |
|
287 |
|
288 TInt napIdCount = 0; |
|
289 User::LeaveIfError(iRepository->Get(accountId + EMmsNapIdCount, napIdCount)); |
|
290 |
|
291 // Delete account |
|
292 User::LeaveIfError(iRepository->StartTransaction(CRepository::EReadWriteTransaction)); |
|
293 iRepository->CleanupRollbackTransactionPushL(); |
|
294 |
|
295 iRepository->Delete(accountId + EMmsAccountName); |
|
296 iRepository->Delete(accountId + EMmsSettingsVersion); |
|
297 iRepository->Delete(accountId + EMmsApplicationID); |
|
298 iRepository->Delete(accountId + EMmsAddress); |
|
299 iRepository->Delete(accountId + EMmsCreationMode); |
|
300 iRepository->Delete(accountId + EMmsSettingsFlags); |
|
301 iRepository->Delete(accountId + EMmsProxyCount); |
|
302 |
|
303 TUint32 proxyId = accountId + EMmsProxyList; |
|
304 for(TInt loop=0; loop < proxyCount; ++loop) |
|
305 { |
|
306 iRepository->Delete(proxyId); |
|
307 ++proxyId; |
|
308 } |
|
309 |
|
310 iRepository->Delete(accountId + EMmsNapIdCount); |
|
311 |
|
312 TUint32 napId = accountId + EMmsNapIdList; |
|
313 for(TInt loop=0; loop < proxyCount; ++loop) |
|
314 { |
|
315 iRepository->Delete(napId); |
|
316 ++napId; |
|
317 } |
|
318 |
|
319 iRepository->Delete(accountId + EMmsAutomaticDownload); |
|
320 iRepository->Delete(accountId + EMmsValidityPeriod); |
|
321 iRepository->Delete(accountId + EMmsMaxDownloadSize); |
|
322 iRepository->Delete(accountId + EMmsMaxDownloadRetries); |
|
323 iRepository->Delete(accountId + EMmsDownloadRetryInterval); |
|
324 iRepository->Delete(accountId + EMmsMaxSendMsgSize); |
|
325 iRepository->Delete(accountId + EMmsDeviceContentClass); |
|
326 iRepository->Delete(accountId + EMmsMaxImageHeight); |
|
327 iRepository->Delete(accountId + EMmsMaxImageWidth); |
|
328 |
|
329 CleanupStack::Pop(); |
|
330 TUint32 errorId = 0; |
|
331 User::LeaveIfError(iRepository->CommitTransaction(errorId)); |
|
332 } |
|
333 |
|
334 /** |
|
335 Loads MMS settings from the Central Repository for the specified account Id. |
|
336 |
|
337 @param aAccountId |
|
338 MMS account Id |
|
339 |
|
340 @param aSettings |
|
341 MMS settings |
|
342 */ |
|
343 EXPORT_C void CMmsAccounts::LoadSettingsL(const TMmsAccountId& aAccountId, CMmsSettings& aSettings) |
|
344 { |
|
345 TUint32 accountId = aAccountId.iMmsAccountId << KShiftAccountId; |
|
346 DoLoadSettingsL(accountId, aSettings); |
|
347 } |
|
348 |
|
349 void CMmsAccounts::DoLoadSettingsL(TUint32 aAccountId, CMmsSettings& aSettings) |
|
350 { |
|
351 TInt temp = 0; |
|
352 User::LeaveIfError(iRepository->Get(aAccountId + EMmsApplicationID, temp)); |
|
353 aSettings.SetApplicationID(temp); |
|
354 |
|
355 TBuf<KMaxSettingLength> tempBuff; |
|
356 User::LeaveIfError(iRepository->Get(aAccountId + EMmsAddress, tempBuff)); |
|
357 aSettings.SetAddressL(tempBuff); |
|
358 |
|
359 User::LeaveIfError(iRepository->Get(aAccountId + EMmsCreationMode, temp)); |
|
360 aSettings.SetCreationModeL(static_cast<TCreationMode>(temp)); |
|
361 |
|
362 User::LeaveIfError(iRepository->Get(aAccountId + EMmsSettingsFlags, temp)); |
|
363 aSettings.SetMmsSettingsFlags(static_cast<TUint32>(temp)); |
|
364 |
|
365 User::LeaveIfError(iRepository->Get(aAccountId + EMmsProxyCount, temp)); |
|
366 TInt count = temp; |
|
367 |
|
368 aSettings.RemoveAllProxies(); |
|
369 TUint32 proxyId = aAccountId + EMmsProxyList; |
|
370 for(TInt loop=0; loop < count; ++loop) |
|
371 { |
|
372 User::LeaveIfError(iRepository->Get(proxyId, temp)); |
|
373 aSettings.AddProxyL(TUid::Uid(temp)); |
|
374 ++proxyId; |
|
375 } |
|
376 |
|
377 User::LeaveIfError(iRepository->Get(aAccountId + EMmsNapIdCount, count)); |
|
378 |
|
379 aSettings.RemoveAllNapIds(); |
|
380 TUint32 napId = aAccountId + EMmsNapIdList; |
|
381 for(TInt loop=0; loop < count; ++loop) |
|
382 { |
|
383 User::LeaveIfError(iRepository->Get(napId, temp)); |
|
384 aSettings.AddNapIdL(TUid::Uid(temp)); |
|
385 ++napId; |
|
386 } |
|
387 |
|
388 User::LeaveIfError(iRepository->Get(aAccountId + EMmsAutomaticDownload, temp)); |
|
389 aSettings.SetAutomaticDownload(static_cast<TAutomaticDownloadOptions>(temp)); |
|
390 |
|
391 User::LeaveIfError(iRepository->Get(aAccountId + EMmsValidityPeriod, temp)); |
|
392 aSettings.SetValidityPeriod(temp); |
|
393 |
|
394 User::LeaveIfError(iRepository->Get(aAccountId + EMmsMaxDownloadSize, temp)); |
|
395 aSettings.SetMaxDownloadSize(temp); |
|
396 |
|
397 User::LeaveIfError(iRepository->Get(aAccountId + EMmsMaxDownloadRetries, temp)); |
|
398 aSettings.SetMaxDownloadRetries(temp); |
|
399 |
|
400 User::LeaveIfError(iRepository->Get(aAccountId + EMmsDownloadRetryInterval, temp)); |
|
401 aSettings.SetDownloadRetryInterval(temp); |
|
402 |
|
403 User::LeaveIfError(iRepository->Get(aAccountId + EMmsMaxSendMsgSize, temp)); |
|
404 aSettings.SetMaxSendMsgSize(temp); |
|
405 |
|
406 User::LeaveIfError(iRepository->Get(aAccountId + EMmsDeviceContentClass, temp)); |
|
407 aSettings.SetDeviceContentClass(temp); |
|
408 |
|
409 User::LeaveIfError(iRepository->Get(aAccountId + EMmsMaxImageHeight, temp)); |
|
410 aSettings.SetMaxImageHeight(temp); |
|
411 |
|
412 User::LeaveIfError(iRepository->Get(aAccountId + EMmsMaxImageWidth, temp)); |
|
413 aSettings.SetMaxImageWidth(temp); |
|
414 } |
|
415 |
|
416 /** |
|
417 Populates the supplied setting object with default values. |
|
418 |
|
419 @param aSettings |
|
420 MMS Setting |
|
421 */ |
|
422 EXPORT_C void CMmsAccounts::PopulateDefaultSettingsL(CMmsSettings& aSettings) |
|
423 { |
|
424 DoLoadSettingsL(KInitialiseSettings, aSettings); |
|
425 } |
|
426 |
|
427 /** |
|
428 Saves MMS settings to the Central Repository for the specified account Id. |
|
429 |
|
430 @param aAccountId |
|
431 MMS account Id |
|
432 |
|
433 @param aSettings |
|
434 MMS settings |
|
435 */ |
|
436 EXPORT_C void CMmsAccounts::SaveSettingsL(const TMmsAccountId& aAccountId, const CMmsSettings& aSettings) const |
|
437 { |
|
438 TUint32 accountId = aAccountId.iMmsAccountId << KShiftAccountId; |
|
439 DoSaveSettingsL(accountId, aSettings, aAccountId.iMmsAccountName); |
|
440 } |
|
441 |
|
442 void CMmsAccounts::DoSaveSettingsL(TUint32 aAccountId, const CMmsSettings& aSettings, const TDesC& aAccountName) const |
|
443 { |
|
444 User::LeaveIfError(iRepository->StartTransaction(CRepository::EReadWriteTransaction)); |
|
445 iRepository->CleanupRollbackTransactionPushL(); |
|
446 // Update account name |
|
447 iRepository->Set(aAccountId + EMmsAccountName, aAccountName); |
|
448 iRepository->Set(aAccountId + EMmsSettingsVersion, KMmsSettingsVersion); |
|
449 iRepository->Set(aAccountId + EMmsApplicationID, aSettings.ApplicationID()); |
|
450 iRepository->Set(aAccountId + EMmsAddress, aSettings.Address()); |
|
451 iRepository->Set(aAccountId + EMmsCreationMode, static_cast<TInt>(aSettings.CreationMode())); |
|
452 iRepository->Set(aAccountId + EMmsSettingsFlags, static_cast<TInt>(aSettings.MmsSettingsFlags())); |
|
453 iRepository->Set(aAccountId + EMmsProxyCount, aSettings.ProxyCount()); |
|
454 |
|
455 TUint32 proxyId = aAccountId + EMmsProxyList; |
|
456 for(TInt loop=0; loop < aSettings.ProxyCount(); ++loop) |
|
457 { |
|
458 iRepository->Set(proxyId, static_cast<TInt>(aSettings.GetProxy(loop).iUid)); |
|
459 ++proxyId; |
|
460 } |
|
461 |
|
462 iRepository->Set(aAccountId + EMmsNapIdCount, aSettings.NapIdCount()); |
|
463 |
|
464 TUint32 napId = aAccountId + EMmsNapIdList; |
|
465 for(TInt loop=0; loop < aSettings.NapIdCount(); ++loop) |
|
466 { |
|
467 iRepository->Set(napId, static_cast<TInt>(aSettings.GetNapId(loop).iUid)); |
|
468 ++napId; |
|
469 } |
|
470 |
|
471 iRepository->Set(aAccountId + EMmsAutomaticDownload, static_cast<TInt>(aSettings.AutomaticDownload())); |
|
472 iRepository->Set(aAccountId + EMmsValidityPeriod, aSettings.ValidityPeriod()); |
|
473 iRepository->Set(aAccountId + EMmsMaxDownloadSize, aSettings.MaxDownloadSize()); |
|
474 iRepository->Set(aAccountId + EMmsMaxDownloadRetries, aSettings.MaxDownloadRetries()); |
|
475 iRepository->Set(aAccountId + EMmsDownloadRetryInterval, aSettings.DownloadRetryInterval()); |
|
476 iRepository->Set(aAccountId + EMmsMaxSendMsgSize, aSettings.MaxSendMsgSize()); |
|
477 iRepository->Set(aAccountId + EMmsDeviceContentClass, aSettings.DeviceContentClass()); |
|
478 iRepository->Set(aAccountId + EMmsMaxImageHeight, aSettings.MaxImageHeight()); |
|
479 iRepository->Set(aAccountId + EMmsMaxImageWidth, aSettings.MaxImageWidth()); |
|
480 |
|
481 CleanupStack::Pop(); |
|
482 TUint32 errorId = 0; |
|
483 User::LeaveIfError(iRepository->CommitTransaction(errorId)); |
|
484 } |
|
485 |
|
486 /** |
|
487 Gets the default MMS account. |
|
488 |
|
489 @return |
|
490 The default account |
|
491 |
|
492 @leave |
|
493 KErrNotFound If the default account not been set previously or if MMSSettings for |
|
494 the default account does not exist in the Central Repository. |
|
495 */ |
|
496 EXPORT_C TMmsAccountId CMmsAccounts::DefaultMMSAccountL( ) const |
|
497 { |
|
498 TMmsAccountId account; |
|
499 User::LeaveIfError(iRepository->Get(KDefaultAccountId, account.iMmsAccountId)); |
|
500 |
|
501 TUint32 accountId = account.iMmsAccountId << KShiftAccountId; |
|
502 User::LeaveIfError(iRepository->Get(accountId + EMmsAccountName, account.iMmsAccountName)); |
|
503 |
|
504 return account; |
|
505 } |
|
506 |
|
507 /** |
|
508 Sets the default MMS account. |
|
509 |
|
510 @param aDefaultAccount |
|
511 Account to be set as default |
|
512 */ |
|
513 EXPORT_C void CMmsAccounts::SetDefaultMMSAccountL(const TMmsAccountId& aDefaultAccount) |
|
514 { |
|
515 iRepository->Set(KDefaultAccountId, aDefaultAccount.iMmsAccountId); |
|
516 } |
|
517 |
|
518 |
|
519 |