brandingserver/BSServer/cbsibywriter.cpp
changeset 31 9dbc70490d9a
equal deleted inserted replaced
30:1fa9b890f29c 31:9dbc70490d9a
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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:   Writes IBY files
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <f32file.h>
       
    21 #include <bautils.h>
       
    22 #include <utf.h>
       
    23 
       
    24 #include "cbsibywriter.h"
       
    25 #include "bsimportconstants.h"
       
    26 //#include "importlogwriter.h"
       
    27 #include "DebugTrace.h"
       
    28 
       
    29 // CONSTANTS
       
    30 _LIT8( KIBYHeader1,           "\n#ifndef __BRAND_" );
       
    31 _LIT8( KIBYHeader2,           "\n#define __BRAND_" );
       
    32 _LIT8( KIBYHeaderEnd,         "_IBY_" );
       
    33 _LIT8( KIBYFileItem,          "\nfile=" );
       
    34 _LIT8( KIBYEmptyLine,         "\n" );
       
    35 _LIT8( KIBYSpace,             " \t\t " );
       
    36 _LIT8( KIBYFooter,            "\n#endif //__BRAND_" );
       
    37 
       
    38 // File & dir
       
    39 _LIT8( KIBYBaseSource,        "\\epoc32\\winscw\\c" );
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CBSIBYWriter::NewL
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CBSIBYWriter* CBSIBYWriter::NewL()
       
    48     {
       
    49     CBSIBYWriter* self = NewLC();
       
    50     CleanupStack::Pop();
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CBSIBYWriter::NewLC
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CBSIBYWriter* CBSIBYWriter::NewLC()
       
    59     {
       
    60     CBSIBYWriter* self = new (ELeave) CBSIBYWriter();
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL();
       
    63     return self;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CBSIBYWriter::ConstructL
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 void CBSIBYWriter::ConstructL()
       
    71     {
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CBSIBYWriter::CBSIBYWriter
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CBSIBYWriter::CBSIBYWriter()
       
    79     {
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CBSIBYWriter::~CBSIBYWriter
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 CBSIBYWriter::~CBSIBYWriter()
       
    87     {
       
    88     iSourcePath.ResetAndDestroy();
       
    89     iSourceFiles.ResetAndDestroy();
       
    90     iDestinations.ResetAndDestroy();
       
    91     delete iIbyFile;
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // CBSIBYWriter::SetFileItemL
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CBSIBYWriter::SetFileItemL( const TDesC& aSrc, const TDesC& aDest )
       
    99     {
       
   100     TRACE( T_LIT( "CBSIBYWriter::SetFileItemL begin") );
       
   101     // Parse and construct filenames
       
   102     TParse srcparse;
       
   103     srcparse.Set( aSrc, NULL, NULL );
       
   104 
       
   105     TParse dstparse;
       
   106     dstparse.Set( aDest, NULL, NULL );
       
   107 
       
   108     // Path
       
   109     HBufC8* srcpath = HBufC8::NewLC( srcparse.Path().Length() );
       
   110     TPtr8 ptr( srcpath->Des() );
       
   111     CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr, srcparse.Path() );
       
   112 
       
   113     HBufC8* src = HBufC8::NewLC( srcparse.NameAndExt().Length() );
       
   114     ptr.Set( src->Des() );
       
   115     CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr, srcparse.NameAndExt() );
       
   116 
       
   117     HBufC8* dst = HBufC8::NewLC( aDest.Length() );
       
   118     ptr.Set( dst->Des() );
       
   119     
       
   120     HBufC* newDst = HBufC::NewLC( aDest.Length() );
       
   121     TPtr dstPtr( newDst->Des() );
       
   122     TPtrC name = dstparse.NameAndExt();
       
   123 	TPtrC path = dstparse.Path();
       
   124 	dstPtr.Append( KBSZDrive );
       
   125 	dstPtr.Append( path );
       
   126 	dstPtr.Append( name );
       
   127 	
       
   128     CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr, dstPtr );
       
   129     
       
   130     TRACE( T_LIT( "CBSIBYWriter::SetFileItemL - setting source file %S"), &aSrc );
       
   131     TRACE( T_LIT( "CBSIBYWriter::SetFileItemL - setting destination file %S"), newDst );
       
   132 	CleanupStack::PopAndDestroy( newDst );
       
   133     CleanupStack::Pop( 3 );
       
   134 
       
   135     // Append filepair
       
   136     iSourcePath.Append( srcpath );
       
   137     iSourceFiles.Append( src );
       
   138     iDestinations.Append( dst );
       
   139     
       
   140     TRACE( T_LIT( "CBSIBYWriter::SetFileItemL end") );
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CBSIBYWriter::WriteIBYFile()
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CBSIBYWriter::WriteIBYFileL( RFs& aFs, const TDesC& aFileName )
       
   148     {
       
   149 	TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL begin") );
       
   150 	
       
   151     TInt count = iSourceFiles.Count();
       
   152     if( count == 0 )
       
   153     	{
       
   154     	TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL - No file elements, nothing to do.") );
       
   155     	return;
       
   156     	}
       
   157     if( count != iDestinations.Count() )
       
   158         {
       
   159 		TRACE( T_LIT("Internal error: IBY filename count mismatch ( %d != %d )"),
       
   160             count, iDestinations.Count() );
       
   161         User::Leave( KErrCorrupt );
       
   162         }
       
   163 
       
   164 	RFile outfile;
       
   165 	TInt err = outfile.Open( aFs, aFileName, EFileWrite );
       
   166 	if( err == KErrNotFound )
       
   167 		{
       
   168 		TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL - IBY file not initialized!") );
       
   169 		// if the file is not initialized -> not ready
       
   170 		err = KErrNotReady;
       
   171 		}
       
   172 	User::LeaveIfError( err );
       
   173 
       
   174     // write data
       
   175     TInt size = -1;
       
   176     User::LeaveIfError( outfile.Size( size ) );
       
   177 
       
   178     outfile.Write( size, KIBYEmptyLine );
       
   179     
       
   180 	TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL - start writing files to IBY") );
       
   181     
       
   182     for( TInt i = 0; i < count; i++ )
       
   183         {
       
   184         outfile.Write( KIBYFileItem );
       
   185         outfile.Write( KIBYBaseSource );
       
   186         outfile.Write( iSourcePath[i]->Des() );
       
   187         outfile.Write( iSourceFiles[i]->Des() );
       
   188         outfile.Write( KIBYSpace );
       
   189         outfile.Write( iDestinations[i]->Des() );
       
   190         TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL - %S"), iDestinations[i] );
       
   191         }
       
   192 
       
   193 	TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL - writing IBY file footer") );
       
   194     outfile.Write( KIBYEmptyLine );
       
   195     outfile.Write( KIBYFooter );
       
   196     outfile.Write( KIBYHeaderEnd );
       
   197 
       
   198     // cleanup
       
   199     outfile.Close();
       
   200     TRACE( T_LIT( "CBSIBYWriter::WriteIBYFileL end") );
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CBSIBYWriter::WriteIBYFile()
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 void CBSIBYWriter::InitIbyFileL( RFs& aFs, const TDesC& aFileName )
       
   208 	{
       
   209 	TRACE( T_LIT( "CBSIBYWriter::InitIbyFileL begin") );
       
   210 	HBufC* temp = aFileName.AllocL();
       
   211 	delete iIbyFile;
       
   212 	iIbyFile = temp;
       
   213 
       
   214     TInt err = aFs.MkDir( KBSIbyDirectory );
       
   215     if( err == KErrAlreadyExists )
       
   216     	{
       
   217     	err = KErrNone;
       
   218     	}
       
   219     User::LeaveIfError( err );
       
   220 	
       
   221 	TRACE( T_LIT( "CBSIBYWriter::InitIbyFileL IBY directory (%S) created"), &KBSIbyDirectory() );
       
   222 	
       
   223 	// Open file for writing
       
   224 	RFile outfile;
       
   225     User::LeaveIfError( outfile.Replace( aFs, aFileName, EFileWrite ) );
       
   226     
       
   227     TRACE( T_LIT( "CBSIBYWriter::InitIbyFileL IBY file (%S) created"), &aFileName );
       
   228     
       
   229     outfile.Write( KIBYHeader1 );
       
   230     outfile.Write( KIBYHeaderEnd );
       
   231     outfile.Write( KIBYHeader2 );
       
   232     outfile.Write( KIBYHeaderEnd );
       
   233     outfile.Close();
       
   234     TRACE( T_LIT( "CBSIBYWriter::InitIbyFileL end") );
       
   235 	}
       
   236 // End of file