codhandler/codeng/src/CodSaver.cpp
changeset 0 dd21522fd290
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 CCodSaver.   
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "CodSaver.h"
       
    24 #include "CodPanic.h"
       
    25 #include "CodLogger.h"
       
    26 #include "CodError.h"
       
    27 #include "FileExt.h"
       
    28 #include "bautils.h"
       
    29 
       
    30 
       
    31 // ================= CONSTANTS =======================
       
    32 
       
    33 /// Response size upper bound multiplier. Used in SetMaxSize().
       
    34 LOCAL_D const TReal KCodOversizeFactor = 1.4;
       
    35 /// Response upper bound of response size (10k). Used in SetMaxSize().
       
    36 LOCAL_D const TInt KCodMinUpperBound = 10240;
       
    37 
       
    38 const TInt KMaxIndexStringLength = 16;   // max size of index string "(4294967296)"
       
    39 _LIT( KIndexString, "(%d)" );
       
    40 const TInt KDownloadNameMaxSize = 120;
       
    41 #define MIN( a, b ) ( (a) < (b) ? (a) : (b) )
       
    42 
       
    43 
       
    44 // ================= MEMBER FUNCTIONS =======================
       
    45 
       
    46 // ---------------------------------------------------------
       
    47 // CCodSaver::ConstructL()
       
    48 // ---------------------------------------------------------
       
    49 //
       
    50 void CCodSaver::ConstructL()
       
    51     {
       
    52     iSourceUri = HBufC8::NewL( 0 );
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------
       
    56 // CCodSaver::~CCodSaver()
       
    57 // ---------------------------------------------------------
       
    58 //
       
    59 CCodSaver::~CCodSaver()
       
    60     {
       
    61     delete iSourceUri;
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // CCodSaver::SetObserver()
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 void CCodSaver::SetObserver( MCodLoadObserver* aObserver )
       
    69     {
       
    70     CLOG(( ECodEng, 2, _L("CCodSaver::SetObserver(0x%x)"), aObserver ));
       
    71 //TODO:    __ASSERT_ALWAYS( !(aObserver && iObserver), \
       
    72 //        CodPanic( ECodObserverAlreadySet ) );
       
    73     iObserver = aObserver;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 // CCodSaver::CheckMaxSize()
       
    78 // ---------------------------------------------------------
       
    79 //
       
    80 TInt CCodSaver::CheckMaxSize( TInt aDataChunkSize )
       
    81     {
       
    82     __ASSERT_DEBUG( aDataChunkSize >= 0, CodPanic( ECodInternal ) );
       
    83     TInt err( KErrNone );
       
    84     if ( iMaxSize && iSize + aDataChunkSize > iMaxSize )
       
    85         {
       
    86         err = KErrCodAttributeMismatch;
       
    87         CLOG(( ECodEng, 2, _L("CCodSaver::CheckMaxSize %d+%d>%d, fail"), \
       
    88             iSize, aDataChunkSize, iMaxSize ));
       
    89         }
       
    90     return err;
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------
       
    94 // CCodSaver::SetMaxSize()
       
    95 // ---------------------------------------------------------
       
    96 //
       
    97 void CCodSaver::SetMaxSize( TInt aSize )
       
    98     {
       
    99     __ASSERT_DEBUG( aSize >= 0, CodPanic( ECodInternal ) );
       
   100     // Max size is set with allowance, to compensate transport encodings,
       
   101     // packaging, and packaging headers.
       
   102     iMaxSize = Max( (TInt)(KCodOversizeFactor * aSize), KCodMinUpperBound );
       
   103     }
       
   104 
       
   105 
       
   106 void CCodSaver::AppendStorageInfoL(TPtr8& aBuf)const
       
   107     {
       
   108     AppendBufL(aBuf,iSourceUri);
       
   109     APPEND_BUF_INT(aBuf,iFname);
       
   110     APPEND_BUF_INT(aBuf,iSize);
       
   111     APPEND_BUF_INT(aBuf,iState);
       
   112     APPEND_BUF_INT(aBuf,iHandler);
       
   113     }
       
   114 
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CCodSaver::ConvertDownloadNameUniqueL
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CCodSaver::ConvertDownloadNameUniqueL( HBufC*& filePath,
       
   121                                             HBufC*& fileName,
       
   122                                             HBufC*& fileExtn)
       
   123     {
       
   124     TBool bFound( EFalse );
       
   125     TInt index( 0 );
       
   126     HBufC* uniqueName = NULL;
       
   127     HBufC* fullNameTemp = HBufC::NewLC(KMaxFileName);
       
   128     TPtr fullNameTempPtr( fullNameTemp->Des() );
       
   129 
       
   130     RFs rFs;
       
   131   	User::LeaveIfError( rFs.Connect() );
       
   132   	CleanupClosePushL(rFs);
       
   133 
       
   134     do
       
   135         {
       
   136         bFound = EFalse;
       
   137         //Generate Unique name.
       
   138         CreateIndexedNameL( uniqueName, *fileName, index );
       
   139 
       
   140         //Name in \\system\\temp.
       
   141         fullNameTempPtr.Copy( *filePath );
       
   142         fullNameTempPtr.Append( *uniqueName );
       
   143         fullNameTempPtr.Append( *fileExtn );
       
   144 
       
   145         if( BaflUtils::FileExists( rFs , fullNameTempPtr ) )
       
   146             //Check if file name exist in Destination path.
       
   147             {
       
   148             bFound =ETrue;
       
   149             }
       
   150 
       
   151         }while( bFound );
       
   152 
       
   153     CleanupStack::PopAndDestroy(&rFs);
       
   154     CleanupStack::PopAndDestroy(fullNameTemp);
       
   155     
       
   156     // This is the unique name that we were looking for.
       
   157     CleanupStack::PopAndDestroy(fileName);
       
   158     fileName = uniqueName;
       
   159     CleanupStack::PushL(fileName);
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CCodSaver::CreateIndexedNameL
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CCodSaver::CreateIndexedNameL( HBufC*& aUniqueName, 
       
   167                                         TDesC& aOrgName, 
       
   168                                         TInt& aIndex )
       
   169     {
       
   170     TPtrC left;
       
   171     TBuf<KMaxIndexStringLength> indexStr;
       
   172 
       
   173     delete aUniqueName; aUniqueName = NULL;
       
   174 
       
   175     if( aIndex )
       
   176         {
       
   177         indexStr.Format( KIndexString, aIndex );
       
   178         }
       
   179 
       
   180     TInt fullLength = aOrgName.Length() + indexStr.Length();
       
   181     if( fullLength > KMaxPath )
       
   182         // name is longer than KMaxPath.
       
   183         {
       
   184         User::Leave( KErrGeneral );
       
   185         }
       
   186 
       
   187     left.Set( aOrgName.Left( KDownloadNameMaxSize - indexStr.Length() ));
       
   188 
       
   189     aUniqueName = HBufC::NewL( fullLength );
       
   190     aUniqueName->Des().Format( _L("%S%S"), &left, 
       
   191                                            &indexStr );
       
   192 
       
   193     ++aIndex;
       
   194     }
       
   195     
       
   196 // -----------------------------------------------------------------------------
       
   197 // CCodSaver::FindUniqueDestinationFileNameL
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CCodSaver::FindUniqueDestinationFileNameL( TDesC& srcFile, HBufC*& destPath )
       
   201     {
       
   202         HBufC* fileExtention = HBufC::NewLC(KMaxFileName);
       
   203         HBufC* fileName = HBufC::NewLC(KMaxFileName);
       
   204         TPtr fileNamePtr( fileName->Des() );
       
   205 
       
   206         fileNamePtr = srcFile;
       
   207         // Retrieve the file extention.
       
   208         TInt dotInd = srcFile.LocateReverse( '.' );
       
   209         if( dotInd != KErrNotFound )
       
   210             // Filename extension found.
       
   211             {
       
   212             fileExtention->Des().Copy( srcFile.Right( srcFile.Length() - dotInd ) );
       
   213             fileNamePtr.Copy( srcFile.Left( dotInd ) );
       
   214             }
       
   215 
       
   216         // Retrieve the file name (excluding file extention).
       
   217         TInt lastSlashPos = fileNamePtr.LocateReverse( '\\' );
       
   218         if( lastSlashPos != KErrNotFound )
       
   219             // Filename found.
       
   220             {
       
   221             fileNamePtr.Copy( fileNamePtr.Right( fileNamePtr.Length() - lastSlashPos - 1 ) );
       
   222             }
       
   223 
       
   224         // Find a unique file name.
       
   225         ConvertDownloadNameUniqueL( destPath, fileName, fileExtention );
       
   226 
       
   227         // Found. Append file name and extention to destination path.
       
   228         destPath->Des().Append( *fileName );
       
   229         destPath->Des().Append( *fileExtention );
       
   230 
       
   231         CleanupStack::PopAndDestroy( fileName );
       
   232         CleanupStack::PopAndDestroy( fileExtention );
       
   233     }