connectivitymodules/SeCon/services/ftp/src/sconfshandler.cpp
branchRCL_3
changeset 52 4f0867e42d62
parent 51 8e7494275d3a
child 56 3e6957da2ff8
equal deleted inserted replaced
51:8e7494275d3a 52:4f0867e42d62
     1 /*
       
     2 * Copyright (c) 2005-2010 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:  File Transfer Controller implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <stringresourcereader.h>
       
    21 #include <sconftp.rsg>
       
    22 #include <pathinfo.h>
       
    23 #include <connect/sbdefs.h> // to get backup/restore mode
       
    24 #include <driveinfo.h>
       
    25 #include <bautils.h>
       
    26 #include <sysutil.h>
       
    27 #include <CDirectoryLocalizer.h>
       
    28 
       
    29 #include "sconfshandler.h"
       
    30 #include "sconconsts.h"
       
    31 #include "irmcconsts.h"
       
    32 #include "cntparserclient.h"
       
    33 #include "cntparserinfolog.h"
       
    34 #include "debug.h"
       
    35 
       
    36 // cntparser related constants
       
    37 _LIT(KPhoneBookIrMCL2Request,"telecom/pb.vcf");
       
    38 _LIT(KInfoLogIrMCL2Request,"telecom/pb/info.log");
       
    39 _LIT(KInfoLogIrMCL2File,"IrMC\\info.log");
       
    40 _LIT(KInfoLogPathAndFileName,"c:\\IrMC\\info.log");
       
    41 _LIT(KPbPathAndFileName,"c:\\IrMC\\pb.vcf");
       
    42 
       
    43 // ============================= MEMBER FUNCTIONS ===============================
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CSConFsHandler::NewL()
       
    47 // Two-phase constructor
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CSConFsHandler* CSConFsHandler::NewL( RFs& aFs )
       
    51     {
       
    52     TRACE_FUNC_ENTRY;
       
    53     CSConFsHandler* self = new (ELeave) CSConFsHandler( aFs );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop( self );
       
    57     TRACE_FUNC_EXIT;
       
    58     return self;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CSConFsHandler::~CSConFsHandler()
       
    63 // Destructor
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CSConFsHandler::~CSConFsHandler()
       
    67     {
       
    68     TRACE_FUNC_ENTRY;
       
    69     delete iLocalizer;
       
    70     TRACE_FUNC_EXIT;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CSConFsHandler::ParseFolderListL( CBufFlat* aBuffer, const TPtrC aPathName
       
    75 //                                  , const TInt aMemNumber )
       
    76 // Parses folder and file listing from specified directory
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 TInt CSConFsHandler::ParseFolderListL( CBufFlat* aBuffer, const TPtrC aPathName,
       
    80                                        const TInt aMemNumber )
       
    81     {
       
    82     TRACE_FUNC_ENTRY;
       
    83     LOGGER_WRITE_1( "CSConFsHandler::ParseFolderListL aPathName =  %S", &aPathName );
       
    84     if( aPathName.Length() == 0 )
       
    85         {
       
    86         LOGGER_WRITE( "CSConFsHandler::ParseFolderListL( CBufFlat* aBuffer, const TPtrC aPathName ) : No path" );
       
    87         return KErrArgument;
       
    88         }
       
    89             
       
    90     if( aPathName.Length() > KDriveLength && !BaflUtils::FolderExists( iFs, aPathName ) )
       
    91         {
       
    92         LOGGER_WRITE( "CSConFsHandler::ParseFolderListL( CBufFlat* aBuffer, const TPtrC aPathName ) : Current folder doesn't exist any more" );
       
    93         return KErrNotFound;
       
    94         }
       
    95     
       
    96     TInt driveNumber;
       
    97     TInt err = iFs.CharToDrive(aPathName[0], driveNumber);
       
    98     if( err )
       
    99         {
       
   100         LOGGER_WRITE_1( "CSConFsHandler::ParseFolderListL : CharToDrive err: %d", err );
       
   101         return KErrNotFound;
       
   102         }
       
   103     TUint driveStatus;
       
   104     err = DriveInfo::GetDriveStatus( iFs, driveNumber, driveStatus);
       
   105     if( err )
       
   106         {
       
   107         LOGGER_WRITE_1( "CSConFsHandler::ParseFolderListL : DriveInfo::GetDriveStatus err: %d", err );
       
   108         return KErrNotFound;
       
   109         }
       
   110     
       
   111     // check mem type (DEV, DEV2, MMC, MMC2..)
       
   112     TBuf8<KSConMemTypeMaxLength> memType;
       
   113     if( driveStatus & DriveInfo::EDriveInternal )
       
   114         {
       
   115         memType.Copy( KSConMemoryTypeDev );
       
   116         if( aMemNumber > 1 )
       
   117             {
       
   118             memType.AppendNum(aMemNumber);
       
   119             }
       
   120         }
       
   121     else if( driveStatus & DriveInfo::EDriveRemovable )
       
   122         {
       
   123         memType.Copy( KSConMemoryTypeMmc );
       
   124         if( aMemNumber > 1 )
       
   125             {
       
   126             memType.AppendNum(aMemNumber);
       
   127             }
       
   128         }
       
   129 
       
   130     CDir *entryList = 0;
       
   131     CDir *dirList = 0;
       
   132     
       
   133     // show only normal files and folders
       
   134     TUint entryAttMask = KEntryAttNormal;
       
   135     
       
   136     
       
   137     // get folders and files
       
   138     err = iFs.GetDir( aPathName, entryAttMask, ESortByName, 
       
   139             entryList, dirList );
       
   140     
       
   141     if ( err )
       
   142         {
       
   143         LOGGER_WRITE_1( "ParseFolderListL GetDir returned: %d", err );
       
   144         return err;
       
   145         }
       
   146     
       
   147     CleanupStack::PushL( entryList );
       
   148     CleanupStack::PushL( dirList );
       
   149     
       
   150     TInt offset = 0;
       
   151     
       
   152     LOGGER_WRITE( "CSConFsHandler::ParseFolderList XML creation" );
       
   153     //Create folder XML
       
   154     aBuffer->ResizeL( KSConXmlDocBegin().Length() );
       
   155     aBuffer->Write( offset, KSConXmlDocBegin );
       
   156     offset += KSConXmlDocBegin().Length();
       
   157 
       
   158     aBuffer->ExpandL( offset, KSConXmlParentFolder().Length() );
       
   159     aBuffer->Write( offset, KSConXmlParentFolder );
       
   160     offset += KSConXmlParentFolder().Length();
       
   161 
       
   162     HBufC* fullNameBuf = HBufC::NewLC( KMaxPath );
       
   163     TPtr fullName = fullNameBuf->Des();
       
   164     
       
   165     // Print folders to folder listing object
       
   166     for( TInt i = 0; i < dirList->Count(); i++ )
       
   167         {
       
   168         const TEntry entry = ( *dirList )[i];
       
   169         
       
   170         fullName.Copy( aPathName );
       
   171         fullName.Append( entry.iName );
       
   172         fullName.Append( KPathDelimiter );
       
   173         
       
   174         // show path if not exluded
       
   175         TBool proceed = !IsExludedPath( fullName );
       
   176         if ( proceed )
       
   177             {
       
   178             PrintFolderL( aBuffer, offset, fullName, memType, entry );
       
   179             }
       
   180         }
       
   181     CleanupStack::PopAndDestroy( fullNameBuf );
       
   182     CleanupStack::PopAndDestroy( dirList );
       
   183     
       
   184     LOGGER_WRITE("Print files");
       
   185     // Print files to folder listing object
       
   186     for( TInt j = 0; j < entryList->Count(); j++ )
       
   187         {
       
   188         const TEntry entry = ( *entryList )[j];
       
   189         PrintFileL( aBuffer, offset, entry );
       
   190         }
       
   191     LOGGER_WRITE("files printed");
       
   192     //Write the end of XML
       
   193     aBuffer->ExpandL( offset, KSConXmlFolderListEnd().Length() );
       
   194     aBuffer->Write( offset, KSConXmlFolderListEnd );
       
   195     aBuffer->Compress();
       
   196     
       
   197     CleanupStack::PopAndDestroy( entryList );
       
   198 
       
   199     LOGGER_WRITE_1( "CSConFsHandler::ParseFolderListL() : returned %d", err );
       
   200     return err;
       
   201     }
       
   202 
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CSConFsHandler::PrintFolderL
       
   206 // Prints folder entry to folder listing object
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CSConFsHandler::PrintFolderL( CBufFlat* aBuffer, TInt& aOffset, const TDesC& aFullPath,
       
   210                     const TDesC8& aMemType, const TEntry& aFolderEntry )
       
   211     {
       
   212     TRACE_FUNC_ENTRY;
       
   213     // initialize Localizer
       
   214     iLocalizer->SetFullPath( aFullPath );
       
   215     
       
   216     TBool dataPath( EFalse );
       
   217     if ( aFullPath.CompareF( PathInfo::PhoneMemoryRootPath() ) == 0 ) // datapath
       
   218         {
       
   219         // printable folder is C:\Data (Localized)
       
   220         dataPath = ETrue;
       
   221         }
       
   222     
       
   223     // print foldername
       
   224     aBuffer->ExpandL( aOffset, KSConXmlFolderNameBegin().Length() );
       
   225     aBuffer->Write( aOffset, KSConXmlFolderNameBegin );
       
   226     aOffset += KSConXmlFolderNameBegin().Length();
       
   227     
       
   228     HBufC8* folderBuf = HBufC8::NewLC( KMaxFileName );
       
   229     TPtr8 folderName = folderBuf->Des();
       
   230     User::LeaveIfError( 
       
   231         CnvUtfConverter::ConvertFromUnicodeToUtf8( folderName, aFolderEntry.iName ) );
       
   232     
       
   233     //Replace special characters
       
   234     ReplaceSpecialChars( folderName );
       
   235 
       
   236     aBuffer->ExpandL( aOffset, folderName.Length() );
       
   237     aBuffer->Write( aOffset, folderName );
       
   238     aOffset += folderName.Length();
       
   239     
       
   240     CleanupStack::PopAndDestroy( folderBuf );
       
   241     
       
   242     // print modified time
       
   243     TBuf<KSConDateMaxLength> modifiedTime;
       
   244     TTime time = aFolderEntry.iModified;
       
   245     time.FormatL( modifiedTime, KSConXmlDate );
       
   246     
       
   247     TBuf8<KSConDateMaxLength> modifiedTime8;
       
   248     User::LeaveIfError( 
       
   249         CnvUtfConverter::ConvertFromUnicodeToUtf8( modifiedTime8, modifiedTime ) );
       
   250     
       
   251     aBuffer->ExpandL( aOffset, KSConXmlModified().Length() );
       
   252     aBuffer->Write( aOffset, KSConXmlModified );
       
   253     aOffset += KSConXmlModified().Length();
       
   254     
       
   255     aBuffer->ExpandL( aOffset, modifiedTime8.Length() );
       
   256     aBuffer->Write( aOffset, modifiedTime8 );
       
   257     aOffset += modifiedTime8.Length();
       
   258     
       
   259     // print attributes
       
   260     aBuffer->ExpandL( aOffset, KSConXmlUserAttributes().Length() );
       
   261     aBuffer->Write( aOffset, KSConXmlUserAttributes );
       
   262     aOffset += KSConXmlUserAttributes().Length();
       
   263     
       
   264     // Add user-perm info
       
   265     if( aFolderEntry.IsReadOnly() )
       
   266         {
       
   267         aBuffer->ExpandL( aOffset, 
       
   268             KSConXmlUserEntryReadOnly().Length() );
       
   269         aBuffer->Write( aOffset, KSConXmlUserEntryReadOnly );
       
   270         aOffset += KSConXmlUserEntryReadOnly().Length();
       
   271         }
       
   272     else if( iLocalizer->IsLocalized() || dataPath ) // delete disabled
       
   273         {
       
   274         aBuffer->ExpandL( aOffset, 
       
   275             KSConXmlUserEntryDrive().Length() );
       
   276         aBuffer->Write( aOffset, KSConXmlUserEntryDrive );
       
   277         aOffset += KSConXmlUserEntryDrive().Length();
       
   278         }
       
   279     else
       
   280         {
       
   281         aBuffer->ExpandL( aOffset, 
       
   282             KSConXmlUserEntryArchive().Length() );
       
   283         aBuffer->Write( aOffset, KSConXmlUserEntryArchive );
       
   284         aOffset += KSConXmlUserEntryArchive().Length();
       
   285         }
       
   286     
       
   287     
       
   288     // print memory type
       
   289     aBuffer->ExpandL( aOffset, KSConXmlMemoryType().Length() );
       
   290     aBuffer->Write( aOffset, KSConXmlMemoryType );
       
   291     aOffset += KSConXmlMemoryType().Length();
       
   292     
       
   293     aBuffer->ExpandL( aOffset, aMemType.Length() );
       
   294     aBuffer->Write( aOffset, aMemType );
       
   295     aOffset += aMemType.Length();
       
   296     
       
   297     // print label if exists
       
   298     if( iLocalizer->IsLocalized() )
       
   299         {
       
   300         //get localized folder name
       
   301         HBufC8* labelBuf = 
       
   302             CnvUtfConverter::ConvertFromUnicodeToUtf8L( iLocalizer->LocalizedName() );
       
   303         CleanupStack::PushL( labelBuf );
       
   304         TPtrC8 label = labelBuf->Des();
       
   305         //memory label
       
   306         aBuffer->ExpandL( aOffset, 
       
   307             KSConXmlMemoryLabel().Length() );
       
   308         aBuffer->Write( aOffset, KSConXmlMemoryLabel );
       
   309         aOffset += KSConXmlMemoryLabel().Length();
       
   310         
       
   311         aBuffer->ExpandL( aOffset, label.Length() );
       
   312         aBuffer->Write( aOffset, label );
       
   313         aOffset += label.Length();
       
   314         
       
   315         CleanupStack::PopAndDestroy( labelBuf );
       
   316         }
       
   317     if ( dataPath ) // datapath
       
   318         {
       
   319         //memory label
       
   320         aBuffer->ExpandL( aOffset, 
       
   321             KSConXmlMemoryLabel().Length() );
       
   322         aBuffer->Write( aOffset, KSConXmlMemoryLabel );
       
   323         aOffset += KSConXmlMemoryLabel().Length();
       
   324         
       
   325         aBuffer->ExpandL( aOffset, iDataPathName.Length() );
       
   326         aBuffer->Write( aOffset, iDataPathName );
       
   327         aOffset += iDataPathName.Length();
       
   328         }
       
   329     // print ending
       
   330     aBuffer->ExpandL( aOffset, KSConXmlFileEnd().Length() );
       
   331     aBuffer->Write( aOffset, KSConXmlFileEnd );
       
   332     aOffset += KSConXmlFileEnd().Length();
       
   333     TRACE_FUNC_EXIT;
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CSConFsHandler::PrintFileL
       
   338 // Prints file entry to folder listing object
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 void CSConFsHandler::PrintFileL( CBufFlat* aBuffer, TInt& aOffset, const TEntry& aFileEntry )
       
   342     {
       
   343     LOGGER_WRITE_1("CSConFsHandler::PrintFileL, filename: %S", &aFileEntry.iName );
       
   344     HBufC8* fileBuf = HBufC8::NewLC( KMaxFileName );
       
   345     TPtr8 fileName = fileBuf->Des();
       
   346         
       
   347     User::LeaveIfError( 
       
   348         CnvUtfConverter::ConvertFromUnicodeToUtf8( fileName, aFileEntry.iName ) );
       
   349 
       
   350     //Replace special characters
       
   351     ReplaceSpecialChars( fileName );
       
   352         
       
   353     // print filename
       
   354     aBuffer->ExpandL( aOffset, KSConXmlFileNameBegin().Length() );
       
   355     aBuffer->Write( aOffset, KSConXmlFileNameBegin );
       
   356     aOffset += KSConXmlFileNameBegin().Length();
       
   357 
       
   358     aBuffer->ExpandL( aOffset, fileName.Length() );
       
   359     aBuffer->Write( aOffset, fileName );
       
   360     aOffset += fileName.Length();
       
   361 
       
   362     // print size
       
   363     aBuffer->ExpandL( aOffset, KSConXmlSize().Length() );
       
   364     aBuffer->Write( aOffset, KSConXmlSize );
       
   365     aOffset += KSConXmlSize().Length();
       
   366 
       
   367     fileName.Delete( 0, fileName.Length() ); // re-use fileName descriptor
       
   368     fileName.AppendNum( aFileEntry.iSize );
       
   369     aBuffer->ExpandL( aOffset, fileName.Length() );
       
   370     aBuffer->Write( aOffset, fileName );
       
   371     aOffset += fileName.Length();
       
   372     
       
   373     CleanupStack::PopAndDestroy( fileBuf );
       
   374 
       
   375     // print modified
       
   376     TBuf<KSConDateMaxLength> modifiedTime;
       
   377     TTime time = aFileEntry.iModified;
       
   378     time.FormatL( modifiedTime, KSConXmlDate );
       
   379     
       
   380     TBuf8<KSConDateMaxLength> modifiedTime8;
       
   381     User::LeaveIfError( 
       
   382         CnvUtfConverter::ConvertFromUnicodeToUtf8( modifiedTime8, modifiedTime ) );
       
   383     
       
   384     
       
   385     aBuffer->ExpandL( aOffset, KSConXmlModified().Length() );
       
   386     aBuffer->Write( aOffset, KSConXmlModified );
       
   387     aOffset += KSConXmlModified().Length();
       
   388     
       
   389     aBuffer->ExpandL( aOffset, modifiedTime8.Length() );
       
   390     aBuffer->Write( aOffset, modifiedTime8 );
       
   391     aOffset += modifiedTime8.Length();
       
   392 
       
   393     // attributes
       
   394     aBuffer->ExpandL( aOffset, KSConXmlUserAttributes().Length() );
       
   395     aBuffer->Write( aOffset, KSConXmlUserAttributes );
       
   396     aOffset += KSConXmlUserAttributes().Length();
       
   397 
       
   398     if( aFileEntry.IsSystem() )
       
   399         {
       
   400         aBuffer->ExpandL( aOffset, 
       
   401             KSConXmlUserEntryReadOnly().Length() );
       
   402         aBuffer->Write( aOffset, KSConXmlUserEntryReadOnly );
       
   403         aOffset += KSConXmlUserEntryReadOnly().Length();
       
   404         }
       
   405     else if( aFileEntry.IsReadOnly() )
       
   406         {
       
   407         LOGGER_WRITE("was readonly" );
       
   408         aBuffer->ExpandL( aOffset, 
       
   409             KSConXmlUserEntryReadOnly().Length() );
       
   410         aBuffer->Write( aOffset, KSConXmlUserEntryReadOnly );
       
   411         aOffset += KSConXmlUserEntryReadOnly().Length();
       
   412         }
       
   413     else
       
   414         {
       
   415         aBuffer->ExpandL( aOffset, 
       
   416             KSConXmlUserEntryArchive().Length() );
       
   417         aBuffer->Write( aOffset, KSConXmlUserEntryArchive );
       
   418         aOffset += KSConXmlUserEntryArchive().Length();
       
   419         }
       
   420     
       
   421 
       
   422     //Word document
       
   423     if ( aFileEntry.iType.IsPresent( KSConWordUid ) ||
       
   424             aFileEntry.iName.Find( KSConEpocWordExt ) == aFileEntry.iName.Length() - 4 )
       
   425         {
       
   426         aBuffer->ExpandL( aOffset, KSConXmlType().Length() );
       
   427         aBuffer->Write( aOffset, KSConXmlType );
       
   428         aOffset += KSConXmlType().Length();
       
   429         
       
   430         aBuffer->ExpandL( aOffset, KSConXmlTypeWord().Length() );
       
   431         aBuffer->Write( aOffset, KSConXmlTypeWord );
       
   432         aOffset += KSConXmlTypeWord().Length();
       
   433         }
       
   434     //Sheet document
       
   435     else if ( aFileEntry.iType.IsPresent( KSConSheetUid ) ||
       
   436             aFileEntry.iName.Find( KSConEpocSheetExt ) == aFileEntry.iName.Length() - 4 )
       
   437         {
       
   438         aBuffer->ExpandL( aOffset, KSConXmlType().Length() );
       
   439         aBuffer->Write( aOffset, KSConXmlType );
       
   440         aOffset += KSConXmlType().Length();
       
   441         
       
   442         aBuffer->ExpandL( aOffset, KSConXmlTypeSheet().Length() );
       
   443         aBuffer->Write( aOffset, KSConXmlTypeSheet );
       
   444         aOffset += KSConXmlTypeSheet().Length();
       
   445         }
       
   446 
       
   447     aBuffer->ExpandL( aOffset, KSConXmlFileEnd().Length() );
       
   448     aBuffer->Write( aOffset, KSConXmlFileEnd );
       
   449     aOffset += KSConXmlFileEnd().Length();
       
   450     }
       
   451 
       
   452 // -----------------------------------------------------------------------------
       
   453 // CSConFsHandler::GetFileObjectL( CObexFileObject*& aFileObject, 
       
   454 // const TPtrC aPathName, const TPtrC aFileName )
       
   455 // Gets a file object
       
   456 // -----------------------------------------------------------------------------
       
   457 //
       
   458 TInt CSConFsHandler::GetFileObjectL( CObexFileObject*& aFileObject, 
       
   459                                 const TPtrC aPathName, const TPtrC aFileName )
       
   460     {
       
   461     TRACE_FUNC_ENTRY;
       
   462     LOGGER_WRITE_2( "CSConFsHandler::GetFileObjectL aFileName =  %S aPathName = %S", &aFileName, &aPathName );
       
   463 
       
   464     TInt ret( KErrNone );
       
   465     
       
   466     if( ( aPathName.Length() == 0 || aFileName.Length() == 0 ) && 
       
   467     !( aFileName==KPhoneBookIrMCL2Request || 
       
   468     aFileName==KInfoLogIrMCL2Request ) )
       
   469         {
       
   470         return KErrArgument;
       
   471         }
       
   472 
       
   473     if( aPathName.Length() + aFileName.Length() > KMaxFileName )
       
   474         {
       
   475         return KErrAbort;
       
   476         }
       
   477 
       
   478     HBufC* pathBuf = HBufC::NewLC( KMaxFileName );
       
   479     TPtr pathBufPtr = pathBuf->Des();
       
   480 
       
   481     /*These modifiactions are mean to catch an IrMC L2 request. 
       
   482     If the request is made, then the requested phonebook file and
       
   483     info log -file are created. Path and filename is also changed 
       
   484     to point to these created files.*/
       
   485     if( aFileName == KPhoneBookIrMCL2Request || 
       
   486     aFileName == KInfoLogIrMCL2Request )
       
   487         {
       
   488         LOGGER_WRITE( "CSConFsHandler: IrMC L2 request!" );
       
   489         TInt err;
       
   490         TInt status;
       
   491         TInt contactsSaved = 0;
       
   492         RCntParserServer cntParserServer;
       
   493 
       
   494         err = cntParserServer.Connect();
       
   495         if( err != KErrNone )
       
   496             {
       
   497             LOGGER_WRITE( "CSConFsHandler: Cannot connect to the cntparserserver, ABORT!" );
       
   498 
       
   499             CleanupStack::PopAndDestroy( pathBuf );
       
   500             return KErrAbort;
       
   501             }
       
   502 
       
   503         LOGGER_WRITE( "CSConFsHandler: Time to call CreateIrMCL2PhoneBook" );
       
   504         TBool force = ( aFileName == KInfoLogIrMCL2Request );
       
   505         status=cntParserServer.CreateIrMCL2PhoneBook( KMaximumNumberOfContacts,
       
   506          force );
       
   507         LOGGER_WRITE_1( "CntParserServer.CreateIrMCL2PhoneBook returned %d", status );
       
   508          
       
   509         //Check how many vcards were saved
       
   510         contactsSaved = cntParserServer.ContactsSaved();    
       
   511         LOGGER_WRITE_1( "CntParserServer.ContactsSaved returned %d", contactsSaved );
       
   512 
       
   513         err = cntParserServer.Disconnect();
       
   514         LOGGER_WRITE_1( "CntParserServer.Disconnect() %d", err );
       
   515         if( err != KErrNone )
       
   516             {
       
   517             CleanupStack::PopAndDestroy( pathBuf );
       
   518             return KErrAbort;
       
   519             }
       
   520         //save info.log, if pb.vcf has changed (Note: KPBNotUpdated is returned
       
   521         // if no changes made to pb.vcf)
       
   522         if( status == KErrNone )    
       
   523             {
       
   524             LOGGER_WRITE( "CSConFsHandler: Create info.log" );
       
   525 
       
   526             CCntParserInfoLog* infoLog=CCntParserInfoLog::NewL();
       
   527             CleanupStack::PushL( infoLog );
       
   528             //see KMaximumNumberOfContacts for more details
       
   529             infoLog->SetMaximumRecords( contactsSaved ); 
       
   530             infoLog->SetTotalRecords( contactsSaved );
       
   531             TFileName privatepath( K_C_ROOT );
       
   532             privatepath.Append( KInfoLogIrMCL2File );
       
   533             infoLog->SaveToFileL( privatepath );
       
   534             CleanupStack::PopAndDestroy( infoLog );
       
   535             }
       
   536 
       
   537         if( aFileName == KInfoLogIrMCL2Request )
       
   538             {
       
   539             //Changes filename and path to info.log
       
   540             pathBufPtr.Copy( KInfoLogPathAndFileName ); 
       
   541             }
       
   542         else
       
   543             {
       
   544             //Changes filename and path to pb.vcf
       
   545             pathBufPtr.Copy( KPbPathAndFileName );  
       
   546             }
       
   547         }       
       
   548     else
       
   549         {
       
   550         pathBufPtr.Copy( aPathName );
       
   551         pathBufPtr.Append( aFileName );         
       
   552         }
       
   553     
       
   554     LOGGER_WRITE( "CSConFsHandler::GetFileObjectL() : aFileObject->InitFromFileL( pathBufPtr )" );
       
   555     TRAP( ret, aFileObject->InitFromFileL( pathBufPtr ) );
       
   556     LOGGER_WRITE( "CSConFsHandler::GetFileObjectL() : aFileObject->InitFromFileL( pathBufPtr ) ok" );
       
   557 
       
   558     CleanupStack::PopAndDestroy( pathBuf );
       
   559 
       
   560     LOGGER_WRITE_1( "CSConFsHandler::GetFileObjectL( CObexFileObject*& aFileObject, const TPtrC aPathName, const TPtrC aFileName ) : returned %d", ret );
       
   561     return ret;
       
   562     }
       
   563 
       
   564 // -----------------------------------------------------------------------------
       
   565 // CSConFsHandler::SaveFileObjectL( const TPtrC aFullPathName, const TTime aTime,
       
   566 // const TPtrC aTempFileName )
       
   567 // Stores a file object
       
   568 // -----------------------------------------------------------------------------
       
   569 //
       
   570 TInt CSConFsHandler::SaveFileObjectL( const TPtrC aFullPathName, 
       
   571                         const TTime aTime, const TPtrC aTempFileName )
       
   572     {
       
   573     TRACE_FUNC_ENTRY;
       
   574     LOGGER_WRITE_2( "CSConFsHandler::SaveFileObjectL aFullPathName = %S aTempFileName = %S", &aFullPathName, &aTempFileName );
       
   575 
       
   576     TInt ret( KErrNone );
       
   577     
       
   578     if( aFullPathName.Length() == 0 )
       
   579         {
       
   580         iFs.Delete( aTempFileName );
       
   581         return KErrPathNotFound;
       
   582         }
       
   583 
       
   584     ret = ValidateFileName( aFullPathName );
       
   585         
       
   586     if( ret == KErrNone )
       
   587         {
       
   588         TUint attr;
       
   589         TInt err = iFs.Att( BaflUtils::DriveAndPathFromFullName(aFullPathName), attr );
       
   590         if ( err == KErrNone && attr & KEntryAttReadOnly )
       
   591             {
       
   592             // folder is read only, permission denied
       
   593             iFs.Delete( aTempFileName );
       
   594             ret = KErrAccessDenied;
       
   595             }
       
   596         else if( !BaflUtils::FileExists( iFs, aFullPathName ) ) 
       
   597             {
       
   598             // file does not exist, rename.
       
   599             ret = iFs.Rename( aTempFileName, aFullPathName );
       
   600 
       
   601             if( ret == KErrNone )
       
   602                 {
       
   603                 ret = iFs.SetModified( aFullPathName, aTime );
       
   604                 ret = KErrNone;
       
   605                 }
       
   606             else
       
   607                 {
       
   608                 iFs.Delete( aTempFileName );
       
   609                 }
       
   610             }
       
   611         else
       
   612             {
       
   613             LOGGER_WRITE( "CSConFsHandler::SaveFileObjectL() file exists. replace" );
       
   614             // file exists. Replace if not read-only file.
       
   615             TUint attr;
       
   616             ret = iFs.Att( aFullPathName, attr );
       
   617             // Check permissions
       
   618             if( ret == KErrNone && ( attr == KEntryAttNormal || 
       
   619             attr == KEntryAttArchive ) && !(attr & KEntryAttReadOnly) )
       
   620                 {
       
   621                 ret = iFs.Replace( aTempFileName, aFullPathName );
       
   622                 LOGGER_WRITE_1( "CSConFsHandler::SaveFileObjectL() iFs.Replace ret %d", ret);
       
   623                 //Set correct time stamp
       
   624                 if( ret == KErrNone )
       
   625                     {
       
   626                     ret = iFs.SetModified( aFullPathName, aTime );
       
   627                     ret = KErrNone;
       
   628                     }
       
   629                 else
       
   630                     {
       
   631                     iFs.Delete( aTempFileName );
       
   632                     }
       
   633                 }
       
   634             else
       
   635                 {
       
   636                 LOGGER_WRITE( "CSConFsHandler::SaveFileObjectL() no permissions" );
       
   637                 iFs.Delete( aTempFileName );
       
   638                 ret = KErrAccessDenied;
       
   639                 }
       
   640             }   
       
   641         }
       
   642     else
       
   643         {
       
   644         iFs.Delete( aTempFileName );
       
   645         }
       
   646     
       
   647     LOGGER_WRITE_1( "CSConFsHandler::SaveFileObjectL() : returned %d", ret );
       
   648     return ret;
       
   649     }
       
   650 
       
   651 // -----------------------------------------------------------------------------
       
   652 // CSConFsHandler::ValidateFileName( const TPtrC aName )
       
   653 // Validates the file name
       
   654 // -----------------------------------------------------------------------------
       
   655 //
       
   656 TInt CSConFsHandler::ValidateFileName( const TPtrC aName )
       
   657     {
       
   658     TInt ret( KErrNone );
       
   659 
       
   660     if( !iFs.IsValidName( aName ) )
       
   661         {
       
   662         ret = KErrBadName;
       
   663         }
       
   664 
       
   665     LOGGER_WRITE_1( "CSConFsHandler::ValidateFileName( const TPtrC aName ) : returned %d", ret  );
       
   666     return ret;
       
   667     }
       
   668 
       
   669 // -----------------------------------------------------------------------------
       
   670 // CSConFsHandler::CreateFolderL( const TPtrC aFolderPath )
       
   671 // Creates a folder
       
   672 // -----------------------------------------------------------------------------
       
   673 //
       
   674 TInt CSConFsHandler::CreateFolderL( const TPtrC aFolderPath )
       
   675     {
       
   676     TRACE_FUNC_ENTRY;
       
   677     
       
   678     TInt ret( KErrNone );
       
   679     
       
   680     if( aFolderPath.Length() == 0 )
       
   681         {
       
   682         return KErrArgument;
       
   683         }
       
   684     if( aFolderPath.Length() > KMaxPath )
       
   685         {
       
   686         return KErrAbort;
       
   687         }
       
   688     if( BaflUtils::PathExists( iFs, aFolderPath ) )
       
   689         {
       
   690         return KErrAlreadyExists;
       
   691         }
       
   692     // get restore mode
       
   693     TBool restoreInProgress( EFalse );
       
   694     RProperty backupProperty;
       
   695     TInt burState( 0 );
       
   696     TInt burErr = backupProperty.Get( KUidSystemCategory, conn::KUidBackupRestoreKey, burState ); 
       
   697     const conn::TBURPartType partType = 
       
   698         static_cast< conn::TBURPartType >( burState & conn::KBURPartTypeMask );
       
   699     if( burErr == KErrNone &&
       
   700      ( partType == conn::EBURRestorePartial || partType == conn::EBURRestoreFull ) )
       
   701         {
       
   702         // restore mode
       
   703         LOGGER_WRITE( "CSConFsHandler::CreateFolderL() restore mode" );
       
   704         restoreInProgress = ETrue;
       
   705         }
       
   706     
       
   707     // check permissions if not restore mode
       
   708     if ( !restoreInProgress )
       
   709         {
       
   710         if ( IsExludedPath( aFolderPath ) )
       
   711             {
       
   712             // user has no access to this folder
       
   713             LOGGER_WRITE( "CSConFsHandler::CreateFolderL() KErrAccessDenied" );
       
   714             return KErrAccessDenied;
       
   715             }
       
   716         
       
   717         // user can create whole folder path at once and we need to check that
       
   718         // user has permission to create folders. We need to find first folder that 
       
   719         // should be created, and see if parent folder has read-only permission.
       
   720         
       
   721         HBufC* fullPath = HBufC::NewLC( KMaxFileName );
       
   722         TPtr fullPathPtr = fullPath->Des();
       
   723     
       
   724         fullPathPtr.Copy(aFolderPath);
       
   725         TBool parentFound(EFalse);
       
   726         LOGGER_WRITE( "CSConFsHandler::CreateFolder() start loop" );
       
   727         TInt err(KErrNone);
       
   728         while (!parentFound && err==KErrNone)
       
   729             {
       
   730             err = GetParentFolder( fullPathPtr, fullPathPtr );
       
   731             if( err == KErrNone )
       
   732                 {
       
   733                 if (BaflUtils::PathExists( iFs, fullPathPtr ))
       
   734                     {
       
   735                     parentFound = ETrue;
       
   736                     TUint attr;
       
   737                     ret = iFs.Att( fullPathPtr, attr );
       
   738                     if ( ret == KErrNone &&  (attr & KEntryAttReadOnly || attr & KEntryAttSystem) )
       
   739                         {
       
   740                         // parent folder is read-only
       
   741                         LOGGER_WRITE( "CSConFsHandler::CreateFolderL() KErrAccessDenied" );
       
   742                         err = KErrAccessDenied;
       
   743                         }
       
   744                     }
       
   745                 }
       
   746             }
       
   747         
       
   748         CleanupStack::PopAndDestroy( fullPath );
       
   749         if ( err == KErrAccessDenied )
       
   750             {
       
   751             return KErrAccessDenied;
       
   752             }
       
   753         }
       
   754     
       
   755     ret = iFs.MkDirAll( aFolderPath );
       
   756     LOGGER_WRITE_1( "CSConFsHandler::CreateFolderL( const TPtrC aFolderPath ) : returned %d", ret );
       
   757     return ret;
       
   758     }
       
   759 
       
   760 // -----------------------------------------------------------------------------
       
   761 // CSConFsHandler::DeleteObjectL( const TPtrC aFullPath )
       
   762 // Removes a folder or a file
       
   763 // -----------------------------------------------------------------------------
       
   764 //
       
   765 TInt CSConFsHandler::DeleteObjectL( const TPtrC aFullPath )
       
   766     {
       
   767     TRACE_FUNC_ENTRY;
       
   768     LOGGER_WRITE_1("aFullPath: %S", &aFullPath);
       
   769     TInt ret( KErrNone );
       
   770     
       
   771     if( aFullPath.Length() == 0 )
       
   772         {
       
   773         return KErrArgument;
       
   774         }
       
   775     
       
   776     if( aFullPath.Length() > KMaxFileName )
       
   777         {
       
   778         return KErrAbort;
       
   779         }
       
   780     
       
   781     TUint attr;
       
   782     // get source folder permissions.
       
   783     ret = iFs.Att( BaflUtils::DriveAndPathFromFullName(aFullPath), attr );
       
   784     if ( ret == KErrNone && (attr & KEntryAttReadOnly || attr & KEntryAttSystem ) )
       
   785         {
       
   786         return KErrAccessDenied;
       
   787         }
       
   788     
       
   789     
       
   790     // Check file/folder permissions
       
   791     ret = iFs.Att( aFullPath, attr );
       
   792     if ( ret == KErrNone && ( attr & KEntryAttReadOnly || attr & KEntryAttSystem ) )
       
   793         {
       
   794         return KErrAccessDenied;
       
   795         }
       
   796     
       
   797     TBool isFolder(EFalse);
       
   798     ret = BaflUtils::IsFolder( iFs, aFullPath, isFolder );
       
   799     
       
   800     if( isFolder && ret == KErrNone )
       
   801         {
       
   802         HBufC* fullPath = HBufC::NewLC( KMaxFileName );
       
   803         TPtr fullPathPtr = fullPath->Des();
       
   804         
       
   805         fullPathPtr.Copy( aFullPath );
       
   806         //check if there is a slash at the end
       
   807         if( fullPathPtr.LocateReverse( KPathDelimiter ) != fullPathPtr.Length()-1 )
       
   808             {
       
   809             fullPathPtr.Append( KPathDelimiter );
       
   810             }
       
   811         
       
   812         if ( !IsFolderVisible( fullPathPtr ) )
       
   813             {
       
   814             LOGGER_WRITE("folder was not visible");
       
   815             CleanupStack::PopAndDestroy( fullPath );
       
   816             return KErrNotFound;
       
   817             }
       
   818         else if ( !IsFolderDeletable( fullPathPtr ) )
       
   819             {
       
   820             LOGGER_WRITE("folder was not deletable");
       
   821             CleanupStack::PopAndDestroy( fullPath );
       
   822             return KErrAccessDenied;
       
   823             }
       
   824         
       
   825         ret = iFs.RmDir( fullPathPtr );
       
   826         LOGGER_WRITE_1("iFs.RmDir err: %d", ret);
       
   827         //delete non-empty folder
       
   828         if( ret != KErrNone )
       
   829             {
       
   830             CFileMan* fileMan = CFileMan::NewL( iFs );
       
   831             CleanupStack::PushL( fileMan );
       
   832             // if directory contains read-only files, those files are not deleted
       
   833             // and KErrAccessDenied is returned.
       
   834             ret = fileMan->RmDir( fullPathPtr );
       
   835             LOGGER_WRITE_1("fileMan->RmDir err: %d", ret);
       
   836             CleanupStack::PopAndDestroy( fileMan ); //fileman
       
   837             }
       
   838         if( ret == KErrNone )
       
   839             {
       
   840             // check is delete operation really succesfully completed
       
   841             if( BaflUtils::PathExists( iFs, fullPathPtr ) )
       
   842                 {
       
   843                 // Folder still exist, return error
       
   844                 // KErrBadName mapped to precondition failed error.
       
   845                 ret = KErrBadName;
       
   846                 }
       
   847             }
       
   848         CleanupStack::PopAndDestroy( fullPath ); //fullPath
       
   849         }
       
   850     else if (ret == KErrNone)
       
   851         {
       
   852         // deleting file
       
   853         if ( IsExludedPath( aFullPath ) )
       
   854             {
       
   855             LOGGER_WRITE("Cannot delete from specified folder, return KErrAccessDenied");
       
   856             ret = KErrAccessDenied;
       
   857             }
       
   858         else
       
   859             {
       
   860             ret = iFs.Delete( aFullPath );
       
   861             }
       
   862         }
       
   863     
       
   864     
       
   865     
       
   866     LOGGER_WRITE_1( "CSConFsHandler::DeleteObjectL() : returned %d", ret );
       
   867     return ret;
       
   868     }
       
   869     
       
   870     
       
   871 // -----------------------------------------------------------------------------
       
   872 // CSConFsHandler::CopyOrMoveFile()
       
   873 // Copy or Move file/folder.
       
   874 // -----------------------------------------------------------------------------
       
   875 //
       
   876 void CSConFsHandler::DoCopyOrMoveFileL( const TDesC& aSource, const TDesC& aTarget,
       
   877                                  const TBool aUseCopyCommand)
       
   878     {
       
   879     TRACE_FUNC_ENTRY;
       
   880     TInt lastError(KErrNone);
       
   881     TBool isFolder;
       
   882     
       
   883     if( !iFs.IsValidName( aSource ) || !iFs.IsValidName( aTarget ) )
       
   884         {
       
   885         LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Not valid name" );
       
   886         User::Leave( KErrBadName );
       
   887         }
       
   888     
       
   889     // will leave if source (file/folder) does not exists.
       
   890     User::LeaveIfError( BaflUtils::IsFolder(iFs, aSource, isFolder ) );
       
   891     
       
   892     // Check source file/folder permissions
       
   893     TUint attr;
       
   894     User::LeaveIfError( iFs.Att( aSource, attr ) );
       
   895     if ( !aUseCopyCommand && (attr & KEntryAttReadOnly || attr & KEntryAttSystem ) )
       
   896         {
       
   897         LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : no permissions, abort" );
       
   898         User::Leave(  KErrAccessDenied );
       
   899         }
       
   900     
       
   901     HBufC* sourceFolderBuf = HBufC::NewLC( KMaxFileName );
       
   902     TPtr sourceFolder = sourceFolderBuf->Des();
       
   903     HBufC* targetFolderBuf = HBufC::NewLC( KMaxFileName );
       
   904     TPtr targetFolder = targetFolderBuf->Des();
       
   905     
       
   906     sourceFolder.Copy( BaflUtils::DriveAndPathFromFullName(aSource) );
       
   907     targetFolder.Copy( BaflUtils::DriveAndPathFromFullName(aTarget) );
       
   908     
       
   909     
       
   910     // check disk space
       
   911     TInt driveNumber;
       
   912     
       
   913     User::LeaveIfError( iFs.CharToDrive(targetFolder[0], driveNumber) );
       
   914     
       
   915     TVolumeInfo volumeInfo;
       
   916     TInt err = iFs.Volume(volumeInfo, driveNumber);
       
   917     if( err != KErrNone )
       
   918         {
       
   919         LOGGER_WRITE_1( "CSConFsHandler::DoCopyOrMoveFileL : iFs.Volume err %d", err );
       
   920         User::Leave( KErrNotFound );
       
   921         }
       
   922     
       
   923     if( !isFolder )
       
   924         {
       
   925         // file handling routines
       
   926         if( !IsFolderVisible( aSource ) )
       
   927             {
       
   928             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Current folder doesn't exist" );
       
   929             User::Leave( KErrNotFound );
       
   930             }
       
   931         else if( !aUseCopyCommand && IsFolderReadOnly( sourceFolder ) )
       
   932             {
       
   933             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : folder read only, abort" );
       
   934             User::Leave( KErrAccessDenied );
       
   935             }
       
   936             
       
   937         // source file is ok, check target
       
   938 
       
   939         if( !IsFolderVisible( targetFolder ) )
       
   940             {
       
   941             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Current target file/folder doesn't exist" );
       
   942             User::Leave( KErrNotFound );
       
   943             }
       
   944         else if( IsFolderReadOnly( targetFolder ) )
       
   945             {
       
   946             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : folder read only, abort" );
       
   947             User::Leave( KErrAccessDenied );
       
   948             }
       
   949         else
       
   950             {
       
   951             TBool diskFull( EFalse );
       
   952             TEntry fileEntry;
       
   953             User::LeaveIfError( iFs.Entry( aSource, fileEntry ) );
       
   954             if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, fileEntry.iSize, driveNumber ) )
       
   955                 {
       
   956                 diskFull = ETrue;
       
   957                 }
       
   958             
       
   959             // target ok, ready to copy or move
       
   960             CFileMan* fileMan=CFileMan::NewL(iFs);
       
   961             CleanupStack::PushL(fileMan);
       
   962             
       
   963             if( aUseCopyCommand )
       
   964                 {
       
   965                 if( diskFull )
       
   966                     {
       
   967                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : disk full" );
       
   968                     User::Leave( KErrDiskFull );
       
   969                     }
       
   970                 LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Copy file" );
       
   971                 User::LeaveIfError( fileMan->Copy( aSource, aTarget, 0 ) );
       
   972                 }
       
   973             else
       
   974                 {
       
   975                 if( aSource.Find( aTarget.Left(2) ) == KErrNone)
       
   976                     {
       
   977                     // same drive
       
   978                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Rename file" );
       
   979                     User::LeaveIfError( fileMan->Rename( aSource, aTarget, 0 ) );
       
   980                     }
       
   981                 else
       
   982                     {
       
   983                     // different drive
       
   984                     if( diskFull )
       
   985                         {
       
   986                         LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : disk full" );
       
   987                         User::Leave( KErrDiskFull );
       
   988                         }
       
   989                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : copy file" );
       
   990                     User::LeaveIfError( fileMan->Copy( aSource, aTarget, 0 ) );
       
   991 
       
   992                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : delete original" );
       
   993                     User::LeaveIfError( fileMan->Delete( aSource, 0 ) );
       
   994                     }
       
   995                 }
       
   996             CleanupStack::PopAndDestroy( fileMan ); // fileMan
       
   997             }
       
   998         }
       
   999     else
       
  1000         {
       
  1001         // set trailing backslash after the final directory name
       
  1002         HBufC* tmpSourceBuf = HBufC::NewLC( KMaxFileName );
       
  1003         TPtr tmpSourcePtr = tmpSourceBuf->Des();
       
  1004         HBufC* tmpTargetBuf = HBufC::NewLC( KMaxFileName );
       
  1005         TPtr tmpTargetPtr = tmpTargetBuf->Des();
       
  1006         
       
  1007         tmpSourcePtr.Copy( aSource );
       
  1008         if( tmpSourcePtr.LocateReverse( KPathDelimiter ) != tmpSourcePtr.Length()-1 )
       
  1009             {
       
  1010             tmpSourcePtr.Append( KPathDelimiter );
       
  1011             }
       
  1012 
       
  1013         tmpTargetPtr.Copy( aTarget );
       
  1014         if( tmpTargetPtr.LocateReverse( KPathDelimiter ) != tmpTargetPtr.Length()-1 )
       
  1015             {
       
  1016             tmpTargetPtr.Append( KPathDelimiter );
       
  1017             }
       
  1018         
       
  1019         if( !IsFolderVisible( tmpSourcePtr ) )
       
  1020             {
       
  1021             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Current folder doesn't exist" );
       
  1022             User::Leave( KErrNotFound );
       
  1023             }
       
  1024         else if( !aUseCopyCommand && IsFolderReadOnly( sourceFolder ) )
       
  1025             {
       
  1026             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : sourcefolder read only, abort" );
       
  1027             User::Leave( KErrAccessDenied );
       
  1028             }
       
  1029         else if( !aUseCopyCommand && !IsFolderDeletable( tmpSourcePtr ) )
       
  1030             {
       
  1031             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : source folder read only, abort" );
       
  1032             User::Leave( KErrAccessDenied );
       
  1033             }
       
  1034         else if( !IsFolderVisible( targetFolder ) )
       
  1035             {
       
  1036             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Current target base-folder doesn't exist" );
       
  1037             User::Leave( KErrNotFound );
       
  1038             }
       
  1039         else if( IsFolderReadOnly( targetFolder) )
       
  1040             {
       
  1041             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : target folder read only, abort" );
       
  1042             User::Leave( KErrAccessDenied );
       
  1043             }
       
  1044         else if( BaflUtils::FolderExists(iFs, tmpTargetPtr ) )
       
  1045             {
       
  1046             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : target folder exist, abort" );
       
  1047             User::Leave( KErrAlreadyExists );
       
  1048             }
       
  1049         else if ( tmpTargetPtr.Find( tmpSourcePtr ) == KErrNone )
       
  1050             {
       
  1051             LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : conflict, abort" );
       
  1052             User::Leave( KErrInUse );
       
  1053             }
       
  1054         else
       
  1055             {
       
  1056             // is folder empty
       
  1057             TBool emptyFolder( ETrue );
       
  1058             CDir *entryList = 0;
       
  1059             CDir *dirList = 0;
       
  1060 
       
  1061             User::LeaveIfError( iFs.GetDir( tmpSourcePtr, KEntryAttNormal, EDirsAnyOrder, 
       
  1062             entryList, dirList ) );
       
  1063             if( entryList->Count() > 0 || dirList->Count() > 0 )
       
  1064                 {
       
  1065                 emptyFolder = EFalse;
       
  1066                 }
       
  1067                 
       
  1068             delete entryList;
       
  1069             delete dirList;
       
  1070             
       
  1071             // get folder size
       
  1072             TBool diskFull( EFalse );
       
  1073             if( !emptyFolder )
       
  1074                 {
       
  1075                 TInt64 folderSize = GetFolderSizeL(tmpSourcePtr);
       
  1076                 LOGGER_WRITE_1( "CSConFsHandler::DoCopyOrMoveFileL : folderSize: %d", folderSize );
       
  1077                 if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, folderSize, driveNumber ) )
       
  1078                     {
       
  1079                     diskFull = ETrue;
       
  1080                     }
       
  1081                 }
       
  1082             
       
  1083             
       
  1084             CFileMan* fileMan = CFileMan::NewL(iFs);
       
  1085             CleanupStack::PushL(fileMan);
       
  1086     
       
  1087             // ready to copy or move
       
  1088             if( aUseCopyCommand )
       
  1089                 {
       
  1090                 LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Copy folder" );
       
  1091                 if( emptyFolder )
       
  1092                     {
       
  1093                     // folder is empty, can't copy folder. Create new empty folder.
       
  1094                     User::LeaveIfError( iFs.MkDir( tmpTargetPtr ) );
       
  1095                     }
       
  1096                 else
       
  1097                     {
       
  1098                     // folder not empty
       
  1099                     if( diskFull )
       
  1100                         {
       
  1101                         LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : disk full" );
       
  1102                         User::Leave( KErrDiskFull );
       
  1103                         }
       
  1104                     
       
  1105                     User::LeaveIfError( 
       
  1106                      fileMan->Copy( aSource, aTarget, CFileMan::ERecurse ) ); 
       
  1107                     if( lastError == KErrNone )
       
  1108                         {
       
  1109                         lastError = fileMan->GetLastError();
       
  1110                         }
       
  1111                     }
       
  1112                 
       
  1113                 TUint attr;
       
  1114                 TInt err = iFs.Att( tmpSourcePtr, attr );
       
  1115                 if ( err == KErrNone && (attr & KEntryAttReadOnly ) )
       
  1116                     {
       
  1117                     //set read only attribute
       
  1118                     err = iFs.SetAtt( tmpTargetPtr, KEntryAttReadOnly, KEntryAttArchive );
       
  1119                     if (err != KErrNone)
       
  1120                         {
       
  1121                         // can't set attribute
       
  1122                         LOGGER_WRITE_1( "CSConFsHandler::DoCopyOrMoveFileL : iFs.SetAtt err: %d",err );
       
  1123                         }
       
  1124                     }
       
  1125                 }
       
  1126             else
       
  1127                 {
       
  1128                 if( aSource.FindF( aTarget.Left(2) ) == KErrNone)
       
  1129                     {
       
  1130                     // same drive
       
  1131                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Rename folder" );
       
  1132                     User::LeaveIfError( fileMan->Rename( aSource, aTarget, 0 ) );
       
  1133                     }
       
  1134                 else
       
  1135                     {
       
  1136                     // different drive
       
  1137                     if( diskFull )
       
  1138                         {
       
  1139                         LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : disk full" );
       
  1140                         User::Leave( KErrDiskFull );
       
  1141                         }
       
  1142                     
       
  1143                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : Copy folder" );
       
  1144                     if( emptyFolder )
       
  1145                         {
       
  1146                         // folder is empty, can't copy empty folder. Create empty folder.
       
  1147                         User::LeaveIfError( iFs.MkDir( tmpTargetPtr ) );
       
  1148                         }
       
  1149                     else
       
  1150                         {
       
  1151                         User::LeaveIfError( 
       
  1152                          fileMan->Copy( aSource, aTarget, CFileMan::ERecurse ) );
       
  1153                         }
       
  1154                     // copy completed, delete original folder.
       
  1155                     LOGGER_WRITE( "CSConFsHandler::DoCopyOrMoveFileL : delete original" );
       
  1156                     User::LeaveIfError( fileMan->RmDir( tmpSourcePtr ) );
       
  1157                     // check was delete operation succesfully completed
       
  1158                     if( BaflUtils::PathExists( iFs, tmpSourcePtr ) )
       
  1159                         {
       
  1160                         // Folder still exist, return error
       
  1161                         lastError = KErrAccessDenied;
       
  1162                         }
       
  1163                     }
       
  1164                 }
       
  1165             CleanupStack::PopAndDestroy(fileMan); // fileMan
       
  1166             }
       
  1167         CleanupStack::PopAndDestroy(tmpTargetBuf); // tmpTargetBuf
       
  1168         CleanupStack::PopAndDestroy(tmpSourceBuf); // tmpSourceBuf
       
  1169         }
       
  1170     
       
  1171     CleanupStack::PopAndDestroy( targetFolderBuf );
       
  1172     CleanupStack::PopAndDestroy( sourceFolderBuf );
       
  1173     
       
  1174     if( lastError != KErrNone )
       
  1175         {
       
  1176         LOGGER_WRITE_1( "CSConFTP::DoCopyOrMoveFileL() : lastError: %d", lastError );
       
  1177         User::Leave( lastError );
       
  1178         }
       
  1179     
       
  1180     TRACE_FUNC_EXIT;
       
  1181     }
       
  1182 
       
  1183 // -----------------------------------------------------------------------------
       
  1184 // CSConFsHandler::IsFolderVisible()
       
  1185 // Check if folder can be showed
       
  1186 // -----------------------------------------------------------------------------
       
  1187 //
       
  1188 TBool CSConFsHandler::IsFolderVisible( const TDesC& aFullPath )
       
  1189     {
       
  1190     TRACE_FUNC_ENTRY;
       
  1191     LOGGER_WRITE_1( "aFullPath: %S", &aFullPath );
       
  1192     TBool visible(EFalse);
       
  1193     
       
  1194     if( !BaflUtils::FolderExists( iFs, aFullPath ) )
       
  1195         {
       
  1196         LOGGER_WRITE( "CSConFsHandler::IsFolderVisible() notExist ret EFalse" );
       
  1197         return EFalse;
       
  1198         }
       
  1199     
       
  1200     // don't show if folder is exluded
       
  1201     visible = !IsExludedPath( aFullPath );
       
  1202     LOGGER_WRITE_1( "CSConFsHandler::IsFolderVisible() : end, ret %d", (TInt)visible );
       
  1203     return visible;
       
  1204     }
       
  1205     
       
  1206 // -----------------------------------------------------------------------------
       
  1207 // CSConFsHandler::IsFolderReadOnly()
       
  1208 // Check if folder is read only (system folder)
       
  1209 // -----------------------------------------------------------------------------
       
  1210 //
       
  1211 TBool CSConFsHandler::IsFolderReadOnly( const TDesC& aFullPath ) const
       
  1212     {
       
  1213     TRACE_FUNC_ENTRY;
       
  1214     LOGGER_WRITE_1( "aFullPath: %S", &aFullPath );
       
  1215     TBool readOnly(EFalse);
       
  1216     
       
  1217     // C-drive root is read only, but there are some exceptions
       
  1218     
       
  1219     readOnly = IsExludedPath( aFullPath );
       
  1220     
       
  1221     if ( aFullPath.CompareF( K_C_ROOT ) == 0 )
       
  1222         {
       
  1223         // Root of C-drive is read-only
       
  1224 		LOGGER_WRITE("Root of C-Drive is read only");
       
  1225         readOnly = ETrue;
       
  1226         }
       
  1227     
       
  1228     // Check folder permissions
       
  1229     TUint attr;
       
  1230     TInt err = iFs.Att( aFullPath, attr );
       
  1231     LOGGER_WRITE_1("iFs.Att err: %d", err);
       
  1232     if ( err == KErrNone && (attr & KEntryAttReadOnly || attr & KEntryAttSystem ) )
       
  1233         {
       
  1234         LOGGER_WRITE("Folder is readonly");
       
  1235         readOnly = ETrue;
       
  1236         }
       
  1237     
       
  1238     LOGGER_WRITE_1( "CSConFsHandler::IsFolderReadOnly() : end, ret %d", (TInt)readOnly );
       
  1239     return readOnly;
       
  1240     }
       
  1241     
       
  1242 // -----------------------------------------------------------------------------
       
  1243 // CSConFsHandler::IsFolderDeletable()
       
  1244 // Check if folder can be deleted. Localized folders cannot delete.
       
  1245 // In C-drive folders can be deleted only from under C:\\Data\\
       
  1246 // -----------------------------------------------------------------------------
       
  1247 //
       
  1248 TBool CSConFsHandler::IsFolderDeletable( const TDesC& aFullPath ) const
       
  1249     {
       
  1250     TRACE_FUNC_ENTRY;
       
  1251     LOGGER_WRITE_1( "aFullPath: %S", &aFullPath );
       
  1252     TBool deleteAllowed(ETrue);
       
  1253     TPath PhoneMem( PathInfo::PhoneMemoryRootPath() ); // "C:\\Data\\"
       
  1254 
       
  1255     if ( aFullPath.CompareF( PhoneMem ) == 0 )
       
  1256         {
       
  1257         // C:\\Data\\
       
  1258         LOGGER_WRITE( "CSConFsHandler::IsFolderDeletable() datapath, ret EFalse" );
       
  1259         deleteAllowed = EFalse;
       
  1260         }
       
  1261     else if ( IsExludedPath( aFullPath ) )
       
  1262         {
       
  1263         deleteAllowed = EFalse;
       
  1264         }
       
  1265     
       
  1266     iLocalizer->SetFullPath( aFullPath );
       
  1267     if ( iLocalizer->IsLocalized() ) // delete disabled
       
  1268         {
       
  1269         // Cannot delete localized paths (default paths)
       
  1270         deleteAllowed = EFalse;
       
  1271         }
       
  1272     
       
  1273     LOGGER_WRITE_1( "CSConFsHandler::IsFolderDeletable() : end, ret %d", (TInt)deleteAllowed );
       
  1274     return deleteAllowed;
       
  1275     }
       
  1276 
       
  1277 // -----------------------------------------------------------------------------
       
  1278 // CSConFsHandler::GetParentFolder( const TPtrC aFullPath, TDes& aParent)
       
  1279 // Get parent folder
       
  1280 // -----------------------------------------------------------------------------
       
  1281 //
       
  1282 TInt CSConFsHandler::GetParentFolder( const TDesC& aFullPath, TDes& aParent)
       
  1283     {
       
  1284     TRACE_FUNC_ENTRY;
       
  1285     if (aFullPath.LocateReverse(KPathDelimiter) == aFullPath.Length()-1 )
       
  1286         {
       
  1287         // remove backlash
       
  1288         aParent.Copy(aFullPath.Left(aFullPath.Length()-1));
       
  1289         }
       
  1290     else
       
  1291         {
       
  1292         aParent.Copy(aFullPath);
       
  1293         }
       
  1294     
       
  1295     TInt pos = aParent.LocateReverse(KPathDelimiter);
       
  1296     if (pos != KErrNotFound)
       
  1297         {
       
  1298         aParent.Copy(aParent.Left(pos+1));
       
  1299         LOGGER_WRITE( "CSConFsHandler::GetParentFolder : end KErrNone" );
       
  1300         return KErrNone;
       
  1301         }
       
  1302     else
       
  1303         {
       
  1304         LOGGER_WRITE_1( "CSConFsHandler::GetParentFolder : end err %d", pos);
       
  1305         return pos;
       
  1306         }
       
  1307     }
       
  1308     
       
  1309 // -----------------------------------------------------------------------------
       
  1310 // CSConFsHandler::CSConFsHandler()
       
  1311 // Constructor
       
  1312 // -----------------------------------------------------------------------------
       
  1313 //
       
  1314 CSConFsHandler::CSConFsHandler( RFs& aFs ) : iFs(aFs)
       
  1315     {
       
  1316     TRACE_FUNC;
       
  1317     }
       
  1318 
       
  1319 // -----------------------------------------------------------------------------
       
  1320 // CSConFsHandler::ConstructL()
       
  1321 // Initializes member data
       
  1322 // -----------------------------------------------------------------------------
       
  1323 //
       
  1324 void CSConFsHandler::ConstructL()
       
  1325     {
       
  1326     TRACE_FUNC_ENTRY;
       
  1327     iLocalizer = CDirectoryLocalizer::NewL();
       
  1328     
       
  1329     GetDataPathNameL();
       
  1330     TRACE_FUNC_EXIT;
       
  1331     }
       
  1332     
       
  1333 // -----------------------------------------------------------------------------
       
  1334 // CSConFsHandler::GetDataPathNameL()
       
  1335 // Updates the name of the data folder from a localized string
       
  1336 // -----------------------------------------------------------------------------
       
  1337 //
       
  1338 void CSConFsHandler::GetDataPathNameL()
       
  1339     {
       
  1340     TRACE_FUNC_ENTRY;
       
  1341     //Read memory string and convert it
       
  1342     TFileName file( KSConResourceName );
       
  1343     
       
  1344     BaflUtils::NearestLanguageFile( iFs, file );
       
  1345        
       
  1346     CStringResourceReader* reader = CStringResourceReader::NewL( file );
       
  1347     CleanupStack::PushL( reader );
       
  1348     
       
  1349     CnvUtfConverter::ConvertFromUnicodeToUtf8( iDataPathName, 
       
  1350         reader->ReadResourceString( R_SECON_DATA_FOLDER ) );
       
  1351     
       
  1352     CleanupStack::PopAndDestroy( reader );
       
  1353     TRACE_FUNC_EXIT;
       
  1354     }
       
  1355     
       
  1356 // -----------------------------------------------------------------------------
       
  1357 // CSConFsHandler::GetFolderSizeL()
       
  1358 // Get folder (and subfolders) size
       
  1359 // -----------------------------------------------------------------------------
       
  1360 //
       
  1361 TInt64 CSConFsHandler::GetFolderSizeL( const TDesC& aFullPath )
       
  1362     {
       
  1363     TRACE_FUNC_ENTRY;
       
  1364     LOGGER_WRITE_1( "aFullPath: %S", &aFullPath );
       
  1365     TInt64 totalSize(0);
       
  1366     CDir* dir( NULL );
       
  1367     
       
  1368     User::LeaveIfError( iFs.GetDir(aFullPath, KEntryAttMatchMask, ESortByName, dir) );
       
  1369     CleanupStack::PushL(dir);
       
  1370     for( TInt i=0; i<dir->Count(); ++i )
       
  1371         {
       
  1372         TEntry entry = (*dir)[i];
       
  1373         if( !entry.IsDir() )
       
  1374             {
       
  1375             // file
       
  1376             totalSize += entry.iSize;
       
  1377             }
       
  1378         else
       
  1379             {
       
  1380             // folder
       
  1381             HBufC* subfolderBuf = HBufC::NewLC( KMaxPath );
       
  1382             TPtr subfolderPtr = subfolderBuf->Des();
       
  1383             
       
  1384             subfolderPtr.Copy(aFullPath);
       
  1385             subfolderPtr.Append(entry.iName);
       
  1386             subfolderPtr.Append( KPathDelimiter );
       
  1387             
       
  1388             LOGGER_WRITE( "CSConFsHandler::GetFolderSizeL() : get subfolder size" );
       
  1389             totalSize += GetFolderSizeL( subfolderPtr );
       
  1390             CleanupStack::PopAndDestroy( subfolderBuf );
       
  1391             }
       
  1392         }
       
  1393     CleanupStack::PopAndDestroy(dir);
       
  1394     TRACE_FUNC_EXIT;
       
  1395     return totalSize;
       
  1396     }
       
  1397 
       
  1398 // -----------------------------------------------------------------------------
       
  1399 // CSConFsHandler::ReplaceSpecialChars()
       
  1400 // Replace special characters to xml compliant
       
  1401 // -----------------------------------------------------------------------------
       
  1402 //
       
  1403 void CSConFsHandler::ReplaceSpecialChars( TDes8& aDes )
       
  1404     {
       
  1405     for( TInt i = 0; i < aDes.Length(); i++ )
       
  1406         {
       
  1407         switch( aDes[i] )
       
  1408             {
       
  1409             case '&':
       
  1410                 aDes.Delete( i, 1 );
       
  1411                 aDes.Insert( i, KReplace1 );
       
  1412                 break;
       
  1413             case '<':
       
  1414                 aDes.Delete( i, 1 );
       
  1415                 aDes.Insert( i, KReplace2 );
       
  1416                 break;
       
  1417             case '>':
       
  1418                 aDes.Delete( i, 1 );
       
  1419                 aDes.Insert( i, KReplace3 );
       
  1420                 break;
       
  1421             case '"':
       
  1422                 aDes.Delete( i, 1 );
       
  1423                 aDes.Insert( i, KReplace4 );
       
  1424                 break;
       
  1425             case '\'':
       
  1426                 aDes.Delete( i, 1 );
       
  1427                 aDes.Insert( i, KReplace5 );
       
  1428                 break;
       
  1429             default:
       
  1430                 break;
       
  1431             }
       
  1432         }
       
  1433     }
       
  1434 
       
  1435 // -----------------------------------------------------------------------------
       
  1436 // CSConFsHandler::IsExludedPath()
       
  1437 // Some folders are exluded from user access
       
  1438 // -----------------------------------------------------------------------------
       
  1439 //
       
  1440 TBool CSConFsHandler::IsExludedPath( const TDesC& aFullPath ) const
       
  1441     {
       
  1442     TInt exluded(ETrue);
       
  1443     LOGGER_WRITE_1( "aFullPath: %S", &aFullPath );
       
  1444     TPath mmcRoot( PathInfo::MemoryCardRootPath() );
       
  1445     if ( aFullPath.Length() == KDriveLength )
       
  1446         {
       
  1447         // root paths not exluded
       
  1448         exluded = EFalse;
       
  1449         }
       
  1450     else if ( aFullPath.FindF( K_C_ROOT ) == 0 )
       
  1451         {
       
  1452         if ( aFullPath.FindF( PathInfo::PhoneMemoryRootPath() ) == 0 )
       
  1453             {
       
  1454             // C:\\Data is visible, not exluded
       
  1455             exluded = EFalse;
       
  1456             }
       
  1457 #ifdef SCON_SHOW_LOGS_FOLDER
       
  1458         else if ( aFullPath.FindF( KSConLogsPath ) == KDriveLength )
       
  1459             {
       
  1460             // C:\\Logs is visible, not exluded
       
  1461             exluded = EFalse;
       
  1462             }
       
  1463 #endif
       
  1464         else {
       
  1465             // other paths on C-drive are exluded
       
  1466             LOGGER_WRITE_1("CSConFsHandler::IsExludedPath() Path '%S' is not visible to user", &aFullPath);
       
  1467             exluded = ETrue;
       
  1468             }
       
  1469         }
       
  1470     // hide undesired folders from E: root level
       
  1471     else if( aFullPath.CompareF( mmcRoot ) == 0 )
       
  1472         {
       
  1473         if ( aFullPath.FindF( KSConSys ) == KDriveLength )
       
  1474             {
       
  1475             LOGGER_WRITE( "hiding mmcRoot KSConSys" );
       
  1476             exluded = ETrue;
       
  1477             }
       
  1478         else if( aFullPath.FindF( KSConResource ) == KDriveLength )
       
  1479             {
       
  1480             LOGGER_WRITE( "hiding mmcRoot KSConResource" );
       
  1481             exluded = ETrue;
       
  1482             }
       
  1483         else if( aFullPath.FindF( KSConPrivate ) == KDriveLength )
       
  1484             {
       
  1485             LOGGER_WRITE( "mmcRoot KSConPrivate" );
       
  1486             exluded = ETrue;
       
  1487             }
       
  1488         else if( aFullPath.FindF( KSConSystem ) == KDriveLength )
       
  1489             {
       
  1490             LOGGER_WRITE( "hiding mmcRoot KSConSystem" );
       
  1491             exluded = ETrue;
       
  1492             }
       
  1493         }
       
  1494     // end of E: root level handling
       
  1495     else
       
  1496         {
       
  1497         // don't exlude paths from other drives
       
  1498         exluded = EFalse;
       
  1499         }
       
  1500     return exluded;
       
  1501     }
       
  1502 
       
  1503 // End of file
       
  1504 
       
  1505