S60 3rd Edition SDK for Symbian OS
Example Applications Guide

AddressBookView.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : AddressBookView.cpp
00004 *  Part of     : AddressBook
00005 *  Interface   : 
00006 *  Description : Application View class, CAddressBookView - 
00007 *                                shows the application state on the screen
00008 *  Version     : 
00009 *
00010 *  Copyright (c) 2002-2006 Nokia Corporation.
00011 *  This material, including documentation and any related 
00012 *  computer programs, is protected by copyright controlled by 
00013 *  Nokia Corporation.
00014 * ==============================================================================
00015 */
00016 
00017 // INCLUDES
00018 #include "AddressBookView.h"
00019 #include "AddressBookEngine.h"
00020 #include "AddressBookContact.h"
00021 
00022 #include <eiklabel.h>  // for example label control
00023 #include <barsread.h>
00024 #include <akntitle.h>
00025 #include <eikbtgpc.h>  // CEikButtonGroupContainer
00026 #include <gulicon.h>   // CGulIcon
00027 #include <fbs.h>           // CFbsBitmap
00028 
00029 #include <SenXmlUtils.h>
00030 
00031 #include <AddressBook.rsg>
00032 
00033 
00034 // ================= MEMBER FUNCTIONS =======================
00035 
00036 // ---------------------------------------------------------------------------- 
00037 // CAddressBookView::CAddressBookView(CAddressBookEngine& aEngine)
00038 // constructor
00039 // ---------------------------------------------------------------------------- 
00040 //
00041 CAddressBookView::CAddressBookView(CAddressBookEngine& aEngine)
00042 :
00043 iEngine(aEngine),
00044 iDetailedView(EFalse),
00045 iSearchList(NULL),
00046 iDetailList(NULL)
00047     {
00048     }
00049 
00050 // ----------------------------------------------------------------------------  
00051 // CAddressBookView::~CAddressBookView()
00052 // destructor
00053 // ---------------------------------------------------------------------------- 
00054 //
00055 CAddressBookView::~CAddressBookView()
00056     {
00057     delete iSearchList;
00058         if (iItemList)
00059                 iItemList->Reset();
00060         
00061         delete iItemList;
00062         delete iDetailList;
00063         }
00064     
00065 // ---------------------------------------------------------------------------- 
00066 // CAddressBookView::ConstructL(const TRect& aRect)
00067 // EPOC two phased constructor
00068 // ---------------------------------------------------------------------------- 
00069 //
00070 void CAddressBookView::ConstructL(const TRect& aRect)
00071     {
00072     CreateWindowL();
00073     
00074     iFont = AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont);
00075     
00076         iSearchList = new (ELeave) CAknSingleStyleListBox();
00077         iSearchList->ConstructL(this);
00078         iSearchList->SetContainerWindowL(*this);
00079         iSearchList->CreateScrollBarFrameL(ETrue);
00080         iSearchList->ScrollBarFrame()->SetScrollBarVisibilityL(
00081                                                                                                         CEikScrollBarFrame::EOn,
00082                                                                                                         CEikScrollBarFrame::EAuto);
00083 
00084         iItemList = new (ELeave) CDesCArrayFlat(3);
00085         iSearchList->Model()->SetItemTextArray(iItemList);
00086         iSearchList->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
00087         
00088         iDetailList = new (ELeave) CAknFormDoubleStyleListBox;
00089         iDetailList->ConstructL(this);
00090         iDetailList->SetContainerWindowL(*this);
00091         iDetailList->CreateScrollBarFrameL(ETrue);
00092         iDetailList->ScrollBarFrame()->SetScrollBarVisibilityL(
00093                                                                                                 CEikScrollBarFrame::EOn,
00094                                                                                                 CEikScrollBarFrame::EAuto);
00095         iDetailList->Model()->SetOwnershipType(ELbmOwnsItemArray);
00096         iDetailList->MakeVisible(EFalse);
00097         iSearchList->MakeVisible(ETrue); // show "no results" at startup
00098         iSearchList->SetListBoxObserver(this);
00099     SetRect(aRect);
00100     ActivateL();
00101         MakeVisible(ETrue);
00102         }
00103 
00104 // ---------------------------------------------------------------------------- 
00105 // CAddressBookView::SizeChanged()
00106 // Is called by framework when the view size is changed
00107 // ---------------------------------------------------------------------------- 
00108 //
00109 void CAddressBookView::SizeChanged()
00110     {
00111         if (iSearchList)
00112                 {
00113                 iSearchList->SetExtent( TPoint(0,0), iSearchList->MinimumSize() );
00114                 }
00115         if (iDetailList)
00116                 {
00117                 iDetailList->SetExtent( TPoint(0,0), iDetailList->MinimumSize() );
00118                 }
00119     }
00120     
00121 // ---------------------------------------------------------------------------- 
00122 // CAddressBookView::UpdateL(RPointerArray<CAddressBookContact>& aContactArray)  
00123 // Updates bufer containing the contact information when search was finished 
00124 // ---------------------------------------------------------------------------- 
00125 //
00126 void CAddressBookView::UpdateL(RPointerArray<CAddressBookContact>& aContactArray)
00127         {
00128         iItemList->Reset();
00129         iSearchList->MakeVisible(ETrue);
00130         TInt count = aContactArray.Count();
00131         if(count > 0)
00132                 {
00133                 for( TInt i = 0; i < count; ++i )
00134                         {
00135                         CAddressBookContact* item = aContactArray[i];
00136                         
00137                         HBufC8* pRow = HBufC8::NewLC(128);
00138                         TPtr8 ptrRow = pRow->Des();
00139                         if (item->MobilePhoneL() != KNullDesC8)
00140                                 {
00141                                 ptrRow.Append(KHasPhoneChar);   
00142                                 }
00143                         ptrRow.Append(KTab);
00144                         ptrRow.Append(item->LastName());
00145                         ptrRow.Append(KSpace);
00146                         ptrRow.Append(item->FirstName());
00147                         ptrRow.Append(KTab);
00148                         ptrRow.Append(KTab);
00149 
00150                         HBufC* pRowAsUnicode = SenXmlUtils::ToUnicodeLC(ptrRow);
00151                         if(pRowAsUnicode)
00152                                 {
00153                                 iItemList->AppendL(*pRowAsUnicode);
00154                                 }
00155                         CleanupStack::PopAndDestroy(pRowAsUnicode); 
00156                         CleanupStack::PopAndDestroy(pRow); 
00157                         }
00158                 
00159                 iSearchList->HandleItemAdditionL();
00160                 iSearchList->SetFocus(ETrue, EDrawNow);
00161                 iSearchList->SetCurrentItemIndexAndDraw(0);
00162                 }
00163         else
00164                 {
00165                 iSearchList->HandleItemRemovalL();
00166                 }
00167         }
00168 
00169 // ---------------------------------------------------------------------------- 
00170 // CAddressBookView::CountComponentControls() const
00171 // it is called by the active scheduler
00172 // ---------------------------------------------------------------------------- 
00173 //
00174 TInt CAddressBookView::CountComponentControls() const
00175     {
00176     return 1; 
00177     }
00178 
00179 // ---------------------------------------------------------------------------- 
00180 // CAddressBookView::ComponentControl(TInt aIndex) const
00181 // it is called by the active scheduler
00182 // ---------------------------------------------------------------------------- 
00183 //
00184 CCoeControl* CAddressBookView::ComponentControl(TInt aIndex) const
00185     {
00186     switch ( aIndex )
00187         {
00188         case 0:
00189                         if (iDetailedView)
00190                                 return iDetailList;
00191                         else
00192                                 {
00193                                 return iSearchList;
00194                                 }
00195         default:
00196             return NULL;
00197         }
00198     }
00199     
00200 // ---------------------------------------------------------------------------- 
00201 // CAddressBookView::Draw(const TRect& aRect) const
00202 // ---------------------------------------------------------------------------- 
00203 //
00204 void CAddressBookView::Draw(const TRect& aRect) const
00205     {
00206     CWindowGc& gc = SystemGc();
00207     
00208     gc.SetPenStyle( CGraphicsContext::ENullPen );
00209     gc.SetBrushColor( KRgbWhite );
00210     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00211     gc.DrawRect( aRect );
00212 
00213         gc.UseFont(iFont);
00214         TInt lineHeight = iFont->HeightInPixels()+1;
00215         
00216         const TInt col = 5;
00217         gc.DrawText(KAboutString1, TPoint(col, lineHeight));
00218         gc.DrawText(KAboutString2, TPoint(col, lineHeight*2));
00219         gc.DrawText(KAboutString3, TPoint(col, lineHeight*3));  
00220         gc.DrawText(KAboutString4, TPoint(col, lineHeight*4));
00221         gc.DrawText(KAboutString5, TPoint(col, lineHeight*5));
00222     }
00223 
00224 // ---------------------------------------------------------------------------- 
00225 // CAddressBookView::HandleControlEventL(CCoeControl*,TCoeEvent)
00226 // ---------------------------------------------------------------------------- 
00227 //
00228 void CAddressBookView::HandleControlEventL(CCoeControl*, TCoeEvent)
00229     {
00230     }
00231 
00232 // ---------------------------------------------------------------------------- 
00233 // CaddressBookView::CloseItem()
00234 // It is called when Contact information was browsed and needed to be closed
00235 // ---------------------------------------------------------------------------- 
00236 //    
00237 void CAddressBookView::CloseItem()
00238         {
00239         iDetailedView = EFalse;
00240         iSearchList->MakeVisible(ETrue);
00241         iDetailList->MakeVisible(EFalse);
00242         DrawDeferred();
00243         }
00244 // ---------------------------------------------------------------------------- 
00245 // CaddressBookView::OpenItemL()
00246 // to browse Contact information
00247 // ---------------------------------------------------------------------------- 
00248 //
00249 void CAddressBookView::OpenItemL()
00250         {
00251         if (iDetailedView)
00252                 return;
00253 
00254         CAddressBookContact* item = NULL; 
00255         TInt success = iEngine.GetContact(iSearchList->CurrentItemIndex(), item);
00256         if (success == KErrNotFound)
00257                 return;
00258         iSearchList->MakeVisible(EFalse);
00259         CDesCArray* array = new (ELeave) CDesCArrayFlat(3);
00260 
00261         AppendRowL(item->LastName(), KLastname, *array);
00262         AppendRowL(item->FirstName(), KFirstname,*array);
00263         AppendRowL(item->MiddleName(), KMiddlename, *array);
00264         AppendRowL(item->ExtraAddress(), KExtAddr, *array);
00265         AppendRowL(item->Street(), KStreet, *array);
00266         AppendRowL(item->Locality(), KLocality, *array);
00267         AppendRowL(item->Region(), KRegion, *array);
00268         AppendRowL(item->PostCode(), KPCode, *array);
00269         AppendRowL(item->Country(), KCountry, *array);
00270         AppendRowL(item->PhoneL(), KPhone, *array);
00271         AppendRowL(item->MobilePhoneL(), KMobile, *array);
00272         AppendRowL(item->FaxL(), KFax, *array);
00273         AppendRowL(item->Email(), KEmail, *array);
00274         AppendRowL(item->Title(), KTitle, *array);
00275         AppendRowL(item->Organization(), KOrganization, *array);
00276         AppendRowL(item->Unit(), KUnit, *array);
00277         
00278         iDetailedView = ETrue;
00279         iDetailList->Model()->SetItemTextArray(array);
00280         iDetailList->MakeVisible(ETrue);
00281         iDetailList->SetCurrentItemIndexAndDraw(0);
00282         SizeChanged();
00283         iDetailList->ActivateL();
00284         }
00285 
00286 // ---------------------------------------------------------------------------- 
00287 // CAddressBookView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType )
00288 // the key event handler function.
00289 // It is called every time when user presses some button.
00290 // ---------------------------------------------------------------------------- 
00291 //
00292 TKeyResponse CAddressBookView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00293                                                                                                                 TEventCode aType )
00294         {
00295         return ComponentControl(0)->OfferKeyEventL(aKeyEvent,aType);
00296         }
00297         
00298 // ---------------------------------------------------------------------------- 
00299 // CAddressBookView::HandleListBoxEventL(CEikListBox*, TListBoxEvent aEventType)
00300 // It is called when user want to browse the card (full contact information)
00301 // about employees who was found
00302 // ---------------------------------------------------------------------------- 
00303 //      
00304 void CAddressBookView::HandleListBoxEventL(CEikListBox*, TListBoxEvent aEventType)
00305     {
00306         if (aEventType == EEventEnterKeyPressed)
00307                 {
00308                 OpenItemL();
00309                 ChangeCbaL(R_AVKON_SOFTKEYS_OPTIONS_BACK, EDrawNow);
00310                 }
00311     }
00312     
00313 // ---------------------------------------------------------------------------- 
00314 // CAddressBookView::ChangeCbaL(TInt aResourceId, TDrawNow aDrawNow)
00315 // It is called when need to open or close some item 
00316 // to change common button area
00317 // ---------------------------------------------------------------------------- 
00318 //
00319 void CAddressBookView::ChangeCbaL(TInt aResourceId, TDrawNow aDrawNow)
00320     {
00321     CEikButtonGroupContainer* Cba = CEikButtonGroupContainer::Current();
00322     if(Cba)
00323         {
00324         Cba->SetCommandSetL(aResourceId);
00325         
00326         if(aDrawNow)
00327                 Cba->DrawNow();
00328         else
00329                 Cba->DrawDeferred();    
00330                 }
00331     }
00332 
00333 // ---------------------------------------------------------------------------- 
00334 // CAddressBookView::ShowAbout(TBool aAboutVisible)
00335 // Shows information about Address Book aplication.
00336 // ---------------------------------------------------------------------------- 
00337 //
00338 void CAddressBookView::ShowAbout(TBool aAboutVisible)
00339         {
00340         iSearchList->MakeVisible(!aAboutVisible);
00341         iDetailList->MakeVisible(EFalse);
00342         DrawDeferred();
00343         }
00344 
00345 // ---------------------------------------------------------------------------- 
00346 // CAddressBookView::CurrentContact()
00347 // Is needed to add current contact into PnoneBook database
00348 // ---------------------------------------------------------------------------- 
00349 //
00350 CAddressBookContact* CAddressBookView::CurrentContact()
00351         {
00352         CAddressBookContact* pContact = NULL;
00353         iEngine.GetContact(iSearchList->CurrentItemIndex(), 
00354                                                 pContact // pContact remains NULL if not found
00355                                                 ); 
00356         return pContact;
00357         }
00358 
00359 // ---------------------------------------------------------------------------- 
00360 // CAddressBookView::AppendRowL(TPtrC8 aValue, const TDesC8& aFieldName, 
00361 //                                                                      CDesCArray& aArray)
00362 // Creates a card (full contact information) about a person who was found
00363 // ---------------------------------------------------------------------------- 
00364 //
00365 void CAddressBookView::AppendRowL(TPtrC8 aValue, const TDesC8& aFieldName, 
00366                                                                         CDesCArray& aArray)
00367         {
00368         if (aValue != KNullDesC8)
00369                 {
00370                 TBuf8<64> row;
00371                 HBufC* pRowAsUnicode = NULL;
00372                 row.Format(KRowFormat, &aFieldName, &aValue);
00373                 pRowAsUnicode = SenXmlUtils::ToUnicodeLC(row);
00374                 if(pRowAsUnicode)
00375                         {
00376                         aArray.AppendL(*pRowAsUnicode);
00377                         CleanupStack::PopAndDestroy(pRowAsUnicode); 
00378                         }
00379                 }
00380         }
00381 
00382 // End of File  
00383 

© Nokia 2006

Back to top