filemanager/Engine/src/CFileManagerDocHandler.cpp
changeset 0 6a9f87576119
child 1 d1daf54a55b5
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     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 file open is denied
       
   191     if ( iObserver && iFileFullPath )
       
   192         {
       
   193         if ( iObserver->NotifyL(
       
   194                 MFileManagerProcessObserver::ENotifyFileOpenDenied,
       
   195                 0,
       
   196                 *iFileFullPath ) )
       
   197             {
       
   198             return;
       
   199             }
       
   200         }
       
   201 
       
   202     TDataType dataType( aMime );
       
   203     CAiwGenericParamList& paramList( iDocHandler->InParamListL() );
       
   204 
       
   205     // If remote drive, make sure that file can be opened remotely
       
   206     if ( iUtils.IsRemoteDrive( *iFileFullPath ) )
       
   207         {
       
   208         TUid appUid;
       
   209         TDataType fileType;
       
   210         User::LeaveIfError( iApaSession.AppForDocument(
       
   211             aShareableFile, appUid, fileType ) );
       
   212         if ( !iEngine.HasAppRemoteDriveSupport( appUid ) )
       
   213             {
       
   214             User::Leave( KErrFmgrNotSupportedRemotely );
       
   215             }
       
   216         dataType = fileType;
       
   217         }
       
   218     // Otherwise just make sure that file exists before opening it
       
   219     else if ( !BaflUtils::FileExists( iEngine.Fs(), *iFileFullPath ) )
       
   220         {
       
   221         User::Leave( KErrNotFound );
       
   222         }
       
   223 
       
   224     TInt err( KErrNone );
       
   225     TInt err2( KErrNone );
       
   226 
       
   227     TRAP( err, err2 = iDocHandler->OpenFileEmbeddedL(
       
   228         aShareableFile, dataType, paramList ) );
       
   229 
       
   230     LOG_IF_ERROR1( err,
       
   231         "CFileManagerDocHandler::OpenShareableFileL-err=%d", err )
       
   232     LOG_IF_ERROR1( err2,
       
   233         "CFileManagerDocHandler::OpenShareableFileL-err2=%d", err2 )
       
   234 
       
   235     // Make sure that not supported functionality is handled properly
       
   236     if ( err2 == KErrNotSupported || err2 == KMimeNotSupported ||
       
   237          err == KErrNotSupported || err == KMimeNotSupported )
       
   238         {
       
   239         User::Leave( KErrNotSupported );
       
   240         }
       
   241     else if ( err == KErrNotFound || err2 == KErrNotFound )
       
   242         {
       
   243         User::Leave( KErrGeneral );
       
   244         }
       
   245     User::LeaveIfError( err );
       
   246     User::LeaveIfError( err2 );
       
   247 
       
   248     iEngine.EmbeddedAppStatus( ETrue );
       
   249     iEmbeddedAppOpen = ETrue;
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CFileManagerDocHandler::ThreadStepL()
       
   254 //
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 void CFileManagerDocHandler::ThreadStepL()
       
   258     {
       
   259     // Open shareable file and make sure that it is loaded to file cache
       
   260     // before opening the file with app
       
   261     TInt err( iFile.Open( iFs, *iFileFullPath, EFileShareReadersOnly ) );
       
   262     if ( err == KErrInUse )
       
   263         {
       
   264         User::LeaveIfError( iFile.Open(
       
   265             iFs, *iFileFullPath, EFileShareReadersOrWriters ) );
       
   266         }
       
   267     else
       
   268         {
       
   269         User::LeaveIfError( err );
       
   270         }
       
   271 
       
   272     TInt64 pos( 0 );
       
   273     TInt64 size( 0 );
       
   274     TInt readSize( 0 );
       
   275     const TInt KReadBufferSize = 16384; // 16KB
       
   276     HBufC8* buffer = HBufC8::NewLC( KReadBufferSize );
       
   277     TPtr8 ptr( buffer->Des() );
       
   278 
       
   279     User::LeaveIfError( iFile.Size( size ) );
       
   280     while ( size > 0 )
       
   281         {
       
   282         if ( iThreadWrapper->IsThreadCanceled() )
       
   283             {
       
   284             User::Leave( KErrCancel );
       
   285             }
       
   286 
       
   287         readSize = Min( size, ptr.MaxSize() );
       
   288         User::LeaveIfError( iFile.Read( pos, ptr, readSize ) );
       
   289         size -= readSize;
       
   290         }
       
   291 
       
   292     CleanupStack::PopAndDestroy( buffer );
       
   293     iFileReady = ETrue;
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CFileManagerDocHandler::IsThreadDone()
       
   298 //
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 TBool CFileManagerDocHandler::IsThreadDone()
       
   302     {
       
   303     return iFileReady;
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CFileManagerDocHandler::NotifyThreadClientL()
       
   308 //
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CFileManagerDocHandler::NotifyThreadClientL(
       
   312         TNotifyType aType, TInt aValue )
       
   313     {
       
   314     switch ( aType )
       
   315         {
       
   316         case ENotifyFinished:
       
   317             {
       
   318             // Open shareable file with app and inform observer
       
   319             if ( aValue == KErrNone && iFileReady )
       
   320                 {
       
   321                 TRAP( aValue, OpenShareableFileL( iFile, *iFileMime ) );
       
   322                 }
       
   323             iFile.Close();
       
   324 
       
   325             if ( iObserver )
       
   326                 {
       
   327                 TRAP_IGNORE( iObserver->ProcessFinishedL( aValue ) );
       
   328                 }
       
   329             if ( aValue != KErrNone )
       
   330                 {
       
   331                 // Ensure that the status gets updated on error
       
   332                 HandleServerAppExit( 0 );
       
   333                 }
       
   334             break;
       
   335             }
       
   336         default:
       
   337             {
       
   338             break;
       
   339             }
       
   340         }
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CFileManagerDocHandler::CancelFileOpen()
       
   345 //
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 void CFileManagerDocHandler::CancelFileOpen()
       
   349     {
       
   350     if ( iFileFullPath )
       
   351         {
       
   352         iEngine.CancelTransfer( *iFileFullPath );
       
   353         }
       
   354     if ( iThreadWrapper )
       
   355         {
       
   356         iThreadWrapper->CancelThread();
       
   357         }
       
   358     }
       
   359 
       
   360 // End of File