mpxmusicplayer/commonui/src/mpxfilenamequerydlg.cpp
changeset 0 ff3acec5bc43
child 21 a1247965635c
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     1 /*
       
     2 * Copyright (c) 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:  rename dialog for Common UI
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <coeutils.h>
       
    21 #include <eikapp.h>
       
    22 #include <StringLoader.h>
       
    23 #include <aknnotewrappers.h>
       
    24 #include <mpxcollectionuihelper.h>
       
    25 #include <mpxcollectionhelperfactory.h>
       
    26 #include <mpxcommonui.rsg>
       
    27 #include <mpxlog.h>
       
    28 
       
    29 #include "mpxcommonuihelper.h"
       
    30 #include "mpxfilenamequerydlg.h"
       
    31 
       
    32 // CONSTANTS
       
    33 const TInt KMPXMaxPlaylistPathLen ( 200 );
       
    34 
       
    35 _LIT( KBackslash, "\\" );
       
    36 _LIT( KDirectionalChars, "\x202A\x202B\x202C\x202D\x200E\x200F" );
       
    37 
       
    38 const TUint16 KIllegalChars[] = {
       
    39     '<', '>', '"', '/', '\\', '|', ':', '*', '?',
       
    40     0xFF02, // Full width quote
       
    41     0xFF0A, // Full width asterisk
       
    42     0xFF0F, // Full width slash
       
    43     0xFF1A, // Full width colon
       
    44     0xFF1C, // Full width left arrow
       
    45     0xFF1E, // Full width right arrow
       
    46     0xFF1F, // Full width question mark
       
    47     0xFF3C, // Full width back slash
       
    48     0xFF5C, // Full width pipe
       
    49     0x201C, // Left quote
       
    50     0x201D, // Right quote
       
    51     0x201F, // Reversed quote
       
    52     0, // Array terminator
       
    53 };
       
    54 
       
    55 
       
    56 // ============================ MEMBER FUNCTIONS ===============================
       
    57 
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Two-phased constructor.
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CMPXFileNameQueryDlg* CMPXFileNameQueryDlg::NewL(
       
    64         const TDesC& aOldName,
       
    65         TDes& aNewName,
       
    66         const TDesC& aPath,
       
    67         TMPXGeneralCategory aCategory,
       
    68         CMPXCommonUiHelper* aParent )
       
    69     {
       
    70     MPX_FUNC( "CMPXFileNameQueryDlg::NewL" );
       
    71     CMPXFileNameQueryDlg* self =
       
    72         new( ELeave ) CMPXFileNameQueryDlg( aNewName, aCategory, aParent );
       
    73 
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL( aOldName, aPath );
       
    76     CleanupStack::Pop( self );
       
    77 
       
    78     return self;
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // C++ default constructor can NOT contain any code, that
       
    83 // might leave.
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 CMPXFileNameQueryDlg::CMPXFileNameQueryDlg(
       
    87     TDes& aNewName,
       
    88     TMPXGeneralCategory aCategory,
       
    89     CMPXCommonUiHelper* aParent ) :
       
    90     CAknTextQueryDialog( aNewName ),
       
    91     iParent( aParent ),
       
    92     iCategory( aCategory )
       
    93     {
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // Symbian 2nd phase constructor can leave.
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 void CMPXFileNameQueryDlg::ConstructL(
       
   101     const TDesC& aOldName, const TDesC& aPath )
       
   102     {
       
   103     MPX_FUNC( "CMPXFileNameQueryDlg::ConstructL" );
       
   104     TParsePtrC name( aOldName );
       
   105     iOldName = aOldName.AllocL();
       
   106     iPath = aPath.AllocL();
       
   107     // Strip any directionality markers to get pure name
       
   108     TPtr ptr( iOldName->Des() );
       
   109     AknTextUtils::StripCharacters( ptr, KDirectionalChars );
       
   110     TInt extLength( 0 );
       
   111     if ( iCategory == EMPXPlaylist )
       
   112         {
       
   113         Text().Copy( name.NameAndExt() );
       
   114         }
       
   115     else
       
   116         {
       
   117         Text().Copy( name.Name() );
       
   118         if ( name.ExtPresent() )
       
   119             {
       
   120             extLength = name.Ext().Length();
       
   121             }
       
   122         }
       
   123     TInt maxLength( KMPXMaxPlaylistPathLen - aPath.Length() - extLength );
       
   124     MPX_DEBUG2( "CMPXFileNameQueryDlg::ConstructL setting max length = %d", maxLength );
       
   125     SetMaxLength( maxLength );
       
   126     iCollectionUiHelper = CMPXCollectionHelperFactory::NewCollectionUiHelperL();
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // Destructor
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 CMPXFileNameQueryDlg::~CMPXFileNameQueryDlg()
       
   134     {
       
   135     if ( iCollectionUiHelper )
       
   136         {
       
   137         iCollectionUiHelper->Close();
       
   138         }
       
   139     delete iOldName;
       
   140     delete iPath;
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CFileManagerFileNameQueryDlg::OkToExitL
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 TBool CMPXFileNameQueryDlg::OkToExitL( TInt aButtonId )
       
   148     {
       
   149     MPX_FUNC( "CMPXFileNameQueryDlg::OkToExitL" );
       
   150     TBool result( CAknTextQueryDialog::OkToExitL( aButtonId ) );
       
   151 
       
   152     HBufC* userText = Text().AllocLC();
       
   153     TPtr ptrUserText( userText->Des() );
       
   154 
       
   155     // Strip any directionality markers to get pure name
       
   156     AknTextUtils::StripCharacters( ptrUserText, KDirectionalChars );
       
   157 
       
   158     TParsePtrC currentPath( *iPath );
       
   159     // Check file name
       
   160     TBool isValidName = IsValidName( currentPath.DriveAndPath(), *userText, EFalse );
       
   161     if( !isValidName )
       
   162         {
       
   163         if( IllegalChars( *userText ) )
       
   164             {
       
   165             iParent->DisplayInfoNoteL(
       
   166                 R_MPX_QTN_FLDR_ILLEGAL_CHARACTERS );
       
   167             }
       
   168         else
       
   169             {
       
   170             iParent->DisplayInfoNoteL(
       
   171                 R_MPX_QTN_FLDR_BAD_FILE_NAME );
       
   172             }
       
   173         CAknQueryControl* queryControl = QueryControl();
       
   174         if ( queryControl )
       
   175             {
       
   176             CEikEdwin* edwin = static_cast< CEikEdwin* >(
       
   177                 queryControl->ControlByLayoutOrNull( EDataLayout ) );
       
   178             if (edwin)
       
   179                 {
       
   180                 edwin->SetSelectionL( edwin->TextLength(), 0 );
       
   181                 }
       
   182             }
       
   183         CleanupStack::PopAndDestroy( userText );
       
   184         return EFalse;
       
   185         }
       
   186 
       
   187     // if some other entry found with same name
       
   188     // not ok except if name same as original
       
   189     if ( iCategory != EMPXPlaylist )
       
   190         {
       
   191         if ( iCollectionUiHelper->TitleExistsL( iCategory, *userText ) )
       
   192             {
       
   193             TBool overWrite( EFalse );
       
   194             HBufC* text = StringLoader::LoadLC( R_MPX_QTN_FLDR_OVERWRITE_QUERY, Text() );
       
   195             CAknQueryDialog* dlg = CAknQueryDialog::NewL();
       
   196             overWrite = dlg->ExecuteLD( R_MPX_CONFIRM_QUERY_WITH_YES_NO, *text );
       
   197             CleanupStack::PopAndDestroy( text );
       
   198             if( !overWrite )
       
   199                 {
       
   200                 CAknQueryControl* queryControl = QueryControl();
       
   201                 if (queryControl)
       
   202                     {
       
   203                     CEikEdwin* edwin = static_cast< CEikEdwin* >(
       
   204                         queryControl->ControlByLayoutOrNull( EDataLayout ) );
       
   205 
       
   206                     HBufC* newTitle = iParent->GenerateTitleL( iCategory, *userText );
       
   207                     CleanupStack::PushL(newTitle);
       
   208 
       
   209                     if ( edwin )
       
   210                         {
       
   211                         edwin->SetTextL( newTitle );
       
   212                         edwin->SetSelectionL( edwin->TextLength(), 0 );
       
   213                         }
       
   214                     CleanupStack::PopAndDestroy(newTitle);
       
   215                     }
       
   216                 result = EFalse;
       
   217                 }
       
   218             }
       
   219         }
       
   220     CleanupStack::PopAndDestroy( userText );
       
   221     if ( result && iCategory != EMPXPlaylist )
       
   222         {
       
   223         // playlist has no extension, don't need to do this
       
   224         TParsePtrC oldName( *iOldName );
       
   225         iDataText.Append( oldName.Ext() );
       
   226         }
       
   227     return result;
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CMPXFileNameQueryDlg::IsValidName
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 TBool CMPXFileNameQueryDlg::IsValidName(
       
   235         const TDesC& aDriveAndPath, const TDesC& aName, TBool aIsFolder ) const
       
   236     {
       
   237     MPX_FUNC( "CMPXFileNameQueryDlg::IsValidName" );
       
   238     // Check name for bad chars
       
   239     const TUint16 KMinAllowedChar = 0x0020;
       
   240     const TUint16 KParagraphSeparator = 0x2029;
       
   241     const TUint16 KDot = '.';
       
   242     TInt nameLen( aName.Length() );
       
   243     if ( !nameLen )
       
   244         {
       
   245         return EFalse;
       
   246         }
       
   247     for ( TInt i( 0 ); i < nameLen; i++ )
       
   248         {
       
   249         TUint16 ch( aName[ i ] );
       
   250         if ( ch < KMinAllowedChar || ch == KParagraphSeparator )
       
   251             {
       
   252             return EFalse;
       
   253             }
       
   254         }
       
   255     // File system ignores totally dot in the end of name, so
       
   256     // we set here as not valid name, so that user gets correctly informed
       
   257     if ( aName[ nameLen - 1 ] == KDot || IllegalChars( aName ) )
       
   258         {
       
   259         return EFalse;
       
   260         }
       
   261     // Get full path length
       
   262     TPtrC pathPtr( aDriveAndPath );
       
   263     TInt pathLen( pathPtr.Length() );
       
   264     if ( !pathLen )
       
   265         {
       
   266         return EFalse;
       
   267         }
       
   268     TInt fullPathLen( pathLen );
       
   269     if ( pathPtr[ pathLen - 1 ] != KBackslash()[ 0 ] )
       
   270         {
       
   271         ++fullPathLen; // Add backslash before name
       
   272         }
       
   273     fullPathLen += nameLen;
       
   274     if ( aIsFolder )
       
   275         {
       
   276         ++fullPathLen; // Add folder final backslash
       
   277         }
       
   278 
       
   279     TBool ret( EFalse );
       
   280     if ( fullPathLen <= KMaxFileName )
       
   281         {
       
   282         // Check full path
       
   283         HBufC* fullPath = HBufC::New( KMaxFileName );
       
   284         if ( fullPath )
       
   285             {
       
   286             TPtr ptr( fullPath->Des() );
       
   287             ptr.Copy( pathPtr );
       
   288             if ( pathPtr[ pathLen - 1 ] != KBackslash()[ 0 ] )
       
   289                 {
       
   290                 ptr.Append( KBackslash ); // Add backslash before name
       
   291                 }
       
   292             ptr.Append( aName );
       
   293             RFs fs;
       
   294             TInt connectError = fs.Connect();
       
   295             if ( connectError == KErrNone )
       
   296             {
       
   297                 ret = fs.IsValidName( ptr );
       
   298                 fs.Close();
       
   299             }
       
   300             delete fullPath;
       
   301             }
       
   302         }
       
   303     return ret;
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CMPXFileNameQueryDlg::IllegalChars
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 TBool CMPXFileNameQueryDlg::IllegalChars( const TDesC& aName ) const
       
   311     {
       
   312     MPX_FUNC( "CMPXFileNameQueryDlg::IllegalChars" );
       
   313     for ( TInt i( 0 ); KIllegalChars[ i ]; i++ )
       
   314         {
       
   315         if ( aName.Locate( KIllegalChars[ i ] ) != KErrNotFound )
       
   316             {
       
   317             return ETrue;
       
   318             }
       
   319         }
       
   320     return EFalse;
       
   321     }
       
   322 
       
   323 //  End of File