emailcontacts/contactactionmenu/src/cfsccontactactionmenumodelimpl.cpp
changeset 0 8466d47a6819
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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:  Implementation of the class CFscContactActionMenuModelImpl.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCUDES
       
    20 //<cmail>
       
    21 #include "emailtrace.h"
       
    22 #include "cfsccontactactionservice.h"
       
    23 #include "mfsccontactaction.h"
       
    24 //</cmail>
       
    25 
       
    26 #include "cfsccontactactionmenumodelimpl.h"
       
    27 #include "cfsccontactactionmenuitemimpl.h"
       
    28 #include "cfsccontactactionmenucasitemimpl.h"
       
    29 #include "fsccontactactionmenudefines.h"
       
    30 
       
    31 // ======== LOCAL FUNCTIONS ========
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CFscContactActionMenuModelImpl::NewL
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CFscContactActionMenuModelImpl* CFscContactActionMenuModelImpl::NewL(
       
    40     CFscContactActionService& aService )
       
    41     {
       
    42     FUNC_LOG;
       
    43 
       
    44     CFscContactActionMenuModelImpl* self = 
       
    45         new (ELeave) CFscContactActionMenuModelImpl( aService );
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop( self );
       
    49         
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // CFscContactActionMenuModelImpl::~CFscContactActionMenuModelImpl
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CFscContactActionMenuModelImpl::~CFscContactActionMenuModelImpl()
       
    58     {
       
    59     FUNC_LOG;
       
    60     iMenuItems.ResetAndDestroy();
       
    61     }
       
    62     
       
    63 // ---------------------------------------------------------------------------
       
    64 // CFscContactActionMenuModelImpl::NewMenuItemL
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CFscContactActionMenuItem* CFscContactActionMenuModelImpl::NewMenuItemL( 
       
    68     const TDesC& aMenuText, const CGulIcon* aIcon, TInt aPriority, 
       
    69     TBool aStatic, TUid aUid )
       
    70     {
       
    71     FUNC_LOG;
       
    72     
       
    73     CFscContactActionMenuItemImpl* newItem = 
       
    74         CFscContactActionMenuItemImpl::NewL(
       
    75             aMenuText, aIcon, aPriority, aStatic, aUid );   
       
    76     
       
    77     return newItem;
       
    78     }
       
    79     
       
    80 // ---------------------------------------------------------------------------
       
    81 // CFscContactActionMenuModelImpl::AddItemL
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 TInt CFscContactActionMenuModelImpl::AddItemL( 
       
    85     CFscContactActionMenuItem* aItem )
       
    86     {
       
    87     FUNC_LOG;
       
    88 
       
    89     // Check that item uid is unique    
       
    90     TBool found = EFalse;
       
    91     TInt itemCount = iMenuItems.Count();
       
    92     for ( TInt i = 0; i < itemCount; i++ )
       
    93         {
       
    94         if ( iMenuItems[i]->ImplementationUid() == aItem->ImplementationUid() )
       
    95             {
       
    96             found = ETrue;
       
    97             break;
       
    98             }
       
    99         }
       
   100     
       
   101     if ( found )
       
   102         {
       
   103         User::Leave( KErrAlreadyExists );
       
   104         }
       
   105 
       
   106     TLinearOrder<CFscContactActionMenuItem> order( PriorityCompare );
       
   107     iMenuItems.InsertInOrderAllowRepeatsL( aItem, order );
       
   108     
       
   109     TInt position = iMenuItems.Find( aItem );   
       
   110     return position;
       
   111     }
       
   112     
       
   113 // ---------------------------------------------------------------------------
       
   114 // CFscContactActionMenuModelImpl::CasQueryResults
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 void CFscContactActionMenuModelImpl::AddPreQueriedCasItemsL()
       
   118     {
       
   119     FUNC_LOG;
       
   120     
       
   121     const CFscContactActionList& results = Service()->QueryResults();
       
   122     
       
   123     // Remove old non-static items
       
   124     RemoveAll();
       
   125     
       
   126     // Add actions to the menu
       
   127     for ( TInt i = 0; i < results.Count(); i++ )
       
   128         {
       
   129         CFscContactActionMenuItem* newItem = 
       
   130             CFscContactActionMenuCasItemImpl::NewLC( results[i] );
       
   131         TLinearOrder<CFscContactActionMenuItem> order( PriorityCompare );
       
   132         iMenuItems.InsertInOrderAllowRepeatsL( newItem, order );
       
   133         CleanupStack::Pop( newItem );
       
   134         }
       
   135     
       
   136     }
       
   137     
       
   138 // ---------------------------------------------------------------------------
       
   139 // CFscContactActionMenuModelImpl::RemoveItemL
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CFscContactActionMenuModelImpl::RemoveItemL( TInt aIndex )
       
   143     {
       
   144     FUNC_LOG;
       
   145     
       
   146     if ( aIndex < 0 || aIndex >= iMenuItems.Count() )
       
   147         {
       
   148         User::Leave( KErrArgument );
       
   149         }
       
   150     delete iMenuItems[aIndex];
       
   151     iMenuItems.Remove( aIndex );    
       
   152  
       
   153     }
       
   154     
       
   155 // ---------------------------------------------------------------------------
       
   156 // CFscContactActionMenuModelImpl::RemoveAll
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void CFscContactActionMenuModelImpl::RemoveAll( TBool aStaticAlso )
       
   160     {
       
   161     FUNC_LOG;
       
   162     
       
   163     if ( aStaticAlso )
       
   164         {
       
   165         iMenuItems.ResetAndDestroy();
       
   166         }
       
   167     else
       
   168         {
       
   169         for ( TInt i = iMenuItems.Count() - 1; i >= 0; i-- )
       
   170             {
       
   171             // CAS items are always deleted even if client had changed 
       
   172             // them to statics
       
   173             CFscContactActionMenuCasItemImpl* temp = 
       
   174                 dynamic_cast<CFscContactActionMenuCasItemImpl*>( 
       
   175                     iMenuItems[i] );
       
   176             if ( !iMenuItems[i]->Static() || 
       
   177                  temp != NULL )
       
   178                 {
       
   179                 delete iMenuItems[i];  
       
   180                 iMenuItems.Remove( i ); 
       
   181                 }
       
   182             }
       
   183         }
       
   184     
       
   185     }
       
   186     
       
   187 // ---------------------------------------------------------------------------
       
   188 // CFscContactActionMenuModelImpl::ItemL
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 CFscContactActionMenuItem& CFscContactActionMenuModelImpl::ItemL( TInt aIndex )
       
   192     {
       
   193     FUNC_LOG;
       
   194     
       
   195     if ( aIndex < 0 || aIndex >= iMenuItems.Count() )
       
   196         {
       
   197         User::Leave( KErrArgument );
       
   198         }
       
   199         
       
   200     return *iMenuItems[aIndex];
       
   201     }
       
   202     
       
   203 // ---------------------------------------------------------------------------
       
   204 // CFscContactActionMenuModelImpl::ItemCount
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 TInt CFscContactActionMenuModelImpl::ItemCount()
       
   208     {
       
   209     FUNC_LOG;
       
   210     TInt itemCount = iMenuItems.Count();
       
   211     return itemCount;
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 // CFscContactActionMenuModelImpl::VisibleItemL
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 CFscContactActionMenuItem& CFscContactActionMenuModelImpl::VisibleItemL( 
       
   219     TInt aIndex )
       
   220     {
       
   221     FUNC_LOG;
       
   222     
       
   223     CFscContactActionMenuItem* foundItem = NULL;
       
   224     TInt itemCount = iMenuItems.Count();
       
   225     TInt visibleIndex = KErrNotFound;
       
   226     for ( TInt i = 0; i < itemCount; i++ )
       
   227         {
       
   228         CFscContactActionMenuItem& item = *iMenuItems[i];
       
   229         if ( IsVisible( item ) )
       
   230             {
       
   231             visibleIndex++;
       
   232             }
       
   233         if ( visibleIndex == aIndex )
       
   234             {
       
   235             foundItem = iMenuItems[i];
       
   236             break;
       
   237             }    
       
   238         }  
       
   239     
       
   240     if ( foundItem == NULL )
       
   241         {
       
   242         User::Leave( KErrArgument );
       
   243         }
       
   244         
       
   245     return *foundItem;
       
   246     }
       
   247     
       
   248 // ---------------------------------------------------------------------------
       
   249 // CFscContactActionMenuModelImpl::VisibleItemCount
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 TInt CFscContactActionMenuModelImpl::VisibleItemCount()
       
   253     {
       
   254     FUNC_LOG;
       
   255     TInt itemCount = iMenuItems.Count();
       
   256     TInt visibleCount = 0;
       
   257     for ( TInt i = 0; i < itemCount; i++ )
       
   258         {
       
   259         CFscContactActionMenuItem& item = *iMenuItems[i];
       
   260         if ( IsVisible( item ) )
       
   261             {
       
   262             visibleCount++;
       
   263             }
       
   264         } 
       
   265     return visibleCount;
       
   266     }  
       
   267     
       
   268 // ---------------------------------------------------------------------------
       
   269 // CFscContactActionMenuModelImpl::ServiceL
       
   270 // ---------------------------------------------------------------------------
       
   271 //
       
   272 CFscContactActionService* CFscContactActionMenuModelImpl::Service()
       
   273     {
       
   274     FUNC_LOG;
       
   275     
       
   276     return &iCas;
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CFscContactActionMenuModelImpl::CFscContactActionMenuModelImpl
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 CFscContactActionMenuModelImpl::CFscContactActionMenuModelImpl( 
       
   284     CFscContactActionService& aService ) 
       
   285     : iCas( aService )
       
   286     {
       
   287     FUNC_LOG;
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // CFscContactActionMenuModelImpl::ConstructL
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CFscContactActionMenuModelImpl::ConstructL()
       
   295     {
       
   296     FUNC_LOG;
       
   297     }   
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // CFscContactActionMenuModelImpl::SortMenuItems
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CFscContactActionMenuModelImpl::SortMenuItems()
       
   304     {
       
   305     FUNC_LOG;
       
   306     TLinearOrder<CFscContactActionMenuItem> order( PriorityCompare );
       
   307     iMenuItems.Sort( order );
       
   308     }
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // CFscContactActionMenuModelImpl::IsVisible
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 TBool CFscContactActionMenuModelImpl::IsVisible( 
       
   315     const CFscContactActionMenuItem& aItem )
       
   316     {
       
   317     FUNC_LOG;
       
   318     TBool result = ( aItem.Icon() != NULL &&
       
   319         !aItem.Dimmed() && !aItem.Hidden() );
       
   320     return result;
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // CFscContactActionMenuModelImpl::PriorityCompare
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 TInt CFscContactActionMenuModelImpl::PriorityCompare( 
       
   328     const CFscContactActionMenuItem& aItem1,
       
   329     const CFscContactActionMenuItem& aItem2 )
       
   330     {
       
   331     FUNC_LOG;
       
   332     TInt result = aItem2.Priority() - aItem1.Priority();
       
   333     return result;
       
   334     }
       
   335