Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added unit tests for the ALTID, PID & PREF properties for the MEMBER vCard property.
[xestiaab/.git] / source / contacteditor / ContactDataObject.cpp
1 // ContactDataObject.cpp - Client Data Object.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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){
22         
23         if (!wxFileExists(Filename)){
24         
25                 return CONTACTLOAD_FILEMISSING;
26         
27         }
28         
29         wxFile ContactFile;
30         
31         if (!ContactFile.Open(Filename, wxFile::read, wxS_DEFAULT)){
32         
33                 return CONTACTLOAD_FILEERROR;
34         
35         }
36         
37         // Check that the vCard is a valid vCard 4.0 file.
39         vCard vCard4FormatCheck;
40         
41         vCard4FormatCheck.LoadFile(Filename);
42         
43         if (vCard4FormatCheck.Get("VERSION") != wxT("4.0")){
44         
45                 return CONTACTLOAD_FILEINVALIDFORMAT;
46         
47         }
49         // Check that the vCard meets the base specification.
50         
51         if (!vCard4FormatCheck.MeetBaseSpecification()){
52         
53                 return CONTACTLOAD_FILEBASESPECFAIL;
54         
55         }
56         
57         wxStringTokenizer wSTContactFileLines(vCard4FormatCheck.WriteString(), wxT("\r\n"));
58         
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));
67                 ContactLineSeek++;              
68         
69         }
70         
71         wxString wxSPropertyNextLine;
72         
73         bool ExtraLineSeek = TRUE;
74         bool QuoteMode = FALSE;
75         bool PropertyFind = TRUE;
76         bool KindProcessed = FALSE;
77         int ContactLineLen = 0;
78         int QuoteBreakPoint = 0;
79         int GroupCount = 0;
80         wxString ContactLine;
81         wxString PropertyLine;
82         wxString PropertySeg1;
83         wxString PropertySeg2;
84         wxString PropertyNextLine;
85         wxString Property;
86         
87         for (std::map<int,wxString>::iterator iter = ContactFileLines.begin(); 
88          iter != ContactFileLines.end(); ++iter){
90                 ExtraLineSeek = TRUE;
91                 QuoteMode = FALSE;
92                 PropertyFind = TRUE;
93                 ContactLineLen = 0;
94                 QuoteBreakPoint = 0;
95                 ContactLine.Clear();
96                 PropertyLine.Clear();
97                 PropertySeg1.Clear();
98                 PropertySeg2.Clear();
99                 Property.Clear();
100          
101                 ContactLine = iter->second;
102                 
103                 while (ExtraLineSeek == TRUE){
104                 
105                         // Check if there is extra data on the next line 
106                         // (indicated by space or tab at the start) and add data.
107                 
108                         iter++;
109                         
110                         if (iter == ContactFileLines.end()){
111                         
112                                 iter--;
113                                 break;
114                         
115                         }                       
116                 
117                         PropertyNextLine = iter->second;
118                 
119                         if (PropertyNextLine.Mid(0, 1) == wxT(" ") || PropertyNextLine.Mid(0, 1) == wxT("\t")){
120                 
121                                 PropertyNextLine.Remove(0, 1);
122                                 ContactLine.Append(PropertyNextLine);
123                 
124                         } else {
125                         
126                                 iter--;
127                                 ExtraLineSeek = FALSE;
128                         
129                         }
130                 
131                 }
133                 ContactLineLen = ContactLine.Len();
134                 
135                 // Make sure we are not in quotation mode.
136                 // Make sure colon does not have \ or \\ before it.
137                 
138                 for (int i = 0; i <= ContactLineLen; i++){
139                 
140                         if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
141                         
142                                 PropertyFind = FALSE;
143                         
144                         } else if (PropertyFind == TRUE){
145                         
146                                 Property.Append(ContactLine.Mid(i, 1));
147                         
148                         }               
149                 
150                         if (ContactLine.Mid(i, 1) == wxT("\"")){
151                         
152                                 if (QuoteMode == TRUE){
153                                 
154                                         QuoteMode = FALSE;
155                                 
156                                 } else {
157                         
158                                         QuoteMode = TRUE;
159                                         
160                                 }
161                         
162                         }
163                         
164                         if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
165                         
166                                 QuoteBreakPoint = i;
167                                 break;
168                         
169                         }
170                 
171                 }
172                 
173                 // Split that line at the point into two variables (ignore the colon).
174                 
175                 PropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
176                 PropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
177                 
178                  if (Property == wxT("KIND") && KindProcessed == FALSE){
179                 
180                         // See frmContactEditor-LoadGroup.cpp
181                 
182                         ProcessKind(PropertySeg2);
183                 
184                 } else if (Property == wxT("MEMBER")){
186                         // See frmContactEditor-LoadGroup.cpp
188                         ProcessMember(PropertySeg1, PropertySeg2, &GroupCount);
189                         GroupCount++;   
190                 
191                 }
192                 
193         }
194         
195         return CONTACTLOAD_OK;
199 void ContactDataObject::ProcessKind(wxString KindType){
201         if (KindType == wxT("individual")){
202                         
203                 ContactKind = CONTACTKIND_INDIVIDUAL;
204                         
205         } else if (KindType == wxT("group")){
206                         
207                 ContactKind = CONTACTKIND_GROUP;
208                         
209         } else if (KindType == wxT("org")){
210                         
211                 ContactKind = CONTACTKIND_ORGANISATION;
212                         
213         } else if (KindType == wxT("location")){
214                         
215                 ContactKind = CONTACTKIND_LOCATION;
216                         
217         } else {
218                         
219                 ContactKind = CONTACTKIND_NONE;                 
220         }
224 void ContactDataObject::ProcessMember(wxString PropertySeg1, wxString PropertySeg2, int *GroupCount){
226         std::map<int, int> SplitPoints;
227         std::map<int, int> SplitLength;
229         int intPrevValue = 8;
230         int intPref = 0;                        
231         int intType = 0;
232         
233         SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
235         intPrevValue = 7;
236         
237         // Look for type before continuing.
238         
239         wxString PropertyName;
240         wxString PropertyValue;
241         wxString PropertyData;
242         wxString PropertyTokens;
243         std::map<int,int>::iterator SLiter;
244         bool FirstToken = TRUE;
245         
246         for (std::map<int, int>::iterator intiter = SplitPoints.begin(); 
247         intiter != SplitPoints.end(); ++intiter){
248         
249                 SLiter = SplitLength.find(intiter->first);
250         
251                 PropertyData = PropertySeg1.Mid(intPrevValue, (SLiter->second));
252                 
253                 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
254                 PropertyName = PropertyElement.GetNextToken();                          
255                 PropertyValue = PropertyElement.GetNextToken();
256                 
257                 intPrevValue = intiter->second;
258                 
259                 CaptureString(&PropertyValue, FALSE);
260                 
261                 //ContactProcess::ContactProcessCaptureStrings(&PropertyValue);
262         
263                 if (PropertyName == wxT("ALTID")){
265                         GroupsListAltID.erase(*GroupCount);
266                         GroupsListAltID.insert(std::make_pair(*GroupCount, PropertyValue));
267                 
268                 } else if (PropertyName == wxT("PID")){
270                         GroupsListPID.erase(*GroupCount);
271                         GroupsListPID.insert(std::make_pair(*GroupCount, PropertyValue));
272                 
273                 } else if (PropertyName == wxT("PREF")){
275                         int PriorityNumber = 0;
276                         bool ValidNumber = TRUE;
277                         
278                         try{
279                                 PriorityNumber = std::stoi(PropertyValue.ToStdString());
280                         }
281                         
282                         catch(std::invalid_argument &e){
283                                 ValidNumber = FALSE;
284                         }
286                         if (ValidNumber == TRUE){
288                                 GroupsListPref.erase(*GroupCount);
289                                 GroupsListPref.insert(std::make_pair(*GroupCount, PriorityNumber));
290                 
291                         }
292                 
293                 } else if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty()){
294                         
295                         if (FirstToken == TRUE){
296                                 
297                                 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
298                                 FirstToken = FALSE;
299                                 
300                         } else {
301                         
302                                 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
303                                 
304                         }
305                         
306                 }
307                 
308         }
310         //SplitValues(&PropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
312         GroupsList.insert(std::make_pair(*GroupCount, PropertySeg2));
317 void SplitValues(wxString *PropertyLine, 
318         std::map<int,int> *SplitPoints, 
319         std::map<int,int> *SplitLength, 
320         int intSize){
321         
322         size_t intPropertyLen = PropertyLine->Len();
323         int intSplitsFound = 0;
324         int intSplitSize = 0;
325         int intSplitSeek = 0;
326         
327         for (int i = intSize; i <= intPropertyLen; i++){
329                 intSplitSize++;
330         
331                 if (PropertyLine->Mid(i, 1) == wxT(";") &&
332                     PropertyLine->Mid((i - 1), 1) != wxT("\\")){
333            
334                     if (intSplitsFound == 0){
335             
336                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
337           
338                     } else {
339            
340                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
341             
342                     }
343             
344                     SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
345             
346                     intSplitsFound++;
347                     intSplitSeek = i;
348                     intSplitSize = 0;
349             
350                 }
352         }
354         if (intSplitsFound == 0){
356                 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
357                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
359         } else {
361                 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
362                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
364         }
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy