photosgallery/viewframework/commandhandlers/commoncommandhandlers/src/glxcommandhandlernewmedia.cpp
changeset 0 4e91876724a2
child 2 7d9067c6fcb1
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     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:    Delete command handler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  * @internal reviewed 06/06/2007 by Dave Schofield
       
    23  */
       
    24 
       
    25 #include "glxcommandhandlernewmedia.h"
       
    26 
       
    27 #include <AknUtils.h>
       
    28 #include <badesca.h>
       
    29 #include <bautils.h>
       
    30 #include <glxattributecontext.h>
       
    31 #include <glxattributeretriever.h>
       
    32 #include <glxcollectiongeneraldefs.h>
       
    33 #include <glxcommandfactory.h>
       
    34 #include <glxcommandhandlers.hrh>
       
    35 #include <glxfetchcontextremover.h>
       
    36 #include <glxgeneraluiutilities.h>
       
    37 #include <glxpanic.h>
       
    38 #include <glxresourceutilities.h>                // for CGlxResourceUtilities
       
    39 #include <glxsetappstate.h>
       
    40 #include <glxtextentrypopup.h>
       
    41 #include <glxuistd.h>
       
    42 #include <glxuiutilities.rsg>
       
    43 #include <mglxmedialist.h>
       
    44 #include <mpxcollectionpath.h>
       
    45 #include <mpxcommonframeworkdefs.h>
       
    46 #include <StringLoader.h>
       
    47 
       
    48 #include <data_caging_path_literals.hrh>
       
    49 #include <glxuiutilities.rsg>
       
    50 
       
    51 const TInt KMaxMediaPopupTitleLength = 0x28;    // Accepts only 40 characters
       
    52 const TInt KMaxNewMediaItemTitleLength = 0x28;  // Accepts only 40 characters
       
    53 const TInt KMaxNumberLength = 10;
       
    54 
       
    55 _LIT(KOpenBracket, "(");
       
    56 _LIT(KCloseBracket, ")");
       
    57 _LIT(KFileNameFormatString, "(%+02u)");
       
    58 // ---------------------------------------------------------------------------
       
    59 // Two-phased constructor.
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C CGlxCommandHandlerNewMedia* CGlxCommandHandlerNewMedia::NewL(MGlxMediaListProvider* aMediaListProvider)
       
    63     {
       
    64     CGlxCommandHandlerNewMedia* self = new (ELeave) CGlxCommandHandlerNewMedia(aMediaListProvider);
       
    65     CleanupStack::PushL(self);
       
    66     self->ConstructL();
       
    67     CleanupStack::Pop(self);
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // C++ default constructor can NOT contain any code, that
       
    73 // might leave.
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CGlxCommandHandlerNewMedia::CGlxCommandHandlerNewMedia(MGlxMediaListProvider* aMediaListProvider)
       
    77         : CGlxMpxCommandCommandHandler(aMediaListProvider)
       
    78     {
       
    79     // Don't do anything.
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CGlxCommandHandlerNewMedia::DoHandleCommandCompleteL
       
    84 // -----------------------------------------------------------------------------
       
    85 //	
       
    86 void CGlxCommandHandlerNewMedia::DoHandleCommandCompleteL(TAny* /*aSessionId*/, CMPXCommand* aCommandResult, 
       
    87             							TInt aError, MGlxMediaList* /*aList*/)
       
    88 	{
       
    89 	if (aError == KErrNone && aCommandResult && aCommandResult->IsSupported(KMPXMediaGeneralId))
       
    90     	{	
       
    91     	iNewMediaId = TGlxMediaId(aCommandResult->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
    92     	iOkToExit = EFalse; // wait for media id to be added to the list 
       
    93                                 // (unless we are using iSchedulerWait)
       
    94     	}	
       
    95 
       
    96 	if (iSchedulerWait && aError != KErrAlreadyExists )
       
    97 		{
       
    98 	    // if iSchedulerWait exists then we know the command is being executed from
       
    99 	    // the ExecuteLD() method.
       
   100 		iNewMediaCreationError = aError;
       
   101 		iSchedulerWait->AsyncStop();
       
   102 		}
       
   103 	}
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CGlxCommandHandlerNewMedia::OkToExit
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C TBool CGlxCommandHandlerNewMedia::OkToExit() const
       
   110     {
       
   111     return iOkToExit;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // Symbian 2nd phase constructor can leave.
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CGlxCommandHandlerNewMedia::ConstructL()
       
   119     {
       
   120     iFileNameAlreadyExists = EFalse ;
       
   121     // Load resource file
       
   122 	TParse parse;
       
   123     parse.Set(KGlxUiUtilitiesResource, &KDC_APP_RESOURCE_DIR, NULL);
       
   124     TFileName resourceFile;
       
   125     resourceFile.Append(parse.FullName());
       
   126     CGlxResourceUtilities::GetResourceFilenameL(resourceFile);  
       
   127    	iResourceOffset = CCoeEnv::Static()->AddResourceFileL(resourceFile);
       
   128 
       
   129     iAsyncFocuser = new (ELeave) CGlxAsyncFocuser(this);
       
   130    	// Add supported command
       
   131    	TCommandInfo info(EGlxCmdAddMedia);
       
   132    	// Filter out static items
       
   133    	AddCommandL(info);
       
   134 	}
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // Destructor
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C CGlxCommandHandlerNewMedia::~CGlxCommandHandlerNewMedia()
       
   141     {
       
   142     if ( iResourceOffset )
       
   143         {
       
   144         CCoeEnv::Static()->DeleteResourceFile(iResourceOffset);
       
   145         }
       
   146     
       
   147     delete iSchedulerWait;
       
   148     delete iAsyncFocuser;
       
   149     delete iNewMediaItemTitle;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CGlxCommandHandlerNewMedia::ExecuteLD()
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C TInt CGlxCommandHandlerNewMedia::ExecuteLD(TGlxMediaId& aNewMediaId)
       
   157 	{
       
   158 	CleanupStack::PushL(this);
       
   159 	iSchedulerWait = new (ELeave) CActiveSchedulerWait();
       
   160 	
       
   161 	CGlxMpxCommandCommandHandler::ExecuteL( EGlxCmdAddMedia );
       
   162 	if (iNewMediaCreationError == KErrNone)
       
   163 		{
       
   164 		// The user pressed OK on the dialog. We need to wait for DoHandleCommandComplete()	
       
   165 		iSchedulerWait->Start();
       
   166 		
       
   167 		if (iNewMediaCreationError == KErrNone)
       
   168 			{
       
   169 			aNewMediaId = iNewMediaId;
       
   170 			}
       
   171 		}
       
   172 	
       
   173 	TInt error = iNewMediaCreationError;
       
   174 	CleanupStack::PopAndDestroy(this);
       
   175 	return error;
       
   176 	}
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // Create an add to container command
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 CMPXCommand* CGlxCommandHandlerNewMedia::CreateCommandL(TInt /*aCommandId*/, 
       
   183         MGlxMediaList& aMediaList, TBool& /*aConsume*/) const
       
   184     {
       
   185     iOkToExit = ETrue;
       
   186     
       
   187     CMPXCollectionPath* path = aMediaList.PathLC( NGlxListDefs::EPathParent );
       
   188     CMPXCommand* command = NULL;
       
   189 
       
   190     TBuf <KMaxNewMediaItemTitleLength> defaultNewMediaItemTitle;
       
   191     
       
   192     TitlesL(TGlxMediaId(path->Id(0)), defaultNewMediaItemTitle);
       
   193     
       
   194     HBufC* mediaPopupTitle = StringLoader::LoadLC(R_GLX_PROMPT_NAME);
       
   195     if(iFileNameAlreadyExists)
       
   196 	    {
       
   197 	    iFileNameAlreadyExists = EFalse ;
       
   198 	    }
       
   199 	else
       
   200 	    {
       
   201    	    delete iNewMediaItemTitle;
       
   202     	iNewMediaItemTitle = NULL;
       
   203     	iNewMediaItemTitle = GenerateNewMediaItemTitleL(defaultNewMediaItemTitle, aMediaList);
       
   204 	    }
       
   205     TPtr newMediaItemTitleDes = iNewMediaItemTitle->Des();
       
   206     
       
   207     CGlxTextEntryPopup* dialog = CGlxTextEntryPopup::NewL(*mediaPopupTitle, newMediaItemTitleDes);
       
   208     
       
   209     
       
   210     if(dialog->ExecuteLD() == EEikBidOk)
       
   211     	{
       
   212         command = TGlxCommandFactory::AddContainerCommandLC(*iNewMediaItemTitle, path->Id(0));
       
   213         CleanupStack::Pop(command);
       
   214     	}
       
   215     else
       
   216     	{
       
   217     	iNewMediaCreationError = KErrCancel;
       
   218     	}
       
   219     	
       
   220     CleanupStack::PopAndDestroy(mediaPopupTitle);
       
   221     CleanupStack::PopAndDestroy(path);
       
   222     return command;
       
   223     }
       
   224 
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // CGlxCommandHandlerNewMedia::HandleErrorL
       
   228 // ---------------------------------------------------------------------------
       
   229 // 
       
   230 void CGlxCommandHandlerNewMedia::HandleErrorL(TInt aError) 
       
   231 	{
       
   232 	if (aError == KErrAlreadyExists && iNewMediaItemTitle)
       
   233 		{
       
   234 		HBufC* info = StringLoader::LoadLC(R_GLX_NAME_ALREADY_USED, *iNewMediaItemTitle);
       
   235 		GlxGeneralUiUtilities::ShowInfoNoteL(*info, ETrue);
       
   236 		CleanupStack::PopAndDestroy(info);
       
   237 		iFileNameAlreadyExists = ETrue ;
       
   238 		// As the new Item name already existing , re-execute the command to show entry pop-up
       
   239 		CGlxMpxCommandCommandHandler::ExecuteL( EGlxCmdAddMedia );
       
   240 		if (iSchedulerWait && iNewMediaCreationError == KErrCancel )
       
   241 			{
       
   242 		    // if iSchedulerWait exists then we know the command is being executed from
       
   243 		    // the ExecuteLD() method.
       
   244 			// Stop the schedulerwait loop if dialog is cancelled after starting the schedulerwait loop
       
   245 			iSchedulerWait->AsyncStop();
       
   246 			}
       
   247 		}
       
   248 	else
       
   249 		{
       
   250 	    // Use default error handler
       
   251 	    CGlxMpxCommandCommandHandler::HandleErrorL(aError);
       
   252 		}
       
   253     }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // BypassFiltersForExecute
       
   257 // -----------------------------------------------------------------------------
       
   258 //	
       
   259 EXPORT_C TBool CGlxCommandHandlerNewMedia::BypassFiltersForExecute() const
       
   260     {
       
   261     // if iSchedulerWait exists then we know the command is being executed from
       
   262     // the ExecuteLD() method and filtering is not required.
       
   263     return iSchedulerWait != NULL;
       
   264     }
       
   265     
       
   266 // ---------------------------------------------------------------------------
       
   267 // TitlesL fetches the 'media popup title' and 'default new media item title'
       
   268 // from the collection.
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 
       
   272 void CGlxCommandHandlerNewMedia::TitlesL(const TGlxMediaId aCollectionId, TDes& aDefaultNewMediaItemTitle) const
       
   273 	{
       
   274     CMPXCollectionPath* path = CMPXCollectionPath::NewL();
       
   275     CleanupStack::PushL(path);
       
   276     MGlxMediaList* rootList = MGlxMediaList::InstanceL(*path);
       
   277     CleanupClosePushL(*rootList);
       
   278 
       
   279     TGlxSpecificIdIterator iter(KGlxIdSpaceIdRoot, aCollectionId);
       
   280     CGlxAttributeContext* attributeContext = new (ELeave) CGlxAttributeContext(&iter);
       
   281     CleanupStack::PushL(attributeContext);
       
   282     attributeContext->AddAttributeL(KGlxMediaCollectionPluginSpecificDefaultMediaTitle);
       
   283     rootList->AddContextL(attributeContext, KGlxFetchContextPriorityBlocking);
       
   284     
       
   285     TGlxFetchContextRemover contextRemover(attributeContext, *rootList);
       
   286     // put to cleanupstack as cleanupstack is emptied before stack objects
       
   287     // are deleted
       
   288     CleanupClosePushL( contextRemover );
       
   289     	User::LeaveIfError(GlxAttributeRetriever::RetrieveL(*attributeContext, *rootList, ETrue));
       
   290     // context off the list
       
   291     CleanupStack::PopAndDestroy( &contextRemover );
       
   292     	
       
   293         TInt index =  rootList->Index(KGlxIdSpaceIdRoot, aCollectionId);
       
   294         
       
   295         __ASSERT_DEBUG(index != KErrNotFound, Panic(EGlxPanicRequiredItemNotFound));
       
   296         
       
   297     	TGlxMedia  item =  rootList->Item(index);
       
   298     	
       
   299     	const CGlxMedia* media = item.Properties();
       
   300     	if (media)
       
   301     		{
       
   302     		aDefaultNewMediaItemTitle.Copy(media->ValueText(KGlxMediaCollectionPluginSpecificDefaultMediaTitle).Left(KMaxMediaPopupTitleLength));
       
   303     		}
       
   304 
       
   305 	CleanupStack::PopAndDestroy(attributeContext);
       
   306 	CleanupStack::PopAndDestroy(rootList);
       
   307     CleanupStack::PopAndDestroy(path);
       
   308 	}
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // TitlesL fetches the 'media popup title' and 'default new media item title'
       
   312 // from the collection.
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 HBufC* CGlxCommandHandlerNewMedia::GenerateNewMediaItemTitleL
       
   316                                    (const TDesC& aDefaultNewMediaItemTitle, MGlxMediaList& aList) const
       
   317     {        
       
   318     TGlxSequentialIterator iter;
       
   319     CGlxAttributeContext* attributeContext = new (ELeave) CGlxAttributeContext(&iter);
       
   320     CleanupStack::PushL(attributeContext);
       
   321     attributeContext->AddAttributeL(KMPXMediaGeneralTitle);
       
   322     aList.AddContextL(attributeContext, KGlxFetchContextPriorityBlocking);
       
   323     TGlxFetchContextRemover contextRemover(attributeContext, aList);
       
   324     // put to cleanupstack as cleanupstack is emptied before stack objects
       
   325     // are deleted
       
   326     CleanupClosePushL( contextRemover );
       
   327     User::LeaveIfError(GlxAttributeRetriever::RetrieveL(*attributeContext, aList, ETrue));
       
   328     // context off the list
       
   329     CleanupStack::PopAndDestroy( &contextRemover );
       
   330     CleanupStack::PopAndDestroy(attributeContext);
       
   331     
       
   332     RArray<TInt> numbers;
       
   333     CleanupClosePushL(numbers);
       
   334     
       
   335     TInt count = aList.Count();
       
   336     for (TInt i = 0; i < count; i++)
       
   337         {
       
   338         TGlxMedia  item =  aList.Item(i);    
       
   339         const CGlxMedia* media = item.Properties();
       
   340         if (media)
       
   341             {
       
   342             const TDesC& title = media->ValueText(KMPXMediaGeneralTitle);
       
   343             
       
   344             TInt length = aDefaultNewMediaItemTitle.Length();
       
   345             if (title.Left(length).Compare(aDefaultNewMediaItemTitle) == 0)
       
   346                 {
       
   347                 if (length == title.Length())
       
   348                     {
       
   349                     numbers.InsertInOrder(0); // special case
       
   350                     }
       
   351                 else if(title.Length() > length + KOpenBracket().Length() + KCloseBracket().Length())
       
   352                     {
       
   353                     TInt pos = length;
       
   354                     length = KOpenBracket().Length();
       
   355                     
       
   356                     if (title.Mid(pos, length).Compare(KOpenBracket) == 0 && 
       
   357                         title.Right(KCloseBracket().Length()).Compare(KCloseBracket) == 0)
       
   358                         {
       
   359                         pos += length;
       
   360                         length = title.Length() - pos - KCloseBracket().Length();
       
   361                         if (length > 0)
       
   362                             {
       
   363                             TLex lex = title.Mid(pos, length);
       
   364                             TInt val = 0;
       
   365                             if (lex.Val(val) == KErrNone)
       
   366                                 {
       
   367                                 numbers.InsertInOrder(val);
       
   368                                 }
       
   369                             }
       
   370                         }
       
   371                     }
       
   372                
       
   373                 }
       
   374             }
       
   375         }
       
   376     
       
   377     TInt nextNumber = 0;
       
   378     count = numbers.Count();
       
   379     for (TInt i = 0; i < count; i++)
       
   380         {
       
   381         if (numbers[i] == nextNumber)
       
   382             {
       
   383             nextNumber++;
       
   384             }
       
   385         else
       
   386             {
       
   387             break;
       
   388             }
       
   389         }
       
   390     
       
   391     CleanupStack::PopAndDestroy(&numbers);
       
   392     
       
   393     TInt defaultTitleLength = aDefaultNewMediaItemTitle.Length() + KFileNameFormatString().Length() + KCloseBracket().Length() + KMaxNumberLength;
       
   394     // If the default title length is bigger than KMaxMediaPopupTitleLength, make sure we allocate enough space for it.
       
   395     TInt titleLength  = defaultTitleLength > KMaxMediaPopupTitleLength ? defaultTitleLength : KMaxMediaPopupTitleLength;
       
   396     HBufC* newMediaItemTitle = HBufC::NewL(titleLength);
       
   397     TPtr newMediaItemTitleDes = newMediaItemTitle->Des();
       
   398     newMediaItemTitleDes.Append(aDefaultNewMediaItemTitle);
       
   399     
       
   400     if (nextNumber > 0)
       
   401         {
       
   402         newMediaItemTitleDes.AppendFormat(KFileNameFormatString,nextNumber);
       
   403         }
       
   404     else
       
   405         {
       
   406         // 0 is a special case, return "New Media", not "New Media (0)"
       
   407         }
       
   408     
       
   409     return newMediaItemTitle;
       
   410     }
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // SetFocusL()
       
   414 // -----------------------------------------------------------------------------
       
   415 // 
       
   416 void CGlxCommandHandlerNewMedia::SetFocusL(TInt aIndex)
       
   417     {
       
   418     iOkToExit = ETrue;
       
   419     MediaList().SetFocusL(NGlxListDefs::EAbsolute, aIndex);
       
   420     TryExitL(KErrNone);
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // HandleItemAddedL
       
   425 // -----------------------------------------------------------------------------
       
   426 //  
       
   427 EXPORT_C void CGlxCommandHandlerNewMedia::HandleItemAddedL(TInt aStartIndex, TInt aEndIndex, MGlxMediaList* aList)
       
   428     {
       
   429     if(aList == &MediaList() && iNewMediaId != KGlxCollectionRootId)
       
   430         {
       
   431         for (TInt i = aStartIndex; i <= aEndIndex; i++)
       
   432             {
       
   433             if (aList->Item(i).Id() == iNewMediaId)
       
   434                 {
       
   435                 iAsyncFocuser->SetFocus(i); // calls CGlxCommandHandlerNewMedia::SetFocusL asynchronously
       
   436                 break;
       
   437                 }
       
   438             }
       
   439         }
       
   440     }
       
   441 
       
   442 // -----------------------------------------------------------------------------
       
   443 // Constructor
       
   444 // -----------------------------------------------------------------------------
       
   445 //  
       
   446 CGlxCommandHandlerNewMedia::CGlxAsyncFocuser::
       
   447     CGlxAsyncFocuser(CGlxCommandHandlerNewMedia* aGlxCommandHandlerNewMedia)
       
   448     : CActive(KMaxTInt), iGlxCommandHandlerNewMedia(aGlxCommandHandlerNewMedia)
       
   449     // The active object has the maximum possible priority to prevent other active objects
       
   450     // running before it. (Unless they too are scheduled to run and have the maximum
       
   451     // possible priority
       
   452     {
       
   453     __ASSERT_DEBUG(aGlxCommandHandlerNewMedia, Panic(EGlxPanicNullPointer));
       
   454     CActiveScheduler::Add(this);
       
   455     }
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // Destructor
       
   459 // -----------------------------------------------------------------------------
       
   460 //  
       
   461 CGlxCommandHandlerNewMedia::CGlxAsyncFocuser::
       
   462     ~CGlxAsyncFocuser()
       
   463     {
       
   464     Cancel();
       
   465     }
       
   466 
       
   467 // -----------------------------------------------------------------------------
       
   468 // RunL
       
   469 // -----------------------------------------------------------------------------
       
   470 //  
       
   471 void CGlxCommandHandlerNewMedia::CGlxAsyncFocuser::RunL()
       
   472     {
       
   473     iGlxCommandHandlerNewMedia->SetFocusL(iFocusIndex);
       
   474     }
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // DoCancel
       
   478 // -----------------------------------------------------------------------------
       
   479 //  
       
   480 void CGlxCommandHandlerNewMedia::CGlxAsyncFocuser::DoCancel()
       
   481     {
       
   482     // No need to do anything 
       
   483     // CActive::Cancel() will wait for the request to complete
       
   484     }
       
   485 
       
   486 // -----------------------------------------------------------------------------
       
   487 // SetFocusL
       
   488 // -----------------------------------------------------------------------------
       
   489 //  
       
   490 void CGlxCommandHandlerNewMedia::CGlxAsyncFocuser::SetFocus(TInt aIndex)
       
   491     {
       
   492     iFocusIndex = aIndex;
       
   493     TRequestStatus* requestStatus = &iStatus;
       
   494     User::RequestComplete(requestStatus,KErrNone);
       
   495     SetActive();
       
   496     }