1 // frmContactEditor-Load.cpp - frmContactEditor load contact 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/>
22 #include <wx/tokenzr.h>
23 #include <wx/datetime.h>
26 #include "frmContactEditor.h"
29 #include "../version.h"
30 #include "../vcard/vcard.h"
31 #include "../common/textprocessing.h"
32 #include "../common/dirs.h"
33 #include "cdo/ContactDataObject.h"
35 bool frmContactEditor::LoadContact(wxString Filename){
37 // Load the contact into the contact editor.
40 wxString wxSContactString;
44 if (StartupEditMode == FALSE){
45 XVMData = MainPtr->GetViewMode();
48 wxSContactFilename = Filename;
50 // Check if we are using wxWidgets version 2.8 or less and
51 // execute the required command accordingly.
53 #if wxABI_VERSION < 20900
54 ContactFile.Open(Filename.c_str(), wxT("r"));
56 ContactFile.Open(Filename, wxT("r"));
59 if (ContactFile.IsOpened() == FALSE){
65 ContactEditorData.LoadFile(Filename);
67 ContactFile.ReadAll(&wxSContactString, wxConvAuto());
71 std::map<int, wxString> ContactFileLines;
72 std::map<int, wxString>::iterator striter;
74 wxStringTokenizer wSTContactFileLines(wxSContactString, wxT("\r\n"));
76 int ContactLineSeek = 0;
78 while (wSTContactFileLines.HasMoreTokens() == TRUE){
80 ContactLine = wSTContactFileLines.GetNextToken();
81 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
88 bool QuoteMode = FALSE;
89 bool PropertyFind = TRUE;
90 bool HasExtraNicknames = FALSE;
91 bool IgnoreGender = FALSE;
92 bool ExtraLineSeek = TRUE;
93 //bool BirthdayProcessed = FALSE;
94 //bool AnniversaryProcessed = FALSE;
95 bool FNProcessed = FALSE;
96 bool GenderProcessed = FALSE;
97 bool NameProcessed = FALSE;
98 //bool UIDProcessed = FALSE;
99 //bool KindProcessed = FALSE;
100 bool ETagFound = FALSE;
101 bool ETagOrigFound = FALSE;
102 bool VersionProcessed = FALSE;
103 int intExtraNickname = 0;
104 wxString wxSProperty;
105 wxString wxSPropertySeg1;
106 wxString wxSPropertySeg2;
107 wxString wxSPropertyNextLine;
108 size_t ContactLineLen = 0;
109 int QuoteBreakPoint = 0;
112 int NicknameCount = 0;
121 int RelatedCount = 0;
126 int CategoryCount = 0;
132 int CalReqAdrCount = 0;
133 int FreeBusyCount = 0;
137 //int intValueSeek = 1;
139 // Process the unique ID (UID)
141 UIDToken = ContactEditorData.UIDToken;
143 // Process the contact type (KIND) (frmContactEditor-LoadGroup.cpp)
145 LoadKind(&ContactEditorData.ContactKind);
147 // Process the Birthday (BDAY) (frmContactEditor-LoadBADays.cpp)
149 LoadBirthday(&ContactEditorData.Birthday, &ContactEditorData.BirthdayText);
151 // Process the Anniversary (ANNIVERSARY) (frmContactEditor-LoadBADays.cpp)
153 LoadAnniversary(&ContactEditorData.Anniversary, &ContactEditorData.AnniversaryText);
155 // Process the Gender (GENDER) (frmContactEditor-LoadGender.cpp)
157 LoadGender(&ContactEditorData.Gender, &ContactEditorData.GenderDetails);
159 // Process the Name (N) (frmContactEditor-LoadName.cpp)
161 LoadName(&ContactEditorData.NameTitle, &ContactEditorData.NameForename,
162 &ContactEditorData.NameSurname, &ContactEditorData.NameOtherNames,
163 &ContactEditorData.NameSuffix);
165 // Process the group members (MEMBER) (frmContactEditor-LoadGroup.cpp)
167 LoadMember(&ContactEditorData.GroupsList);
169 // Process the addresses (ADR) (frmContactEditor-LoadAddress.cpp)
171 LoadAddress(&ContactEditorData.GeneralAddressList,
172 &ContactEditorData.GeneralAddressListTown,
173 &ContactEditorData.GeneralAddressListCounty,
174 &ContactEditorData.GeneralAddressListPostCode,
175 &ContactEditorData.GeneralAddressListPref,
176 &ContactEditorData.HomeAddressList,
177 &ContactEditorData.HomeAddressListTown,
178 &ContactEditorData.HomeAddressListCounty,
179 &ContactEditorData.HomeAddressListPostCode,
180 &ContactEditorData.HomeAddressListPref,
181 &ContactEditorData.BusinessAddressList,
182 &ContactEditorData.BusinessAddressListTown,
183 &ContactEditorData.BusinessAddressListCounty,
184 &ContactEditorData.BusinessAddressListPostCode,
185 &ContactEditorData.BusinessAddressListPref,
188 // Process the timezones (TZ).
190 LoadData(&ContactEditorData.GeneralTZList,
191 &ContactEditorData.GeneralTZListPref,
193 &ContactEditorData.HomeTZList,
194 &ContactEditorData.HomeTZListPref,
196 &ContactEditorData.BusinessTZList,
197 &ContactEditorData.BusinessTZListPref,
198 lboBusinessTimezones,
201 // Process the emails (EMAIL).
203 LoadData(&ContactEditorData.GeneralEmailList,
204 &ContactEditorData.GeneralEmailListPref,
206 &ContactEditorData.HomeEmailList,
207 &ContactEditorData.HomeEmailListPref,
209 &ContactEditorData.BusinessEmailList,
210 &ContactEditorData.BusinessEmailListPref,
214 // Process the nicknames (NICKNAME).
216 LoadData(&ContactEditorData.GeneralNicknamesList,
217 &ContactEditorData.GeneralNicknamesListPref,
219 &ContactEditorData.HomeNicknamesList,
220 &ContactEditorData.HomeNicknamesListPref,
222 &ContactEditorData.BusinessNicknamesList,
223 &ContactEditorData.BusinessNicknamesListPref,
224 lboBusinessNicknames,
227 // Process the languages (LANG).
229 LoadData(&ContactEditorData.GeneralLanguageList,
230 &ContactEditorData.GeneralLanguageListPref,
232 &ContactEditorData.HomeLanguageList,
233 &ContactEditorData.HomeLanguageListPref,
235 &ContactEditorData.BusinessLanguageList,
236 &ContactEditorData.BusinessLanguageListPref,
237 lboBusinessLanguages,
240 // Process the geopositiosn (GEO).
242 LoadData(&ContactEditorData.GeneralGeographyList,
243 &ContactEditorData.GeneralGeographyListPref,
245 &ContactEditorData.HomeGeographyList,
246 &ContactEditorData.HomeGeographyListPref,
248 &ContactEditorData.BusinessGeographyList,
249 &ContactEditorData.BusinessGeographyListPref,
250 lboBusinessGeoposition,
253 // Process the websites (URL).
255 LoadData(&ContactEditorData.GeneralWebsiteList,
256 &ContactEditorData.GeneralWebsiteListPref,
258 &ContactEditorData.HomeWebsiteList,
259 &ContactEditorData.HomeWebsiteListPref,
261 &ContactEditorData.BusinessWebsiteList,
262 &ContactEditorData.BusinessWebsiteListPref,
266 // Process the titles (TITLE).
268 LoadData(&ContactEditorData.GeneralTitleList,
269 &ContactEditorData.GeneralTitleListPref,
271 &ContactEditorData.HomeTitleList,
272 &ContactEditorData.HomeTitleListPref,
274 &ContactEditorData.BusinessTitleList,
275 &ContactEditorData.BusinessTitleListPref,
279 // Process the roles (ROLE).
281 LoadData(&ContactEditorData.GeneralRoleList,
282 &ContactEditorData.GeneralRoleListPref,
284 &ContactEditorData.HomeRoleList,
285 &ContactEditorData.HomeRoleListPref,
287 &ContactEditorData.BusinessRoleList,
288 &ContactEditorData.BusinessRoleListPref,
292 // Process the roles (ORG).
294 LoadData(&ContactEditorData.GeneralOrganisationsList,
295 &ContactEditorData.GeneralOrganisationsListPref,
297 &ContactEditorData.HomeOrganisationsList,
298 &ContactEditorData.HomeOrganisationsListPref,
299 lboHomeOrganisations,
300 &ContactEditorData.BusinessOrganisationsList,
301 &ContactEditorData.BusinessOrganisationsListPref,
302 lboBusinessOrganisations,
305 // Process the notes (NOTE).
307 LoadData(&ContactEditorData.GeneralNoteList,
308 &ContactEditorData.GeneralNoteListPref,
310 &ContactEditorData.HomeNoteList,
311 &ContactEditorData.HomeNoteListPref,
313 &ContactEditorData.BusinessNoteList,
314 &ContactEditorData.BusinessNoteListPref,
320 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
321 iter != ContactFileLines.end(); ++iter){
323 // Find the colon which splits the start bit from the data part.
325 ContactLine = iter->second;
327 while (ExtraLineSeek == TRUE){
329 // Check if there is extra data on the next line
330 // (indicated by space or tab at the start) and add data.
334 if (iter == ContactFileLines.end()){
341 wxSPropertyNextLine = iter->second;
344 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
346 wxSPropertyNextLine.Remove(0, 1);
347 //wxSPropertyNextLine.Trim(FALSE);
348 //ContactLine.Trim();
349 ContactLine.Append(wxSPropertyNextLine);
354 ExtraLineSeek = FALSE;
360 ContactLineLen = ContactLine.Len();
362 // Make sure we are not in quotation mode.
363 // Make sure colon does not have \ or \\ before it.
365 for (int i = 0; i <= ContactLineLen; i++){
367 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
369 PropertyFind = FALSE;
371 } else if (PropertyFind == TRUE){
373 wxSProperty.Append(ContactLine.Mid(i, 1));
377 if (ContactLine.Mid(i, 1) == wxT("\"")){
379 if (QuoteMode == TRUE){
391 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
400 // Split that line at the point into two variables (ignore the colon).
402 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
403 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
405 // Add the data into the contact editor depending on what it is.
407 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
409 // Check if version is 4.0, otherwise don't
412 if (wxSPropertySeg2 != wxT("4.0")){
413 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
414 _("Contact not supported"), wxICON_ERROR);
419 VersionProcessed = TRUE;
421 }/* if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
423 // See frmContactEditor-LoadGroup.cpp
425 LoadKind(wxSPropertySeg2);
427 }/* else if (wxSProperty == wxT("MEMBER")){
429 // See frmContactEditor-LoadGroup.cpp
431 LoadMember(wxSPropertySeg2, &GroupCount);
433 }*/ else if (wxSProperty == wxT("FN")){
435 // See frmContactEditor-LoadName.cpp
437 LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
439 }/* else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
441 // See frmContactEditor-LoadName.cpp
443 LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
445 } else if (wxSProperty == wxT("NICKNAME")){
447 // See frmContactEditor-LoadNickname.cpp
449 LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
451 } else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
453 // See frmContactEditor-LoadGender.cpp
455 LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
457 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
459 // See frmContactEditor-LoadBADays.cpp
461 LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
463 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
465 // See frmContactEditor-LoadBADays.cpp
467 LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
469 } else if (wxSProperty == wxT("TZ")){
471 // See frmContactEditor-LoadTimeZone.cpp
473 LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);
475 } else if (wxSProperty == wxT("ADR")){
477 // See frmContactEditor-LoadAddress.cpp
479 LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
481 } else if (wxSProperty == wxT("EMAIL")){
483 // See frmContactEditor-LoadEmail.cpp
485 LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);
487 }*/ else if (wxSProperty == wxT("IMPP")){
489 // See frmContactEditor-LoadIM.cpp
491 LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
493 } else if (wxSProperty == wxT("TEL")){
495 // See frmContactEditor-LoadTelephone.cpp
497 LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
499 }/* else if (wxSProperty == wxT("LANG")){
501 // See frmContactEditor-LoadLanguage.cpp
503 LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
505 } else if (wxSProperty == wxT("GEO")){
507 // See frmContactEditor-LoadGeo.cpp
509 LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);
511 }*/ else if (wxSProperty == wxT("RELATED")){
513 // See fromContactEditor-LoadRelated.cpp
515 LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);
517 }/* else if (wxSProperty == wxT("URL")){
519 // See frmContactEditor-LoadURL.cpp
521 LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
523 } else if (wxSProperty == wxT("TITLE")) {
525 // See frmContactEditor-LoadTitle.cpp
527 LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
529 } else if (wxSProperty == wxT("ROLE")) {
531 // See frmContactEditor-LoadRole.cpp
533 LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
535 } else if (wxSProperty == wxT("ORG")) {
537 // See frmContactEditor-LoadOrg.cpp
539 LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
541 } else if (wxSProperty == wxT("NOTE")) {
543 // See frmContactEditor-LoadNote.cpp
545 LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount);
547 }*/ else if (wxSProperty == wxT("CATEGORIES")) {
549 // See frmContactEditor-LoadCategory.cpp
551 LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount);
553 } else if (wxSProperty == wxT("PHOTO")) {
555 // See frmContactEditor-LoadPhoto.cpp
557 LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
559 } else if (wxSProperty == wxT("LOGO")) {
561 // See frmContactEditor-LoadLogo.cpp
563 LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
565 } else if (wxSProperty == wxT("SOUND")) {
567 // See frmContactEditor-LoadSound.cpp
569 LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
571 } else if (wxSProperty == wxT("CALURI")){
573 // See frmContactEditor-LoadCalendar.cpp
575 LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
577 } else if (wxSProperty == wxT("CALADRURI")){
579 // See frmContactEditor-LoadCalendar.cpp
581 LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
583 } else if (wxSProperty == wxT("FBURL")){
585 // See frmContactEditor-LoadCalendar.cpp
587 LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
589 } else if (wxSProperty == wxT("KEY")){
591 // See frmContactEditor-LoadKey.cpp
593 LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
595 }/* else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
597 UIDToken = wxSPropertySeg2;
600 }*/ else if (wxSProperty.Mid(0, 3) == wxT("VND")){
602 // Split the Vendor three ways.
604 wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
607 wxString wxSVNDPropName;
610 while (wSTVendorDetails.HasMoreTokens() == TRUE){
612 wSTVendorDetails.GetNextToken();
613 wxSVNDID = wSTVendorDetails.GetNextToken();
614 wxSVNDPropName = wSTVendorDetails.GetNextToken();
619 if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
621 // Setup the values for later processing.
623 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
624 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
625 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
627 // Add the data to the vendor variables.
631 coldata.SetId(intValueSeek);
632 coldata.SetData(intValueSeek);
633 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
635 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
637 VendorList.erase(intValueSeek);
638 VendorListPEN.erase(intValueSeek);
639 VendorListElement.erase(intValueSeek);
641 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
642 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
643 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
650 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
654 XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
655 XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
661 coldata.SetId(intValueSeek);
662 coldata.SetData(intValueSeek);
663 coldata.SetText(wxSPropertySeg1.Mid(2));
665 ListCtrlIndex = lboXToken->InsertItem(coldata);
673 // Reset the variables.
677 ExtraLineSeek = TRUE;
685 FMTimer.SetFilename(Filename);
686 FMTimer.Start(10000, FALSE);
693 void frmContactEditor::SplitValues(wxString *PropertyLine,
694 std::map<int,int> *SplitPoints,
695 std::map<int,int> *SplitLength,
698 size_t intPropertyLen = PropertyLine->Len();
699 int intSplitsFound = 0;
700 int intSplitSize = 0;
701 int intSplitSeek = 0;
703 for (int i = intSize; i <= intPropertyLen; i++){
707 if (PropertyLine->Mid(i, 1) == wxT(";") &&
708 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
710 if (intSplitsFound == 0){
712 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
716 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
720 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
730 if (intSplitsFound == 0){
732 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
733 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
737 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
738 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
744 void frmContactEditor::LoadData(std::map<int, wxString> *GeneralList,
745 std::map<int, int> *GeneralListPref,
746 wxListCtrl *GeneralListCtrl,
747 std::map<int, wxString> *HomeList,
748 std::map<int, int> *HomeListPref,
749 wxListCtrl *HomeListCtrl,
750 std::map<int, wxString> *BusinessList,
751 std::map<int, int> *BusinessListPref,
752 wxListCtrl *BusinessListCtrl,
755 long ListCtrlIndex = -1;
757 // Deal with the general addresses.
759 for (std::map<int,wxString>::iterator Iter = GeneralList->begin();
760 Iter != GeneralList->end();
765 coldata.SetId(*DataCount);
766 coldata.SetData(*DataCount);
767 coldata.SetText(Iter->second);
769 ListCtrlIndex = GeneralListCtrl->InsertItem(coldata);
771 if (MapDataExists(DataCount, GeneralListPref)){
773 GeneralListCtrl->SetItem(ListCtrlIndex, 1, wxString::Format("%i", GeneralListPref->find(*DataCount)->second));
781 // Deal with the home addresses.
783 for (std::map<int,wxString>::iterator Iter = HomeList->begin();
784 Iter != HomeList->end();
789 coldata.SetId(*DataCount);
790 coldata.SetData(*DataCount);
791 coldata.SetText(Iter->second);
793 ListCtrlIndex = HomeListCtrl->InsertItem(coldata);
795 if (MapDataExists(DataCount, HomeListPref)){
797 HomeListCtrl->SetItem(ListCtrlIndex, 1, wxString::Format("%i", HomeListPref->find(*DataCount)->second));
805 // Deal with the work addresses.
807 for (std::map<int,wxString>::iterator Iter = BusinessList->begin();
808 Iter != BusinessList->end();
813 coldata.SetId(*DataCount);
814 coldata.SetData(*DataCount);
815 coldata.SetText(Iter->second);
817 ListCtrlIndex = BusinessListCtrl->InsertItem(coldata);
819 if (MapDataExists(DataCount, BusinessListPref)){
821 BusinessListCtrl->SetItem(ListCtrlIndex, 1, wxString::Format("%i", BusinessListPref->find(*DataCount)->second));