commonuis/CommonDialogs/src/CAknCommonDialogsQueryDialog.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CAknCommonDialogsQueryDialog.h"
       
    21 
       
    22 #include <apparc.h>
       
    23 #include <aknnotewrappers.h>
       
    24 #include <bautils.h>
       
    25 #include <StringLoader.h>
       
    26 #include <commondialogs.rsg>
       
    27 #include <aknQueryControl.h>
       
    28 
       
    29 #include <AknTasHook.h> // for testability hooks
       
    30 #include "MAknFileSelectionObserver.h"
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CAknCommonDialogsQueryDialog
       
    36 //
       
    37 //
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CAknCommonDialogsQueryDialog::CAknCommonDialogsQueryDialog(
       
    41 	MAknFileSelectionObserver* aObserver,
       
    42 	const TDesC& aPath,
       
    43 	TDes& aDataText,
       
    44 	const TDesC& aExtension,
       
    45 	CCoeEnv* aCoeEnv )
       
    46 	:	CAknTextQueryDialog( aDataText ),
       
    47 		iPath( aPath ),
       
    48 		iExtension( aExtension ),
       
    49 		iObserver( aObserver ),
       
    50 		iCoeEnv( aCoeEnv )
       
    51 	{
       
    52 	AKNTASHOOK_ADD( this, "CAknCommonDialogsQueryDialog" );
       
    53 	}
       
    54 
       
    55 // Destructor
       
    56 CAknCommonDialogsQueryDialog::~CAknCommonDialogsQueryDialog()
       
    57 	{
       
    58 	AKNTASHOOK_REMOVE();
       
    59 	}
       
    60 
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CAknCommonDialogsQueryDialog::OkToExitL
       
    64 //
       
    65 //
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 TBool CAknCommonDialogsQueryDialog::OkToExitL( TInt aButtonId )
       
    69 	{
       
    70 	// Call OkToExitL of the base class
       
    71 	TBool returnValue( CAknTextQueryDialog::OkToExitL( aButtonId ) );
       
    72 
       
    73 	if( returnValue )
       
    74 		{
       
    75 		// User wants to go with the typed filename
       
    76 
       
    77         TBool userCancelled( EFalse );
       
    78 		if( !FileNameAllowedL( iPath, iDataText, iExtension, userCancelled ) )
       
    79 			{
       
    80             // If user cancelled the "rename" query, this dialog must exit,
       
    81             // so return ETrue, but clear the given name first:
       
    82             if( userCancelled )
       
    83                 {
       
    84                 iDataText.Zero();
       
    85                 return ETrue;
       
    86                 }
       
    87 			// Otherwise if the name is not allowed, it is not Ok to exit.
       
    88             // CAknQueryControl::SetTextL() is used here to get the
       
    89             // user-written text selected:
       
    90             CAknQueryControl* queryCtrl = QueryControl();
       
    91             queryCtrl->SetTextL( iDataText );
       
    92 			return EFalse;
       
    93 			}
       
    94 
       
    95 		// Filename is valid, ask observer's opinion too.
       
    96 
       
    97 		if( iObserver )
       
    98 			{
       
    99 			TEntry entry;
       
   100 			entry.iName = iDataText;
       
   101 			if( !iObserver->OkToExitL( iPath, entry ) )
       
   102 				{
       
   103 				// Observer doesn't think the file is Ok, don't exit. But first,
       
   104                 // make the user-written text selected:
       
   105                 CAknQueryControl* queryCtrl = QueryControl();
       
   106                 queryCtrl->SetTextL( iDataText );
       
   107 				return EFalse;
       
   108 				}
       
   109 			}
       
   110 		}
       
   111 	return returnValue;
       
   112 	}
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CAknCommonDialogsQueryDialog::FileNameAllowedL
       
   116 //
       
   117 //
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 TBool CAknCommonDialogsQueryDialog::FileNameAllowedL(
       
   121 	const TDesC& aPath, const TDesC& aFileName, const TDesC& aExtension,
       
   122     TBool& aUserCancelled )
       
   123 	{
       
   124 	// Create a TFileName descriptor and set full path and filename to it
       
   125 	TFileName fileName( aPath );
       
   126 	fileName += aFileName;
       
   127 	fileName += aExtension;
       
   128 	// Create a TParsePtrC with the full path and filename, parsed filename points
       
   129 	// its data to the created TFileName
       
   130 	TParsePtrC parsedFile( fileName );
       
   131 	// Temporary descriptor pointer
       
   132 	TPtrC temp( parsedFile.Name() );
       
   133 
       
   134 	RFs fs( iCoeEnv->FsSession() );
       
   135 	TBool badFileName( EFalse );
       
   136 	TBool illegalCharacters( EFalse );
       
   137 	_LIT( KSingleDotCharacter, "."  );
       
   138 	_LIT( KDualDotCharacter,   ".." );
       
   139 	_LIT( KCarriageReturn, "\x2029" );
       
   140 	
       
   141 	if( ( temp.Compare( KSingleDotCharacter ) == 0 ) ||
       
   142 		( temp.Compare( KDualDotCharacter   ) == 0 ) ||
       
   143 		( aFileName.Find( KCarriageReturn ) != KErrNotFound ) ||
       
   144         ( ( aFileName.Compare( KSingleDotCharacter ) == 0 ) && 
       
   145           ( aExtension.Compare( KNullDesC ) == 0 ) ) )
       
   146 		{
       
   147 		// If filename consists only of "." or ".." it is BAD file name
       
   148 		badFileName= ETrue;
       
   149 		}
       
   150 	else
       
   151 		{
       
   152 		// Also RFs::IsValidName tells efficiently if the filename is ok.
       
   153 		// Get the name and extension for next check
       
   154 		temp.Set( parsedFile.FullName() );
       
   155 		illegalCharacters = !fs.IsValidName( temp );
       
   156         
       
   157         // RFs::IsValidName does not check if filename includes '\' and mirror '?'       
       
   158         TChar backslash( '\\' );
       
   159         _LIT( KMirrorQuestion, "\x061F" );
       
   160         if (( aFileName.Locate( backslash ) != KErrNotFound ) || 
       
   161             ( aFileName.Find( KMirrorQuestion ) != KErrNotFound ) )
       
   162             {
       
   163             illegalCharacters = ETrue;
       
   164             }
       
   165 		}
       
   166 
       
   167 	if( badFileName || illegalCharacters )
       
   168 		{
       
   169 		// Depending on the error, read loc string
       
   170 		HBufC* text = iCoeEnv->AllocReadResourceLC( badFileName ?
       
   171 			R_CFD_QTN_FLDR_BAD_FILE_NAME : R_CFD_QTN_FLDR_ILLEGAL_CHARACTERS );
       
   172 		CAknErrorNote* note = new( ELeave ) CAknErrorNote( EFalse ); 
       
   173 		note->ExecuteLD( *text );
       
   174 		CleanupStack::PopAndDestroy(); // text
       
   175 		return EFalse;
       
   176 		}
       
   177 
       
   178 	// the filename is valid
       
   179 
       
   180 	// Get the full filename and path for FileExists check
       
   181 	temp.Set( parsedFile.FullName() );
       
   182 	if( !BaflUtils::FileExists( fs, temp ) )
       
   183 		{
       
   184 		// If the file does not exist, accept it
       
   185 		return ETrue;
       
   186 		}
       
   187 
       
   188     // The file already exists
       
   189     // Check if it's a folder (it cannot be overwritten).
       
   190     TBool overwriteNotAllowed( EFalse );
       
   191     User::LeaveIfError( BaflUtils:: IsFolder( fs, temp, overwriteNotAllowed ) );
       
   192 	temp.Set( parsedFile.Name() );
       
   193 
       
   194 	CAknQueryDialog* dialog = new( ELeave ) CAknQueryDialog();
       
   195 	dialog->PrepareLC( R_CFD_CONFIRMATION_QUERY );
       
   196 	HBufC* confirmationText = NULL;
       
   197     if( overwriteNotAllowed )
       
   198         {
       
   199         confirmationText = StringLoader::LoadLC(     // "Name %U already in use.
       
   200                 R_CFD_QTN_FLDR_RENAME_QUERY, temp ); // Rename?"
       
   201         }
       
   202     else
       
   203         {
       
   204         confirmationText = StringLoader::LoadLC(        // "%U already exists!
       
   205                 R_CFD_QTN_FLDR_OVERWRITE_QUERY, temp ); // Overwrite?"
       
   206         }
       
   207 	dialog->SetPromptL( *confirmationText );
       
   208 	CleanupStack::PopAndDestroy(); // confirmationText
       
   209 	TInt result( dialog->RunLD() );
       
   210 
       
   211 	if( ( result == EAknSoftkeyYes ) || ( result == EAknSoftkeyOk ) )
       
   212 		{
       
   213 		// User accepted overwrite or OK'ed the rename query:
       
   214 		return !overwriteNotAllowed;
       
   215 		}
       
   216 
       
   217 	// User answered "No", if it was rename query, this dialog must be exited:
       
   218     if ( overwriteNotAllowed )
       
   219         {
       
   220         aUserCancelled = ETrue;
       
   221         return EFalse;
       
   222         }
       
   223 
       
   224     // User didnt' accept overwrite, ask a new name from user.
       
   225 
       
   226 	HBufC* renamePrompt = iCoeEnv->AllocReadResourceLC( R_CFD_QTN_FLDR_ITEM_NAME_PRMPT ); // "New name:"
       
   227 	SetPromptL( *renamePrompt );
       
   228 	CleanupStack::PopAndDestroy(); // renamePrompt
       
   229 
       
   230 	return EFalse;
       
   231 	}
       
   232 
       
   233 // End of File