|
1 /* |
|
2 * Copyright (c) 2006 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: Static helper class for handling terms of use dialog |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "cnuitermsofusedialoghandler.h" |
|
20 |
|
21 #include <cimpssapsettingsstore.h> |
|
22 #include <cimpssapsettings.h> |
|
23 #include <centralrepository.h> // for KMaxUnicodeStringLength |
|
24 |
|
25 #include "mcnuiuifacade.h" |
|
26 |
|
27 // Difference between KMaxUnicodeStringLength and CIMPSSAPSettings |
|
28 // opaque data length, see cimpssapsettings.h SetOpaqueDesC16 method description. |
|
29 const TInt KSapSettingsDataBufferLength = 5; |
|
30 // Collation level 1 to ignore case |
|
31 const TInt KCollationLevel = 1; |
|
32 |
|
33 // ======== LOCAL FUNCTIONS ======== |
|
34 |
|
35 // --------------------------------------------------------------------------- |
|
36 // Finds aId from aIdList. aIdList contains dot separated ids. |
|
37 // Returns KErrNone if aId is found, KErrNotFound otherwise. |
|
38 // --------------------------------------------------------------------------- |
|
39 // |
|
40 TInt FindId( const TDesC& aId, const TDesC& aIdList ) |
|
41 { |
|
42 // Get pointer descriptor to work with |
|
43 TPtrC ptr( aIdList ); |
|
44 // Locate first dot |
|
45 TInt dotPos = ptr.FindC( KIMDot ); |
|
46 |
|
47 while ( dotPos != KErrNotFound ) |
|
48 { |
|
49 // Get current id from list for comparison |
|
50 TPtrC curId( ptr.Left( dotPos ) ); |
|
51 |
|
52 if ( curId.CompareC( aId, KCollationLevel, NULL ) == 0 ) |
|
53 { |
|
54 // Match found, return |
|
55 return KErrNone; |
|
56 } |
|
57 else |
|
58 { |
|
59 // Move pointer to next id in list |
|
60 TInt len = ptr.Length() - dotPos - 1; // -1 to get rid of dot |
|
61 if ( len < 0 ) |
|
62 { |
|
63 // This should never happen but let's check |
|
64 // just to be sure to avoid panics |
|
65 return KErrNotFound; |
|
66 } |
|
67 ptr.Set( ptr.Right( len ) ); |
|
68 // Locate next dot |
|
69 dotPos = ptr.FindC( KIMDot ); |
|
70 } |
|
71 } |
|
72 |
|
73 // Match not found |
|
74 return KErrNotFound; |
|
75 } |
|
76 |
|
77 // ======== MEMBER FUNCTIONS ======== |
|
78 |
|
79 // --------------------------------------------------------------------------- |
|
80 // CnUiTermsOfUseDialogHandler::HandleTermsOfUseDialogL |
|
81 // --------------------------------------------------------------------------- |
|
82 // |
|
83 TInt CnUiTermsOfUseDialogHandler::HandleTermsOfUseDialogL( |
|
84 CIMPSSAPSettings& aSap, |
|
85 MCnUiUiFacade& aUi, |
|
86 CIMPSSAPSettingsStore& aSapStore ) |
|
87 { |
|
88 TInt sapError = KErrNone; |
|
89 TPtrC16 acceptedIds; |
|
90 |
|
91 // Get SAP settings from SAP store to work with |
|
92 CIMPSSAPSettings* tmpSap = CIMPSSAPSettings::NewLC(); |
|
93 TBool tmpSapOwned = ETrue; |
|
94 TRAPD( err, aSapStore.GetSAPL( aSap.Uid(), tmpSap ) ); |
|
95 if ( err != KErrNone ) |
|
96 { |
|
97 // In case of error use aSap directly |
|
98 CleanupStack::PopAndDestroy( tmpSap ); |
|
99 tmpSap = NULL; |
|
100 tmpSap = &aSap; |
|
101 tmpSapOwned = EFalse; |
|
102 } |
|
103 |
|
104 // Get pointer descriptor to ids that have accepted ToU from SAP |
|
105 sapError = tmpSap->GetOpaqueDesC16( KIMToUAccepted, acceptedIds ); |
|
106 |
|
107 // Ignore KErrNotFound errors (means that |
|
108 // this is the first login attempt to this SAP) |
|
109 if ( sapError != KErrNotFound ) |
|
110 { |
|
111 User::LeaveIfError( sapError ); |
|
112 } |
|
113 |
|
114 // Get current user id from aSap |
|
115 const TDesC& currentId( aSap.SAPUserId() ); |
|
116 |
|
117 // Check if ToU has already been accepted for current id |
|
118 TInt retVal = FindId( currentId, acceptedIds ); |
|
119 |
|
120 if ( retVal == KErrNotFound ) |
|
121 { |
|
122 // Show Terms of Use, return KErrCancel if user does not accept them, |
|
123 // otherwise continue |
|
124 if ( !aUi.DisplayTermsOfUseAndQueryL() ) |
|
125 { |
|
126 // User canceled ToU query, login cancelled |
|
127 // note is already shown inside DisplayTermsOfUseAndQueryL |
|
128 // method, just return KErrCancel |
|
129 if ( tmpSapOwned ) |
|
130 { |
|
131 // Cleanup needed |
|
132 CleanupStack::PopAndDestroy( tmpSap ); |
|
133 } |
|
134 return KErrCancel; |
|
135 } |
|
136 else |
|
137 { |
|
138 // ToU accepted, set it shown |
|
139 // for this user id |
|
140 HBufC* newAcceptedIds = HBufC::NewLC( |
|
141 acceptedIds.Length() + |
|
142 KIMDot().Length() + |
|
143 currentId.Length() ); |
|
144 TPtr ids( newAcceptedIds->Des() ); |
|
145 |
|
146 // Append previous user ids if any |
|
147 if ( acceptedIds.Length() > 0 ) |
|
148 { |
|
149 ids.Append( acceptedIds ); |
|
150 } |
|
151 // Append current id |
|
152 ids.Append( currentId ); |
|
153 // Append dot as separator |
|
154 ids.Append( KIMDot ); |
|
155 |
|
156 // Check key+value length |
|
157 TInt keyLen = KIMToUAccepted().Length(); |
|
158 TInt newLen = ids.Length() + keyLen; |
|
159 TInt maxLen = NCentralRepositoryConstants::KMaxUnicodeStringLength - |
|
160 KSapSettingsDataBufferLength; |
|
161 |
|
162 // if key+value length too big, delete |
|
163 // previous ids from start of list |
|
164 // until length is small enough |
|
165 while ( newLen > maxLen ) |
|
166 { |
|
167 // Find first dot |
|
168 TInt dotPos = ids.FindC( KIMDot ); |
|
169 if ( dotPos == KErrNotFound ) |
|
170 { |
|
171 // No dot => data corrupted, all we can do |
|
172 // is to delete previous key+value |
|
173 // pair and set new one with only current id |
|
174 tmpSap->DeleteOpaque( KIMToUAccepted ); |
|
175 ids.Zero(); |
|
176 ids.Append( currentId ); |
|
177 newLen = ids.Length() + keyLen; |
|
178 } |
|
179 else |
|
180 { |
|
181 // Delete first id+dot from list |
|
182 ids.Delete( 0, dotPos + 1 ); |
|
183 newLen = ids.Length() + keyLen; |
|
184 } |
|
185 } |
|
186 |
|
187 // Set new ids to SAP |
|
188 sapError = tmpSap->SetOpaqueDesC16( |
|
189 KIMToUAccepted, *newAcceptedIds ); |
|
190 // Leave if errors |
|
191 User::LeaveIfError( sapError ); |
|
192 |
|
193 // Cleanup |
|
194 CleanupStack::PopAndDestroy( newAcceptedIds ); |
|
195 |
|
196 // Update SAP to store if it was succesfully |
|
197 // loaded from store |
|
198 if ( tmpSapOwned ) |
|
199 { |
|
200 aSapStore.UpdateOldSAPL( tmpSap, aSap.Uid() ); |
|
201 } |
|
202 } |
|
203 } |
|
204 |
|
205 if ( tmpSapOwned ) |
|
206 { |
|
207 // Cleanup needed |
|
208 CleanupStack::PopAndDestroy( tmpSap ); |
|
209 } |
|
210 |
|
211 return KErrNone; |
|
212 } |