21
|
1 |
/*
|
|
2 |
* Copyright (c) 2004-2007 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: Wraps document handler functions
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
// INCLUDES
|
|
21 |
#include <DocumentHandler.h>
|
|
22 |
#include <apmstd.h>
|
|
23 |
#include <bautils.h>
|
|
24 |
#include "CFileManagerDocHandler.h"
|
|
25 |
#include "CFileManagerEngine.h"
|
|
26 |
#include "CFileManagerUtils.h"
|
|
27 |
#include "CFileManagerThreadWrapper.h"
|
|
28 |
#include "MFileManagerProcessObserver.h"
|
|
29 |
#include "FileManagerDebug.h"
|
|
30 |
|
|
31 |
|
|
32 |
// -----------------------------------------------------------------------------
|
|
33 |
// CFileManagerDocHandler::CFileManagerDocHandler()
|
|
34 |
//
|
|
35 |
// -----------------------------------------------------------------------------
|
|
36 |
//
|
|
37 |
CFileManagerDocHandler::CFileManagerDocHandler(
|
|
38 |
CFileManagerEngine& aEngine,
|
|
39 |
CFileManagerUtils& aUtils ) :
|
|
40 |
iEngine( aEngine ),
|
|
41 |
iUtils( aUtils ),
|
|
42 |
iFs( aEngine.Fs() )
|
|
43 |
{
|
|
44 |
}
|
|
45 |
|
|
46 |
// -----------------------------------------------------------------------------
|
|
47 |
// CFileManagerDocHandler::NewL()
|
|
48 |
//
|
|
49 |
// ------------------------------------------------------------------------------
|
|
50 |
//
|
|
51 |
CFileManagerDocHandler* CFileManagerDocHandler::NewL(
|
|
52 |
CFileManagerEngine& aEngine,
|
|
53 |
CFileManagerUtils& aUtils )
|
|
54 |
{
|
|
55 |
CFileManagerDocHandler* self = new (ELeave) CFileManagerDocHandler(
|
|
56 |
aEngine, aUtils );
|
|
57 |
CleanupStack::PushL( self );
|
|
58 |
self->ConstructL();
|
|
59 |
CleanupStack::Pop( self );
|
|
60 |
return self;
|
|
61 |
}
|
|
62 |
|
|
63 |
// -----------------------------------------------------------------------------
|
|
64 |
// CFileManagerDocHandler::~CFileManagerDocHandler()
|
|
65 |
//
|
|
66 |
// ------------------------------------------------------------------------------
|
|
67 |
//
|
|
68 |
CFileManagerDocHandler::~CFileManagerDocHandler()
|
|
69 |
{
|
|
70 |
iApaSession.Close();
|
|
71 |
delete iThreadWrapper;
|
|
72 |
delete iFileMime;
|
|
73 |
delete iFileFullPath;
|
|
74 |
delete iDocHandler;
|
|
75 |
iFile.Close();
|
|
76 |
}
|
|
77 |
|
|
78 |
// -----------------------------------------------------------------------------
|
|
79 |
// CFileManagerDocHandler::HandleServerAppExit()
|
|
80 |
//
|
|
81 |
// -----------------------------------------------------------------------------
|
|
82 |
void CFileManagerDocHandler::HandleServerAppExit( TInt aReason )
|
|
83 |
{
|
|
84 |
if ( iEmbeddedAppOpen )
|
|
85 |
{
|
|
86 |
iEngine.NoticeServerAppExit( aReason );
|
|
87 |
iEngine.EmbeddedAppStatus( EFalse );
|
|
88 |
iEmbeddedAppOpen = EFalse;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
// -----------------------------------------------------------------------------
|
|
93 |
// CFileManagerDocHandler::ConstructL()
|
|
94 |
//
|
|
95 |
// -----------------------------------------------------------------------------
|
|
96 |
//
|
|
97 |
void CFileManagerDocHandler::ConstructL()
|
|
98 |
{
|
|
99 |
iDocHandler = CDocumentHandler::NewL();
|
|
100 |
iDocHandler->SetExitObserver( this );
|
|
101 |
User::LeaveIfError( iApaSession.Connect() );
|
|
102 |
}
|
|
103 |
|
|
104 |
// -----------------------------------------------------------------------------
|
|
105 |
// CFileManagerDocHandler::OpenFileL()
|
|
106 |
//
|
|
107 |
// -----------------------------------------------------------------------------
|
|
108 |
//
|
|
109 |
void CFileManagerDocHandler::OpenFileL(
|
|
110 |
const TDesC& aFullPath, MFileManagerProcessObserver* aObserver )
|
|
111 |
{
|
|
112 |
if ( iEmbeddedAppOpen )
|
|
113 |
{
|
|
114 |
ERROR_LOG( "CFileManagerDocHandler::OpenFileL-OpenAlreadyOngoing" )
|
|
115 |
return; // Ignore too fast open events to prevent mess up
|
|
116 |
}
|
|
117 |
|
|
118 |
delete iThreadWrapper;
|
|
119 |
iThreadWrapper = NULL;
|
|
120 |
|
|
121 |
delete iFileFullPath;
|
|
122 |
iFileFullPath = NULL;
|
|
123 |
|
|
124 |
delete iFileMime;
|
|
125 |
iFileMime = NULL;
|
|
126 |
|
|
127 |
iFileReady = EFalse;
|
|
128 |
iFile.Close();
|
|
129 |
|
|
130 |
iObserver = aObserver;
|
|
131 |
|
|
132 |
//Judge the file extention, if a sis file, notify Engine to flush the cache
|
|
133 |
if ( EFileManagerAppFileIcon == iUtils.ResolveIconL( aFullPath ) )
|
|
134 |
{
|
|
135 |
iEngine.SetSisFile( ETrue );
|
|
136 |
}
|
|
137 |
|
|
138 |
// Get fullpath and MIME type
|
|
139 |
TPtrC mime16( iUtils.MimeTypeL( aFullPath ) );
|
|
140 |
iFileMime = HBufC8::NewL( mime16.Length() );
|
|
141 |
iFileMime->Des().Copy( mime16 );
|
|
142 |
iFileFullPath = aFullPath.AllocL();
|
|
143 |
|
|
144 |
if ( iUtils.IsRemoteDrive( *iFileFullPath ) )
|
|
145 |
{
|
|
146 |
iThreadWrapper = CFileManagerThreadWrapper::NewL();
|
|
147 |
User::LeaveIfError( iThreadWrapper->StartThread(
|
|
148 |
*this,
|
|
149 |
MFileManagerThreadFunction::ENotifyFinished, EPriorityNormal ) );
|
|
150 |
|
|
151 |
// Async file open
|
|
152 |
if ( iObserver )
|
|
153 |
{
|
|
154 |
TRAP_IGNORE( iObserver->ProcessStartedL(
|
|
155 |
MFileManagerProcessObserver::EFileOpenProcess, KErrNone ) );
|
|
156 |
}
|
|
157 |
}
|
|
158 |
else
|
|
159 |
{
|
|
160 |
// Sync file open
|
|
161 |
if ( iObserver )
|
|
162 |
{
|
|
163 |
TRAP_IGNORE( iObserver->ProcessStartedL(
|
|
164 |
MFileManagerProcessObserver::EFileOpenProcess, KErrNone ) );
|
|
165 |
}
|
|
166 |
|
|
167 |
RFile64 sharableFile;
|
|
168 |
TRAPD( err, iDocHandler->OpenTempFileL( *iFileFullPath, sharableFile ) );
|
|
169 |
if ( err == KErrNone )
|
|
170 |
{
|
|
171 |
TRAP( err, OpenShareableFileL( sharableFile, *iFileMime ) );
|
|
172 |
}
|
|
173 |
sharableFile.Close();
|
|
174 |
|
|
175 |
if ( iObserver )
|
|
176 |
{
|
|
177 |
TRAP_IGNORE( iObserver->ProcessFinishedL( err ) );
|
|
178 |
}
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
// -----------------------------------------------------------------------------
|
|
183 |
// CFileManagerDocHandler::OpenShareableFileL()
|
|
184 |
//
|
|
185 |
// -----------------------------------------------------------------------------
|
|
186 |
//
|
|
187 |
void CFileManagerDocHandler::OpenShareableFileL(
|
|
188 |
RFile64& aShareableFile, const TDesC8& aMime )
|
|
189 |
{
|
|
190 |
// Check if iFileFullPath is NULL
|
|
191 |
if ( !iFileFullPath )
|
|
192 |
{
|
|
193 |
User::Leave( KErrNotFound );
|
|
194 |
}
|
|
195 |
|
|
196 |
// Check if file open is denied
|
|
197 |
if ( iObserver )
|
|
198 |
{
|
|
199 |
if ( iObserver->NotifyL(
|
|
200 |
MFileManagerProcessObserver::ENotifyFileOpenDenied,
|
|
201 |
0,
|
|
202 |
*iFileFullPath ) )
|
|
203 |
{
|
|
204 |
return;
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
TDataType dataType( aMime );
|
|
209 |
CAiwGenericParamList& paramList( iDocHandler->InParamListL() );
|
|
210 |
|
|
211 |
// If remote drive, make sure that file can be opened remotely
|
|
212 |
if ( iUtils.IsRemoteDrive( *iFileFullPath ) )
|
|
213 |
{
|
|
214 |
TUid appUid;
|
|
215 |
TDataType fileType;
|
|
216 |
User::LeaveIfError( iApaSession.AppForDocument(
|
|
217 |
aShareableFile, appUid, fileType ) );
|
|
218 |
if ( !iEngine.HasAppRemoteDriveSupport( appUid ) )
|
|
219 |
{
|
|
220 |
User::Leave( KErrFmgrNotSupportedRemotely );
|
|
221 |
}
|
|
222 |
dataType = fileType;
|
|
223 |
}
|
|
224 |
// Otherwise just make sure that file exists before opening it
|
|
225 |
else if ( !BaflUtils::FileExists( iEngine.Fs(), *iFileFullPath ) )
|
|
226 |
{
|
|
227 |
User::Leave( KErrNotFound );
|
|
228 |
}
|
|
229 |
|
|
230 |
TInt err( KErrNone );
|
|
231 |
TInt err2( KErrNone );
|
|
232 |
|
|
233 |
TRAP( err, err2 = iDocHandler->OpenFileEmbeddedL(
|
|
234 |
aShareableFile, dataType, paramList ) );
|
|
235 |
|
|
236 |
LOG_IF_ERROR1( err,
|
|
237 |
"CFileManagerDocHandler::OpenShareableFileL-err=%d", err )
|
|
238 |
LOG_IF_ERROR1( err2,
|
|
239 |
"CFileManagerDocHandler::OpenShareableFileL-err2=%d", err2 )
|
|
240 |
|
|
241 |
// Make sure that not supported functionality is handled properly
|
|
242 |
if ( err2 == KErrNotSupported || err2 == KMimeNotSupported ||
|
|
243 |
err == KErrNotSupported || err == KMimeNotSupported )
|
|
244 |
{
|
|
245 |
User::Leave( KErrNotSupported );
|
|
246 |
}
|
|
247 |
else if ( err == KErrNotFound || err2 == KErrNotFound )
|
|
248 |
{
|
|
249 |
User::Leave( KErrGeneral );
|
|
250 |
}
|
|
251 |
User::LeaveIfError( err );
|
|
252 |
User::LeaveIfError( err2 );
|
|
253 |
|
|
254 |
iEngine.EmbeddedAppStatus( ETrue );
|
|
255 |
iEmbeddedAppOpen = ETrue;
|
|
256 |
}
|
|
257 |
|
|
258 |
// -----------------------------------------------------------------------------
|
|
259 |
// CFileManagerDocHandler::ThreadStepL()
|
|
260 |
//
|
|
261 |
// -----------------------------------------------------------------------------
|
|
262 |
//
|
|
263 |
void CFileManagerDocHandler::ThreadStepL()
|
|
264 |
{
|
|
265 |
// Open shareable file and make sure that it is loaded to file cache
|
|
266 |
// before opening the file with app
|
|
267 |
TInt err( iFile.Open( iFs, *iFileFullPath, EFileShareReadersOnly ) );
|
|
268 |
if ( err == KErrInUse )
|
|
269 |
{
|
|
270 |
User::LeaveIfError( iFile.Open(
|
|
271 |
iFs, *iFileFullPath, EFileShareReadersOrWriters ) );
|
|
272 |
}
|
|
273 |
else
|
|
274 |
{
|
|
275 |
User::LeaveIfError( err );
|
|
276 |
}
|
|
277 |
|
|
278 |
TInt64 pos( 0 );
|
|
279 |
TInt64 size( 0 );
|
|
280 |
TInt readSize( 0 );
|
|
281 |
const TInt KReadBufferSize = 16384; // 16KB
|
|
282 |
HBufC8* buffer = HBufC8::NewLC( KReadBufferSize );
|
|
283 |
TPtr8 ptr( buffer->Des() );
|
|
284 |
|
|
285 |
User::LeaveIfError( iFile.Size( size ) );
|
|
286 |
while ( size > 0 )
|
|
287 |
{
|
|
288 |
if ( iThreadWrapper->IsThreadCanceled() )
|
|
289 |
{
|
|
290 |
User::Leave( KErrCancel );
|
|
291 |
}
|
|
292 |
|
|
293 |
readSize = Min( size, ptr.MaxSize() );
|
|
294 |
User::LeaveIfError( iFile.Read( pos, ptr, readSize ) );
|
|
295 |
size -= readSize;
|
|
296 |
}
|
|
297 |
|
|
298 |
CleanupStack::PopAndDestroy( buffer );
|
|
299 |
iFileReady = ETrue;
|
|
300 |
}
|
|
301 |
|
|
302 |
// -----------------------------------------------------------------------------
|
|
303 |
// CFileManagerDocHandler::IsThreadDone()
|
|
304 |
//
|
|
305 |
// -----------------------------------------------------------------------------
|
|
306 |
//
|
|
307 |
TBool CFileManagerDocHandler::IsThreadDone()
|
|
308 |
{
|
|
309 |
return iFileReady;
|
|
310 |
}
|
|
311 |
|
|
312 |
// -----------------------------------------------------------------------------
|
|
313 |
// CFileManagerDocHandler::NotifyThreadClientL()
|
|
314 |
//
|
|
315 |
// -----------------------------------------------------------------------------
|
|
316 |
//
|
|
317 |
void CFileManagerDocHandler::NotifyThreadClientL(
|
|
318 |
TNotifyType aType, TInt aValue )
|
|
319 |
{
|
|
320 |
switch ( aType )
|
|
321 |
{
|
|
322 |
case ENotifyFinished:
|
|
323 |
{
|
|
324 |
// Open shareable file with app and inform observer
|
|
325 |
if ( aValue == KErrNone && iFileReady )
|
|
326 |
{
|
|
327 |
TRAP( aValue, OpenShareableFileL( iFile, *iFileMime ) );
|
|
328 |
}
|
|
329 |
iFile.Close();
|
|
330 |
|
|
331 |
if ( iObserver )
|
|
332 |
{
|
|
333 |
TRAP_IGNORE( iObserver->ProcessFinishedL( aValue ) );
|
|
334 |
}
|
|
335 |
if ( aValue != KErrNone )
|
|
336 |
{
|
|
337 |
// Ensure that the status gets updated on error
|
|
338 |
HandleServerAppExit( 0 );
|
|
339 |
}
|
|
340 |
break;
|
|
341 |
}
|
|
342 |
default:
|
|
343 |
{
|
|
344 |
break;
|
|
345 |
}
|
|
346 |
}
|
|
347 |
}
|
|
348 |
|
|
349 |
// -----------------------------------------------------------------------------
|
|
350 |
// CFileManagerDocHandler::CancelFileOpen()
|
|
351 |
//
|
|
352 |
// -----------------------------------------------------------------------------
|
|
353 |
//
|
|
354 |
void CFileManagerDocHandler::CancelFileOpen()
|
|
355 |
{
|
|
356 |
if ( iFileFullPath )
|
|
357 |
{
|
|
358 |
iEngine.CancelTransfer( *iFileFullPath );
|
|
359 |
}
|
|
360 |
if ( iThreadWrapper )
|
|
361 |
{
|
|
362 |
iThreadWrapper->CancelThread();
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
// End of File
|