|
1 /* |
|
2 * Copyright (c) 2002 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: |
|
15 * Attachment file handler |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDES |
|
21 #include "CPbkAttachmentFile.h" |
|
22 #include <e32std.h> |
|
23 |
|
24 /// Unnamed namespace for local definitions |
|
25 namespace { |
|
26 |
|
27 // LOCAL CONSTANTS |
|
28 _LIT(KRamDrive, "d:"); |
|
29 _LIT(KPersistentDrive, "c:"); |
|
30 _LIT(KTempFilePath, "\\system\\temp\\"); |
|
31 _LIT(KInvalidFileNameChars, "?*<>/\"|\\:"); |
|
32 _LIT(KDefaultTempFileName, "TEMP.tmp"); |
|
33 const TInt KMaxDriveNameLength = 2; |
|
34 const TInt KMaxNumberLength = 5; |
|
35 const TInt KMaxFileNumber = 99999; |
|
36 |
|
37 #ifdef _DEBUG |
|
38 enum TPanicCode |
|
39 { |
|
40 EPanicPostCond_Constructor = 1, |
|
41 EPanicPreCond_ConstructL, |
|
42 EPanicPostCond_ConstructL, |
|
43 EPanicPreCond_CreateFileL, |
|
44 EPanicPostCond_CreateFileL |
|
45 }; |
|
46 |
|
47 void Panic(TPanicCode aReason) |
|
48 { |
|
49 _LIT(KPanicText, "CPbkAttachmentFile"); |
|
50 User::Panic(KPanicText,aReason); |
|
51 } |
|
52 |
|
53 #endif // _DEBUG |
|
54 |
|
55 } // namespace |
|
56 |
|
57 |
|
58 // ================= MEMBER FUNCTIONS ======================= |
|
59 |
|
60 EXPORT_C CPbkAttachmentFile* CPbkAttachmentFile::NewL |
|
61 (const TDesC& aBaseName, |
|
62 RFs& aRfs, |
|
63 TUint aFileMode /*= EFileStream|EFileShareExclusive|EFileWrite*/) |
|
64 { |
|
65 CPbkAttachmentFile* self = CPbkAttachmentFile::NewLC(aBaseName, aRfs, aFileMode); |
|
66 CleanupStack::Pop(); // self |
|
67 return self; |
|
68 } |
|
69 |
|
70 EXPORT_C CPbkAttachmentFile* CPbkAttachmentFile::NewLC |
|
71 (const TDesC& aBaseName, |
|
72 RFs& aRfs, |
|
73 TUint aFileMode /*= EFileStream|EFileShareExclusive|EFileWrite*/) |
|
74 { |
|
75 CPbkAttachmentFile* self = new(ELeave) CPbkAttachmentFile; |
|
76 CleanupStack::PushL(self); |
|
77 self->iRfs = aRfs; |
|
78 self->iFileMode = aFileMode; |
|
79 self->ConstructL(aBaseName); |
|
80 return self; |
|
81 } |
|
82 |
|
83 CPbkAttachmentFile::~CPbkAttachmentFile() |
|
84 { |
|
85 DeleteFile(); |
|
86 delete iFileName; |
|
87 delete iBaseName; |
|
88 } |
|
89 |
|
90 EXPORT_C const TDesC& CPbkAttachmentFile::FileName() const |
|
91 { |
|
92 if (iFileName) |
|
93 { |
|
94 return *iFileName; |
|
95 } |
|
96 else |
|
97 { |
|
98 return KNullDesC; |
|
99 } |
|
100 } |
|
101 |
|
102 EXPORT_C RFile& CPbkAttachmentFile::File() |
|
103 { |
|
104 return iFile; |
|
105 } |
|
106 |
|
107 EXPORT_C void CPbkAttachmentFile::Release() |
|
108 { |
|
109 iFile.Close(); |
|
110 iOwnsFile = EFalse; |
|
111 } |
|
112 |
|
113 EXPORT_C void CPbkAttachmentFile::SwitchDriveL() |
|
114 { |
|
115 TPtrC drive; |
|
116 if (iFileName) |
|
117 { |
|
118 // File already created successfully: switch the drive |
|
119 TParsePtrC fileNameParser(*iFileName); |
|
120 drive.Set(fileNameParser.Drive()==KRamDrive ? KPersistentDrive : KRamDrive); |
|
121 } |
|
122 else |
|
123 { |
|
124 // Default is RAM drive in ConstructL |
|
125 drive.Set(KPersistentDrive); |
|
126 } |
|
127 |
|
128 // Create new file |
|
129 CreateFileL(drive, KTempFilePath, *iBaseName); |
|
130 } |
|
131 |
|
132 CPbkAttachmentFile::CPbkAttachmentFile() |
|
133 { |
|
134 // CBase::operator new will reset members |
|
135 __ASSERT_DEBUG(!iBaseName && !iFileName && iFile.SubSessionHandle()==KNullHandle, |
|
136 Panic(EPanicPostCond_Constructor)); |
|
137 } |
|
138 |
|
139 void CPbkAttachmentFile::ConstructL(const TDesC& aBaseName) |
|
140 { |
|
141 __ASSERT_DEBUG(!iBaseName && !iFileName && iFile.SubSessionHandle()==KNullHandle, |
|
142 Panic(EPanicPreCond_ConstructL)); |
|
143 |
|
144 // Clean up aBaseName |
|
145 const TInt maxLength = KMaxFileName - KMaxDriveNameLength - KTempFilePath().Length() - KMaxNumberLength; |
|
146 iBaseName = HBufC::NewL(maxLength); |
|
147 TPtr baseName = iBaseName->Des(); |
|
148 for (TInt i=0; i < aBaseName.Length() && i < maxLength; ++i) |
|
149 { |
|
150 TChar ch = aBaseName[i]; |
|
151 if (KInvalidFileNameChars().Locate(ch) == KErrNotFound) |
|
152 { |
|
153 baseName.Append(ch); |
|
154 } |
|
155 } |
|
156 baseName.TrimAll(); |
|
157 if (baseName.Length() == 0) |
|
158 { |
|
159 baseName = KDefaultTempFileName; |
|
160 } |
|
161 |
|
162 // Try to create the file |
|
163 TRAPD(err, CreateFileL(KRamDrive, KTempFilePath, baseName)); |
|
164 if (err != KErrNone) |
|
165 { |
|
166 // Try on different drive |
|
167 SwitchDriveL(); |
|
168 } |
|
169 |
|
170 __ASSERT_DEBUG(iBaseName && iFileName, Panic(EPanicPostCond_ConstructL)); |
|
171 } |
|
172 |
|
173 void CPbkAttachmentFile::CreateFileL |
|
174 (const TDesC& aDrive, const TDesC& aDir, const TDesC& aBaseName) |
|
175 { |
|
176 __ASSERT_DEBUG(iBaseName && iRfs.Handle()!=KNullHandle, |
|
177 Panic(EPanicPreCond_CreateFileL)); |
|
178 |
|
179 // Create and init a local buffer for the file name |
|
180 HBufC* fileNameBuf = HBufC::NewLC(KMaxFileName); |
|
181 TPtr fileName = fileNameBuf->Des(); |
|
182 fileName = aDrive; |
|
183 fileName.Append(aDir); |
|
184 // Create directory |
|
185 TInt error = iRfs.MkDirAll(fileName); |
|
186 if (error != KErrNone && error != KErrAlreadyExists) |
|
187 { |
|
188 User::Leave(error); |
|
189 } |
|
190 fileName.Append(aBaseName); |
|
191 |
|
192 TInt number = 0; |
|
193 FOREVER |
|
194 { |
|
195 RFile file; |
|
196 const TInt error = file.Create(iRfs, fileName, iFileMode); |
|
197 |
|
198 TParsePtrC parsePtr(fileName); |
|
199 |
|
200 if (error == KErrNone && parsePtr.Name().Length() > 0) |
|
201 { |
|
202 // File created succesfully: switch state |
|
203 DeleteFile(); // Delete previous file |
|
204 iFile = file; |
|
205 delete iFileName; |
|
206 iFileName = fileNameBuf; |
|
207 iOwnsFile = ETrue; |
|
208 CleanupStack::Pop(); // fileNameBuf |
|
209 __ASSERT_DEBUG(iFileName && iFile.SubSessionHandle()!=KNullHandle, |
|
210 Panic(EPanicPostCond_CreateFileL)); |
|
211 return; |
|
212 } |
|
213 else if (error != KErrAlreadyExists && error != KErrNone) |
|
214 { |
|
215 User::Leave(error); |
|
216 } |
|
217 |
|
218 // File name was reserved: append number to basename to make it unique |
|
219 fileName = aDrive; |
|
220 fileName.Append(aDir); |
|
221 TParsePtrC baseNameParser(aBaseName); |
|
222 fileName.Append(baseNameParser.Name()); |
|
223 fileName.AppendNum(number); |
|
224 fileName.Append(baseNameParser.Ext()); |
|
225 if (++number > KMaxFileNumber) |
|
226 { |
|
227 User::Leave(KErrTooBig); |
|
228 } |
|
229 } |
|
230 } |
|
231 |
|
232 void CPbkAttachmentFile::DeleteFile() |
|
233 { |
|
234 iFile.Close(); |
|
235 if (iFileName && iOwnsFile) |
|
236 { |
|
237 // Delete the file. If the deletion fails for some reason trust that the |
|
238 // system will delete the file when it cleans up temp file directories. |
|
239 iRfs.Delete(*iFileName); |
|
240 } |
|
241 } |
|
242 |
|
243 EXPORT_C CPbkAttachmentFileArray::CPbkAttachmentFileArray(TInt aGranularity) |
|
244 : CArrayPtrFlat<CPbkAttachmentFile>(aGranularity) |
|
245 { |
|
246 } |
|
247 |
|
248 EXPORT_C CPbkAttachmentFileArray::~CPbkAttachmentFileArray() |
|
249 { |
|
250 // Delete all objects in the array |
|
251 ResetAndDestroy(); |
|
252 } |
|
253 |
|
254 EXPORT_C TInt CPbkAttachmentFileArray::MdcaCount() const |
|
255 { |
|
256 return Count(); |
|
257 } |
|
258 |
|
259 EXPORT_C TPtrC CPbkAttachmentFileArray::MdcaPoint(TInt aIndex) const |
|
260 { |
|
261 return At(aIndex)->FileName(); |
|
262 } |
|
263 |
|
264 // End of File |