1 // ContactDataObject.cpp - Client Data Object.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "ContactDataObject.h"
21 ContactLoadStatus ContactDataObject::LoadFile(wxString Filename){
23 if (!wxFileExists(Filename)){
25 return CONTACTLOAD_FILEMISSING;
31 if (!ContactFile.Open(Filename, wxFile::read, wxS_DEFAULT)){
33 return CONTACTLOAD_FILEERROR;
37 // Check that the vCard is a valid vCard 4.0 file.
39 vCard vCard4FormatCheck;
41 vCard4FormatCheck.LoadFile(Filename);
43 if (vCard4FormatCheck.Get("VERSION") != wxT("4.0")){
45 return CONTACTLOAD_FILEINVALIDFORMAT;
49 // Check that the vCard meets the base specification.
51 if (!vCard4FormatCheck.MeetBaseSpecification()){
53 return CONTACTLOAD_FILEBASESPECFAIL;
57 wxStringTokenizer wSTContactFileLines(vCard4FormatCheck.WriteString(), wxT("\r\n"));
59 std::map<int, wxString> ContactFileLines;
61 int ContactLineSeek = 0;
63 while (wSTContactFileLines.HasMoreTokens() == TRUE){
65 wxString ContactLine = wSTContactFileLines.GetNextToken();
66 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
71 wxString wxSPropertyNextLine;
73 bool ExtraLineSeek = TRUE;
74 bool QuoteMode = FALSE;
75 bool PropertyFind = TRUE;
76 bool KindProcessed = FALSE;
77 bool NameProcessed = FALSE;
78 int ContactLineLen = 0;
79 int QuoteBreakPoint = 0;
82 int NicknameCount = 0;
84 wxString PropertyLine;
85 wxString PropertySeg1;
86 wxString PropertySeg2;
87 wxString PropertyNextLine;
90 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
91 iter != ContactFileLines.end(); ++iter){
100 PropertySeg1.Clear();
101 PropertySeg2.Clear();
104 ContactLine = iter->second;
106 while (ExtraLineSeek == TRUE){
108 // Check if there is extra data on the next line
109 // (indicated by space or tab at the start) and add data.
113 if (iter == ContactFileLines.end()){
120 PropertyNextLine = iter->second;
122 if (PropertyNextLine.Mid(0, 1) == wxT(" ") || PropertyNextLine.Mid(0, 1) == wxT("\t")){
124 PropertyNextLine.Remove(0, 1);
125 ContactLine.Append(PropertyNextLine);
130 ExtraLineSeek = FALSE;
136 ContactLineLen = ContactLine.Len();
138 // Make sure we are not in quotation mode.
139 // Make sure colon does not have \ or \\ before it.
141 for (int i = 0; i <= ContactLineLen; i++){
143 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
145 PropertyFind = FALSE;
147 } else if (PropertyFind == TRUE){
149 Property.Append(ContactLine.Mid(i, 1));
153 if (ContactLine.Mid(i, 1) == wxT("\"")){
155 if (QuoteMode == TRUE){
167 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
176 // Split that line at the point into two variables (ignore the colon).
178 PropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
179 PropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
181 if (Property == wxT("KIND") && KindProcessed == FALSE){
183 ProcessKind(PropertySeg2);
185 } else if (Property == wxT("MEMBER")){
187 ProcessMember(PropertySeg1, PropertySeg2, &GroupCount);
190 } else if (Property == wxT("FN")){
192 ProcessFN(PropertySeg1, PropertySeg2, &FNCount);
195 } else if (Property == wxT("N") && NameProcessed == FALSE){
197 ProcessN(PropertySeg1, PropertySeg2);
198 NameProcessed = TRUE;
200 } else if (Property == wxT("NICKNAME")){
202 ProcessNickname(PropertySeg1, PropertySeg2, &NicknameCount);
209 return CONTACTLOAD_OK;
213 void ContactDataObject::ProcessKind(wxString KindType){
215 if (KindType == wxT("individual")){
217 ContactKind = CONTACTKIND_INDIVIDUAL;
219 } else if (KindType == wxT("group")){
221 ContactKind = CONTACTKIND_GROUP;
223 } else if (KindType == wxT("org")){
225 ContactKind = CONTACTKIND_ORGANISATION;
227 } else if (KindType == wxT("location")){
229 ContactKind = CONTACTKIND_LOCATION;
233 ContactKind = CONTACTKIND_NONE;
238 void ContactDataObject::ProcessMember(wxString PropertySeg1, wxString PropertySeg2, int *GroupCount){
240 std::map<int, int> SplitPoints;
241 std::map<int, int> SplitLength;
243 int intPrevValue = 8;
247 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
251 wxString PropertyName;
252 wxString PropertyValue;
253 wxString PropertyData;
254 wxString PropertyTokens;
255 std::map<int,int>::iterator SLiter;
256 bool FirstToken = TRUE;
258 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
259 intiter != SplitPoints.end(); ++intiter){
261 SLiter = SplitLength.find(intiter->first);
263 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
265 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
266 PropertyName = PropertyElement.GetNextToken();
267 PropertyValue = PropertyElement.GetNextToken();
269 intPrevValue = intiter->second;
271 CaptureString(&PropertyValue, FALSE);
273 if (PropertyName == wxT("ALTID")){
275 GroupsListAltID.erase(*GroupCount);
276 GroupsListAltID.insert(std::make_pair(*GroupCount, PropertyValue));
278 } else if (PropertyName == wxT("PID")){
280 GroupsListPID.erase(*GroupCount);
281 GroupsListPID.insert(std::make_pair(*GroupCount, PropertyValue));
283 } else if (PropertyName == wxT("PREF")){
285 int PriorityNumber = 0;
286 bool ValidNumber = TRUE;
289 PriorityNumber = std::stoi(PropertyValue.ToStdString());
292 catch(std::invalid_argument &e){
296 if (ValidNumber == TRUE){
298 GroupsListPref.erase(*GroupCount);
299 GroupsListPref.insert(std::make_pair(*GroupCount, PriorityNumber));
303 } else if (PropertyName == wxT("MEDIATYPE")){
305 GroupsListMediaType.erase(*GroupCount);
306 GroupsListMediaType.insert(std::make_pair(*GroupCount, PropertyValue));
308 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
310 if (FirstToken == TRUE){
312 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
317 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
325 GroupsList.insert(std::make_pair(*GroupCount, PropertySeg2));
327 if (!PropertyTokens.IsEmpty()){
329 GroupsListTokens.insert(std::make_pair(*GroupCount, PropertyTokens));
336 void ContactDataObject::ProcessFN(wxString PropertySeg1, wxString PropertySeg2, int *FNCount){
338 std::map<int, int> SplitPoints;
339 std::map<int, int> SplitLength;
341 int intPrevValue = 4;
345 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
349 wxString PropertyName;
350 wxString PropertyValue;
351 wxString PropertyData;
352 wxString PropertyTokens;
353 std::map<int,int>::iterator SLiter;
354 bool FirstToken = TRUE;
356 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
357 intiter != SplitPoints.end(); ++intiter){
359 SLiter = SplitLength.find(intiter->first);
361 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
363 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
364 PropertyName = PropertyElement.GetNextToken();
365 PropertyValue = PropertyElement.GetNextToken();
367 intPrevValue = intiter->second;
369 CaptureString(&PropertyValue, FALSE);
371 if (PropertyName == wxT("TYPE")){
373 if (!PropertyValue.IsEmpty() || PropertyValue == wxT("home") ||
374 PropertyValue == wxT("work") ){
376 FullNamesListType.erase(*FNCount);
377 FullNamesListType.insert(std::make_pair(*FNCount, PropertyValue));
381 } else if (PropertyName == wxT("LANGUAGE")){
383 FullNamesListLanguage.erase(*FNCount);
384 FullNamesListLanguage.insert(std::make_pair(*FNCount, PropertyValue));
386 } else if (PropertyName == wxT("ALTID")){
388 FullNamesListAltID.erase(*FNCount);
389 FullNamesListAltID.insert(std::make_pair(*FNCount, PropertyValue));
391 } else if (PropertyName == wxT("PID")){
393 FullNamesListPID.erase(*FNCount);
394 FullNamesListPID.insert(std::make_pair(*FNCount, PropertyValue));
396 } else if (PropertyName == wxT("PREF")){
398 int PriorityNumber = 0;
399 bool ValidNumber = TRUE;
402 PriorityNumber = std::stoi(PropertyValue.ToStdString());
405 catch(std::invalid_argument &e){
409 if (ValidNumber == TRUE){
411 FullNamesListPref.erase(*FNCount);
412 FullNamesListPref.insert(std::make_pair(*FNCount, PriorityNumber));
416 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
418 if (FirstToken == TRUE){
420 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
425 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
433 FullNamesList.insert(std::make_pair(*FNCount, PropertySeg2));
435 if (!PropertyTokens.IsEmpty()){
437 FullNamesListTokens.insert(std::make_pair(*FNCount, PropertyTokens));
443 void ContactDataObject::ProcessN(wxString PropertySeg1, wxString PropertySeg2){
445 std::map<int, int> SplitPoints;
446 std::map<int, int> SplitLength;
448 int intPrevValue = 3;
452 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
456 wxString PropertyName;
457 wxString PropertyValue;
458 wxString PropertyData;
459 wxString PropertyTokens;
460 std::map<int,int>::iterator SLiter;
461 bool FirstToken = TRUE;
463 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
464 intiter != SplitPoints.end(); ++intiter){
466 SLiter = SplitLength.find(intiter->first);
468 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
470 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
471 PropertyName = PropertyElement.GetNextToken();
472 PropertyValue = PropertyElement.GetNextToken();
474 intPrevValue = intiter->second;
476 CaptureString(&PropertyValue, FALSE);
478 if (PropertyName == wxT("ALTID")){
480 NameAltID = PropertyValue;
482 } else if (PropertyName == wxT("LANGUAGE")){
484 NameLanguage = PropertyValue;
486 } else if (PropertyName == wxT("SORT-AS")){
488 if (PropertyValue.Left(1) == wxT("\"") && PropertyValue.Right(1) == wxT("\"") &&
489 PropertyValue.Len() >= 3){
490 NameDisplayAs = PropertyValue.Mid(1, (PropertyValue.Len() - 2));
493 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
495 if (FirstToken == TRUE){
497 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
502 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
510 // Split the name data.
512 int intSplitSeek = 0;
513 int intSplitsFound = 0;
514 int intSplitSize = 0;
515 int intPropertyLen = PropertySeg2.Len();
517 std::map<int,wxString> NameValues;
520 for (int i = 0; i <= intPropertyLen; i++){
522 if (PropertySeg2.Mid(i, 1) == wxT(";") && PropertySeg2.Mid((i - 1), 1) != wxT("\\")){
524 NameValues.insert(std::make_pair(++intSplitsFound, PropertySeg2.Mid(intSplitSeek, intSplitSize)));
529 if (intSplitsFound == 4){
531 NameValues.insert(std::make_pair(++intSplitsFound, PropertySeg2.Mid(intSplitSeek, wxString::npos)));
545 // Split the data into several parts.
547 for (std::map<int, wxString>::iterator iter = NameValues.begin();
548 iter != NameValues.end(); ++iter){
550 if (iter->first == 1){
552 // Deal with family name.
554 NameSurname = iter->second;
556 } else if (iter->first == 2){
558 // Deal with given names.
560 NameForename = iter->second;
562 } else if (iter->first == 3){
564 // Deal with additional names.
566 NameOtherNames = iter->second;
568 } else if (iter->first == 4){
570 // Deal with honorifix prefixes and suffixes.
572 NameTitle = iter->second;
576 if (iter == NameValues.end()){
582 NameSuffix = iter->second;
588 // Add the name token data.
590 if (!PropertyTokens.IsEmpty()){
592 NameTokens = PropertyTokens;
598 void ContactDataObject::ProcessNickname(wxString PropertySeg1, wxString PropertySeg2, int *NicknameCount){
600 std::map<int, int> SplitPoints;
601 std::map<int, int> SplitLength;
603 int intPrevValue = 10;
606 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
610 PropertyType PropType;
612 // Look for type before continuing.
614 CheckType(&PropertySeg1, &SplitPoints, &SplitLength, &intPrevValue, &PropType);
618 std::map<int, wxString> *NicknamesList = NULL;
619 std::map<int, wxString> *NicknamesListType = NULL;
620 std::map<int, wxString> *NicknamesListLanguage = NULL;
621 std::map<int, wxString> *NicknamesListAltID = NULL;
622 std::map<int, wxString> *NicknamesListPID = NULL;
623 std::map<int, wxString> *NicknamesListTokens = NULL;
624 std::map<int, int> *NicknamesListPref = NULL;
628 NicknamesList = &GeneralNicknamesList;
629 NicknamesListType = &GeneralNicknamesListType;
630 NicknamesListLanguage = &GeneralNicknamesListLanguage;
631 NicknamesListAltID = &GeneralNicknamesListAltID;
632 NicknamesListPID = &GeneralNicknamesListPID;
633 NicknamesListTokens = &GeneralNicknamesListTokens;
634 NicknamesListPref = &GeneralNicknamesListPref;
637 NicknamesList = &HomeNicknamesList;
638 NicknamesListType = &HomeNicknamesListType;
639 NicknamesListLanguage = &HomeNicknamesListLanguage;
640 NicknamesListAltID = &HomeNicknamesListAltID;
641 NicknamesListPID = &HomeNicknamesListPID;
642 NicknamesListTokens = &HomeNicknamesListTokens;
643 NicknamesListPref = &HomeNicknamesListPref;
646 NicknamesList = &BusinessNicknamesList;
647 NicknamesListType = &BusinessNicknamesListType;
648 NicknamesListLanguage = &BusinessNicknamesListLanguage;
649 NicknamesListAltID = &BusinessNicknamesListAltID;
650 NicknamesListPID = &BusinessNicknamesListPID;
651 NicknamesListTokens = &BusinessNicknamesListTokens;
652 NicknamesListPref = &BusinessNicknamesListPref;
656 std::map<int, int>::iterator SLiter;
657 wxString PropertyData;
658 wxString PropertyName;
659 wxString PropertyValue;
660 wxString PropertyTokens;
661 bool FirstToken = TRUE;
663 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
664 intiter != SplitPoints.end(); ++intiter){
666 SLiter = SplitLength.find(intiter->first);
668 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
670 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
671 PropertyName = PropertyElement.GetNextToken();
672 PropertyValue = PropertyElement.GetNextToken();
674 intPrevValue = intiter->second;
676 CaptureString(&PropertyValue, FALSE);
678 if (PropertyName == wxT("ALTID")){
680 NicknamesListAltID->erase(*NicknameCount);
681 NicknamesListAltID->insert(std::make_pair(*NicknameCount, PropertyValue));
683 } else if (PropertyName == wxT("PID")){
685 NicknamesListPID->erase(*NicknameCount);
686 NicknamesListPID->insert(std::make_pair(*NicknameCount, PropertyValue));
688 } else if (PropertyName == wxT("PREF")){
690 int PriorityNumber = 0;
691 bool ValidNumber = TRUE;
694 PriorityNumber = std::stoi(PropertyValue.ToStdString());
697 catch(std::invalid_argument &e){
701 if (ValidNumber == TRUE){
703 NicknamesListPref->erase(*NicknameCount);
704 NicknamesListPref->insert(std::make_pair(*NicknameCount, PriorityNumber));
708 } else if (PropertyName == wxT("LANGUAGE")){
710 NicknamesListLanguage->erase(*NicknameCount);
711 NicknamesListLanguage->insert(std::make_pair(*NicknameCount, PropertyValue));
715 // Something else we don't know about so append
716 // to the tokens variable.
718 if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty() && PropertyName != wxT("TYPE")){
720 if (FirstToken == TRUE){
722 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
727 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
737 NicknamesList->insert(std::make_pair(*NicknameCount, PropertySeg2));
739 // Add the name token data.
741 if (!PropertyTokens.IsEmpty()){
743 NicknamesListTokens->insert(std::make_pair(*NicknameCount, PropertyTokens));
749 void SplitValues(wxString *PropertyLine,
750 std::map<int,int> *SplitPoints,
751 std::map<int,int> *SplitLength,
754 size_t intPropertyLen = PropertyLine->Len();
755 int intSplitsFound = 0;
756 int intSplitSize = 0;
757 int intSplitSeek = 0;
759 for (int i = intSize; i <= intPropertyLen; i++){
763 if (PropertyLine->Mid(i, 1) == wxT(";") &&
764 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
766 if (intSplitsFound == 0){
768 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
772 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
776 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
786 if (intSplitsFound == 0){
788 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
789 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
793 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
794 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
800 void CheckType(wxString *PropertySeg1,
801 std::map<int,int> *SplitPoints,
802 std::map<int,int> *SplitLength,
804 PropertyType *PropType){
806 wxString PropertyData;
807 wxString PropertyName;
808 wxString PropertyValue;
809 std::map<int,int>::iterator SLiter;
811 for (std::map<int, int>::iterator intiter = SplitPoints->begin();
812 intiter != SplitPoints->end(); ++intiter){
814 SLiter = SplitLength->find(intiter->first);
816 PropertyData = PropertySeg1->Mid(*intPrevValue, (SLiter->second));
818 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
819 PropertyName = PropertyElement.GetNextToken();
820 PropertyValue = PropertyElement.GetNextToken();
822 *intPrevValue = intiter->second;
824 if (PropertyName == wxT("TYPE")){
826 if (PropertyValue == wxT("work")){
828 *PropType = PROPERTY_WORK;
830 } else if (PropertyValue == wxT("home")){
832 *PropType = PROPERTY_HOME;
836 *PropType = PROPERTY_NONE;