1 // frmContactEditor-LoadAddress.cpp - frmContactEditor load address subroutines.
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 "frmContactEditor.h"
21 void frmContactEditor::LoadADR(wxString wxSPropertySeg1, wxString wxSPropertySeg2, int *ADRCount){
23 size_t intPropertyLen = wxSPropertySeg1.Len();
24 std::map<int, int> SplitPoints;
25 std::map<int, int> SplitLength;
26 std::map<int, int>::iterator SLiter;
27 wxString PropertyData;
28 wxString PropertyName;
29 wxString PropertyValue;
30 wxString PropertyTokens;
31 wxString AddressLabel;
33 wxString AddressAltID;
35 wxString AddressTokens;
37 wxString AddressTimezone;
39 wxString AddressMediatype;
40 wxString AddressPOBox;
41 wxString AddressExtended;
42 wxString AddressStreet;
43 wxString AddressLocality;
45 wxString AddressRegion;
46 wxString AddressPostalCode;
47 wxString AddressCountry;
48 bool FirstToken = TRUE;
49 int intSplitsFound = 0;
56 SplitValues(&wxSPropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
60 // Look for type before continuing.
62 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
63 intiter != SplitPoints.end(); ++intiter){
65 SLiter = SplitLength.find(intiter->first);
67 PropertyData = wxSPropertySeg1.Mid(intPrevValue, (SLiter->second));
69 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
70 PropertyName = PropertyElement.GetNextToken();
71 PropertyValue = PropertyElement.GetNextToken();
73 intPrevValue = intiter->second;
75 if (PropertyName == wxT("TYPE")){
77 if (PropertyValue == wxT("work")){
81 } else if (PropertyValue == wxT("home")){
95 // Setup blank lines for later on.
99 GeneralAddressList.insert(std::make_pair(intValueSeek, wxT("")));
100 GeneralAddressListTown.insert(std::make_pair(intValueSeek, wxT("")));
101 GeneralAddressListCounty.insert(std::make_pair(intValueSeek, wxT("")));
102 GeneralAddressListPostCode.insert(std::make_pair(intValueSeek, wxT("")));
103 GeneralAddressListCountry.insert(std::make_pair(intValueSeek, wxT("")));
104 GeneralAddressListLabel.insert(std::make_pair(intValueSeek, wxT("")));
105 GeneralAddressListLang.insert(std::make_pair(intValueSeek, wxT("")));
106 GeneralAddressListAltID.insert(std::make_pair(intValueSeek, wxT("")));
107 GeneralAddressListPID.insert(std::make_pair(intValueSeek, wxT("")));
108 GeneralAddressListGeo.insert(std::make_pair(intValueSeek, wxT("")));
109 GeneralAddressListTimezone.insert(std::make_pair(intValueSeek, wxT("")));
110 GeneralAddressListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
111 GeneralAddressListPref.insert(std::make_pair(intValueSeek, 0));
112 GeneralAddressListTokens.insert(std::make_pair(intValueSeek, wxT("")));
114 } else if (intType == 1){
116 HomeAddressList.insert(std::make_pair(intValueSeek, wxT("")));
117 HomeAddressListTown.insert(std::make_pair(intValueSeek, wxT("")));
118 HomeAddressListCounty.insert(std::make_pair(intValueSeek, wxT("")));
119 HomeAddressListPostCode.insert(std::make_pair(intValueSeek, wxT("")));
120 HomeAddressListCountry.insert(std::make_pair(intValueSeek, wxT("")));
121 HomeAddressListLabel.insert(std::make_pair(intValueSeek, wxT("")));
122 HomeAddressListLang.insert(std::make_pair(intValueSeek, wxT("")));
123 HomeAddressListAltID.insert(std::make_pair(intValueSeek, wxT("")));
124 HomeAddressListPID.insert(std::make_pair(intValueSeek, wxT("")));
125 HomeAddressListGeo.insert(std::make_pair(intValueSeek, wxT("")));
126 HomeAddressListTimezone.insert(std::make_pair(intValueSeek, wxT("")));
127 HomeAddressListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
128 HomeAddressListPref.insert(std::make_pair(intValueSeek, 0));
129 HomeAddressListTokens.insert(std::make_pair(intValueSeek, wxT("")));
131 } else if (intType == 2){
133 BusinessAddressList.insert(std::make_pair(intValueSeek, wxT("")));
134 BusinessAddressListTown.insert(std::make_pair(intValueSeek, wxT("")));
135 BusinessAddressListCounty.insert(std::make_pair(intValueSeek, wxT("")));
136 BusinessAddressListPostCode.insert(std::make_pair(intValueSeek, wxT("")));
137 BusinessAddressListCountry.insert(std::make_pair(intValueSeek, wxT("")));
138 BusinessAddressListLabel.insert(std::make_pair(intValueSeek, wxT("")));
139 BusinessAddressListLang.insert(std::make_pair(intValueSeek, wxT("")));
140 BusinessAddressListAltID.insert(std::make_pair(intValueSeek, wxT("")));
141 BusinessAddressListPID.insert(std::make_pair(intValueSeek, wxT("")));
142 BusinessAddressListGeo.insert(std::make_pair(intValueSeek, wxT("")));
143 BusinessAddressListTimezone.insert(std::make_pair(intValueSeek, wxT("")));
144 BusinessAddressListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
145 BusinessAddressListPref.insert(std::make_pair(intValueSeek, 0));
146 BusinessAddressListTokens.insert(std::make_pair(intValueSeek, wxT("")));
152 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
153 intiter != SplitPoints.end(); ++intiter){
155 SLiter = SplitLength.find(intiter->first);
157 PropertyData = wxSPropertySeg1.Mid(intPrevValue, (SLiter->second));
159 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
160 PropertyName = PropertyElement.GetNextToken();
161 PropertyValue = PropertyElement.GetNextToken();
163 intPrevValue = intiter->second;
165 ProcessCaptureStrings(&PropertyValue);
167 // Process properties.
169 if (PropertyName == wxT("LABEL")){
171 if (intType == 0){ GeneralAddressListLabel.erase(intValueSeek); GeneralAddressListLabel.insert(std::make_pair(intValueSeek, PropertyValue)); }
172 else if (intType == 1){ HomeAddressListLabel.erase(intValueSeek); HomeAddressListLabel.insert(std::make_pair(intValueSeek, PropertyValue)); }
173 else if (intType == 2){ BusinessAddressListLabel.erase(intValueSeek); BusinessAddressListLabel.insert(std::make_pair(intValueSeek, PropertyValue));}
175 } else if (PropertyName == wxT("LANGUAGE")){
177 if (intType == 0){ GeneralAddressListLang.erase(intValueSeek); GeneralAddressListLang.insert(std::make_pair(intValueSeek, PropertyValue)); }
178 else if (intType == 1){ HomeAddressListLang.erase(intValueSeek); HomeAddressListLang.insert(std::make_pair(intValueSeek, PropertyValue)); }
179 else if (intType == 2){ BusinessAddressListLang.erase(intValueSeek); BusinessAddressListLang.insert(std::make_pair(intValueSeek, PropertyValue)); }
181 } else if (PropertyName == wxT("ALTID")){
183 if (intType == 0){ GeneralAddressListAltID.erase(intValueSeek); GeneralAddressListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
184 else if (intType == 1){ HomeAddressListAltID.erase(intValueSeek); HomeAddressListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
185 else if (intType == 2){ BusinessAddressListAltID.erase(intValueSeek); BusinessAddressListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
187 } else if (PropertyName == wxT("PID")){
189 if (intType == 0){ GeneralAddressListPID.erase(intValueSeek); GeneralAddressListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
190 else if (intType == 1){ HomeAddressListPID.erase(intValueSeek); HomeAddressListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
191 else if (intType == 2){ BusinessAddressListPID.erase(intValueSeek); BusinessAddressListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
193 } else if (PropertyName == wxT("GEO")){
195 if (intType == 0){ GeneralAddressListGeo.erase(intValueSeek); GeneralAddressListGeo.insert(std::make_pair(intValueSeek, PropertyValue)); }
196 else if (intType == 1){ HomeAddressListGeo.erase(intValueSeek); HomeAddressListGeo.insert(std::make_pair(intValueSeek, PropertyValue)); }
197 else if (intType == 2){ BusinessAddressListGeo.erase(intValueSeek); BusinessAddressListGeo.insert(std::make_pair(intValueSeek, PropertyValue)); }
199 } else if (PropertyName == wxT("TZ")){
201 if (intType == 0){ GeneralAddressListTimezone.erase(intValueSeek); GeneralAddressListTimezone.insert(std::make_pair(intValueSeek, PropertyValue)); }
202 else if (intType == 1){ HomeAddressListTimezone.erase(intValueSeek); HomeAddressListTimezone.insert(std::make_pair(intValueSeek, PropertyValue)); }
203 else if (intType == 2){ BusinessAddressListTimezone.erase(intValueSeek); BusinessAddressListTimezone.insert(std::make_pair(intValueSeek, PropertyValue)); }
205 } else if (PropertyName == wxT("MEDIATYPE")){
207 if (intType == 0){ GeneralAddressListMediatype.erase(intValueSeek); GeneralAddressListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
208 else if (intType == 1){ HomeAddressListMediatype.erase(intValueSeek); HomeAddressListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
209 else if (intType == 2){ BusinessAddressListMediatype.erase(intValueSeek); BusinessAddressListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
211 } else if (PropertyName == wxT("PREF")){
213 intPref = wxAtoi(PropertyValue);
215 if (intPref > 0 && intPref < 101){
217 if (intType == 0){ GeneralAddressListPref.erase(intValueSeek); GeneralAddressListPref.insert(std::make_pair(intValueSeek, intPref)); }
218 else if (intType == 1){ HomeAddressListPref.erase(intValueSeek); HomeAddressListPref.insert(std::make_pair(intValueSeek, intPref)); }
219 else if (intType == 2){ BusinessAddressListPref.erase(intValueSeek); BusinessAddressListPref.insert(std::make_pair(intValueSeek, intPref)); }
225 if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty() && PropertyName != wxT("TYPE")){
227 if (FirstToken == TRUE){
229 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
234 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
244 // Split the address.
246 //std::map<int, int>::iterator SLiter;
247 intPropertyLen = wxSPropertySeg2.Len();
254 for (int i = 0; i <= intPropertyLen; i++){
258 if (wxSPropertySeg2.Mid(i, 1) == wxT(";") && wxSPropertySeg2.Mid((i - 1), 1) != wxT("\\")){
261 SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
263 if (intSplitsFound == 6){
265 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
270 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
280 // Split the data into several parts.
282 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
283 intiter != SplitPoints.end(); ++intiter){
285 if (intiter->first == 1){
289 SLiter = SplitLength.find(1);
291 //txtSurname->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(0, SLiter->second), TRUE));
292 AddressPOBox = wxSPropertySeg2.Mid(0, SLiter->second);
293 intPrevValue = intiter->second;
295 } else if (intiter->first == 2){
297 // Deal with extended address.
299 SLiter = SplitLength.find(2);
301 AddressExtended = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
302 //txtForename->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
303 intPrevValue = intiter->second;
305 } else if (intiter->first == 3){
307 // Deal with street address.
309 SLiter = SplitLength.find(3);
311 AddressStreet = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
312 //txtOtherNames->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
313 intPrevValue = intiter->second;
315 } else if (intiter->first == 4){
317 // Deal with locality
319 SLiter = SplitLength.find(4);
321 AddressLocality = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
322 //txtTitle->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
323 intPrevValue = intiter->second;
325 //txtSuffix->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue), TRUE));
327 } else if (intiter->first == 5){
331 SLiter = SplitLength.find(5);
333 AddressRegion = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
334 //txtTitle->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
335 intPrevValue = intiter->second;
337 //txtSuffix->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue), TRUE));
339 } else if (intiter->first == 6){
341 // Deal with post code.
343 SLiter = SplitLength.find(6);
345 AddressPostalCode = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
346 //txtTitle->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
347 intPrevValue = intiter->second;
349 // Deal with country.
351 AddressCountry = wxSPropertySeg2.Mid(intPrevValue);
352 //txtSuffix->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue), TRUE));
358 // Add the data to the General/Home/Work address variables.
360 ProcessCaptureStrings(&AddressStreet, &AddressLocality, &AddressRegion, &AddressPostalCode, &AddressCountry);
364 coldata.SetId(intValueSeek);
365 coldata.SetData(intValueSeek);
366 coldata.SetText(AddressStreet);
370 ListCtrlIndex = lboAddresses->InsertItem(coldata);
371 lboAddresses->SetItem(ListCtrlIndex, 1, AddressLocality);
372 lboAddresses->SetItem(ListCtrlIndex, 2, AddressRegion);
373 lboAddresses->SetItem(ListCtrlIndex, 3, AddressPostalCode);
375 if (intPref > 0 && intPref < 101){
377 lboAddresses->SetItem(ListCtrlIndex, 4, wxString::Format(wxT("%i"), intPref));
381 GeneralAddressList.erase(intValueSeek);
382 GeneralAddressListTown.erase(intValueSeek);
383 GeneralAddressListCounty.erase(intValueSeek);
384 GeneralAddressListPostCode.erase(intValueSeek);
385 GeneralAddressListCountry.erase(intValueSeek);
386 GeneralAddressListType.erase(intValueSeek);
387 GeneralAddressListTokens.erase(intValueSeek);
388 GeneralAddressList.insert(std::make_pair(intValueSeek, AddressStreet));
389 GeneralAddressListTown.insert(std::make_pair(intValueSeek, AddressLocality));
390 GeneralAddressListCounty.insert(std::make_pair(intValueSeek, AddressRegion));
391 GeneralAddressListPostCode.insert(std::make_pair(intValueSeek, AddressPostalCode));
392 GeneralAddressListCountry.insert(std::make_pair(intValueSeek, AddressCountry));
393 GeneralAddressListType.insert(std::make_pair(intValueSeek, wxT("")));
394 GeneralAddressListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));
396 } else if (intType == 1){
398 ListCtrlIndex = lboHomeAddresses->InsertItem(coldata);
399 lboHomeAddresses->SetItem(ListCtrlIndex, 1, AddressLocality);
400 lboHomeAddresses->SetItem(ListCtrlIndex, 2, AddressRegion);
401 lboHomeAddresses->SetItem(ListCtrlIndex, 3, AddressPostalCode);
403 if (intPref > 0 && intPref < 101){
405 lboHomeAddresses->SetItem(ListCtrlIndex, 1, wxString::Format(wxT("%i"), intPref));
409 HomeAddressList.erase(intValueSeek);
410 HomeAddressListTown.erase(intValueSeek);
411 HomeAddressListCounty.erase(intValueSeek);
412 HomeAddressListPostCode.erase(intValueSeek);
413 HomeAddressListCountry.erase(intValueSeek);
414 HomeAddressListType.erase(intValueSeek);
415 HomeAddressListTokens.erase(intValueSeek);
416 HomeAddressList.insert(std::make_pair(intValueSeek, AddressStreet));
417 HomeAddressListTown.insert(std::make_pair(intValueSeek, AddressLocality));
418 HomeAddressListCounty.insert(std::make_pair(intValueSeek, AddressRegion));
419 HomeAddressListPostCode.insert(std::make_pair(intValueSeek, AddressPostalCode));
420 HomeAddressListCountry.insert(std::make_pair(intValueSeek, AddressCountry));
421 HomeAddressListType.insert(std::make_pair(intValueSeek, wxT("home")));
422 HomeAddressListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));
424 } else if (intType == 2){
426 ListCtrlIndex = lboBusinessAddresses->InsertItem(coldata);
427 lboBusinessAddresses->SetItem(ListCtrlIndex, 1, AddressLocality);
428 lboBusinessAddresses->SetItem(ListCtrlIndex, 2, AddressRegion);
429 lboBusinessAddresses->SetItem(ListCtrlIndex, 3, AddressPostalCode);
431 if (intPref > 0 && intPref < 101){
433 lboBusinessAddresses->SetItem(ListCtrlIndex, 1, wxString::Format(wxT("%i"), intPref));
437 BusinessAddressList.erase(intValueSeek);
438 BusinessAddressListTown.erase(intValueSeek);
439 BusinessAddressListCounty.erase(intValueSeek);
440 BusinessAddressListPostCode.erase(intValueSeek);
441 BusinessAddressListCountry.erase(intValueSeek);
442 BusinessAddressListType.erase(intValueSeek);
443 BusinessAddressListTokens.erase(intValueSeek);
444 BusinessAddressList.insert(std::make_pair(intValueSeek, AddressStreet));
445 BusinessAddressListTown.insert(std::make_pair(intValueSeek, AddressLocality));
446 BusinessAddressListCounty.insert(std::make_pair(intValueSeek, AddressRegion));
447 BusinessAddressListPostCode.insert(std::make_pair(intValueSeek, AddressPostalCode));
448 BusinessAddressListCountry.insert(std::make_pair(intValueSeek, AddressCountry));
449 BusinessAddressListType.insert(std::make_pair(intValueSeek, wxT("work")));
450 BusinessAddressListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));