00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00030 #include "ContactViews.h"
00031
00032
00033 const TInt KTotalViews = 3;
00034
00035 static CConsoleBase* console;
00036
00037 _LIT(KContactsFilename,"exampleViews.cdb");
00038
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
00074 iSortOrder.AppendL(KUidContactFieldGivenName);
00075 iSortOrder.AppendL(KUidContactFieldFamilyName);
00076 iSortOrder.AppendL(KUidContactFieldPhoneNumber);
00077 iSortOrder.AppendL(KUidContactFieldEMail);
00078 CActiveScheduler::Add(this);
00079 }
00080
00081
00082 void CExampleViews::RunL()
00083 {
00084 iLocalView=CContactLocalView::NewL(*this,iDb,iSortOrder,EContactsOnly);
00085
00086 CPtrCArray* desArray =new (ELeave) CPtrCArray(1);
00087 _LIT(KSearchString,"Smith");
00088 TPtrC searchString(KSearchString);
00089 desArray->AppendL(searchString);
00090
00091 iFindView = CContactFindView::NewL(iDb, *iLocalView,*this, desArray);
00092 desArray->Reset();
00093 delete desArray;
00094
00095
00096 iFilterView = CContactFilteredView::NewL(*this, iDb, *iLocalView, CContactDatabase::EPhonable);
00097 }
00098
00099
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
00118 {
00119 _LIT(KConsoleMessage,"Contents of find view:\n");
00120 console->Printf(KConsoleMessage);
00121 for (TInt count = 0; count < iFindView->CountL(); count++)
00122 {
00123
00124 _LIT(KFieldSeparator,",");
00125 HBufC* text = iFindView->AllFieldsLC(count,KFieldSeparator);
00126 _LIT(KConsoleMessage2,"%S\n");
00127 console->Printf(KConsoleMessage2,text);
00128 CleanupStack::PopAndDestroy();
00129 }
00130 }
00131 else if (&aView == iFilterView)
00132
00133 {
00134 _LIT(KConsoleMessage,"Contents of filter view:\n");
00135 console->Printf(KConsoleMessage);
00136 for (TInt count = 0; count < iFilterView->CountL(); count++)
00137 {
00138
00139 _LIT(KFieldSeparator,",");
00140 HBufC* text = iFilterView->AllFieldsLC(count,KFieldSeparator);
00141 _LIT(KConsoleMessage2,"%S\n");
00142 console->Printf(KConsoleMessage2,text);
00143 CleanupStack::PopAndDestroy();
00144 }
00145 }
00146
00147 if (iNumViewsCreated == KTotalViews)
00148 {
00149 CActiveScheduler::Stop();
00150 }
00151 }
00152
00153 void CExampleViews::DoCancel()
00154 {
00155
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
00168 {
00169 CExampleViews *exampleViews = CExampleViews::NewL(*db);
00170 CleanupStack::PushL(exampleViews);
00171 exampleViews->Start();
00172 CActiveScheduler::Start();
00173 CleanupStack::PopAndDestroy(exampleViews);
00174
00175 delete db;
00176 db=NULL;
00177 }
00178
00179
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
00188 CContactCard* newCard = CContactCard::NewLC();
00189
00190
00191 CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
00192 firstName->TextStorage()->SetTextL(KForename);
00193 newCard->AddFieldL(*firstName);
00194 CleanupStack::Pop(firstName);
00195
00196
00197 CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
00198 lastName ->TextStorage()->SetTextL(KSurname);
00199 newCard->AddFieldL(*lastName);
00200 CleanupStack::Pop(lastName);
00201
00202
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
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
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
00222 console->Printf(KConsoleMessage);
00223 User::Leave(contactId);
00224 }
00225 CleanupStack::PopAndDestroy(newCard);
00226 }
00227
00228
00229
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
00240
00241
00242 if(err!=KErrNone)
00243 {
00244 _LIT(KConsoleMessage,"Not able to create the database");
00245
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
00254 AddEntryL();
00255
00256 }
00257 }
00258
00259
00260 static void DoExampleL()
00261 {
00262 _LIT(KTxtConsoleTitle,"Contact views example");
00263
00264 console = Console::NewL(KTxtConsoleTitle,TSize(KConsFullScreen,KConsFullScreen));
00265 CleanupStack::PushL(console);
00266
00267
00268 CreateDatabaseL();
00269
00270
00271 CreateExampleViewL();
00272
00273
00274 _LIT(KMsgPressAnyKey,"Press any key to continue\n\n");
00275 console->Printf(KMsgPressAnyKey);
00276 console->Getch();
00277 CleanupStack::PopAndDestroy(console);
00278 }
00279
00280
00281 extern TInt E32Main()
00282 {
00283 __UHEAP_MARK;
00284
00285 CActiveScheduler* scheduler=new CActiveScheduler;
00286
00287
00288 if (scheduler)
00289 {
00290 CActiveScheduler::Install(scheduler);
00291
00292 CTrapCleanup* cleanup=CTrapCleanup::New();
00293 if (cleanup)
00294 {
00295
00296 TRAP_IGNORE(DoExampleL());
00297 delete cleanup;
00298 }
00299 delete scheduler;
00300 }
00301
00302 __UHEAP_MARKEND;
00303 return KErrNone;
00304 }