multimediacommsengine/tsrc/testdriver/siptester/src/TCmdTransferFile.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 <f32file.h>
       
    19 #include <bautils.h>
       
    20 
       
    21 #include "CTcFileHandler.h"
       
    22 
       
    23 #include "TCmdTransferFile.h"
       
    24 #include "SIPConstants.h"
       
    25 #include "CTcSIPContext.h"
       
    26 
       
    27 // Literals used for action name
       
    28 _LIT8( KWrite, "write" );
       
    29 _LIT8( KDelete, "delete" );
       
    30 _LIT8( KExists, "exists" );
       
    31 
       
    32 
       
    33 /**
       
    34  * INPUT:
       
    35  *   Headers:		-
       
    36  *   Parameters:	ActionName, *SrcPath, DestinationPath, *Data
       
    37  *   IDs:			-
       
    38  *
       
    39  * OUTPUT:
       
    40  *   Parameters:	StatusCode
       
    41  *   IDs:			-
       
    42  */
       
    43 void TCmdTransferFile::ExecuteL()
       
    44 	{
       
    45 	// -- Setup ---------------------------------------------------------------
       
    46 
       
    47     // Get action name for operation (e.g. write&read)
       
    48     TPtrC8 actionName = ExtractTextL( KParamActionName );
       
    49 
       
    50     // Get parameter destination path
       
    51     TPtrC8 sourcePath = ExtractTextL( KParamSourcePath, EFalse );
       
    52     
       
    53 	// Get parameter destination path
       
    54     TPtrC8 destinationPath = ExtractTextL( KParamDestinationPath );
       
    55     
       
    56 	// Get actual data. Data parameter is having data in binary format.
       
    57 	TPtrC8 data = ExtractTextL( KParamData, EFalse );
       
    58 	
       
    59 	// Extract content. Content parameter is having textual data.
       
    60 	HBufC8* content = ExtractHBufLC( KParamContent );
       
    61 	if ( !content )
       
    62 	    {
       
    63 	    // Create dummy zero length content
       
    64 	    content = HBufC8::NewLC( 0 );
       
    65 	    }
       
    66 	    
       
    67 
       
    68 	// -- Execution -----------------------------------------------------------
       
    69  
       
    70     // Compare the operation and do it
       
    71     if( !actionName.CompareC( KWrite ) )
       
    72 		{
       
    73 		if ( sourcePath.Compare( KNullDesC8 ) != 0 )
       
    74 		    {
       
    75 		    // If source path was defined copy file from sourcepath
       
    76 		    // to destinationpath
       
    77 		    DoCopyL( sourcePath, destinationPath );
       
    78 		    }
       
    79 		else if ( content->Length() != 0 )
       
    80 		    {
       
    81 		     // Otherwise write received text data to destinationpath
       
    82 		    TPtrC8 ptr(content->Right(content->Length()));
       
    83 		    DoWriteL( destinationPath, ptr );   
       
    84 		    }		    
       
    85 		else if ( data.Compare( KNullDesC8 ) != 0 )
       
    86 		    {
       
    87 		    // Otherwise write received data to destinationpath
       
    88 		    DoWriteL( destinationPath, data );   
       
    89 		    }
       
    90 		else
       
    91 		    {
       
    92 		    User::Leave( KErrNotFound );
       
    93 		    }
       
    94 		}
       
    95     else if( !actionName.CompareC( KExists ) )
       
    96     	{
       
    97     	TFileName filename;
       
    98     	filename.Copy(destinationPath);
       
    99     	if( CheckFileExists(filename) )
       
   100 			{
       
   101 			AddBooleanResponseL(_L8("FileExists"), ETrue);
       
   102 			}
       
   103 		else
       
   104 			{
       
   105 			AddBooleanResponseL(_L8("FileExists"), EFalse);
       
   106 			}
       
   107     	}
       
   108     else if( !actionName.CompareC( KDelete ) )
       
   109     	{   
       
   110     	TParse parse;
       
   111     	TFileName name;
       
   112     	name.Copy(destinationPath);
       
   113     	parse.Set(name, NULL, NULL);
       
   114     	if( parse.DrivePresent() && parse.PathPresent() && parse.NameOrExtPresent() )
       
   115     		{
       
   116     		iContext.FileHandler().DeleteFileL(destinationPath);
       
   117     		}
       
   118     	else
       
   119     		{
       
   120     		User::Leave( KErrArgument);
       
   121     		}
       
   122     	}
       
   123     else { User::Leave( KErrNotSupported ); }
       
   124     
       
   125     // -- Response creation ---------------------------------------------------
       
   126 	
       
   127     AddIntegerResponseL( KResponseActionReturnCode, KSIPReasonOk );
       
   128     CleanupStack::Pop(1);
       
   129     }
       
   130 
       
   131 
       
   132 TBool TCmdTransferFile::Match( const TTcIdentifier& aId )
       
   133 	{
       
   134 	return TTcSIPCommandBase::Match( aId, _L8("fileXfer") );
       
   135 	}
       
   136 
       
   137 TTcCommandBase* TCmdTransferFile::CreateL( MTcTestContext& aContext )
       
   138 	{
       
   139 	return new( ELeave ) TCmdTransferFile( aContext );
       
   140 	}
       
   141 
       
   142 void TCmdTransferFile::DoWriteL( TPtrC8& aDestinationPath, TPtrC8& aData )
       
   143     {
       
   144     // Use FileHandler server for writing since SIPTester cannot have access
       
   145     // to /sys and /resource directories
       
   146     iContext.FileHandler().CreateFileL( aDestinationPath, aData );
       
   147     }
       
   148     
       
   149 void TCmdTransferFile::DoCopyL( TPtrC8& aSourcePath, TPtrC8& aDestinationPath )
       
   150     {
       
   151     iContext.FileHandler().CopyFileL( aSourcePath, aDestinationPath );
       
   152     }
       
   153 
       
   154 TDesC8 TCmdTransferFile::DoReadL( TFileName& aDestinationPath )
       
   155     {
       
   156     // Read the document into a buffer.
       
   157 	RFs fs;
       
   158 	User::LeaveIfError( fs.Connect() );
       
   159 	CleanupClosePushL( fs );
       
   160 
       
   161 	RFile in;
       
   162 	User::LeaveIfError( in.Open( fs, aDestinationPath, 
       
   163 						EFileRead ) );
       
   164 	CleanupClosePushL( in );
       
   165 
       
   166     //TBuf8< KTcRequestMaxLength > data; // 5120
       
   167     //TBuf8< KTcResponseMaxLength > data; // 40960
       
   168     TBuf8< 20 > data;
       
   169 	User::LeaveIfError( in.Read( data ) );
       
   170 
       
   171     in.Close();
       
   172     fs.Close();
       
   173 	CleanupStack::PopAndDestroy( 2 );	// in, fs
       
   174 
       
   175     return data;
       
   176     }
       
   177 
       
   178 TBool TCmdTransferFile::CheckFileExists(const TDesC& aFileName)
       
   179 	{
       
   180 	RFs fs;
       
   181 	User::LeaveIfError( fs.Connect() );
       
   182 	CleanupClosePushL( fs );
       
   183 	TParse p;
       
   184 	// do isolated parse
       
   185 	User::LeaveIfError(p.Set(aFileName, NULL, NULL));
       
   186 	    	
       
   187 	User::LeaveIfError(fs.Parse(aFileName, p));
       
   188 	
       
   189 	TBool result = BaflUtils::FileExists(fs, aFileName);
       
   190 	
       
   191 	fs.Close();
       
   192 	CleanupStack::PopAndDestroy( 1 );
       
   193 	
       
   194 	if(result)
       
   195 		return ETrue;
       
   196 	else
       
   197 		return EFalse;
       
   198 	}