Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added source code, headers and unit test to check that a file has loaded OK in Contac...
[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         int ContactLineLen = 0;
77         int QuoteBreakPoint = 0;
78         wxString ContactLine;
79         wxString PropertyLine;
80         wxString PropertySeg1;
81         wxString PropertySeg2;
82         wxString PropertyNextLine;
83         wxString Property;
84         
85         for (std::map<int,wxString>::iterator iter = ContactFileLines.begin(); 
86          iter != ContactFileLines.end(); ++iter){
88                 ExtraLineSeek = TRUE;
89                 QuoteMode = FALSE;
90                 PropertyFind = TRUE;
91                 ContactLineLen = 0;
92                 QuoteBreakPoint = 0;
93                 ContactLine.Clear();
94                 PropertyLine.Clear();
95                 PropertySeg1.Clear();
96                 PropertySeg2.Clear();
97                 Property.Clear();
98          
99                 ContactLine = iter->second;
100                 
101                 while (ExtraLineSeek == TRUE){
102                 
103                         // Check if there is extra data on the next line 
104                         // (indicated by space or tab at the start) and add data.
105                 
106                         iter++;
107                         
108                         if (iter == ContactFileLines.end()){
109                         
110                                 iter--;
111                                 break;
112                         
113                         }                       
114                 
115                         PropertyNextLine = iter->second;
116                 
117                         if (PropertyNextLine.Mid(0, 1) == wxT(" ") || PropertyNextLine.Mid(0, 1) == wxT("\t")){
118                 
119                                 PropertyNextLine.Remove(0, 1);
120                                 ContactLine.Append(PropertyNextLine);
121                 
122                         } else {
123                         
124                                 iter--;
125                                 ExtraLineSeek = FALSE;
126                         
127                         }
128                 
129                 }
131                 ContactLineLen = ContactLine.Len();
132                 
133                 // Make sure we are not in quotation mode.
134                 // Make sure colon does not have \ or \\ before it.
135                 
136                 for (int i = 0; i <= ContactLineLen; i++){
137                 
138                         if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
139                         
140                                 PropertyFind = FALSE;
141                         
142                         } else if (PropertyFind == TRUE){
143                         
144                                 Property.Append(ContactLine.Mid(i, 1));
145                         
146                         }               
147                 
148                         if (ContactLine.Mid(i, 1) == wxT("\"")){
149                         
150                                 if (QuoteMode == TRUE){
151                                 
152                                         QuoteMode = FALSE;
153                                 
154                                 } else {
155                         
156                                         QuoteMode = TRUE;
157                                         
158                                 }
159                         
160                         }
161                         
162                         if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
163                         
164                                 QuoteBreakPoint = i;
165                                 break;
166                         
167                         }
168                 
169                 }
170                 
171                 // Split that line at the point into two variables (ignore the colon).
172                 
173                 PropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
174                 PropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
175                 
176                 
177          
178         }
179         
180         return CONTACTLOAD_OK;
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