appinstaller/AppinstUi/Plugin/SisxUI/Src/SisxUIStartupFile.cpp
changeset 80 9dcba1ee99f7
parent 77 d1838696558c
equal deleted inserted replaced
77:d1838696558c 80:9dcba1ee99f7
     1 /*
       
     2 * Copyright (c) 2002-2004 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:   This file contains the implementation of CSisxUIStartupFile
       
    15 *                class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <s32file.h>
       
    22 #include <barsc.h>
       
    23 #include <barsread.h>
       
    24 #include <data_caging_path_literals.hrh>
       
    25 #include <pathinfo.h>
       
    26 #include <SWInstStartupTaskParam.h>
       
    27 
       
    28 #include "SisxUIStartupFile.h"
       
    29 
       
    30 using namespace SwiUI;
       
    31 
       
    32 _LIT( KNewFileFormat, "%S.rsc" );
       
    33 _LIT( KNewFileMatch, "[*].rsc" );
       
    34 _LIT( KPrivateFileDir, "startup\\" );
       
    35 _LIT( KPrivateFileFormat, "%S.dat" );
       
    36 _LIT( KImportDir, "Import\\" );
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CSisxUIStartupFile::CSisxUIStartupFile
       
    42 // C++ default constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CSisxUIStartupFile::CSisxUIStartupFile()
       
    47     {
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CSisxUIStartupFile::ConstructL
       
    52 // Symbian 2nd phase constructor can leave.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 void CSisxUIStartupFile::ConstructL( const TUid& aUid )
       
    56     {
       
    57     User::LeaveIfError( iFileSession.Connect() );
       
    58 
       
    59     TUidName uidStr = aUid.Name();
       
    60     TFileName tmp;
       
    61     
       
    62     iFileSession.PrivatePath( tmp );    
       
    63     tmp.Insert( 0, TParsePtrC( PathInfo::PhoneMemoryRootPath() ).Drive() );
       
    64     tmp.Append( KImportDir );      
       
    65     tmp.AppendFormat( KNewFileFormat, &uidStr ); 
       
    66     iNewFile = tmp.AllocL();
       
    67     tmp.Zero();
       
    68         
       
    69     iFileSession.PrivatePath( tmp );
       
    70     tmp.Insert( 0, TParsePtrC( PathInfo::PhoneMemoryRootPath() ).Drive() );
       
    71     tmp.Append( KPrivateFileDir );    
       
    72     iFileSession.MkDir( tmp );    
       
    73     tmp.AppendFormat( KPrivateFileFormat, &uidStr );
       
    74     iPrivateFile = tmp.AllocL();
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CSisxUIStartupFile::NewL
       
    79 // Two-phased constructor.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CSisxUIStartupFile* CSisxUIStartupFile::NewL( const TUid& aUid )
       
    83     {
       
    84     CSisxUIStartupFile* self = new( ELeave ) CSisxUIStartupFile();
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL( aUid );
       
    87     CleanupStack::Pop( self );
       
    88     return self;    
       
    89     }
       
    90     
       
    91 // Destructor
       
    92 CSisxUIStartupFile::~CSisxUIStartupFile()
       
    93     {
       
    94     iFileSession.Close();
       
    95     delete iPrivateFile;
       
    96     delete iNewFile;   
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CSisxUIStartupFile::NewFileExists
       
   101 // Checks if new startup file exists in the import directory.
       
   102 // (other items were commented in a header).
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 TBool CSisxUIStartupFile::NewFileExists()
       
   106     {
       
   107     TBool result ( EFalse );
       
   108     
       
   109     // See if the file exists
       
   110     TUint dummy;
       
   111     if ( iFileSession.Att( *iNewFile, dummy ) == KErrNone )
       
   112         {
       
   113         result = ETrue;
       
   114         }
       
   115 
       
   116     return result;
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CSisxUIStartupFile::PrivateFileExists
       
   121 // Checks if old, processed startup file exists in the private directory.
       
   122 // (other items were commented in a header).
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 TBool CSisxUIStartupFile::PrivateFileExists()
       
   126     {
       
   127     TBool result ( EFalse );
       
   128 
       
   129     // See if the file exists
       
   130     TUint dummy;
       
   131     if ( iFileSession.Att( *iPrivateFile, dummy) == KErrNone )
       
   132         {
       
   133         result = ETrue;
       
   134         }
       
   135 
       
   136     return result;
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CSisxUIStartupFile::ProcessNewFile
       
   141 // Moves the new startup file from import directory to private directory and
       
   142 // returns all startup items in that file.
       
   143 // (other items were commented in a header).
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TInt CSisxUIStartupFile::ProcessNewFile( RArray<TStartupTaskParam>& aStartupItems,
       
   147                                          const RPointerArray<HBufC>& aFiles )
       
   148     {
       
   149     TInt result( KErrNone );
       
   150     // should check if old file is already there
       
   151     
       
   152     TRAP( result, ReadNewStartupItemsL( aStartupItems, aFiles ) );
       
   153     if ( result == KErrNone )
       
   154         { 
       
   155         TRAP( result, WritePrivateStartupItemsL( aStartupItems ) );
       
   156         }    
       
   157 
       
   158     if ( result != KErrNone )
       
   159         {        
       
   160         // Do some cleanup
       
   161         aStartupItems.Close();
       
   162         if ( PrivateFileExists() )
       
   163             {
       
   164             iFileSession.Delete( *iPrivateFile );
       
   165             }
       
   166         }    
       
   167 
       
   168     return result;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CSisxUIStartupFile::RemovePrivateFile
       
   173 // Deletes the old startup file from private directory and returns all startup 
       
   174 // items in that file.
       
   175 // (other items were commented in a header).
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 TInt CSisxUIStartupFile::RemovePrivateFile( RArray<TStartupTaskParam>& aStartupItems )
       
   179     {
       
   180     TInt result( KErrNone );
       
   181     TRAP( result, ReadPrivateStartupItemsL( aStartupItems ) );
       
   182     if ( result == KErrNone )
       
   183         {
       
   184         iFileSession.Delete( *iPrivateFile );
       
   185         } 
       
   186     else
       
   187         {
       
   188         // Do some cleanup
       
   189         aStartupItems.Close();        
       
   190         }    
       
   191    
       
   192     return result;
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CSisxUIStartupFile::ClearAllNewFiles
       
   197 // Deletes all resource files from import directory.
       
   198 // (other items were commented in a header).
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 TInt CSisxUIStartupFile::ClearAllNewFiles()
       
   202     {
       
   203     TInt result( KErrNone );
       
   204     RFs fileSess;
       
   205     CFileMan* fileMan = NULL;
       
   206     
       
   207     result = fileSess.Connect();
       
   208     if ( result == KErrNone )
       
   209         {
       
   210         TRAP( result, fileMan = CFileMan::NewL( fileSess ) );
       
   211         }
       
   212     
       
   213     if ( result == KErrNone )
       
   214         {  
       
   215         TFileName resFile;
       
   216         fileSess.PrivatePath( resFile );
       
   217         resFile.Insert( 0, TParsePtrC( PathInfo::PhoneMemoryRootPath() ).Drive() );
       
   218         resFile.Append( KImportDir );      
       
   219         resFile.Append( KNewFileMatch ); 
       
   220         fileMan->Delete( resFile );
       
   221         }
       
   222     
       
   223     delete fileMan;    
       
   224     fileSess.Close();
       
   225     
       
   226     return result;
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CSisxUIStartupFile::ReadStartupItemsL
       
   231 // Get all startup items from the given resource file.
       
   232 // (other items were commented in a header).
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 void CSisxUIStartupFile::ReadNewStartupItemsL( RArray<TStartupTaskParam>& aStartupItems,
       
   236                                                const RPointerArray<HBufC>& aFiles )
       
   237     {
       
   238     RResourceFile resource;
       
   239     CleanupClosePushL( resource );
       
   240     resource.OpenL( iFileSession, *iNewFile );
       
   241   
       
   242     TInt index( 0 );    
       
   243 
       
   244     // Read resources
       
   245     for ( index = 1; resource.OwnsResourceId( index ); index++ )
       
   246         {
       
   247         // Create the reader
       
   248         HBufC8* resBuffer = resource.AllocReadLC( index );
       
   249         TResourceReader reader;
       
   250         reader.SetBuffer( resBuffer );
       
   251  
       
   252         // Read version byte
       
   253         TInt version = reader.ReadUint8();        
       
   254      
       
   255         // Read actual parameters
       
   256         TStartupTaskParam item;
       
   257         item.iFileName.Copy( reader.ReadTPtrC() );        
       
   258         item.iRecoveryPolicy = (TStartupExceptionPolicy) reader.ReadUint16();
       
   259         
       
   260         aStartupItems.Append( item );
       
   261 
       
   262         CleanupStack::PopAndDestroy( resBuffer );
       
   263         }   
       
   264 
       
   265     // Validate the array. Each and every executable to be added to the 
       
   266     // startup list must also be installed by this package.
       
   267     for ( index = 0; index < aStartupItems.Count(); index++ ) 
       
   268         {
       
   269         // Sanity check
       
   270         if ( !aStartupItems[index].iFileName.Length() )
       
   271             {
       
   272             continue;
       
   273             }        
       
   274              
       
   275         TBool entryFound( EFalse );        
       
   276         TInt j( 0 );        
       
   277         iTemp = aStartupItems[index].iFileName;
       
   278         iTemp.Delete( 0, 1 );        
       
   279         iTemp.Fold();        
       
   280 
       
   281         for ( j = 0; j < aFiles.Count(); j++ )
       
   282             {            
       
   283             if ( aFiles[j]->FindF( iTemp ) >= 0 )
       
   284                 {
       
   285                 entryFound = ETrue;
       
   286                 // Replace the possible ! with correct drive
       
   287                 if ( aStartupItems[index].iFileName[0] == '!' )
       
   288                     {
       
   289                     TBuf<1> temp;
       
   290                     temp.Append( (*aFiles[j])[0] );                    
       
   291                     aStartupItems[index].iFileName.Replace(0, 1, temp );                    
       
   292                     }
       
   293                 
       
   294                 break;                
       
   295                 }            
       
   296             }
       
   297 
       
   298         if ( !entryFound )
       
   299             {
       
   300             aStartupItems.Remove( index );
       
   301             index--;            
       
   302             }        
       
   303         }    
       
   304 
       
   305     CleanupStack::PopAndDestroy(); // resource
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // CSisxUIStartupFile::ReadPrivateStartupItemsL
       
   310 // Read startup items from private file.
       
   311 // (other items were commented in a header).
       
   312 // -----------------------------------------------------------------------------
       
   313 //
       
   314 void CSisxUIStartupFile::ReadPrivateStartupItemsL( RArray<TStartupTaskParam>& aStartupItems )
       
   315     {
       
   316     RFileReadStream reader;
       
   317     reader.PushL();
       
   318     
       
   319     User::LeaveIfError( reader.Open( iFileSession, *iPrivateFile, EFileRead ) );
       
   320     
       
   321     // Read the number of entries
       
   322     TInt count = reader.ReadInt32L();
       
   323 
       
   324      // Read file names from the file
       
   325     while ( count-- )
       
   326         {
       
   327         TStartupTaskParam item;
       
   328 
       
   329         HBufC* fileName = HBufC::NewLC( reader, KMaxFileName );
       
   330         item.iFileName.Copy( *fileName );        
       
   331         CleanupStack::PopAndDestroy( fileName);       
       
   332 
       
   333         aStartupItems.Append( item );
       
   334         }
       
   335 
       
   336     CleanupStack::PopAndDestroy(); // reader
       
   337     }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // CSisxUIStartupFile::WritePrivateStartupItemsL
       
   341 // Write startup items into private file.
       
   342 // (other items were commented in a header).
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void CSisxUIStartupFile::WritePrivateStartupItemsL( RArray<TStartupTaskParam>& aStartupItems )
       
   346     {
       
   347     if ( PrivateFileExists() )
       
   348         {
       
   349         iFileSession.Delete( *iPrivateFile );
       
   350         }
       
   351 
       
   352     if ( aStartupItems.Count() > 0 )
       
   353         {
       
   354         RFileWriteStream writer;
       
   355         writer.PushL();    
       
   356         User::LeaveIfError( writer.Create( iFileSession, *iPrivateFile, EFileWrite ) );
       
   357     
       
   358         // Write the number of entries to the file
       
   359         writer.WriteInt32L( aStartupItems.Count() );
       
   360         
       
   361         // Write file names to the file
       
   362         for ( TInt index = 0; index < aStartupItems.Count(); index++ )
       
   363             {
       
   364             if ( iFileSession.IsValidName( aStartupItems[index].iFileName ) )
       
   365                 {            
       
   366                 writer << aStartupItems[index].iFileName;
       
   367                 }
       
   368             else
       
   369                 {
       
   370                 User::Leave( KErrArgument );            
       
   371                 }               
       
   372             }
       
   373 
       
   374         writer.CommitL();    
       
   375         CleanupStack::PopAndDestroy(); // writer
       
   376         }    
       
   377     }
       
   378 
       
   379 //  End of File