profilesapplication/Profiles/ProfileApp/SettingsViewSrc/CProfileAlertForSettingItem.cpp
branchRCL_3
changeset 19 cd54903d48da
equal deleted inserted replaced
18:b7fa36b488f8 19:cd54903d48da
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Setting item class for "Alert for" setting.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include "CProfileAlertForSettingItem.h"
       
    22 
       
    23 #include <coemain.h>
       
    24 #include <cntdb.h>
       
    25 #include <cntitem.h>
       
    26 #include <StringLoader.h>
       
    27 #include <CPbkContactEngine.h>
       
    28 #include <CPbkContactChangeNotifier.h>
       
    29 #include <profilesettingsview.rsg>
       
    30 
       
    31 #include "CProfileAlertForSettingPage.h"
       
    32 
       
    33 // CONSTANTS
       
    34 const TInt KContactItemArrayGranularity( 8 );
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CProfileAlertForSettingItem::CProfileAlertForSettingItem
       
    40 // C++ constructor can NOT contain any code, that might leave.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CProfileAlertForSettingItem::CProfileAlertForSettingItem(
       
    44     TInt aIdentifier,
       
    45     RArray<TContactItemId>& aContactList,
       
    46     CCoeEnv* aCoeEnv )
       
    47     :   CAknSettingItem( aIdentifier ),
       
    48         iPopupArray( KContactItemArrayGranularity ),
       
    49         iExternalContactArray( aContactList ),
       
    50         iCoeEnv( aCoeEnv )
       
    51     {
       
    52     }
       
    53 
       
    54 // Destructor
       
    55 CProfileAlertForSettingItem::~CProfileAlertForSettingItem()
       
    56     {
       
    57     delete iChangeNotifier;
       
    58     delete iPbkContactEngine;
       
    59     delete iTextSeveralGroups;
       
    60     delete iTextAllCallers;
       
    61     iFullContactArray.Close();
       
    62     iPopupArray.ResetAndDestroy();
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CProfileAlertForSettingItem::CompleteConstructionL
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CProfileAlertForSettingItem::CompleteConstructionL()
       
    70     {
       
    71     // Read localised texts from resource
       
    72     iTextAllCallers = StringLoader::LoadL( R_PROFILE_TEXT_ALL_CALLERS );
       
    73 	iTextSeveralGroups = StringLoader::LoadL( R_PROFILE_TEXT_SEVERAL_GROUPS );
       
    74 
       
    75     // Create Phonebook contact engine and change notifier
       
    76     iPbkContactEngine = CPbkContactEngine::NewL();
       
    77     iChangeNotifier = iPbkContactEngine->CreateContactChangeNotifierL( this );
       
    78 
       
    79     PopulatePopupArrayL();
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CProfileAlertForSettingItem::AlertForStatus
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CProfileAlertForSettingItem::TAlertForStatus CProfileAlertForSettingItem::AlertForStatus() const
       
    87     {
       
    88     TInt count( iPopupArray.Count() );
       
    89     if( count > 1 )
       
    90         {
       
    91         // Array contains 2 or more items
       
    92         if( SelectionStatus( 0 ) )
       
    93             {
       
    94             // If first item is selected, return EAlertForAllCallers
       
    95             return EAlertForAllCallers;
       
    96             }
       
    97         TInt selectCount( 0 );
       
    98         // Start from index 1, because 0 is "All callers"
       
    99         for( TInt index( 1 ); index < count; index++ )
       
   100             {
       
   101             if( SelectionStatus( index ) )
       
   102                 {
       
   103                 // Found a selected item, increase counter
       
   104                 selectCount++;
       
   105                 }
       
   106             }
       
   107         if( selectCount == 1 )
       
   108             {
       
   109             return EAlertForSingleGroup;
       
   110             }
       
   111         return EAlertForSeveralGroups;
       
   112         }
       
   113     // Pop-up array is empty or contains only "All callers"
       
   114     return EAlertForAllCallers;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CProfileAlertForSettingItem::SetSelectionStatus
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CProfileAlertForSettingItem::SetSelectionStatus( TInt aIndex, TBool aStatus )
       
   122     {
       
   123     if( aIndex < iPopupArray.MdcaCount()  )
       
   124 	    {
       
   125     	iPopupArray.At( aIndex )->SetSelectionStatus( aStatus );
       
   126 	    }
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CProfileAlertForSettingItem::SelectionStatus
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 TBool CProfileAlertForSettingItem::SelectionStatus( TInt aIndex ) const
       
   134     {
       
   135     if( aIndex < iPopupArray.MdcaCount()  )
       
   136 	    {
       
   137 	    return iPopupArray.At( aIndex )->SelectionStatus();
       
   138 	    }
       
   139 	else
       
   140 		{
       
   141 		return EFalse;
       
   142 		}	    
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CProfileAlertForSettingItem::PopulatePopupArrayL
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CProfileAlertForSettingItem::PopulatePopupArrayL()
       
   150     {
       
   151     iPopupArray.ResetAndDestroy();
       
   152     iFullContactArray.Reset();
       
   153 
       
   154     // Create first item to popup array, "All callers"
       
   155     CSelectableItem* item = new( ELeave ) CSelectableItem(
       
   156         *iTextAllCallers, EFalse );
       
   157     CleanupStack::PushL( item );
       
   158     item->ConstructL();
       
   159     iPopupArray.AppendL( item ); // Ownership is transferred
       
   160     CleanupStack::Pop( item );
       
   161 
       
   162     CContactIdArray* groupIdArray = iPbkContactEngine->Database().GetGroupIdListL();
       
   163     CleanupStack::PushL( groupIdArray );
       
   164     TInt count( groupIdArray->Count() );
       
   165     TContactItemId contactId;
       
   166     CContactGroup* contactGroup = NULL;
       
   167     TPtrC contactGroupName;
       
   168     for( TInt index( 0 ); index < count; index++ )
       
   169         {
       
   170         contactId = ( *groupIdArray )[ index ];
       
   171 
       
   172         iFullContactArray.Append( contactId );
       
   173         contactGroup = iPbkContactEngine->ReadContactGroupL( contactId );
       
   174         CleanupStack::PushL( contactGroup );
       
   175         contactGroupName.Set( contactGroup->GetGroupLabelL() );
       
   176 
       
   177         item = new( ELeave ) CSelectableItem( contactGroupName, EFalse );
       
   178         CleanupStack::PushL( item );
       
   179         item->ConstructL();
       
   180         iPopupArray.AppendL( item ); // Ownership is transferred
       
   181         CleanupStack::Pop( item ); 
       
   182         
       
   183         CleanupStack::PopAndDestroy( contactGroup );
       
   184         }
       
   185 
       
   186     CleanupStack::PopAndDestroy( groupIdArray );
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CProfileAlertForSettingItem::CheckAllCallersSetting
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CProfileAlertForSettingItem::CheckAllCallersSetting()
       
   194     {
       
   195     TInt count( iPopupArray.Count() );
       
   196     if( count > 0 )
       
   197         {
       
   198         // Check whether the first item, "All callers", is selected or not.
       
   199         if( SelectionStatus( 0 ) )
       
   200             {
       
   201             // "All callers" is selected.
       
   202             // Set other groups unselected.
       
   203             for( TInt index( 1 ); index < count; index++ )
       
   204                 {
       
   205                 SetSelectionStatus( index, EFalse );
       
   206                 }
       
   207             }
       
   208         }
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CProfileAlertForSettingItem::UpdateSettingPageL
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CProfileAlertForSettingItem::UpdateSettingPageL()
       
   216     {
       
   217     // Closing and opening the database has  because
       
   218     // the database doesn't update its information otherwise
       
   219     iPbkContactEngine->Database().CloseTables();
       
   220     iPbkContactEngine->Database().OpenTablesL();
       
   221 
       
   222     RArray<TContactItemId> selectedContacts;
       
   223     CleanupClosePushL( selectedContacts );
       
   224     StoreL( selectedContacts );
       
   225     PopulatePopupArrayL();
       
   226     LoadL( selectedContacts );
       
   227     CleanupStack::PopAndDestroy(); // selectedContacts
       
   228 
       
   229     UpdateListBoxTextL(); // Calls SettingTextL()
       
   230 
       
   231     if( SettingPage() )
       
   232         {
       
   233         // Pop-up is popped up
       
   234         CProfileAlertForSettingPage* sp =
       
   235             static_cast< CProfileAlertForSettingPage* >( SettingPage() );
       
   236         TInt lastIndex( iPopupArray.Count() - 1 );
       
   237         TInt currentIndex( sp->ListBoxControl()->View()->CurrentItemIndex() );
       
   238         if( currentIndex > lastIndex )
       
   239             {
       
   240             sp->ListBoxControl()->SetCurrentItemIndex( lastIndex );
       
   241             }
       
   242         if( currentIndex < 0 )
       
   243             {
       
   244             sp->ListBoxControl()->SetCurrentItemIndex( 0 );
       
   245             }
       
   246 
       
   247         // Must clear listbox view's selections, because if items have been deleted,
       
   248         // non existent selections remain in view's selection array.
       
   249         sp->ListBoxControl()->View()->ClearSelection();
       
   250         // Set the new selection array to setting page
       
   251         sp->SetSelectionItemArrayL( &iPopupArray );
       
   252         // Update possibly changed CBA visibility
       
   253         sp->UpdateCba();
       
   254         }
       
   255     else // Setting page is not visible
       
   256         {
       
   257         LoadL();
       
   258         }
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CProfileAlertForSettingItem::StoreL
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CProfileAlertForSettingItem::StoreL( RArray<TContactItemId>& aArray ) const
       
   266     {
       
   267     aArray.Reset();
       
   268     TInt count( iPopupArray.Count() );
       
   269     // Start from index 1 (0 is 'All callers')
       
   270     for( TInt index( 1 ); index < count; index++ )
       
   271         {
       
   272         // If an item in array is selected
       
   273         if( SelectionStatus( index ) )
       
   274             {
       
   275             // Add its id to external contact array
       
   276             aArray.Append( iFullContactArray[ index - 1 ] );
       
   277             }
       
   278         }
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CProfileAlertForSettingItem::LoadL
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 void CProfileAlertForSettingItem::LoadL( const RArray<TContactItemId>& aArray )
       
   286     {
       
   287     TInt index( 0 );
       
   288     TInt count( iPopupArray.Count() );
       
   289 
       
   290     // Set all selections false
       
   291     for( ; index < count; index++ )
       
   292         {
       
   293         SetSelectionStatus( index, EFalse );
       
   294         }
       
   295 
       
   296     TContactItemId contactId;
       
   297     TInt foundCount( 0 );
       
   298     TInt foundIndex;
       
   299     count = aArray.Count();
       
   300     for( index = 0; index < count; index++ )
       
   301         {
       
   302         contactId = aArray[ index ];
       
   303         // Try to find contact id from full contact group array
       
   304         foundIndex = iFullContactArray.Find( contactId );
       
   305         if( foundIndex >= 0 )
       
   306             {
       
   307             // Indices in iFullContactArray are the same as in iPopupArray
       
   308             // except that iPopupArray's 1st item is "All callers". Therefore + 1.
       
   309             SetSelectionStatus( foundIndex + 1, ETrue );
       
   310             // A valid contact id is found, increase counter
       
   311             foundCount++;
       
   312             }
       
   313         }
       
   314 
       
   315     // If there were no contact groups selected,
       
   316     // or selected groups are not longer valid, select "All callers"
       
   317     if( foundCount == 0 )
       
   318         {
       
   319         SetSelectionStatus( 0, ETrue );
       
   320         }
       
   321     }
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // CProfileAlertForSettingItem::StoreL
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 void CProfileAlertForSettingItem::StoreL()
       
   328     {
       
   329     StoreL( iExternalContactArray );
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CProfileAlertForSettingItem::LoadL
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CProfileAlertForSettingItem::LoadL()
       
   337     {
       
   338     LoadL( iExternalContactArray );
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CProfileAlertForSettingItem::EditItemL
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void CProfileAlertForSettingItem::EditItemL( TBool /* aCalledFromMenu */ )
       
   346     {
       
   347     CProfileAlertForSettingPage* dlg = new( ELeave ) CProfileAlertForSettingPage(
       
   348         SettingPageResourceId(), &iPopupArray );
       
   349     SetSettingPage( dlg ); // Set iSettingPage to indicate that pop-up is displayed
       
   350     dlg->SetSettingPageObserver( this );
       
   351     dlg->ExecuteLD();
       
   352     SetSettingPage( NULL ); // iSettingPage is NULL, pop-up is closed
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CProfileAlertForSettingItem::SettingTextL
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 const TDesC& CProfileAlertForSettingItem::SettingTextL()
       
   360     {
       
   361     switch( AlertForStatus() )
       
   362         {
       
   363         case EAlertForAllCallers:
       
   364             {
       
   365             iSettingText = *iTextAllCallers;
       
   366             break;
       
   367             }
       
   368         case EAlertForSingleGroup:
       
   369             {
       
   370             TInt count( iPopupArray.Count() );
       
   371             if( count > 0 )
       
   372                 {
       
   373                 // Search the selected group
       
   374                 while( !SelectionStatus( --count ) && count > 1 ) {}
       
   375                 // Get its name
       
   376                 iSettingText = iPopupArray.At( count )->ItemText();
       
   377                 }
       
   378             break;
       
   379             }
       
   380         case EAlertForSeveralGroups:
       
   381             {
       
   382             iSettingText = *iTextSeveralGroups;
       
   383             break;
       
   384             }
       
   385         default:
       
   386             {
       
   387             break;
       
   388             }
       
   389         }
       
   390     return iSettingText;
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CProfileAlertForSettingItem::HandleDatabaseEventL
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 void CProfileAlertForSettingItem::HandleDatabaseEventL( TContactDbObserverEvent aEvent )
       
   398     {
       
   399     if( ( aEvent.iType == EContactDbObserverEventGroupAdded ) || // A contact item group has been added.
       
   400         ( aEvent.iType == EContactDbObserverEventGroupChanged ) || // A contact item group has been updated.
       
   401         ( aEvent.iType == EContactDbObserverEventGroupDeleted ) ) // A contact item group has been deleted from the database.
       
   402         {
       
   403         UpdateSettingPageL();
       
   404         }
       
   405     }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CProfileAlertForSettingItem::HandleSettingPageEventL
       
   409 // After settings have been accepted, some checks are made in this method.
       
   410 // If "Alert for" is selected , all other groups must be unselected.
       
   411 // If nothing is selected , "Alert for" must be selected.
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 void CProfileAlertForSettingItem::HandleSettingPageEventL(
       
   415     CAknSettingPage* aSettingPage, TAknSettingPageEvent aEventType )
       
   416     {
       
   417     switch( aEventType )
       
   418         {
       
   419         case EEventSettingOked:
       
   420             {
       
   421             // Check that "All callers" is in correct state
       
   422             CheckAllCallersSetting();
       
   423             break;
       
   424             }
       
   425         case EEventSettingCancelled:
       
   426             {
       
   427             // If setting is cancelled, load old values
       
   428             LoadL();
       
   429             break;
       
   430             }
       
   431         case EEventSettingChanged:
       
   432             {
       
   433             // If setting has changed, update CBA visibility
       
   434             static_cast<CProfileAlertForSettingPage*>( aSettingPage )->UpdateCba();
       
   435             break;
       
   436             }
       
   437         default:
       
   438             {
       
   439             break;
       
   440             }
       
   441         }
       
   442     CAknSettingItem::HandleSettingPageEventL( aSettingPage, aEventType );
       
   443     }
       
   444 
       
   445 
       
   446 //  End of File