commonuis/CommonDialogs/src/CAknFileNamePromptDialog.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 "CAknFileNamePromptDialog.h"
       
    21 
       
    22 #include <AknQueryDialog.h>     // CAknTextQueryDialog
       
    23 #include <barsread.h>           // TResourceReader
       
    24 #include <bautils.h>
       
    25 #include <commondialogs.rsg>
       
    26 
       
    27 #include "CAknCommonDialogsQueryDialog.h"
       
    28 #include "AknCFDUtility.h"
       
    29 
       
    30 // CONSTANTS
       
    31 // If number is 10 or more 2 digits are needed to display it
       
    32 const TInt KTwoDigitNeeded = 10;
       
    33 _LIT( KFormatStringTwoDigit, "%02d" );
       
    34 _LIT( KFormatString, "%d" );
       
    35 const TInt KFormatBufLen = 16;
       
    36 const TInt KMaxAutoNum = 99;
       
    37 // =========================== MEMBER FUNCTIONS ==============================
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // CAknFileNamePromptDialog::CAknFileNamePromptDialog
       
    41 //
       
    42 //
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CAknFileNamePromptDialog::CAknFileNamePromptDialog()
       
    46     {
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CAknFileNamePromptDialog::ConstructFromResourceL
       
    51 //
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void CAknFileNamePromptDialog::ConstructFromResourceL( TInt aResourceId )
       
    55     {
       
    56     BaseConstructL();
       
    57     if( aResourceId )
       
    58         {
       
    59         // If a resource id is given, read settings from it.
       
    60         ReadFromResourceL( aResourceId );
       
    61         }
       
    62     // After user resource is read, "patch" the missing values with defaults
       
    63     // by reading all missing settings from default resource.
       
    64     SetResourceId( aResourceId );
       
    65     ReadFromResourceL( aResourceId );
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CAknFileNamePromptDialog::NewL
       
    70 //
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 EXPORT_C CAknFileNamePromptDialog* CAknFileNamePromptDialog::NewL()
       
    74     {
       
    75     return NewL( 0 );
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CAknFileNamePromptDialog::NewL
       
    80 //
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C CAknFileNamePromptDialog* CAknFileNamePromptDialog::NewL( TInt aResourceId )
       
    84     {
       
    85     CAknFileNamePromptDialog* self = new( ELeave ) CAknFileNamePromptDialog();
       
    86     CleanupStack::PushL( self );
       
    87     self->ConstructFromResourceL( aResourceId );
       
    88     CleanupStack::Pop(); // self
       
    89     return self;
       
    90     }
       
    91 
       
    92 // Destructor
       
    93 EXPORT_C CAknFileNamePromptDialog::~CAknFileNamePromptDialog()
       
    94     {
       
    95     delete iTitle;
       
    96     delete iRightSoftkey;
       
    97     delete iLeftSoftkey;
       
    98     delete iPath;
       
    99     delete iDefaultFileName;
       
   100     }
       
   101 
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CAknFileNamePromptDialog::SetPathL
       
   105 //
       
   106 //
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C void CAknFileNamePromptDialog::SetPathL( const TDesC& aPath )
       
   110     {
       
   111     AknCFDUtility::AllocateIfValidL( iPath, aPath );
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CAknFileNamePromptDialog::SetObserver
       
   116 //
       
   117 //
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 EXPORT_C void CAknFileNamePromptDialog::SetObserver( MAknFileSelectionObserver* aObserver )
       
   121     {
       
   122     iObserver = aObserver;
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CAknFileNamePromptDialog::SetTitleL
       
   127 //
       
   128 //
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 EXPORT_C void CAknFileNamePromptDialog::SetTitleL( const TDesC& aText )
       
   132     {
       
   133     AknCFDUtility::AllocateIfValidL( iTitle, aText );
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CAknFileNamePromptDialog::SetLeftSoftkeyL
       
   138 //
       
   139 //
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C void CAknFileNamePromptDialog::SetLeftSoftkeyL( const TDesC& aText )
       
   143     {
       
   144     AknCFDUtility::AllocateIfValidL( iLeftSoftkey, aText );
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CAknFileNamePromptDialog::SetRightSoftkeyL
       
   149 //
       
   150 //
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 EXPORT_C void CAknFileNamePromptDialog::SetRightSoftkeyL( const TDesC& aText )
       
   154     {
       
   155     AknCFDUtility::AllocateIfValidL( iRightSoftkey, aText );
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CAknFileNamePromptDialog::ShowDialogL
       
   160 //
       
   161 //
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 TBool CAknFileNamePromptDialog::ShowDialogL( TDes& aFilename, TBool aIfRename )
       
   165     {
       
   166     __ASSERT_DEBUG( iPath,
       
   167         User::Panic( KCFDPanicText, ECFDPanicNoPathForFileNamePromptDialog ) );
       
   168     __ASSERT_DEBUG( iTitle && iLeftSoftkey && iRightSoftkey,
       
   169         User::Panic( KCFDPanicText, ECFDPanicTitleOrSoftkeyNotSet ) );
       
   170 
       
   171     TParsePtr parse(aFilename);
       
   172     if( ( iDefaultFileName ) && ( aFilename.Length() == 0 ) )
       
   173         {
       
   174         // If default filename is only set in resource, use it.
       
   175         TPtr defaultFileName = iDefaultFileName->Des();
       
   176         parse = TParsePtr( defaultFileName );
       
   177         }
       
   178 
       
   179     HBufC * originDataText = parse.Name().AllocL();
       
   180     originDataText = originDataText->ReAllocL( KMaxFileName );
       
   181     CleanupStack::PushL( originDataText );
       
   182 
       
   183     TPtr dataText = originDataText->Des();
       
   184     TInt pathLength( 0 );
       
   185     if( iPath )
       
   186         {
       
   187         pathLength = iPath->Length();
       
   188         }
       
   189     TInt maxLength( aFilename.MaxLength() - parse.Ext().Length() - pathLength );
       
   190 	if ( aIfRename == EFalse )
       
   191 		{
       
   192 		//Give a default file name
       
   193 		TInt i = 0;
       
   194 		TFileName fileName;
       
   195 		TBuf< KFormatBufLen > numBuf; // For autonumbering
       
   196 		
       
   197 		while ( ETrue )
       
   198 			{
       
   199 			// Create a TFileName descriptor and set full path + filename to it
       
   200 			fileName = *iPath; 
       
   201 			TInt tempNumber = dataText.Length();
       
   202 			if ( ( i > 1 ) && ( tempNumber >= 2 ) )
       
   203 			    {
       
   204 			    // Delete previous automatic number
       
   205 				dataText.Delete( tempNumber - 2, 2 ); 
       
   206 			    }
       
   207 			dataText += numBuf; 
       
   208 			tempNumber = dataText.Length();
       
   209 			if( tempNumber > maxLength )
       
   210 				{
       
   211 				if( i )
       
   212 				    {
       
   213 				    dataText.Delete(( maxLength - 2 ), ( tempNumber - maxLength ));
       
   214 				    }
       
   215 				else
       
   216 				    {
       
   217 				    dataText.Delete( maxLength, ( tempNumber - maxLength ));	
       
   218 			        }
       
   219 				}
       
   220 			fileName += dataText;
       
   221 			fileName += parse.Ext();
       
   222 			
       
   223 			if( !BaflUtils::FileExists( iCoeEnv->FsSession(), fileName ) )
       
   224 				{
       
   225 				// If the file does not exist, accept it
       
   226 				break;
       
   227 				}
       
   228 			else
       
   229 				{	
       
   230 				i++;
       
   231 				if( i > KMaxAutoNum )
       
   232 				    {
       
   233 					break;
       
   234 				    }
       
   235 				numBuf.Zero();
       
   236 			    if( i < KTwoDigitNeeded ) 
       
   237 				    {
       
   238 				    numBuf.Format( KFormatStringTwoDigit, i );
       
   239 				    }   
       
   240 			    else
       
   241 				    {
       
   242 				    numBuf.Format( KFormatString, i );
       
   243 				    }
       
   244 				
       
   245                 AknTextUtils::DisplayTextLanguageSpecificNumberConversion(
       
   246                     numBuf ); // Localize digits
       
   247 				}
       
   248 			}
       
   249 		} 
       
   250 
       
   251     CAknCommonDialogsQueryDialog* dialog = new( ELeave ) CAknCommonDialogsQueryDialog(
       
   252         iObserver, *iPath, dataText, parse.Ext(), iCoeEnv );
       
   253 
       
   254     dialog->PrepareLC( R_CFD_FILENAME_PROMPT_DIALOG );
       
   255 
       
   256     if( ( maxLength >= 0 ) && ( maxLength < KMaxFileName ) )
       
   257         {
       
   258         dialog->SetMaxLength( maxLength );
       
   259         }
       
   260 
       
   261     dialog->SetPromptL( *iTitle );
       
   262     CEikButtonGroupContainer* cba = &dialog->ButtonGroupContainer();
       
   263     MEikButtonGroup* buttonGroup = cba->ButtonGroup();
       
   264     cba->SetCommandL( buttonGroup->CommandId( 0 ), *iLeftSoftkey );
       
   265     cba->SetCommandL( buttonGroup->CommandId( 2 ), *iRightSoftkey );
       
   266 
       
   267     // Run dialog
       
   268     TBool returnValue( dialog->RunLD() == EAknSoftkeyOk );
       
   269 
       
   270     if( dataText.Length() == 0 )
       
   271         {
       
   272         // User has cancelled a query launched by dialog:
       
   273         CleanupStack::PopAndDestroy(); // originDataText
       
   274         return EFalse;
       
   275         }
       
   276 
       
   277     if( returnValue )
       
   278         {
       
   279         dataText += parse.Ext();
       
   280         aFilename = dataText;
       
   281         }
       
   282 
       
   283     CleanupStack::PopAndDestroy(); // originDataText
       
   284     return returnValue;
       
   285     }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CAknFileNamePromptDialog::ExecuteL
       
   289 //
       
   290 //
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 EXPORT_C TBool CAknFileNamePromptDialog::ExecuteL( TDes& aFileName )
       
   294     {
       
   295 	return ShowDialogL( aFileName, EFalse );
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CAknFileNamePromptDialog::RenameL
       
   300 //
       
   301 //
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 EXPORT_C TBool CAknFileNamePromptDialog::RenameL( TDes& aFileName )
       
   305 	{
       
   306     return ShowDialogL( aFileName, ETrue );
       
   307 	}
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CAknFileNamePromptDialog::RunDlgLD
       
   311 // A wrapper for RunL.
       
   312 //
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 EXPORT_C TBool CAknFileNamePromptDialog::RunDlgLD( TDes& aFileName,
       
   316     const TDesC& aPath,
       
   317     MAknFileSelectionObserver* aObserver )
       
   318     {
       
   319     return RunL( 0, aFileName, aPath, KNullDesC, aObserver );
       
   320     }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CAknFileNamePromptDialog::RunDlgLD
       
   324 // A wrapper for RunL.
       
   325 //
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 EXPORT_C TBool CAknFileNamePromptDialog::RunDlgLD( TDes& aFileName,
       
   329     const TDesC& aPath,
       
   330     const TDesC& aTitle,
       
   331     MAknFileSelectionObserver* aObserver )
       
   332     {
       
   333     return RunL( 0, aFileName, aPath, aTitle, aObserver );
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CAknFileNamePromptDialog::RunDlgLD
       
   338 // A wrapper for RunL.
       
   339 //
       
   340 // -----------------------------------------------------------------------------
       
   341 //
       
   342 EXPORT_C TBool CAknFileNamePromptDialog::RunDlgLD( TDes& aFileName,
       
   343     const TDesC& aPath,
       
   344     TInt aResourceId,
       
   345     MAknFileSelectionObserver* aObserver )
       
   346     {
       
   347     return RunL( aResourceId, aFileName, aPath, KNullDesC, aObserver );
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CAknFileNamePromptDialog::RunL
       
   352 //
       
   353 //
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 TBool CAknFileNamePromptDialog::RunL(
       
   357     TInt aResourceId,
       
   358     TDes& aFileName,
       
   359     const TDesC& aPath,
       
   360     const TDesC& aTitle,
       
   361     MAknFileSelectionObserver* aObserver )
       
   362     {
       
   363     CAknFileNamePromptDialog* self = CAknFileNamePromptDialog::NewL( aResourceId );
       
   364     CleanupStack::PushL( self );
       
   365     self->SetPathL( aPath );
       
   366     self->SetTitleL( aTitle );
       
   367     self->iObserver = aObserver;
       
   368 
       
   369     TBool returnValue( self->ExecuteL( aFileName ) );
       
   370 
       
   371     CleanupStack::PopAndDestroy(); // self
       
   372     return returnValue;
       
   373     }
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // CAknFileNamePromptDialog::?member_function
       
   377 //
       
   378 //
       
   379 // -----------------------------------------------------------------------------
       
   380 //
       
   381 void CAknFileNamePromptDialog::SetResourceId( TInt& aResourceId ) const
       
   382     {
       
   383     aResourceId = R_CFD_DEFAULT_SAVE_FILENAME_PROMPT;
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CAknFileNamePromptDialog::?member_function
       
   388 //
       
   389 //
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 void CAknFileNamePromptDialog::ReadFromResourceL( TInt aResourceId )
       
   393     {
       
   394     TResourceReader reader;
       
   395     iCoeEnv->CreateResourceReaderLC( reader, aResourceId );
       
   396     AknCFDUtility::AllocateIfNullL( iTitle, reader.ReadTPtrC() );
       
   397     AknCFDUtility::AllocateIfNullL( iDefaultFileName, reader.ReadTPtrC() );
       
   398     AknCFDUtility::AllocateIfNullL( iPath, reader.ReadTPtrC() );
       
   399     AknCFDUtility::AllocateIfNullL( iLeftSoftkey, reader.ReadTPtrC() );
       
   400     AknCFDUtility::AllocateIfNullL( iRightSoftkey, reader.ReadTPtrC() );
       
   401     CleanupStack::PopAndDestroy(); // reader
       
   402     }
       
   403 
       
   404 //  End of File