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 int ContactLineLen = 0;
78 int QuoteBreakPoint = 0;
82 wxString PropertyLine;
83 wxString PropertySeg1;
84 wxString PropertySeg2;
85 wxString PropertyNextLine;
88 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
89 iter != ContactFileLines.end(); ++iter){
102 ContactLine = iter->second;
104 while (ExtraLineSeek == TRUE){
106 // Check if there is extra data on the next line
107 // (indicated by space or tab at the start) and add data.
111 if (iter == ContactFileLines.end()){
118 PropertyNextLine = iter->second;
120 if (PropertyNextLine.Mid(0, 1) == wxT(" ") || PropertyNextLine.Mid(0, 1) == wxT("\t")){
122 PropertyNextLine.Remove(0, 1);
123 ContactLine.Append(PropertyNextLine);
128 ExtraLineSeek = FALSE;
134 ContactLineLen = ContactLine.Len();
136 // Make sure we are not in quotation mode.
137 // Make sure colon does not have \ or \\ before it.
139 for (int i = 0; i <= ContactLineLen; i++){
141 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
143 PropertyFind = FALSE;
145 } else if (PropertyFind == TRUE){
147 Property.Append(ContactLine.Mid(i, 1));
151 if (ContactLine.Mid(i, 1) == wxT("\"")){
153 if (QuoteMode == TRUE){
165 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
174 // Split that line at the point into two variables (ignore the colon).
176 PropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
177 PropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
179 if (Property == wxT("KIND") && KindProcessed == FALSE){
181 // See frmContactEditor-LoadGroup.cpp
183 ProcessKind(PropertySeg2);
185 } else if (Property == wxT("MEMBER")){
187 // See frmContactEditor-LoadGroup.cpp
189 ProcessMember(PropertySeg1, PropertySeg2, &GroupCount);
192 } else if (Property == wxT("FN")){
194 ProcessFN(PropertySeg1, PropertySeg2, &FNCount);
201 return CONTACTLOAD_OK;
205 void ContactDataObject::ProcessKind(wxString KindType){
207 if (KindType == wxT("individual")){
209 ContactKind = CONTACTKIND_INDIVIDUAL;
211 } else if (KindType == wxT("group")){
213 ContactKind = CONTACTKIND_GROUP;
215 } else if (KindType == wxT("org")){
217 ContactKind = CONTACTKIND_ORGANISATION;
219 } else if (KindType == wxT("location")){
221 ContactKind = CONTACTKIND_LOCATION;
225 ContactKind = CONTACTKIND_NONE;
230 void ContactDataObject::ProcessMember(wxString PropertySeg1, wxString PropertySeg2, int *GroupCount){
232 std::map<int, int> SplitPoints;
233 std::map<int, int> SplitLength;
235 int intPrevValue = 8;
239 SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
243 // Look for type before continuing.
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 //ContactProcess::ContactProcessCaptureStrings(&PropertyValue);
269 if (PropertyName == wxT("ALTID")){
271 GroupsListAltID.erase(*GroupCount);
272 GroupsListAltID.insert(std::make_pair(*GroupCount, PropertyValue));
274 } else if (PropertyName == wxT("PID")){
276 GroupsListPID.erase(*GroupCount);
277 GroupsListPID.insert(std::make_pair(*GroupCount, PropertyValue));
279 } else if (PropertyName == wxT("PREF")){
281 int PriorityNumber = 0;
282 bool ValidNumber = TRUE;
285 PriorityNumber = std::stoi(PropertyValue.ToStdString());
288 catch(std::invalid_argument &e){
292 if (ValidNumber == TRUE){
294 GroupsListPref.erase(*GroupCount);
295 GroupsListPref.insert(std::make_pair(*GroupCount, PriorityNumber));
299 } else if (PropertyName == wxT("MEDIATYPE")){
301 GroupsListMediaType.erase(*GroupCount);
302 GroupsListMediaType.insert(std::make_pair(*GroupCount, PropertyValue));
304 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
306 if (FirstToken == TRUE){
308 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
313 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
321 GroupsList.insert(std::make_pair(*GroupCount, PropertySeg2));
323 if (!PropertyTokens.IsEmpty()){
325 GroupsListTokens.insert(std::make_pair(*GroupCount, PropertyTokens));
332 void ContactDataObject::ProcessFN(wxString PropertySeg1, wxString PropertySeg2, int *FNCount){
334 FullNamesList.insert(std::make_pair(*FNCount, PropertySeg2));
338 void SplitValues(wxString *PropertyLine,
339 std::map<int,int> *SplitPoints,
340 std::map<int,int> *SplitLength,
343 size_t intPropertyLen = PropertyLine->Len();
344 int intSplitsFound = 0;
345 int intSplitSize = 0;
346 int intSplitSeek = 0;
348 for (int i = intSize; i <= intPropertyLen; i++){
352 if (PropertyLine->Mid(i, 1) == wxT(";") &&
353 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
355 if (intSplitsFound == 0){
357 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
361 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
365 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
375 if (intSplitsFound == 0){
377 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
378 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
382 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
383 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));