phonebookui/Phonebook2/xSPExtensionManager/src/CxSPSortViewControl.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Implementation for CxSPSortViewControl.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CxSPSortViewControl.h"
       
    21 
       
    22 // System includes
       
    23 #include <aknlists.h>   // CAknSingleStyleListBox
       
    24 #include <avkon.mbg>
       
    25 #include <eikclbd.h>
       
    26 #include <gulicon.h>
       
    27 #include <AknGlobalNote.h>
       
    28 #include <StringLoader.h>
       
    29 #include <avkon.hrh>
       
    30 
       
    31 // internal includes
       
    32 #include "CxSPLoader.h"
       
    33 #include "MxSPFactory.h"
       
    34 #include "CxSPViewIdChanger.h"
       
    35 #include "CxSPViewInfo.h"
       
    36 #include <ExtensionManagerRes.rsg>
       
    37 #include "xSPOrderOrganizer.h"
       
    38 #include "CGlobalNoteObserver.h"
       
    39 #include <MPbk2ApplicationServices2.h>
       
    40 #include <MPbk2ApplicationServices.h>
       
    41 #include <CPbk2ServiceManager.h>
       
    42 #include <MPbk2AppUi.h>
       
    43 #include <CPbk2UIExtensionView.h>
       
    44 
       
    45 // CONSTANTS
       
    46 _LIT( KTab, "\t" );
       
    47 const TUint KMaxListBoxBufSize( 256 ); // Maximum listbox item text size
       
    48 
       
    49 
       
    50 void CleanUpResetAndDestroy( TAny* aArray )
       
    51 	{
       
    52 	if( aArray )
       
    53 		{
       
    54 		CArrayPtrFlat<TDesC>* array = (CArrayPtrFlat<TDesC>*)aArray;
       
    55 		array->ResetAndDestroy();
       
    56 		delete array;
       
    57 		}
       
    58 	}
       
    59 
       
    60 // ================= MEMBER FUNCTIONS =======================
       
    61 
       
    62 CxSPSortViewControl::CxSPSortViewControl( CxSPViewIdChanger& aViewIdChanger, 
       
    63 											CxSPArray& aExtensions,
       
    64 											CPbk2UIExtensionView& aView ) :
       
    65 											iViewIdChanger( aViewIdChanger ),
       
    66 											iExtensions(aExtensions),
       
    67 											iGlobalNote(0),
       
    68 											iGlobalNoteObserver(0),
       
    69 											iView( aView )
       
    70     {
       
    71     }
       
    72 
       
    73 void CxSPSortViewControl::ConstructL()
       
    74     {
       
    75     CreateWindowL();
       
    76     
       
    77     // Initialize the list box
       
    78     iListBox = new (ELeave) CAknSingleGraphicStyleListBox;
       
    79     TInt flags = EAknListBoxLoopScrolling | EAknListBoxMarkableList;
       
    80     iListBox->ConstructL(this, flags);
       
    81     iListBox->SetContainerWindowL(*this);
       
    82     iListBox->CreateScrollBarFrameL(ETrue);
       
    83     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL
       
    84         (CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);	
       
    85 	SetListBoxContentsL();
       
    86     }
       
    87 
       
    88 CxSPSortViewControl* CxSPSortViewControl::NewL( CxSPViewIdChanger& aViewIdChanger,
       
    89 											CxSPArray& aExtensions, 
       
    90 											CPbk2UIExtensionView& aView )
       
    91     {
       
    92     CxSPSortViewControl* self = NewLC( aViewIdChanger, aExtensions, aView );
       
    93     CleanupStack::Pop(self);
       
    94     return self;
       
    95     }
       
    96 
       
    97 CxSPSortViewControl* CxSPSortViewControl::NewLC( CxSPViewIdChanger& aViewIdChanger,
       
    98 											CxSPArray& aExtensions, 
       
    99 											CPbk2UIExtensionView& aView )
       
   100     {
       
   101     CxSPSortViewControl* self =
       
   102         new (ELeave) CxSPSortViewControl( aViewIdChanger, aExtensions, aView );
       
   103     CleanupStack::PushL(self);
       
   104     self->ConstructL();
       
   105     return self;
       
   106     }
       
   107 
       
   108 CxSPSortViewControl::~CxSPSortViewControl()
       
   109     {
       
   110     delete iListBox;   
       
   111     iSortedOrder.Close();
       
   112     delete iGlobalNote;
       
   113     delete iGlobalNoteObserver;
       
   114     }
       
   115 
       
   116 TKeyResponse CxSPSortViewControl::OfferKeyEventL
       
   117         (const TKeyEvent& aKeyEvent,TEventCode aType)
       
   118     {
       
   119     TKeyResponse result = EKeyWasNotConsumed;
       
   120     
       
   121 	if( iListBox && result == EKeyWasNotConsumed )
       
   122         {
       
   123         result = iListBox->OfferKeyEventL(aKeyEvent, aType);
       
   124         }
       
   125 	
       
   126     // Update popup when moving up or down in the list
       
   127      switch(aKeyEvent.iCode)
       
   128          {
       
   129          case EKeyUpArrow:
       
   130          case EKeyDownArrow:
       
   131              {
       
   132              const CListBoxView::CSelectionIndexArray* inds = NULL;
       
   133              TInt count = 0;
       
   134              
       
   135              if ( iListBox )
       
   136             	 {
       
   137             	 inds = iListBox->SelectionIndexes();
       
   138             	 count = inds->Count();
       
   139             	 }
       
   140              
       
   141              if ( count <= 0 )
       
   142                  {
       
   143                  iView.Cba()->SetCommandSetL( R_EXTENSION_MANAGER_SORT_VIEW_SOFTKEYS  );
       
   144                  }
       
   145              else
       
   146                  {
       
   147                  iView.Cba()->SetCommandSetL( R_AVKON_SOFTKEYS_OK_CANCEL );
       
   148                  }
       
   149              iView.Cba()->DrawDeferred();
       
   150 
       
   151              break;
       
   152              }
       
   153          case EKeyEnter:
       
   154          case EKeyOK: 
       
   155              {
       
   156              const CListBoxView::CSelectionIndexArray* inds = NULL;
       
   157              TInt count = 0;
       
   158              
       
   159              if ( iListBox )
       
   160             	 {
       
   161             	 inds = iListBox->SelectionIndexes();
       
   162             	 count = inds->Count();
       
   163             	 }
       
   164              
       
   165              if ( count <= 0 )
       
   166                  {
       
   167                  SetCurrentItemMarkedL( ETrue );
       
   168                  iView.Cba()->SetCommandSetL( R_AVKON_SOFTKEYS_OK_CANCEL );
       
   169                  }
       
   170              else
       
   171                  {
       
   172                  MoveMarkedItemL();
       
   173                  iView.Cba()->SetCommandSetL( R_EXTENSION_MANAGER_SORT_VIEW_SOFTKEYS  );
       
   174                  }
       
   175              iView.Cba()->DrawDeferred();
       
   176              break;
       
   177              }
       
   178          default:
       
   179              {
       
   180              break;
       
   181              }
       
   182          }
       
   183     return result;
       
   184     }
       
   185 
       
   186 void CxSPSortViewControl::SizeChanged()
       
   187     {
       
   188     if ( iListBox )
       
   189         {
       
   190         iListBox->SetRect(Rect());
       
   191         }    
       
   192     }
       
   193 
       
   194 TInt CxSPSortViewControl::CountComponentControls() const
       
   195     {
       
   196     TInt controls = 0;
       
   197     if (iListBox) ++controls;
       
   198     return controls;
       
   199     }
       
   200 
       
   201 CCoeControl* CxSPSortViewControl::ComponentControl
       
   202         (TInt /*aIndex*/) const
       
   203     {
       
   204     return iListBox;
       
   205     }
       
   206 
       
   207 void CxSPSortViewControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
       
   208     {
       
   209     if ( iListBox )
       
   210         {
       
   211         iListBox->HandlePointerEventL( aPointerEvent );
       
   212         }
       
   213     }
       
   214 
       
   215 CCoeControl& CxSPSortViewControl::ComponentControl() const
       
   216     {
       
   217     return *iListBox;
       
   218     }
       
   219 
       
   220 void CxSPSortViewControl::SetListBoxContentsL()
       
   221 	{ 
       
   222 	_LIT( KFormat, "%d" );
       
   223 	
       
   224 	CArrayPtr<CGulIcon>* icons = new(ELeave) CArrayPtrFlat<CGulIcon>( 1 );
       
   225     CleanupStack::PushL( TCleanupItem( CleanUpResetAndDestroy, icons ));
       
   226     
       
   227     CFbsBitmap* bitmap = NULL;
       
   228 	CFbsBitmap* mask = NULL;	
       
   229 	// Load the avkon "mark" icon
       
   230 	AknIconUtils::CreateIconLC(	bitmap,	mask, AknIconUtils::AvkonIconFileName(),
       
   231     				  EMbmAvkonQgn_indi_marked_add,
       
   232     				  EMbmAvkonQgn_indi_marked_add_mask );
       
   233 
       
   234 	CGulIcon* icon = CGulIcon::NewL(bitmap,mask);
       
   235 	CleanupStack::Pop( 2 ); // mask, bitmap
       
   236 	CleanupStack::PushL( icon );
       
   237     icons->AppendL( icon );
       
   238 	CleanupStack::Pop( icon );
       
   239 	
       
   240 		  		        
       
   241     RPointerArray<CxSPViewInfo> infoArray;
       
   242     CleanupClosePushL( infoArray );
       
   243     iViewIdChanger.GetTabViewInfoL( infoArray );
       
   244     const TInt count = infoArray.Count();
       
   245     CDesC16ArrayFlat* texts = new (ELeave) CDesC16ArrayFlat( count );
       
   246     CleanupStack::PushL(texts);
       
   247     
       
   248     for( TInt i = 0; i < count; i++ )
       
   249     	{
       
   250     	const CxSPViewInfo& info = *infoArray[i];    		    
       
   251 	    TBool found( EFalse );
       
   252 	    for( TInt j = 0; j < iExtensions.Count() && !found; j++ )
       
   253 	    	{
       
   254 	    	if( info.Id() == iExtensions.At( j )->ExtensionFactory()->Id() )
       
   255 	    		{
       
   256 	    		TBuf<KMaxListBoxBufSize> buf;
       
   257 	    		CFbsBitmap* bitmap = NULL;
       
   258 	    		CFbsBitmap* mask = NULL;
       
   259                 //info->Name() denotes the ServiceName of the Service in SpSettings                    
       
   260                 //PBK2 ServiceManager has the brandicons of all the services installed
       
   261                 //to the device.
       
   262                 //So iterate thro the PBK2ServiceManager, to find the service which matches
       
   263                 //the info->Name()
       
   264                 //If matches use this icon else use the default icon for the tabs
       
   265                 //get the XSP ServiceName 
       
   266                         
       
   267                 // CPbk2ServiceManager stores all the brandinfo
       
   268                 // related to the services configured to the phone
       
   269                 // use this to show uniform icon throughout PhoneBook
       
   270                 MPbk2ApplicationServices2* servicesExtension = 
       
   271                     reinterpret_cast<MPbk2ApplicationServices2*>
       
   272                         ( Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   273                             MPbk2ApplicationServicesExtension(
       
   274                                 KMPbk2ApplicationServicesExtension2Uid ) );
       
   275                 CPbk2ServiceManager& servMan = servicesExtension->ServiceManager();
       
   276                 const CPbk2ServiceManager::RServicesArray& services = servMan.Services();    
       
   277                 for ( TInt i = 0; i < services.Count(); i++ )
       
   278                     {
       
   279                     const CPbk2ServiceManager::TService& service = services[i];
       
   280                     //Found the appropriate service info
       
   281                     if ( service.iName == info.Name() )
       
   282                         {
       
   283                         if ( service.iBitmap && service.iMask )
       
   284                             {
       
   285                             TSize size( 25, 25 ); //default size used in PB
       
   286                             
       
   287                             AknIconUtils::SetSize(
       
   288                                     service.iBitmap,
       
   289                                     size );
       
   290                             AknIconUtils::SetSize(
       
   291                                     service.iMask,
       
   292                                     size );                               
       
   293                             
       
   294                             bitmap = new (ELeave) CFbsBitmap;
       
   295                             CleanupStack::PushL( bitmap );
       
   296                             bitmap->Duplicate( service.iBitmap->Handle() );
       
   297                             
       
   298                             mask = new (ELeave) CFbsBitmap;
       
   299                             CleanupStack::PushL( mask );
       
   300                             mask->Duplicate( service.iMask->Handle() );
       
   301                             }
       
   302                         break;
       
   303                         }
       
   304                     } 
       
   305                 
       
   306     			buf.Format( KFormat, icons->Count() );
       
   307     			
       
   308     			if ( !bitmap )
       
   309     			    {
       
   310     			    AknIconUtils::CreateIconLC(	bitmap,	mask, info.SortIconFile(),
       
   311     									info.SortIconId(), info.SortMaskId() );
       
   312     			    }
       
   313     			
       
   314 				CGulIcon* icon = CGulIcon::NewL( bitmap, mask );
       
   315 				CleanupStack::Pop( 2 ); // mask, bitmap
       
   316 				CleanupStack::PushL( icon );
       
   317     			icons->AppendL( icon );
       
   318 				CleanupStack::Pop( icon );													
       
   319 	    					
       
   320 				buf.Append( KTab );
       
   321 	    		buf.Append( info.Name() );
       
   322 	    		buf.Append( KTab );
       
   323 	    		buf.Append( KTab );
       
   324 	    		texts->AppendL( buf );
       
   325 	    		iSortedOrder.AppendL( i ); // create the initial sort order	
       
   326 	    		found = ETrue;
       
   327 	    		}
       
   328 	    	}	        		    
       
   329     	}            	    	                        	    	    	
       
   330     
       
   331     	
       
   332     iListBox->Model()->SetItemTextArray( texts );
       
   333     iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
       
   334     CleanupStack::Pop(texts);    
       
   335     iListBox->HandleItemAdditionL();
       
   336     
       
   337     CleanupStack::PopAndDestroy(); // infoArray
       
   338         	
       
   339     CleanupStack::Pop( icons );
       
   340     
       
   341     iListBox->ItemDrawer()->ColumnData()->SetIconArray( icons );
       
   342 	}
       
   343 	
       
   344 	
       
   345 void CxSPSortViewControl::SetCurrentItemMarkedL( TBool aMark )
       
   346 	{
       
   347 	if( aMark )
       
   348 		{
       
   349 		AknSelectionService::HandleMarkableListProcessCommandL( EAknCmdMark, iListBox );
       
   350 		}
       
   351 	else
       
   352 		{
       
   353 		AknSelectionService::HandleMarkableListProcessCommandL( EAknUnmarkAll, iListBox );
       
   354 		}
       
   355 	iListBox->DrawNow();		
       
   356 	}
       
   357 	
       
   358 	
       
   359 void CxSPSortViewControl::MoveMarkedItemL()
       
   360 	{
       
   361 	TInt currentIndex = iListBox->CurrentItemIndex();
       
   362 
       
   363 	TInt markedIndex = -1;
       
   364 	
       
   365 	const CListBoxView::CSelectionIndexArray* inds = iListBox->SelectionIndexes();
       
   366 	TInt count = inds->Count();
       
   367 	if( count == 1 ) // sanity check, single selection list should have only one selection
       
   368 		{
       
   369 		markedIndex = (*inds)[0];
       
   370 		if( markedIndex == currentIndex )
       
   371 			{
       
   372 			AknSelectionService::HandleMarkableListProcessCommandL( EAknUnmarkAll, iListBox );
       
   373 			iListBox->DrawNow();
       
   374 			return;
       
   375 			}
       
   376 		}	
       
   377 	else
       
   378 		{
       
   379 		AknSelectionService::HandleMarkableListProcessCommandL( EAknUnmarkAll, iListBox );
       
   380 		iListBox->DrawNow();
       
   381 		return;
       
   382 		}
       
   383 		
       
   384 	CTextListBoxModel* model = iListBox->Model();
       
   385     CDesCArray* texts = static_cast<CDesCArray*>( model->ItemTextArray() );
       
   386     if(( texts->Count() > markedIndex ) && ( markedIndex >= 0 ))
       
   387     	{
       
   388 	    TBuf<KMaxListBoxBufSize> markedText = (*texts)[markedIndex];
       
   389 		texts->Delete( markedIndex );
       
   390 		iListBox->HandleItemRemovalL();
       
   391 
       
   392 		TUint32 moveItem = iSortedOrder[markedIndex];
       
   393 		iSortedOrder.Remove( markedIndex );
       
   394 	
       
   395 		texts->InsertL( currentIndex, markedText );
       
   396 		iListBox->HandleItemAdditionL();
       
   397 		User::LeaveIfError( iSortedOrder.Insert( moveItem, currentIndex ));
       
   398     	}
       
   399     else if( markedIndex < 0 )
       
   400     	{
       
   401     	User::Leave( KErrUnderflow );
       
   402     	}
       
   403     else
       
   404     	{
       
   405     	User::Leave( KErrOverflow );
       
   406     	}
       
   407 	
       
   408 	AknSelectionService::HandleMarkableListProcessCommandL( EAknUnmarkAll, iListBox );
       
   409 	iListBox->DrawNow();
       
   410 	}
       
   411 	
       
   412 
       
   413 TBool CxSPSortViewControl::CommitSortL( MGlobalNoteObserver* aObserver )	
       
   414 	{
       
   415     TBool changes = EFalse;
       
   416 	for( TInt i=0; i<iSortedOrder.Count(); i++)
       
   417 		{
       
   418 		// check if the order has really changed
       
   419 		if( i != iSortedOrder[i] )
       
   420 			{
       
   421 			changes = ETrue;
       
   422 			break;
       
   423 			}
       
   424 		}
       
   425 
       
   426 	if( changes )
       
   427 		{
       
   428 		// new re-sorted array of extension IDs
       
   429 		RArray<TxSPOrderUnit> idArray;
       
   430 		CleanupClosePushL( idArray );
       
   431 		RPointerArray<CxSPViewInfo> infoArray;
       
   432     	CleanupClosePushL( infoArray );
       
   433     	iViewIdChanger.GetTabViewInfoL( infoArray );
       
   434 		
       
   435 		for( TInt i = 0; i < iSortedOrder.Count(); i++ )
       
   436 			{
       
   437 			const CxSPViewInfo& info = *infoArray[iSortedOrder[i]];
       
   438 			TxSPOrderUnit orderUnit;
       
   439 			orderUnit.iId = info.Id();
       
   440 			orderUnit.iViewId = info.OldViewId();			
       
   441 			idArray.AppendL( orderUnit );
       
   442 			}
       
   443 		
       
   444 		xSPOrderOrganizer::Reorganize( idArray );				
       
   445 		
       
   446 		CleanupStack::PopAndDestroy( 2 ); // infoArray, idArray
       
   447 
       
   448 		// Load the used text label from resource
       
   449 	    HBufC* stringholder = StringLoader::LoadLC
       
   450     	    ( R_QTN_EXTENSION_MANAGER_SHUTDOWN_NOTE, iEikonEnv );
       
   451 
       
   452 	    // Create observer for listening global note dismissal 
       
   453 	    delete iGlobalNoteObserver;
       
   454 	    iGlobalNoteObserver = 0;
       
   455 	    iGlobalNoteObserver = CGlobalNoteObserver::NewL( aObserver );
       
   456 
       
   457 	    // Create and show global note
       
   458 	    // The note cannot be deleted immediately because deleting it also
       
   459 	    // causes the observer to be notified with KErrCancel
       
   460 	    delete iGlobalNote;
       
   461 	    iGlobalNote = 0;
       
   462 	    iGlobalNote = CAknGlobalNote::NewL();
       
   463 	    iGlobalNote->SetSoftkeys( R_AVKON_SOFTKEYS_OK_EMPTY );
       
   464 	    iGlobalNote->ShowNoteL(
       
   465                 iGlobalNoteObserver->iStatus,
       
   466                 EAknGlobalInformationNote,
       
   467                 *stringholder );
       
   468 
       
   469         CleanupStack::PopAndDestroy( stringholder );	
       
   470 		}	
       
   471 	return changes;
       
   472 	}
       
   473 
       
   474 // End of file