phonebookui/Phonebook2/UIControls/src/CPbk2DefaultSavingStorageSettingItem.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 default saving storage setting item.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CPbk2DefaultSavingStorageSettingItem.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include "CPbk2DefaultSavingStorageQueryListBoxModel.h"
       
    24 #include <MPbk2ApplicationServices.h>
       
    25 #include <MPbk2AppUi.h>
       
    26 #include <CPbk2StoreConfiguration.h>
       
    27 #include <CPbk2StorePropertyArray.h>
       
    28 #include <CPbk2StoreProperty.h>
       
    29 #include <Pbk2UIControls.rsg>
       
    30 #include <Pbk2StoreProperty.hrh>
       
    31 #include <Phonebook2InternalCRKeys.h>
       
    32 
       
    33 // Virtual Phonebook
       
    34 #include <MVPbkContactStoreList.h>
       
    35 #include <CVPbkContactStoreUriArray.h>
       
    36 #include <TVPbkContactStoreUriPtr.h>
       
    37 #include <CVPbkContactManager.h>
       
    38 #include <MVPbkContactStore.h>
       
    39 #include <MVPbkContactStoreProperties.h>
       
    40 
       
    41 // System includes
       
    42 #include <badesca.h>
       
    43 #include <aknsettingitemlist.h>
       
    44 #include <akncheckboxsettingpage.h>
       
    45 #include <aknselectionlist.h>
       
    46 #include <aknnotewrappers.h>
       
    47 #include <StringLoader.h>
       
    48 #include <aknlists.h>
       
    49 #include <aknPopup.h>
       
    50 #include <centralrepository.h>
       
    51 
       
    52 
       
    53 /// Unnamed namespace for local definitions
       
    54 namespace {
       
    55 
       
    56 const TInt KStoreUriArrayGranularity = 3;
       
    57 
       
    58 #ifdef _DEBUG
       
    59 
       
    60 enum TPanicCode
       
    61     {
       
    62     EPanic_UpdateL_OOB = 1,
       
    63     EPanic_StoreL_OOB,
       
    64     EPanic_CreatePopupItemListL_OOB
       
    65     };
       
    66 
       
    67 void Panic( TPanicCode aPanic )
       
    68     {
       
    69     _LIT( KPanicCat, "CPbk2DefaultSavingStorageSettingItem" );
       
    70     User::Panic( KPanicCat, aPanic );
       
    71     }
       
    72 
       
    73 #endif // _DEBUG
       
    74 
       
    75 } /// namespace
       
    76 
       
    77 // --------------------------------------------------------------------------
       
    78 // CPbk2DefaultSavingStorageSettingItem::CPbk2DefaultSavingStorageSettingItem
       
    79 // --------------------------------------------------------------------------
       
    80 //
       
    81 CPbk2DefaultSavingStorageSettingItem::CPbk2DefaultSavingStorageSettingItem
       
    82         ( TInt aResourceId,  TInt& aValue,
       
    83         CVPbkContactStoreUriArray& aSupportedStores,
       
    84         CSelectionItemList& aSelectionItemList ) :
       
    85             CAknEnumeratedTextPopupSettingItem( aResourceId, aValue ),
       
    86             iSupportedStores( aSupportedStores ),
       
    87             iSelectionItemList( aSelectionItemList )
       
    88     {
       
    89     }
       
    90 
       
    91 // --------------------------------------------------------------------------
       
    92 // CPbk2DefaultSavingStorageSettingItem::~CPbk2DefaultSavingStorageSettingItem
       
    93 // --------------------------------------------------------------------------
       
    94 //
       
    95 CPbk2DefaultSavingStorageSettingItem::~CPbk2DefaultSavingStorageSettingItem()
       
    96     {
       
    97     delete iRepository;
       
    98     if ( iStoreUriArray )
       
    99         {
       
   100         iStoreUriArray->ResetAndDestroy();
       
   101         }
       
   102     delete iStoreUriArray;
       
   103     }
       
   104 
       
   105 // --------------------------------------------------------------------------
       
   106 // CPbk2DefaultSavingStorageSettingItem::Value
       
   107 // --------------------------------------------------------------------------
       
   108 //
       
   109 TInt CPbk2DefaultSavingStorageSettingItem::Value()
       
   110     {
       
   111     return InternalValue();
       
   112     }
       
   113 
       
   114 // --------------------------------------------------------------------------
       
   115 // CPbk2DefaultSavingStorageSettingItem::UpdateL
       
   116 // --------------------------------------------------------------------------
       
   117 //
       
   118 TBool CPbk2DefaultSavingStorageSettingItem::UpdateL()
       
   119     {
       
   120     TInt value( Value() );
       
   121     __ASSERT_DEBUG( iStoreUriArray->Count() > value,
       
   122         Panic ( EPanic_UpdateL_OOB ) );
       
   123     // Save old item text
       
   124     HBufC* oldItem = iStoreUriArray->At( value )->AllocLC();
       
   125     // Create new item list
       
   126     CreatePopupItemListL();
       
   127     
       
   128     TBool result = EFalse;
       
   129 
       
   130     if ( CheckSelectedItems() )
       
   131         {
       
   132         // Check is the selected default saving storage in the selected list
       
   133         if ( IsDefaultSavingStorageL( *oldItem ) )
       
   134             {
       
   135             // Set new index
       
   136             SetSelectedIndex( *oldItem );            
       
   137             // Update list box and view
       
   138             UpdateListBoxTextL();
       
   139             result = ETrue;
       
   140             }
       
   141         else
       
   142             {            
       
   143             // Check store count, must be at least one
       
   144             TInt storeCount( iStoreUriArray->Count() );
       
   145             if ( storeCount == 1 )
       
   146                 {
       
   147                 SetSelectedIndex( 0 );
       
   148                 UpdateListBoxTextL();
       
   149                 ShowInformationNoteL();
       
   150                 result = ETrue;
       
   151                 }
       
   152             else
       
   153                 {
       
   154                 result = LaunchDefaultSavingMemoryQueryL();
       
   155                 }
       
   156             }
       
   157         }
       
   158     else
       
   159         {        
       
   160         // Memory selection list includes only read only items
       
   161         result = LaunchDefaultSavingMemoryQueryL();
       
   162         }
       
   163     
       
   164     CleanupStack::PopAndDestroy( oldItem );        
       
   165     return result;        
       
   166     }
       
   167 
       
   168 // --------------------------------------------------------------------------
       
   169 // CPbk2DefaultSavingStorageSettingItem::RestoreStateL
       
   170 // --------------------------------------------------------------------------
       
   171 //
       
   172 void CPbk2DefaultSavingStorageSettingItem::RestoreStateL()
       
   173     {
       
   174     CreatePopupItemListL();
       
   175     UpdateListBoxTextL();
       
   176     }
       
   177 
       
   178 // --------------------------------------------------------------------------
       
   179 // CPbk2DefaultSavingStorageSettingItem::CompleteConstructionL
       
   180 // --------------------------------------------------------------------------
       
   181 //
       
   182 void CPbk2DefaultSavingStorageSettingItem::CompleteConstructionL()
       
   183     {
       
   184     CAknEnumeratedTextPopupSettingItem::CompleteConstructionL();
       
   185 
       
   186     iRepository = CRepository::NewL(
       
   187         TUid::Uid( KCRUidPhonebookStoreConfiguration ) );
       
   188 
       
   189     iEnumeratedTextArray = EnumeratedTextArray();
       
   190     iPopupTextArray = PoppedUpTextArray();
       
   191 
       
   192     iStoreUriArray = new ( ELeave ) CArrayPtrFlat<HBufC>
       
   193         ( KStoreUriArrayGranularity );
       
   194 
       
   195     CreatePopupItemListL();
       
   196     }
       
   197 
       
   198 // --------------------------------------------------------------------------
       
   199 // CPbk2DefaultSavingStorageSettingItem::StoreL
       
   200 // --------------------------------------------------------------------------
       
   201 //
       
   202 void CPbk2DefaultSavingStorageSettingItem::StoreL()
       
   203     {
       
   204     TInt value( Value() );
       
   205     __ASSERT_DEBUG( iStoreUriArray->Count() > value,
       
   206         Panic ( EPanic_StoreL_OOB ) );
       
   207 
       
   208     HBufC* defaultSavingStore = iStoreUriArray->At( value );
       
   209     User::LeaveIfError( iRepository->Set( KPhonebookDefaultSavingStoreUri,
       
   210         *defaultSavingStore ) );
       
   211     }
       
   212 
       
   213 // --------------------------------------------------------------------------
       
   214 // CPbk2DefaultSavingStorageSettingItem::LoadL
       
   215 // --------------------------------------------------------------------------
       
   216 //
       
   217 void CPbk2DefaultSavingStorageSettingItem::LoadL()
       
   218     {
       
   219     TVPbkContactStoreUriPtr defaultSavingStore =
       
   220         Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   221             StoreConfiguration().DefaultSavingStoreL();
       
   222 
       
   223     const TInt count( iStoreUriArray->Count() );
       
   224     for ( TInt i( 0 ); i < count; ++i )
       
   225         {
       
   226         if ( iStoreUriArray->At( i )->Compare
       
   227                 ( defaultSavingStore.UriDes() ) == 0 )
       
   228             {
       
   229             SetSelectedIndex( i );
       
   230             }
       
   231         }
       
   232     }
       
   233 
       
   234 // --------------------------------------------------------------------------
       
   235 // CPbk2DefaultSavingStorageSettingItem::CreatePopupItemListL
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 void CPbk2DefaultSavingStorageSettingItem::CreatePopupItemListL()
       
   239     {
       
   240     iPopupTextArray->ResetAndDestroy();
       
   241     iStoreUriArray->ResetAndDestroy();
       
   242 
       
   243     const TInt count( iSelectionItemList.Count() );
       
   244     for ( TInt i( 0 ); i < count; ++i )
       
   245         {
       
   246         if ( iSelectionItemList.At( i )->SelectionStatus() )
       
   247             {
       
   248             __ASSERT_DEBUG( iSupportedStores.Count() > i,
       
   249                 Panic( EPanic_CreatePopupItemListL_OOB ) );
       
   250             const CPbk2StoreProperty* storeProperty =
       
   251                 Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   252                     StoreProperties().FindProperty
       
   253                         ( iSupportedStores[i] );
       
   254 
       
   255             if ( storeProperty->Flags() & KPbk2DefaultSavingStorageItem )
       
   256                 {
       
   257                 HBufC* item = iSelectionItemList.At(i)->ItemText().AllocLC();
       
   258                 iPopupTextArray->AppendL( item ); // take ownership
       
   259                 CleanupStack::Pop(); // item
       
   260 
       
   261                 // Store URI array
       
   262                 item = iSupportedStores[i].UriDes().AllocLC();
       
   263                 iStoreUriArray->AppendL( item );
       
   264                 CleanupStack::Pop( item );
       
   265                 }
       
   266             }
       
   267         }
       
   268 
       
   269     if ( iPopupTextArray->Count() == 0 )
       
   270         {
       
   271         // All the stores are read only, select all the writable stores
       
   272         for ( TInt i( 0 ); i < count; ++i )
       
   273             {
       
   274             // All the selected stores are read only
       
   275             if ( !iSelectionItemList.At( i )->SelectionStatus() )
       
   276                 {
       
   277                 __ASSERT_DEBUG( iSupportedStores.Count() > i,
       
   278                     Panic( EPanic_CreatePopupItemListL_OOB ) );
       
   279                 const CPbk2StoreProperty* storeProperty =
       
   280                     Phonebook2::Pbk2AppUi()->ApplicationServices().StoreProperties().
       
   281                         FindProperty( iSupportedStores[i] );
       
   282                 if ( storeProperty->Flags() & KPbk2DefaultSavingStorageItem )
       
   283                     {
       
   284                     HBufC* item = iSelectionItemList.At( i )->
       
   285                         ItemText().AllocLC();
       
   286                     iPopupTextArray->AppendL( item ); // takes ownership
       
   287                     CleanupStack::Pop(); // item
       
   288 
       
   289                     // Store URI array
       
   290                     item = iSupportedStores[i].UriDes().AllocLC();
       
   291                     iStoreUriArray->AppendL( item );
       
   292                     CleanupStack::Pop( item );
       
   293                     }
       
   294                 }
       
   295             }
       
   296         }
       
   297 
       
   298     CreateEnumeratedItemListL();
       
   299     }
       
   300 
       
   301 // --------------------------------------------------------------------------
       
   302 // CPbk2DefaultSavingStorageSettingItem::CreateEnumeratedItemListL
       
   303 // --------------------------------------------------------------------------
       
   304 //
       
   305 void CPbk2DefaultSavingStorageSettingItem::CreateEnumeratedItemListL()
       
   306     {
       
   307     iEnumeratedTextArray->ResetAndDestroy();
       
   308     const TInt count( iPopupTextArray->Count() );
       
   309     for ( TInt i( 0 ); i< count; ++i )
       
   310         {
       
   311         // Enumerated test array
       
   312         HBufC* buf = iPopupTextArray->At( i )->AllocLC();
       
   313         CAknEnumeratedText* textItem =
       
   314             new ( ELeave ) CAknEnumeratedText( i, buf );
       
   315         CleanupStack::Pop( buf );
       
   316         CleanupStack::PushL( textItem );
       
   317         iEnumeratedTextArray->AppendL( textItem ); // takes ownership
       
   318         CleanupStack::Pop( textItem );
       
   319         }
       
   320     }
       
   321 
       
   322 // --------------------------------------------------------------------------
       
   323 // CPbk2DefaultSavingStorageSettingItem::ShowInformationNoteL
       
   324 // --------------------------------------------------------------------------
       
   325 //
       
   326 void CPbk2DefaultSavingStorageSettingItem::ShowInformationNoteL()
       
   327     {
       
   328     CEikonEnv* env = CEikonEnv::Static();
       
   329     HBufC* noteText = StringLoader::LoadLC
       
   330         ( R_PHONEBOOK2_SETTINGS_INFORMATION_NOTE, env );
       
   331 
       
   332     CAknInformationNote* dialog =
       
   333         new ( ELeave ) CAknInformationNote( R_AKN_INFORMATION_NOTE_WAIT );
       
   334     dialog->ExecuteLD( *noteText );
       
   335 
       
   336     CleanupStack::PopAndDestroy( noteText ); // noteText
       
   337     }
       
   338 
       
   339 // --------------------------------------------------------------------------
       
   340 // CPbk2DefaultSavingStorageSettingItem::LaunchDefaultSavingMemoryQueryL
       
   341 // --------------------------------------------------------------------------
       
   342 //
       
   343 TBool CPbk2DefaultSavingStorageSettingItem::LaunchDefaultSavingMemoryQueryL()
       
   344     {
       
   345     // Create list box
       
   346     CAknSinglePopupMenuStyleListBox* listBox =
       
   347         new(ELeave) CAknSinglePopupMenuStyleListBox;
       
   348     CleanupStack::PushL( listBox );
       
   349 
       
   350     // Create popup list
       
   351     CAknPopupList* popupList = CAknPopupList::NewL( listBox,
       
   352         R_AVKON_SOFTKEYS_SELECT_CANCEL__SELECT,
       
   353         AknPopupLayouts::EMenuWindow );
       
   354     CleanupStack::PushL( popupList );
       
   355     HBufC* title = StringLoader::LoadLC
       
   356         ( R_PHONEBOOK2_SELECT_SAV_MEM_POPUP_TITLE );
       
   357     popupList->SetTitleL( *title );
       
   358     CleanupStack::PopAndDestroy( title );
       
   359 
       
   360     // Init list box
       
   361     listBox->ConstructL( popupList, EAknListBoxMenuList );
       
   362     listBox->CreateScrollBarFrameL( ETrue );
       
   363     listBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   364         CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
       
   365 
       
   366     // Create list box model
       
   367     MDesCArray* model = CPbk2DefaultSavingStorageQueryListBoxModel::NewLC
       
   368         ( *iPopupTextArray );
       
   369     // Listbox takes the ownership
       
   370     listBox->Model()->SetItemTextArray( model );
       
   371     listBox->Model()->SetOwnershipType( ELbmOwnsItemArray );
       
   372     CleanupStack::Pop( 2 ); // model, popupList
       
   373 
       
   374     TBool result = popupList->ExecuteLD();
       
   375 
       
   376     if ( result )
       
   377         {
       
   378         // Select is pressed
       
   379         SetSelectedIndex( listBox->CurrentItemIndex() );
       
   380         UpdateListBoxTextL();
       
   381         }
       
   382     CleanupStack::PopAndDestroy(); //listBox
       
   383     return result;
       
   384     }
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPbk2DefaultSavingStorageSettingItem::IsDefaultSavingStorageL
       
   388 // --------------------------------------------------------------------------
       
   389 //
       
   390 TBool CPbk2DefaultSavingStorageSettingItem::IsDefaultSavingStorageL
       
   391         ( HBufC& aStoreName )
       
   392     {
       
   393     TBool result( EFalse );
       
   394 
       
   395     const TInt count( iStoreUriArray->Count() );
       
   396     for ( TInt i( 0 ); i < count; ++i )
       
   397         {
       
   398         // This compare is enough, because aOldItem is taken from the
       
   399         // same list and that's why it does not matter if iStoreUriArray
       
   400         // includes UriNames or Uris
       
   401         HBufC* store = iStoreUriArray->At( i );
       
   402         if ( store->Compare( aStoreName ) == 0 )
       
   403             {
       
   404             result = ETrue;
       
   405             break;
       
   406             }
       
   407         }
       
   408 
       
   409     return result;
       
   410     }
       
   411 
       
   412 // --------------------------------------------------------------------------
       
   413 // CPbk2DefaultSavingStorageSettingItem::SetSelectedIndex
       
   414 // --------------------------------------------------------------------------
       
   415 //
       
   416 void CPbk2DefaultSavingStorageSettingItem::SetSelectedIndex
       
   417         ( TInt aNewIndex )
       
   418     {
       
   419     SetInternalValue( aNewIndex );
       
   420     CAknEnumeratedTextPopupSettingItem::SetSelectedIndex( aNewIndex );
       
   421     }
       
   422 
       
   423 // --------------------------------------------------------------------------
       
   424 // CPbk2DefaultSavingStorageSettingItem::SetSelectedIndex
       
   425 // --------------------------------------------------------------------------
       
   426 //
       
   427 void CPbk2DefaultSavingStorageSettingItem::SetSelectedIndex
       
   428         ( const TDesC& aStoreUri )
       
   429     {
       
   430     const TInt count( iStoreUriArray->Count() );
       
   431     for ( TInt i( 0 ); i < count; ++i )
       
   432         {
       
   433         if ( iStoreUriArray->At( i )->Compare( aStoreUri ) == 0 )
       
   434             {
       
   435             SetSelectedIndex( i );
       
   436             break;
       
   437             }
       
   438         }
       
   439     }
       
   440 
       
   441 // --------------------------------------------------------------------------
       
   442 // CPbk2DefaultSavingStorageSettingItem::CheckSelectedItems
       
   443 // --------------------------------------------------------------------------
       
   444 //
       
   445 TBool CPbk2DefaultSavingStorageSettingItem::CheckSelectedItems()
       
   446     {
       
   447     TBool result( EFalse );
       
   448 
       
   449     const TInt count( iSelectionItemList.Count() );
       
   450     for ( TInt i( 0 ); i < count; ++i )
       
   451         {
       
   452         if ( iSelectionItemList.At( i )->SelectionStatus() )
       
   453             {
       
   454             const CPbk2StoreProperty* storeProperty =
       
   455                 Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   456                     StoreProperties().FindProperty
       
   457                         ( iSupportedStores[i] );
       
   458 
       
   459             if ( storeProperty->Flags() & KPbk2DefaultSavingStorageItem )
       
   460                 {
       
   461                 result = ETrue;
       
   462                 break;
       
   463                 }
       
   464             }
       
   465         }
       
   466 
       
   467     return result;
       
   468     }
       
   469 
       
   470 // End of File