|
1 // Copyright (c) 2006-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 // |
|
15 |
|
16 #include "agsattachmentindex.h" |
|
17 #include "agmentry.h" |
|
18 #include "agmpanic.h" |
|
19 #include "agsfilemanager.h" |
|
20 #include "agmdebug.h" |
|
21 |
|
22 #include <badesca.h> |
|
23 |
|
24 // CAgnAttachmentIndexItem // |
|
25 |
|
26 // Create a new attachment index item for a given attachment and the entry that owns it |
|
27 CAgnAttachmentIndexItem* CAgnAttachmentIndexItem::NewL(const CAgnAttachmentFile& aAttachment, TCalLocalUid aLocalUid) |
|
28 { |
|
29 CAgnAttachmentIndexItem* self = new (ELeave) CAgnAttachmentIndexItem(aAttachment); |
|
30 CleanupStack::PushL(self); |
|
31 self->ConstructL(aAttachment, aLocalUid); |
|
32 CleanupStack::Pop(self); |
|
33 return self; |
|
34 } |
|
35 |
|
36 // Create a new attachment index from a read stream |
|
37 CAgnAttachmentIndexItem* CAgnAttachmentIndexItem::NewL(RReadStream& aReadStream) |
|
38 { |
|
39 CAgnAttachmentIndexItem* self = new (ELeave) CAgnAttachmentIndexItem(); |
|
40 CleanupStack::PushL(self); |
|
41 self->InternalizeL(aReadStream); |
|
42 CleanupStack::Pop(self); |
|
43 return self; |
|
44 } |
|
45 |
|
46 |
|
47 // Create a new attachment index item for a given attachment |
|
48 CAgnAttachmentIndexItem::CAgnAttachmentIndexItem(const CAgnAttachmentFile& aAttachment) : |
|
49 iSize(aAttachment.Size()), |
|
50 iLastModifiedDateUtc(aAttachment.LastModifiedTimeUtc()), |
|
51 iUid(aAttachment.Uid()) |
|
52 { |
|
53 } |
|
54 |
|
55 CAgnAttachmentIndexItem::CAgnAttachmentIndexItem() |
|
56 { |
|
57 } |
|
58 |
|
59 void CAgnAttachmentIndexItem::ConstructL(const CAgnAttachmentFile& aAttachment, TCalLocalUid aLocalUid) |
|
60 { |
|
61 iEntries.AppendL(aLocalUid); |
|
62 iFileName = aAttachment.FileName().AllocL(); |
|
63 } |
|
64 |
|
65 CAgnAttachmentIndexItem::~CAgnAttachmentIndexItem() |
|
66 { |
|
67 delete iFileName; |
|
68 iEntries.Reset(); |
|
69 } |
|
70 |
|
71 TInt CAgnAttachmentIndexItem::Size() const |
|
72 { |
|
73 return iSize; |
|
74 } |
|
75 |
|
76 void CAgnAttachmentIndexItem::SetFileName(HBufC* aFileName) |
|
77 { |
|
78 delete iFileName; |
|
79 iFileName = aFileName; |
|
80 } |
|
81 |
|
82 const TTime& CAgnAttachmentIndexItem::LastModifiedDateUtc() const |
|
83 { |
|
84 return iLastModifiedDateUtc; |
|
85 } |
|
86 |
|
87 const TDesC& CAgnAttachmentIndexItem::FileName() const |
|
88 { |
|
89 if (iFileName) |
|
90 { |
|
91 return *iFileName; |
|
92 } |
|
93 return KNullDesC; |
|
94 } |
|
95 |
|
96 void CAgnAttachmentIndexItem::InternalizeL(RReadStream& aReadStream) |
|
97 { |
|
98 iSize = aReadStream.ReadInt32L(); |
|
99 |
|
100 TInt64 time64; |
|
101 aReadStream >> time64; |
|
102 TTime temptime(time64); |
|
103 iLastModifiedDateUtc = temptime; |
|
104 |
|
105 iFileName = HBufC::NewL(aReadStream, KMaxTInt); |
|
106 |
|
107 TUint32 count = aReadStream.ReadUint32L(); |
|
108 for (TUint32 i = 0; i < count; ++i) |
|
109 { |
|
110 TCalLocalUid localid = static_cast<TCalLocalUid>(aReadStream.ReadUint32L()); |
|
111 iEntries.Append(localid); |
|
112 } |
|
113 |
|
114 iUid = static_cast<TCalAttachmentUid>(aReadStream.ReadUint32L()); |
|
115 } |
|
116 |
|
117 void CAgnAttachmentIndexItem::ExternalizeL(RWriteStream& aWriteStream) const |
|
118 { |
|
119 aWriteStream.WriteInt32L(iSize); |
|
120 |
|
121 TInt64 time64 = iLastModifiedDateUtc.Int64(); |
|
122 aWriteStream << time64; |
|
123 |
|
124 aWriteStream << *iFileName; |
|
125 |
|
126 TUint32 count = iEntries.Count(); |
|
127 aWriteStream.WriteUint32L(count); |
|
128 for (TUint32 i=0; i < count; ++i) |
|
129 { |
|
130 aWriteStream.WriteUint32L(iEntries[i]); |
|
131 } |
|
132 |
|
133 aWriteStream.WriteUint32L(iUid); |
|
134 } |
|
135 |
|
136 |
|
137 // Return the array of entries that contain this attachment. |
|
138 // Multiple entries owning the same attachment can only occur if an entry is copied. |
|
139 const RArray<TCalLocalUid>& CAgnAttachmentIndexItem::Entries() const |
|
140 { |
|
141 return iEntries; |
|
142 } |
|
143 |
|
144 TCalAttachmentUid CAgnAttachmentIndexItem::Uid() const |
|
145 { |
|
146 return iUid; |
|
147 } |
|
148 |
|
149 // Called when another entry is added with the same attachment |
|
150 void CAgnAttachmentIndexItem::AddEntryL(TCalLocalUid aLocalUid) |
|
151 { |
|
152 // Don't allow duplicate local UIDs. This would only happen if an entry was associated with an attachment more than once |
|
153 // which is invalid anyway. |
|
154 if (iEntries.Find(aLocalUid) == KErrNotFound) |
|
155 { |
|
156 iEntries.AppendL(aLocalUid); |
|
157 } |
|
158 } |
|
159 |
|
160 // Called when an entry containing this attachment has been deleted |
|
161 void CAgnAttachmentIndexItem::DeleteEntry(TCalLocalUid aLocalUid) |
|
162 { |
|
163 TInt pos = iEntries.Find(aLocalUid); |
|
164 if (pos != KErrNotFound) |
|
165 { |
|
166 iEntries.Remove(pos); |
|
167 } |
|
168 } |
|
169 |
|
170 // CAgnAttachmentIndex // |
|
171 void CAgnAttachmentIndex::InternalizeL(RReadStream& aReadStream) |
|
172 { |
|
173 TUint32 count = aReadStream.ReadUint32L(); |
|
174 for (TUint32 i=0; i < count; ++i) |
|
175 { |
|
176 CAgnAttachmentIndexItem* item = CAgnAttachmentIndexItem::NewL(aReadStream); |
|
177 iIndex.Append(item); |
|
178 } |
|
179 } |
|
180 |
|
181 |
|
182 void CAgnAttachmentIndex::ExternalizeL(RWriteStream& aWriteStream) const |
|
183 { |
|
184 TUint32 count = iIndex.Count(); |
|
185 aWriteStream.WriteUint32L(count); |
|
186 for (TUint32 i=0; i < count; ++i) |
|
187 { |
|
188 iIndex[i]->ExternalizeL(aWriteStream); |
|
189 } |
|
190 |
|
191 } |
|
192 |
|
193 |
|
194 CAgnAttachmentIndex::CAgnAttachmentIndex() |
|
195 { |
|
196 } |
|
197 |
|
198 CAgnAttachmentIndex::~CAgnAttachmentIndex() |
|
199 { |
|
200 delete iFileRollbackArray; |
|
201 Reset(); |
|
202 } |
|
203 |
|
204 void CAgnAttachmentIndex::Reset() |
|
205 { |
|
206 iIndex.ResetAndDestroy(); |
|
207 } |
|
208 |
|
209 // add an entry's attachments to the index |
|
210 void CAgnAttachmentIndex::AddEntryL(CAgnEntry& aEntry) |
|
211 { |
|
212 const TInt KAttachCount = aEntry.AttachmentCount(); |
|
213 for (TInt ii = 0; ii < KAttachCount; ++ii) |
|
214 { |
|
215 CAgnAttachment& attach = aEntry.Attachment(ii); |
|
216 if (attach.Uid() && attach.Type() == CCalContent::EDispositionInline) |
|
217 { |
|
218 TInt index = AttachmentIndex(attach.Uid()); |
|
219 CAgnAttachmentFile* attachFile = static_cast<CAgnAttachmentFile*>(&attach); |
|
220 // Add a new attach item to the attach index if the attachment uid is not found from the index |
|
221 //Otherwise, there is existing attachment already in the index. Whether should the entry share or update the attach item depends on: |
|
222 // 1. Update the attach item if the entry has the same entry uid as the one in the index (In this case, the entry is to be updated) |
|
223 // 2. Add an attach item if the drive name of the attachment of the entry is different from the one in the index |
|
224 // 3. Share the attachment if the drive name of the attachment of the entry is the same as the one in the index |
|
225 |
|
226 TBool addNewItem = ETrue; |
|
227 TBool shareItem = EFalse; |
|
228 |
|
229 if (index >= 0) |
|
230 {// An attacment with the same attach uid has been found from the attach index |
|
231 const TDesC& filename = iIndex[index]->FileName(); |
|
232 TDriveName oldDrive = filename.Left(2); |
|
233 TDriveName newDrive = attachFile->Drive(); |
|
234 TBool isSameDrive = (oldDrive.CompareF(newDrive) == 0); |
|
235 const RArray<TCalLocalUid> entries = iIndex[index]->Entries(); |
|
236 TInt entryCount = entries.Count();//Those entries have shared the same attachments. |
|
237 TInt jj=entryCount; |
|
238 for (jj=0; jj<entryCount; ++jj) |
|
239 { |
|
240 if(entries[jj] == aEntry.LocalUid()) |
|
241 { |
|
242 //This entry is to be updated so update its attachment drive if it is different otherwise don't do anything since attachment is same |
|
243 addNewItem = EFalse; |
|
244 if(!isSameDrive)//drive is not the same |
|
245 { |
|
246 HBufC* newfilename = filename.AllocL(); |
|
247 newfilename->Des().Replace(0,2,newDrive); |
|
248 iIndex[index]->SetFileName(newfilename); |
|
249 } |
|
250 break; |
|
251 } |
|
252 } |
|
253 |
|
254 if (jj == entryCount)//This is newly added entry |
|
255 {//check the drive has been changed or not |
|
256 if(isSameDrive)//drive is the same |
|
257 { |
|
258 addNewItem = EFalse;//This entry will share an exiting attachment |
|
259 shareItem = ETrue; |
|
260 } |
|
261 } |
|
262 } |
|
263 |
|
264 if(addNewItem && attachFile->FileName().Length() > 0) |
|
265 { |
|
266 CAgnAttachmentIndexItem* newItem = CAgnAttachmentIndexItem::NewL(*attachFile, aEntry.LocalUid()); |
|
267 CleanupStack::PushL(newItem); |
|
268 iIndex.AppendL(newItem); |
|
269 CleanupStack::Pop(newItem); |
|
270 } |
|
271 else if (shareItem ) |
|
272 {//The entry will share this attachment |
|
273 iIndex[index]->AddEntryL(aEntry.LocalUid()); |
|
274 } |
|
275 } |
|
276 } |
|
277 |
|
278 // see if this entry replaces an existing entry and some of the attachments are no longer present |
|
279 const TInt KEntryLocalUid = aEntry.LocalUid(); |
|
280 for (TInt i = iIndex.Count() - 1; i >= 0; --i) |
|
281 { |
|
282 // check every attachment |
|
283 const CAgnAttachmentIndexItem* KAttachmentItem = iIndex[i]; |
|
284 for (TInt j = 0; j < KAttachmentItem->Entries().Count(); ++j) |
|
285 { |
|
286 // check every entry associated with each attachment |
|
287 if (KEntryLocalUid == (KAttachmentItem->Entries())[j]) |
|
288 { |
|
289 // If the entry being updated used to contain this attachment, check it still does |
|
290 TBool attachmentFound = EFalse; |
|
291 for (TInt k = 0; k < KAttachCount; ++k) |
|
292 { |
|
293 if (aEntry.Attachment(k).Uid() == KAttachmentItem->Uid()) |
|
294 { |
|
295 attachmentFound = ETrue; |
|
296 break; |
|
297 } |
|
298 } |
|
299 if ( ! attachmentFound) |
|
300 { |
|
301 // this entry used to be associated with this attachment but isn't any more |
|
302 RemoveEntryFromAttachmentL(aEntry.LocalUid(), KAttachmentItem->Uid()); |
|
303 } |
|
304 } |
|
305 } |
|
306 } |
|
307 } |
|
308 |
|
309 // remove an entry's attachments from the index |
|
310 void CAgnAttachmentIndex::DeleteEntryL(CAgnEntry& aEntry) |
|
311 { |
|
312 const TInt KAttachCount = aEntry.AttachmentCount(); |
|
313 for (TInt j = 0; j < KAttachCount; ++j) |
|
314 { |
|
315 CAgnAttachment& attach = aEntry.Attachment(j); |
|
316 if (attach.Type() == CCalContent::EDispositionInline) |
|
317 { |
|
318 RemoveEntryFromAttachmentL(aEntry.LocalUid(), attach.Uid()); |
|
319 } |
|
320 } |
|
321 } |
|
322 |
|
323 // remove a particular entry from a particular attachment in the index |
|
324 void CAgnAttachmentIndex::RemoveEntryFromAttachmentL(TCalLocalUid aEntryUid, TCalAttachmentUid aAttachmentUid) |
|
325 { |
|
326 TInt index = AttachmentIndex(aAttachmentUid); |
|
327 if (index != KErrNotFound && iIndex[index]) |
|
328 { |
|
329 iIndex[index]->DeleteEntry(aEntryUid); |
|
330 _DBGLOG_ATTACH(AgmDebug::DebugLog("Remove Entry Attachment: Removing entry from attachment using Entry UID='%d', Attachment UID='%d'",aEntryUid,aAttachmentUid);) |
|
331 if (iIndex[index]->Entries().Count() == 0) |
|
332 { |
|
333 // if there are no more entries with this attachment, mark this file for deletion |
|
334 if (!iFileRollbackArray) |
|
335 { |
|
336 iFileRollbackArray = new (ELeave) CDesCArraySeg(8); |
|
337 } |
|
338 iFileRollbackArray->AppendL(iIndex[index]->FileName()); |
|
339 delete iIndex[index]; |
|
340 iIndex.Remove(index); |
|
341 } |
|
342 } |
|
343 } |
|
344 |
|
345 void CAgnAttachmentIndex::RemoveAttachment(TCalAttachmentUid aUid) |
|
346 { |
|
347 TInt index = AttachmentIndex(aUid); |
|
348 if (index != KErrNotFound) |
|
349 { |
|
350 _DBGLOG_ATTACH(AgmDebug::DebugLog("Remove Attachment: Removing attachment with Attachment UID='%d'", aUid);) |
|
351 delete iIndex[index]; |
|
352 iIndex.Remove(index); |
|
353 } |
|
354 } |
|
355 |
|
356 // delete all attachment files already marked for deletion in RemoveEntryFromAttachmentL |
|
357 void CAgnAttachmentIndex::CommitL(CAgnServFile& aServFile) |
|
358 { |
|
359 if (iFileRollbackArray) |
|
360 { |
|
361 for (TInt i = 0; i < iFileRollbackArray->Count(); ++i) |
|
362 { |
|
363 aServFile.DeleteFileL((*iFileRollbackArray)[i]); |
|
364 } |
|
365 |
|
366 delete iFileRollbackArray; |
|
367 iFileRollbackArray = NULL; |
|
368 } |
|
369 } |
|
370 |
|
371 // cancel any file deletes |
|
372 void CAgnAttachmentIndex::Rollback() |
|
373 { |
|
374 delete iFileRollbackArray; |
|
375 iFileRollbackArray = NULL; |
|
376 } |
|
377 |
|
378 TInt CAgnAttachmentIndex::AttachmentIndex(TCalAttachmentUid aUid) const |
|
379 { |
|
380 const TInt KCount = iIndex.Count(); |
|
381 for (TInt i = 0; i < KCount; ++i) |
|
382 { |
|
383 if (iIndex[i]->Uid() == aUid) |
|
384 { |
|
385 return i; |
|
386 } |
|
387 } |
|
388 return KErrNotFound; |
|
389 } |
|
390 |
|
391 const CAgnAttachmentIndexItem* CAgnAttachmentIndex::Attachment(TCalAttachmentUid aUid) const |
|
392 { |
|
393 _DBGLOG_ATTACH(AgmDebug::DebugLog("Attachment: Getting attachment using Uid - %d",aUid);) |
|
394 |
|
395 TInt index = AttachmentIndex(aUid); |
|
396 if (index != KErrNotFound) |
|
397 { |
|
398 return iIndex[index]; |
|
399 } |
|
400 return NULL; |
|
401 } |
|
402 |
|
403 TInt CAgnAttachmentIndex::Count() const |
|
404 { |
|
405 return iIndex.Count(); |
|
406 } |
|
407 |
|
408 typedef TInt (*SortFunctionPtr)(const CAgnAttachmentIndexItem&, const CAgnAttachmentIndexItem&); |
|
409 |
|
410 // return a new array of pointers to attachments, ordered by size of last modified date |
|
411 void CAgnAttachmentIndex::GetSortedIndexL(CCalAttachmentManager::TSortOrder aSortOrder, RPointerArray<CAgnAttachmentIndexItem>& aAttachments) const |
|
412 { |
|
413 SortFunctionPtr sortFunction = NULL; |
|
414 switch (aSortOrder) |
|
415 { |
|
416 case CCalAttachmentManager::ESortBySizeLargestFirst: |
|
417 sortFunction = CAgnAttachmentIndex::SortBySizeLargestFirst; |
|
418 break; |
|
419 case CCalAttachmentManager::ESortByDateModifiedNewestFirst: |
|
420 sortFunction = CAgnAttachmentIndex::SortByDateModifiedNewestFirst; |
|
421 break; |
|
422 case CCalAttachmentManager::ESortByDateModifiedOldestFirst: |
|
423 sortFunction = CAgnAttachmentIndex::SortByDateModifiedOldestFirst; |
|
424 break; |
|
425 default: |
|
426 Panic(EAgmErrInvalidAttachmentSortOrder); |
|
427 } |
|
428 TLinearOrder<CAgnAttachmentIndexItem> order(*sortFunction); |
|
429 |
|
430 const TInt KCount = iIndex.Count(); |
|
431 for (TInt i = 0; i < KCount; ++i) |
|
432 { |
|
433 aAttachments.InsertInOrderAllowRepeatsL(iIndex[i], order); |
|
434 } |
|
435 } |
|
436 |
|
437 /*static*/ TInt CAgnAttachmentIndex::SortBySizeLargestFirst(const CAgnAttachmentIndexItem& aLeft, const CAgnAttachmentIndexItem& aRight) |
|
438 { |
|
439 return aRight.Size() - aLeft.Size(); |
|
440 } |
|
441 |
|
442 /*static*/ TInt CAgnAttachmentIndex::SortByDateModifiedNewestFirst(const CAgnAttachmentIndexItem& aLeft, const CAgnAttachmentIndexItem& aRight) |
|
443 { |
|
444 if (aRight.LastModifiedDateUtc() > aLeft.LastModifiedDateUtc()) |
|
445 { |
|
446 return 1; |
|
447 } |
|
448 else if (aRight.LastModifiedDateUtc() == aLeft.LastModifiedDateUtc()) |
|
449 { |
|
450 return 0; |
|
451 } |
|
452 return -1; |
|
453 } |
|
454 |
|
455 /*static*/ TInt CAgnAttachmentIndex::SortByDateModifiedOldestFirst(const CAgnAttachmentIndexItem& aLeft, const CAgnAttachmentIndexItem& aRight) |
|
456 { |
|
457 // return opposite value of SortByDateModifiedNewestFirst |
|
458 return ( - SortByDateModifiedNewestFirst(aLeft, aRight)); |
|
459 } |
|
460 |
|
461 const TDesC& CAgnAttachmentIndex::FileName(TCalAttachmentUid aUid) |
|
462 { |
|
463 // Get the file name for given attachment uid. Return KNullDesC if it the attachment with that uid is not found |
|
464 |
|
465 TInt index = AttachmentIndex(aUid); |
|
466 |
|
467 if (index >= 0) |
|
468 { |
|
469 return iIndex[index]->FileName(); |
|
470 } |
|
471 |
|
472 return KNullDesC(); |
|
473 } |