menufw/hierarchynavigator/hnmetadatamodel/src/hnxmlsuitefilesreg.cpp
branchRCL_3
changeset 26 1b758917cafc
parent 25 137ebc85284b
child 28 d721605b30d0
equal deleted inserted replaced
25:137ebc85284b 26:1b758917cafc
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  
       
    15 *  Version     : %version:  9 % << Don't touch! Updated by Synergy at check-out.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <sysutil.h> 
       
    20 #include "hnxmlsuitefilesreg.h"
       
    21 #include "hnglobals.h"
       
    22 #include "hnconvutils.h"
       
    23 #include "menudebug.h"
       
    24 #include "hnhelper.inl"
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // 
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 TBool CHnXmlSuiteFilesReg::SynchronizeL( RFs & aFs, const TDesC &aSuiteName )
       
    33 	{
       
    34 	CHnXmlSuiteFilesReg* self = new (ELeave) CHnXmlSuiteFilesReg( aFs );
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL( aSuiteName );
       
    37 	TBool ret = self->SynchronizeSuiteFilesL();
       
    38 	CleanupStack::PopAndDestroy( self );
       
    39 	return ret;
       
    40 	}
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // 
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CHnXmlSuiteFilesReg::~CHnXmlSuiteFilesReg()
       
    47 	{
       
    48 	iSuiteName.Close();
       
    49 	DestroySuiteFilesMaps();
       
    50 	delete iFileMan;
       
    51 	}
       
    52    
       
    53 // ---------------------------------------------------------------------------
       
    54 // 
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CHnXmlSuiteFilesReg::CHnXmlSuiteFilesReg( RFs & aFs ) : iFs(aFs),
       
    58 	iSuiteFiles(&TDesC16Hash, &TDesC16Ident)
       
    59 	{
       
    60 
       
    61 	}
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // 
       
    65 // ---------------------------------------------------------------------------
       
    66 //   
       
    67 HBufC* CHnXmlSuiteFilesReg::GetSuiteInstallPathL()
       
    68 	{
       
    69 	TBuf<KMaxPath> installPath;	
       
    70 	User::LeaveIfError( iFs.PrivatePath(installPath) );
       
    71 	TChar drive;
       
    72 	TBuf<1> driveLetter;
       
    73 	iFs.DriveToChar( EDriveC, drive );
       
    74 	installPath.Insert(0, KColon );
       
    75 	driveLetter.Append( drive );
       
    76 	installPath.Insert(0, driveLetter );
       
    77 	installPath.Append(KEntriesSuffix);
       
    78 	installPath.Append( iSuiteName );
       
    79 	installPath.Append( Kbackslash );
       
    80 	return installPath.AllocL();
       
    81 	}
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // 
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CHnXmlSuiteFilesReg::ConstructL( const TDesC &aSuiteName )
       
    88 	{
       
    89 	iSuiteName.CreateL( aSuiteName );
       
    90 	iFileMan = CFileMan::NewL(iFs);
       
    91 	}
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // 
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void CHnXmlSuiteFilesReg::DestroySuiteFilesMaps()
       
    98 	{
       
    99 	DestroyMapValue( iSuiteFiles );
       
   100 	iSuiteFiles.Close();
       
   101 	}
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // 
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 HBufC* CHnXmlSuiteFilesReg::GetSuiteImportDirL( TInt aDriveNumber ) const
       
   108 	{
       
   109 	TBuf<KMaxPath> outputDir; 
       
   110 			
       
   111 	TChar driveLetter;
       
   112     User::LeaveIfError( iFs.DriveToChar( aDriveNumber, driveLetter ) );  
       
   113     
       
   114     TBuf< KSingleChar > driveLetterConst;
       
   115     driveLetterConst.Append( driveLetter );
       
   116       
       
   117     User::LeaveIfError( iFs.PrivatePath( outputDir ) );
       
   118 
       
   119     outputDir.Insert( 0, driveLetterConst );
       
   120     outputDir.Insert( 1, KColon );
       
   121     outputDir.Append( KSuitesDir );
       
   122     outputDir.Append( iSuiteName );
       
   123     outputDir.Append( Kbackslash );
       
   124     
       
   125     return outputDir.AllocL();
       
   126 	}
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // 
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void CHnXmlSuiteFilesReg::SearchDrivesForSuiteFilesL()
       
   134     {
       
   135     DestroySuiteFilesMaps();
       
   136     
       
   137     TDriveList driveList;
       
   138     User::LeaveIfError( iFs.DriveList( driveList ) );
       
   139     TFixedArray< TInt, KMaxDrives > driveSearchOrder( KDriveSearchOrder, KMaxDrives );
       
   140     
       
   141     for(TInt iterator(0); iterator < driveSearchOrder.Count(); iterator++ )
       
   142     	{ 
       
   143         if ( driveList[ driveSearchOrder[ iterator ] ] )
       
   144             {
       
   145             RBuf filePath( GetSuiteImportDirL( driveSearchOrder[ iterator ] ) );
       
   146             CleanupClosePushL( filePath );
       
   147             SearchDirForSuiteFilesL(filePath);
       
   148             CleanupStack::PopAndDestroy( &filePath );
       
   149             }
       
   150     	} 
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // 
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 void CHnXmlSuiteFilesReg::SearchDirForSuiteFilesL( const TDesC& aSuitePath )
       
   158     {
       
   159     CDir* fileList = NULL;
       
   160     if (!iFs.GetDir( aSuitePath, KEntryAttMaskSupported, ESortByName, fileList ))
       
   161 	    {
       
   162 	    CleanupStack::PushL( fileList );
       
   163         for( TInt i( 0 ); i < fileList->Count(); i++ ) 
       
   164             {  
       
   165             if (! (*fileList)[i].IsDir() )
       
   166         	    {
       
   167         	    AddSuiteFileToMapL( aSuitePath, (*fileList)[i] );
       
   168         	    }
       
   169             }
       
   170         CleanupStack::PopAndDestroy( fileList );
       
   171         }
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // 
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CHnXmlSuiteFilesReg::AddSuiteFileToMapL( const TDesC& aDir, const TEntry& aFile )
       
   179 	{
       
   180     THnFileInfo fileInfo = { aFile.iName, aDir, aFile.iModified, aFile.iSize };
       
   181 	fileInfo.iFilePath.Append( aFile.iName );
       
   182 		
       
   183 	THnFileInfo** existingFileInfo = iSuiteFiles.Find( &aFile.iName );
       
   184 	
       
   185     TInt newFileDrive( KErrNotFound );
       
   186     iFs.CharToDrive( fileInfo.iFilePath[0], newFileDrive );
       
   187     
       
   188     TBool newerThanExisting( EFalse );
       
   189 	TInt existingFileDrive( KErrNotFound );
       
   190 	if ( existingFileInfo )
       
   191 		{
       
   192 		iFs.CharToDrive( (*existingFileInfo)->iFilePath[0], existingFileDrive);
       
   193         newerThanExisting =
       
   194                 (*existingFileInfo)->iLastModified < fileInfo.iLastModified &&
       
   195                 newFileDrive != EDriveZ;
       
   196 		}
       
   197 	
       
   198     if ( !existingFileInfo || newerThanExisting || existingFileDrive == EDriveZ )
       
   199         {
       
   200         if( existingFileInfo )
       
   201             {
       
   202             iSuiteFiles.Remove( &aFile.iName );
       
   203             }
       
   204         THnFileInfo* fileInfoPointer = new (ELeave) THnFileInfo( fileInfo );
       
   205         iSuiteFiles.InsertL( &fileInfo.iFileName, fileInfoPointer );
       
   206         }
       
   207 	}
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // 
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 TInt CHnXmlSuiteFilesReg::CalculateCurrentSuiteSizeL()
       
   214 	{
       
   215 	RBuf suiteInstallDir( GetSuiteInstallPathL() );
       
   216 	CleanupClosePushL( suiteInstallDir );
       
   217 	CDir* suiteFileList;                  
       
   218 	iFs.GetDir( suiteInstallDir, KEntryAttMaskSupported, ESortByName, suiteFileList);
       
   219 	CleanupStack::PushL(suiteFileList);
       
   220 
       
   221 	TInt suiteSize(0); 
       
   222 	for( TInt i(0); suiteFileList && i< suiteFileList->Count(); i++ )  
       
   223 		{
       
   224 		suiteSize+= (*suiteFileList)[i].iSize;
       
   225 		}
       
   226 	CleanupStack::PopAndDestroy( suiteFileList );
       
   227 	CleanupStack::PopAndDestroy( &suiteInstallDir );
       
   228 	return suiteSize;
       
   229 	}
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // 
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 TInt CHnXmlSuiteFilesReg::CalculateSynchronizationSuiteSize()
       
   236 	{
       
   237 	TInt retSize( 0 );
       
   238 	
       
   239 	THashMapIter< const TDesC*, THnFileInfo* > iter( iSuiteFiles );
       
   240 	
       
   241 	while ( iter.NextKey() )
       
   242 		{
       
   243 		const THnFileInfo& fileInfo =  **(iter.CurrentValue());
       
   244 		retSize += fileInfo.iSize;
       
   245 		}
       
   246 	
       
   247 	return retSize;
       
   248 	}
       
   249 // ---------------------------------------------------------------------------
       
   250 // 
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 TBool CHnXmlSuiteFilesReg::SynchronizeSuiteFilesL()
       
   254 	{
       
   255 	TBool ret( EFalse );
       
   256 	SearchDrivesForSuiteFilesL();
       
   257 
       
   258 	TInt spaceNeeded = CalculateSynchronizationSuiteSize() -
       
   259 		CalculateCurrentSuiteSizeL();
       
   260 	
       
   261 	RBuf suiteInstallationDir( GetSuiteInstallPathL() );
       
   262 	CleanupClosePushL( suiteInstallationDir );
       
   263 	iFileMan->RmDir( suiteInstallationDir );
       
   264 	if( iSuiteFiles.Count() && ( spaceNeeded < 0 || !SysUtil::DiskSpaceBelowCriticalLevelL(
       
   265 	        &iFs, spaceNeeded , EDriveC ) ) )
       
   266         {
       
   267         THashMapIter< const TDesC*, THnFileInfo* > iter( iSuiteFiles );
       
   268         iFs.MkDirAll( suiteInstallationDir );
       
   269         while( iter.NextKey() )
       
   270             {
       
   271             const THnFileInfo& fileInfo =  **(iter.CurrentValue());
       
   272             TBuf<KMaxPath> filePath( suiteInstallationDir );
       
   273             filePath.Append( fileInfo.iFileName );
       
   274             if( filePath.Compare( fileInfo.iFilePath ) )
       
   275                 {
       
   276                 if( BaflUtils::FileExists( iFs, filePath ) )
       
   277                     {
       
   278                     iFs.SetAtt( filePath, NULL, KEntryAttReadOnly );
       
   279                     iFs.Delete( filePath );
       
   280                     }
       
   281                 iFileMan->Copy(
       
   282                         fileInfo.iFilePath,
       
   283                         filePath, CFileMan::EOverWrite );
       
   284                 ret = ETrue;
       
   285                 }
       
   286             }
       
   287         }
       
   288 	CleanupStack::PopAndDestroy( &suiteInstallationDir );
       
   289 	return ret;
       
   290 	}