From c1f0c1c8b56a8304a894dea7f361bda8cbc8d11a Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Tue, 22 Dec 2015 23:54:10 +0000 Subject: [PATCH] Added source, headers and unit testing for the NOTE vCard property for ContactDataObject. --- source/contacteditor/ContactDataObject.cpp | 188 +++++++++++++++++++++ source/contacteditor/ContactDataObject.h | 1 + source/tests/LoadCheck-Load4.vcf | 6 + source/tests/xestiaab_contactload.h | 121 +++++++++++++ 4 files changed, 316 insertions(+) diff --git a/source/contacteditor/ContactDataObject.cpp b/source/contacteditor/ContactDataObject.cpp index 7bd80be..0770747 100644 --- a/source/contacteditor/ContactDataObject.cpp +++ b/source/contacteditor/ContactDataObject.cpp @@ -95,6 +95,7 @@ ContactLoadStatus ContactDataObject::LoadFile(wxString Filename){ int TitleCount = 0; int RoleCount = 0; int OrganisationCount = 0; + int NoteCount = 0; wxString ContactLine; wxString PropertyLine; wxString PropertySeg1; @@ -306,6 +307,13 @@ ContactLoadStatus ContactDataObject::LoadFile(wxString Filename){ ProcessOrganisation(PropertySeg1, PropertySeg2, &OrganisationCount); OrganisationCount++; + } else if (Property == wxT("NOTE")) { + + // See frmContactEditor-LoadNote.cpp + + ProcessNote(PropertySeg1, PropertySeg2, &NoteCount); + NoteCount++; + } } @@ -3546,6 +3554,186 @@ void ContactDataObject::ProcessOrganisation(wxString PropertySeg1, wxString Prop } +void ContactDataObject::ProcessNote(wxString PropertySeg1, wxString PropertySeg2, int *NoteCount){ + + std::map SplitPoints; + std::map SplitLength; + std::map::iterator SLiter; + wxString PropertyData; + wxString PropertyName; + wxString PropertyValue; + wxString PropertyTokens; + bool FirstToken = TRUE; + int intPrevValue = 6; + int intPref = 0; + int intType = 0; + long ListCtrlIndex; + + SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue); + + intPrevValue = 5; + + PropertyType PropType = PROPERTY_NONE; + + // Look for type before continuing. + + CheckType(&PropertySeg1, &SplitPoints, &SplitLength, &intPrevValue, &PropType); + + // Setup the pointers. + + std::map *NoteList = NULL; + std::map *NoteListAltID = NULL; + std::map *NoteListPID = NULL; + std::map *NoteListType = NULL; + std::map *NoteListTokens = NULL; + std::map *NoteListLanguage = NULL; + std::map *NoteListSortAs = NULL; + std::map *NoteListPref = NULL; + + // Setup blank lines for later on. + + switch(PropType){ + case PROPERTY_NONE: + NoteList = &GeneralNoteList; + NoteListType = &GeneralNoteListType; + NoteListAltID = &GeneralNoteListAltID; + NoteListPID = &GeneralNoteListPID; + NoteListTokens = &GeneralNoteListTokens; + NoteListLanguage = &GeneralNoteListLanguage; + NoteListPref = &GeneralNoteListPref; + break; + case PROPERTY_HOME: + NoteList = &HomeNoteList; + NoteListType = &HomeNoteListType; + NoteListAltID = &HomeNoteListAltID; + NoteListPID = &HomeNoteListPID; + NoteListTokens = &HomeNoteListTokens; + NoteListLanguage = &HomeNoteListLanguage; + NoteListPref = &HomeNoteListPref; + break; + case PROPERTY_WORK: + NoteList = &BusinessNoteList; + NoteListType = &BusinessNoteListType; + NoteListAltID = &BusinessNoteListAltID; + NoteListPID = &BusinessNoteListPID; + NoteListTokens = &BusinessNoteListTokens; + NoteListLanguage = &BusinessNoteListLanguage; + NoteListPref = &BusinessNoteListPref; + break; + } + + intPrevValue = 5; + + for (std::map::iterator intiter = SplitPoints.begin(); + intiter != SplitPoints.end(); ++intiter){ + + SLiter = SplitLength.find(intiter->first); + + PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second)); + + wxStringTokenizer PropertyElement (PropertyData, wxT("=")); + PropertyName = PropertyElement.GetNextToken(); + PropertyValue = PropertyElement.GetNextToken(); + + intPrevValue = intiter->second; + + // Process properties. + + size_t intPropertyValueLen = PropertyValue.Len(); + + if (PropertyValue.Mid((intPropertyValueLen - 1), 1) == wxT("\"")){ + + PropertyValue.Trim(); + PropertyValue.RemoveLast(); + + } + + if (PropertyValue.Mid(0, 1) == wxT("\"")){ + + PropertyValue.Remove(0, 1); + + } + + CaptureString(&PropertyValue, FALSE); + + if (PropertyName == wxT("ALTID")){ + + NoteListAltID->erase(*NoteCount); + NoteListAltID->insert(std::make_pair(*NoteCount, PropertyValue)); + + } else if (PropertyName == wxT("PID")){ + + NoteListPID->erase(*NoteCount); + NoteListPID->insert(std::make_pair(*NoteCount, PropertyValue)); + + } else if (PropertyName == wxT("SORT-AS")){ + + NoteListSortAs->erase(*NoteCount); + NoteListSortAs->insert(std::make_pair(*NoteCount, PropertyValue)); + + } else if (PropertyName == wxT("PREF")){ + + int PriorityNumber = 0; + bool ValidNumber = TRUE; + + try{ + PriorityNumber = std::stoi(PropertyValue.ToStdString()); + } + + catch(std::invalid_argument &e){ + ValidNumber = FALSE; + } + + if (ValidNumber == TRUE){ + + NoteListPref->erase(*NoteCount); + NoteListPref->insert(std::make_pair(*NoteCount, PriorityNumber)); + + } + + } else if (PropertyName == wxT("LANGUAGE")){ + + NoteListLanguage->erase(*NoteCount); + NoteListLanguage->insert(std::make_pair(*NoteCount, PropertyValue)); + + } else { + + // Something else we don't know about so append + // to the tokens variable. + + if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty() && PropertyName != wxT("TYPE")){ + + if (FirstToken == TRUE){ + + PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue); + FirstToken = FALSE; + + } else { + + PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue); + + } + + } + + } + + } + + // Add the data to the General/Home/Work address variables. + + CaptureString(&PropertySeg2, FALSE); + + NoteList->insert(std::make_pair(*NoteCount, PropertySeg2)); + + if (!PropertyTokens.IsEmpty()){ + + NoteListTokens->insert(std::make_pair(*NoteCount, PropertyTokens)); + + } + +} + void SplitValues(wxString *PropertyLine, std::map *SplitPoints, std::map *SplitLength, diff --git a/source/contacteditor/ContactDataObject.h b/source/contacteditor/ContactDataObject.h index f5beef9..1ea9eba 100644 --- a/source/contacteditor/ContactDataObject.h +++ b/source/contacteditor/ContactDataObject.h @@ -74,6 +74,7 @@ class ContactDataObject{ void ProcessTitle(wxString PropertySeg1, wxString PropertySeg2, int *TitleCount); void ProcessRole(wxString PropertySeg1, wxString PropertySeg2, int *RoleCount); void ProcessOrganisation(wxString PropertySeg1, wxString PropertySeg2, int *OrganisationCount); + void ProcessNote(wxString PropertySeg1, wxString PropertySeg2, int *NoteCount); public: diff --git a/source/tests/LoadCheck-Load4.vcf b/source/tests/LoadCheck-Load4.vcf index 317caea..e282dbc 100644 --- a/source/tests/LoadCheck-Load4.vcf +++ b/source/tests/LoadCheck-Load4.vcf @@ -76,4 +76,10 @@ ORG;TYPE=home;SORT-AS="Person, Ordinary Lazy";ALTID=60;PID=61;PREF=62;LANGUA GE=en-GB;SOMEWHERE=There:Ordinary Lazy Person ORG;TYPE=work;SORT-AS="Owner, Company";ALTID=70;PID=71;PREF=72;LANGUAGE=en-A U;HERE=Nope:Company Owner +NOTE;ALTID=90;PID=91;LANGUAGE=en;PREF=92;WOO=WOOP:Note\n\nLine 3\nLine 4\nLi + ne 5 +NOTE;TYPE=home;ALTID=80;PID=81;LANGUAGE=kw;PREF=82;MOO=MEEP:Note\n\n\n\nLine + 5\nLine 6\nLine 7 +NOTE;TYPE=work;ALTID=70;PID=71;LANGUAGE=fr;PREF=72;BOING=BOOP:Note\n\n\n\nLi + ne 8\nLine 9\nLine 10 END:VCARD diff --git a/source/tests/xestiaab_contactload.h b/source/tests/xestiaab_contactload.h index 5746b9d..8f0f752 100644 --- a/source/tests/xestiaab_contactload.h +++ b/source/tests/xestiaab_contactload.h @@ -1959,4 +1959,125 @@ TEST(ContactLoad, OrganisationTests){ } +TEST(ContactLoad, NoteTests){ + + ContactDataObject TestFile; + + // Check that the vCard 4.0 file loads OK. + + ASSERT_EQ(CONTACTLOAD_OK, TestFile.LoadFile("LoadCheck-Load4.vcf")); + + std::map::iterator TestFileIter; + std::map::iterator TestFileIntIter; + + // Start with the general note. + + TestFileIter = TestFile.GeneralNoteList.find(0); + ASSERT_NE(TestFile.GeneralNoteList.end(), TestFileIter); + ASSERT_EQ("Note\n\nLine 3\nLine 4\nLine 5", TestFileIter->second); + + // Check the ALTID section. + + TestFileIter = TestFile.GeneralNoteListAltID.find(0); + ASSERT_NE(TestFile.GeneralNoteListAltID.end(), TestFileIter); + ASSERT_EQ("90", TestFileIter->second); + + // Check the PID section. + + TestFileIter = TestFile.GeneralNoteListPID.find(0); + ASSERT_NE(TestFile.GeneralNoteListPID.end(), TestFileIter); + ASSERT_EQ("91", TestFileIter->second); + + // Check the LANGUAGE section. + + TestFileIter = TestFile.GeneralNoteListLanguage.find(0); + ASSERT_NE(TestFile.GeneralNoteListLanguage.end(), TestFileIter); + ASSERT_EQ("en", TestFileIter->second); + + // Check the PREF section. + + TestFileIntIter = TestFile.GeneralNoteListPref.find(0); + ASSERT_NE(TestFile.GeneralNoteListPref.end(), TestFileIntIter); + ASSERT_EQ(92, TestFileIntIter->second); + + // Check the extra tokens. + + TestFileIter = TestFile.GeneralNoteListTokens.find(0); + ASSERT_NE(TestFile.GeneralNoteListTokens.end(), TestFileIter); + ASSERT_EQ("WOO=WOOP", TestFileIter->second); + + // Start with the home note. + + TestFileIter = TestFile.HomeNoteList.find(1); + ASSERT_NE(TestFile.HomeNoteList.end(), TestFileIter); + ASSERT_EQ("Note\n\n\n\nLine 5\nLine 6\nLine 7", TestFileIter->second); + + // Check the ALTID section. + + TestFileIter = TestFile.HomeNoteListAltID.find(1); + ASSERT_NE(TestFile.HomeNoteListAltID.end(), TestFileIter); + ASSERT_EQ("80", TestFileIter->second); + + // Check the PID section. + + TestFileIter = TestFile.HomeNoteListPID.find(1); + ASSERT_NE(TestFile.HomeNoteListPID.end(), TestFileIter); + ASSERT_EQ("81", TestFileIter->second); + + // Check the LANGUAGE section. + + TestFileIter = TestFile.HomeNoteListLanguage.find(1); + ASSERT_NE(TestFile.HomeNoteListLanguage.end(), TestFileIter); + ASSERT_EQ("kw", TestFileIter->second); + + // Check the PREF section. + + TestFileIntIter = TestFile.HomeNoteListPref.find(1); + ASSERT_NE(TestFile.HomeNoteListPref.end(), TestFileIntIter); + ASSERT_EQ(82, TestFileIntIter->second); + + // Check the extra tokens. + + TestFileIter = TestFile.HomeNoteListTokens.find(1); + ASSERT_NE(TestFile.HomeNoteListTokens.end(), TestFileIter); + ASSERT_EQ("MOO=MEEP", TestFileIter->second); + + // Start with the business note. + + TestFileIter = TestFile.BusinessNoteList.find(2); + ASSERT_NE(TestFile.BusinessNoteList.end(), TestFileIter); + ASSERT_EQ("Note\n\n\n\nLine 8\nLine 9\nLine 10", TestFileIter->second); + + // Check the ALTID section. + + TestFileIter = TestFile.BusinessNoteListAltID.find(2); + ASSERT_NE(TestFile.BusinessNoteListAltID.end(), TestFileIter); + ASSERT_EQ("70", TestFileIter->second); + + // Check the PID section. + + TestFileIter = TestFile.BusinessNoteListPID.find(2); + ASSERT_NE(TestFile.BusinessNoteListPID.end(), TestFileIter); + ASSERT_EQ("71", TestFileIter->second); + + // Check the LANGUAGE section. + + TestFileIter = TestFile.BusinessNoteListLanguage.find(2); + ASSERT_NE(TestFile.BusinessNoteListLanguage.end(), TestFileIter); + ASSERT_EQ("fr", TestFileIter->second); + + // Check the PREF section. + + TestFileIntIter = TestFile.BusinessNoteListPref.find(2); + ASSERT_NE(TestFile.BusinessNoteListPref.end(), TestFileIntIter); + ASSERT_EQ(72, TestFileIntIter->second); + + // Check the extra tokens. + + TestFileIter = TestFile.BusinessNoteListTokens.find(2); + ASSERT_NE(TestFile.BusinessNoteListTokens.end(), TestFileIter); + ASSERT_EQ("BOING=BOOP", TestFileIter->second); + +} + // TODO: Add tests for the Contact Loading process. \ No newline at end of file -- 2.39.2