From ac5416e95caeeefefdbf9706b65926f6b75589cf Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 13 Dec 2015 18:07:53 +0000 Subject: [PATCH] Added source, header and unit tests for TZ vCard property in the ContactDataObject. --- source/contacteditor/ContactDataObject.cpp | 164 ++++++++++++++++++++- source/contacteditor/ContactDataObject.h | 1 + source/tests/LoadCheck-Load4.vcf | 6 + source/tests/xestiaab_contactload.h | 127 +++++++++++++++- 4 files changed, 290 insertions(+), 8 deletions(-) diff --git a/source/contacteditor/ContactDataObject.cpp b/source/contacteditor/ContactDataObject.cpp index 5275553..2a7f6a4 100644 --- a/source/contacteditor/ContactDataObject.cpp +++ b/source/contacteditor/ContactDataObject.cpp @@ -83,6 +83,7 @@ ContactLoadStatus ContactDataObject::LoadFile(wxString Filename){ int GroupCount = 0; int FNCount = 0; int NicknameCount = 0; + int TimeZoneCount = 0; wxString ContactLine; wxString PropertyLine; wxString PropertySeg1; @@ -220,6 +221,11 @@ ContactLoadStatus ContactDataObject::LoadFile(wxString Filename){ ProcessAnniversary(PropertySeg1, PropertySeg2); AnniversaryProcessed = TRUE; + } else if (Property == wxT("TZ")){ + + ProcessTimeZone(PropertySeg1, PropertySeg2, &TimeZoneCount); + TimeZoneCount++; + } } @@ -990,11 +996,11 @@ void ContactDataObject::ProcessAnniversary(wxString PropertySeg1, wxString Prope wxString PropertyValue; wxString PropertyTokens; bool AnniversaryText = FALSE; - int intPrevValue = 6; + int intPrevValue = 13; SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue); - intPrevValue = 5; + intPrevValue = 12; // Look for type before continuing. @@ -1023,7 +1029,7 @@ void ContactDataObject::ProcessAnniversary(wxString PropertySeg1, wxString Prope // Setup blank lines for later on. - intPrevValue = 5; + intPrevValue = 12; bool FirstToken = TRUE; for (std::map::iterator intiter = SplitPoints.begin(); @@ -1104,6 +1110,158 @@ void ContactDataObject::ProcessAnniversary(wxString PropertySeg1, wxString Prope } +void ContactDataObject::ProcessTimeZone(wxString PropertySeg1, wxString PropertySeg2, int *TimeZoneCount){ + + std::map SplitPoints; + std::map SplitLength; + + int intPrevValue = 4; + int intPref = 0; + + SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue); + + intPrevValue = 3; + + PropertyType PropType; + + // Look for type before continuing. + + CheckType(&PropertySeg1, &SplitPoints, &SplitLength, &intPrevValue, &PropType); + + intPrevValue = 3; + + std::map *TZList = NULL; + std::map *TZListType = NULL; + std::map *TZListMediatype = NULL; + std::map *TZListAltID = NULL; + std::map *TZListPID = NULL; + std::map *TZListTokens = NULL; + std::map *TZListPref = NULL; + + switch(PropType){ + case PROPERTY_NONE: + TZList = &GeneralTZList; + TZListType = &GeneralTZListType; + TZListMediatype = &GeneralTZListMediatype; + TZListAltID = &GeneralTZListAltID; + TZListPID = &GeneralTZListPID; + TZListTokens = &GeneralTZListTokens; + TZListPref = &GeneralTZListPref; + break; + case PROPERTY_HOME: + TZList = &HomeTZList; + TZListType = &HomeTZListType; + TZListMediatype = &HomeTZListMediatype; + TZListAltID = &HomeTZListAltID; + TZListPID = &HomeTZListPID; + TZListTokens = &HomeTZListTokens; + TZListPref = &HomeTZListPref; + break; + case PROPERTY_WORK: + TZList = &BusinessTZList; + TZListType = &BusinessTZListType; + TZListMediatype = &BusinessTZListMediatype; + TZListAltID = &BusinessTZListAltID; + TZListPID = &BusinessTZListPID; + TZListTokens = &BusinessTZListTokens; + TZListPref = &BusinessTZListPref; + break; + } + + std::map::iterator SLiter; + wxString PropertyData; + wxString PropertyName; + wxString PropertyValue; + wxString PropertyTokens; + bool FirstToken = TRUE; + + 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; + + CaptureString(&PropertyValue, FALSE); + + if (PropertyName == wxT("ALTID")){ + + TZListAltID->erase(*TimeZoneCount); + TZListAltID->insert(std::make_pair(*TimeZoneCount, PropertyValue)); + + } else if (PropertyName == wxT("PID")){ + + TZListPID->erase(*TimeZoneCount); + TZListPID->insert(std::make_pair(*TimeZoneCount, 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){ + + TZListPref->erase(*TimeZoneCount); + TZListPref->insert(std::make_pair(*TimeZoneCount, PriorityNumber)); + + } + + } else if (PropertyName == wxT("MEDIATYPE")){ + + TZListMediatype->erase(*TimeZoneCount); + TZListMediatype->insert(std::make_pair(*TimeZoneCount, 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); + + } + + } + + } + + } + + TZList->insert(std::make_pair(*TimeZoneCount, PropertySeg2)); + + // Add the name token data. + + if (!PropertyTokens.IsEmpty()){ + + TZListTokens->insert(std::make_pair(*TimeZoneCount, PropertyTokens)); + + } + + +} + void SplitValues(wxString *PropertyLine, std::map *SplitPoints, std::map *SplitLength, diff --git a/source/contacteditor/ContactDataObject.h b/source/contacteditor/ContactDataObject.h index 3a2ca7f..abf22dd 100644 --- a/source/contacteditor/ContactDataObject.h +++ b/source/contacteditor/ContactDataObject.h @@ -572,6 +572,7 @@ class ContactDataObject{ void ProcessGender(wxString PropertySeg1, wxString PropertySeg2); void ProcessBirthday(wxString PropertySeg1, wxString PropertySeg2); void ProcessAnniversary(wxString PropertySeg1, wxString PropertySeg2); + void ProcessTimeZone(wxString PropertySeg1, wxString PropertySeg2, int *TimeZoneCount); }; diff --git a/source/tests/LoadCheck-Load4.vcf b/source/tests/LoadCheck-Load4.vcf index 32ef415..1a6a8b9 100644 --- a/source/tests/LoadCheck-Load4.vcf +++ b/source/tests/LoadCheck-Load4.vcf @@ -14,4 +14,10 @@ NICKNAME;TYPE=work;ALTID=99;PID=10;PREF=1;LANGUAGE=en-GB;YAY=Maybe;Boop=Boin GENDER;BEEP=Boop:F;Example Text BDAY;ALTID=35;CALSCALE=georgian;HAPPY=Days:20040101 ANNIVERSARY;ALTID=70;CALSCALE=georgian;WONDERFUL=Day:20200516 +TZ;ALTID=35;PID=60;PREF=20;MEDIATYPE=text/plain;WHATS=This;A=Potato:Europe/T + ruro +TZ;TYPE=home;ALTID=15;PID=20;PREF=40;MEDIATYPE=text/plain;MAYBE=not:Europe/P + enzance +TZ;TYPE=work;PID=70;ALTID=45;PREF=80;MEDIATYPE=text/plain;MAYBE=yes:Europe/S + t Austell END:VCARD diff --git a/source/tests/xestiaab_contactload.h b/source/tests/xestiaab_contactload.h index 10d10b0..d934194 100644 --- a/source/tests/xestiaab_contactload.h +++ b/source/tests/xestiaab_contactload.h @@ -302,8 +302,6 @@ TEST(ContactLoad, BirthdayTests){ // Check that the birthday has been read. - // Check that the gender has been read. - ContactDataObject TestFile; // Check that the vCard 4.0 file loads OK. @@ -340,9 +338,7 @@ TEST(ContactLoad, BirthdayTests){ TEST(ContactLoad, AnniversaryTests){ - // Check that the birthday has been read. - - // Check that the gender has been read. + // Check that the anniversary has been read. ContactDataObject TestFile; @@ -378,4 +374,125 @@ TEST(ContactLoad, AnniversaryTests){ } +TEST(ContactLoad, TimeZoneTests){ + + 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; + + // Check that the timezone has been read (TZ). General. + + TestFileIter = TestFile.GeneralTZList.find(0); + ASSERT_NE(TestFile.GeneralTZList.end(), TestFileIter); + ASSERT_EQ("Europe/Truro", TestFileIter->second); + + // Check the ALTID parameter. + + TestFileIter = TestFile.GeneralTZListAltID.find(0); + ASSERT_NE(TestFile.GeneralTZListAltID.end(), TestFileIter); + ASSERT_EQ("35", TestFileIter->second); + + // Check the PID parameter. + + TestFileIter = TestFile.GeneralTZListPID.find(0); + ASSERT_NE(TestFile.GeneralTZListPID.end(), TestFileIter); + ASSERT_EQ("60", TestFileIter->second); + + // Check the PREF parameter. + + TestFileIntIter = TestFile.GeneralTZListPref.find(0); + ASSERT_NE(TestFile.GeneralTZListPref.end(), TestFileIntIter); + ASSERT_EQ(20, TestFileIntIter->second); + + // Check the LANGUAGE parameter. + + TestFileIter = TestFile.GeneralTZListMediatype.find(0); + ASSERT_NE(TestFile.GeneralTZListMediatype.end(), TestFileIter); + ASSERT_EQ("text/plain", TestFileIter->second); + + // Check the extra tokens parameter. + + TestFileIter = TestFile.GeneralTZListTokens.find(0); + ASSERT_NE(TestFile.GeneralTZListTokens.end(), TestFileIter); + ASSERT_EQ("WHATS=This;A=Potato", TestFileIter->second); + + // Check that the timezone has been read (TZ). Home. + + TestFileIter = TestFile.HomeTZList.find(1); + ASSERT_NE(TestFile.HomeTZList.end(), TestFileIter); + ASSERT_EQ("Europe/Penzance", TestFileIter->second); + + // Check the ALTID parameter. + + TestFileIter = TestFile.HomeTZListAltID.find(1); + ASSERT_NE(TestFile.HomeTZListAltID.end(), TestFileIter); + ASSERT_EQ("15", TestFileIter->second); + + // Check the PID parameter. + + TestFileIter = TestFile.HomeTZListPID.find(1); + ASSERT_NE(TestFile.HomeTZListPID.end(), TestFileIter); + ASSERT_EQ("20", TestFileIter->second); + + // Check the PREF parameter. + + TestFileIntIter = TestFile.HomeTZListPref.find(1); + ASSERT_NE(TestFile.HomeTZListPref.end(), TestFileIntIter); + ASSERT_EQ(40, TestFileIntIter->second); + + // Check the LANGUAGE parameter. + + TestFileIter = TestFile.HomeTZListMediatype.find(1); + ASSERT_NE(TestFile.HomeTZListMediatype.end(), TestFileIter); + ASSERT_EQ("text/plain", TestFileIter->second); + + // Check the extra tokens parameter. + + TestFileIter = TestFile.HomeTZListTokens.find(1); + ASSERT_NE(TestFile.HomeTZListTokens.end(), TestFileIter); + ASSERT_EQ("MAYBE=not", TestFileIter->second); + + // Check that the timezone has been read (TZ). Business. + + TestFileIter = TestFile.BusinessTZList.find(2); + ASSERT_NE(TestFile.BusinessTZList.end(), TestFileIter); + ASSERT_EQ("Europe/St Austell", TestFileIter->second); + + // Check the ALTID parameter. + + TestFileIter = TestFile.BusinessTZListAltID.find(2); + ASSERT_NE(TestFile.BusinessTZListAltID.end(), TestFileIter); + ASSERT_EQ("45", TestFileIter->second); + + // Check the PID parameter. + + TestFileIter = TestFile.BusinessTZListPID.find(2); + ASSERT_NE(TestFile.BusinessTZListPID.end(), TestFileIter); + ASSERT_EQ("70", TestFileIter->second); + + // Check the PREF parameter. + + TestFileIntIter = TestFile.BusinessTZListPref.find(2); + ASSERT_NE(TestFile.BusinessTZListPref.end(), TestFileIntIter); + ASSERT_EQ(80, TestFileIntIter->second); + + // Check the LANGUAGE parameter. + + TestFileIter = TestFile.BusinessTZListMediatype.find(2); + ASSERT_NE(TestFile.BusinessTZListMediatype.end(), TestFileIter); + ASSERT_EQ("text/plain", TestFileIter->second); + + // Check the extra tokens parameter. + + TestFileIter = TestFile.BusinessTZListTokens.find(2); + ASSERT_NE(TestFile.BusinessTZListTokens.end(), TestFileIter); + ASSERT_EQ("MAYBE=yes", TestFileIter->second); + +} + // TODO: Add tests for the Contact Loading process. \ No newline at end of file -- 2.39.5