phonebookui/Phonebook2/UIControls/src/CPbk2MergeConflictsDlg.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-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: 
       
    15 *       Provides Conflict resolution dialog for Contact Merge command.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <avkon.rsg>
       
    20 #include <aknlists.h>
       
    21 #include <eikclbd.h>
       
    22 #include <AknIconArray.h>
       
    23 #include <eikapp.h>
       
    24 #include <gulicon.h>
       
    25 #include <CPbk2IconFactory.h>
       
    26 #include <Pbk2UID.h>
       
    27 #include <Pbk2IconId.hrh>
       
    28 #include <TPbk2IconId.h>
       
    29 #include <StringLoader.h>
       
    30 #include <aknnavide.h>
       
    31 #include <akntitle.h>
       
    32 #include <Pbk2UIControls.rsg>
       
    33 #include <MPbk2MergeResolver.h>
       
    34 #include <MPbk2MergeConflict.h>
       
    35 #include "CPbk2MergeConflictsDlg.h"
       
    36 
       
    37 /// Unnamed namespace for local definitions
       
    38 namespace {
       
    39 
       
    40 const TInt KSelectionCount = 2; // number of selections is fixed
       
    41 const TInt KFirstRadioButt = 1;
       
    42 const TInt KSecondRadioButt = 2;
       
    43 _LIT( KSelectedRadioIconIndex, "0" );
       
    44 _LIT( KDeselectedRadioIconIndex, "1" );
       
    45 _LIT( KLabelRowFormat, "\t%S\t\t" );
       
    46 _LIT( KRadioRowFormat, "%S\t   %S\t\t" );
       
    47 
       
    48 } /// namespace
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CPbk2MergeConflictsDlg::CPbk2MergeConflictsDlg
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CPbk2MergeConflictsDlg::CPbk2MergeConflictsDlg( 
       
    55         MPbk2MergeResolver* aConflictResolver, TBool aPhotoConflictExpected ):
       
    56         iConflictResolver( aConflictResolver ), iPhotoConflictExpected( aPhotoConflictExpected )
       
    57     {
       
    58     // No implementation required
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CPbk2MergeConflictsDlg::~CPbk2MergeConflictsDlg
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CPbk2MergeConflictsDlg::~CPbk2MergeConflictsDlg()
       
    66     {
       
    67     iRadioControls.Close();
       
    68     delete iNaviDecorator;
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CPbk2MergeConflictsDlg::NewL
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C CPbk2MergeConflictsDlg* CPbk2MergeConflictsDlg::NewL( 
       
    76         MPbk2MergeResolver* aConflictResolver, TBool aPhotoConflictExpected )
       
    77     {
       
    78     CPbk2MergeConflictsDlg* self = new (ELeave) 
       
    79         CPbk2MergeConflictsDlg( aConflictResolver, aPhotoConflictExpected );
       
    80     
       
    81     CleanupStack::PushL(self);
       
    82     self->ConstructL();
       
    83     CleanupStack::Pop(); // self;
       
    84     return self;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CPbk2MergeConflictsDlg::ConstructL
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CPbk2MergeConflictsDlg::ConstructL()
       
    92     {
       
    93     SetNaviPaneL();
       
    94     SetTitlePaneL( ETrue );
       
    95     CAknDialog::ConstructL( R_AVKON_MENUPANE_EMPTY );
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CPbk2MergeConflictsDlg::PreLayoutDynInitL
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CPbk2MergeConflictsDlg::PreLayoutDynInitL()
       
   103     {
       
   104     CAknDialog::PreLayoutDynInitL();
       
   105 
       
   106     CEikButtonGroupContainer& cba = ButtonGroupContainer();
       
   107     if ( iPhotoConflictExpected )
       
   108         {
       
   109         cba.SetCommandSetL( R_PBK2_SOFTKEYS_NEXT_CANCEL_SELECT );
       
   110         }
       
   111     else
       
   112         {
       
   113         cba.SetCommandSetL( R_PBK2_SOFTKEYS_DONE_CANCEL_SELECT );
       
   114         }
       
   115     
       
   116     CEikCba* eikCba = static_cast<CEikCba*>( cba.ButtonGroup() );
       
   117     if( eikCba )
       
   118         {
       
   119         eikCba->EnableItemSpecificSoftkey( EFalse );
       
   120         }
       
   121     
       
   122     iListBox = static_cast<CEikColumnListBox*>( Control( ESelectionListControl ) );
       
   123     
       
   124     SetIconsL();
       
   125     SetItemsL();
       
   126     
       
   127     iListBox->CreateScrollBarFrameL( ETrue );
       
   128     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   129             CEikScrollBarFrame::EOff,
       
   130             CEikScrollBarFrame::EAuto );
       
   131   
       
   132     iListBox->HandleItemAdditionL();
       
   133     iListBox->SetListBoxObserver( this ); // MEikListBoxObserver
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CPbk2MergeConflictsDlg::OkToExitL
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 TBool CPbk2MergeConflictsDlg::OkToExitL( TInt aButtonId )
       
   141     {
       
   142     if ( aButtonId == EAknSoftkeyDone || aButtonId == EAknSoftkeyCancel )
       
   143         {
       
   144         ResolveConflicts();
       
   145         TRAP_IGNORE( SetTitlePaneL( EFalse ) );
       
   146         return ETrue;
       
   147         }
       
   148     return EFalse;
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CPbk2MergeConflictsDlg::OfferKeyEventL
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 TKeyResponse CPbk2MergeConflictsDlg::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
       
   156     {
       
   157     TKeyResponse result = EKeyWasNotConsumed;
       
   158     
       
   159     if ( aKeyEvent.iCode == EKeyEscape )
       
   160        {
       
   161        result = CEikDialog::OfferKeyEventL( aKeyEvent, aType );
       
   162        return result;
       
   163        }
       
   164     
       
   165     result = iListBox->OfferKeyEventL( aKeyEvent, aType );
       
   166     
       
   167     if ( aKeyEvent.iCode == EKeyDownArrow )
       
   168         {
       
   169         TInt rowType = 0; 
       
   170         TInt currentSelection = iListBox->CurrentItemIndex();
       
   171         rowType = RowType( currentSelection );
       
   172         TInt itemsCount = iListBox->Model()->ItemTextArray()->MdcaCount();
       
   173         
       
   174         if ( rowType == ELabelRow )
       
   175           {
       
   176           iListBox->SetCurrentItemIndexAndDraw( currentSelection + 1 );
       
   177           }
       
   178         }
       
   179     else if ( aKeyEvent.iCode == EKeyUpArrow )
       
   180         {
       
   181         TInt rowType = 0; 
       
   182         TInt currentSelection = iListBox->CurrentItemIndex();
       
   183         rowType = RowType( currentSelection );
       
   184         TInt itemsCount = iListBox->Model()->ItemTextArray()->MdcaCount();
       
   185         
       
   186         if ( rowType == ELabelRow )
       
   187           {
       
   188           if ( currentSelection - 1 >= 0 )
       
   189               {
       
   190               iListBox->SetCurrentItemIndexAndDraw( currentSelection - 1 );
       
   191               }
       
   192           else
       
   193               {
       
   194               // select last one
       
   195               iListBox->SetCurrentItemIndexAndDraw( itemsCount - 1 );
       
   196               }
       
   197           }
       
   198         }
       
   199     else if ( aType == EEventKeyDown && aKeyEvent.iScanCode == EStdKeyDevice3 )
       
   200         {
       
   201         SwitchRadioButtonL();
       
   202         }
       
   203    
       
   204     return result;
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CPbk2MergeConflictsDlg::HandleListBoxEventL
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 void CPbk2MergeConflictsDlg::HandleListBoxEventL( CEikListBox* /*aListBox*/, TListBoxEvent aEventType )
       
   212     {     
       
   213     if( AknLayoutUtils::PenEnabled() )  
       
   214         {
       
   215         if ( aEventType == EEventItemSingleClicked || aEventType == EEventEnterKeyPressed)
       
   216             {
       
   217             SwitchRadioButtonL();
       
   218             }
       
   219         }
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CPbk2MergeConflictsDlg::SetIconsL
       
   224 // Sets icons for the listbox.
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CPbk2MergeConflictsDlg::SetIconsL()
       
   228     {
       
   229     CPbk2IconFactory* iconFactory = CPbk2IconFactory::NewLC();
       
   230     CArrayPtr<CGulIcon>* iconList = new (ELeave) CAknIconArray( 2 );
       
   231     CleanupStack::PushL( iconList );
       
   232 
       
   233     TPbk2IconId radioOnId = TPbk2IconId( TUid::Uid( KPbk2UID3 ), EPbk2qgn_prop_radiobutt_on );
       
   234     CGulIcon* radioOn = iconFactory->CreateIconL( radioOnId );
       
   235     CleanupStack::PushL( radioOn );
       
   236     iconList->AppendL( radioOn );
       
   237     CleanupStack::Pop( radioOn );
       
   238     
       
   239     TPbk2IconId radioOffId = TPbk2IconId( TUid::Uid( KPbk2UID3 ), EPbk2qgn_prop_radiobutt_off );
       
   240     CGulIcon* radioOff = iconFactory->CreateIconL( radioOffId );
       
   241     CleanupStack::PushL( radioOff );
       
   242     iconList->AppendL( radioOff );
       
   243     CleanupStack::Pop( radioOff );
       
   244     
       
   245     iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconList );
       
   246     CleanupStack::Pop( iconList );
       
   247     CleanupStack::PopAndDestroy( iconFactory );
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CPbk2MergeConflictsDlg::SetItemsL
       
   252 // Sets items to the listbox.
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CPbk2MergeConflictsDlg::SetItemsL()
       
   256     {    
       
   257     TInt conflictsCount = iConflictResolver->CountConflicts();
       
   258     CDesCArray* itemArray = new ( ELeave ) CDesCArrayFlat( 3 );
       
   259 
       
   260     for ( TInt i = 0; i < conflictsCount; i++ )
       
   261         {
       
   262         MPbk2MergeConflict& conflict = iConflictResolver->GetConflictAt( i );
       
   263         TInt type = conflict.GetConflictType();
       
   264         if ( type != EPbk2ConflictTypeImage )
       
   265             {
       
   266             HBufC* label = conflict.GetLabelLC();
       
   267             RBuf listItem;
       
   268             listItem.Create( KLabelRowFormat().Length() + label->Length() );
       
   269             listItem.Format( KLabelRowFormat, label );
       
   270             itemArray->AppendL( listItem );
       
   271             CleanupStack::PopAndDestroy( label );
       
   272             listItem.Close();
       
   273             
       
   274             HBufC* first = conflict.GetTextLC( EPbk2ConflictedFirst );
       
   275             listItem.Create( KRadioRowFormat().Length() + first->Length() );
       
   276             listItem.Format( KRadioRowFormat, &KSelectedRadioIconIndex, first );
       
   277             itemArray->AppendL( listItem );
       
   278             CleanupStack::PopAndDestroy( first );
       
   279             listItem.Close();
       
   280             
       
   281             HBufC* second = conflict.GetTextLC( EPbk2ConflictedSecond );
       
   282             listItem.Create( KRadioRowFormat().Length() + second->Length() );
       
   283             listItem.Format( KRadioRowFormat, &KDeselectedRadioIconIndex, second );
       
   284             itemArray->AppendL( listItem );
       
   285             CleanupStack::PopAndDestroy( second );
       
   286             listItem.Close();
       
   287             }
       
   288         
       
   289         TRadioControl radio;
       
   290         radio.iBeginIndex = IndexOfNewRadioControl();
       
   291         radio.iSelected = KFirstRadioButt;
       
   292         iRadioControls.Append( radio );
       
   293         
       
   294         }
       
   295 
       
   296     iListBox->Model()->SetItemTextArray( itemArray );
       
   297     iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray );
       
   298     
       
   299     iListBox->HandleItemAdditionL();
       
   300     
       
   301     TListItemProperties properties;
       
   302     properties.SetUnderlined( ETrue );
       
   303     TInt count = iRadioControls.Count();
       
   304     for ( TInt i= 0; i < count; i++ )
       
   305         {
       
   306         iListBox->ItemDrawer()->SetPropertiesL( iRadioControls[i].iBeginIndex , properties );
       
   307         }
       
   308     }
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // CPbk2MergeConflictsDlg::RowType
       
   312 // -----------------------------------------------------------------------------
       
   313 //
       
   314 TInt CPbk2MergeConflictsDlg::RowType( TInt aIndex )
       
   315     {
       
   316     TInt listboxItemsCount = iListBox->Model()->ItemTextArray()->MdcaCount();
       
   317     if ( aIndex >= listboxItemsCount || aIndex < 0 )
       
   318         {
       
   319         return KErrNotFound;
       
   320         }
       
   321     
       
   322     TInt count = iRadioControls.Count();
       
   323     for ( TInt i= 0; i < count; i++ )
       
   324         {
       
   325         if ( iRadioControls[i].iBeginIndex > aIndex )
       
   326             {
       
   327             break;
       
   328             }
       
   329         if ( iRadioControls[i].iBeginIndex == aIndex )
       
   330             {
       
   331             return ELabelRow;
       
   332             }
       
   333         }
       
   334     return ESelectionRow;
       
   335     }
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CPbk2MergeConflictsDlg::SwitchRadioButtonL
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 void CPbk2MergeConflictsDlg::SwitchRadioButtonL()
       
   342     {
       
   343     TInt currentSelection = iListBox->CurrentItemIndex();
       
   344     TInt rowType = 0; 
       
   345     rowType = RowType( currentSelection );
       
   346 
       
   347     if ( rowType == ESelectionRow )
       
   348         {
       
   349         // do nothing if selected
       
   350         if ( IsRadioSelected( currentSelection ) )
       
   351             {
       
   352             return;
       
   353             }
       
   354         }
       
   355     else
       
   356         {
       
   357         return;
       
   358         }
       
   359     
       
   360     TRadioControl radio = { 0, 0 };
       
   361     TInt index = 0;
       
   362     TInt count = iRadioControls.Count();
       
   363     for (  index = 0; index < count; index++ )
       
   364         {
       
   365         if ( iRadioControls[index].iBeginIndex < currentSelection && 
       
   366              iRadioControls[index].iBeginIndex + KSelectionCount >= currentSelection )
       
   367             {
       
   368             radio = iRadioControls[index];
       
   369             break;
       
   370             }
       
   371         }
       
   372     
       
   373     CDesCArray* itemArray = static_cast<CDesC16ArrayFlat*>( iListBox->Model()->ItemTextArray() );
       
   374     // deselect item
       
   375     TInt selectedItem = radio.iBeginIndex + radio.iSelected;
       
   376     TPtrC oldText = itemArray->MdcaPoint( selectedItem );
       
   377     RBuf newText;
       
   378     newText.CreateL( oldText );
       
   379     newText.Replace( 0, KDeselectedRadioIconIndex().Length(), KDeselectedRadioIconIndex() );
       
   380     itemArray->Delete( selectedItem );
       
   381     CleanupClosePushL( newText );
       
   382     itemArray->InsertL( selectedItem, newText );
       
   383     CleanupStack::PopAndDestroy( &newText );
       
   384     
       
   385     // select new item
       
   386     oldText.Set( itemArray->MdcaPoint( currentSelection ) );
       
   387     newText.CreateL( oldText );
       
   388     newText.Replace( 0, KSelectedRadioIconIndex().Length(), KSelectedRadioIconIndex() );
       
   389     itemArray->Delete( currentSelection );
       
   390     CleanupClosePushL( newText );
       
   391     itemArray->InsertL( currentSelection, newText );
       
   392     CleanupStack::PopAndDestroy( &newText );
       
   393     
       
   394     iRadioControls[index].iSelected = currentSelection - radio.iBeginIndex;
       
   395 
       
   396     iListBox->DrawDeferred();
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CPbk2MergeConflictsDlg::IsRadioSelected
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 TBool CPbk2MergeConflictsDlg::IsRadioSelected( TInt aIndex )
       
   404     {
       
   405     TInt count = iRadioControls.Count();
       
   406     for ( TInt i= 0; i < count; i++ )
       
   407         {
       
   408         if ( iRadioControls[i].iBeginIndex > aIndex )
       
   409             {
       
   410             break;
       
   411             }
       
   412         if ( iRadioControls[i].iBeginIndex + iRadioControls[i].iSelected == aIndex )
       
   413             {
       
   414             return ETrue;
       
   415             }
       
   416         }
       
   417     return EFalse; 
       
   418     }
       
   419 
       
   420 // -----------------------------------------------------------------------------
       
   421 // CPbk2MergeConflictsDlg::IndexOfNewRadioControl
       
   422 // -----------------------------------------------------------------------------
       
   423 //
       
   424 TInt CPbk2MergeConflictsDlg::IndexOfNewRadioControl()
       
   425     {
       
   426     TInt count = iRadioControls.Count();
       
   427     if ( count )
       
   428         {
       
   429         return iRadioControls[count-1].iBeginIndex + KSelectionCount + 1;
       
   430         }
       
   431     else
       
   432         {
       
   433         return 0;
       
   434         }
       
   435     }
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // CPbk2MergeConflictsDlg::ResolveConflicts
       
   439 // -----------------------------------------------------------------------------
       
   440 //
       
   441 void CPbk2MergeConflictsDlg::ResolveConflicts()
       
   442     {
       
   443     TInt syncIndex = 0;
       
   444     TInt count = iConflictResolver->CountConflicts();
       
   445     for ( TInt i= 0; i < count; i++ )
       
   446         {
       
   447         MPbk2MergeConflict& conflict = iConflictResolver->GetConflictAt( i );
       
   448         TInt type = conflict.GetConflictType();
       
   449         if ( type != EPbk2ConflictTypeImage )
       
   450             {
       
   451             if ( iRadioControls[i + syncIndex].iSelected == KFirstRadioButt )
       
   452                 {
       
   453                 conflict.ResolveConflict( EPbk2ConflictedFirst );
       
   454                 }
       
   455             else if ( iRadioControls[i + syncIndex].iSelected == KSecondRadioButt )
       
   456                 {
       
   457                 conflict.ResolveConflict( EPbk2ConflictedSecond );
       
   458                 }
       
   459             }
       
   460         else
       
   461             {
       
   462             syncIndex--;
       
   463             }
       
   464         }
       
   465     }
       
   466 
       
   467 // -----------------------------------------------------------------------------
       
   468 // CPbk2MergeConflictsDlg::SetNaviPaneL
       
   469 // -----------------------------------------------------------------------------
       
   470 //
       
   471 void CPbk2MergeConflictsDlg::SetNaviPaneL()
       
   472     {
       
   473     CEikStatusPane* statusPane = iAvkonAppUi->StatusPane();
       
   474     if ( statusPane && statusPane->PaneCapabilities( TUid::Uid( EEikStatusPaneUidNavi ) ).IsPresent() )
       
   475         {
       
   476         CAknNavigationControlContainer* naviPane = static_cast<CAknNavigationControlContainer*>
       
   477             ( statusPane->ControlL ( TUid::Uid( EEikStatusPaneUidNavi ) ) );
       
   478             
       
   479         HBufC* naviLabel = StringLoader::LoadLC( R_QTN_PHOB_TITLE_SELECT_VALUE );
       
   480 
       
   481         iNaviDecorator = naviPane->CreateNavigationLabelL( *naviLabel );
       
   482         CleanupStack::PopAndDestroy( naviLabel );   
       
   483         naviPane->PushL( *iNaviDecorator );
       
   484         }
       
   485     }
       
   486 
       
   487 // -----------------------------------------------------------------------------
       
   488 // CPbk2MergeConflictsDlg::SetTitlePaneL
       
   489 // -----------------------------------------------------------------------------
       
   490 //
       
   491 void CPbk2MergeConflictsDlg::SetTitlePaneL( TBool aCustom )
       
   492     {
       
   493     CEikStatusPane* statusPane = iAvkonAppUi->StatusPane();
       
   494     if ( statusPane && statusPane->PaneCapabilities( TUid::Uid( EEikStatusPaneUidTitle ) ).IsPresent() )
       
   495         {
       
   496         CAknTitlePane* titlePane = static_cast<CAknTitlePane*>
       
   497             ( statusPane->ControlL ( TUid::Uid( EEikStatusPaneUidTitle ) ) );
       
   498         
       
   499         if ( aCustom )
       
   500             {
       
   501             HBufC* title = StringLoader::LoadLC( R_QTN_PHOB_TITLE_CONFLICT_RESOLUTION );
       
   502             titlePane->SetTextL( *title );
       
   503             CleanupStack::PopAndDestroy( title );  
       
   504             }
       
   505         else
       
   506             {
       
   507             titlePane->SetTextToDefaultL();
       
   508             }
       
   509         }
       
   510     }