// Helper function for RenameDefectL() below. Sets a sort ordering for the database. // LOCAL_C void SetSortOrderL(CContactDatabase& aDb, TFieldType aFieldType) { CArrayFix<CContactDatabase::TSortPref>* sortOrder=new(ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(3); CleanupStack::PushL(sortOrder); sortOrder->AppendL(CContactDatabase::TSortPref(aFieldType,CContactDatabase::TSortPref::EAsc)); aDb.SortL(sortOrder); CleanupStack::Pop(); // sortOrder }
GLDEF_C void DoSortL(CContactDatabase& aDb) { CContactTextDef* textDef=CContactTextDef::NewLC(); textDef->AppendL(TContactTextDefItem(KUidContactFieldGivenName)); textDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName)); textDef->AppendL(TContactTextDefItem(KUidContactFieldCompanyName)); aDb.SetTextDefinitionL(textDef); // Takes ownership. CleanupStack::Pop(); // textDef. CArrayFix<CContactDatabase::TSortPref>* sortOrder=new(ELeave)CArrayFixFlat<CContactDatabase::TSortPref>(2); CleanupStack::PushL(sortOrder); sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldDefinedText)); aDb.SortL(sortOrder); // Takes ownership. CleanupStack::Pop(); // sortOrder }
LOCAL_C void DoTestsL() { test.Start(_L("@SYMTESTCaseID:PIM-T-DB-SORTL-TEST-0001 T_DB_SortL_test")); // create default, empty database CContactDatabase* db = CContactDatabase::ReplaceL(KDbName); CleanupStack::PushL(db); // create contact 1 and add it to the db CContactCard* contact1 = CContactCard::NewLC(); CContactItemField* fname1 = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName); _LIT(KFname1, "Aaron"); fname1->TextStorage()->SetTextL(KFname1() ); contact1->AddFieldL(*fname1); CleanupStack::Pop(fname1); CContactItemField* lname1 = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName); _LIT(KLname1, "Zimmerman"); lname1->TextStorage()->SetTextL(KLname1() ); contact1->AddFieldL(*lname1); CleanupStack::Pop(lname1); db->AddNewContactL(*contact1); // create contact 2 and add it to the db CContactCard* contact2 = CContactCard::NewLC(); CContactItemField* fname2 = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName); _LIT(KFname2, "Zachary"); fname2->TextStorage()->SetTextL(KFname2() ); contact2->AddFieldL(*fname2); CleanupStack::Pop(fname2); CContactItemField* lname2 = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName); _LIT(KLname2, "Abrahams"); lname2->TextStorage()->SetTextL(KLname2() ); contact2->AddFieldL(*lname2); CleanupStack::Pop(lname2); db->AddNewContactL(*contact2); // fetch and print contact IDs. Expected order: 1, 2 const CContactIdArray* items = db->SortedItemsL(); // doesn't take ownership _LIT(KFormattedIdList1, "Contact IDs *before* sorting: %d, %d\n"); test.Printf(KFormattedIdList1, (*items)[0], (*items)[1]); TInt item1BeforeSort = (*items)[0]; TInt item2BeforeSort = (*items)[1]; // create sort order array CArrayFix<CContactDatabase::TSortPref>* sortOrder = new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(2); CleanupStack::PushL(sortOrder); sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldFamilyName) ); sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldGivenName) ); // sort db and measure differences in heap allocations as a proxy for // whether sortOrder has been deleted by SortL() or not. TInt totalAllocSize(0); TInt heapCellsDifference = User::AllocSize(totalAllocSize); db->SortL(sortOrder); heapCellsDifference -= User::AllocSize(totalAllocSize); CleanupStack::Pop(sortOrder); // fetch and print contact IDs. Expected order: 2, 1 items = db->SortedItemsL(); // doesn't take ownership _LIT(KFormattedIdList2, "Contact IDs *after* sorting: %d, %d\n"); test.Printf(KFormattedIdList2, (*items)[0], (*items)[1]); TInt item1AfterSort = (*items)[0]; TInt item2AfterSort = (*items)[1]; //check the sort order to make sure test(item1BeforeSort == item2AfterSort); test(item2BeforeSort == item1AfterSort); // check to see if sortOrder is still usable if (heapCellsDifference == 0) { // attempt to re-use sortOrder -- should be allowed after workaround CContactDatabase::TSortPref sortPref( (*sortOrder)[0]); _LIT(KSortOrderInfo, "The first sort order preference's field type is: %d.\n"); test.Printf(KSortOrderInfo, sortPref.iFieldType); _LIT(KYesSame, "Yes, have successfully re-used sortOrder.\n"); test.Printf(KYesSame); } else { // looks like sortOrder has been deleted so don't try to use it _LIT(KNoDifferent, "No, have not reused sortOrder as it has probably been deleted.\n"); test.Printf(KNoDifferent); } // cleanup CleanupStack::PopAndDestroy(3, db); // and contact1, contact2 CContactDatabase::DeleteDatabaseL(KDbName); test.End(); test.Close(); }