Nokia N97 SDK
Example Applications Guide

ccontcontainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : ccontcontainer.cpp
00004 *  Part of     : Contacts database example
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2004 - 2006 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include <aknquerydialog.h>
00018 #include <aknnotewrappers.h>
00019 #include <cntdb.h>
00020 #include <cntitem.h>
00021 #include <cntfldst.h>
00022 #include <contacts.rsg>
00023 
00024 #include <aknlists.h>
00025 #include <avkon.hrh>
00026 #include <CPbkContactEngine.h>
00027 #include <CPbkViewState.h>
00028 #include <CPbkContactEditorDlg.h>
00029 #include <CPbkContactItem.h>
00030 #include <f32file.h>
00031 #include <rpbkviewresourcefile.h>
00032 #include <stringloader.h>
00033 
00034 #include "CContContainer.h"
00035 #include "contacts.hrh"
00036 
00037 
00038 // ================= MEMBER FUNCTIONS =======================
00039 //
00040 // ---------------------------------------------------------
00041 // CContContainer::CContContainer()
00042 // constructor
00043 // ---------------------------------------------------------
00044 //
00045 CContContainer::CContContainer()
00046     {
00047     iContactDb = NULL;
00048     iSelectedItemId = -1;
00049     }
00050 
00051 // ---------------------------------------------------------
00052 // CContContainer::ConstructL()
00053 // Two phased constructor
00054 // ---------------------------------------------------------
00055 //
00056 void CContContainer::ConstructL( const TRect& aRect )
00057     {
00058     CreateWindowL();
00059 
00060     iContactArray = new (ELeave) CDesCArrayFlat( KArraySize );
00061     iContactGroupArray = new (ELeave) CDesCArrayFlat( KArraySize );
00062 
00063     // Create Listbox
00064     iListBox = new (ELeave) CAknSingleStyleListBox;
00065 
00066     if ( iListBox )
00067         {
00068         iListBox->ConstructL( NULL, EAknListBoxSelectionList );
00069         iListBox->Model()->SetItemTextArray( iContactArray );
00070         iListBox->CreateScrollBarFrameL();
00071         iListBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
00072         iListBox->SetListBoxObserver( this );
00073         iListBox->HandleItemAdditionL();
00074         }
00075     SizeChanged();
00076 
00077     SetRect( aRect );
00078 
00079     ActivateL();
00080     }
00081 
00082 // ---------------------------------------------------------
00083 // CContContainer::~CContContainer ()
00084 // Destructor
00085 // ---------------------------------------------------------
00086 //
00087 CContContainer::~CContContainer()
00088     {
00089     if ( iListBox )
00090         {
00091         delete iListBox;
00092         }
00093 
00094     if ( iContactDb )
00095         {
00096         iContactDb->CloseTables();
00097         delete iContactDb;
00098         }
00099 
00100     if ( iContactArray )
00101         {
00102         delete iContactArray;
00103         }
00104 
00105     if ( iContactGroupArray )
00106         {
00107         delete iContactGroupArray;
00108         }
00109     }
00110 
00111 // ---------------------------------------------------------
00112 // CContContainer::SizeChanged()
00113 // Called by framework when the view size is changed.
00114 // ---------------------------------------------------------
00115 //
00116 void CContContainer::SizeChanged()
00117     {
00118     // We want the list box to begin from top of the main pane
00119     // and fill at least its minimum size.
00120         TPoint coordListBox;
00121         AknLayoutUtils::LayoutMetricsPosition(AknLayoutUtils::EMainPane, coordListBox);
00122         iListBox->SetExtent( coordListBox, iListBox->MinimumSize() );
00123     }
00124 
00125 // ---------------------------------------------------------
00126 // CContContainer::CountComponentControls()
00127 // Return the number of controls inside this container. We
00128 // have only the list box.
00129 // ---------------------------------------------------------
00130 //
00131 TInt CContContainer::CountComponentControls() const
00132     {
00133     return 1; // return nbr of controls inside this container
00134     }
00135 
00136 // ---------------------------------------------------------
00137 // CContContainer::ComponentControl()
00138 // ---------------------------------------------------------
00139 //
00140 CCoeControl* CContContainer::ComponentControl(TInt aIndex) const
00141     {
00142     switch ( aIndex )
00143         {
00144         case 0:
00145             if ( iListBox )
00146                 {
00147                 return iListBox;
00148                 }
00149 
00150         default:
00151             return NULL;
00152         }
00153     }
00154 
00155 // ---------------------------------------------------------
00156 // CContContainer::Draw()
00157 // ---------------------------------------------------------
00158 //
00159 void CContContainer::Draw( const TRect& aRect ) const
00160     {
00161     CWindowGc& gc = SystemGc();
00162     gc.SetPenStyle( CGraphicsContext::ENullPen );
00163     gc.SetBrushColor( KRgbWhite );
00164     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00165     gc.DrawRect( aRect );
00166     }
00167 
00168 // ----------------------------------------------------------------------------
00169 // CContContainer::OfferKeyEventL()
00170 // Handles the key events.
00171 // ----------------------------------------------------------------------------
00172 //
00173 TKeyResponse CContContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent,
00174                                               TEventCode aType )
00175         {
00176     if ( aType != EEventKey )
00177         {
00178         return EKeyWasNotConsumed;
00179         }
00180 
00181         if ( iListBox )
00182         {
00183         return iListBox->OfferKeyEventL( aKeyEvent, aType );
00184         }
00185 
00186         return EKeyWasNotConsumed;
00187 
00188     }
00189 
00190 // ---------------------------------------------------------------------------
00191 // CContContainer::HandleListBoxEventL()
00192 // Handles listboxevents.
00193 // ---------------------------------------------------------------------------
00194 //
00195 void CContContainer::HandleListBoxEventL( CEikListBox* /*aListBox */,
00196     TListBoxEvent aEventType )
00197     {
00198     switch( aEventType )
00199         {
00200         case EEventEnterKeyPressed: // Flow through
00201         case EEventItemClicked:
00202             EditContactDlgL();
00203             break;
00204         default:
00205             break;
00206         }
00207     }
00208 
00209 // ---------------------------------------------------------
00210 // CContContainer::HandleControlEventL()
00211 // ---------------------------------------------------------
00212 //
00213 void CContContainer::HandleControlEventL(
00214     CCoeControl* /*Control*/,TCoeEvent /*aEventType*/)
00215     {
00216     }
00217 
00218 // ---------------------------------------------------------
00219 // CContContainer::UpdateContactDbName()
00220 // Store current database path and name.
00221 // ---------------------------------------------------------
00222 //
00223 void CContContainer::UpdateContactDbName( TDesC& aDbName )
00224     {
00225     iDbName = aDbName;
00226     }
00227 
00228 // ----------------------------------------------------
00229 // CContContainer::UpdateListBoxL()
00230 // Updates listbox.
00231 // ----------------------------------------------------
00232 //
00233 void CContContainer::UpdateListBoxL()
00234     {
00235     iListBox->HandleItemAdditionL();
00236     iListBox->SetCurrentItemIndexAndDraw( 0 );
00237     SizeChanged();
00238     DrawNow();
00239     }
00240 
00241 // ----------------------------------------------------
00242 // CContContainer::IsContactsOnListBox()
00243 // The function returns state of the listbox.
00244 // If contacts on the listbox has been selected
00245 // the function returns ETrue.
00246 // ----------------------------------------------------
00247 //
00248 TBool CContContainer::IsContactsOnListBox()
00249     {
00250     CDesCArray* itemArray = (CDesCArray*) iListBox->Model()->ItemTextArray();
00251 
00252     if ( iContactArray == itemArray )
00253         {
00254             return ETrue;
00255             }
00256 
00257     return EFalse;
00258     }
00259 // ----------------------------------------------------
00260 // CContContainer::ShowContactsL()
00261 // Show contacts in the listbox.
00262 // ----------------------------------------------------
00263 //
00264 void CContContainer::ShowContactsL()
00265     {
00266 
00267     if ( !IfContactDbSelected() )
00268         {
00269         ShowInformationNoteNoDbL();
00270         return;
00271         }
00272 
00273     if ( IsContactsOnListBox() )
00274         {
00275         return;
00276         }
00277 
00278     iListBox->Model()->SetItemTextArray( iContactArray );
00279     iListBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
00280     UpdateListBoxL();
00281     }
00282 
00283 // ----------------------------------------------------
00284 // CContContainer::ShowContactGroupsL()
00285 // Show contact groups in the listbox.
00286 // ----------------------------------------------------
00287 //
00288 void CContContainer::ShowContactGroupsL()
00289         {
00290 
00291     if ( !IfContactDbSelected() )
00292         {
00293         ShowInformationNoteNoDbL();
00294         return;
00295         }
00296 
00297     if ( !IsContactsOnListBox() )
00298             {
00299             return;
00300             }
00301 
00302     iListBox->Model()->SetItemTextArray( iContactGroupArray );
00303     iListBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
00304     UpdateListBoxL();
00305     }
00306 
00307 
00308 
00309 // ----------------------------------------------------
00310 // CContContainer::ShowInformationNoteNoDbL()
00311 // Show information not if contact database
00312 // did not opened.
00313 // ----------------------------------------------------
00314 //
00315 void CContContainer::ShowInformationNoteNoDbL()
00316     {
00317     HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_DB_SELECTED );
00318     CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
00319     informationNote->ExecuteLD( textResource->Des() );
00320     CleanupStack::PopAndDestroy( textResource );
00321 
00322     }
00323 // ----------------------------------------------------
00324 // CContContainer::IfContactDbSelected()
00325 // The function checks is contact database
00326 // has been opened.
00327 // ----------------------------------------------------
00328 //
00329 TBool CContContainer::IfContactDbSelected()
00330     {
00331 
00332     if ( iContactDb )
00333             {
00334             return ETrue;
00335             }
00336 
00337     return EFalse;
00338     }
00339 
00340 // ----------------------------------------------------
00341 // CContContainer::CanAddContactToGroupL()
00342 // The function returns ETrue is contact database
00343 // has been opened and contains at least one contact item.
00344 // ----------------------------------------------------
00345 //
00346 TBool CContContainer::CanAddContactToGroupL()
00347     {
00348     if ( !IfContactDbSelected() )
00349             {
00350         ShowInformationNoteNoDbL();
00351             return EFalse;
00352             }
00353 
00354     if ( !iContactArray->Count() )
00355         {
00356         HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_CONTACT_SELECTED );
00357         CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
00358         informationNote->ExecuteLD( textResource->Des() );
00359         CleanupStack::PopAndDestroy( textResource );
00360         return EFalse;
00361         }
00362 
00363     return ETrue;
00364     }
00365 
00366 // ----------------------------------------------------------------------------
00367 // CContContainer::OpenDefaultDatabaseL()
00368 // Open default contact database.
00369 // Database is same as what Phonebook uses.
00370 // ----------------------------------------------------------------------------
00371 //
00372 void CContContainer::OpenDefaultDatabaseL()
00373     {
00374     // Read name of the default database
00375     _LIT( KOrgContactFile,"" );
00376     TBuf<KMaxDatabasePathAndNameLength> orgContactFile( KOrgContactFile );
00377     CContactDatabase::GetDefaultNameL( orgContactFile );
00378     orgContactFile.LowerCase();
00379 
00380     if ( iContactDb )
00381         {
00382         if ( !iDbName.Compare( orgContactFile ) )
00383             {
00384             return;
00385             }
00386         iContactDb->CloseTables();
00387         delete iContactDb;
00388         iContactDb = NULL;
00389         }
00390 
00391     TRAPD(err, iContactDb = CContactDatabase::OpenL( orgContactFile ););
00392     // Check if database already exist
00393     if ( err == KErrNotFound )
00394         {
00395 
00396         HBufC* text = StringLoader::LoadLC( R_CONTACTS_DB_NOT_FOUND );
00397         CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
00398         informationNote->ExecuteLD( *text );
00399         CleanupStack::PopAndDestroy( text );
00400         iContactDb = NULL;
00401         ReadContactsFromDbL();// Update iContactArray and iContactGroupArray
00402 
00403         TBuf<15> noContactFile( KNullDesC );
00404         UpdateContactDbName( noContactFile );
00405 
00406         UpdateListBoxL();
00407         }
00408     else
00409         {
00410         // Update iContactArray and iContactGroupArray
00411         ReadContactsFromDbL();
00412         UpdateContactDbName( orgContactFile );
00413         UpdateListBoxL();
00414         }
00415     }
00416 
00417 // ----------------------------------------------------------------------------
00418 // CContContainer::ShowDeleteConfirmationQueryL()
00419 // The display of Delete Confirmation.
00420 // ----------------------------------------------------------------------------
00421 //
00422 TBool CContContainer::ShowDeleteConfirmationQueryL( const TInt aTextResourceId )
00423     {
00424     HBufC* prompt = StringLoader::LoadLC( aTextResourceId );
00425 
00426     CAknQueryDialog* dlg = CAknQueryDialog::NewL();
00427 
00428     dlg->SetPromptL( *prompt );
00429     TInt retCode( dlg->ExecuteLD( R_CONTACTS_CONFIRMATION_QUERY ) );
00430 
00431     CleanupStack::PopAndDestroy(); //prompt
00432     return retCode;
00433     }
00434 
00435 // ----------------------------------------------------------------------------
00436 // CContContainer::OpenTextBoxQueryL()
00437 // Display of Data Query.
00438 // ----------------------------------------------------------------------------
00439 //
00440 TBool CContContainer::OpenTextBoxQueryL(const TInt aTextResourceId,
00441                                                TPtr aTextData,
00442                                                                                            TInt aTextLength)
00443     {
00444     HBufC* prompt = StringLoader::LoadLC( aTextResourceId );
00445     // aTextData is smaller than 40 so this does not crash
00446     TBuf<KMaxTextBoxQueryLength> text = aTextData;
00447 
00448     CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog( text,
00449                                                 CAknQueryDialog::ENoTone );
00450 
00451     CleanupStack::PushL( dlg );
00452     dlg->SetPromptL( *prompt );
00453     dlg->SetMaxLength( aTextLength );
00454 
00455     TBool answer( dlg->ExecuteLD( R_CONTACTS_TEXTBOX_QUERY ) );
00456     CleanupStack::Pop( dlg );
00457     CleanupStack::PopAndDestroy( prompt );
00458     aTextData = text;
00459     return answer;
00460     }
00461 
00462 // ----------------------------------------------------
00463 // CContContainer::EditContactDlgL()
00464 // Opens CPbkContactEditorDlg for contact.
00465 // ----------------------------------------------------
00466 //
00467 void CContContainer::EditContactDlgL()
00468     {
00469     if ( !IfContactDbSelected() )
00470         {
00471         ShowInformationNoteNoDbL();
00472         return;
00473         }
00474 
00475     // Check if contacts list on the list box
00476     if ( !IsContactsOnListBox() )
00477         {
00478         return;
00479         }
00480 
00481     // The caller does not take ownership of this object.
00482     // so do not push it onto the CleanupStack.
00483     const CContactIdArray* contacts = iContactDb->SortedItemsL();
00484 
00485     TInt currentItemIndex( iListBox->CurrentItemIndex() );
00486     TInt count( iContactArray->Count() );
00487     if ( currentItemIndex < 0 || currentItemIndex >= count )
00488         {
00489         HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_CONTACT_SELECTED );
00490         CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
00491         informationNote->ExecuteLD( textResource->Des() );
00492         CleanupStack::PopAndDestroy( textResource );
00493             return;
00494         }
00495 
00496     TContactItemId itemId( ((*contacts)[currentItemIndex]) );
00497 
00498     CPbkContactEngine* pbkContactEng = CPbkContactEngine::NewL( &iCoeEnv->FsSession());
00499     CleanupStack::PushL( pbkContactEng );
00500     // load the contacts dialog resources
00501     CCoeEnv *env = CEikonEnv::Static();
00502     RPbkViewResourceFile pbkRes( *env );
00503     pbkRes.OpenL();
00504 
00505     CPbkContactItem* aContactItem = pbkContactEng->OpenContactL( itemId );
00506     CleanupStack::PushL( aContactItem );
00507 
00508     // Open the contacts dialog
00509     // third parameter: set to true if aContactItem is a
00510     // new contact (not yet in the database)
00511     // fourth parameter index of field to focus initially
00512     // fifth paramter if true sets the contact initially as "edited".
00513     CPbkContactEditorDlg* pbkContactDlg =
00514                     CPbkContactEditorDlg::NewL( *pbkContactEng,
00515                                                 *aContactItem,
00516                                                 EFalse, -1,
00517                                                 EFalse );
00518 
00519     CleanupStack::PushL( pbkContactDlg );
00520     pbkContactDlg->SetMopParent( iAvkonAppUi );
00521 
00522     TInt res( KErrNone );
00523     TRAPD( err, res = pbkContactDlg->ExecuteLD() );
00524 
00525     pbkRes.Close();
00526     pbkContactEng->CloseContactL( aContactItem->Id() );
00527 
00528     CleanupStack::Pop( pbkContactDlg );
00529     CleanupStack::PopAndDestroy( 2 ); //aContactItem, pbkContactEng
00530 
00531     // Update iContactArray and iContactGroupArray
00532     ReadContactsFromDbL();
00533     UpdateListBoxL();
00534     }
00535 
00536 // ----------------------------------------------------
00537 // CContContainer::GetSelectedContactL()
00538 // Set selected contact's item id to
00539 // variable and changes listbox data to contact groups.
00540 // ----------------------------------------------------
00541 //
00542 void CContContainer::GetSelectedContactL()
00543     {
00544     if ( !IfContactDbSelected() )
00545         {
00546         ShowInformationNoteNoDbL();
00547         return;
00548         }
00549 
00550     if ( !IsContactsOnListBox() )
00551         {
00552             return;
00553         }
00554 
00555     iContactDb->SetDbViewContactType( KUidContactCard );
00556     // The caller does not take ownership of this object.
00557     // so do not push it onto the CleanupStack
00558     const CContactIdArray* contacts = iContactDb->SortedItemsL();
00559 
00560     TInt currentItemIndex( iListBox->CurrentItemIndex() );
00561     TInt count( iContactArray->Count() );
00562     if ( currentItemIndex < 0 || currentItemIndex >= count )
00563         {
00564             return;
00565         }
00566 
00567     iSelectedItemId = ((*contacts)[currentItemIndex]);
00568 
00569     ShowContactGroupsL();
00570     }
00571 
00572 // ----------------------------------------------------
00573 // CContContainer::AddContactToGrp()
00574 // Add selected contact to contact group.
00575 // ----------------------------------------------------
00576 
00577 void CContContainer::AddContactToGrpL()
00578     {
00579     if ( !IfContactDbSelected() )
00580         {
00581         ShowInformationNoteNoDbL();
00582         return;
00583         }
00584 
00585     if ( iSelectedItemId == -1 )
00586             {
00587             return;
00588             }
00589 
00590     TInt currentItemIndex( iListBox->CurrentItemIndex() );
00591     TInt count( iContactGroupArray->Count() );
00592     if ( currentItemIndex < 0 || currentItemIndex >= count )
00593         {
00594         return;
00595         }
00596 
00597     // The caller takes ownership of the returned object.
00598     // groupIdList is NULL if there are no groups in the database.
00599     CContactIdArray* groupIdList = iContactDb->GetGroupIdListL();
00600     if ( !groupIdList )
00601         {
00602             return;
00603         }
00604 
00605     CleanupStack::PushL( groupIdList );
00606 
00607     TContactItemId groupItemId = ((*groupIdList)[currentItemIndex]);
00608     iContactDb->AddContactToGroupL( iSelectedItemId , groupItemId );
00609 
00610     iSelectedItemId = -1;
00611 
00612     CleanupStack::PopAndDestroy( groupIdList );
00613     }
00614 // ----------------------------------------------------
00615 // CContContainer::AddNewContactDlg()
00616 // Open CPbkContactEditorDlg dialog
00617 // for new contact item.
00618 // ----------------------------------------------------
00619 //
00620 void CContContainer::AddNewContactDlgL()
00621     {
00622 
00623     if ( !IfContactDbSelected() )
00624         {
00625         ShowInformationNoteNoDbL();
00626         return;
00627         }
00628 
00629     if ( !IsContactsOnListBox() )
00630         {
00631         return;
00632         }
00633 
00634         CPbkContactEngine* pbkContactEng = CPbkContactEngine::NewL( &iCoeEnv->FsSession());
00635     // Put object to the stack
00636     CleanupStack::PushL( pbkContactEng );
00637 
00638     // load the contacts dialog resources
00639     CCoeEnv *env = CEikonEnv::Static();
00640     RPbkViewResourceFile pbkRes( *env );
00641     pbkRes.OpenL();
00642 
00643     //create a new empty contact
00644     // The caller takes ownership of this object.
00645     // so push it onto the CleanupStack
00646     CPbkContactItem* aContactItem = pbkContactEng->CreateEmptyContactL();
00647     CleanupStack::PushL( aContactItem );
00648 
00649     // launch the contact dialog
00650     CPbkContactEditorDlg* pbkContactDlg =
00651                 CPbkContactEditorDlg::NewL( *pbkContactEng,
00652                                             *aContactItem,
00653                                             ETrue, -1,
00654                                             ETrue );
00655 
00656     CleanupStack::PushL( pbkContactDlg );
00657 
00658     pbkContactDlg->SetMopParent( iAvkonAppUi );
00659 
00660     TInt res( KErrNone );
00661 
00662     // Show contact dialog
00663     TRAPD( err, res = pbkContactDlg->ExecuteLD() );
00664 
00665     pbkRes.Close();
00666 
00667     CleanupStack::Pop( pbkContactDlg );
00668     CleanupStack::PopAndDestroy( 2 ); //aContactItem, pbkContactEng
00669 
00670     // Update internal database connection
00671     iContactDb->CloseTables();
00672     delete iContactDb;
00673     iContactDb = CContactDatabase::OpenL( iDbName );
00674 
00675     // Update iContactArray and iContactGroupArray
00676     ReadContactsFromDbL();
00677     // Update display
00678     UpdateListBoxL();
00679     }
00680 
00681 // ----------------------------------------------------
00682 // CContContainer::DeleteContactDlg()
00683 // Delete contact from contact database.
00684 // ----------------------------------------------------
00685 //
00686 void CContContainer::DeleteContactDlgL()
00687     {
00688 
00689     if ( !IfContactDbSelected() )
00690         {
00691         ShowInformationNoteNoDbL();
00692         return;
00693         }
00694 
00695     if ( !IsContactsOnListBox() )
00696         {
00697         return;
00698         }
00699 
00700     // The caller does not take ownership of this object.
00701     // so do not push it onto the CleanupStack
00702 
00703     TInt currentItemIndex( iListBox->CurrentItemIndex() );
00704     TInt count( iContactArray->Count() );
00705     if (currentItemIndex < 0 || currentItemIndex >= count )
00706         {
00707         HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_CONTACT_SELECTED );
00708         CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
00709         informationNote->ExecuteLD( textResource->Des() );
00710         CleanupStack::PopAndDestroy( textResource );
00711         return;
00712         }
00713 
00714     // Ask about deletion
00715     if ( !ShowDeleteConfirmationQueryL( R_CONTACTS_DELETE_CONTACT_MESSAGE ) )
00716         {
00717         return;
00718         }
00719 
00720     // Sets the type of contact items to be included in
00721     // sorted views of the database
00722     iContactDb->SetDbViewContactType( KUidContactCard );
00723 
00724     const CContactIdArray* contacts = iContactDb->SortedItemsL();
00725     TContactItemId itemId( ( (*contacts)[currentItemIndex] ) );
00726                                         
00727         CPbkContactEngine* pbkContactEng = CPbkContactEngine::NewL( &iCoeEnv->FsSession());                                              
00728     CleanupStack::PushL( pbkContactEng );
00729     pbkContactEng->DeleteContactL( itemId );
00730     CleanupStack::PopAndDestroy( pbkContactEng );
00731 
00732     iContactDb->CloseTables();
00733     delete iContactDb;
00734     iContactDb = NULL;
00735     iContactDb = CContactDatabase::OpenL( iDbName );
00736 
00737     // Update iContactArray and iContactGroupArray
00738     ReadContactsFromDbL();
00739     UpdateListBoxL();
00740     }
00741 
00742 
00743 // ----------------------------------------------------
00744 // CContContainer::CreateContactTemplateL()
00745 // Creates new contact template to contact database.
00746 // ----------------------------------------------------
00747 //
00748 void CContContainer::CreateContactTemplateL()
00749     {
00750 
00751     if ( !IfContactDbSelected() )
00752         {
00753         ShowInformationNoteNoDbL();
00754         return;
00755         }
00756 
00757     HBufC* templateName = HBufC::NewLC( KMaxTemplateNameLength );
00758     TPtr ptr( templateName->Des() );
00759     if( !OpenTextBoxQueryL( R_CONTACTS_CREATE_TEMPLATE_MESSAGE,
00760                                 ptr,
00761                                 KMaxTextLength ) )
00762             {
00763             CleanupStack::PopAndDestroy( 1 ); // templateName
00764             return;
00765             }
00766 
00767     // The caller takes ownership of this object.
00768     // so push it onto the CleanupStack
00769     CContactItem* contactTemplate =
00770                  iContactDb->CreateContactCardTemplateL( templateName->Des() );
00771     CleanupStack::PushL( contactTemplate );
00772     CContactItemField* givenNameField =
00773                    CContactItemField::NewLC( KStorageTypeText,
00774                                             KUidContactFieldGivenName );
00775     CContactItemField* familyNameField =
00776                    CContactItemField::NewLC(KStorageTypeText,
00777                                             KUidContactFieldFamilyName );
00778     CContactItemField* phoneNumberField =
00779                    CContactItemField::NewLC(KStorageTypeText,
00780                                             KUidContactFieldPhoneNumber );
00781 
00782     // Add data to fields
00783     givenNameField->TextStorage()->SetTextL( KFirstName );
00784     givenNameField->SetMapping( KUidContactFieldVCardMapUnusedN );
00785     familyNameField->TextStorage()->SetTextL( KLastName );
00786     familyNameField->SetMapping( KUidContactFieldVCardMapUnusedN );
00787     phoneNumberField->TextStorage()->SetTextL( KPhoneNumber );
00788     phoneNumberField->SetMapping( KUidContactFieldVCardMapTEL );
00789 
00790     // Add fields to a contact template
00791     contactTemplate->AddFieldL( *givenNameField );
00792     contactTemplate->AddFieldL( *familyNameField );
00793     contactTemplate->AddFieldL( *phoneNumberField );
00794 
00795     // Item takes the ownership of fields
00796     CleanupStack::Pop( 3 ); // givenNameField, familyNameField, phoneNumberField
00797     CleanupStack::PopAndDestroy( 2 );// templateName, contactTemplate
00798 
00799     // Update iContactArray and iContactGroupArray
00800     ReadContactsFromDbL();
00801     UpdateListBoxL();
00802         }
00803 
00804 // ----------------------------------------------------
00805 // CContContainer::CreateContactGroupL()
00806 // Create new contact group.
00807 // ----------------------------------------------------
00808 //
00809 void CContContainer::CreateContactGroupL()
00810         {
00811 
00812     if ( !IfContactDbSelected() )
00813         {
00814         ShowInformationNoteNoDbL();
00815         return;
00816         }
00817 
00818     if ( IsContactsOnListBox() )
00819         {
00820         return;
00821         }
00822            
00823         CPbkContactEngine* pbkContactEng = CPbkContactEngine::NewL( &iCoeEnv->FsSession());                                            
00824     CleanupStack::PushL( pbkContactEng );
00825 
00826     // Open dialog
00827     HBufC* groupName = HBufC::NewLC( KMaxGroupNameLength );
00828     TPtr ptr( groupName->Des() );
00829     if ( OpenTextBoxQueryL( R_CONTACTS_CREATE_GROUP_MESSAGE,
00830                             ptr,
00831                             KMaxTextLength ) )
00832             {
00833             //Create contactGroup
00834             CContactGroup* contactGroup =
00835                     pbkContactEng->CreateContactGroupL( groupName->Des() );
00836         delete contactGroup;
00837             }
00838 
00839     CleanupStack::PopAndDestroy( 2 ); // pbkContactEng, groupName
00840 
00841     iContactDb->CloseTables();
00842     delete iContactDb;
00843     iContactDb = NULL;
00844     iContactDb = CContactDatabase::OpenL( iDbName );
00845 
00846     // Update iContactArray and iContactGroupArray
00847     ReadContactsFromDbL();
00848     UpdateListBoxL();
00849     }
00850 
00851 // ----------------------------------------------------
00852 // CContContainer::DeleteContactGroupL()
00853 // Delete contact group from contact database.
00854 // ----------------------------------------------------
00855 //
00856 void CContContainer::DeleteContactGroupL()
00857         {
00858 
00859     if ( !IfContactDbSelected() )
00860         {
00861         ShowInformationNoteNoDbL();
00862         return;
00863         }
00864 
00865     if ( IsContactsOnListBox() )
00866         {
00867         return;
00868         }
00869 
00870     TInt currentItemIndex( iListBox->CurrentItemIndex() );
00871     TInt count( iContactGroupArray->Count() );
00872     if ( currentItemIndex < 0 || currentItemIndex >= count )
00873         {
00874         return;
00875         }
00876 
00877     // The caller takes ownership of the returned object.
00878     // groupIdList is NULL if there are no groups in the database.
00879     CContactIdArray* groupIdList = iContactDb->GetGroupIdListL();
00880     if ( !groupIdList )
00881         {
00882         return;
00883         }
00884 
00885     CleanupStack::PushL( groupIdList );
00886 
00887         CPbkContactEngine* pbkContactEng = CPbkContactEngine::NewL( &iCoeEnv->FsSession());                                             
00888     CleanupStack::PushL(pbkContactEng);
00889 
00890     TContactItemId itemId = ((*groupIdList)[currentItemIndex]);
00891 
00892     // Ask about deletion
00893     if (ShowDeleteConfirmationQueryL( R_CONTACTS_DELETE_GROUP_MESSAGE ) )
00894         {
00895         pbkContactEng->DeleteContactGroupL( itemId );
00896         }
00897 
00898     CleanupStack::PopAndDestroy( 2 ); //pbkContactEng , groupIdList
00899 
00900     iContactDb->CloseTables();
00901     delete iContactDb;
00902     iContactDb = NULL;
00903     iContactDb = CContactDatabase::OpenL( iDbName );
00904 
00905     // Update iContactArray and iContactGroupArray
00906     ReadContactsFromDbL();
00907     UpdateListBoxL();
00908         }
00909 
00910 // ---------------------------------------------------------
00911 // CContContainer::ReadContactsFromDbL()
00912 // Read contacts from contact database
00913 // and sort contacts by Family Name and
00914 // Given Name. Add sorted items to
00915 // iContactArray.
00916 // ---------------------------------------------------------
00917 //
00918 void CContContainer::ReadContactsFromDbL()
00919     {
00920     ReadContactGroupsFromDbL();
00921 
00922     // Delete old contact from listbox
00923     TInt count( iContactArray->Count() );
00924     if ( count )
00925         {
00926         // Clear array. Start to index 0
00927             iContactArray->Delete( 0, count );
00928         }
00929 
00930     if ( !IfContactDbSelected() )
00931         {
00932             return;
00933         }
00934 
00935     iContactDb->SetDbViewContactType( KUidContactCard );
00936 
00937     TFieldType aFieldType1( KUidContactFieldFamilyName );
00938     TFieldType aFieldType2( KUidContactFieldGivenName );
00939     CContactDatabase::TSortPref sortPref1( aFieldType1 );
00940     CContactDatabase::TSortPref sortPref2( aFieldType2 );
00941 
00942     // Sort contacts by Family and Given Name
00943     CArrayFixFlat<CContactDatabase::TSortPref>* aSortOrder =
00944                     new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(2);
00945 
00946     CleanupStack::PushL( aSortOrder );
00947     aSortOrder->AppendL( sortPref1 );
00948     aSortOrder->AppendL( sortPref2 );
00949 
00950     // The database takes ownership of the sort order array passed in
00951     iContactDb->SortL( aSortOrder );
00952 
00953     // The caller does not take ownership of this object.
00954     // so do not push it onto the CleanupStack
00955     const CContactIdArray* contacts = iContactDb->SortedItemsL();
00956 
00957     // Go through each contact item and
00958     // make items for listbox
00959     const TInt nc( contacts->Count() );
00960 
00961     for ( TInt i( nc-1 ); i >= 0; i-- ) //For each ContactId
00962         {
00963         CContactItem* contact = NULL;
00964         // The caller takes ownership of the returned object.
00965         // So push it onto the CleanupStack
00966         contact = iContactDb->OpenContactL( (*contacts)[i] );
00967         CleanupStack::PushL( contact );
00968 
00969         CContactItemFieldSet& fieldSet = contact->CardFields();
00970 
00971         HBufC* firstNameBuf = NULL;
00972         HBufC* lastNameBuf = NULL;
00973 
00974         // Get first name
00975         TInt findpos( fieldSet.Find( KUidContactFieldGivenName ) );
00976 
00977         // Check that the first name field is actually there.
00978         if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
00979             {
00980             CContactItemField& firstNameField = fieldSet[findpos];
00981             CContactTextField* firstName = firstNameField.TextStorage();
00982             firstNameBuf = firstName->Text().AllocLC();
00983             }
00984 
00985             // Get last name
00986         findpos = fieldSet.Find( KUidContactFieldFamilyName );
00987 
00988         // Check that the last name field is actually there.
00989         if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
00990             {
00991             CContactItemField& lastNameField = fieldSet[ findpos ];
00992             CContactTextField* lastName = lastNameField.TextStorage();
00993             lastNameBuf = lastName->Text().AllocLC();
00994             }
00995 
00996         TInt len( 0 );
00997 
00998         if ( firstNameBuf )
00999             {
01000             len+=firstNameBuf->Length();
01001             }
01002 
01003         if ( lastNameBuf )
01004             {
01005             len+=lastNameBuf->Length();
01006             }
01007 
01008             // Create new buffer and add space for formatters
01009             HBufC* combinedDes = HBufC::NewLC( len + KFormattersSpace );
01010         if ( ( firstNameBuf ) && ( lastNameBuf ) )
01011             {
01012             combinedDes->Des().Format( KListItemFormatter,
01013                                        lastNameBuf,
01014                                        firstNameBuf );
01015             }
01016         else
01017             {
01018             if ( firstNameBuf )
01019                 {
01020                 combinedDes->Des().Format( KListOItemFormatter, firstNameBuf );
01021                 }
01022             else if ( lastNameBuf )
01023                 {
01024                 combinedDes->Des().Format( KListOItemFormatter, lastNameBuf );
01025                 }
01026             }
01027 
01028         // Insert a contacts name into the array at the specified position.
01029         // If the specified position is the same as the current number of
01030         // descriptor elements in the array, this has the effect of
01031         // appending the element.
01032         iContactArray->InsertL( 0, *combinedDes );
01033 
01034         CleanupStack::PopAndDestroy( combinedDes );
01035 
01036         if ( lastNameBuf )
01037             {
01038             CleanupStack::PopAndDestroy( lastNameBuf );
01039             }
01040 
01041         if ( firstNameBuf )
01042             {
01043             CleanupStack::PopAndDestroy( firstNameBuf );
01044             }
01045 
01046         iContactDb->CloseContactL( contact->Id() );
01047         CleanupStack::PopAndDestroy( contact );
01048 
01049         }
01050 
01051     CleanupStack::Pop( aSortOrder );
01052     }
01053 
01054 // ---------------------------------------------------------
01055 // CContContainer::ReadContactGroupsFromDbL()
01056 // Read contact groups from contact database.
01057 // Do not sort contact groups. Add group items
01058 // to iContactGroupArray.
01059 // ---------------------------------------------------------
01060 //
01061 void CContContainer::ReadContactGroupsFromDbL()
01062     {
01063     TInt count( iContactGroupArray->Count() );
01064     if ( count )
01065         {
01066         // Clear array. Start to index 0
01067         iContactGroupArray->Delete( 0, count );
01068         }
01069     if ( !IfContactDbSelected() )
01070         {
01071         return;
01072         }
01073 
01074         //The caller takes ownership of the returned object.
01075     // groupIdList is NULL if there are no groups in the database.
01076     CContactIdArray* groupIdList = iContactDb->GetGroupIdListL();
01077     if( !groupIdList )
01078         {
01079         return;
01080         }
01081 
01082     CleanupStack::PushL( groupIdList );
01083 
01084     CContactGroup* contactGroup = NULL;
01085     const TInt nc( groupIdList->Count() );
01086     for ( TInt i( nc-1 ); i >= 0; i-- ) //For each Contact Group Id
01087         {
01088         CContactItem* contact=NULL;
01089 
01090         // The caller takes ownership of the returned object.
01091         // So push it onto the CleanupStack
01092         contact = iContactDb->OpenContactL( ((*groupIdList)[i]) );
01093         CleanupStack::PushL( contact );
01094 
01095         contactGroup = (CContactGroup*) contact;
01096         TInt len( contactGroup->GetGroupLabelL().Length() );
01097         HBufC* groupLabelBuf = HBufC::NewLC( len + KFormattersSpace );
01098         HBufC* combinedDes = HBufC::NewLC( len + KFormattersSpace );
01099 
01100         *groupLabelBuf = contactGroup->GetGroupLabelL();
01101         combinedDes->Des().Format( KListOItemFormatter, groupLabelBuf );
01102 
01103         // Insert a group name into the array at the specified position.
01104         // If the specified position is the same as the current number of
01105         // descriptor elements in the array, this has the effect of
01106         // appending the element.
01107         iContactGroupArray->InsertL( 0, *combinedDes );
01108 
01109             CleanupStack::PopAndDestroy( combinedDes );
01110             CleanupStack::PopAndDestroy( groupLabelBuf );
01111 
01112             iContactDb->CloseContactL( contact->Id() );
01113             CleanupStack::PopAndDestroy( contact );
01114             }
01115 
01116     CleanupStack::PopAndDestroy( groupIdList );
01117     }
01118 
01119 // End of File

© Nokia 2009

Back to top