// frmContactEditor-LoadAddress.cpp - frmContactEditor load address subroutines.
//
// (c) 2012-2015 Xestia Software Development.
//
// This file is part of Xestia Address Book.
//
// Xestia Address Book is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, version 3 of the license.
//
// Xestia Address Book is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with Xestia Address Book. If not, see
#include "frmContactEditor.h"
void frmContactEditor::LoadADR(wxString wxSPropertySeg1, wxString wxSPropertySeg2, int *ADRCount){
size_t intPropertyLen = wxSPropertySeg1.Len();
std::map SplitPoints;
std::map SplitLength;
std::map::iterator SLiter;
wxString PropertyData;
wxString PropertyName;
wxString PropertyValue;
wxString PropertyTokens;
wxString AddressLabel;
wxString AddressLang;
wxString AddressAltID;
wxString AddressPID;
wxString AddressTokens;
wxString AddressGeo;
wxString AddressTimezone;
wxString AddressType;
wxString AddressMediatype;
wxString AddressPOBox;
wxString AddressExtended;
wxString AddressStreet;
wxString AddressLocality;
wxString AddressCity;
wxString AddressRegion;
wxString AddressPostalCode;
wxString AddressCountry;
bool FirstToken = TRUE;
int intSplitsFound = 0;
int intSplitSize = 0;
int intPrevValue = 5;
int intPref = 0;
int intType = 0;
long ListCtrlIndex;
SplitValues(&wxSPropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
intPrevValue = 4;
// Look for type before continuing.
for (std::map::iterator intiter = SplitPoints.begin();
intiter != SplitPoints.end(); ++intiter){
SLiter = SplitLength.find(intiter->first);
PropertyData = wxSPropertySeg1.Mid(intPrevValue, (SLiter->second));
wxStringTokenizer PropertyElement (PropertyData, wxT("="));
PropertyName = PropertyElement.GetNextToken();
PropertyValue = PropertyElement.GetNextToken();
intPrevValue = intiter->second;
if (PropertyName == wxT("TYPE")){
if (PropertyValue == wxT("work")){
intType = 2;
} else if (PropertyValue == wxT("home")){
intType = 1;
} else {
intType = 0;
}
}
}
// Setup blank lines for later on.
if (intType == 0){
GeneralAddressList.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListTown.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListCounty.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListPostCode.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListCountry.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListLabel.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListLang.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListAltID.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListPID.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListGeo.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListTimezone.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListPref.insert(std::make_pair(intValueSeek, 0));
GeneralAddressListTokens.insert(std::make_pair(intValueSeek, wxT("")));
} else if (intType == 1){
HomeAddressList.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListTown.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListCounty.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListPostCode.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListCountry.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListLabel.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListLang.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListAltID.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListPID.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListGeo.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListTimezone.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
HomeAddressListPref.insert(std::make_pair(intValueSeek, 0));
HomeAddressListTokens.insert(std::make_pair(intValueSeek, wxT("")));
} else if (intType == 2){
BusinessAddressList.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListTown.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListCounty.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListPostCode.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListCountry.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListLabel.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListLang.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListAltID.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListPID.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListGeo.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListTimezone.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
BusinessAddressListPref.insert(std::make_pair(intValueSeek, 0));
BusinessAddressListTokens.insert(std::make_pair(intValueSeek, wxT("")));
}
intPrevValue = 4;
for (std::map::iterator intiter = SplitPoints.begin();
intiter != SplitPoints.end(); ++intiter){
SLiter = SplitLength.find(intiter->first);
PropertyData = wxSPropertySeg1.Mid(intPrevValue, (SLiter->second));
wxStringTokenizer PropertyElement (PropertyData, wxT("="));
PropertyName = PropertyElement.GetNextToken();
PropertyValue = PropertyElement.GetNextToken();
intPrevValue = intiter->second;
ProcessCaptureStrings(&PropertyValue);
// Process properties.
if (PropertyName == wxT("LABEL")){
if (intType == 0){ GeneralAddressListLabel.erase(intValueSeek); GeneralAddressListLabel.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListLabel.erase(intValueSeek); HomeAddressListLabel.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListLabel.erase(intValueSeek); BusinessAddressListLabel.insert(std::make_pair(intValueSeek, PropertyValue));}
} else if (PropertyName == wxT("LANGUAGE")){
if (intType == 0){ GeneralAddressListLang.erase(intValueSeek); GeneralAddressListLang.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListLang.erase(intValueSeek); HomeAddressListLang.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListLang.erase(intValueSeek); BusinessAddressListLang.insert(std::make_pair(intValueSeek, PropertyValue)); }
} else if (PropertyName == wxT("ALTID")){
if (intType == 0){ GeneralAddressListAltID.erase(intValueSeek); GeneralAddressListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListAltID.erase(intValueSeek); HomeAddressListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListAltID.erase(intValueSeek); BusinessAddressListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
} else if (PropertyName == wxT("PID")){
if (intType == 0){ GeneralAddressListPID.erase(intValueSeek); GeneralAddressListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListPID.erase(intValueSeek); HomeAddressListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListPID.erase(intValueSeek); BusinessAddressListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
} else if (PropertyName == wxT("GEO")){
if (intType == 0){ GeneralAddressListGeo.erase(intValueSeek); GeneralAddressListGeo.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListGeo.erase(intValueSeek); HomeAddressListGeo.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListGeo.erase(intValueSeek); BusinessAddressListGeo.insert(std::make_pair(intValueSeek, PropertyValue)); }
} else if (PropertyName == wxT("TZ")){
if (intType == 0){ GeneralAddressListTimezone.erase(intValueSeek); GeneralAddressListTimezone.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListTimezone.erase(intValueSeek); HomeAddressListTimezone.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListTimezone.erase(intValueSeek); BusinessAddressListTimezone.insert(std::make_pair(intValueSeek, PropertyValue)); }
} else if (PropertyName == wxT("MEDIATYPE")){
if (intType == 0){ GeneralAddressListMediatype.erase(intValueSeek); GeneralAddressListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 1){ HomeAddressListMediatype.erase(intValueSeek); HomeAddressListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
else if (intType == 2){ BusinessAddressListMediatype.erase(intValueSeek); BusinessAddressListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
} else if (PropertyName == wxT("PREF")){
intPref = wxAtoi(PropertyValue);
if (intPref > 0 && intPref < 101){
if (intType == 0){ GeneralAddressListPref.erase(intValueSeek); GeneralAddressListPref.insert(std::make_pair(intValueSeek, intPref)); }
else if (intType == 1){ HomeAddressListPref.erase(intValueSeek); HomeAddressListPref.insert(std::make_pair(intValueSeek, intPref)); }
else if (intType == 2){ BusinessAddressListPref.erase(intValueSeek); BusinessAddressListPref.insert(std::make_pair(intValueSeek, intPref)); }
}
} else {
if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty() && PropertyName != wxT("TYPE")){
if (FirstToken == TRUE){
PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
FirstToken = FALSE;
} else {
PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
}
}
}
}
// Split the address.
//std::map::iterator SLiter;
intPropertyLen = wxSPropertySeg2.Len();
SplitPoints.clear();
SplitLength.clear();
intSplitsFound = 0;
intSplitSize = 0;
intPrevValue = 0;
for (int i = 0; i <= intPropertyLen; i++){
intSplitSize++;
if (wxSPropertySeg2.Mid(i, 1) == wxT(";") && wxSPropertySeg2.Mid((i - 1), 1) != wxT("\\")){
intSplitsFound++;
SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
if (intSplitsFound == 6){
SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
break;
} else {
SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
}
intSplitSize = 0;
}
}
// Split the data into several parts.
for (std::map::iterator intiter = SplitPoints.begin();
intiter != SplitPoints.end(); ++intiter){
if (intiter->first == 1){
// Deal with PO Box.
SLiter = SplitLength.find(1);
//txtSurname->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(0, SLiter->second), TRUE));
AddressPOBox = wxSPropertySeg2.Mid(0, SLiter->second);
intPrevValue = intiter->second;
} else if (intiter->first == 2){
// Deal with extended address.
SLiter = SplitLength.find(2);
AddressExtended = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
//txtForename->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
intPrevValue = intiter->second;
} else if (intiter->first == 3){
// Deal with street address.
SLiter = SplitLength.find(3);
AddressStreet = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
//txtOtherNames->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
intPrevValue = intiter->second;
} else if (intiter->first == 4){
// Deal with locality
SLiter = SplitLength.find(4);
AddressLocality = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
//txtTitle->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
intPrevValue = intiter->second;
//txtSuffix->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue), TRUE));
} else if (intiter->first == 5){
// Deal with region.
SLiter = SplitLength.find(5);
AddressRegion = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
//txtTitle->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
intPrevValue = intiter->second;
//txtSuffix->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue), TRUE));
} else if (intiter->first == 6){
// Deal with post code.
SLiter = SplitLength.find(6);
AddressPostalCode = wxSPropertySeg2.Mid(intPrevValue, SLiter->second);
//txtTitle->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue, SLiter->second), TRUE));
intPrevValue = intiter->second;
// Deal with country.
AddressCountry = wxSPropertySeg2.Mid(intPrevValue);
//txtSuffix->SetValue(ContactData.Convert(wxSPropertySeg2.Mid(intPrevValue), TRUE));
}
}
// Add the data to the General/Home/Work address variables.
ProcessCaptureStrings(&AddressStreet, &AddressLocality, &AddressRegion, &AddressPostalCode, &AddressCountry);
wxListItem coldata;
coldata.SetId(intValueSeek);
coldata.SetData(intValueSeek);
coldata.SetText(AddressStreet);
if (intType == 0){
ListCtrlIndex = lboAddresses->InsertItem(coldata);
lboAddresses->SetItem(ListCtrlIndex, 1, AddressLocality);
lboAddresses->SetItem(ListCtrlIndex, 2, AddressRegion);
lboAddresses->SetItem(ListCtrlIndex, 3, AddressPostalCode);
if (intPref > 0 && intPref < 101){
lboAddresses->SetItem(ListCtrlIndex, 4, wxString::Format(wxT("%i"), intPref));
}
GeneralAddressList.erase(intValueSeek);
GeneralAddressListTown.erase(intValueSeek);
GeneralAddressListCounty.erase(intValueSeek);
GeneralAddressListPostCode.erase(intValueSeek);
GeneralAddressListCountry.erase(intValueSeek);
GeneralAddressListType.erase(intValueSeek);
GeneralAddressListTokens.erase(intValueSeek);
GeneralAddressList.insert(std::make_pair(intValueSeek, AddressStreet));
GeneralAddressListTown.insert(std::make_pair(intValueSeek, AddressLocality));
GeneralAddressListCounty.insert(std::make_pair(intValueSeek, AddressRegion));
GeneralAddressListPostCode.insert(std::make_pair(intValueSeek, AddressPostalCode));
GeneralAddressListCountry.insert(std::make_pair(intValueSeek, AddressCountry));
GeneralAddressListType.insert(std::make_pair(intValueSeek, wxT("")));
GeneralAddressListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));
} else if (intType == 1){
ListCtrlIndex = lboHomeAddresses->InsertItem(coldata);
lboHomeAddresses->SetItem(ListCtrlIndex, 1, AddressLocality);
lboHomeAddresses->SetItem(ListCtrlIndex, 2, AddressRegion);
lboHomeAddresses->SetItem(ListCtrlIndex, 3, AddressPostalCode);
if (intPref > 0 && intPref < 101){
lboHomeAddresses->SetItem(ListCtrlIndex, 1, wxString::Format(wxT("%i"), intPref));
}
HomeAddressList.erase(intValueSeek);
HomeAddressListTown.erase(intValueSeek);
HomeAddressListCounty.erase(intValueSeek);
HomeAddressListPostCode.erase(intValueSeek);
HomeAddressListCountry.erase(intValueSeek);
HomeAddressListType.erase(intValueSeek);
HomeAddressListTokens.erase(intValueSeek);
HomeAddressList.insert(std::make_pair(intValueSeek, AddressStreet));
HomeAddressListTown.insert(std::make_pair(intValueSeek, AddressLocality));
HomeAddressListCounty.insert(std::make_pair(intValueSeek, AddressRegion));
HomeAddressListPostCode.insert(std::make_pair(intValueSeek, AddressPostalCode));
HomeAddressListCountry.insert(std::make_pair(intValueSeek, AddressCountry));
HomeAddressListType.insert(std::make_pair(intValueSeek, wxT("home")));
HomeAddressListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));
} else if (intType == 2){
ListCtrlIndex = lboBusinessAddresses->InsertItem(coldata);
lboBusinessAddresses->SetItem(ListCtrlIndex, 1, AddressLocality);
lboBusinessAddresses->SetItem(ListCtrlIndex, 2, AddressRegion);
lboBusinessAddresses->SetItem(ListCtrlIndex, 3, AddressPostalCode);
if (intPref > 0 && intPref < 101){
lboBusinessAddresses->SetItem(ListCtrlIndex, 1, wxString::Format(wxT("%i"), intPref));
}
BusinessAddressList.erase(intValueSeek);
BusinessAddressListTown.erase(intValueSeek);
BusinessAddressListCounty.erase(intValueSeek);
BusinessAddressListPostCode.erase(intValueSeek);
BusinessAddressListCountry.erase(intValueSeek);
BusinessAddressListType.erase(intValueSeek);
BusinessAddressListTokens.erase(intValueSeek);
BusinessAddressList.insert(std::make_pair(intValueSeek, AddressStreet));
BusinessAddressListTown.insert(std::make_pair(intValueSeek, AddressLocality));
BusinessAddressListCounty.insert(std::make_pair(intValueSeek, AddressRegion));
BusinessAddressListPostCode.insert(std::make_pair(intValueSeek, AddressPostalCode));
BusinessAddressListCountry.insert(std::make_pair(intValueSeek, AddressCountry));
BusinessAddressListType.insert(std::make_pair(intValueSeek, wxT("work")));
BusinessAddressListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));
}
ADRCount++;
intValueSeek++;
}