voicerecorder/UtilsSrc/TVRRename.cpp
branchRCL_3
changeset 21 c6bafb5162d8
parent 0 845549f293a7
equal deleted inserted replaced
20:072a5fa0c63b 21:c6bafb5162d8
       
     1 /*
       
     2 * Copyright (c) 2002-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *     This class encapsulates the functionality associated with renaming
       
    16 *     files in different situation. The implementation is made according
       
    17 *     to the Folders v.4.0 specification.
       
    18 *
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 // INCLUDES
       
    24 #include <f32file.h>
       
    25 #include <AknQueryDialog.h>
       
    26 #include <aknnotewrappers.h>
       
    27 #include <bautils.h>
       
    28 #include <StringLoader.h>
       
    29 #include <apparc.h>
       
    30 
       
    31 #include <voicerecorder.rsg>
       
    32 #include "voicerecorder.hrh"
       
    33 #include "TVRRename.h"
       
    34 
       
    35 // CONSTANTS
       
    36 const TInt KMaxFileNameLength( 64 );
       
    37 
       
    38 
       
    39 // ================= MEMBER FUNCTIONS ========================================
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // TVRRename::TVRRename
       
    43 // 
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C TVRRename::TVRRename( RFs& aFs ) : iFs( aFs )
       
    47     {
       
    48     }
       
    49 
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // TVRRename::GetNewNameL
       
    53 // 
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C TBool TVRRename::GetNewNameL( const TDesC& aOldName, 
       
    57 									   TDes& aNewName, 
       
    58 									   const TInt aPrompt )
       
    59 	{
       
    60 	CCoeEnv* coeEnv = CCoeEnv::Static();
       
    61 
       
    62 	TParsePtrC nameParse( aOldName );
       
    63 	TBuf<KMaxFileNameLength> fileName;		
       
    64 
       
    65 	// The query can fit only VRMEMONAMEMAXLENGTH characters
       
    66 	fileName.Copy( nameParse.Name().Left( VRMEMONAMEMAXLENGTH ) );
       
    67 
       
    68 	CAknStaticSizeDataQuerydialog* nameDlg = new( ELeave ) 
       
    69 								CAknTextQueryDialog( fileName ); // CSI: 35 #
       
    70 								
       
    71 	nameDlg->PrepareLC( R_VR_RENAME_DIALOG );
       
    72 	HBufC* prompt = StringLoader::LoadLC( aPrompt, coeEnv );
       
    73 	nameDlg->SetPromptL( *prompt );
       
    74 	CleanupStack::PopAndDestroy();
       
    75 
       
    76 	while ( nameDlg->RunLD() != 0 )
       
    77 		{
       
    78 		// Construct the full path of the new proposed (by the user) name 
       
    79 		aNewName.Copy( nameParse.DriveAndPath() );
       
    80 		aNewName.Append( fileName );
       
    81 		aNewName.Append( nameParse.Ext() );
       
    82 
       
    83 		TInt backslash = fileName.Find(_L( "\\" ) );
       
    84 
       
    85 		// Check that the name is valid for the fs32
       
    86 		// Is ValidFileName does not check backslash
       
    87 		if ( iFs.IsValidName( aNewName ) && backslash == KErrNotFound )
       
    88 			{	
       
    89 			// Check if it exists?
       
    90 			if ( !BaflUtils::FileExists( iFs, aNewName ) ||
       
    91 				aOldName == aNewName )
       
    92 				{
       
    93 				// Nope => accepted 
       
    94 				return ETrue;
       
    95 				}
       
    96 
       
    97 			// Prompt the user for "Overwrite?"
       
    98 			prompt = StringLoader::LoadLC( R_QTN_FLDR_OVERWRITE_QUERY, 
       
    99 										   fileName, 
       
   100 										   coeEnv );
       
   101 										   
       
   102 			CAknQueryDialog* queryDlg = CAknQueryDialog::NewL();
       
   103 			TBool accepted( queryDlg->ExecuteLD( R_VR_CONFIRMATION_QUERY, 
       
   104 												 *prompt ) );
       
   105 
       
   106 			CleanupStack::PopAndDestroy( prompt );
       
   107 
       
   108 			if ( accepted )
       
   109 				{
       
   110 				// User accepted overwrite
       
   111 				return ETrue;
       
   112 				}
       
   113 
       
   114 			// Create a new dialog for the next round
       
   115 			// TfileName. (CApaApplication::GenerateFileName needs it)
       
   116 			TFileName generatedName;	 
       
   117 			generatedName.Copy( nameParse.DriveAndPath() );
       
   118 			generatedName.Append( fileName );
       
   119 			generatedName.Append( nameParse.Ext() );
       
   120 			User::LeaveIfError( 
       
   121 					CApaApplication::GenerateFileName( iFs, generatedName ) );
       
   122 			
       
   123 			TParsePtrC generatedNameParse( generatedName );
       
   124 			TPtrC generatedNamePtr( generatedNameParse.Name() );
       
   125 			if ( generatedNamePtr.Length() > VRMEMONAMEMAXLENGTH )
       
   126 				{
       
   127 				// Name got too long => clip the suffix
       
   128 				fileName.Copy( generatedNamePtr.Left( 
       
   129 					generatedNamePtr.LocateReverse( '(' ) ) );
       
   130 				}
       
   131 			else 
       
   132 				{
       
   133 				fileName.Copy( generatedNamePtr );
       
   134 				}
       
   135 			
       
   136 			}
       
   137 		else
       
   138 			{
       
   139 			// The name wasn't a valid fs32 filename => show info note
       
   140 			HBufC* noteText = coeEnv->AllocReadResourceLC
       
   141 											( R_QTN_FLDR_ILLEGAL_CHARACTERS );
       
   142 			CAknInformationNote* infoNote = new( ELeave ) 
       
   143 												CAknInformationNote( ETrue );
       
   144 			infoNote->ExecuteLD( *noteText );
       
   145 			CleanupStack::PopAndDestroy();	// noteText
       
   146 			}
       
   147 
       
   148 		nameDlg = new( ELeave ) CAknTextQueryDialog( fileName );
       
   149 		nameDlg->PrepareLC( R_VR_RENAME_DIALOG );	
       
   150 		}
       
   151 
       
   152 	// User canceled
       
   153 	return EFalse;
       
   154 	}
       
   155 
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // TVRRename::RenameL
       
   159 // 
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 EXPORT_C TBool TVRRename::RenameL( TDes& aName, const TInt aPrompt )
       
   163 	{
       
   164 	HBufC* oldName = aName.AllocLC();
       
   165 	if ( GetNewNameL( *oldName, aName, aPrompt ) )
       
   166 		{
       
   167 		TInt err ( BaflUtils::RenameFile( iFs, *oldName, aName ) );
       
   168 		if ( err )
       
   169 			{
       
   170 			// Show error note "Unable to rename %U"
       
   171 			CAknInformationNote* infoNote = NULL;
       
   172 			TParsePtrC parse( oldName->Des() );
       
   173 			HBufC* noteText = StringLoader::LoadLC( R_QTN_FLDR_CANT_RENAME_ITEM, 
       
   174 													parse.Name(), 
       
   175 													CCoeEnv::Static() );
       
   176 		    if ( !noteText )
       
   177 		    	{
       
   178 		    	CleanupStack::PopAndDestroy(); // oldName 
       
   179 		    	return EFalse;
       
   180 		    	}
       
   181 			
       
   182 			infoNote = new( ELeave ) CAknInformationNote( ETrue );
       
   183 			if ( infoNote )
       
   184 				{
       
   185 				infoNote->ExecuteLD( *noteText );
       
   186 				}
       
   187 			CleanupStack::PopAndDestroy( 2 ); // oldName, noteText	
       
   188 			return EFalse;
       
   189 			}
       
   190 			
       
   191 		CleanupStack::PopAndDestroy( oldName );
       
   192 		return ETrue;
       
   193 		}
       
   194 	else
       
   195 		{
       
   196 		CleanupStack::PopAndDestroy( oldName );
       
   197 		return EFalse;
       
   198 		}
       
   199 	}
       
   200 
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // TVRRename::RenameL
       
   204 // The file handle may not point to private folder, since renaming can't be
       
   205 // used in embedded mode
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 EXPORT_C TBool TVRRename::RenameL( RFile& aFile, const TInt aPrompt )
       
   209 	{
       
   210 	TFileName name( KNullDesC );
       
   211 	aFile.FullName( name );
       
   212 	HBufC* oldName = name.AllocLC();
       
   213 	
       
   214 	if ( GetNewNameL( *oldName, name, aPrompt ) )
       
   215 		{
       
   216 		aFile.Close();
       
   217 		TInt err ( BaflUtils::RenameFile( iFs, *oldName, name ) );
       
   218 		if ( err )
       
   219 			{
       
   220 			// Show error note "Unable to rename %U"
       
   221 			// and open the old file again			
       
   222 			User::LeaveIfError( aFile.Open( iFs, oldName->Des(),
       
   223 			                                EFileShareReadersOnly ) );
       
   224 			
       
   225 			CAknInformationNote* infoNote = NULL;
       
   226 			TParsePtrC parse( oldName->Des() );
       
   227 			HBufC* noteText = StringLoader::LoadLC(
       
   228 			    R_QTN_FLDR_CANT_RENAME_ITEM, parse.Name(), CCoeEnv::Static() );
       
   229 			
       
   230 			infoNote = new( ELeave ) CAknInformationNote( ETrue );
       
   231 			infoNote->ExecuteLD( *noteText );
       
   232 
       
   233 			CleanupStack::PopAndDestroy( 2 ); // oldName, noteText	
       
   234 			return EFalse;
       
   235 			}
       
   236 		CleanupStack::PopAndDestroy( oldName );
       
   237 		User::LeaveIfError( aFile.Open( iFs, name, EFileShareReadersOnly ) );
       
   238 		return ETrue;
       
   239 		}
       
   240 	else
       
   241 		{
       
   242 		CleanupStack::PopAndDestroy( oldName );
       
   243 		return EFalse;
       
   244 		}
       
   245 	}