homesync/contentmanager/mediaservant/src/msmultiselectiondialog.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /*
       
     2 * Copyright (c) 2008 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:  CMSMultiselectionDialog class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <akntitle.h>
       
    22 #include <avkon.mbg>
       
    23 #include <aknlists.h>
       
    24 #include <akncheckboxsettingpage.h>
       
    25 #include <mediaservant.rsg>
       
    26 
       
    27 #include "msmultiselectiondialog.h"
       
    28 #include "mediaservant.hrh"
       
    29 #include "msconstants.h"
       
    30 #include "msdebug.h"
       
    31 
       
    32 
       
    33 //CONSTANTS
       
    34 
       
    35 // Reserved list items at a time
       
    36 const TInt KListGranularity = 10;
       
    37 // Used for item icon definition
       
    38 _LIT(KOneSlashT, "1\t");
       
    39 
       
    40 
       
    41 // --------------------------------------------------------------------------
       
    42 // CMSMultiselectionDialog::NewL
       
    43 // --------------------------------------------------------------------------
       
    44 //
       
    45 CMSMultiselectionDialog* CMSMultiselectionDialog::NewL(
       
    46                                             CSelectionItemList* aItemArray,
       
    47                                             const TDesC& aTitle )
       
    48     {
       
    49     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::NewL"));
       
    50 
       
    51     CMSMultiselectionDialog* self =
       
    52                     CMSMultiselectionDialog::NewLC( aItemArray, aTitle );                        
       
    53     CleanupStack::Pop(self);
       
    54 
       
    55     return self;
       
    56     }
       
    57 
       
    58 // --------------------------------------------------------------------------
       
    59 // CMSMultiselectionDialog::NewLC
       
    60 // --------------------------------------------------------------------------
       
    61 //
       
    62 CMSMultiselectionDialog* CMSMultiselectionDialog::NewLC(
       
    63                                             CSelectionItemList* aItemArray,
       
    64                                             const TDesC& aTitle )
       
    65     {
       
    66     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::NewLC"));
       
    67 
       
    68     CMSMultiselectionDialog* self =
       
    69                         new(ELeave) CMSMultiselectionDialog( aItemArray );
       
    70 
       
    71     CleanupStack::PushL(self);
       
    72     self->ConstructL( aTitle );
       
    73 
       
    74     return self;
       
    75     }
       
    76     
       
    77 // --------------------------------------------------------------------------
       
    78 // CMSMultiselectionDialog::ConstructL
       
    79 // Symbian 2nd phase constructor can leave.
       
    80 // --------------------------------------------------------------------------
       
    81 //
       
    82 void CMSMultiselectionDialog::ConstructL( const TDesC& aTitle )
       
    83     {
       
    84     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
    85     ConstructL"));
       
    86 
       
    87     // base class call
       
    88     // menubar id has no effect because options menu is not used
       
    89     CAknSelectionListDialog::ConstructL( R_MS_MENUBAR_MAIN_VIEW );
       
    90     
       
    91     // keep safe previous view title
       
    92     iPreviousTitle = HBufC::NewL( KMaxFileName );
       
    93 
       
    94     // set dialog title
       
    95     CEikStatusPane* statusPane =
       
    96                     (( CAknAppUi* )iEikonEnv->EikAppUi() )->StatusPane();
       
    97     iTitlePane = (CAknTitlePane*)statusPane->ControlL(
       
    98                     TUid::Uid(EEikStatusPaneUidTitle));
       
    99     // keep previous title safe
       
   100     iPreviousTitle->Des().Copy( *iTitlePane->Text() );
       
   101     // set new title
       
   102     iTitlePane->SetTextL(aTitle);
       
   103     }
       
   104 
       
   105 
       
   106 // --------------------------------------------------------------------------
       
   107 // CMSMultiselectionDialog::CMSMultiselectionDialog
       
   108 // C++ default constructor can NOT contain any code, that
       
   109 // might leave.
       
   110 // --------------------------------------------------------------------------
       
   111 //
       
   112 CMSMultiselectionDialog::CMSMultiselectionDialog(
       
   113                                 CSelectionItemList* aItemArray)
       
   114 : CAknSelectionListDialog(iSelectedItem, NULL, NULL),
       
   115   iItemArray (aItemArray)
       
   116     {
       
   117     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog:: \
       
   118     CMSMultiselectionDialog"));
       
   119     }
       
   120 
       
   121 // --------------------------------------------------------------------------
       
   122 // CMSMultiselectionDialog::CMSMultiselectionDialog
       
   123 // C++ default destructor.
       
   124 // --------------------------------------------------------------------------
       
   125 //
       
   126 CMSMultiselectionDialog::~CMSMultiselectionDialog()
       
   127     {
       
   128     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog:: \
       
   129     ~CMSMultiselectionDialog"));
       
   130 
       
   131     // set previous title back
       
   132     if( iTitlePane )
       
   133         {
       
   134         TInt error = KErrNone;
       
   135         TRAP( error, iTitlePane->SetTextL( *iPreviousTitle ) );
       
   136         if( error )
       
   137             {
       
   138             TRACE( Print(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog\
       
   139                   SetTextL error %d\n"),error ) );
       
   140             }
       
   141         }
       
   142     
       
   143     delete iPreviousTitle;
       
   144 
       
   145     delete iInternalItemArray;
       
   146 
       
   147     delete iNaviDecorator;
       
   148     }
       
   149 
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CMSMultiselectionDialog::OkToExitL(TInt aButtonId)
       
   153 // called by framework when the softkey is pressed
       
   154 // --------------------------------------------------------------------------
       
   155 //
       
   156 TBool CMSMultiselectionDialog::OkToExitL(TInt aButtonId)
       
   157     {
       
   158     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
   159     OkToExitL"));
       
   160 
       
   161     TBool retval = ETrue;
       
   162     
       
   163     // When using middle softkey list update must be called from
       
   164     // this way because dialog doesn't receive middle softkey key
       
   165     // event.
       
   166     if ( aButtonId == EAknSoftkeyChange )
       
   167         {
       
   168         HandleSelectionKeyPressL();
       
   169         // don't exit dialog
       
   170         retval = EFalse;
       
   171         }
       
   172 
       
   173     if ( retval ) 
       
   174         {
       
   175         // Update item selections to the list
       
   176         UpdateAllSelectionsL();
       
   177 
       
   178         // Clear navi pane text
       
   179         iNaviPane->Pop( iNaviDecorator );
       
   180 
       
   181         if ( aButtonId == EAknSoftkeyBack )
       
   182             {
       
   183             iAvkonAppUi->ProcessCommandL( EEikCmdExit );
       
   184             }
       
   185         }
       
   186 
       
   187     return retval;
       
   188     }
       
   189 
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CMSMultiselectionDialog::PreLayoutDynInitL()
       
   193 // called by framework before dialog is shown
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 void CMSMultiselectionDialog::PreLayoutDynInitL()
       
   197     {
       
   198     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog:: \
       
   199     PreLayoutDynInitL"));
       
   200 
       
   201     iListBox = static_cast<CAknSingleGraphicStyleListBox*>
       
   202                             (Control(EMultiSelectionListBoxId));
       
   203 
       
   204     // display find box
       
   205     SetupFind(CAknSelectionListDialog::EFixedFind);
       
   206 
       
   207     CAknIconArray* icons = new (ELeave) CAknIconArray(2); //magic: icon count
       
   208     CleanupStack::PushL( icons );
       
   209 
       
   210     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   211     TFileName iconsPath( AknIconUtils::AvkonIconFileName() );
       
   212 
       
   213     AppendIconToArrayL( icons, skin,
       
   214                         iconsPath,
       
   215                         KAknsIIDQgnPropCheckboxOn,
       
   216                         EMbmAvkonQgn_indi_checkbox_on,
       
   217                         EMbmAvkonQgn_indi_checkbox_on_mask );
       
   218 
       
   219     AppendIconToArrayL( icons, skin,
       
   220                         iconsPath,
       
   221                         KAknsIIDQgnPropCheckboxOff,
       
   222                         EMbmAvkonQgn_indi_checkbox_off,
       
   223                         EMbmAvkonQgn_indi_checkbox_off_mask );
       
   224 
       
   225 
       
   226     iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
       
   227     CleanupStack::Pop(icons);
       
   228 
       
   229     GenerateInternalArrayL();
       
   230 
       
   231     CEikStatusPane* statusPane=iEikonEnv->AppUiFactory()->StatusPane();
       
   232 
       
   233     // Set empty navi pane text
       
   234     iNaviPane = static_cast<CAknNavigationControlContainer*>
       
   235             ( statusPane->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) ) );
       
   236                 // ownership of decorator is transfered to application
       
   237     iNaviDecorator = iNaviPane->CreateNavigationLabelL( _L("") );
       
   238     iNaviPane->PushL( *iNaviDecorator );
       
   239 
       
   240     iListBox->ActivateL();
       
   241     CAknSelectionListDialog::PreLayoutDynInitL();
       
   242     }
       
   243 
       
   244 // --------------------------------------------------------------------------
       
   245 // CMSMultiselectionDialog::HandleSelectionKeyPressL()
       
   246 // handles middle softkey press
       
   247 // --------------------------------------------------------------------------
       
   248 //
       
   249 void CMSMultiselectionDialog::HandleSelectionKeyPressL()
       
   250     {
       
   251     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
   252     HandleSelectionKeyPressL"));
       
   253     // Make special selection handling
       
   254     HandleSelectionsL();
       
   255     }
       
   256 
       
   257 // --------------------------------------------------------------------------
       
   258 // CMSMultiselectionSettingPage::HandlePointerEventL
       
   259 // called by framework when the pointer device is pressed
       
   260 // --------------------------------------------------------------------------
       
   261 //
       
   262 void CMSMultiselectionDialog::HandlePointerEventL(
       
   263                                 const TPointerEvent& aPointerEvent ) 
       
   264 	{
       
   265 	CCoeControl::HandlePointerEventL( aPointerEvent );
       
   266 	
       
   267 	if ( TPointerEvent::EButton1Up == aPointerEvent.iType )
       
   268 		{
       
   269 		HandleSelectionKeyPressL();
       
   270 		}
       
   271 	}
       
   272 
       
   273 // --------------------------------------------------------------------------
       
   274 // CMSMultiselectionDialog::AppendIconToArray
       
   275 // Load a possibly skinned icon (with mask) and append it to an
       
   276 // icon array.
       
   277 // --------------------------------------------------------------------------
       
   278 //
       
   279 void CMSMultiselectionDialog::AppendIconToArrayL(CAknIconArray* aArray,
       
   280                                                MAknsSkinInstance* aSkin,
       
   281                                                const TDesC& aMbmFile,
       
   282                                                const TAknsItemID& aID,
       
   283                                                TInt aBitmapId,
       
   284                                                TInt aMaskId) const
       
   285     {
       
   286     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
   287     AppendIconToArrayL"));
       
   288 
       
   289     __ASSERT_DEBUG( aArray, User::Leave(KErrArgument) );
       
   290 
       
   291     CFbsBitmap* bitmap = NULL;
       
   292     CFbsBitmap* mask = NULL;
       
   293 
       
   294     AknsUtils::CreateIconLC(aSkin, aID,
       
   295         bitmap, mask, aMbmFile, aBitmapId, aMaskId);
       
   296 
       
   297     CGulIcon* icon = CGulIcon::NewL(bitmap, mask);
       
   298     icon->SetBitmapsOwnedExternally(EFalse);
       
   299 
       
   300     // icon now owns the bitmaps, no need to keep on cleanup stack.
       
   301     CleanupStack::Pop(2); // mask, bitmap
       
   302     bitmap = NULL;
       
   303     mask = NULL;
       
   304 
       
   305     CleanupStack::PushL(icon);
       
   306 
       
   307     aArray->AppendL(icon);
       
   308 
       
   309     // aArray now owns the icon, no need to delete.
       
   310     CleanupStack::Pop(icon);
       
   311     }
       
   312 
       
   313 // --------------------------------------------------------------------------
       
   314 // CMSMultiselectionDialog::GenerateInternalArrayL
       
   315 // Creates an internal array shown by listbox
       
   316 // --------------------------------------------------------------------------
       
   317 //
       
   318 void CMSMultiselectionDialog::GenerateInternalArrayL()
       
   319     {
       
   320     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
   321     GenerateInternalArrayL"));
       
   322 
       
   323 
       
   324     // delete existing array
       
   325     delete iInternalItemArray;
       
   326     iInternalItemArray = 0;
       
   327 
       
   328     // Make a new owned text array from the input one:
       
   329     iInternalItemArray = new(ELeave)CDesCArrayFlat( KListGranularity );
       
   330     TInt count = iItemArray->Count();
       
   331 
       
   332     for (TInt index = 0; index < count; index++)
       
   333         {
       
   334         // Fill up the control-owned space for the modified buffers:
       
   335         TInt newLen =
       
   336             ((*iItemArray)[index]->ItemText()).Length() +
       
   337             KOneSlashT().Length();
       
   338 
       
   339         HBufC* newText = HBufC::NewMaxLC( newLen );
       
   340 
       
   341         TPtr newTPtr = newText->Des();        
       
   342         newTPtr.Copy( ((*iItemArray)[index])->ItemText() );
       
   343         
       
   344         // replace '\t' with space
       
   345         TInt offset = newTPtr.Find( KSlashT );
       
   346         if ( offset >= 0 )
       
   347             {
       
   348             TInt replaceCount = KSlashT().Length();
       
   349             newTPtr.Replace( offset, replaceCount, KSPaceT );    
       
   350             }        
       
   351         
       
   352         newTPtr.Insert( 0, KOneSlashT ); // magic: insert to the beginning
       
   353         
       
   354         // A copy of the new buffer goes to the internal itemarray:                
       
   355         iInternalItemArray->AppendL(newTPtr);
       
   356         CleanupStack::PopAndDestroy( newText );
       
   357         }
       
   358 
       
   359     CTextListBoxModel* model=iListBox->Model();
       
   360     model->SetItemTextArray(iInternalItemArray);
       
   361     model->SetOwnershipType(ELbmDoesNotOwnItemArray);
       
   362 
       
   363     // Transfer the selected status from
       
   364     // the input array into the listbox view
       
   365     SetAllSelectionsL();
       
   366     }
       
   367 
       
   368 // --------------------------------------------------------------------------
       
   369 // CMSMultiselectionDialog::SetAllSelectionsL
       
   370 // Sets selection status of items
       
   371 // --------------------------------------------------------------------------
       
   372 //
       
   373 void CMSMultiselectionDialog::SetAllSelectionsL()
       
   374     {
       
   375     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
   376     SetAllSelectionsL"));
       
   377 
       
   378     TInt count = iItemArray->Count();
       
   379 
       
   380     CListBoxView* view = iListBox->View();
       
   381 
       
   382     for (TInt index = 0; index < count; index++)
       
   383         {
       
   384         if ( (*iItemArray)[ index ]->SelectionStatus() )
       
   385             {
       
   386             view->SelectItemL( index );
       
   387             }
       
   388         else
       
   389             {
       
   390             view->DeselectItem( index );
       
   391             }
       
   392         }
       
   393     }
       
   394 
       
   395 // --------------------------------------------------------------------------
       
   396 // CMSMultiselectionSettingPage::HandleSelectionsL
       
   397 // Removes selection marks from other items if first one is selected
       
   398 // --------------------------------------------------------------------------
       
   399 //
       
   400 void CMSMultiselectionDialog::HandleSelectionsL()
       
   401     {
       
   402     LOG(_L("[MediaServant]\t CMSMultiselectionDialog::\
       
   403     HandleSelectionsL"));
       
   404     CAknListBoxFilterItems* filter =
       
   405                     static_cast < CAknFilteredTextListBoxModel* >
       
   406                     ( iListBox->Model() )->Filter();
       
   407     const CArrayFix<TInt>* indexes = iListBox->View()->SelectionIndexes();
       
   408     if ( indexes->Count() )
       
   409         {
       
   410         TKeyArrayFix sortKey(0,ECmpTUint);
       
   411         TInt dummy;
       
   412         if ( FindBox()->TextLength() == 0 ||
       
   413              filter->VisibleItemIndex( 0 ) != KErrNotFound )
       
   414             {
       
   415             // Check if first item is selected
       
   416             if ( indexes->Find(0, sortKey, dummy) == 0 )
       
   417                 {
       
   418                 // clear all
       
   419                 iListBox->View()->ClearSelection();
       
   420                 
       
   421                 // select current item
       
   422                 iListBox->View()->SelectItemL( iListBox->CurrentItemIndex() );
       
   423                 }
       
   424             else // first item not selected
       
   425                 {
       
   426                 // check if first item is highlighted
       
   427                 if ( iListBox->CurrentItemIndex() == 0 )
       
   428                     {
       
   429                     // remove all selections...
       
   430                     iListBox->View()->ClearSelection();
       
   431                     }
       
   432                 //...and toggle highlighted item
       
   433                 iListBox->View()->ToggleItemL( iListBox->CurrentItemIndex() );
       
   434                 
       
   435                 // check if we don't have selected items
       
   436                 if ( indexes->Count() == 0 )
       
   437                     {
       
   438                     // check the first one
       
   439                     iListBox->View()->SelectItemL(0);
       
   440                     }
       
   441                 }
       
   442             }
       
   443         else
       
   444             {
       
   445             //...and toggle highlighted item
       
   446             iListBox->View()->ToggleItemL( iListBox->CurrentItemIndex() );
       
   447 
       
   448             // check if we don't have selected items
       
   449             if ( indexes->Count() == 0 )
       
   450                 {
       
   451                 // check the first one
       
   452                 iListBox->View()->SelectItemL(0);
       
   453                 }
       
   454             }
       
   455         }
       
   456     else // no items selected
       
   457         {
       
   458         iListBox->View()->SelectItemL(0);
       
   459         }
       
   460     }
       
   461 
       
   462 // --------------------------------------------------------------------------
       
   463 // CMSMultiselectionDialog::UpdateAllSelectionsL
       
   464 // Updates selection status of the items in the item array
       
   465 // --------------------------------------------------------------------------
       
   466 //
       
   467 void CMSMultiselectionDialog::UpdateAllSelectionsL()
       
   468     {
       
   469     LOG(_L("[UPNP_HOMECONNECT]\t CMSMultiselectionDialog::\
       
   470     UpdateAllSelectionsL"));
       
   471 
       
   472     TInt count = iItemArray->Count();
       
   473 
       
   474     CAknListBoxFilterItems* filter
       
   475                 = static_cast < CAknFilteredTextListBoxModel* >
       
   476                                 ( iListBox->Model() )->Filter();
       
   477     // reset filtering so that real selected indexes can be get
       
   478     filter->ResetFilteringL();
       
   479 
       
   480     CListBoxView* view = iListBox->View();
       
   481 
       
   482     for (TInt index = 0; index < count; index++)
       
   483         {
       
   484         (*iItemArray)[ index ]->SetSelectionStatus(
       
   485                                     view->ItemIsSelected( index ) );
       
   486         }
       
   487     }
       
   488 
       
   489 // End of file