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;
83 wxString PropertyLine;
84 wxString PropertySeg1;
85 wxString PropertySeg2;
86 wxString PropertyNextLine;
89 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
90 iter != ContactFileLines.end(); ++iter){
100 PropertySeg2.Clear();
103 ContactLine = iter->second;
105 while (ExtraLineSeek == TRUE){
107 // Check if there is extra data on the next line
108 // (indicated by space or tab at the start) and add data.
112 if (iter == ContactFileLines.end()){
119 PropertyNextLine = iter->second;
121 if (PropertyNextLine.Mid(0, 1) == wxT(" ") || PropertyNextLine.Mid(0, 1) == wxT("\t")){
123 PropertyNextLine.Remove(0, 1);
124 ContactLine.Append(PropertyNextLine);
129 ExtraLineSeek = FALSE;
135 ContactLineLen = ContactLine.Len();
137 // Make sure we are not in quotation mode.
138 // Make sure colon does not have \ or \\ before it.
140 for (int i = 0; i <= ContactLineLen; i++){
142 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
144 PropertyFind = FALSE;
146 } else if (PropertyFind == TRUE){
148 Property.Append(ContactLine.Mid(i, 1));
152 if (ContactLine.Mid(i, 1) == wxT("\"")){
154 if (QuoteMode == TRUE){
166 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
175 // Split that line at the point into two variables (ignore the colon).
177 PropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
178 PropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
180 if (Property == wxT("KIND") && KindProcessed == FALSE){
182 ProcessKind(PropertySeg2);
184 } else if (Property == wxT("MEMBER")){
186 ProcessMember(PropertySeg1, PropertySeg2, &GroupCount);
189 } else if (Property == wxT("FN")){
191 ProcessFN(PropertySeg1, PropertySeg2, &FNCount);
194 } else if (Property == wxT("N") && NameProcessed == FALSE){
196 ProcessN(PropertySeg1, PropertySeg2);
197 NameProcessed = TRUE;
203 return CONTACTLOAD_OK;
207 void ContactDataObject::ProcessKind(wxString KindType){
209 if (KindType == wxT("individual")){
211 ContactKind = CONTACTKIND_INDIVIDUAL;
213 } else if (KindType == wxT("group")){
215 ContactKind = CONTACTKIND_GROUP;
217 } else if (KindType == wxT("org")){
219 ContactKind = CONTACTKIND_ORGANISATION;
221 } else if (KindType == wxT("location")){
223 ContactKind = CONTACTKIND_LOCATION;
227 ContactKind = CONTACTKIND_NONE;
232 void ContactDataObject::ProcessMember(wxString PropertySeg1, wxString PropertySeg2, int *GroupCount){
234 std::map<int, int> SplitPoints;
235 std::map<int, int> SplitLength;
237 int intPrevValue = 8;
241 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
245 wxString PropertyName;
246 wxString PropertyValue;
247 wxString PropertyData;
248 wxString PropertyTokens;
249 std::map<int,int>::iterator SLiter;
250 bool FirstToken = TRUE;
252 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
253 intiter != SplitPoints.end(); ++intiter){
255 SLiter = SplitLength.find(intiter->first);
257 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
259 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
260 PropertyName = PropertyElement.GetNextToken();
261 PropertyValue = PropertyElement.GetNextToken();
263 intPrevValue = intiter->second;
265 CaptureString(&PropertyValue, FALSE);
267 if (PropertyName == wxT("ALTID")){
269 GroupsListAltID.erase(*GroupCount);
270 GroupsListAltID.insert(std::make_pair(*GroupCount, PropertyValue));
272 } else if (PropertyName == wxT("PID")){
274 GroupsListPID.erase(*GroupCount);
275 GroupsListPID.insert(std::make_pair(*GroupCount, PropertyValue));
277 } else if (PropertyName == wxT("PREF")){
279 int PriorityNumber = 0;
280 bool ValidNumber = TRUE;
283 PriorityNumber = std::stoi(PropertyValue.ToStdString());
286 catch(std::invalid_argument &e){
290 if (ValidNumber == TRUE){
292 GroupsListPref.erase(*GroupCount);
293 GroupsListPref.insert(std::make_pair(*GroupCount, PriorityNumber));
297 } else if (PropertyName == wxT("MEDIATYPE")){
299 GroupsListMediaType.erase(*GroupCount);
300 GroupsListMediaType.insert(std::make_pair(*GroupCount, PropertyValue));
302 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
304 if (FirstToken == TRUE){
306 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
311 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
319 GroupsList.insert(std::make_pair(*GroupCount, PropertySeg2));
321 if (!PropertyTokens.IsEmpty()){
323 GroupsListTokens.insert(std::make_pair(*GroupCount, PropertyTokens));
330 void ContactDataObject::ProcessFN(wxString PropertySeg1, wxString PropertySeg2, int *FNCount){
332 std::map<int, int> SplitPoints;
333 std::map<int, int> SplitLength;
335 int intPrevValue = 4;
339 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
343 wxString PropertyName;
344 wxString PropertyValue;
345 wxString PropertyData;
346 wxString PropertyTokens;
347 std::map<int,int>::iterator SLiter;
348 bool FirstToken = TRUE;
350 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
351 intiter != SplitPoints.end(); ++intiter){
353 SLiter = SplitLength.find(intiter->first);
355 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
357 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
358 PropertyName = PropertyElement.GetNextToken();
359 PropertyValue = PropertyElement.GetNextToken();
361 intPrevValue = intiter->second;
363 CaptureString(&PropertyValue, FALSE);
365 if (PropertyName == wxT("TYPE")){
367 if (!PropertyValue.IsEmpty() || PropertyValue == wxT("home") ||
368 PropertyValue == wxT("work") ){
370 FullNamesListType.erase(*FNCount);
371 FullNamesListType.insert(std::make_pair(*FNCount, PropertyValue));
375 } else if (PropertyName == wxT("LANGUAGE")){
377 FullNamesListLanguage.erase(*FNCount);
378 FullNamesListLanguage.insert(std::make_pair(*FNCount, PropertyValue));
380 } else if (PropertyName == wxT("ALTID")){
382 FullNamesListAltID.erase(*FNCount);
383 FullNamesListAltID.insert(std::make_pair(*FNCount, PropertyValue));
385 } else if (PropertyName == wxT("PID")){
387 FullNamesListPID.erase(*FNCount);
388 FullNamesListPID.insert(std::make_pair(*FNCount, PropertyValue));
390 } else if (PropertyName == wxT("PREF")){
392 int PriorityNumber = 0;
393 bool ValidNumber = TRUE;
396 PriorityNumber = std::stoi(PropertyValue.ToStdString());
399 catch(std::invalid_argument &e){
403 if (ValidNumber == TRUE){
405 FullNamesListPref.erase(*FNCount);
406 FullNamesListPref.insert(std::make_pair(*FNCount, PriorityNumber));
410 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
412 if (FirstToken == TRUE){
414 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
419 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
427 FullNamesList.insert(std::make_pair(*FNCount, PropertySeg2));
429 if (!PropertyTokens.IsEmpty()){
431 FullNamesListTokens.insert(std::make_pair(*FNCount, PropertyTokens));
437 void ContactDataObject::ProcessN(wxString PropertySeg1, wxString PropertySeg2){
439 std::map<int, int> SplitPoints;
440 std::map<int, int> SplitLength;
442 int intPrevValue = 3;
446 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
450 NameForename = PropertySeg2;
452 wxString PropertyName;
453 wxString PropertyValue;
454 wxString PropertyData;
455 wxString PropertyTokens;
456 std::map<int,int>::iterator SLiter;
457 bool FirstToken = TRUE;
459 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
460 intiter != SplitPoints.end(); ++intiter){
462 SLiter = SplitLength.find(intiter->first);
464 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
466 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
467 PropertyName = PropertyElement.GetNextToken();
468 PropertyValue = PropertyElement.GetNextToken();
470 intPrevValue = intiter->second;
472 CaptureString(&PropertyValue, FALSE);
474 if (PropertyName == wxT("ALTID")){
476 NameAltID = PropertyValue;
478 } else if (PropertyName == wxT("LANGUAGE")){
480 NameLanguage = PropertyValue;
482 } else if (PropertyName == wxT("SORT-AS")){
484 if (PropertyValue.Left(1) == wxT("\"") && PropertyValue.Right(1) == wxT("\"") &&
485 PropertyValue.Len() >= 3){
486 NameDisplayAs = PropertyValue.Mid(1, (PropertyValue.Len() - 2));
489 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
491 if (FirstToken == TRUE){
493 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
498 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
506 // Split the name data.
508 int intSplitSeek = 0;
509 int intSplitsFound = 0;
510 int intSplitSize = 0;
511 int intPropertyLen = PropertySeg2.Len();
513 std::map<int,wxString> NameValues;
516 for (int i = 0; i <= intPropertyLen; i++){
518 if (PropertySeg2.Mid(i, 1) == wxT(";") && PropertySeg2.Mid((i - 1), 1) != wxT("\\")){
520 NameValues.insert(std::make_pair(++intSplitsFound, PropertySeg2.Mid(intSplitSeek, intSplitSize)));
525 if (intSplitsFound == 4){
527 NameValues.insert(std::make_pair(++intSplitsFound, PropertySeg2.Mid(intSplitSeek, wxString::npos)));
541 // Split the data into several parts.
543 for (std::map<int, wxString>::iterator iter = NameValues.begin();
544 iter != NameValues.end(); ++iter){
546 if (iter->first == 1){
548 // Deal with family name.
550 NameSurname = iter->second;
552 } else if (iter->first == 2){
554 // Deal with given names.
556 NameForename = iter->second;
558 } else if (iter->first == 3){
560 // Deal with additional names.
562 NameOtherNames = iter->second;
564 } else if (iter->first == 4){
566 // Deal with honorifix prefixes and suffixes.
568 NameTitle = iter->second;
572 if (iter == NameValues.end()){
578 NameSuffix = iter->second;
584 // Add the name token data.
586 if (!PropertyTokens.IsEmpty()){
588 NameTokens = PropertyTokens;
594 void SplitValues(wxString *PropertyLine,
595 std::map<int,int> *SplitPoints,
596 std::map<int,int> *SplitLength,
599 size_t intPropertyLen = PropertyLine->Len();
600 int intSplitsFound = 0;
601 int intSplitSize = 0;
602 int intSplitSeek = 0;
604 for (int i = intSize; i <= intPropertyLen; i++){
608 if (PropertyLine->Mid(i, 1) == wxT(";") &&
609 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
611 if (intSplitsFound == 0){
613 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
617 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
621 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
631 if (intSplitsFound == 0){
633 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
634 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
638 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
639 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));