codhandler/codeng/src/FileSaver.cpp
branchRCL_3
changeset 94 919f36ff910f
parent 93 79859ed3eea9
--- a/codhandler/codeng/src/FileSaver.cpp	Tue Aug 31 16:17:46 2010 +0300
+++ b/codhandler/codeng/src/FileSaver.cpp	Wed Sep 01 12:28:30 2010 +0100
@@ -11,16 +11,14 @@
 *
 * Contributors:
 *
-* Description:
-*      Implementation of class CFileSaver.
-*
+* Description: 
+*      Implementation of class CFileSaver.   
+*      
 *
 */
 
-
 // INCLUDE FILES
-//#include <platform/mw/Browser_platform_variant.hrh>
-#include <Browser_platform_variant.hrh>
+#include <browser_platform_variant.hrh>
 #include "FileSaver.h"
 #include "CodBuffStorage.h"
 #include "CodLoadObserver.h"
@@ -33,13 +31,10 @@
 #include "CodData.h"
 
 #include <f32file.h>
-#include <HttpDownloadMgrCommon.h>
+#include <httpdownloadmgrcommon.h>
 #include <DocumentHandler.h>
 #include <pathinfo.h>
 
-// following one line is temporary: AVKON dependency removal
-#undef BRDO_APP_GALLERY_SUPPORTED_FF
-
 #ifdef BRDO_APP_GALLERY_SUPPORTED_FF
 #include <MGXFileManagerFactory.h>
 #include <CMGXFileManager.h>
@@ -47,11 +42,13 @@
 
 #include <DcfEntry.h>
 #include <DcfRep.h>
+#ifdef DOWNLOADMGR_PATH_PLUGIN_ENABLED_FF
+#include <DownloadPathHandler.h>
+#endif
 
 const TInt KDefaultStorageBufferSize = 128 * 1024;
 const TInt KDefaultStorageBufferSizePD = 16 * 1024;
 
-
 // ================= MEMBER FUNCTIONS =======================
 
 // ---------------------------------------------------------
@@ -84,7 +81,7 @@
     CCodSaver::ConstructL();
     iBufferSize =  KDefaultStorageBufferSize ;
     iProgressiveDownload = EFalse ;
-
+    
     iStorage = CCodBuffStorage::NewL(this);
     }
 
@@ -110,7 +107,7 @@
     {
     CLOG(( ECodEng, 2, _L("*** CFileSaver::CFileSaver") ));
     }
-
+  
 // ---------------------------------------------------------
 // CFileSaver::~CFileSaver()
 // ---------------------------------------------------------
@@ -131,33 +128,33 @@
     {
     CLOG(( ECodEng, 2, _L("CFileSaver::OpenStoreL") ));
     __ASSERT_DEBUG( iState == EInit, CodPanic( ECodInternal ) );
-
+    
     // Store (temp file) is not opened now.
     // Opening is deferred until first data chunk arrives.
 
-    TInt err = iFile.Open(  iFs,
-                            iFname,
-                            EFileShareAny |
-                            EFileStream |
+    TInt err = iFile.Open(  iFs, 
+                            iFname, 
+                            EFileShareAny | 
+                            EFileStream | 
 #ifdef BRDO_RFILE_WRITE_DIRECT_IO_FF
                             EFileWrite |
                             EFileWriteDirectIO );
-#else
+#else                            
                             EFileWrite );
-#endif
-
-	if(err == KErrNotFound)
+#endif                            
+                       
+	if(err == KErrNotFound)                               
 	    {
-        err = iFile.Replace( iFs,
-                           iFname,
-                           EFileShareAny |
-                           EFileStream |
+        err = iFile.Replace( iFs, 
+                           iFname, 
+                           EFileShareAny | 
+                           EFileStream | 
 #ifdef BRDO_RFILE_WRITE_DIRECT_IO_FF
                                        EFileWrite |
                                        EFileWriteDirectIO );
-#else
+#else                           
                            EFileWrite );
-#endif
+#endif                           
 
 	    }
 
@@ -169,7 +166,7 @@
     iSize = 0;
     iState = EStoreOpen;
     }
