upnpsettings/multiselectionui/src/upnpmultiselectionutility.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     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:      Multiselection dialog implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include <StringLoader.h>
       
    25 #include <avkon.mbg>
       
    26 #include <eikclbd.h>
       
    27 #include <aknlists.h>
       
    28 #include <AknsUtils.h>
       
    29 
       
    30 #include <cupnpmultiselectionui.rsg>
       
    31 #include "upnpmultiselectionutility.h"
       
    32 #include "upnpfilesharingengine.h"
       
    33 
       
    34 _LIT( KComponentLogfile, "multiselectionui.txt");
       
    35 #include "upnplog.h"
       
    36 
       
    37 
       
    38 //CONSTANTS
       
    39 
       
    40 // Format string for listbox items
       
    41 _LIT( KItemFormatString, "1\t%S" );
       
    42 
       
    43 const TInt KShareNoneIndex = 0; // listbox index of "do not share"
       
    44 const TInt KShareAllIndex = 1;  // listbox index of "share all"
       
    45 const TInt KPredefinedSelections = 2; // Share none and share all
       
    46 
       
    47 // ================= MEMBER FUNCTIONS =======================
       
    48 
       
    49 // --------------------------------------------------------------------------
       
    50 // CUpnpMultiselectionUtility::CUpnpMultiselectionUtility
       
    51 // C++ default constructor can NOT contain any code, that
       
    52 // might leave.
       
    53 // --------------------------------------------------------------------------
       
    54 //
       
    55 CUpnpMultiselectionUtility::CUpnpMultiselectionUtility()
       
    56     {
       
    57     __LOG("CUpnpMultiselectionUtility::CUpnpMultiselectionUtility");
       
    58     iShareNoneSelected = EFalse;
       
    59     iShareAllSelected = EFalse;
       
    60     }
       
    61 
       
    62 // --------------------------------------------------------------------------
       
    63 // CUpnpMultiselectionUtility::CUpnpMultiselectionUtility
       
    64 // C++ default destructor.
       
    65 // --------------------------------------------------------------------------
       
    66 //
       
    67 CUpnpMultiselectionUtility::~CUpnpMultiselectionUtility()
       
    68     {
       
    69     __LOG("CUpnpMultiselectionUtility::~CUpnpMultiselectionUtility");
       
    70     }
       
    71 
       
    72 // --------------------------------------------------------------------------
       
    73 // CUpnpMultiselectionUtility::CollectSelectedItemsL()
       
    74 // Collects selected items to member variable for later use
       
    75 // --------------------------------------------------------------------------
       
    76 //
       
    77 void CUpnpMultiselectionUtility::CollectSelectedItemsL( 
       
    78     CEikListBox* aListBox, TBool aFirstRun )
       
    79     {
       
    80     __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL begin");
       
    81 
       
    82     const CArrayFix<TInt>* indexes = NULL;
       
    83 
       
    84     //get the true indexes of marked items
       
    85     CAknListBoxFilterItems* filter 
       
    86             = static_cast < CAknFilteredTextListBoxModel* >     
       
    87                             ( aListBox->Model() )->Filter();
       
    88     if ( filter )
       
    89         {
       
    90         // Filter knows all.
       
    91         filter->UpdateSelectionIndexesL();
       
    92         indexes = filter->SelectionIndexes();
       
    93         }
       
    94     else
       
    95         {
       
    96         // No filter.
       
    97         indexes = aListBox->View()->SelectionIndexes();
       
    98         }
       
    99     
       
   100     if ( indexes->Count() > 0 )
       
   101         {
       
   102         // obtain selection state  
       
   103         TShareSelectionState selectionState = 
       
   104             ShareSelectionStateFromArray( *indexes );
       
   105 
       
   106         // make some decisions based on the tabs selected
       
   107         DoSelectionLogicL( aListBox, aFirstRun,
       
   108             indexes, filter, selectionState );
       
   109 
       
   110         }
       
   111     else // no items selected
       
   112         {
       
   113         __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   114 No items selected");
       
   115 
       
   116         filter->ResetFilteringL();
       
   117         aListBox->View()->SelectItemL( KShareNoneIndex );
       
   118         }
       
   119 
       
   120     // update iShareNoneSelected and iShareAllSelected member variables
       
   121     iShareNoneSelected = EFalse;
       
   122     iShareAllSelected = EFalse;
       
   123 
       
   124     const CArrayFix<TInt>* indexesAfter = NULL;
       
   125 
       
   126     if ( filter )
       
   127         {
       
   128         // Filter knows all.
       
   129         filter->UpdateSelectionIndexesL();
       
   130         indexesAfter = filter->SelectionIndexes();
       
   131         }
       
   132     else
       
   133         {
       
   134         // No filter.
       
   135         indexesAfter = aListBox->View()->SelectionIndexes();
       
   136         }
       
   137 
       
   138     for ( TInt i = 0 ; i < indexesAfter->Count() ; i++ ) 
       
   139         {
       
   140         if ( indexesAfter->At( i ) == KShareNoneIndex ) 
       
   141             {
       
   142             iShareNoneSelected = ETrue;
       
   143             }
       
   144         else if ( indexesAfter->At( i ) == KShareAllIndex ) 
       
   145             {
       
   146             iShareAllSelected = ETrue;
       
   147             }
       
   148         }
       
   149 
       
   150     __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL end");
       
   151     }
       
   152 
       
   153 // --------------------------------------------------------------------------
       
   154 // CUpnpMultiselectionUtility::DoSelectionLogicL
       
   155 // Do some decisions based on the selection state
       
   156 // --------------------------------------------------------------------------
       
   157 //
       
   158 void CUpnpMultiselectionUtility::DoSelectionLogicL( 
       
   159     CEikListBox* aListBox,
       
   160     TBool aFirstRun,
       
   161     const CArrayFix<TInt>* aIndexes,
       
   162     CAknListBoxFilterItems* aFilter,
       
   163     TShareSelectionState aSelectionState )
       
   164     {
       
   165     const TInt arrayGranularity( aIndexes->Count() + 1 );
       
   166 
       
   167     // If "do not share" was not selected before and it is now, unselect
       
   168     // all except "do not share".
       
   169     if ( ( aSelectionState == EShareNoneSelected ||
       
   170            aSelectionState == EShareNoneAndShareAllSelected ) && 
       
   171          !iShareNoneSelected )
       
   172         {
       
   173         // Create new selectionindexarray with only "do not share" 
       
   174         // selected and assign it to the listbox.
       
   175         __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL:\
       
   176 Do not share selected");
       
   177         CListBoxView::CSelectionIndexArray* noneArr = 
       
   178             new (ELeave) CArrayFixFlat<TInt>( arrayGranularity );
       
   179         CleanupStack::PushL( noneArr );
       
   180         noneArr->AppendL( KShareNoneIndex );
       
   181         aListBox->SetSelectionIndexesL( noneArr );
       
   182         CleanupStack::PopAndDestroy( noneArr );
       
   183         }
       
   184     // if "share all" was not selected before and it is now, select all
       
   185     // except "do not share". 
       
   186     // OR if this method is executed first time (from PostLayoutDynInitL)
       
   187     // and share all is selected, then we trust that all items should be 
       
   188     // selected.  
       
   189     else if ( ( ( aSelectionState == EShareAllSelected || 
       
   190                   aSelectionState == EShareNoneAndShareAllSelected ) &&
       
   191                   !iShareAllSelected ) 
       
   192               || aSelectionState == EShareAllSelected && aFirstRun )
       
   193         {
       
   194         // Create new selectionindexarray with "share all" and all other
       
   195         // items except "do not share" selected and assign it to the 
       
   196         // listbox.
       
   197         __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   198 All files selected");
       
   199         TInt itemCount = aFilter->NonFilteredNumberOfItems();
       
   200 
       
   201         CListBoxView::CSelectionIndexArray* allArr = 
       
   202             new (ELeave) CArrayFixFlat<TInt>( arrayGranularity );
       
   203         CleanupStack::PushL( allArr );
       
   204         allArr->AppendL( KShareAllIndex );
       
   205         for ( TInt i( KPredefinedSelections ); i < itemCount; i++ )
       
   206             {
       
   207             allArr->AppendL( i );
       
   208             }
       
   209         aFilter->ResetFilteringL();
       
   210         aListBox->SetSelectionIndexesL( allArr );
       
   211         CleanupStack::PopAndDestroy( allArr );
       
   212         }
       
   213     // if share all selection is removed
       
   214     else if ( aSelectionState == EItemsSelected && 
       
   215               iShareAllSelected ) 
       
   216         {
       
   217         // Create new selectionindexarray with only "do not share" 
       
   218         // selected and assign it to the listbox.
       
   219         __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   220 Do not share selected");
       
   221         CListBoxView::CSelectionIndexArray* noneArr = 
       
   222             new (ELeave) CArrayFixFlat<TInt>( arrayGranularity );
       
   223         CleanupStack::PushL( noneArr );
       
   224         noneArr->AppendL( KShareNoneIndex );
       
   225         aListBox->SetSelectionIndexesL( noneArr );
       
   226         CleanupStack::PopAndDestroy( noneArr );
       
   227         }
       
   228     // if only some individual items have been modified
       
   229     else if ( aSelectionState == EItemsSelected && 
       
   230               !iShareAllSelected &&
       
   231               !iShareNoneSelected ) 
       
   232         {
       
   233         // do nothing. selections are kept as they are
       
   234         }
       
   235     // if share none is selected with some individual item, 
       
   236     // unselect share none
       
   237     else if ( aSelectionState == EShareNoneSelected && aIndexes->Count() > 1 ) 
       
   238         {
       
   239         // Create similar selectionindexarray without "do not share" 
       
   240         // selected and assign it to the listbox.
       
   241         __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   242 Do not share selected");
       
   243 
       
   244         for( TInt y=0; y < aIndexes->Count(); y++ )
       
   245             {
       
   246             TInt itemIndex = aIndexes->At(y);
       
   247             if ( itemIndex == 0 || itemIndex == 1 )
       
   248                 {
       
   249                 aListBox->View()->DeselectItem( 
       
   250                     aFilter->VisibleItemIndex(itemIndex) );
       
   251                 }
       
   252             }
       
   253         }
       
   254     else if ( aSelectionState == EShareAllSelected &&
       
   255               aIndexes->Count() < aFilter->NonFilteredNumberOfItems() - 1 ) 
       
   256         {
       
   257         if ( aIndexes->Count() > 0 ) 
       
   258             {
       
   259             // all items were selected and some item (not share all) has 
       
   260             // been unselected -> remove selection also from share all.
       
   261             __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   262 Do not share selected");
       
   263 
       
   264             for( TInt y=0; y < aIndexes->Count(); y++ )
       
   265                 {
       
   266                 TInt itemIndex = aIndexes->At(y);
       
   267                 if ( itemIndex == KShareAllIndex )
       
   268                     {
       
   269                     aListBox->View()->DeselectItem( 
       
   270                         aFilter->VisibleItemIndex( itemIndex ) );
       
   271                     }
       
   272                 }
       
   273             
       
   274             if ( !aListBox->View()->SelectionIndexes()->Count() ) 
       
   275                 {
       
   276                 // Last individual item has been unselected.
       
   277                 // Create new selectionindexarray with only 
       
   278                 // "do not share" selected and assign it to the listbox.
       
   279                 __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   280 Do not share selected");
       
   281                 CListBoxView::CSelectionIndexArray* noneArr = 
       
   282                     new (ELeave) CArrayFixFlat<TInt>( arrayGranularity );
       
   283                 CleanupStack::PushL( noneArr );
       
   284                 noneArr->AppendL( KShareNoneIndex );
       
   285                 aListBox->SetSelectionIndexesL( noneArr );
       
   286                 CleanupStack::PopAndDestroy( noneArr );
       
   287                 }
       
   288             }
       
   289         else 
       
   290             {
       
   291             // some item unselected and no other items left except 
       
   292             // "share all"
       
   293 
       
   294             // Create new selectionindexarray with only "do not share" 
       
   295             // selected and assign it to the listbox.
       
   296             __LOG("CUpnpMultiselectionUtility::CollectSelectedItemsL: \
       
   297 Do not share selected");
       
   298             CListBoxView::CSelectionIndexArray* noneArr = 
       
   299                 new (ELeave) CArrayFixFlat<TInt>( arrayGranularity );
       
   300             CleanupStack::PushL( noneArr );
       
   301             noneArr->AppendL( KShareNoneIndex );
       
   302             aListBox->SetSelectionIndexesL( noneArr );
       
   303             CleanupStack::PopAndDestroy( noneArr );
       
   304             }
       
   305         }
       
   306     }
       
   307 
       
   308 // --------------------------------------------------------------------------
       
   309 // CUpnpMultiselectionUtility::ConvertSelectionsForEngineL
       
   310 // Convert the selections from UI to engine as "old style selections"
       
   311 // Has effect only if "Share All" is selected
       
   312 // --------------------------------------------------------------------------
       
   313 //
       
   314 void CUpnpMultiselectionUtility::ConvertSelectionsForEngineL( 
       
   315     CArrayFix<TInt>& aSelections ) const
       
   316     {
       
   317     __LOG("CUpnpMultiselectionUtility::ConvertSelectionsForEngineL begin");
       
   318 
       
   319     for ( TInt i(0); i < aSelections.Count(); i++ )
       
   320         {
       
   321         if ( aSelections.At( i ) == KShareAllIndex )
       
   322             {
       
   323             aSelections.Reset();
       
   324             aSelections.AppendL( KShareAllIndex );
       
   325             i = aSelections.Count();
       
   326             }
       
   327         }
       
   328     
       
   329     __LOG("CUpnpMultiselectionUtility::ConvertSelectionsForEngineL end");
       
   330     }
       
   331 
       
   332 
       
   333 // --------------------------------------------------------------------------
       
   334 // CUpnpMultiselectionUtility::AppendIconToArray
       
   335 // Load a possibly skinned icon (with mask) and append it to an
       
   336 // icon array.
       
   337 // --------------------------------------------------------------------------
       
   338 //
       
   339 void CUpnpMultiselectionUtility::AppendIconToArrayL(CAknIconArray* aArray,
       
   340                                                MAknsSkinInstance* aSkin,
       
   341                                                const TDesC& aMbmFile,
       
   342                                                const TAknsItemID& aID,
       
   343                                                TInt aBitmapId,
       
   344                                                TInt aMaskId) const
       
   345     {
       
   346     __LOG("CUpnpMultiselectionUtility::AppendIconToArrayL begin");
       
   347 
       
   348     __ASSERT_DEBUG(aArray != NULL, User::Leave(KErrArgument));
       
   349 
       
   350     CFbsBitmap* bitmap = NULL;
       
   351     CFbsBitmap* mask = NULL;
       
   352 
       
   353     AknsUtils::CreateIconLC( aSkin, aID,
       
   354         bitmap, mask, aMbmFile, aBitmapId, aMaskId );
       
   355 
       
   356     CGulIcon* icon = CGulIcon::NewL( bitmap, mask );
       
   357     icon->SetBitmapsOwnedExternally( EFalse );
       
   358 
       
   359     // icon now owns the bitmaps, no need to keep on cleanup stack.
       
   360     CleanupStack::Pop(2); // mask, bitmap
       
   361     bitmap = NULL;
       
   362     mask = NULL;
       
   363 
       
   364     CleanupStack::PushL( icon );
       
   365 
       
   366     aArray->AppendL( icon );
       
   367 
       
   368     // aArray now owns the icon, no need to delete.
       
   369     CleanupStack::Pop(icon);
       
   370     __LOG("CUpnpMultiselectionUtility::AppendIconToArrayL end");
       
   371 
       
   372     }
       
   373 
       
   374 // --------------------------------------------------------------------------
       
   375 // CUpnpMultiselectionUtility::AppendShareAllSelectionL
       
   376 // Appends "share all files" item to the listbox
       
   377 // --------------------------------------------------------------------------
       
   378 //
       
   379 void CUpnpMultiselectionUtility::AppendShareAllSelectionL(
       
   380                                     CDesCArray* aListBoxArray ) const
       
   381     {
       
   382     __LOG("CUpnpMultiselectionUtility::AppendShareAllSelectionL begin");
       
   383     HBufC* item = HBufC::NewL( KMaxFileName );
       
   384     CleanupStack::PushL( item );
       
   385     HBufC* item2 = HBufC::NewL( KMaxFileName );
       
   386     CleanupStack::PushL( item2 );
       
   387     HBufC* itemText1 = StringLoader::LoadLC( 
       
   388         R_UPNP_MULTISELECTION_SHARE_ALL_TEXT );
       
   389     HBufC* itemText2 = StringLoader::LoadLC(
       
   390         R_UPNP_MULTISELECTION_DO_NOT_SHARE_TEXT );
       
   391     // create item string for the listbox (icon + album name)
       
   392     item->Des().Format( KItemFormatString, itemText1 );
       
   393     item2->Des().Format( KItemFormatString, itemText2 );
       
   394     // append "share all files" -selection to the listbox
       
   395     aListBoxArray->AppendL( item2->Des() );
       
   396     aListBoxArray->AppendL( item->Des() );
       
   397 
       
   398     CleanupStack::PopAndDestroy(2); // stringloader
       
   399     CleanupStack::PopAndDestroy(2); // item item2
       
   400     __LOG( "CUpnpMultiselectionUtility::AppendShareAllSelectionL end" );
       
   401     }
       
   402     
       
   403 // --------------------------------------------------------------------------
       
   404 // CUpnpMultiselectionUtility::SetShareSelectionState
       
   405 // (Commented in the header)
       
   406 // --------------------------------------------------------------------------
       
   407 //
       
   408 void CUpnpMultiselectionUtility::SetShareSelectionState( 
       
   409     TShareSelectionState aSelectionState ) 
       
   410     {
       
   411     switch ( aSelectionState )
       
   412         {
       
   413         case EShareNoneSelected:
       
   414             {
       
   415             iShareNoneSelected = ETrue;
       
   416             iShareAllSelected = EFalse;
       
   417             break;
       
   418             }
       
   419         case EShareAllSelected:
       
   420             {
       
   421             iShareNoneSelected = EFalse;
       
   422             iShareAllSelected = ETrue;
       
   423             break;
       
   424             }
       
   425         case ENoShareNoneOrShareAllSelected:
       
   426             {
       
   427             iShareNoneSelected = EFalse;
       
   428             iShareAllSelected = EFalse;
       
   429             break;
       
   430             }
       
   431         default:
       
   432             {
       
   433             // Should not be reached.
       
   434             break;
       
   435             }
       
   436         }
       
   437     }
       
   438 
       
   439 // --------------------------------------------------------------------------
       
   440 // CUpnpMultiselectionUtility::ShareSelectionStateFromArray
       
   441 // (Commented in the header)
       
   442 // --------------------------------------------------------------------------
       
   443 //
       
   444 TShareSelectionState 
       
   445     CUpnpMultiselectionUtility::ShareSelectionStateFromArray( 
       
   446     const CArrayFix<TInt>& aIndexes ) const
       
   447     {
       
   448     TShareSelectionState retval = EUndefined;
       
   449     TBool shareNoneFound = EFalse;
       
   450     TBool shareAllFound = EFalse;
       
   451     TBool itemsSelected = EFalse;
       
   452         
       
   453     // Check if the indexes array contains do not share or share all.
       
   454     for ( TInt i = 0 ; i < aIndexes.Count() ; i++ ) 
       
   455         {
       
   456         if ( aIndexes.At( i ) == KShareNoneIndex ) 
       
   457             {
       
   458             shareNoneFound = ETrue;
       
   459             }
       
   460         else if ( aIndexes.At( i ) == KShareAllIndex )  
       
   461             {
       
   462             shareAllFound = ETrue;
       
   463             }
       
   464         }
       
   465 
       
   466     // 
       
   467     if ( !shareAllFound && !shareNoneFound ) 
       
   468         {
       
   469         // "do not share" or "share all" are not selected. Check if there are
       
   470         // other items selected.
       
   471         if ( aIndexes.Count() ) 
       
   472             {
       
   473             itemsSelected = ETrue;
       
   474             }
       
   475         }
       
   476 
       
   477     // set retval
       
   478     if ( itemsSelected ) 
       
   479         {
       
   480         retval = EItemsSelected;
       
   481         }    
       
   482     else if ( shareNoneFound && !shareAllFound ) 
       
   483         {
       
   484         retval = EShareNoneSelected;
       
   485         }
       
   486     else if ( !shareNoneFound && shareAllFound ) 
       
   487         {
       
   488         retval = EShareAllSelected;
       
   489         }    
       
   490     else if ( shareNoneFound && shareAllFound ) 
       
   491         {
       
   492         retval = EShareNoneAndShareAllSelected;
       
   493         }
       
   494     else if ( !shareNoneFound && !shareAllFound ) 
       
   495         {
       
   496         retval = ENoShareNoneOrShareAllSelected;
       
   497         }
       
   498     
       
   499     return retval;
       
   500     }
       
   501 
       
   502 
       
   503 // End of file