|
1 /* |
|
2 * Copyright (c) 2008-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: Rename command handler |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include "glxcommandhandlerrename.h" |
|
22 |
|
23 #include <bautils.h> |
|
24 #include <data_caging_path_literals.hrh> |
|
25 #include <StringLoader.h> |
|
26 |
|
27 #include <glxuiutilities.rsg> |
|
28 #include <mglxmedialist.h> |
|
29 #include <glxuistd.h> |
|
30 #include <glxcommandhandlers.hrh> |
|
31 #include <glxuiutility.h> |
|
32 #include <glxtextentrypopup.h> |
|
33 #include <glxattributeretriever.h> |
|
34 #include <glxattributecontext.h> |
|
35 #include <glxfetchcontextremover.h> |
|
36 #include <glxresourceutilities.h> // for CGlxResourceUtilities |
|
37 #include <glxgeneraluiutilities.h> |
|
38 #include <glxicons.mbg> |
|
39 #include <glxtracer.h> |
|
40 #include <glxlog.h> |
|
41 #include <glxmediageneraldefs.h> // for KMaxMediaPopupTextLength |
|
42 |
|
43 #include "glxcommandfactory.h" |
|
44 |
|
45 |
|
46 // --------------------------------------------------------------------------- |
|
47 // Two-phased constructor. |
|
48 // --------------------------------------------------------------------------- |
|
49 // |
|
50 EXPORT_C CGlxCommandHandlerRename* CGlxCommandHandlerRename::NewL( |
|
51 MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem, |
|
52 const TDesC& aFileName) |
|
53 { |
|
54 CGlxCommandHandlerRename* self = new (ELeave) CGlxCommandHandlerRename( |
|
55 aMediaListProvider, aHasToolbarItem); |
|
56 CleanupStack::PushL(self); |
|
57 self->ConstructL(aFileName); |
|
58 CleanupStack::Pop(self); |
|
59 return self; |
|
60 } |
|
61 |
|
62 // --------------------------------------------------------------------------- |
|
63 // C++ default constructor can NOT contain any code, that |
|
64 // might leave. |
|
65 // --------------------------------------------------------------------------- |
|
66 // |
|
67 CGlxCommandHandlerRename::CGlxCommandHandlerRename( MGlxMediaListProvider* |
|
68 aMediaListProvider, TBool aHasToolbarItem ) |
|
69 : CGlxMpxCommandCommandHandler( aMediaListProvider, aHasToolbarItem ) |
|
70 { |
|
71 // Do nothing |
|
72 } |
|
73 |
|
74 |
|
75 // --------------------------------------------------------------------------- |
|
76 // Symbian 2nd phase constructor can leave. |
|
77 // --------------------------------------------------------------------------- |
|
78 // |
|
79 void CGlxCommandHandlerRename::ConstructL(const TDesC& aFileName) |
|
80 { |
|
81 iRenameText = HBufC::NewL(KMaxMediaPopupTextLength); |
|
82 |
|
83 iResourceOffset = CCoeEnv::Static()->AddResourceFileL(aFileName); |
|
84 |
|
85 // Add supported command |
|
86 TCommandInfo info(EGlxCmdRename); |
|
87 // This setup disables the command if a static item is focused, |
|
88 // if the view is empty or if more than one item is selected. |
|
89 info.iMinSelectionLength = 1; |
|
90 info.iMaxSelectionLength = 1; |
|
91 // Filter out system items |
|
92 info.iDisallowSystemItems = ETrue; |
|
93 // Allow animated GIFs to be renamed |
|
94 info.iStopAnimationForExecution = ETrue; |
|
95 |
|
96 AddCommandL(info); |
|
97 |
|
98 iErrorCallback = new (ELeave) CAsyncCallBack(TCallBack(HandleErrorL, this), |
|
99 CActive::EPriorityStandard); |
|
100 } |
|
101 |
|
102 // --------------------------------------------------------------------------- |
|
103 // Destructor |
|
104 // --------------------------------------------------------------------------- |
|
105 // |
|
106 EXPORT_C CGlxCommandHandlerRename::~CGlxCommandHandlerRename() |
|
107 { |
|
108 delete iRenameText; |
|
109 |
|
110 iErrorCallback->Cancel(); |
|
111 delete iErrorCallback; |
|
112 |
|
113 if ( iResourceOffset ) |
|
114 { |
|
115 CCoeEnv::Static()->DeleteResourceFile( iResourceOffset ); |
|
116 } |
|
117 } |
|
118 |
|
119 // --------------------------------------------------------------------------- |
|
120 // Create a rename command |
|
121 // --------------------------------------------------------------------------- |
|
122 // |
|
123 CMPXCommand* CGlxCommandHandlerRename::CreateCommandL(TInt /*aCommandId*/, |
|
124 MGlxMediaList& aMediaList, TBool& /*aConsume*/) const |
|
125 { |
|
126 TRACER( "CGlxCommandHandlerRename::CreateCommandL" ); |
|
127 |
|
128 // Return value |
|
129 CMPXCommand* command = NULL; |
|
130 |
|
131 // Prompt the user for the new name |
|
132 // |
|
133 // Show popup with the item's current title |
|
134 TPtr textPtr = iRenameText->Des(); |
|
135 GetTitleL( textPtr, aMediaList ); |
|
136 // store the current name. |
|
137 TBuf<KMaxMediaPopupTextLength> currentName; |
|
138 currentName.Copy(textPtr); |
|
139 |
|
140 // Load the title for the popup |
|
141 HBufC* title = StringLoader::LoadLC( R_GLX_POPUP_RENAME_TITLE ); |
|
142 CGlxTextEntryPopup* popup = CGlxTextEntryPopup::NewL( *title, |
|
143 textPtr ); |
|
144 if ( popup->ExecuteLD() == EEikBidOk && currentName != *iRenameText) |
|
145 { |
|
146 // Text entry was successful |
|
147 // There can be no selection when renaming, so assume that path contains focused item |
|
148 CMPXCollectionPath* path = aMediaList.PathLC( NGlxListDefs::EPathFocusOrSelection ); |
|
149 // Create the rename command with the relevant text |
|
150 command = TGlxCommandFactory::RenameCommandLC( *iRenameText, *path ); |
|
151 CleanupStack::Pop( command ); |
|
152 CleanupStack::PopAndDestroy( path) ; |
|
153 } |
|
154 |
|
155 CleanupStack::PopAndDestroy( title ); |
|
156 |
|
157 return command; |
|
158 } |
|
159 |
|
160 // ----------------------------------------------------------------------------- |
|
161 // Handle an error |
|
162 // ----------------------------------------------------------------------------- |
|
163 // |
|
164 void CGlxCommandHandlerRename::HandleErrorL(TInt aErrorCode) |
|
165 { |
|
166 TRACER( "CGlxCommandHandlerRename::HandleErrorL" ); |
|
167 |
|
168 // Callback is required because the code to handle the error is blocking |
|
169 // There is only ever one command issued, store the error to handle later |
|
170 iError = aErrorCode; |
|
171 |
|
172 __ASSERT_DEBUG( !iErrorCallback->IsActive(), Panic( EGlxPanicLogicError ) ); |
|
173 iErrorCallback->CallBack(); |
|
174 } |
|
175 |
|
176 // ----------------------------------------------------------------------------- |
|
177 // Callback to handle an error |
|
178 // ----------------------------------------------------------------------------- |
|
179 // |
|
180 TBool CGlxCommandHandlerRename::HandleErrorL(TAny* aPtr) |
|
181 { |
|
182 TRACER( "CGlxCommandHandlerRename::HandleErrorL" ); |
|
183 |
|
184 static_cast<CGlxCommandHandlerRename*>( aPtr )->HandleErrorL(); |
|
185 return 0; |
|
186 } |
|
187 |
|
188 // ----------------------------------------------------------------------------- |
|
189 // Handle an error - e.g. if user inputs a name that already exists. |
|
190 // ----------------------------------------------------------------------------- |
|
191 // |
|
192 void CGlxCommandHandlerRename::HandleErrorL() |
|
193 { |
|
194 TRACER( "CGlxCommandHandlerRename::HandleErrorL" ); |
|
195 |
|
196 // If name already exists, Show rename query and allow the user to try again |
|
197 // See EMYP-7A2E6J |
|
198 if ( iError == KErrAlreadyExists ) |
|
199 { |
|
200 HBufC* text = StringLoader::LoadLC( R_GLX_RENAME_QUERY, *iRenameText ); |
|
201 |
|
202 TBool confirmed = GlxGeneralUiUtilities::ConfirmQueryL( R_GLX_QUERY_OK_CANCEL, *text ); |
|
203 if ( confirmed ) |
|
204 { |
|
205 // Let user have another try at entering the new name |
|
206 ExecuteL( EGlxCmdRename ); |
|
207 } |
|
208 |
|
209 CleanupStack::PopAndDestroy( text ); |
|
210 } |
|
211 else |
|
212 { |
|
213 // Call the base class' implementation |
|
214 CGlxMpxCommandCommandHandler::HandleErrorL( iError ); |
|
215 } |
|
216 } |
|
217 |
|
218 // ----------------------------------------------------------------------------- |
|
219 // Retrieve the current item's title string |
|
220 // ----------------------------------------------------------------------------- |
|
221 // |
|
222 void CGlxCommandHandlerRename::GetTitleL( TDes& aTitle, |
|
223 MGlxMediaList& aList ) const |
|
224 { |
|
225 TRACER( "CGlxCommandHandlerRename::GetTitleL" ); |
|
226 |
|
227 // Set the title to a blank name by default |
|
228 aTitle = KNullDesC; |
|
229 |
|
230 // Create an iterator to retrieve the relevant attribute |
|
231 TGlxSelectionIterator iterator; |
|
232 // only want the title of one item |
|
233 iterator.SetRange( 1 ); |
|
234 CGlxAttributeContext* context = new( ELeave ) |
|
235 CGlxAttributeContext( &iterator ); |
|
236 CleanupStack::PushL( context ); |
|
237 |
|
238 // Want to read the title attribute |
|
239 // Configure the context |
|
240 context->AddAttributeL( KMPXMediaGeneralTitle ); |
|
241 |
|
242 // Add the context to the media list |
|
243 aList.AddContextL( context, KGlxFetchContextPriorityBlocking ); |
|
244 |
|
245 // TGlxContextRemover will remove the context when it goes out of scope |
|
246 // Used here to avoid a trap and still have safe cleanup |
|
247 TGlxFetchContextRemover contextRemover( context, aList ); |
|
248 // put to cleanupstack as cleanupstack is emptied before stack objects |
|
249 // are deleted |
|
250 CleanupClosePushL( contextRemover ); |
|
251 // Yuck - a leave and a return code |
|
252 // EFalse => don't show a progress dialog |
|
253 TInt err = GlxAttributeRetriever::RetrieveL( *context, aList, EFalse ); |
|
254 // Using a distinct error value as "LeaveIfError( FuncL() );" looks bad |
|
255 User::LeaveIfError( err ); |
|
256 // context off the list |
|
257 CleanupStack::PopAndDestroy( &contextRemover ); |
|
258 |
|
259 // Now get the title of the relevant item: if *an* item is selected use its |
|
260 // title, otherwise use the item with focus |
|
261 // Get the index of the item to rename |
|
262 TInt index = KErrNotFound; |
|
263 // first see if there's a selected item... |
|
264 if ( aList.SelectionCount() == 1 ) |
|
265 { |
|
266 // Find the index of the selected item |
|
267 index = aList.Count(); |
|
268 do |
|
269 { |
|
270 --index; |
|
271 } |
|
272 while ( !aList.IsSelected( index ) && index > 0 ); |
|
273 } |
|
274 else |
|
275 { |
|
276 // Use the index of the item with focus |
|
277 index = aList.FocusIndex(); |
|
278 } |
|
279 |
|
280 if ( index != KErrNotFound ) |
|
281 { |
|
282 // use iterator to get the right item |
|
283 iterator.SetToFirst( &aList ); |
|
284 const CGlxMedia* media = aList.Item( iterator++ ).Properties(); |
|
285 if( media ) |
|
286 { |
|
287 // found the item's media properties, extract the title |
|
288 aTitle = media->ValueText( KMPXMediaGeneralTitle ) ; |
|
289 } |
|
290 } |
|
291 CleanupStack::PopAndDestroy( context ); |
|
292 } |
|
293 |
|
294 //----------------------------------------------------------------------------- |
|
295 // PopulateToolbar |
|
296 //----------------------------------------------------------------------------- |
|
297 // |
|
298 void CGlxCommandHandlerRename::PopulateToolbarL() |
|
299 { |
|
300 TRACER( "CGlxCommandHandlerRename::PopulateToolbar" ); |
|
301 } |
|
302 |
|
303 //End of file |