ui/commandhandlers/commoncommandhandlers/src/glxcommandhandlerrename.cpp
branchRCL_3
changeset 59 8e5f6eea9c9f
equal deleted inserted replaced
57:ea65f74e6de4 59:8e5f6eea9c9f
       
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <hbmessagebox.h>
       
    19 #include <hblabel.h>
       
    20 
       
    21 #include <mpxcollectionpath.h>
       
    22 #include <mglxmedialist.h>
       
    23 #include <glxcommandfactory.h>
       
    24 #include <glxcommandhandlerrename.h>
       
    25 #include <glxattributecontext.h>
       
    26 #include <glxattributeretriever.h>
       
    27 #include <glxfetchcontextremover.h>
       
    28 #include <glxcommondialogs.h>
       
    29 #include "glxlocalisationstrings.h"
       
    30 
       
    31 GlxCommandHandlerRename::GlxCommandHandlerRename()
       
    32     {
       
    33     }
       
    34 
       
    35 GlxCommandHandlerRename::~GlxCommandHandlerRename()
       
    36     {
       
    37     }
       
    38 
       
    39 CMPXCommand* GlxCommandHandlerRename::CreateCommandL(TInt aCommandId,
       
    40         MGlxMediaList& aMediaList, TBool& aConsume) const
       
    41     {
       
    42     Q_UNUSED(aCommandId);
       
    43     Q_UNUSED(aConsume);
       
    44     
       
    45     CMPXCommand* command = NULL;
       
    46     QString mainPane = GetName(aMediaList);
       
    47     QString mediaTitle = NULL;
       
    48     bool ok = false;    
       
    49     GlxTextInputDialog* dlg = new GlxTextInputDialog();
       
    50     mediaTitle = dlg->getText(GLX_DIALOG_NAME_PROMPT, mainPane, &ok);
       
    51     delete dlg;
       
    52 
       
    53     if(ok == true)
       
    54         {
       
    55         TPtrC16 newMediaItemTitleDes
       
    56             = (reinterpret_cast<const TUint16*> (mediaTitle.utf16()));
       
    57 
       
    58         HBufC* newMediaItemTitle = newMediaItemTitleDes.Alloc();
       
    59         CleanupStack::PushL(newMediaItemTitle);
       
    60 
       
    61         CMPXCollectionPath* path = aMediaList.PathLC(
       
    62                 NGlxListDefs::EPathFocusOrSelection);
       
    63         command = 
       
    64             TGlxCommandFactory::RenameCommandLC(*newMediaItemTitle,*path);
       
    65         CleanupStack::Pop(command);
       
    66         CleanupStack::PopAndDestroy(path);
       
    67         CleanupStack::PopAndDestroy(newMediaItemTitle);
       
    68         }
       
    69     return command;
       
    70     }
       
    71 
       
    72 QString GlxCommandHandlerRename::CompletionTextL() const
       
    73     {
       
    74 	//return command completion text
       
    75 	return QString();
       
    76     }
       
    77 
       
    78 QString GlxCommandHandlerRename::GetName(MGlxMediaList& aMediaList) const
       
    79     {
       
    80         // Create an iterator to retrieve the relevant attribute
       
    81     TGlxSelectionIterator iterator;
       
    82     // only want the title of one item
       
    83 	iterator.SetRange( 1 );
       
    84     CGlxAttributeContext* context = new( ELeave )
       
    85         CGlxAttributeContext( &iterator );
       
    86 	CleanupStack::PushL( context );
       
    87         
       
    88 	// Want to read the title attribute    
       
    89     // Configure the context
       
    90 	context->AddAttributeL( KMPXMediaGeneralTitle );
       
    91 	
       
    92 	// Add the context to the media list
       
    93 	aMediaList.AddContextL( context, KGlxFetchContextPriorityBlocking );
       
    94 
       
    95     // TGlxContextRemover will remove the context when it goes out of scope
       
    96     // Used here to avoid a trap and still have safe cleanup    
       
    97     TGlxFetchContextRemover contextRemover( context, aMediaList );
       
    98     // put to cleanupstack as cleanupstack is emptied before stack objects
       
    99     // are deleted
       
   100     CleanupClosePushL( contextRemover );
       
   101 	// Yuck - a leave and a return code!
       
   102 	// EFalse => don't show a progress dialog
       
   103     TInt err = GlxAttributeRetriever::RetrieveL( *context, aMediaList, EFalse );
       
   104     // Using a distinct error value as "LeaveIfError( FuncL() );" looks bad
       
   105     User::LeaveIfError( err );
       
   106     // context off the list
       
   107     CleanupStack::PopAndDestroy( &contextRemover );
       
   108 
       
   109     // Now get the title of the relevant item: if *an* item is selected use its
       
   110     // title, otherwise use the item with focus
       
   111 	// Get the index of the item to rename
       
   112     TInt index = KErrNotFound;
       
   113     QString title = NULL;
       
   114 	// first see if there's a selected item...
       
   115     if ( aMediaList.SelectionCount() == 1 )
       
   116     	{
       
   117         // Find the index of the selected item
       
   118     	index = aMediaList.Count();
       
   119     	do
       
   120             {
       
   121             --index;
       
   122             }
       
   123         while ( !aMediaList.IsSelected( index ) && index > 0 );
       
   124     	}
       
   125     else
       
   126     	{
       
   127     	// Use the index of the item with focus
       
   128        	index = aMediaList.FocusIndex();
       
   129     	}
       
   130 
       
   131 	if ( index != KErrNotFound )
       
   132 		{
       
   133         // use iterator to get the right item
       
   134         iterator.SetToFirst( &aMediaList );
       
   135         const CGlxMedia* media = aMediaList.Item( iterator++ ).Properties();
       
   136 		if( media )
       
   137 			{
       
   138 			// found the item's media properties, extract the title
       
   139             const TDesC& titleDesc = media->ValueText(KMPXMediaGeneralTitle);
       
   140             title = QString::fromUtf16(titleDesc.Ptr(),
       
   141                     titleDesc.Length());
       
   142 			}
       
   143 		}
       
   144     CleanupStack::PopAndDestroy( context );
       
   145 
       
   146     return title;
       
   147     }
       
   148 
       
   149 void GlxCommandHandlerRename::HandleErrorL(TInt aErrorCode)
       
   150 	{
       
   151     if(aErrorCode == KErrAlreadyExists)
       
   152         {
       
   153         HbMessageBox::warning("Name Already Exist!!!", new HbLabel(
       
   154                 "Rename"));
       
   155         }
       
   156 	else{
       
   157 		GlxMpxCommandHandler::HandleErrorL(aErrorCode);
       
   158 		}	
       
   159 	}
       
   160