|
1 /* |
|
2 * Copyright (c) 2003-2005 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: Takes care of persistent storaging for groups |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 |
|
22 #include <f32file.h> |
|
23 #include <s32mem.h> |
|
24 |
|
25 #include "ChatDebugPrint.h" |
|
26 |
|
27 #include "CCAGroupStorage.h" |
|
28 #include "CCAStorageDefs.h" |
|
29 #include "MCAExtendedStoredGroup.h" |
|
30 |
|
31 // ================= MEMBER FUNCTIONS ======================= |
|
32 |
|
33 |
|
34 // destructor |
|
35 CCAGroupStorage::~CCAGroupStorage() |
|
36 { |
|
37 CloseTable(); |
|
38 delete iColset; |
|
39 } |
|
40 |
|
41 // ---------------------------------------------------------- |
|
42 // CCAGroupStorage::NewL |
|
43 // ---------------------------------------------------------- |
|
44 // |
|
45 CCAGroupStorage* CCAGroupStorage::NewL() |
|
46 { |
|
47 CCAGroupStorage* self = new ( ELeave ) CCAGroupStorage(); |
|
48 return self; |
|
49 } |
|
50 |
|
51 // ---------------------------------------------------------- |
|
52 // CCAGroupStorage::SetDbL |
|
53 // ---------------------------------------------------------- |
|
54 // |
|
55 void CCAGroupStorage::SetDbL( RDbStoreDatabase aDb ) |
|
56 { |
|
57 CDbColSet* tempColSet = aDb.ColSetL( KGroupTable ); |
|
58 delete iColset; |
|
59 iColset = tempColSet; |
|
60 iDb = aDb; |
|
61 } |
|
62 |
|
63 // ---------------------------------------------------------- |
|
64 // CCAGroupStorage::ReadFirstL |
|
65 // ---------------------------------------------------------- |
|
66 // |
|
67 TInt CCAGroupStorage::ReadFirstL( MCAExtendedStoredGroup* aGroup ) |
|
68 { |
|
69 OpenTableL(); |
|
70 iTable.BeginningL(); |
|
71 return ReadNextL( aGroup ); |
|
72 } |
|
73 |
|
74 // ---------------------------------------------------------- |
|
75 // CCAGroupStorage::ReadNextL |
|
76 // ---------------------------------------------------------- |
|
77 // |
|
78 TInt CCAGroupStorage::ReadNextL( MCAExtendedStoredGroup* aGroup ) |
|
79 { |
|
80 OpenTableL(); |
|
81 if ( iTable.NextL() ) |
|
82 { |
|
83 TRAPD( err, ReadCurrentL( aGroup ) ); |
|
84 |
|
85 // For some reason ReadCurrentL leaves with KErrEof, |
|
86 // even if the contact was read succesfully. |
|
87 if ( err == KErrEof ) |
|
88 { |
|
89 return KErrNone; |
|
90 } |
|
91 return err; |
|
92 } |
|
93 |
|
94 return KErrNotFound; |
|
95 } |
|
96 |
|
97 // ---------------------------------------------------------- |
|
98 // CCAGroupStorage::ReadGroupL |
|
99 // ---------------------------------------------------------- |
|
100 // |
|
101 void CCAGroupStorage::ReadGroupL( MCAExtendedStoredGroup* aGroup ) |
|
102 { |
|
103 OpenTableL(); |
|
104 TDbColNo colNo = iColset->ColNo( KGroupId ); |
|
105 if ( SeekRowL( colNo, aGroup->GroupId() ) ) |
|
106 { |
|
107 TRAPD( err, ReadCurrentL( aGroup ) ); |
|
108 |
|
109 // For some reason ReadCurrentL leaves with KErrEof, |
|
110 // even if the contact was read succesfully. |
|
111 if ( ( err == KErrEof ) || ( err == KErrNone ) ) |
|
112 { |
|
113 return; |
|
114 } |
|
115 User::Leave( err ); |
|
116 } |
|
117 } |
|
118 |
|
119 // ---------------------------------------------------------- |
|
120 // CCAGroupStorage::WriteGroupL |
|
121 // ---------------------------------------------------------- |
|
122 // |
|
123 void CCAGroupStorage::WriteGroupL( MCAExtendedStoredGroup* aGroup ) |
|
124 { |
|
125 OpenTableL(); |
|
126 WriteToStoreDbL( aGroup ); |
|
127 } |
|
128 |
|
129 // ---------------------------------------------------------- |
|
130 // CCAGroupStorage::DeleteGroupL |
|
131 // ---------------------------------------------------------- |
|
132 // |
|
133 void CCAGroupStorage::DeleteGroupL( MCAExtendedStoredGroup* aGroup ) |
|
134 { |
|
135 CHAT_DP_FUNC_ENTER( "DeleteGroupL" ); |
|
136 |
|
137 OpenTableL(); |
|
138 TDbColNo colNo = iColset->ColNo( KGroupId ); |
|
139 |
|
140 if ( SeekRowL( colNo, aGroup->GroupId() ) ) |
|
141 { |
|
142 CHAT_DP_FUNC_DP( "DeleteGroupL", "Found it, deleting" ); |
|
143 iTable.DeleteL(); |
|
144 } |
|
145 else |
|
146 { |
|
147 CHAT_DP_FUNC_DP( "DeleteGroupL", "Group not found, not deleted" ); |
|
148 } |
|
149 |
|
150 CHAT_DP_FUNC_DONE( "DeleteGroupL" ); |
|
151 } |
|
152 |
|
153 // ---------------------------------------------------------- |
|
154 // CCAGroupStorage::OpenTableL |
|
155 // ---------------------------------------------------------- |
|
156 // |
|
157 void CCAGroupStorage::OpenTableL() |
|
158 { |
|
159 if ( iTableOpened ) |
|
160 { |
|
161 return; |
|
162 } |
|
163 |
|
164 TInt err( iTable.Open( iDb, KGroupTable ) ); |
|
165 |
|
166 if ( err != KErrNone ) |
|
167 { |
|
168 iTable.Close(); |
|
169 iTableOpened = EFalse; |
|
170 User::Leave( err ); |
|
171 return; |
|
172 } |
|
173 |
|
174 iTableOpened = ETrue; |
|
175 return; |
|
176 } |
|
177 |
|
178 // ---------------------------------------------------------- |
|
179 // CCAGroupStorage::CloseTable |
|
180 // ---------------------------------------------------------- |
|
181 // |
|
182 void CCAGroupStorage::CloseTable() |
|
183 { |
|
184 if ( iTableOpened ) |
|
185 { |
|
186 iTable.Close(); |
|
187 } |
|
188 iTableOpened = EFalse; |
|
189 } |
|
190 |
|
191 // ---------------------------------------------------------- |
|
192 // CCAGroupStorage::WriteToStoreDbL |
|
193 // ---------------------------------------------------------- |
|
194 // |
|
195 void CCAGroupStorage::WriteToStoreDbL( MCAExtendedStoredGroup* aGroup ) |
|
196 { |
|
197 |
|
198 TDbColNo colNo = iColset->ColNo( KGroupId ); |
|
199 |
|
200 if ( SeekRowL( colNo, aGroup->GroupId() ) ) |
|
201 { |
|
202 iTable.UpdateL(); |
|
203 } |
|
204 else |
|
205 { |
|
206 iTable.InsertL(); |
|
207 } |
|
208 |
|
209 // Write group id |
|
210 iTable.SetColL( colNo, aGroup->GroupId() ); |
|
211 |
|
212 RDbColWriteStream stream; |
|
213 colNo = iColset->ColNo( KGroupData ); |
|
214 stream.OpenL( iTable, colNo ); |
|
215 stream.PushL(); |
|
216 |
|
217 // Externalize contact to buffer |
|
218 aGroup->ExternalizeL( stream ); |
|
219 stream.Close(); |
|
220 |
|
221 // Write row to table |
|
222 TRAPD( err, iTable.PutL() ); |
|
223 if ( err != KErrNone ) |
|
224 { |
|
225 // recover |
|
226 iTable.Cancel(); |
|
227 iTable.Reset(); |
|
228 } |
|
229 CleanupStack::PopAndDestroy(); // stream |
|
230 } |
|
231 |
|
232 // ---------------------------------------------------------- |
|
233 // CCAGroupStorage::ReadCurrentL |
|
234 // ---------------------------------------------------------- |
|
235 // |
|
236 void CCAGroupStorage::ReadCurrentL( MCAExtendedStoredGroup* aGroup ) |
|
237 { |
|
238 iTable.GetL(); |
|
239 |
|
240 // Get Column number for contact data size |
|
241 TDbColNo colNo = iColset->ColNo( KGroupDataSize ); |
|
242 |
|
243 RDbColReadStream stream; |
|
244 colNo = iColset->ColNo( KGroupData ); |
|
245 stream.OpenL( iTable, colNo ); |
|
246 stream.PushL(); |
|
247 |
|
248 // Read stream to contact |
|
249 aGroup->InternalizeL( stream ); |
|
250 stream.Close(); |
|
251 |
|
252 CleanupStack::PopAndDestroy(); // stream |
|
253 } |
|
254 |
|
255 // ---------------------------------------------------------- |
|
256 // CCAGroupStorage::SeekRowL |
|
257 // ---------------------------------------------------------- |
|
258 // |
|
259 TBool CCAGroupStorage::SeekRowL( TDbColNo aColNo, const TDesC& aId ) |
|
260 { |
|
261 iTable.BeginningL(); |
|
262 |
|
263 while ( iTable.NextL() ) |
|
264 { |
|
265 iTable.GetL(); |
|
266 if ( !iTable.ColDes( aColNo ).CompareC( aId, KCollationLevel, NULL ) ) |
|
267 { |
|
268 return ETrue; |
|
269 } |
|
270 } |
|
271 return EFalse; |
|
272 } |
|
273 |
|
274 // constructor |
|
275 CCAGroupStorage::CCAGroupStorage() |
|
276 { |
|
277 } |
|
278 |
|
279 |
|
280 // End of File |
|
281 |