phonebookui/Phonebook2/NamesListExtension/src/CPbk2NlxReorderingModel.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Phonebook 2 reordering model.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CPbk2NlxReorderingModel.h"
       
    21 
       
    22 #include <CPbk2ContactIconsUtils.h>
       
    23 
       
    24 // Virtual Phonebook
       
    25 #include <CVPbkContactLinkArray.h>
       
    26 #include <CVPbkContactManager.h>
       
    27 #include <MVPbkContactViewBase.h>
       
    28 #include <MVPbkContactLink.h>
       
    29 #include <MPbk2ContactNameFormatter.h>
       
    30 #include <MVPbkViewContact.h>
       
    31 #include <Pbk2UIControls.rsg>
       
    32 #include <CPbk2IconArray.h>
       
    33 #include <MPbk2ContactUiControlExtension.h>
       
    34 #include <MPbk2ContactUiControlExtension2.h>
       
    35 #include <MPbk2UIExtensionFactory.h>
       
    36 #include <CPbk2UIExtensionManager.h>
       
    37 
       
    38 namespace
       
    39 	{
       
    40 _LIT(KListItemFormat, "\t%S");
       
    41 _LIT(KListItemFormatIcon, "%d\t%S");	
       
    42 
       
    43 const TInt KDefaultListFormatting =
       
    44         MPbk2ContactNameFormatter::EUseSeparator |
       
    45         MPbk2ContactNameFormatter::EPreserveLeadingSpaces;
       
    46 
       
    47 #ifdef _DEBUG
       
    48 enum TPanicCode
       
    49     {
       
    50     EPanicPreCond_IconArrayNotOurs = 1
       
    51     };
       
    52 
       
    53 void Panic(TPanicCode aReason)
       
    54     {
       
    55     _LIT(KPanicText, "CPbk2NlxReorderingModel");
       
    56     User::Panic(KPanicText, aReason);
       
    57     }
       
    58 #endif // _DEBUG
       
    59 	}
       
    60 
       
    61 // --------------------------------------------------------------------------
       
    62 // CPbk2NlxReorderingModel::CPbk2NlxReorderingModel
       
    63 // --------------------------------------------------------------------------
       
    64 CPbk2NlxReorderingModel::CPbk2NlxReorderingModel(
       
    65 		const MVPbkContactViewBase& aView,
       
    66 		MPbk2ContactNameFormatter& aFormatter ):
       
    67 		iView(aView),
       
    68 		iFormatter(aFormatter),
       
    69 		iIconsOwnership(ETrue)
       
    70 	{
       
    71 	}
       
    72 
       
    73 // --------------------------------------------------------------------------
       
    74 // CPbk2NlxReorderingModel::~CPbk2NlxReorderingModel
       
    75 // --------------------------------------------------------------------------
       
    76 CPbk2NlxReorderingModel::~CPbk2NlxReorderingModel()
       
    77 	{
       
    78 	delete iContactIcons;
       
    79 	delete iUiExtension;
       
    80 	if ( iIconsOwnership )
       
    81 	    {
       
    82 	    delete iIcons;
       
    83 	    }
       
    84 	iReorderingMap.Close();
       
    85 	delete iFocusedItem;
       
    86 	}
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CPbk2NlxReorderingModel::NewL
       
    90 // --------------------------------------------------------------------------
       
    91 CPbk2NlxReorderingModel* CPbk2NlxReorderingModel::NewL(
       
    92         CVPbkContactManager& aContactManager,
       
    93         const MVPbkContactViewBase& aView,
       
    94         MPbk2ContactNameFormatter& aFormatter,
       
    95         CPbk2StorePropertyArray& aStoreProperties )
       
    96 	{
       
    97 	CPbk2NlxReorderingModel* self = new(ELeave) CPbk2NlxReorderingModel(
       
    98 			aView, aFormatter);
       
    99 	CleanupStack::PushL(self);
       
   100 	self->ConstructL( aContactManager, aStoreProperties );
       
   101 	CleanupStack::Pop(self);
       
   102 	return self;
       
   103 	}
       
   104 
       
   105 // --------------------------------------------------------------------------
       
   106 // CPbk2NlxReorderingModel::ConstructL
       
   107 // --------------------------------------------------------------------------
       
   108 void CPbk2NlxReorderingModel::ConstructL(
       
   109         CVPbkContactManager& aContactManager,
       
   110         CPbk2StorePropertyArray& aStoreProperties )
       
   111 	{
       
   112 	iIcons = CPbk2ContactIconsUtils::CreateListboxIconArrayL(R_PBK2_CONTACT_LIST_ICONS);
       
   113 	
       
   114     CPbk2UIExtensionManager* extManager =
       
   115         CPbk2UIExtensionManager::InstanceL();
       
   116     extManager->PushL();
       
   117 
       
   118     iUiExtension = 
       
   119         extManager->FactoryL()->CreatePbk2UiControlExtensionL( aContactManager );
       
   120     CleanupStack::PopAndDestroy(); // extManager
       
   121     
       
   122     if ( iUiExtension )
       
   123         {
       
   124         MPbk2ContactUiControlExtension2* ext = reinterpret_cast
       
   125             <MPbk2ContactUiControlExtension2*>( iUiExtension->
       
   126                 ContactUiControlExtensionExtension(
       
   127                     KMPbk2ContactUiControlExtensionExtension2Uid ) );
       
   128         if ( ext != NULL )
       
   129             {
       
   130             CPbk2IconArray* extIconArray = const_cast<CPbk2IconArray*>(iIcons);
       
   131             ext->SetIconArray( *extIconArray );
       
   132             }
       
   133         }
       
   134 
       
   135 	iContactIcons = CPbk2ContactIconsUtils::NewL(
       
   136 	        aStoreProperties,
       
   137 	        iUiExtension );
       
   138 
       
   139 	//init mapping array
       
   140 	for ( TInt i = 0; i < iView.ContactCountL(); i++ )
       
   141 		{
       
   142 		User::LeaveIfError( iReorderingMap.Append( i ) );
       
   143 		}
       
   144 	}
       
   145 
       
   146 // --------------------------------------------------------------------------
       
   147 // CPbk2NlxReorderingModel::Move
       
   148 // --------------------------------------------------------------------------
       
   149 void CPbk2NlxReorderingModel::Move(TInt aFrom, TInt aTo)
       
   150 	{
       
   151 	if ( aFrom > aTo )
       
   152 		{
       
   153 		iReorderingMap.Insert( iReorderingMap[aFrom], aTo);
       
   154 		iReorderingMap.Remove( aFrom + 1 );
       
   155 		}
       
   156 	else if ( aTo > aFrom )
       
   157 		{
       
   158 		iReorderingMap.Insert( iReorderingMap[aFrom], aTo + 1 );
       
   159 		iReorderingMap.Remove( aFrom );
       
   160 		}
       
   161 	}
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CPbk2NlxReorderingModel::MdcaCount
       
   165 // --------------------------------------------------------------------------
       
   166 TInt CPbk2NlxReorderingModel::MdcaCount() const
       
   167 	{
       
   168 	return iReorderingMap.Count();
       
   169 	}
       
   170 
       
   171 // --------------------------------------------------------------------------
       
   172 // Formats contact item for listbox.
       
   173 // --------------------------------------------------------------------------
       
   174 inline void CPbk2NlxReorderingModel::FormatItemL(TDes& aBuf, TInt aIndex) const
       
   175 	{
       
   176 	RArray<TPbk2IconId> ids;
       
   177 	CleanupClosePushL( ids );
       
   178 	iContactIcons->GetIconIdsForContactL( iView.ContactAtL(aIndex), ids );
       
   179 	
       
   180 	HBufC* title = iFormatter.GetContactTitleL(
       
   181 			iView.ContactAtL(aIndex).Fields(),
       
   182 			KDefaultListFormatting );
       
   183 	CleanupStack::PushL(title);
       
   184 	TInt iconIndex = KErrNotFound;
       
   185 	if ( ids.Count() > 0 )
       
   186 	    {
       
   187 	    iconIndex = iIcons->FindIcon( ids[0] );
       
   188 	    }
       
   189 	if ( iconIndex == KErrNotFound)
       
   190 	    {
       
   191 	    aBuf.Format(KListItemFormat, title);
       
   192 	    }
       
   193 	else
       
   194 	    {
       
   195 	    aBuf.Format(KListItemFormatIcon, iconIndex, title);
       
   196 	    }
       
   197 	CleanupStack::PopAndDestroy(title);
       
   198 	CleanupStack::PopAndDestroy(); //ids
       
   199 	}
       
   200 
       
   201 // --------------------------------------------------------------------------
       
   202 // Backup formatting for error cases.
       
   203 // --------------------------------------------------------------------------
       
   204 inline void CPbk2NlxReorderingModel::FormatEmptyItem(TDes& aBuf ) const
       
   205 	{
       
   206 	TPtrC emptyTitle( KNullDesC );
       
   207 	aBuf.Format(KListItemFormat, &emptyTitle);
       
   208 	}
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CPbk2NlxReorderingModel::MdcaPoint
       
   212 // --------------------------------------------------------------------------
       
   213 TPtrC CPbk2NlxReorderingModel::MdcaPoint(TInt aIndex) const
       
   214 	{
       
   215 	//remove const from iBuffer to be able to format in const method
       
   216 	TRAPD(err, FormatItemL( const_cast< TBuf<EMaxListBoxText>& >( iBuffer ),
       
   217 						   iReorderingMap[aIndex]));
       
   218 	if ( err != KErrNone )
       
   219 		{
       
   220 		FormatEmptyItem( const_cast< TBuf<EMaxListBoxText>& >( iBuffer ) );		
       
   221 		}
       
   222 	return iBuffer;
       
   223 	}
       
   224 
       
   225 // --------------------------------------------------------------------------
       
   226 // CPbk2NlxReorderingModel::FocusedContactIndex
       
   227 // --------------------------------------------------------------------------
       
   228 TInt CPbk2NlxReorderingModel::FocusedContactIndex() const
       
   229 	{
       
   230 	TInt result = KErrNotFound;
       
   231 	if ( iFocusedItem )
       
   232 		{
       
   233 		result = ContactIndex( *iFocusedItem );
       
   234 		}
       
   235 	return result;
       
   236 	}
       
   237 
       
   238 // --------------------------------------------------------------------------
       
   239 // CPbk2NlxReorderingModel::ContactIndex
       
   240 // --------------------------------------------------------------------------
       
   241 TInt CPbk2NlxReorderingModel::ContactIndex(
       
   242         const MVPbkContactLink& aContact ) const
       
   243     {
       
   244     TInt result = KErrNotFound;
       
   245     TInt viewIndex = KErrNotFound;
       
   246     TRAP_IGNORE( viewIndex = iView.IndexOfLinkL( aContact ) );
       
   247     if ( viewIndex >= 0 )
       
   248         {
       
   249         result = iReorderingMap.Find( viewIndex );
       
   250         }
       
   251     return result;
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CPbk2NlxReorderingModel::FocusedItem
       
   256 // --------------------------------------------------------------------------
       
   257 void CPbk2NlxReorderingModel::SetFocusedContactL(
       
   258 		const MVPbkContactLink& aFocusedLink )
       
   259 	{
       
   260 	delete iFocusedItem;
       
   261 	iFocusedItem = NULL;
       
   262 	iFocusedItem = aFocusedLink.CloneLC();
       
   263 	CleanupStack::Pop();
       
   264 	}
       
   265 
       
   266 // --------------------------------------------------------------------------
       
   267 // CPbk2NlxReorderingModel::FocusedItem
       
   268 // --------------------------------------------------------------------------
       
   269 void CPbk2NlxReorderingModel::SetFocusedContactL( TInt aFocusedIndex )
       
   270 	{
       
   271 	delete iFocusedItem;
       
   272 	iFocusedItem = NULL;
       
   273 	iFocusedItem = iView.CreateLinkLC( iReorderingMap[aFocusedIndex] );
       
   274 	CleanupStack::Pop();
       
   275 	}
       
   276 
       
   277 // --------------------------------------------------------------------------
       
   278 // CPbk2NlxReorderingModel::FocusedItem
       
   279 // --------------------------------------------------------------------------
       
   280 MVPbkContactLink*  CPbk2NlxReorderingModel::FocusedContactLinkL() const
       
   281 	{
       
   282 	MVPbkContactLink* result = NULL;
       
   283 	if ( iFocusedItem )
       
   284 	    {
       
   285 	    result = iFocusedItem->CloneLC();
       
   286 	    CleanupStack::Pop();
       
   287 	    }
       
   288 	return result;
       
   289 	}
       
   290 
       
   291 // --------------------------------------------------------------------------
       
   292 // CPbk2NlxReorderingModel::ReorderedLinksLC
       
   293 // --------------------------------------------------------------------------
       
   294 MVPbkContactLinkArray* CPbk2NlxReorderingModel::ReorderedLinksLC() const
       
   295 	{
       
   296 	CVPbkContactLinkArray* result = CVPbkContactLinkArray::NewLC();
       
   297 	for( TInt i = 0; i < iReorderingMap.Count(); i++ )
       
   298 		{
       
   299 		MVPbkContactLink* link = iView.CreateLinkLC( iReorderingMap[i] );
       
   300 		result->AppendL( link );
       
   301 		CleanupStack::Pop();
       
   302 		}
       
   303 	return result;
       
   304 	}
       
   305 
       
   306 CPbk2IconArray* CPbk2NlxReorderingModel::TakeIconArray()
       
   307     {
       
   308     __ASSERT_DEBUG( iIconsOwnership, Panic(EPanicPreCond_IconArrayNotOurs));
       
   309     iIconsOwnership = EFalse;
       
   310     return iIcons;
       
   311     }
       
   312 // --------------------------------------------------------------------------
       
   313 // CPbk2NlxReorderingModel::SetContactUpdater
       
   314 // --------------------------------------------------------------------------
       
   315 void CPbk2NlxReorderingModel::SetContactUpdater(
       
   316         MPbk2ContactUiControlUpdate* aContactUpdator )
       
   317     {
       
   318     if ( iUiExtension )
       
   319         {
       
   320         iUiExtension->SetContactUiControlUpdate( aContactUpdator );
       
   321         }
       
   322     }
       
   323 // End of File