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