-
+    
 // ---------------------------------------------------------
 // CFileSaver::AppendData()
 // ---------------------------------------------------------
@@ -189,10 +186,10 @@
             {
             // nothing persisted yet. If there's a file with the same name,
             // delete it.
-            err = iFile.Replace( iFs,
-                                   iFname,
-                                   EFileShareAny |
-                                   EFileStream |
+            err = iFile.Replace( iFs, 
+                                   iFname, 
+                                   EFileShareAny | 
+                                   EFileStream | 
                                    EFileWrite );
             }
         else*/
@@ -202,7 +199,7 @@
         {
         err = iStorage->WriteOutNextBodyDataL(aData );
         }
-
+    
     CLOG(( EHttpLoad, 2, _L("<- CFileSaver::AppendData returns (%d)"), err ));
     return err;
     }
@@ -250,7 +247,7 @@
     //
     // There is a safety upper bound on the transaction size, that is already
     // applied. See SetMaxSize().
-
+    
     TDataType drm( KOma1DrmMessageContentType );
     TDataType dcf( KOma1DcfContentType );
 
@@ -287,7 +284,7 @@
     else
         {
         // Other than DRM stuff arrived. Proper check for MIME type.
-        if( !(*aData[aData.ActiveDownload()]).HasType( iType.Des8() ) )
+        if( !(*aData[aData.ActiveDownload()]).HasType( iType.Des8() ) && ! iDocHandler.CanSaveL(iType) )
             {
             CLOG(( ECodEng, 4, _L("  mismatch") ));
             User::Leave( KErrCodAttributeMismatch );
@@ -314,7 +311,7 @@
 */
     if (!aAttached)
 	    {
-#if 0
+#if 0 
         RFs fs;
         TInt err( KErrNone );
         HBufC* filename = HBufC::NewLC(KMaxFileName);
@@ -328,25 +325,25 @@
         TInt error = fs.MkDirAll(filenamePtr);
         if (error!=KErrNone && error!=KErrAlreadyExists)
            {
-            User::Leave(error);
+            User::Leave(error);   
            }
-
+   
         // Find a unique name to avoid any conflict.
         // Here iFname has full path of current location of file
         // and filename has destination path.
         FindUniqueDestinationFileNameL( iFname, filename );
-
-        filenamePtr = filename->Des();
-
+        
+        filenamePtr = filename->Des();     
+               
         err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
-
+    
         if(err != KErrNone)
            {
            User::Leave(err);
            }
-
-        iFname = filenamePtr;
-        NotifyMediaGalleryL( filenamePtr );
+       
+        iFname = filenamePtr;              
+        NotifyMediaGalleryL( filenamePtr );        
         CleanupStack::PopAndDestroy(file);
         CleanupStack::PopAndDestroy(&fs);
         CleanupStack::PopAndDestroy(filename);
@@ -356,40 +353,73 @@
         CleanupClosePushL(fs);
         CFileMan* file=CFileMan::NewL(fs);
         CleanupStack::PushL(file);
+#ifdef DOWNLOADMGR_PATH_PLUGIN_ENABLED_FF
+        CDownloadPathHandler* downloadPathPlugin = NULL;
 
+        if ( aData.Count() )
+            {
+            // Get the mime type of the first media object
+            TPtrC8 mimeType = (*aData[1]).Type();
+            downloadPathPlugin = GetDownloadPathPluginInstanceL( mimeType );
 
+            if( downloadPathPlugin )
+                {
+                CleanupStack::PushL( downloadPathPlugin );
+                }
+            }
+#endif
         for( TInt i = 1; i <= aData.Count() ; ++i )
             {
             HBufC* filename = HBufC::NewLC(KMaxFileName);
             TPtr filenamePtr = filename->Des();
             filenamePtr = (*aData[i]).iRootPath;
-
-            filenamePtr.Append(_L("download\\"));
+            
+#ifdef DOWNLOADMGR_PATH_PLUGIN_ENABLED_FF
+            if( downloadPathPlugin )
+                {
+                TFileName iTempFlName = (*aData[i]).iFullName->Des();
+                GetUpdatedPathFromPluginL( downloadPathPlugin,iTempFlName, filenamePtr );
+                }
+            else
+                {
+                filenamePtr.Append( _L( "download\\" ) );
+                }
+#else
+            filenamePtr.Append( _L( "download\\" ) );
+                
+#endif
+            
             TInt error = fs.MkDirAll(filenamePtr);
             if (error!=KErrNone && error!=KErrAlreadyExists)
                {
-                User::Leave(error);
+                User::Leave(error);   
                }
             iFname = (*aData[i]).iFullName->Des();
-
+            
             // Find a unique name to avoid any conflict.
             // Here iFname has full path of current location of file
             // and filename has destination path.
             FindUniqueDestinationFileNameL( iFname, filename );
-
+            
             filenamePtr = filename->Des();
             TInt err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
             if(err != KErrNone)
             	{
             	User::LeaveIfError(err);
             	}
-            iFname = filenamePtr;
+            iFname = filenamePtr;              
             NotifyMediaGalleryL( filenamePtr );
 
             (*aData[i]).iFileName = iFname;
             (*aData[i]).iFullName = NameL();
             CleanupStack::PopAndDestroy(filename);
             }
+#ifdef DOWNLOADMGR_PATH_PLUGIN_ENABLED_FF
+        if( downloadPathPlugin )
+            {
+            CleanupStack::PopAndDestroy( downloadPathPlugin );
+            }
+#endif
         CleanupStack::PopAndDestroy(file);
         CleanupStack::PopAndDestroy(&fs);
 
@@ -425,13 +455,32 @@
 */
     if (!aAttached)
 	    {
-#ifdef RD_MULTIPLE_DRIVE
+#ifdef RD_MULTIPLE_DRIVE 
         RFs fs;
         TInt err( KErrNone );
         HBufC* filename = HBufC::NewLC(KMaxFileName);
         TPtr filenamePtr = filename->Des();
         filenamePtr = iRootPath;
-        filenamePtr.Append(_L("download\\"));
+#ifdef DOWNLOADMGR_PATH_PLUGIN_ENABLED_FF
+        // Get the mime type of the content
+        TPtrC8 mimeType( iType.Des8() );
+        CDownloadPathHandler* downloadPathPlugin = NULL;
+        downloadPathPlugin = GetDownloadPathPluginInstanceL( mimeType );
+
+        if( downloadPathPlugin )
+            {
+            CleanupStack::PushL( downloadPathPlugin );
+            GetUpdatedPathFromPluginL( downloadPathPlugin,iFname, filenamePtr );
+            CleanupStack::PopAndDestroy( downloadPathPlugin );
+            }
+        else
+            {
+            filenamePtr.Append( _L( "download\\" ) );
+            }
+#else
+            filenamePtr.Append( _L( "download\\" ) );
+            
+#endif
         User::LeaveIfError( fs.Connect() );
         CleanupClosePushL(fs);
         CFileMan* file=CFileMan::NewL(fs);
@@ -439,25 +488,25 @@
         TInt error = fs.MkDirAll(filenamePtr);
         if (error!=KErrNone && error!=KErrAlreadyExists)
            {
-            User::Leave(error);
+            User::Leave(error);   
            }
-
+   
         // Find a unique name to avoid any conflict.
         // Here iFname has full path of current location of file
         // and filename has destination path.
         FindUniqueDestinationFileNameL( iFname, filename );
-
-        filenamePtr = filename->Des();
-
+        
+        filenamePtr = filename->Des();     
+               
         err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
-
+    
         if(err != KErrNone)
            {
            User::Leave(err);
            }
-
-        iFname = filenamePtr;
-        NotifyMediaGalleryL( filenamePtr );
+       
+        iFname = filenamePtr;              
+        NotifyMediaGalleryL( filenamePtr );        
         CleanupStack::PopAndDestroy(file);
         CleanupStack::PopAndDestroy(&fs);
         CleanupStack::PopAndDestroy(filename);
@@ -475,22 +524,22 @@
             TInt error = fs.MkDirAll(filenamePtr);
             if (error!=KErrNone && error!=KErrAlreadyExists)
                {
-                User::Leave(error);
+                User::Leave(error);   
                }
 
             // Find a unique name to avoid any conflict.
             // Here iFname has full path of current location of file
             // and filename has destination path.
             FindUniqueDestinationFileNameL( iFname, filename );
-
+            
             filenamePtr = filename->Des();
             TInt err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
-
+        
             if(err != KErrNone)
                {
                User::Leave(err);
                 }
-            iFname = filenamePtr;
+            iFname = filenamePtr;              
             NotifyMediaGalleryL( filenamePtr );
             CleanupStack::PopAndDestroy(file);
             CleanupStack::PopAndDestroy(&fs);
@@ -560,7 +609,7 @@
     // Expected error: KErrNotFound (if there is no temp file).
     // Unexpected error: all the rest -> nothing we can do with them.
     CloseStore();
-
+    
 	if( aDeleteFile )
 	    {
 #ifdef __TEST_COD_LOG
@@ -584,53 +633,53 @@
     CLOG(( ECodEng, 2, _L("<- CFileSaver::Cleanup") ));
     }
 
-
+  
 // ---------------------------------------------------------
 // CFileSaver::UpdateDCFRepositoryL()
-// Update saved file to DCFRepository
+// Update saved file to DCFRepository  
 // ---------------------------------------------------------
-//
+// 
 void CFileSaver::UpdateDCFRepositoryL( const TDesC& aFileName )
     {
     CDcfEntry* dcfEntry = NULL;
-    dcfEntry = CDcfEntry::NewL();
+    dcfEntry = CDcfEntry::NewL();    
     CleanupStack::PushL( dcfEntry );
-
+    
     CDcfRep* dcfRep = NULL;
     dcfRep = CDcfRep::NewL();
     CleanupStack::PushL( dcfRep );
 
-    dcfEntry->SetLocationL( aFileName, 0 );
+    dcfEntry->SetLocationL( aFileName, 0 );    
     dcfRep->UpdateL( dcfEntry );
     CleanupStack::PopAndDestroy(2); // dcfEntry, dcfRep
-    }
-
+    }   
+    
 // ---------------------------------------------------------
 // CFileSaver::UpdateMediaGalleryIfNeededL()
 // Calls MediaGallery Update method so that media gallery
 // knows update its view.
 // ---------------------------------------------------------
-//
+// 
 
 
 void CFileSaver::UpdateMediaGalleryIfNeededL( const TDesC& aFileName )
     {
-#ifdef BRDO_APP_GALLERY_SUPPORTED_FF
+#ifdef BRDO_APP_GALLERY_SUPPORTED_FF           
     CMGXFileManager* fm = MGXFileManagerFactory::NewFileManagerL( iFs );
     CleanupStack::PushL(fm);
     fm->UpdateL(aFileName);
     CleanupStack::PopAndDestroy(); // fm
-#endif
+#endif    
     }
 // ---------------------------------------------------------
 // CFileSaver::NotifyMediaGalleryL()
 // Notify media gallery about the new file.
 // ---------------------------------------------------------
-//
+// 
 void CFileSaver::NotifyMediaGalleryL( const TDesC& aFileName )
     {
-#ifdef BRDO_APP_GALLERY_SUPPORTED_FF
-
+#ifdef BRDO_APP_GALLERY_SUPPORTED_FF    
+    
     CMGXFileManager* mgFileManager = MGXFileManagerFactory::NewFileManagerL( iFs );
     CleanupStack::PushL( mgFileManager );
 
@@ -641,8 +690,8 @@
     TRAP_IGNORE( UpdateMediaGalleryIfNeededL( aFileName ) );
 
 #endif
-
-
+    
+    
     // Notify DCF repository
     TRAP_IGNORE( UpdateDCFRepositoryL( aFileName ) );
     }
@@ -655,7 +704,7 @@
     // check how many bytes are already persisted
     TInt DownloadedSize(0);
     TInt err = iFile.Size( (TInt&)DownloadedSize );
-
+    
     CLOG(( ECodStorage, 2, _L("CFileSaver::DownloadedFileSize  Downloaded size = %d  error = %d"), \
                DownloadedSize,err ));
 
@@ -663,21 +712,21 @@
         return err;
     return DownloadedSize;
     }
-
-
+    
+    
 /**
 * Discard the old contents
-*/
+*/        
 void CFileSaver::ResetL()
     {
-
-
+    
+    
     iFile.Close();
-
-    User::LeaveIfError( iFile.Replace(  iFs,
-                                        iFname,
-                                        EFileShareAny |
-                                        EFileStream |
+    
+    User::LeaveIfError( iFile.Replace(  iFs, 
+                                        iFname, 
+                                        EFileShareAny | 
+                                        EFileStream | 
                                         EFileWrite ) );
     }
 
@@ -693,8 +742,8 @@
         {
         iBufferSize = KDefaultStorageBufferSizePD;
         }
-
-
+    
+    
     if( iBufferSize != iStorage->CurrentBufferSize())
         {
         FlushL();
@@ -702,10 +751,10 @@
 
     if ( iFile.SubSessionHandle() )
         {
-
+     	
         if( iProgressiveDownload )
             {
-            if( iLength != KDefaultContentLength )
+            if( iLength != KDefaultContentLength )    
                 {
                 iFile.SetSize( iLength );
                 }
@@ -741,11 +790,11 @@
 //
 void CFileSaver::OnComplete()
     {
-    CLOG(( ECodEng, 2, _L("-> CFileSaver::OnComplete") ));
+    CLOG(( ECodEng, 2, _L("-> CFileSaver::OnComplete") ));    
     FlushL();
     iStorage->ClearErrors();
     iFile.Close();
-    CLOG(( ECodEng, 2, _L("<- CFileSaver::OnComplete") ));
+    CLOG(( ECodEng, 2, _L("<- CFileSaver::OnComplete") ));     
     }
 
 
@@ -758,3 +807,37 @@
     {
     return iFname.AllocL();
     }
+#ifdef DOWNLOADMGR_PATH_PLUGIN_ENABLED_FF
+
+// ---------------------------------------------------------
+// CFileSaver::GetDownloadPathPluginInstanceL
+// ---------------------------------------------------------
+CDownloadPathHandler* CFileSaver::GetDownloadPathPluginInstanceL(TPtrC8& mimetype )
+    {
+    CDownloadPathHandler* downloadPathPlugin = NULL;
+    TRAPD( kErr, downloadPathPlugin = CDownloadPathHandler::NewL( mimetype ) );
+    if( kErr != KErrNone )
+        {
+        downloadPathPlugin = NULL;
+        }
+    return downloadPathPlugin;
+    }
+
+// ---------------------------------------------------------
+// CFileSaver::GetUpdatedPathFromPluginL
+// ---------------------------------------------------------
+void CFileSaver::GetUpdatedPathFromPluginL(CDownloadPathHandler* downloadPathPlugin,TFileName& fname, TPtr& fileNamePtr)
+    {
+    TRAPD( kErr, downloadPathPlugin->GetUpdatedPathL( fname, fileNamePtr ) );
+    if( kErr == KErrNone )
+        {
+        fileNamePtr.Append( _L( "\\" ) );
+        }
+    else
+        {
+        fileNamePtr.Append( _L( "download\\" ) );
+        }
+    }
+#endif
+
+//EOF