codhandler/codeng/src/FileSaver.cpp
changeset 0 dd21522fd290
child 8 7c90e6132015
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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: 
       
    15 *      Implementation of class CFileSaver.   
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "FileSaver.h"
       
    24 #include "CodBuffStorage.h"
       
    25 #include "CodLoadObserver.h"
       
    26 #include "RFeatMgr.h"
       
    27 #include "CodUtil.h"
       
    28 #include "CodLogger.h"
       
    29 #include "CodError.h"
       
    30 #include "CodPanic.h"
       
    31 #include "DrmHandler.h"
       
    32 #include "CodData.h"
       
    33 
       
    34 #include <f32file.h>
       
    35 #include <HttpDownloadMgrCommon.h>
       
    36 #include <DocumentHandler.h>
       
    37 #include <pathinfo.h>
       
    38 #include <MGXFileManagerFactory.h>
       
    39 #include <CMGXFileManager.h>
       
    40 #include <DcfEntry.h>
       
    41 #include <DcfRep.h>
       
    42 
       
    43 const TInt KDefaultStorageBufferSize = 128 * 1024;
       
    44 const TInt KDefaultStorageBufferSizePD = 16 * 1024;
       
    45 
       
    46 
       
    47 // ================= MEMBER FUNCTIONS =======================
       
    48 
       
    49 // ---------------------------------------------------------
       
    50 // CFileSaver::NewL()
       
    51 // ---------------------------------------------------------
       
    52 //
       
    53 CFileSaver* CFileSaver::NewL
       
    54         (
       
    55         const TDesC8& aType,
       
    56         RFs& aFs,
       
    57         CDocumentHandler& aDocHandler,
       
    58         const TFileName& aTempPath,
       
    59         const TFileName& aRootPath,
       
    60         const TFileName& aFname
       
    61         )
       
    62     {
       
    63     CFileSaver* saver = new (ELeave) CFileSaver
       
    64         ( aType, aFs, aDocHandler, aTempPath, aRootPath, aFname );
       
    65     CleanupStack::PushL( saver );
       
    66     saver->ConstructL();
       
    67     CleanupStack::Pop( saver );
       
    68     return saver;
       
    69     }
       
    70 // ---------------------------------------------------------
       
    71 // CFileSaver::ConstructL()
       
    72 // ---------------------------------------------------------
       
    73 //
       
    74 void CFileSaver::ConstructL()
       
    75     {
       
    76     CCodSaver::ConstructL();
       
    77     iBufferSize =  KDefaultStorageBufferSize ;
       
    78     iProgressiveDownload = EFalse ;
       
    79     
       
    80     iStorage = CCodBuffStorage::NewL(this);
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------
       
    84 // CFileSaver::CFileSaver()
       
    85 // ---------------------------------------------------------
       
    86 //
       
    87 CFileSaver::CFileSaver
       
    88     (
       
    89     const TDesC8& aType,
       
    90     RFs& aFs,
       
    91     CDocumentHandler& aDocHandler,
       
    92     const TFileName& aTempPath,
       
    93     const TFileName& aRootPath,
       
    94     const TFileName& aFname
       
    95     )
       
    96 : CCodSaver( aType ),
       
    97   iDocHandler( aDocHandler ),
       
    98   iFs( aFs ),
       
    99   iTempPath( aTempPath ),
       
   100   iRootPath( aRootPath ),
       
   101   iFname( aFname)
       
   102     {
       
   103     CLOG(( ECodEng, 2, _L("*** CFileSaver::CFileSaver") ));
       
   104     }
       
   105   
       
   106 // ---------------------------------------------------------
       
   107 // CFileSaver::~CFileSaver()
       
   108 // ---------------------------------------------------------
       
   109 //
       
   110 CFileSaver::~CFileSaver()
       
   111     {
       
   112     CLOG(( ECodEng, 2, _L("-> CFileSaver::~CFileSaver") ));
       
   113     delete iDrmHandler;
       
   114     delete iStorage;
       
   115     CLOG(( ECodEng, 2, _L("<- CFileSaver::~CFileSaver") ));
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------
       
   119 // CFileSaver::OpenStoreL()
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 void CFileSaver::OpenStoreL()
       
   123     {
       
   124     CLOG(( ECodEng, 2, _L("CFileSaver::OpenStoreL") ));
       
   125     __ASSERT_DEBUG( iState == EInit, CodPanic( ECodInternal ) );
       
   126     
       
   127     // Store (temp file) is not opened now.
       
   128     // Opening is deferred until first data chunk arrives.
       
   129 
       
   130     TInt err = iFile.Open(  iFs, 
       
   131                             iFname, 
       
   132                             EFileShareAny | 
       
   133                             EFileStream | 
       
   134 #ifdef BRDO_RFILE_WRITE_DIRECT_IO_FF
       
   135                             EFileWrite |
       
   136                             EFileWriteDirectIO );
       
   137 #else                            
       
   138                             EFileWrite );
       
   139 #endif                            
       
   140                        
       
   141 	if(err == KErrNotFound)                               
       
   142 	    {
       
   143         err = iFile.Replace( iFs, 
       
   144                            iFname, 
       
   145                            EFileShareAny | 
       
   146                            EFileStream | 
       
   147 #ifdef BRDO_RFILE_WRITE_DIRECT_IO_FF
       
   148                                        EFileWrite |
       
   149                                        EFileWriteDirectIO );
       
   150 #else                           
       
   151                            EFileWrite );
       
   152 #endif                           
       
   153 
       
   154 	    }
       
   155 
       
   156     if( !err )
       
   157         {
       
   158         TInt pos( 0 );
       
   159         iFile.Seek( ESeekEnd, pos );
       
   160         }
       
   161     iSize = 0;
       
   162     iState = EStoreOpen;
       
   163     }
       
   164     
       
   165 // ---------------------------------------------------------
       
   166 // CFileSaver::AppendData()
       
   167 // ---------------------------------------------------------
       
   168 //
       
   169 TInt CFileSaver::AppendData( const TDesC8& aData )
       
   170     {
       
   171     CLOG(( ECodEng, 4, \
       
   172         _L("-> CFileSaver::AppendData (%d) bytes"), aData.Length() ));
       
   173 //TODO:    __ASSERT_DEBUG( iState == EStoreOpen, CodPanic( ECodInternal ) );
       
   174     TInt err = CheckMaxSize( aData.Size() );
       
   175     if ( !err && !iFile.SubSessionHandle() )
       
   176         {
       
   177 //TODO:        __ASSERT_DEBUG( !iFname.Length(), CodPanic( ECodInternal ) );
       
   178         (void)iFs.MkDirAll( iTempPath );
       
   179 
       
   180         /*if( !iPausable )
       
   181             {
       
   182             // nothing persisted yet. If there's a file with the same name,
       
   183             // delete it.
       
   184             err = iFile.Replace( iFs, 
       
   185                                    iFname, 
       
   186                                    EFileShareAny | 
       
   187                                    EFileStream | 
       
   188                                    EFileWrite );
       
   189             }
       
   190         else*/
       
   191 
       
   192         }
       
   193     if ( !err )
       
   194         {
       
   195         err = iStorage->WriteOutNextBodyDataL(aData );
       
   196         }
       
   197     
       
   198     CLOG(( EHttpLoad, 2, _L("<- CFileSaver::AppendData returns (%d)"), err ));
       
   199     return err;
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------
       
   203 // CFileSaver::CloseStore()
       
   204 // ---------------------------------------------------------
       
   205 //
       
   206 void CFileSaver::CloseStore()
       
   207     {
       
   208     CLOG(( ECodEng, 2, _L("-> CFileSaver::CloseStore") ));
       
   209     FlushL();
       
   210     iFile.Close();
       
   211     iState = EStoreClosed;
       
   212     CLOG(( ECodEng, 2, _L("<- CFileSaver::CloseStore") ));
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // CFileSaver::CheckResponseAttributesL()
       
   217 // ---------------------------------------------------------
       
   218 //
       
   219 void CFileSaver::CheckResponseAttributesL( const CCodData& aData )
       
   220     {
       
   221     CLOG(( ECodEng, 2, _L("-> CFileSaver::CheckResponseAttributesL") ));
       
   222     __ASSERT_DEBUG( iState == EStoreClosed, CodPanic( ECodInternal ) );
       
   223 #ifdef __TEST_COD_LOG
       
   224     TPtrC8 mime( iType.Des8() );
       
   225     CLOG(( ECodEng, 4, _L8("  MIME==<%S>, size(%d)"), &mime, iSize ));
       
   226 #endif /* def __TEST_COD_LOG */
       
   227 
       
   228     if ( !iSize )
       
   229         {
       
   230         CLOG(( ECodEng, 4, _L("  0 bytes data") ));
       
   231         User::Leave( KErrCodAttributeMismatch );
       
   232         }
       
   233 //TODO:    __ASSERT_DEBUG( iFname.Length(), CodPanic( ECodInternal ) );
       
   234 
       
   235     // Compare content MIME type against descriptor.
       
   236 
       
   237     // Size is not checked, no exact match is required. Quote form spec:
       
   238     // "The storage size and the execution size are dependent on the
       
   239     // environment and may be different from the value of the size attribute.
       
   240     // The transport size may also be different, if compression or some
       
   241     // packaging format is used."
       
   242     //
       
   243     // There is a safety upper bound on the transaction size, that is already
       
   244     // applied. See SetMaxSize().
       
   245     
       
   246     TDataType drm( KOma1DrmMessageContentType );
       
   247     TDataType dcf( KOma1DcfContentType );
       
   248 
       
   249     if ( iType == drm )
       
   250         {
       
   251         // DRM message arrived.
       
   252         // DRM filter should have already turned this into a DCF. Fail now.
       
   253         CLOG(( ECodEng, 4, _L("  DRM unexpected") ));
       
   254         User::Leave( KErrCodAttributeMismatch );
       
   255         }
       
   256     else if ( iType == dcf )
       
   257         {
       
   258         // DCF arrived. MIME type check may be limited:
       
   259         // - Received file (inside DRM) must be DRM-supported (CDrmHandler
       
   260         //   does this).
       
   261         // - COD file must specify COD, DRM, or the received MIME type.
       
   262         __ASSERT_DEBUG( !iDrmHandler, CodPanic( ECodInternal ) );
       
   263         iDrmHandler = CDrmHandler::NewL( iFname );
       
   264         TDataType typeInsideDcf( iDrmHandler->Type() );
       
   265 #ifdef __TEST_COD_LOG
       
   266         mime.Set( typeInsideDcf.Des8() );
       
   267         CLOG(( ECodEng, 4, _L8("  inside DCF: MIME==<%S>"), &mime ));
       
   268 #endif /* def __TEST_COD_LOG */
       
   269         if ( ! ((*aData[aData.ActiveDownload()]).HasType( typeInsideDcf.Des8() ) ||
       
   270                 (*aData[aData.ActiveDownload()]).HasType( KOma1DrmMessageContentType ) ||
       
   271                 (*aData[aData.ActiveDownload()]).HasType( KOma1DcfContentType ) )
       
   272            )
       
   273             {
       
   274             // MIME type mismatch, attribute mismatch.
       
   275             CLOG(( ECodEng, 4,  _L("  type mismatch") ));
       
   276             User::Leave( KErrCodAttributeMismatch );
       
   277             }
       
   278         }
       
   279     else
       
   280         {
       
   281         // Other than DRM stuff arrived. Proper check for MIME type.
       
   282         if( !(*aData[aData.ActiveDownload()]).HasType( iType.Des8() ) )
       
   283             {
       
   284             CLOG(( ECodEng, 4, _L("  mismatch") ));
       
   285             User::Leave( KErrCodAttributeMismatch );
       
   286             }
       
   287         }
       
   288 
       
   289     iState = ERespChecked;
       
   290     CLOG(( ECodEng, 2, _L("<- CFileSaver::CheckResponseAttributesL (match)") ));
       
   291     }
       
   292 
       
   293 
       
   294 // ---------------------------------------------------------
       
   295 // CFileSaver::BulkInstallL()
       
   296 // ---------------------------------------------------------
       
   297 //
       
   298 void CFileSaver::BulkInstallL( TRequestStatus* aStatus, const CCodData &aData, const TBool aAttached )
       
   299     {
       
   300     CLOG(( ECodEng, 2, _L("-> CFileSaver::BulkInstallL") ));
       
   301     __ASSERT_DEBUG( iState == ERespChecked, CodPanic( ECodInternal ) );
       
   302     __ASSERT_DEBUG( aStatus, CodPanic( ECodInternal ) );
       
   303 
       
   304 /* check if the PD app has been started (PD appl is responsible for file moving after play is done)
       
   305    In this case leave and go to the next state.
       
   306 */
       
   307     if (!aAttached)
       
   308 	    {
       
   309 #if 0 
       
   310         RFs fs;
       
   311         TInt err( KErrNone );
       
   312         HBufC* filename = HBufC::NewLC(KMaxFileName);
       
   313         TPtr filenamePtr = filename->Des();
       
   314         filenamePtr = iRootPath;
       
   315         filenamePtr.Append(_L("download\\"));
       
   316         User::LeaveIfError( fs.Connect() );
       
   317         CleanupClosePushL(fs);
       
   318         CFileMan* file=CFileMan::NewL(fs);
       
   319         CleanupStack::PushL(file);
       
   320         TInt error = fs.MkDirAll(filenamePtr);
       
   321         if (error!=KErrNone && error!=KErrAlreadyExists)
       
   322            {
       
   323             User::Leave(error);   
       
   324            }
       
   325    
       
   326         // Find a unique name to avoid any conflict.
       
   327         // Here iFname has full path of current location of file
       
   328         // and filename has destination path.
       
   329         FindUniqueDestinationFileNameL( iFname, filename );
       
   330         
       
   331         filenamePtr = filename->Des();     
       
   332                
       
   333         err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
       
   334     
       
   335         if(err != KErrNone)
       
   336            {
       
   337            User::Leave(err);
       
   338            }
       
   339        
       
   340         iFname = filenamePtr;              
       
   341         NotifyMediaGalleryL( filenamePtr );        
       
   342         CleanupStack::PopAndDestroy(file);
       
   343         CleanupStack::PopAndDestroy(&fs);
       
   344         CleanupStack::PopAndDestroy(filename);
       
   345 #else
       
   346         RFs fs;
       
   347         User::LeaveIfError( fs.Connect() );
       
   348         CleanupClosePushL(fs);
       
   349         CFileMan* file=CFileMan::NewL(fs);
       
   350         CleanupStack::PushL(file);
       
   351         
       
   352         
       
   353         for( TInt i = 1; i <= aData.Count() ; ++i )
       
   354             {
       
   355             HBufC* filename = HBufC::NewLC(KMaxFileName);
       
   356             TPtr filenamePtr = filename->Des();
       
   357             filenamePtr = (*aData[i]).iRootPath;
       
   358 
       
   359             filenamePtr.Append(_L("download\\"));
       
   360             TInt error = fs.MkDirAll(filenamePtr);
       
   361             if (error!=KErrNone && error!=KErrAlreadyExists)
       
   362                {
       
   363                 User::Leave(error);   
       
   364                }
       
   365             iFname = (*aData[i]).iFullName->Des();
       
   366             
       
   367             // Find a unique name to avoid any conflict.
       
   368             // Here iFname has full path of current location of file
       
   369             // and filename has destination path.
       
   370             FindUniqueDestinationFileNameL( iFname, filename );
       
   371             
       
   372             filenamePtr = filename->Des();
       
   373             TInt err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
       
   374             if(err != KErrNone)
       
   375             	{
       
   376             	User::LeaveIfError(err);
       
   377             	}
       
   378             iFname = filenamePtr;              
       
   379             NotifyMediaGalleryL( filenamePtr );
       
   380 
       
   381             (*aData[i]).iFileName = iFname;
       
   382             (*aData[i]).iFullName = NameL();
       
   383             CleanupStack::PopAndDestroy(filename);
       
   384             }
       
   385         CleanupStack::PopAndDestroy(file);
       
   386         CleanupStack::PopAndDestroy(&fs);
       
   387 
       
   388 #endif // RD_MULTIPLE_DRIVE
       
   389     // TODO set iType to that of the packaged object (??)
       
   390     CLOG(( ECodEng, 4, _L("CFileSaver::InstallL: saved==<%S> handler(0x%x)"), \
       
   391         &iFname, iHandler ));
       
   392 
       
   393 	}
       
   394 
       
   395     // Normal file saving is not async.
       
   396     *aStatus = KRequestPending;
       
   397     User::RequestComplete( aStatus, KErrNone );
       
   398 
       
   399     iState = EInstalled;
       
   400     CLOG(( ECodEng, 2, _L("<- CFileSaver::BulkInstallL") ));
       
   401     }
       
   402 
       
   403 
       
   404 // ---------------------------------------------------------
       
   405 // CFileSaver::InstallL()
       
   406 // ---------------------------------------------------------
       
   407 //
       
   408 void CFileSaver::InstallL( TRequestStatus* aStatus, const TDesC& /* aName */, const TBool aAttached )
       
   409     {
       
   410     CLOG(( ECodEng, 2, _L("-> CFileSaver::InstallL") ));
       
   411     __ASSERT_DEBUG( iState == ERespChecked, CodPanic( ECodInternal ) );
       
   412 //    __ASSERT_DEBUG( iFname.Length(), CodPanic( ECodInternal ) );
       
   413     __ASSERT_DEBUG( aStatus, CodPanic( ECodInternal ) );
       
   414 
       
   415 /* check if the PD app has been started (PD appl is responsible for file moving after play is done)
       
   416    In this case leave and go to the next state.
       
   417 */
       
   418     if (!aAttached)
       
   419 	    {
       
   420 #ifdef RD_MULTIPLE_DRIVE 
       
   421         RFs fs;
       
   422         TInt err( KErrNone );
       
   423         HBufC* filename = HBufC::NewLC(KMaxFileName);
       
   424         TPtr filenamePtr = filename->Des();
       
   425         filenamePtr = iRootPath;
       
   426         filenamePtr.Append(_L("download\\"));
       
   427         User::LeaveIfError( fs.Connect() );
       
   428         CleanupClosePushL(fs);
       
   429         CFileMan* file=CFileMan::NewL(fs);
       
   430         CleanupStack::PushL(file);
       
   431         TInt error = fs.MkDirAll(filenamePtr);
       
   432         if (error!=KErrNone && error!=KErrAlreadyExists)
       
   433            {
       
   434             User::Leave(error);   
       
   435            }
       
   436    
       
   437         // Find a unique name to avoid any conflict.
       
   438         // Here iFname has full path of current location of file
       
   439         // and filename has destination path.
       
   440         FindUniqueDestinationFileNameL( iFname, filename );
       
   441         
       
   442         filenamePtr = filename->Des();     
       
   443                
       
   444         err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
       
   445     
       
   446         if(err != KErrNone)
       
   447            {
       
   448            User::Leave(err);
       
   449            }
       
   450        
       
   451         iFname = filenamePtr;              
       
   452         NotifyMediaGalleryL( filenamePtr );        
       
   453         CleanupStack::PopAndDestroy(file);
       
   454         CleanupStack::PopAndDestroy(&fs);
       
   455         CleanupStack::PopAndDestroy(filename);
       
   456 #else
       
   457             RFs fs;
       
   458             HBufC* filename = HBufC::NewLC(KMaxFileName);
       
   459             TPtr filenamePtr = filename->Des();
       
   460             filenamePtr = iRootPath;
       
   461 
       
   462             filenamePtr.Append(_L("download\\"));
       
   463             User::LeaveIfError( fs.Connect() );
       
   464             CleanupClosePushL(fs);
       
   465             CFileMan* file=CFileMan::NewL(fs);
       
   466             CleanupStack::PushL(file);
       
   467             TInt error = fs.MkDirAll(filenamePtr);
       
   468             if (error!=KErrNone && error!=KErrAlreadyExists)
       
   469                {
       
   470                 User::Leave(error);   
       
   471                }
       
   472 
       
   473             // Find a unique name to avoid any conflict.
       
   474             // Here iFname has full path of current location of file
       
   475             // and filename has destination path.
       
   476             FindUniqueDestinationFileNameL( iFname, filename );
       
   477             
       
   478             filenamePtr = filename->Des();
       
   479             TInt err = file->Move(iFname, filenamePtr, CFileMan::EOverWrite);
       
   480         
       
   481             if(err != KErrNone)
       
   482                {
       
   483                User::Leave(err);
       
   484                 }
       
   485             iFname = filenamePtr;              
       
   486             NotifyMediaGalleryL( filenamePtr );
       
   487             CleanupStack::PopAndDestroy(file);
       
   488             CleanupStack::PopAndDestroy(&fs);
       
   489             CleanupStack::PopAndDestroy(filename);
       
   490 #endif // RD_MULTIPLE_DRIVE
       
   491     // TODO set iType to that of the packaged object (??)
       
   492     CLOG(( ECodEng, 4, _L("CFileSaver::InstallL: saved==<%S> handler(0x%x)"), \
       
   493         &iFname, iHandler ));
       
   494 
       
   495 	}
       
   496 
       
   497     // Normal file saving is not async.
       
   498     *aStatus = KRequestPending;
       
   499     User::RequestComplete( aStatus, KErrNone );
       
   500 
       
   501     iState = EInstalled;
       
   502     CLOG(( ECodEng, 2, _L("<- CFileSaver::InstallL") ));
       
   503     }
       
   504 
       
   505 // ---------------------------------------------------------
       
   506 // CFileSaver::CancelInstall()
       
   507 // ---------------------------------------------------------
       
   508 //
       
   509 void CFileSaver::CancelInstall()
       
   510     {
       
   511     CLOG(( ECodEng, 2, _L("CFileSaver::CancelInstall") ));
       
   512     // Do nothing, saving is completed already (not async).
       
   513     }
       
   514 
       
   515 // ---------------------------------------------------------
       
   516 // CFileSaver::ReleaseContent()
       
   517 // ---------------------------------------------------------
       
   518 //
       
   519 void CFileSaver::ReleaseContent( TFileName& aFname, TUid& aHandler )
       
   520     {
       
   521     CLOG(( ECodEng, 2, _L("-> CFileSaver::ReleaseContent") ));
       
   522     //__ASSERT_DEBUG( iState == EInstalled, CodPanic( ECodInternal ) );
       
   523     aFname = iFname;
       
   524     aHandler = iHandler;
       
   525     iFname = KNullDesC;
       
   526     iHandler = KNullUid;
       
   527     iSize = 0;
       
   528     delete iDrmHandler;
       
   529     iDrmHandler = NULL;
       
   530     iState = EInit;  //for PD iState = StartFeatch
       
   531     CLOG(( ECodEng, 2, _L("<- CFileSaver::ReleaseContent") ));
       
   532     }
       
   533 
       
   534 // ---------------------------------------------------------
       
   535 // CFileSaver::ReleaseFileName()
       
   536 // ---------------------------------------------------------
       
   537 //
       
   538 void CFileSaver::ReleaseFileName( TFileName& aFname)
       
   539 {
       
   540     CLOG(( ECodEng, 2, _L("-> CFileSaver::ReleaseFileName") ));
       
   541     aFname = iFname;
       
   542 }
       
   543 
       
   544 // ---------------------------------------------------------
       
   545 // CFileSaver::Cleanup()
       
   546 // ---------------------------------------------------------
       
   547 //
       
   548 void CFileSaver::Cleanup( TBool aDeleteFile )
       
   549     {
       
   550     CLOG(( ECodEng, 2, _L("-> CFileSaver::Cleanup") ));
       
   551     // Clean up the temporary file, ignore errors.
       
   552     // Expected error: KErrNotFound (if there is no temp file).
       
   553     // Unexpected error: all the rest -> nothing we can do with them.
       
   554     CloseStore();
       
   555     
       
   556 	if( aDeleteFile )
       
   557 	    {
       
   558 #ifdef __TEST_COD_LOG
       
   559         TInt err =  // ('Log-only' variable.)
       
   560 #endif /* def __TEST_COD_LOG */
       
   561         iFs.Delete( iFname );
       
   562         CLOG(( ECodEng, 4, \
       
   563         _L("CFileSaver::Cleanup <%S> err(%d)"), &iFname, err ));
       
   564 	    }
       
   565 
       
   566     iFname = KNullDesC; // Now we are done with the file.
       
   567     iHandler = KNullUid;
       
   568     iSize = 0;
       
   569     if ( iDrmHandler )
       
   570         {
       
   571         iDrmHandler->Cleanup();
       
   572         delete iDrmHandler;
       
   573         iDrmHandler = NULL;
       
   574         }
       
   575     iState = EInit;
       
   576     CLOG(( ECodEng, 2, _L("<- CFileSaver::Cleanup") ));
       
   577     }
       
   578 
       
   579   
       
   580 // ---------------------------------------------------------
       
   581 // CFileSaver::UpdateDCFRepositoryL()
       
   582 // Update saved file to DCFRepository  
       
   583 // ---------------------------------------------------------
       
   584 // 
       
   585 void CFileSaver::UpdateDCFRepositoryL( const TDesC& aFileName )
       
   586     {
       
   587     CDcfEntry* dcfEntry = NULL;
       
   588     dcfEntry = CDcfEntry::NewL();    
       
   589     CleanupStack::PushL( dcfEntry );
       
   590     
       
   591     CDcfRep* dcfRep = NULL;
       
   592     dcfRep = CDcfRep::NewL();
       
   593     CleanupStack::PushL( dcfRep );
       
   594 
       
   595     dcfEntry->SetLocationL( aFileName, 0 );    
       
   596     dcfRep->UpdateL( dcfEntry );
       
   597     CleanupStack::PopAndDestroy(2); // dcfEntry, dcfRep
       
   598     }   
       
   599     
       
   600 // ---------------------------------------------------------
       
   601 // CFileSaver::UpdateMediaGalleryIfNeededL()
       
   602 // Calls MediaGallery Update method so that media gallery
       
   603 // knows update its view.
       
   604 // ---------------------------------------------------------
       
   605 // 
       
   606 void CFileSaver::UpdateMediaGalleryIfNeededL( const TDesC& aFileName )
       
   607     {       
       
   608     CMGXFileManager* fm = MGXFileManagerFactory::NewFileManagerL( iFs );
       
   609     CleanupStack::PushL(fm);
       
   610     fm->UpdateL(aFileName);
       
   611     CleanupStack::PopAndDestroy(); // fm
       
   612     }
       
   613 // ---------------------------------------------------------
       
   614 // CFileSaver::NotifyMediaGalleryL()
       
   615 // Notify media gallery about the new file.
       
   616 // ---------------------------------------------------------
       
   617 // 
       
   618 void CFileSaver::NotifyMediaGalleryL( const TDesC& aFileName )
       
   619     {
       
   620     CMGXFileManager* mgFileManager = MGXFileManagerFactory::NewFileManagerL( iFs );
       
   621     CleanupStack::PushL( mgFileManager );
       
   622 
       
   623     TRAP_IGNORE( mgFileManager->UpdateL() );
       
   624     CleanupStack::PopAndDestroy( mgFileManager );
       
   625 
       
   626     // Notify Media Gallery
       
   627     TRAP_IGNORE( UpdateMediaGalleryIfNeededL( aFileName ) );
       
   628     
       
   629     // Notify DCF repository
       
   630     TRAP_IGNORE( UpdateDCFRepositoryL( aFileName ) );
       
   631     }
       
   632 // ---------------------------------------------------------
       
   633 // CFileSaver::DownloadedFileSize()
       
   634 // ---------------------------------------------------------
       
   635 //
       
   636 TInt CFileSaver::DownloadedFileSize()
       
   637     {
       
   638     // check how many bytes are already persisted
       
   639     TInt DownloadedSize(0);
       
   640     TInt err = iFile.Size( (TInt&)DownloadedSize );
       
   641     
       
   642     CLOG(( ECodStorage, 2, _L("CFileSaver::DownloadedFileSize  Downloaded size = %d  error = %d"), \
       
   643                DownloadedSize,err ));
       
   644 
       
   645     if(err != KErrNone)
       
   646         return err;
       
   647     return DownloadedSize;
       
   648     }
       
   649     
       
   650     
       
   651 /**
       
   652 * Discard the old contents
       
   653 */        
       
   654 void CFileSaver::ResetL()
       
   655     {
       
   656     
       
   657     
       
   658     iFile.Close();
       
   659     
       
   660     User::LeaveIfError( iFile.Replace(  iFs, 
       
   661                                         iFname, 
       
   662                                         EFileShareAny | 
       
   663                                         EFileStream | 
       
   664                                         EFileWrite ) );
       
   665     }
       
   666 
       
   667 
       
   668 // -----------------------------------------------------------------------------
       
   669 // CHttpStorage::SetProgressiveMode
       
   670 // -----------------------------------------------------------------------------
       
   671 //
       
   672 void CFileSaver::SetProgressiveMode( TBool aValue )
       
   673     {
       
   674     iProgressiveDownload = aValue;
       
   675     if( iProgressiveDownload )
       
   676         {
       
   677         iBufferSize = KDefaultStorageBufferSizePD;
       
   678         }
       
   679     
       
   680     
       
   681     if( iBufferSize != iStorage->CurrentBufferSize())
       
   682         {
       
   683         FlushL();
       
   684         }
       
   685 
       
   686     if ( iFile.SubSessionHandle() )
       
   687         {
       
   688      	
       
   689         if( iProgressiveDownload )
       
   690             {
       
   691             if( iLength != KDefaultContentLength )    
       
   692                 {
       
   693                 iFile.SetSize( iLength );
       
   694                 }
       
   695             }
       
   696         else
       
   697             {
       
   698             iFile.SetSize( iSize );
       
   699             }
       
   700         }
       
   701     }
       
   702 
       
   703 
       
   704 // -----------------------------------------------------------------------------
       
   705 // CHttpStorage::Flush
       
   706 // Flush internal buffer to disk
       
   707 // (other items were commented in a header).
       
   708 // -----------------------------------------------------------------------------
       
   709 //
       
   710 void CFileSaver::FlushL()
       
   711     {
       
   712     CLOG(( ECodEng, 2, _L("-> CFileSaver::FlushL") ));
       
   713     iStorage->FlushBuffersL();
       
   714     iStorage->ResetBuffers();
       
   715     CLOG(( ECodEng, 2, _L("<- CFileSaver::FlushL") ));
       
   716     }
       
   717 
       
   718 
       
   719 
       
   720 // -----------------------------------------------------------------------------
       
   721 // CFileSaver::OnCompleted
       
   722 // Called if download completed.
       
   723 // -----------------------------------------------------------------------------
       
   724 //
       
   725 void CFileSaver::OnComplete()
       
   726     {
       
   727     CLOG(( ECodEng, 2, _L("-> CFileSaver::OnComplete") ));    
       
   728     FlushL();
       
   729     iStorage->ClearErrors();
       
   730     iFile.Close();
       
   731     CLOG(( ECodEng, 2, _L("<- CFileSaver::OnComplete") ));     
       
   732     }
       
   733 
       
   734 
       
   735 
       
   736 
       
   737 // ---------------------------------------------------------
       
   738 // CFileSaver::Name()
       
   739 // ---------------------------------------------------------
       
   740 HBufC* CFileSaver::NameL() const
       
   741     {
       
   742     return iFname.AllocL();
       
   743     }