|
1 // Copyright (c) 1998-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 // Telephone Voice Mailbox number settings parser |
|
15 // |
|
16 // |
|
17 |
|
18 #include <msventry.h> |
|
19 |
|
20 #include <cmsvattachment.h> |
|
21 #include <mmsvattachmentmanager.h> |
|
22 #include <mmsvattachmentmanagersync.h> |
|
23 #include <cbioasyncwaiter.h> |
|
24 |
|
25 #ifdef SYMBIAN_BOOKMARK_DATABASE |
|
26 #include <bookmarkdatabase.h> |
|
27 #include <bookmark.h> |
|
28 #endif // SYMBIAN_BOOKMARK_DATABASE |
|
29 |
|
30 #include "BSP.H" |
|
31 #include "IACP.H" |
|
32 #include "wwwp.h" |
|
33 #include "IACPDEF.H" |
|
34 #include "IACPERR.H" |
|
35 |
|
36 _LIT8(KCRLinefeed, "\r\n"); |
|
37 _LIT8(KEBookmarkItemBegin, "BEGIN:eBOOKMARK\r\n"); // Precedes a Bookmark in the Bookmark file |
|
38 _LIT8(KEBookmarkItemName, "NAME:"); // Precedes a Bookmark Name |
|
39 _LIT8(KEBookmarkItemURL, "URL:"); // Precedes a Bookmark URL |
|
40 _LIT8(KEBookmarkType, "TYPE:Web\r\n"); |
|
41 _LIT8(KEBookmarkItemEnd, "END:eBOOKMARK\r\n"); |
|
42 |
|
43 _LIT(KEBookmarkExtension, ".eBM"); |
|
44 |
|
45 // The number of characters that are constant for an eBookmark |
|
46 // file. It is the sum of the above literals. |
|
47 // |
|
48 const TInt KEBookmarkConstantChars = 55; |
|
49 |
|
50 |
|
51 CWWWHotlistItem::CWWWHotlistItem() |
|
52 { |
|
53 } |
|
54 |
|
55 CWWWHotlistItem::~CWWWHotlistItem() |
|
56 { |
|
57 delete iBmkName; |
|
58 delete iBmkUrl; |
|
59 } |
|
60 |
|
61 const TDesC& CWWWHotlistItem::Name() const |
|
62 { |
|
63 if (iBmkName == NULL) |
|
64 return KNullDesC; |
|
65 else |
|
66 return *iBmkName; |
|
67 } |
|
68 |
|
69 const TDesC& CWWWHotlistItem::Url() const |
|
70 { |
|
71 if (iBmkUrl == NULL) |
|
72 return KNullDesC; |
|
73 else |
|
74 return iBmkUrl->Addr(); |
|
75 } |
|
76 |
|
77 |
|
78 void CWWWHotlistItem::SetNameL(const TDesC& aDes) |
|
79 { |
|
80 HBufC* tempName = aDes.AllocL(); |
|
81 delete iBmkName; |
|
82 iBmkName = tempName; |
|
83 } |
|
84 |
|
85 void CWWWHotlistItem::SetUrlL(const TDesC& aDes) |
|
86 { |
|
87 if(!iBmkUrl) |
|
88 iBmkUrl = CIpAddress::NewL(aDes); |
|
89 else |
|
90 iBmkUrl->SetAddrL(aDes); |
|
91 } |
|
92 |
|
93 |
|
94 // |
|
95 // Constructor |
|
96 // |
|
97 CWWWHotlistParser::CWWWHotlistParser(RFs& aFs) |
|
98 :iFs(aFs) |
|
99 { |
|
100 } |
|
101 // |
|
102 // Destruction |
|
103 // |
|
104 CWWWHotlistParser::~CWWWHotlistParser() |
|
105 { |
|
106 if(iHotlistItemList) |
|
107 iHotlistItemList->ResetAndDestroy(); |
|
108 |
|
109 delete iHotlistItemList; |
|
110 } |
|
111 |
|
112 // |
|
113 // factory fn |
|
114 // |
|
115 CWWWHotlistParser* CWWWHotlistParser::NewL(RFs& aFs) |
|
116 { |
|
117 CWWWHotlistParser* self = new(ELeave)CWWWHotlistParser(aFs); |
|
118 CleanupStack::PushL(self); |
|
119 self->ConstructL(); |
|
120 CleanupStack::Pop(); |
|
121 return self; |
|
122 } |
|
123 |
|
124 void CWWWHotlistParser::ConstructL() |
|
125 { |
|
126 iHotlistItemList = new(ELeave)CArrayPtrSeg<CWWWHotlistItem>(5); |
|
127 } |
|
128 |
|
129 // |
|
130 // Parse/Set data members |
|
131 // |
|
132 void CWWWHotlistParser::ParseL(CParsedFieldCollection& aIacpFields) |
|
133 { |
|
134 CWWWHotlistItem* item = NULL; |
|
135 // step through |
|
136 TInt count = aIacpFields.Count(); |
|
137 for(TInt i = 0; i< count;i++) |
|
138 { |
|
139 CParsedField& field = *(aIacpFields[i]); |
|
140 if(field.FieldName().Compare(SMS_HOTLIST_ITEM_NAME)==0) |
|
141 { |
|
142 item = new(ELeave)CWWWHotlistItem(); |
|
143 CleanupStack::PushL(item); |
|
144 |
|
145 item->SetNameL(field.FieldValue()); |
|
146 if(++i<count) |
|
147 { |
|
148 CParsedField& urlfield = *(aIacpFields[i]); |
|
149 if(urlfield.FieldName().Compare(SMS_HOTLIST_ITEM_URL)==0) |
|
150 { |
|
151 item->SetUrlL(urlfield.FieldValue()); |
|
152 iHotlistItemList->AppendL(item); |
|
153 CleanupStack::Pop(); |
|
154 } |
|
155 else |
|
156 { |
|
157 CleanupStack::PopAndDestroy(); |
|
158 User::Leave(KErrCorrupt); |
|
159 } |
|
160 } |
|
161 else |
|
162 { |
|
163 CleanupStack::PopAndDestroy(); |
|
164 User::Leave(KErrCorrupt); |
|
165 } |
|
166 } |
|
167 } |
|
168 } |
|
169 // |
|
170 // Currently just write items to a file |
|
171 // |
|
172 void CWWWHotlistParser::ProcessL(CMsvEntry& aEntry) |
|
173 { |
|
174 StoreL(aEntry); |
|
175 } |
|
176 |
|
177 // |
|
178 // StoreL() - Store bookmarks as an attachment file |
|
179 // in the .eBM format. If the system runs out of memory while |
|
180 // the bookmarks are being written to the file, the file will be |
|
181 // deleted. For example, if 2 bookmarks have already been written |
|
182 // to the file, and the writing of the third bookmark fails, the |
|
183 // file will be deleted. Otherwise, a failure of file writing would |
|
184 // need to be handled differently from memory allocation failure. |
|
185 // |
|
186 void CWWWHotlistParser::StoreL(CMsvEntry& aEntry) |
|
187 { |
|
188 TInt numberOfItems = iHotlistItemList->Count(); |
|
189 |
|
190 if (numberOfItems>0) // Only create a file if there is something to save! |
|
191 { |
|
192 |
|
193 // Generate fileName from msgId and bookmark file extension. |
|
194 // The file name consists of the msgId in hex format |
|
195 // followed by .eBM. Sizeof operator returns the size of msgId |
|
196 // in bytes and each byte requires 2 hex digits to represent it, |
|
197 // hence sizeof is multipled by 2. Didn't want to make |
|
198 // fileNameLength constant because the size of TMsvId may change |
|
199 // in the future. |
|
200 |
|
201 TMsvId entryId = aEntry.Entry().Id(); |
|
202 |
|
203 TInt fileNameLength = 2*sizeof(entryId) + |
|
204 KEBookmarkExtension().Length(); |
|
205 |
|
206 HBufC *fileName = HBufC::NewLC(fileNameLength); |
|
207 |
|
208 TPtr fileNameDes = fileName->Des(); |
|
209 |
|
210 // The file name uses the hex representation of the entry Id. |
|
211 // If this changes to some other representation then |
|
212 // fileNameLength will need to be calculated differently. |
|
213 |
|
214 fileNameDes.Num(entryId,EHex); |
|
215 |
|
216 fileNameDes.Append(KEBookmarkExtension); |
|
217 |
|
218 // Get the attachment manager and create an empty attachment file |
|
219 CMsvStore* store = aEntry.EditStoreL(); |
|
220 CleanupStack::PushL(store); |
|
221 |
|
222 MMsvAttachmentManagerSync& managerSync = store->AttachmentManagerExtensionsL(); |
|
223 CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile); |
|
224 CleanupStack::PushL(attachment); |
|
225 attachment->SetAttachmentNameL(*fileName); |
|
226 RFile file; |
|
227 managerSync.CreateAttachmentL(*fileName, file, attachment); |
|
228 CleanupStack::Pop(attachment); // ownership passed |
|
229 CleanupClosePushL(file); |
|
230 |
|
231 #ifdef SYMBIAN_BOOKMARK_DATABASE |
|
232 // Open the bookmark database ready to add the bookmarks |
|
233 RBkDatabase bookmarkDb; |
|
234 bookmarkDb.OpenL(); |
|
235 CleanupClosePushL(bookmarkDb); |
|
236 #endif // SYMBIAN_BOOKMARK_DATABASE |
|
237 |
|
238 // Stream each bookmark into the file. |
|
239 // The eBookmark file must contain only 8bit ascii. |
|
240 // Add a linefeed to the end of each line. |
|
241 |
|
242 for(TInt count=0; count < numberOfItems; count++) |
|
243 { |
|
244 CWWWHotlistItem &item = *iHotlistItemList->At(count); |
|
245 |
|
246 // Allocate enough space to hold the full bookmark entry. |
|
247 |
|
248 TInt length = item.Name().Length() + |
|
249 item.Url().Length() + |
|
250 KEBookmarkConstantChars; |
|
251 |
|
252 HBufC8 *writeBuf = HBufC8::NewLC(length); |
|
253 |
|
254 TPtr8 des = writeBuf->Des(); |
|
255 |
|
256 des.Append(KEBookmarkItemBegin); |
|
257 des.Append(KEBookmarkItemURL); |
|
258 des.Append(item.Url()); |
|
259 des.Append(KCRLinefeed); |
|
260 des.Append(KEBookmarkItemName); |
|
261 des.Append(item.Name()); |
|
262 des.Append(KCRLinefeed); |
|
263 des.Append(KEBookmarkType); |
|
264 des.Append(KEBookmarkItemEnd); |
|
265 |
|
266 User::LeaveIfError(file.Write(des)); |
|
267 |
|
268 CleanupStack::PopAndDestroy(); |
|
269 |
|
270 #ifdef SYMBIAN_BOOKMARK_DATABASE |
|
271 // Add the bookmark to the bookmark database |
|
272 RBkBookmark bookmark = bookmarkDb.CreateBookmarkL(); |
|
273 CleanupClosePushL(bookmark); |
|
274 bookmark.SetTitleL(item.Name()); |
|
275 // Convert Uri to 8-bit |
|
276 HBufC8* bookmarkUri = HBufC8::NewLC(item.Url().Length()); |
|
277 bookmarkUri->Des().Copy(item.Url()); |
|
278 bookmark.SetUriL(*bookmarkUri); |
|
279 CleanupStack::PopAndDestroy(2, &bookmark); // bookmarkUri, bookmark |
|
280 #endif // SYMBIAN_BOOKMARK_DATABASE |
|
281 } |
|
282 |
|
283 #ifdef SYMBIAN_BOOKMARK_DATABASE |
|
284 // Commit all the added bookmarks and close bookmark db |
|
285 bookmarkDb.CommitL(); |
|
286 CleanupStack::PopAndDestroy(&bookmarkDb); |
|
287 #endif // SYMBIAN_BOOKMARK_DATABASE |
|
288 |
|
289 // File writing has completed, set the size in the attachment |
|
290 TInt fileSize; |
|
291 User::LeaveIfError(file.Size(fileSize)); |
|
292 attachment->SetSize(fileSize); |
|
293 |
|
294 // commit the changes |
|
295 store->CommitL(); |
|
296 CleanupStack::PopAndDestroy(3, fileName); // file, store, fileName |
|
297 } |
|
298 } |
|
299 |