multimediacommsengine/tsrc/MMCTestDriver/MCETester/src/CTcMCEFileHandler.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2009 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 "CTcMCEFileHandler.h"
       
    19 
       
    20 _LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B");
       
    21 
       
    22 CTcMCEFileHandler::CTcMCEFileHandler()
       
    23 	{
       
    24 	// No implementation required
       
    25 	}
       
    26 
       
    27 CTcMCEFileHandler::~CTcMCEFileHandler()
       
    28 	{
       
    29 	iFsSession.Close();
       
    30 	}
       
    31 
       
    32 CTcMCEFileHandler* CTcMCEFileHandler::NewLC()
       
    33 	{
       
    34 	CTcMCEFileHandler* self = new (ELeave) CTcMCEFileHandler();
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL();
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 CTcMCEFileHandler* CTcMCEFileHandler::NewL()
       
    41 	{
       
    42 	CTcMCEFileHandler* self = CTcMCEFileHandler::NewLC();
       
    43 	CleanupStack::Pop(); // self;
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 void CTcMCEFileHandler::ConstructL()
       
    48 	{
       
    49 	User::LeaveIfError(iFsSession.Connect());
       
    50 	}
       
    51 
       
    52 void CTcMCEFileHandler::FindFile(const TDesC& aPath, 
       
    53 			const TDesC& aFileName, CDesC8Array* aArray)
       
    54 	{
       
    55 	CDirScan* DirScan = CDirScan::NewLC(iFsSession);
       
    56 	DirScan->SetScanDataL(aPath, 
       
    57 		KEntryAttDir|KEntryAttMatchExclusive, 
       
    58 		ESortNone, CDirScan::EScanDownTree);
       
    59 	 
       
    60 	while(true)
       
    61 		{
       
    62 		CDir* dir = NULL;
       
    63 		TRAPD(error, DirScan->NextL(dir));
       
    64 		if (error || !dir)
       
    65 			break;
       
    66 		
       
    67 		delete dir;
       
    68  
       
    69 		ScanDirectoryL(DirScan->FullPath(), aFileName, aArray);
       
    70 		};
       
    71 	
       
    72 	CleanupStack::PopAndDestroy(DirScan);
       
    73 
       
    74 	}
       
    75 
       
    76 TInt CTcMCEFileHandler::GetFilesFromFolderL(TDesC& aFolder,CDesC8Array* aArray)
       
    77 	{
       
    78 	if(aArray == NULL)
       
    79 		return KErrArgument;
       
    80 	
       
    81 	CDir* FileList(NULL);
       
    82 	TInt res = iFsSession.GetDir(aFolder,
       
    83 		KEntryAttMaskSupported, EDirsFirst, FileList);
       
    84 	if( res == KErrNone )	
       
    85 		{
       
    86 			if(FileList && aArray)
       
    87 			{
       
    88 				FileList->Sort(ESortByName);
       
    89 				for(TInt i=0; i < FileList->Count(); i++)
       
    90 				{
       
    91 					if((*FileList)[i].IsSystem() 
       
    92 					|| (*FileList)[i].IsHidden()
       
    93 					|| (*FileList)[i].IsDir())
       
    94 					{
       
    95 						// ignore System & Hidden Files & folders
       
    96 					}
       
    97 					else
       
    98 					{
       
    99 						TBuf8<255> buf8;
       
   100 						buf8.Copy((*FileList)[i].iName);
       
   101 						//add the file name to the array
       
   102 						aArray->AppendL(buf8);
       
   103 					}
       
   104 				}
       
   105 			}
       
   106 		
       
   107 			delete FileList;
       
   108 			FileList = NULL;
       
   109 		}
       
   110 	return res;
       
   111 	}
       
   112 
       
   113 TBool CTcMCEFileHandler::Delete(const TDesC& aName, TUint aSwitch)
       
   114 	{
       
   115 	CFileMan* fileMan=CFileMan::NewL(iFsSession);
       
   116 	CleanupStack::PushL(fileMan);
       
   117  
       
   118 	TInt err=fileMan->Delete(aName,aSwitch);
       
   119 	CleanupStack::PopAndDestroy(fileMan);
       
   120  
       
   121 	if(err==KErrNone)
       
   122 		return ETrue;
       
   123 	else
       
   124 		return EFalse;
       
   125 
       
   126 	}
       
   127 
       
   128 
       
   129 TBool CTcMCEFileHandler::CheckFileExists(const TDesC& aFileName)
       
   130 	{
       
   131 	TParse p;
       
   132 	// do isolated parse
       
   133 	User::LeaveIfError(p.Set(aFileName, NULL, NULL));
       
   134 	    	
       
   135 	User::LeaveIfError(iFsSession.Parse(aFileName, p));
       
   136 	
       
   137 	TBool result = BaflUtils::FileExists(iFsSession, aFileName);
       
   138 	
       
   139 	if(result)
       
   140 		return ETrue;
       
   141 	else
       
   142 		return EFalse;
       
   143 	}
       
   144 
       
   145 //name: value
       
   146 void CTcMCEFileHandler::GetFileInfoL(const TDesC& aFileName, CDesC8Array* aArray)
       
   147 	{
       
   148 	if(aArray == NULL)
       
   149 		User::LeaveIfError(KErrArgument);
       
   150 
       
   151 	if(CheckFileExists(aFileName) == EFalse)
       
   152 		User::LeaveIfError(KErrNotFound);
       
   153 	
       
   154     _LIT8(KFileName,"File Name:");
       
   155     _LIT8(KDateModified,"Date Modified:");
       
   156     _LIT8(KFileSize,"File Size:");
       
   157     _LIT8(KFileAttrib,"Entry details:");
       
   158 	
       
   159 	TParse parse;
       
   160 	User::LeaveIfError(parse.Set(aFileName, NULL, NULL));
       
   161 	
       
   162     //Consists of a drive and path
       
   163     iFsSession.SetSessionPath( parse.DriveAndPath() );   
       
   164     
       
   165     TFileName name( parse.NameAndExt() );
       
   166 		
       
   167 	// Get entry details for file and print them    
       
   168 	TEntry entry;  
       
   169 	User::LeaveIfError(iFsSession.Entry(name, entry)); 
       
   170 	
       
   171 	TBuf<30> dateString;
       
   172 	entry.iModified.FormatL(dateString, KDateString);
       
   173 	
       
   174 	TBuf8<255> buf8;
       
   175 	//file name
       
   176 	aArray->AppendL( KFileName );
       
   177 	buf8.Copy( parse.NameAndExt() );
       
   178 	aArray->AppendL( buf8 );
       
   179 	buf8.Zero();
       
   180 	//date modified
       
   181 	aArray->AppendL( KDateModified );
       
   182 	buf8.Copy(dateString);
       
   183 	aArray->AppendL( buf8 );
       
   184 	buf8.Zero();
       
   185 	
       
   186 	//file size
       
   187 	TBuf8< 12 > convert;
       
   188 	convert.AppendNum( entry.iSize );
       
   189 	aArray->AppendL( KFileSize );
       
   190 	aArray->AppendL( convert );
       
   191 	
       
   192 	TBuf8<15> buffer;
       
   193 	FormatEntry(buffer,entry); // Archive attribute should be set
       
   194 	//attribs
       
   195 	aArray->AppendL(KFileAttrib);
       
   196 	aArray->AppendL(buffer);
       
   197 	}
       
   198 
       
   199 void CTcMCEFileHandler::FormatEntry(TDes8& aBuffer, const TEntry& aEntry)
       
   200     {
       
   201     _LIT(KReadOnly,"Read-only");
       
   202     _LIT(KHidden,"Hidden");
       
   203     _LIT(KSystem,"System");
       
   204     _LIT(KDirectory,"Directory");
       
   205     _LIT(KArchive,"Archive");
       
   206     
       
   207     if(aEntry.IsReadOnly())
       
   208         aBuffer.Append(KReadOnly);
       
   209     if(aEntry.IsHidden())
       
   210         aBuffer.Append(KHidden);
       
   211     if(aEntry.IsSystem())
       
   212         aBuffer.Append(KSystem);
       
   213     if(aEntry.IsDir())
       
   214         aBuffer.Append(KDirectory);
       
   215     if(aEntry.IsArchive())
       
   216         aBuffer.Append(KArchive);
       
   217     }
       
   218 
       
   219 void CTcMCEFileHandler::ScanDirectoryL(const TDesC& aDir,
       
   220 		const TDesC& aWildCard, CDesC8Array* aArray)
       
   221     {
       
   222     TParse parse;
       
   223     parse.Set(aWildCard, &aDir, NULL);
       
   224     TPtrC spec(parse.FullName());
       
   225  
       
   226     TFindFile FindFile(iFsSession);
       
   227     CDir* dir;
       
   228  
       
   229     if (!FindFile.FindWildByPath(parse.FullName(), NULL, dir))
       
   230         {
       
   231         CleanupStack::PushL(dir);
       
   232  
       
   233         for(TInt i = 0; i < dir->Count(); i++)
       
   234             {
       
   235             parse.Set((*dir)[i].iName, &spec, NULL);
       
   236             TBuf8<255> buf8;
       
   237             buf8.Copy(parse.FullName());
       
   238             aArray->AppendL(buf8);
       
   239             }
       
   240         CleanupStack::PopAndDestroy(dir);
       
   241         }
       
   242     }