examples/AppEngine/ContactsModel/contactviews/ContactViews.cpp

00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Find and filtered views
00015 // This example demonstrates CContactFindView and CContactFilteredView
00016 // This code creates a contact database, "exampleViews.cdb", adds an item
00017 // to it containing four fields, then uses a find view and filtered view
00018 // to search/filter the database. The contents of all matching items are
00019 // printed to the console.
00020 //
00021 
00022 
00023 #include <e32base.h>
00024 #include <e32cons.h>
00025 #include <cntdb.h>
00026 #include <cntitem.h>
00027 #include <cntfield.h>
00028 #include <cntfldst.h>
00029 // User include
00030 #include "ContactViews.h"
00031 
00032 // stores the total number of views created in the example
00033 const TInt KTotalViews = 3;
00034 
00035 static CConsoleBase* console;
00036 // name of contact database to be created
00037 _LIT(KContactsFilename,"exampleViews.cdb");
00038 // pointer to the database
00039 static CContactDatabase* db;
00040 
00041 CExampleViews* CExampleViews::NewL(CContactDatabase& aDb)
00042         {
00043         CExampleViews* self=new(ELeave) CExampleViews(aDb);
00044         CleanupStack::PushL(self);
00045         self->ConstructL();
00046         CleanupStack::Pop();
00047         return self;
00048         }
00049 
00050 CExampleViews::~CExampleViews()
00051         {
00052         if (iFilterView!=NULL)
00053                 {
00054                 iFilterView->Close(*this);
00055                 }
00056         if (iFindView!=NULL)
00057                 {
00058                 iFindView->Close(*this);
00059                 }
00060         if (iLocalView!=NULL)
00061                 {
00062                 iLocalView->Close(*this);
00063                 }
00064         iSortOrder.Close();
00065         }
00066 
00067 
00068 CExampleViews::CExampleViews(CContactDatabase& aDb)
00069 :       CActive(EPriorityStandard),     iDb(aDb) {}
00070 
00071 void CExampleViews::ConstructL()
00072         {
00073         // iSortOrder defines the fields that are used in the view
00074         iSortOrder.AppendL(KUidContactFieldGivenName);
00075         iSortOrder.AppendL(KUidContactFieldFamilyName);
00076         iSortOrder.AppendL(KUidContactFieldPhoneNumber);
00077         iSortOrder.AppendL(KUidContactFieldEMail);
00078         CActiveScheduler::Add(this);
00079         }
00080 
00081 // Handles the active object’s request completion event.
00082 void CExampleViews::RunL()
00083         {
00084         iLocalView=CContactLocalView::NewL(*this,iDb,iSortOrder,EContactsOnly);
00085         // creates the search string for the find view
00086         CPtrCArray* desArray =new (ELeave) CPtrCArray(1);
00087         _LIT(KSearchString,"Smith");
00088     TPtrC searchString(KSearchString);
00089         desArray->AppendL(searchString);
00090         // Creates a find view based on the local view
00091         iFindView = CContactFindView::NewL(iDb, *iLocalView,*this, desArray);
00092         desArray->Reset();
00093     delete desArray;
00094     // Creates a filtered view based on the local view
00095     // Filters out items without a telephone number
00096         iFilterView = CContactFilteredView::NewL(*this, iDb, *iLocalView, CContactDatabase::EPhonable);
00097         }
00098 
00099 // invoked by the active object mechanism
00100 void CExampleViews::HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent)
00101         {
00102         if (aEvent.iEventType==TContactViewEvent::EReady)
00103                 {
00104                 TRAPD(err,HandleViewEventL(aView));
00105                 if(err)
00106                         {
00107                         _LIT(KFormatFailed,"failed: leave code=%d");
00108                         console->Printf(KFormatFailed, err);
00109                         }
00110                 }
00111         }
00112 
00113 void CExampleViews::HandleViewEventL(const CContactViewBase& aView)
00114         {
00115         iNumViewsCreated++;
00116         if (&aView == iFindView)
00117         // find view is ready to be used
00118                 {
00119                 _LIT(KConsoleMessage,"Contents of find view:\n");
00120                 console->Printf(KConsoleMessage);
00121                 for (TInt count = 0; count < iFindView->CountL(); count++)
00122                         {
00123                         // prints out contents of all fields for all items in the find view
00124                         _LIT(KFieldSeparator,",");
00125                         HBufC* text = iFindView->AllFieldsLC(count,KFieldSeparator);
00126                         _LIT(KConsoleMessage2,"%S\n");
00127                         console->Printf(KConsoleMessage2,text);
00128                         CleanupStack::PopAndDestroy(); // text
00129                         }
00130                 }
00131         else if (&aView == iFilterView)
00132         // filter view is ready
00133                 {
00134                 _LIT(KConsoleMessage,"Contents of filter view:\n");
00135                 console->Printf(KConsoleMessage);
00136                 for (TInt count = 0; count < iFilterView->CountL(); count++)
00137                         {
00138                         // prints out contents of all fields for all items in the filter view
00139                         _LIT(KFieldSeparator,",");
00140                         HBufC* text = iFilterView->AllFieldsLC(count,KFieldSeparator);
00141                         _LIT(KConsoleMessage2,"%S\n");
00142                         console->Printf(KConsoleMessage2,text);
00143                         CleanupStack::PopAndDestroy(); // text
00144                         }
00145                 }
00146         // if all three views have been created, stop the active scheduler
00147         if (iNumViewsCreated == KTotalViews)
00148                 {
00149                 CActiveScheduler::Stop();
00150                 }
00151         }
00152 
00153 void CExampleViews::DoCancel()
00154         {
00155         //empty implementation.
00156         }
00157 
00158 void CExampleViews::Start()
00159         {
00160         TRequestStatus *pS=&iStatus;
00161         User::RequestComplete(pS,KErrNone);
00162         SetActive();
00163         }
00164 
00165 
00166 static void CreateExampleViewL()
00167 // Create the example views
00168         {
00169         CExampleViews *exampleViews = CExampleViews::NewL(*db);
00170         CleanupStack::PushL(exampleViews);
00171         exampleViews->Start();
00172         CActiveScheduler::Start();
00173         CleanupStack::PopAndDestroy(exampleViews);
00174         // close and cleanup database
00175         delete db;
00176         db=NULL;
00177         }
00178 
00179 // Adds a contact item to the contact database.
00180 static void AddEntryL()
00181         {
00182         _LIT(KForename,"John");
00183         _LIT(KSurname,"Smith");
00184         _LIT(KPhoneNumber,"+441617779700");
00185         _LIT(KEmailAddress,"john.smith@nokia.com");
00186 
00187         // Create a  contact card to contain the data
00188         CContactCard* newCard = CContactCard::NewLC();
00189 
00190         // Create the firstName field and add the data to it
00191         CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
00192         firstName->TextStorage()->SetTextL(KForename);
00193         newCard->AddFieldL(*firstName);
00194         CleanupStack::Pop(firstName);
00195 
00196         // Create the lastName field and add the data to it
00197         CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
00198         lastName ->TextStorage()->SetTextL(KSurname);
00199         newCard->AddFieldL(*lastName);
00200         CleanupStack::Pop(lastName);
00201 
00202         // Create the emailAddress field and add the data to it
00203         CContactItemField* emailAddr = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldEMail);
00204         emailAddr->SetMapping(KUidContactFieldVCardMapEMAILINTERNET);
00205         emailAddr ->TextStorage()->SetTextL(KEmailAddress);
00206         newCard->AddFieldL(*emailAddr);
00207         CleanupStack::Pop(emailAddr);
00208 
00209         // Create the phoneNo field and add the data to it
00210         CContactItemField* phoneNumber = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPhoneNumber);
00211         phoneNumber->SetMapping(KUidContactFieldVCardMapTEL);
00212         phoneNumber ->TextStorage()->SetTextL(KPhoneNumber);
00213         newCard->AddFieldL(*phoneNumber);
00214         CleanupStack::Pop(phoneNumber);
00215 
00216         // Add newCard to the database
00217     const TContactItemId contactId = db->AddNewContactL(*newCard);
00218         if(contactId == KErrDiskFull)
00219         {
00220         _LIT(KConsoleMessage,"disk does not have enough free space to add entry");
00221         // Failed to add an entry in to the database
00222         console->Printf(KConsoleMessage);
00223         User::Leave(contactId);
00224         }       
00225         CleanupStack::PopAndDestroy(newCard);
00226         }
00227 
00228 
00229 // Creates the contact database and opens it
00230 static void CreateDatabaseL()
00231         {
00232         TDriveUnit sysDrive (RFs::GetSystemDrive());
00233         TDriveName sysDriveName (sysDrive.Name());
00234         TFileName fileName(sysDriveName);
00235         fileName.Append(KContactsFilename);
00236 
00237         TRAPD(err,db=CContactDatabase::ReplaceL(fileName));
00238 
00239         // Checks if the database was created successfully
00240         // if not, exits with an error message.
00241 
00242         if(err!=KErrNone)
00243                 {
00244                 _LIT(KConsoleMessage,"Not able to create the database");
00245                 // Failed to create the database
00246                 console->Printf(KConsoleMessage);
00247                 User::Leave(err);
00248                 }
00249         else
00250                 {
00251                 _LIT(KConsoleMessage,"Successfully created the database\n");
00252                 console->Printf(KConsoleMessage);
00253                 // Add a contact entry to the database
00254                 AddEntryL();
00255 
00256                 }
00257         }
00258 
00259 
00260 static void DoExampleL()
00261         {
00262         _LIT(KTxtConsoleTitle,"Contact views example");
00263         // Create a console
00264         console = Console::NewL(KTxtConsoleTitle,TSize(KConsFullScreen,KConsFullScreen));
00265         CleanupStack::PushL(console);
00266 
00267         // Create the database
00268         CreateDatabaseL();
00269 
00270         // Create the views
00271         CreateExampleViewL();
00272 
00273         // wait for user to press a key before destroying console
00274         _LIT(KMsgPressAnyKey,"Press any key to continue\n\n");
00275         console->Printf(KMsgPressAnyKey);
00276         console->Getch();
00277         CleanupStack::PopAndDestroy(console);
00278         }
00279 
00280 // Standard entry point function
00281 extern TInt E32Main()
00282         {
00283         __UHEAP_MARK;
00284         // Active scheduler required as this is a console app
00285         CActiveScheduler* scheduler=new CActiveScheduler;
00286 
00287         // If active scheduler has been created, install it.
00288         if (scheduler)
00289                 {
00290                 CActiveScheduler::Install(scheduler);
00291                 // Cleanup stack needed
00292                 CTrapCleanup* cleanup=CTrapCleanup::New();
00293                 if (cleanup)
00294                         {
00295             // Call the function DoExampleL()
00296                     TRAP_IGNORE(DoExampleL());
00297                         delete cleanup;
00298                         }
00299                 delete scheduler;
00300                 }
00301 
00302         __UHEAP_MARKEND;
00303         return KErrNone;
00304         }

Generated on Thu Jan 21 10:32:54 2010 for TB10.1 Example Applications by  doxygen 1.5.3