|
1 /* |
|
2 * Copyright (c) 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 the License "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: A class that fetches resources via RFile. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <e32std.h> |
|
21 #include <f32file.h> |
|
22 |
|
23 #include "FileHandler.h" |
|
24 #include "LeakTracker.h" |
|
25 #include "Logger.h" |
|
26 |
|
27 |
|
28 // ----------------------------------------------------------------------------- |
|
29 // CFileHandler::NewL |
|
30 // |
|
31 // Two-phased constructor. |
|
32 // ----------------------------------------------------------------------------- |
|
33 // |
|
34 CFileHandler* CFileHandler::NewL() |
|
35 { |
|
36 CFileHandler* self = new (ELeave) CFileHandler(); |
|
37 |
|
38 CleanupStack::PushL(self); |
|
39 self->ConstructL(); |
|
40 CleanupStack::Pop(); |
|
41 |
|
42 return self; |
|
43 } |
|
44 |
|
45 |
|
46 // ----------------------------------------------------------------------------- |
|
47 // CFileHandler::CFileHandler |
|
48 // C++ default constructor can NOT contain any code, that |
|
49 // might leave. |
|
50 // ----------------------------------------------------------------------------- |
|
51 // |
|
52 CFileHandler::CFileHandler(): |
|
53 iLeakTracker(CLeakTracker::EFileHandler) |
|
54 { |
|
55 } |
|
56 |
|
57 |
|
58 // ----------------------------------------------------------------------------- |
|
59 // CFileHandler::ConstructL |
|
60 // Symbian 2nd phase constructor can leave. |
|
61 // ----------------------------------------------------------------------------- |
|
62 // |
|
63 void CFileHandler::ConstructL() |
|
64 { |
|
65 iIdle = CIdle::NewL(CActive::EPriorityIdle); |
|
66 } |
|
67 |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 // CFileHandler::~CFileHandler |
|
71 // Deconstructor. |
|
72 // ----------------------------------------------------------------------------- |
|
73 // |
|
74 CFileHandler::~CFileHandler() |
|
75 { |
|
76 delete iIdle; |
|
77 delete iUrl; |
|
78 } |
|
79 |
|
80 |
|
81 // ----------------------------------------------------------------------------- |
|
82 // CFileHandler::LoadUrl |
|
83 // |
|
84 // Loads the given url -- asynchronously |
|
85 // ----------------------------------------------------------------------------- |
|
86 // |
|
87 void CFileHandler::LoadUrlL(const TDesC& aUrl, MLoadObserver& aObserver) |
|
88 { |
|
89 iUrl = aUrl.AllocL(); |
|
90 iObserver = &aObserver; |
|
91 |
|
92 // To be consistent with other asynchronous UrlHandlers the buffer isn't |
|
93 // processed until after the callstack unrolls. |
|
94 iIdle->Start(TCallBack(LoadCompleted, this)); |
|
95 } |
|
96 |
|
97 |
|
98 // ----------------------------------------------------------------------------- |
|
99 // CFileHandler::LoadCompleted |
|
100 // |
|
101 // Loads and passes the buffer to the observer. |
|
102 // ----------------------------------------------------------------------------- |
|
103 // |
|
104 TInt CFileHandler::LoadCompleted(TAny* aPtr) |
|
105 { |
|
106 CFileHandler* self = static_cast<CFileHandler*>(aPtr); |
|
107 HBufC8* buffer = NULL; |
|
108 |
|
109 TRAPD(err, buffer = self->LoadCompletedHelperL()); |
|
110 |
|
111 // Pass the buffer to the observer. |
|
112 self->iObserver->LoadCompleted(err, buffer, KNullDesC, KNullDesC); |
|
113 |
|
114 return EFalse; |
|
115 } |
|
116 |
|
117 |
|
118 // ----------------------------------------------------------------------------- |
|
119 // CFileHandler::LoadCompletedHelperL |
|
120 // |
|
121 // Loads and passes the buffer to the observer. |
|
122 // ----------------------------------------------------------------------------- |
|
123 // |
|
124 HBufC8* CFileHandler::LoadCompletedHelperL() |
|
125 { |
|
126 _LIT(KFileScheme, "file://"); |
|
127 |
|
128 TInt size; |
|
129 HBufC16* correcttedPath = NULL; |
|
130 HBufC8* buffer = NULL; |
|
131 TPtr8 bufferPtr(NULL, 0); |
|
132 |
|
133 // Extract the file path from the url. |
|
134 TPtr path(iUrl->Des()); |
|
135 |
|
136 if (iUrl->Find(KFileScheme) == 0) |
|
137 { |
|
138 path.Set((TUint16*) iUrl->Ptr() + KFileScheme().Length(), |
|
139 iUrl->Length() - KFileScheme().Length(), iUrl->Length() - |
|
140 KFileScheme().Length()); |
|
141 |
|
142 correcttedPath = HBufC16::NewLC(path.Length()); |
|
143 correcttedPath->Des() = path; |
|
144 path.Set(correcttedPath->Des()); |
|
145 |
|
146 for (TInt i = 0; i < path.Length(); i++) |
|
147 { |
|
148 if (path[i] == '/') |
|
149 { |
|
150 path[i] = '\\'; |
|
151 } |
|
152 } |
|
153 } |
|
154 |
|
155 // Otherwise leave as this isn't a file url. |
|
156 else |
|
157 { |
|
158 User::Leave(KErrNotSupported); |
|
159 } |
|
160 |
|
161 // Open the file. |
|
162 RFs rfs; |
|
163 RFile file; |
|
164 |
|
165 User::LeaveIfError(rfs.Connect()); |
|
166 CleanupClosePushL(rfs); |
|
167 |
|
168 User::LeaveIfError(file.Open(rfs, path, EFileRead | EFileShareReadersOnly)); |
|
169 CleanupClosePushL(file); |
|
170 |
|
171 // Read file |
|
172 User::LeaveIfError(file.Size(size)); |
|
173 |
|
174 buffer = HBufC8::NewL(size); |
|
175 bufferPtr.Set(buffer->Des()); |
|
176 |
|
177 file.Read(bufferPtr, size); |
|
178 |
|
179 // Clean up. |
|
180 CleanupStack::PopAndDestroy(/*file*/); |
|
181 CleanupStack::PopAndDestroy(/*rfs*/); |
|
182 CleanupStack::PopAndDestroy(correcttedPath); |
|
183 |
|
184 return buffer; |
|
185 } |