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 int ContactLineLen = 0;
77 int QuoteBreakPoint = 0;
79 wxString PropertyLine;
80 wxString PropertySeg1;
81 wxString PropertySeg2;
82 wxString PropertyNextLine;
85 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
86 iter != ContactFileLines.end(); ++iter){
99 ContactLine = iter->second;
101 while (ExtraLineSeek == TRUE){
103 // Check if there is extra data on the next line
104 // (indicated by space or tab at the start) and add data.
108 if (iter == ContactFileLines.end()){
115 PropertyNextLine = iter->second;
117 if (PropertyNextLine.Mid(0, 1) == wxT(" ") || PropertyNextLine.Mid(0, 1) == wxT("\t")){
119 PropertyNextLine.Remove(0, 1);
120 ContactLine.Append(PropertyNextLine);
125 ExtraLineSeek = FALSE;
131 ContactLineLen = ContactLine.Len();
133 // Make sure we are not in quotation mode.
134 // Make sure colon does not have \ or \\ before it.
136 for (int i = 0; i <= ContactLineLen; i++){
138 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
140 PropertyFind = FALSE;
142 } else if (PropertyFind == TRUE){
144 Property.Append(ContactLine.Mid(i, 1));
148 if (ContactLine.Mid(i, 1) == wxT("\"")){
150 if (QuoteMode == TRUE){
162 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
171 // Split that line at the point into two variables (ignore the colon).
173 PropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
174 PropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
180 return CONTACTLOAD_OK;