multimediacommsengine/tsrc/testdriver/testclient/filehandler/src/CTcFileHandlerSession.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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:  Implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CTcFileHandlerSession.h"
       
    19 #include "CTcFileHandlerServer.h"
       
    20 #include "FileHandlerConstants.h"
       
    21 
       
    22 #include <f32file.h>
       
    23 
       
    24 CTcFileHandlerSession* CTcFileHandlerSession::NewL( 
       
    25     CTcFileHandlerServer* aServer )
       
    26 	{
       
    27 	CTcFileHandlerSession* self = new( ELeave ) CTcFileHandlerSession;
       
    28 
       
    29 	CleanupStack::PushL( self );
       
    30 	self->ConstructL( aServer );
       
    31 	CleanupStack::Pop();
       
    32 
       
    33 	return self;
       
    34 	}
       
    35 
       
    36 CTcFileHandlerSession::~CTcFileHandlerSession()
       
    37 	{
       
    38 	iFileHandlerServer->SessionRemoved();
       
    39 	}
       
    40 
       
    41 CTcFileHandlerSession::CTcFileHandlerSession()
       
    42 	{
       
    43 	}
       
    44 
       
    45 void CTcFileHandlerSession::ConstructL( CTcFileHandlerServer* aServer )
       
    46 	{
       
    47 	iFileHandlerServer = aServer;
       
    48 	iFileHandlerServer->SessionAdded();
       
    49 	}
       
    50 
       
    51 void CTcFileHandlerSession::ServiceL( const RMessage2& aMessage )
       
    52 	{
       
    53 	// Any leaves from this function will be catched by the Active Scheduler
       
    54 	// and forwarded to CTcFileHandler::RunError().
       
    55 
       
    56 	// Check for session related requests
       
    57 	TInt status( KErrNone );
       
    58 	switch( aMessage.Function() )
       
    59 		{
       
    60 		case ECloseSession:
       
    61 			{
       
    62 			// Session destruction is handled
       
    63 			// by the client-server framework.
       
    64 			break;
       
    65 			}
       
    66 		case EConfigAndStart:
       
    67 			{
       
    68 			// Nothing to do actually
       
    69 			break;
       
    70 			}
       
    71 		case ECreateFile:
       
    72 			{
       
    73 			DoCreateFileL( aMessage );	
       
    74 			break;
       
    75 			}
       
    76 		case ECopyFile:
       
    77 			{
       
    78 			DoCopyFileL( aMessage );	
       
    79 			break;
       
    80 			}
       
    81 		case EDeleteFile:
       
    82 			{
       
    83 			DoDeleteFileL( aMessage );
       
    84 			break;
       
    85 			}
       
    86 			
       
    87 		default:
       
    88 			{
       
    89 			// Unknown request
       
    90 			status = KErrUnknown;
       
    91 			}
       
    92 		}
       
    93 
       
    94 	aMessage.Complete( status );
       
    95 	}
       
    96 	
       
    97 void CTcFileHandlerSession::DoCreateFileL( const RMessage2& aMessage ) const
       
    98     {
       
    99     TFileName destinationPath;
       
   100     ReadFileNameL( 0, aMessage, destinationPath );
       
   101    	
       
   102    	HBufC8* data = ReadLC( 1, aMessage );
       
   103    	
       
   104    	RFs fs;
       
   105 	User::LeaveIfError( fs.Connect() );
       
   106 	CleanupClosePushL( fs );
       
   107 	
       
   108 	// Make sure path exists, ignore errors since path might exist already
       
   109 	fs.MkDirAll( destinationPath );	
       
   110 
       
   111 	RFile out;
       
   112 	User::LeaveIfError( out.Replace( fs, destinationPath, 
       
   113 						EFileWrite ) );
       
   114 	CleanupClosePushL( out );
       
   115 	User::LeaveIfError( out.Write( *data ) );
       
   116 	
       
   117 	CleanupStack::PopAndDestroy( 3 );	// out, fs, data
       
   118     }
       
   119 	
       
   120 void CTcFileHandlerSession::DoCopyFileL( const RMessage2& aMessage ) const
       
   121     {
       
   122     TFileName sourcePath;
       
   123     ReadFileNameL( 0, aMessage, sourcePath );
       
   124     
       
   125     TFileName destinationPath;
       
   126     ReadFileNameL( 1, aMessage, destinationPath );
       
   127    	
       
   128     RFs fs;
       
   129 	User::LeaveIfError( fs.Connect() );
       
   130 	CleanupClosePushL( fs );
       
   131 	
       
   132 	CFileMan* fileMan = CFileMan::NewL(fs);
       
   133 	CleanupStack::PushL(fileMan); 
       
   134 	
       
   135 	// Make sure path exists, ignore errors since path might exist already
       
   136 	fs.MkDirAll( destinationPath );	
       
   137 	
       
   138 	// Make the destination file writeable, ignore errors since most probably
       
   139 	// file doesn't exist yet
       
   140     fileMan->Attribs( destinationPath, 0, KEntryAttReadOnly, TTime( 0 ), 0 );
       
   141     
       
   142 	User::LeaveIfError( fileMan->Copy( sourcePath, destinationPath ) );
       
   143     
       
   144 	CleanupStack::PopAndDestroy( 2 ); // fileMan, fs
       
   145     }
       
   146     
       
   147 void CTcFileHandlerSession::DoDeleteFileL( const RMessage2& aMessage ) const
       
   148     {
       
   149     TFileName destinationPath;
       
   150     ReadFileNameL( 0, aMessage, destinationPath );
       
   151     
       
   152     // Connect to file server
       
   153 	RFs fs;
       
   154 	User::LeaveIfError( fs.Connect() );
       
   155 	CleanupClosePushL( fs );
       
   156 
       
   157 	// Clear read-only and system flags in order to be able to delete the file
       
   158 	// Ignore errors.
       
   159 	fs.SetAtt( destinationPath, 0, KEntryAttReadOnly | KEntryAttSystem );
       
   160 
       
   161 	// Try deleting the file, report errors (typically KErrNotFound)
       
   162 	User::LeaveIfError( fs.Delete( destinationPath ) );
       
   163 
       
   164 	// Close file server session
       
   165 	CleanupStack::PopAndDestroy();	// fs
       
   166     }
       
   167     
       
   168 void CTcFileHandlerSession::ReadFileNameL( 
       
   169     TInt aItcArgIndex,
       
   170     const RMessage2& aMessage,
       
   171     TFileName& aFileName ) const
       
   172     {
       
   173    	HBufC8* fileName = ReadLC( aItcArgIndex, aMessage );
       
   174    	
       
   175    	__ASSERT_ALWAYS( fileName->Des().Length() <= KMaxFileName, 
       
   176    	                 User::Leave( KErrArgument ) );
       
   177    	aFileName.Copy( *fileName );
       
   178    	
       
   179    	CleanupStack::PopAndDestroy(); // fileName
       
   180     }
       
   181     
       
   182 HBufC8* CTcFileHandlerSession::ReadLC( 
       
   183     TInt aItcArgIndex,
       
   184     const RMessage2& aMessage ) const
       
   185     {
       
   186     TInt length = aMessage.GetDesLength( aItcArgIndex );
       
   187     if ( length < 0 )
       
   188         {
       
   189         User::Leave( KErrBadDescriptor );
       
   190         }
       
   191     HBufC8* buf = HBufC8::NewLC( length );
       
   192 	if ( length > 0 )
       
   193 		{
       
   194 		TPtr8 bufPtr( buf->Des() );
       
   195 		aMessage.ReadL( aItcArgIndex, bufPtr );
       
   196 		}
       
   197     return buf;
       
   198     }