photosgallery/viewframework/commandhandlers/commoncommandhandlers/src/glxmediaselectionpopup.cpp
branchRCL_3
changeset 26 5b3385a43d68
child 27 34937ec34dac
equal deleted inserted replaced
25:8e5f6eea9c9f 26:5b3385a43d68
       
     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:    Generic selection popup class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  * @internal reviewed 06/06/2007 by Dave Schofield
       
    21  * @internal reviewed 13/07/2007 by Aki Vanhatalo
       
    22  */
       
    23 // INCLUDE FILES
       
    24 
       
    25 #include "glxmediaselectionpopup.h"
       
    26 
       
    27 #include <data_caging_path_literals.hrh>// for KDC_APP_RESOURCE_DIR
       
    28 
       
    29 #include <aknconsts.h>
       
    30 #include <AknIconArray.h>
       
    31 #include <aknlists.h>	
       
    32 #include <AknQueryDialog.h>
       
    33 #include <avkon.mbg>
       
    34 #include <bautils.h>
       
    35 #include <eikclbd.h>
       
    36 #include <glxattributecontext.h>
       
    37 #include <glxattributeretriever.h>
       
    38 #include <glxcollectiongeneraldefs.h>
       
    39 #include <glxcommoncommandhandlers.rsg>
       
    40 #include <glxfetchcontextremover.h>
       
    41 #include <glxgeneraluiutilities.h>
       
    42 #include <glxlog.h>
       
    43 #include <glxtracer.h>
       
    44 #include <glxresourceutilities.h>                // for CGlxResourceUtilities
       
    45 #include <glxuistd.h>
       
    46 #include <mpxcollectionmessage.h>
       
    47 #include <mpxmediacontainerdefs.h>
       
    48 #include <mpxmediageneraldefs.h>
       
    49 #include <StringLoader.h>
       
    50 #include <glxuiutility.h>
       
    51 
       
    52 #include "glxcommandhandlernewmedia.h"
       
    53 
       
    54 _LIT(KUnselectedIconIndex, "1\t");
       
    55 _LIT(KAlbumIconIndex, "2\t");
       
    56 _LIT(KBlankIconIndex, "3\t"); // No icon
       
    57 
       
    58 _LIT(KGlxCommonCommandHandlerResource, "glxcommoncommandhandlers.rsc");
       
    59 
       
    60 const TInt KMediaSelectionPopupMediaListHierarchyId = 0x2000A77A;
       
    61 const TInt KIconArrayGranularity = 2;
       
    62 const TInt KNewItemId = 12345;
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // Constructor
       
    66 // ---------------------------------------------------------------------------
       
    67 //   
       
    68 EXPORT_C CGlxMediaListAdaptor::CGlxMediaListAdaptor(
       
    69         const MGlxMediaList* aMediaList, TBool aMultiSelection) :
       
    70     iMediaList(aMediaList), iMultiSelection(aMultiSelection),
       
    71 			iStaticItemSelected(EFalse)
       
    72     {
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // CGlxMediaListAdaptor::SetEnabled()
       
    77 // ---------------------------------------------------------------------------
       
    78 //   
       
    79 void CGlxMediaListAdaptor::SetEnabled(TBool aEnabled)
       
    80     {
       
    81     TRACER("CGlxMediaListAdaptor::SetEnabled");
       
    82     iEnabled = aEnabled;
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CGlxMediaListAdaptor::MediaList()
       
    87 // ---------------------------------------------------------------------------
       
    88 //   
       
    89 const MGlxMediaList* CGlxMediaListAdaptor::MediaList()
       
    90     {
       
    91     TRACER("CGlxMediaListAdaptor::MediaList");
       
    92     return iMediaList;
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CGlxMediaListAdaptor::MultiSelectionEnabled()
       
    97 // ---------------------------------------------------------------------------
       
    98 //   
       
    99 TBool CGlxMediaListAdaptor::MultiSelectionEnabled()
       
   100     {
       
   101     return iMultiSelection;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CGlxMediaListAdaptor::MdcaCount()
       
   106 // ---------------------------------------------------------------------------
       
   107 //   
       
   108 EXPORT_C TInt CGlxMediaListAdaptor::MdcaCount() const
       
   109     {
       
   110     TRACER("CGlxMediaListAdaptor::MdcaCount");
       
   111     __ASSERT_DEBUG(iMediaList, Panic(EGlxPanicNullPointer));
       
   112     if (iEnabled)
       
   113         {
       
   114         return iMediaList->Count();
       
   115         }
       
   116     else
       
   117         {
       
   118         return 0;
       
   119         }
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // CGlxMediaListAdaptor::MdcaPoint()
       
   124 // ---------------------------------------------------------------------------
       
   125 //   
       
   126 EXPORT_C TPtrC CGlxMediaListAdaptor::MdcaPoint(TInt aIndex) const
       
   127     {
       
   128     TRACER("CGlxMediaListAdaptor::MdcaPoint");
       
   129     const TGlxMedia& item = iMediaList->Item(aIndex);
       
   130 
       
   131     TPtrC title = item.Title();
       
   132     if (item.IsStatic() || title.Length() == 0)
       
   133         {
       
   134         // The item is a static item or the item's title length is 0
       
   135         iCurrentTitleString = KBlankIconIndex;
       
   136         }
       
   137     else
       
   138         {
       
   139         if (iMultiSelection)
       
   140             {
       
   141             iCurrentTitleString = KUnselectedIconIndex;
       
   142             }
       
   143         else
       
   144             {
       
   145             iCurrentTitleString = KAlbumIconIndex;
       
   146             }
       
   147         }
       
   148 
       
   149     // iCurrentTitleString contains a tab character
       
   150     iCurrentTitleString.Append(title.Left(KMaxTitleStringLength
       
   151             - iCurrentTitleString.Length()));
       
   152 
       
   153     return TPtrC(iCurrentTitleString);
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // CGlxMediaListAdaptor::IsStaticItemSelected
       
   158 // ---------------------------------------------------------------------------
       
   159 TBool CGlxMediaListAdaptor::IsStaticItemSelected()
       
   160 	{
       
   161 	TRACER("CGlxMediaListAdaptor::IsStaticItemSelected");
       
   162 	return iStaticItemSelected;
       
   163 	}
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CGlxMediaListAdaptor::SetStaticItemSelected
       
   167 // ---------------------------------------------------------------------------
       
   168 void CGlxMediaListAdaptor::SetStaticItemSelected(TBool aSelected)
       
   169 	{
       
   170 	TRACER("CGlxMediaListAdaptor::SetStaticItemSelected");
       
   171 	iStaticItemSelected = aSelected;
       
   172 	}
       
   173 
       
   174 //---------------------------------------------------------------------------
       
   175 // CGlxSingleGraphicPopupMenuStyleList::NewL
       
   176 //---------------------------------------------------------------------------
       
   177 CGlxSingleGraphicPopupMenuStyleList* CGlxSingleGraphicPopupMenuStyleList::NewL(
       
   178 		CEikListBox* aListBox, TInt aCbaResource,
       
   179 		AknPopupLayouts::TAknPopupLayouts aType)
       
   180 	{
       
   181 	TRACER("CGlxSingleGraphicPopupMenuStyleList::NewL");
       
   182 
       
   183 	CGlxSingleGraphicPopupMenuStyleList* self =
       
   184 		new (ELeave) CGlxSingleGraphicPopupMenuStyleList;
       
   185 	CleanupStack::PushL(self);
       
   186 	self->ConstructL(aListBox, aCbaResource, aType);
       
   187 	CleanupStack::Pop(self);
       
   188 	return self;
       
   189 	}
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CGlxSingleGraphicPopupMenuStyleList::HandleListBoxEventL()
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 void CGlxSingleGraphicPopupMenuStyleList::HandleListBoxEventL(
       
   196 		CEikListBox *aListBox, TListBoxEvent aEventType)
       
   197     {
       
   198     TRACER("CGlxSingleGraphicPopupMenuStyleList::HandleListBoxEventL");
       
   199 
       
   200     CAknSingleGraphicPopupMenuStyleListBox* listBox =
       
   201 			static_cast<CAknSingleGraphicPopupMenuStyleListBox*> (aListBox);
       
   202 
       
   203     CGlxMediaListAdaptor
       
   204 			* mediaListAdaptor =
       
   205 					static_cast<CGlxMediaListAdaptor*> (listBox->Model()->ItemTextArray());
       
   206 
       
   207     TBool staticItemSelected = EFalse;
       
   208     TInt currItemIndx = listBox->View()->CurrentItemIndex();
       
   209     GLX_LOG_INFO1("Glx Pop-up listbox - HandleListBoxEventL() currItemIndx=%d",
       
   210             currItemIndx);
       
   211 
       
   212     switch (aEventType)
       
   213         {
       
   214         case EEventItemClicked:
       
   215         case EEventItemSingleClicked:
       
   216             {
       
   217             if (mediaListAdaptor->MultiSelectionEnabled() && currItemIndx
       
   218                     >= 0)
       
   219                 {
       
   220                 const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
       
   221 						currItemIndx);
       
   222 
       
   223                 //Check if media item is user-defined or static
       
   224                 staticItemSelected = item.IsStatic();
       
   225                 mediaListAdaptor->SetStaticItemSelected(staticItemSelected);
       
   226                 if (!staticItemSelected)
       
   227                     {
       
   228 					//Mark or UnMark the user-defined item
       
   229                     TBool isMarked = listBox->View()->ItemIsSelected(
       
   230                             currItemIndx);
       
   231                     (isMarked == (TBool) ETrue) ? (listBox->View()->DeselectItem(
       
   232                                                   currItemIndx))
       
   233                                                 : (listBox->View()->SelectItemL(currItemIndx));
       
   234                     }
       
   235 
       
   236                 //Show Command Set based on selected items
       
   237                 TInt selectCount =
       
   238                         listBox->View()->SelectionIndexes()->Count();
       
   239                 CEikButtonGroupContainer* cbaContainer =
       
   240                         ButtonGroupContainer();
       
   241 				//Show 'OK' only if a static item or more than
       
   242 				//1 user-defined item is selected
       
   243                 if (staticItemSelected || selectCount)
       
   244 					{
       
   245 					cbaContainer->SetCommandSetL(R_GLX_SOFTKEYS_OK_CANCEL);
       
   246 					}
       
   247 				else
       
   248 					{
       
   249 					cbaContainer->SetCommandSetL(R_AVKON_SOFTKEYS_CANCEL);
       
   250 					}
       
   251 				cbaContainer->DrawDeferred();
       
   252 
       
   253                 }
       
   254             listBox->DrawDeferred();
       
   255 
       
   256             //Forward for default processing, if static item is selected(i.e. For Tags)
       
   257 			//or if List Box's Multiple selection is Disabled(i.e. For Albums)
       
   258 			if (staticItemSelected
       
   259 					|| (!mediaListAdaptor->MultiSelectionEnabled()))
       
   260 				{
       
   261                 CAknPopupList::HandleListBoxEventL(aListBox, aEventType);
       
   262 				}
       
   263 
       
   264 			//After Scrolling, then Select "New Tag" i.e Static item is selected
       
   265 			if (staticItemSelected)
       
   266 				{
       
   267 				ProcessCommandL(EAknSoftkeyOk);
       
   268 				}
       
   269 
       
   270             break;
       
   271             }
       
   272         case EEventEnterKeyPressed:
       
   273             {
       
   274             // Check for multiselection is disbaled(i.e. for Albums) 
       
   275             // and valid index
       
   276             if (!(mediaListAdaptor->MultiSelectionEnabled()) && currItemIndx
       
   277                     >= 0)
       
   278                 {
       
   279                 //Set if its a static item
       
   280                 const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
       
   281                         currItemIndx);
       
   282                 mediaListAdaptor->SetStaticItemSelected(item.IsStatic());
       
   283                 }
       
   284             CAknPopupList::HandleListBoxEventL(aListBox, aEventType);
       
   285             break;
       
   286             }
       
   287         default:
       
   288         	{
       
   289             CAknPopupList::HandleListBoxEventL(aListBox, aEventType);
       
   290         	break;
       
   291         	}
       
   292         }
       
   293     }
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // CGlxSingleGraphicPopupMenuStyleList::HandlePointerEventL()
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void CGlxSingleGraphicPopupMenuStyleList::HandlePointerEventL(
       
   300 		const TPointerEvent& aPointerEvent)
       
   301     {
       
   302 	TRACER("CGlxSingleGraphicPopupMenuStyleList::HandlePointerEventL");
       
   303 
       
   304     CAknSingleGraphicPopupMenuStyleListBox* listBox =
       
   305 			static_cast<CAknSingleGraphicPopupMenuStyleListBox*> (ListBox());
       
   306 	CGlxMediaListAdaptor
       
   307 			* mediaListAdaptor =
       
   308 					static_cast<CGlxMediaListAdaptor*> (listBox->Model()->ItemTextArray());
       
   309 
       
   310 	//Check for MultipleSelection is Disbaled
       
   311     if ( !(mediaListAdaptor->MultiSelectionEnabled()) )
       
   312     	{
       
   313 		//checking if the index has changed & has valid item available
       
   314 		TInt changedItemIndex = KErrNotFound;
       
   315         TBool isItemAvailable = listBox->View()->XYPosToItemIndex(
       
   316 				aPointerEvent.iPosition, changedItemIndex);
       
   317         if(isItemAvailable)
       
   318 			{
       
   319 			//Check if Selected item is static
       
   320 			const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
       
   321 					changedItemIndex);
       
   322 			mediaListAdaptor->SetStaticItemSelected( item.IsStatic() );
       
   323 			}
       
   324         }
       
   325     //Forward for default processing
       
   326     CAknPopupList::HandlePointerEventL( aPointerEvent);
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CGlxSingleGraphicPopupMenuStyleListBox::OfferKeyEventL
       
   331 // ---------------------------------------------------------------------------
       
   332 //  
       
   333 TKeyResponse CGlxSingleGraphicPopupMenuStyleListBox::OfferKeyEventL(
       
   334 		const TKeyEvent& aKeyEvent, TEventCode aType)
       
   335 	{
       
   336     TRACER("CGlxSingleGraphicPopupMenuStyleListBox::OfferKeyEventL");
       
   337 
       
   338     //Based on the selected item index, disable the MultipleSelection flag
       
   339     //to stop the flickering of 'marked box', when Highlighted 'New Tag' is selected.
       
   340     CGlxMediaListAdaptor* mediaListAdaptor =
       
   341             static_cast<CGlxMediaListAdaptor*> (Model()->ItemTextArray());
       
   342     TInt currItemIndx = View()->CurrentItemIndex();
       
   343     GLX_LOG_INFO1("Glx Pop-up listbox - OfferKeyEventL(1) currItemIndx=%d",
       
   344             currItemIndx);
       
   345 
       
   346     if (mediaListAdaptor->MultiSelectionEnabled() && currItemIndx >= 0)
       
   347         {
       
   348         const TGlxMedia& oldItem = mediaListAdaptor->MediaList()->Item(
       
   349                 currItemIndx);
       
   350         if (oldItem.IsStatic())
       
   351             {
       
   352             iListBoxFlags &= (~EMultipleSelection); // turn off multiple selection
       
   353             }
       
   354         else
       
   355             {
       
   356             iListBoxFlags |= EMultipleSelection; // turn on multiple selection
       
   357             }
       
   358         }
       
   359 
       
   360     //Forward for default processing
       
   361     TKeyResponse response =
       
   362             CAknSingleGraphicPopupMenuStyleListBox::OfferKeyEventL(aKeyEvent,
       
   363                     aType);
       
   364 
       
   365     currItemIndx = View()->CurrentItemIndex();
       
   366     GLX_LOG_INFO1("Glx Pop-up listbox - OfferKeyEventL(2) currItemIndx=%d",
       
   367             currItemIndx);
       
   368     //Check if 'Enter'/'Ok' key was consumed for 'MultipleSelection' List Box i.e for Tags
       
   369     if ((response == EKeyWasConsumed) && ((aKeyEvent.iCode == EKeyEnter)
       
   370             || (aKeyEvent.iCode == EKeyOK))
       
   371             && mediaListAdaptor->MultiSelectionEnabled() && currItemIndx >= 0)
       
   372         {
       
   373         //current selected item
       
   374         const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
       
   375                 currItemIndx);
       
   376         TBool staticItemSelected = item.IsStatic();
       
   377         mediaListAdaptor->SetStaticItemSelected(staticItemSelected);
       
   378 
       
   379         TInt selectCount = View()->SelectionIndexes()->Count();
       
   380         CEikButtonGroupContainer* cbaContainer =
       
   381                 CEikButtonGroupContainer::Current();
       
   382         //Check if selected Item is static or other item is selected
       
   383         if (staticItemSelected || selectCount)
       
   384             {
       
   385             cbaContainer->SetCommandSetL(R_GLX_SOFTKEYS_OK_CANCEL);
       
   386             }
       
   387         //no item is selected
       
   388         else
       
   389             {
       
   390             cbaContainer->SetCommandSetL(R_AVKON_SOFTKEYS_CANCEL);
       
   391             }
       
   392         cbaContainer->DrawDeferred();
       
   393 
       
   394         //Enter Key is Pressed and Static Item is selected
       
   395         if (staticItemSelected)
       
   396             {
       
   397             //Report 'Enter' key is pressed to ListBox observers
       
   398             ReportListBoxEventL(MEikListBoxObserver::EEventEnterKeyPressed);
       
   399             }
       
   400         }
       
   401 
       
   402     return response;
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // Constructor
       
   407 // ---------------------------------------------------------------------------
       
   408 //  
       
   409 EXPORT_C CGlxMediaSelectionPopup::CGlxMediaSelectionPopup() :
       
   410     CActive(EPriorityStandard)
       
   411     {
       
   412     TRACER("GlxMediaSelectionPopup::CGlxMediaSelectionPopup");
       
   413     CActiveScheduler::Add(this);
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------------------------
       
   417 // CGlxMediaSelectionPopup::RunL()
       
   418 // ---------------------------------------------------------------------------
       
   419 //  
       
   420 void CGlxMediaSelectionPopup::RunL()
       
   421     {
       
   422     TRACER("CGlxMediaSelectionPopup::RunL");
       
   423     // The media list adaptor is enabled after the popup list is invoked
       
   424     // to ensure that the popup list is laid out correctly.
       
   425     iMediaListAdaptor->SetEnabled(ETrue);
       
   426     AddNewMediaCreationItemL();
       
   427     }
       
   428 
       
   429 // ---------------------------------------------------------------------------
       
   430 // CGlxMediaSelectionPopup::DoCancel()
       
   431 // ---------------------------------------------------------------------------
       
   432 //  
       
   433 void CGlxMediaSelectionPopup::DoCancel()
       
   434     {
       
   435     // no implementation required
       
   436     }
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // CGlxMediaSelectionPopup::ExecuteLD()
       
   440 // ---------------------------------------------------------------------------
       
   441 //  
       
   442 EXPORT_C CMPXCollectionPath* CGlxMediaSelectionPopup::ExecuteLD(
       
   443         CMPXCollectionPath& aPath, TBool& aAccepted, TBool aMultiSelection,
       
   444         TBool aEnableContainerCreation, CMPXFilter* aFilter)
       
   445     {
       
   446     TRACER("CGlxMediaSelectionPopup::ExecuteLD");
       
   447     CleanupStack::PushL(this); // if something leaves delete 'this' object
       
   448 
       
   449     iEnableContainerCreation = aEnableContainerCreation;
       
   450 
       
   451     iCollectionId = TGlxMediaId(aPath.Id(0));
       
   452 
       
   453     FetchTitlesL();
       
   454 
       
   455     iMediaList = MGlxMediaList::InstanceL(aPath,
       
   456             KMediaSelectionPopupMediaListHierarchyId, aFilter);
       
   457 
       
   458     iMediaList->AddMediaListObserverL(this);
       
   459 
       
   460     iMediaListAdaptor = new (ELeave) CGlxMediaListAdaptor(iMediaList,
       
   461             aMultiSelection);
       
   462 
       
   463     SetupAttributeContextL();
       
   464 
       
   465     AddResourceFileL();
       
   466     ConstructPopupListL(aMultiSelection);
       
   467     InitIconsL();
       
   468 
       
   469     CompleteSelf(); // This will cause RunL() to be called by the active scheduler.
       
   470 
       
   471     // Invoke the dialog. 
       
   472     // The media list adaptor MdcaCount() method must return 0 at this point in order
       
   473     // for the popup to be displayed correctly. If MdcaCount() returns 1 then the popup will contain only
       
   474     // a single row.
       
   475     // iPopupList->ExecuteLD() starts a nested active scheduler so acvtive objects will be executed before
       
   476     // iPopupList->ExecuteLD() completes.
       
   477     aAccepted = iPopupList->ExecuteLD();
       
   478 
       
   479     if (iListBox->CurrentItemIndex() < 0)
       
   480         {
       
   481         // There is no current item therefore there is nothing to select
       
   482         // Fix for error ID: ERBS-7BSKFV Application crashes if user attempts to add a tag when in full screen view.
       
   483         aAccepted = EFalse;
       
   484         }
       
   485 
       
   486     iMediaList->RemoveMediaListObserver(this); // We no longer require any callbacks from the media list
       
   487 
       
   488 
       
   489     CMPXCollectionPath* path = NULL;
       
   490     if (aAccepted)
       
   491         {
       
   492 
       
   493 		//Check if a static item is selected
       
   494 		if (iMediaListAdaptor->IsStaticItemSelected())
       
   495             {
       
   496 			TFileName uiutilitiesrscfile;
       
   497 			uiutilitiesrscfile.Append(
       
   498 					CGlxResourceUtilities::GetUiUtilitiesResourceFilenameL());
       
   499             CGlxCommandHandlerNewMedia* commandHandlerNewMedia =
       
   500 			CGlxCommandHandlerNewMedia::NewL(this,uiutilitiesrscfile);
       
   501             TGlxMediaId newMediaId;
       
   502             TInt error = commandHandlerNewMedia->ExecuteLD(newMediaId);
       
   503             if (error == KErrNone)
       
   504                 {
       
   505                 path = CMPXCollectionPath::NewL(aPath);
       
   506                 CleanupStack::PopAndDestroy(this);
       
   507                 CleanupStack::PushL(path);
       
   508                 path->AppendL(newMediaId.Value());
       
   509                 CleanupStack::Pop(path);
       
   510                 }
       
   511             else if (error == KErrCancel)
       
   512                 {
       
   513                 // The user has cancelled the popup list
       
   514                 aAccepted = EFalse;
       
   515                 CleanupStack::PopAndDestroy(this);
       
   516                 }
       
   517             else
       
   518                 {
       
   519                 // The error is neither KErrNone or KErrCancel, leave.
       
   520                 User::Leave(error);
       
   521                 }
       
   522 
       
   523             }
       
   524         else
       
   525             {
       
   526             iMediaList->SetFocusL(NGlxListDefs::EAbsolute,
       
   527                     iListBox->CurrentItemIndex());
       
   528 
       
   529             const CListBoxView::CSelectionIndexArray* selectionIndices =
       
   530                     iListBox->SelectionIndexes();
       
   531 
       
   532             TInt count = selectionIndices->Count();
       
   533             for (TInt i = 0; i < count; i++)
       
   534                 {
       
   535                 iMediaList->SetSelectedL(selectionIndices->At(i), ETrue);
       
   536                 }
       
   537 
       
   538             path = iMediaList->PathLC(NGlxListDefs::EPathFocusOrSelection);
       
   539             CleanupStack::Pop(path);
       
   540             CleanupStack::PopAndDestroy(this);
       
   541             }
       
   542         }
       
   543     else
       
   544         {
       
   545         CleanupStack::PopAndDestroy(this);
       
   546         }
       
   547 
       
   548     return path;
       
   549     }
       
   550 
       
   551 // ---------------------------------------------------------------------------
       
   552 // CGlxMediaSelectionPopup::ListBoxItemsChanged()
       
   553 // ---------------------------------------------------------------------------
       
   554 //  
       
   555 void CGlxMediaSelectionPopup::ListBoxItemsChanged(CEikListBox* /*aListBox*/)
       
   556     {
       
   557     // Don't do anything.
       
   558     }
       
   559 
       
   560 // -----------------------------------------------------------------------------
       
   561 // Destructor
       
   562 // -----------------------------------------------------------------------------
       
   563 //
       
   564 CGlxMediaSelectionPopup::~CGlxMediaSelectionPopup()
       
   565     {
       
   566     TRACER("CGlxMediaSelectionPopup::~CGlxMediaSelectionPopup");
       
   567     Cancel();
       
   568 
       
   569     delete iMediaListAdaptor;
       
   570     delete iListBox;
       
   571 
       
   572     if (iResourceOffset)
       
   573         {
       
   574         CCoeEnv::Static()->DeleteResourceFile(iResourceOffset);
       
   575         }
       
   576 
       
   577     if (iMediaList)
       
   578         {
       
   579         if (iAttributeContext)
       
   580             {
       
   581             iMediaList->RemoveContext(iAttributeContext);
       
   582             }
       
   583         iMediaList->RemoveMediaListObserver(this); // This is required in case ExecuteLD() left.
       
   584 
       
   585         iMediaList->Close();
       
   586         }
       
   587 
       
   588     delete iAttributeContext;
       
   589     delete iSelectMediaPopupTitle;
       
   590     delete iNewMediaItemTitle;
       
   591     }
       
   592 
       
   593 // -----------------------------------------------------------------------------
       
   594 // CGlxMediaSelectionPopup::HandleItemAddedL()
       
   595 // -----------------------------------------------------------------------------
       
   596 //
       
   597 void CGlxMediaSelectionPopup::HandleItemAddedL(TInt aStartIndex,
       
   598         TInt aEndIndex, MGlxMediaList* aList)
       
   599     {
       
   600     TRACER("CGlxMediaSelectionPopup::HandleItemAddedL");
       
   601     // The call to HandleItemAdditionL() should be deferred if all of the new items
       
   602     // don't have title attributes as unnecessary calls to  HandleItemAdditionL will 
       
   603     // cause the dialog to flicker.
       
   604     if (iListBox)
       
   605         {
       
   606         TBool handleItemAdditionRequired = EFalse;
       
   607         for (TInt i = aStartIndex; i <= aEndIndex; i++)
       
   608             {
       
   609             TGlxMedia item = aList->Item(i);
       
   610             if (item.Title().Length() > 0)
       
   611             // The title length is greater than 0, i.e. there is a title.
       
   612                 {
       
   613                 handleItemAdditionRequired = ETrue;
       
   614                 break;
       
   615                 }
       
   616             }
       
   617 
       
   618         if (handleItemAdditionRequired)
       
   619             {
       
   620             iListBox->HandleItemAdditionL();
       
   621             }
       
   622         else
       
   623             {
       
   624             iHandleItemAdditionRequired = ETrue; // defer the call until we have title attributes
       
   625             }
       
   626         }
       
   627 
       
   628     CGlxUiUtility* uiUtility = CGlxUiUtility::UtilityL();
       
   629     CleanupClosePushL(*uiUtility);
       
   630     if ((iMediaListAdaptor && !iMediaListAdaptor->MultiSelectionEnabled())
       
   631             || !uiUtility->IsPenSupported())
       
   632         {
       
   633         CEikButtonGroupContainer* cbaContainer =
       
   634                 iPopupList->ButtonGroupContainer();
       
   635         cbaContainer->SetCommandSetL(R_GLX_SOFTKEYS_OK_CANCEL);
       
   636         cbaContainer->DrawDeferred();
       
   637         }
       
   638     CleanupStack::PopAndDestroy(uiUtility);
       
   639 
       
   640     ///@todo if required: update the existing selection when items are added
       
   641     }
       
   642 
       
   643 // -----------------------------------------------------------------------------
       
   644 // CGlxMediaSelectionPopup::HandleMediaL()
       
   645 // -----------------------------------------------------------------------------
       
   646 //
       
   647 void CGlxMediaSelectionPopup::HandleMediaL(TInt /*aListIndex*/,
       
   648         MGlxMediaList* /*aList*/)
       
   649     {
       
   650     // Don't do anything.
       
   651     }
       
   652 
       
   653 // -----------------------------------------------------------------------------
       
   654 // CGlxMediaSelectionPopup::HandleItemRemovedL()
       
   655 // -----------------------------------------------------------------------------
       
   656 //
       
   657 void CGlxMediaSelectionPopup::HandleItemRemovedL(TInt /*aStartIndex*/,
       
   658         TInt /*aEndIndex*/, MGlxMediaList* /*aList*/)
       
   659     {
       
   660     TRACER("CGlxMediaSelectionPopup::HandleItemRemovedL");
       
   661     if (iListBox)
       
   662         {
       
   663         iListBox->HandleItemRemovalL();
       
   664         }
       
   665     }
       
   666 
       
   667 // -----------------------------------------------------------------------------
       
   668 // CGlxMediaSelectionPopup::HandleItemModifiedL()
       
   669 // -----------------------------------------------------------------------------
       
   670 //
       
   671 void CGlxMediaSelectionPopup::HandleItemModifiedL(
       
   672         const RArray<TInt>& aItemIndexes, MGlxMediaList* /*aList*/)
       
   673     {
       
   674     TRACER("CGlxMediaSelectionPopup::HandleItemModifiedL");
       
   675     if (iListBox)
       
   676         {
       
   677         for (TInt i = 0; i < aItemIndexes.Count(); i++)
       
   678             {
       
   679             if (IsListBoxItemVisible(aItemIndexes[i]))
       
   680                 {
       
   681                 iListBox->RedrawItem(aItemIndexes[i]);
       
   682                 }
       
   683             }
       
   684         }
       
   685     }
       
   686 
       
   687 // -----------------------------------------------------------------------------
       
   688 // CGlxMediaSelectionPopup::HandleAttributesAvailableL()
       
   689 // -----------------------------------------------------------------------------
       
   690 //
       
   691 void CGlxMediaSelectionPopup::HandleAttributesAvailableL(TInt aItemIndex,
       
   692         const RArray<TMPXAttribute>& /*aAttributes*/, MGlxMediaList* /*aList*/)
       
   693     {
       
   694     TRACER("CGlxMediaSelectionPopup::HandleAttributesAvailableL");
       
   695     if (iListBox)
       
   696         {
       
   697         if (iHandleItemAdditionRequired)
       
   698             {
       
   699             iListBox->HandleItemAdditionL();
       
   700             iHandleItemAdditionRequired = EFalse;
       
   701             }
       
   702         if (IsListBoxItemVisible(aItemIndex))
       
   703 			{
       
   704             iListBox->RedrawItem(aItemIndex);
       
   705             }
       
   706         }
       
   707     }
       
   708 
       
   709 // -----------------------------------------------------------------------------
       
   710 // CGlxMediaSelectionPopup::HandleFocusChangedL()
       
   711 // -----------------------------------------------------------------------------
       
   712 //
       
   713 void CGlxMediaSelectionPopup::HandleFocusChangedL(
       
   714         NGlxListDefs::TFocusChangeType /*aType*/, TInt /*aNewIndex*/,
       
   715         TInt /*aOldIndex*/, MGlxMediaList* /*aList*/)
       
   716     {
       
   717     // Don't do anything.
       
   718     }
       
   719 
       
   720 // -----------------------------------------------------------------------------
       
   721 // CGlxMediaSelectionPopup::HandleItemSelectedL()
       
   722 // -----------------------------------------------------------------------------
       
   723 //
       
   724 void CGlxMediaSelectionPopup::HandleItemSelectedL(TInt /*aIndex*/,
       
   725         TBool /*aSelected*/, MGlxMediaList* /*aList*/)
       
   726     {
       
   727     // Don't do anything.
       
   728     }
       
   729 
       
   730 // -----------------------------------------------------------------------------
       
   731 // CGlxMediaSelectionPopup::HandleMessageL()
       
   732 // -----------------------------------------------------------------------------
       
   733 //
       
   734 void CGlxMediaSelectionPopup::HandleMessageL(const CMPXMessage& /*aMessage*/,
       
   735         MGlxMediaList* /*aList*/)
       
   736     {
       
   737     // Don't do anything.
       
   738     }
       
   739 
       
   740 // -----------------------------------------------------------------------------
       
   741 // CGlxMediaSelectionPopup::HandleError()
       
   742 // -----------------------------------------------------------------------------
       
   743 //
       
   744 void CGlxMediaSelectionPopup::HandleError(TInt /*aError*/)
       
   745     {
       
   746     ///@todo implement
       
   747     }
       
   748 
       
   749 // -----------------------------------------------------------------------------
       
   750 // CGlxMediaSelectionPopup::MediaList()
       
   751 // -----------------------------------------------------------------------------
       
   752 //
       
   753 MGlxMediaList& CGlxMediaSelectionPopup::MediaList()
       
   754     {
       
   755     // Provides a media list to CGlxCommandHandlerNewMedia
       
   756     return *iMediaList;
       
   757     }
       
   758 
       
   759 // -----------------------------------------------------------------------------
       
   760 // CGlxMediaSelectionPopup::AddResourceFileL()
       
   761 // -----------------------------------------------------------------------------
       
   762 //
       
   763 void CGlxMediaSelectionPopup::AddResourceFileL()
       
   764     {
       
   765     TRACER("CGlxMediaSelectionPopup::AddResourceFileL");
       
   766     // Load resource
       
   767     TParse parse;
       
   768     parse.Set(KGlxCommonCommandHandlerResource, &KDC_APP_RESOURCE_DIR, NULL);
       
   769     TFileName resourceFile;
       
   770     resourceFile.Append(parse.FullName());
       
   771     CGlxResourceUtilities::GetResourceFilenameL(resourceFile);
       
   772     iResourceOffset = CCoeEnv::Static()->AddResourceFileL(resourceFile);
       
   773     }
       
   774 
       
   775 // -----------------------------------------------------------------------------
       
   776 // CGlxMediaSelectionPopup::SetupAttributeContextL()
       
   777 // -----------------------------------------------------------------------------
       
   778 //
       
   779 void CGlxMediaSelectionPopup::SetupAttributeContextL()
       
   780     {
       
   781     TRACER("CGlxMediaSelectionPopup::SetupAttributeContextL");
       
   782     iAttributeContext = new (ELeave) CGlxAttributeContext(&iIterator);
       
   783     iAttributeContext->AddAttributeL(KMPXMediaGeneralTitle);
       
   784     iMediaList->AddContextL(iAttributeContext, KGlxFetchContextPriorityLow);
       
   785     }
       
   786 
       
   787 // -----------------------------------------------------------------------------
       
   788 // CGlxMediaSelectionPopup::SetupAttributeContextL()
       
   789 // -----------------------------------------------------------------------------
       
   790 //
       
   791 void CGlxMediaSelectionPopup::InitIconsL()
       
   792     {
       
   793     TRACER("CGlxMediaSelectionPopup::InitIconsL");
       
   794     CAknIconArray* iconArray = new (ELeave) CAknIconArray(
       
   795             KIconArrayGranularity);
       
   796     CleanupStack::PushL(iconArray);
       
   797 
       
   798     // Sets graphics as ListBox icon.
       
   799     iListBox->ItemDrawer()->ColumnData()->SetIconArray(iconArray);
       
   800 
       
   801     CleanupStack::Pop(iconArray); // iconArray
       
   802 
       
   803     ///@todo use mgallery icons when available
       
   804     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   805     CGulIcon* icon =
       
   806             AknsUtils::CreateGulIconL(skin, KAknsIIDQgnPropCheckboxOn,
       
   807                     KAvkonBitmapFile, EMbmAvkonQgn_prop_checkbox_on,
       
   808                     EMbmAvkonQgn_prop_checkbox_on_mask);
       
   809     CleanupStack::PushL(icon);
       
   810     iconArray->AppendL(icon);
       
   811     CleanupStack::Pop(icon);
       
   812 
       
   813     icon = AknsUtils::CreateGulIconL(skin, KAknsIIDQgnPropCheckboxOff,
       
   814             KAvkonBitmapFile, EMbmAvkonQgn_prop_checkbox_off,
       
   815             EMbmAvkonQgn_prop_checkbox_off_mask);
       
   816     CleanupStack::PushL(icon);
       
   817     iconArray->AppendL(icon);
       
   818     CleanupStack::Pop(icon);
       
   819 
       
   820     iconArray->AppendFromResourceL(R_GLX_ALBUM_SELECTION_ICONS);
       
   821 
       
   822     ///@todo Find a more elegant way of not painting an icon.
       
   823     CFbsBitmap* bitmap = new (ELeave) CFbsBitmap;
       
   824     CleanupStack::PushL(bitmap);
       
   825     icon = CGulIcon::NewL(bitmap);
       
   826     CleanupStack::Pop(bitmap);
       
   827     CleanupStack::PushL(icon);
       
   828     iconArray->AppendL(icon);
       
   829     CleanupStack::Pop(icon);
       
   830     }
       
   831 
       
   832 // -----------------------------------------------------------------------------
       
   833 // CGlxMediaSelectionPopup::AddNewMediaCreationItemL()
       
   834 // -----------------------------------------------------------------------------
       
   835 //
       
   836 void CGlxMediaSelectionPopup::AddNewMediaCreationItemL()
       
   837     {
       
   838     TRACER("CGlxMediaSelectionPopup::AddNewMediaCreationItemL");
       
   839     if (iEnableContainerCreation)
       
   840         {
       
   841         CGlxMedia* newMediaCreationStaticItem = new (ELeave) CGlxMedia(
       
   842                 TGlxMediaId(KNewItemId));
       
   843         CleanupStack::PushL(newMediaCreationStaticItem);
       
   844         __ASSERT_DEBUG(iNewMediaItemTitle, Panic(EGlxPanicNullPointer));
       
   845         newMediaCreationStaticItem->SetTextValueL(KMPXMediaGeneralTitle,
       
   846                 *iNewMediaItemTitle);
       
   847         iMediaList->AddStaticItemL(newMediaCreationStaticItem,
       
   848                 NGlxListDefs::EInsertFirst);
       
   849         CleanupStack::Pop(newMediaCreationStaticItem);
       
   850         }
       
   851     }
       
   852 
       
   853 // -----------------------------------------------------------------------------
       
   854 // CGlxMediaSelectionPopup::CompleteSelf()
       
   855 // -----------------------------------------------------------------------------
       
   856 //
       
   857 void CGlxMediaSelectionPopup::CompleteSelf()
       
   858     {
       
   859     TRACER("CGlxMediaSelectionPopup::CompleteSelf");
       
   860     TRequestStatus* status = &iStatus;
       
   861     User::RequestComplete(status, KErrNone);
       
   862     SetActive();
       
   863     }
       
   864 
       
   865 // -----------------------------------------------------------------------------
       
   866 // CGlxMediaSelectionPopup::FetchTitlesL()
       
   867 // -----------------------------------------------------------------------------
       
   868 //
       
   869 void CGlxMediaSelectionPopup::FetchTitlesL()
       
   870     {
       
   871     TRACER("CGlxMediaSelectionPopup::FetchTitlesL");
       
   872     CMPXCollectionPath* path = CMPXCollectionPath::NewL();
       
   873     CleanupStack::PushL(path);
       
   874 
       
   875     MGlxMediaList* rootList = MGlxMediaList::InstanceL(*path);
       
   876     CleanupClosePushL(*rootList);
       
   877 
       
   878     TGlxSpecificIdIterator iter(KGlxIdSpaceIdRoot, iCollectionId);
       
   879     CGlxAttributeContext* attributeContext =
       
   880             new (ELeave) CGlxAttributeContext(&iter);
       
   881     CleanupStack::PushL(attributeContext);
       
   882     attributeContext->AddAttributeL(
       
   883             KGlxMediaCollectionPluginSpecificNewMediaItemTitle);
       
   884     attributeContext->AddAttributeL(
       
   885             KGlxMediaCollectionPluginSpecificSelectMediaPopupTitle);
       
   886     rootList->AddContextL(attributeContext, KGlxFetchContextPriorityBlocking);
       
   887 
       
   888     // TGlxContextRemover will remove the context when it goes out of scope
       
   889     // Used here to avoid a trap and still have safe cleanup   
       
   890     TGlxFetchContextRemover contextRemover(attributeContext, *rootList);
       
   891     CleanupClosePushL(contextRemover);
       
   892     User::LeaveIfError(GlxAttributeRetriever::RetrieveL(*attributeContext,
       
   893             *rootList, EFalse));
       
   894     // context off the list
       
   895     CleanupStack::PopAndDestroy(&contextRemover);
       
   896 
       
   897     TInt index = rootList->Index(KGlxIdSpaceIdRoot, iCollectionId);
       
   898 
       
   899     __ASSERT_DEBUG(index> KErrNotFound, Panic(EGlxPanicRequiredItemNotFound));
       
   900     TGlxMedia item = rootList->Item(index);
       
   901 
       
   902     const CGlxMedia* media = item.Properties();
       
   903     if (media)
       
   904         {
       
   905         iNewMediaItemTitle = media->ValueText(
       
   906                 KGlxMediaCollectionPluginSpecificNewMediaItemTitle).AllocL();
       
   907         iSelectMediaPopupTitle
       
   908                 = media->ValueText(
       
   909                         KGlxMediaCollectionPluginSpecificSelectMediaPopupTitle).AllocL();
       
   910         }
       
   911 
       
   912     CleanupStack::PopAndDestroy(attributeContext);
       
   913     CleanupStack::PopAndDestroy(rootList);
       
   914     CleanupStack::PopAndDestroy(path);
       
   915     }
       
   916 
       
   917 // -----------------------------------------------------------------------------
       
   918 // CGlxMediaSelectionPopup::ConstructPopupListL()
       
   919 // -----------------------------------------------------------------------------
       
   920 //
       
   921 void CGlxMediaSelectionPopup::ConstructPopupListL(TBool aMultiSelection)
       
   922     {
       
   923     TRACER("CGlxMediaSelectionPopup::ConstructPopupListL");
       
   924     // create the list box
       
   925     iListBox = new (ELeave) CGlxSingleGraphicPopupMenuStyleListBox;
       
   926 
       
   927     // create the popup list
       
   928     iPopupList = CGlxSingleGraphicPopupMenuStyleList::NewL( iListBox, R_AVKON_SOFTKEYS_CANCEL) ;
       
   929 
       
   930     // set the title of the popup
       
   931     __ASSERT_DEBUG(iSelectMediaPopupTitle, Panic(EGlxPanicNullPointer));
       
   932 
       
   933     iPopupList->SetTitleL(*iSelectMediaPopupTitle);
       
   934 
       
   935     iListBox ->ConstructL(iPopupList,
       
   936             aMultiSelection
       
   937                             ?  EAknListBoxMultiselectionList
       
   938                                : EAknListBoxMenuList);
       
   939     iListBox->CreateScrollBarFrameL();
       
   940     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   941             CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   942     // create the listbox model
       
   943     iListBox->Model()->SetItemTextArray(iMediaListAdaptor);
       
   944     iListBox->View()->CalcBottomItemIndex();
       
   945     iListBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
       
   946 
       
   947     //set 'iPopupList' as List Box observer
       
   948     iListBox->SetListBoxObserver(iPopupList);
       
   949     }
       
   950 
       
   951 //-----------------------------------------------------------------------------
       
   952 // CGlxMediaSelectionPopup::IsListBoxItemVisible
       
   953 //-----------------------------------------------------------------------------
       
   954 TBool CGlxMediaSelectionPopup::IsListBoxItemVisible(TInt aIndex)
       
   955 	{
       
   956 	TRACER("CGlxMediaSelectionPopup::IsListBoxItemVisible");
       
   957 	return iListBox && (iListBox->TopItemIndex() <= aIndex &&
       
   958 			aIndex <= iListBox->BottomItemIndex());
       
   959 	}