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 address (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 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
189 iter != ContactFileLines.end(); ++iter){
191 // Find the colon which splits the start bit from the data part.
193 ContactLine = iter->second;
195 while (ExtraLineSeek == TRUE){
197 // Check if there is extra data on the next line
198 // (indicated by space or tab at the start) and add data.
202 if (iter == ContactFileLines.end()){
209 wxSPropertyNextLine = iter->second;
212 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
214 wxSPropertyNextLine.Remove(0, 1);
215 //wxSPropertyNextLine.Trim(FALSE);
216 //ContactLine.Trim();
217 ContactLine.Append(wxSPropertyNextLine);
222 ExtraLineSeek = FALSE;
228 ContactLineLen = ContactLine.Len();
230 // Make sure we are not in quotation mode.
231 // Make sure colon does not have \ or \\ before it.
233 for (int i = 0; i <= ContactLineLen; i++){
235 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
237 PropertyFind = FALSE;
239 } else if (PropertyFind == TRUE){
241 wxSProperty.Append(ContactLine.Mid(i, 1));
245 if (ContactLine.Mid(i, 1) == wxT("\"")){
247 if (QuoteMode == TRUE){
259 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
268 // Split that line at the point into two variables (ignore the colon).
270 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
271 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
273 // Add the data into the contact editor depending on what it is.
275 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
277 // Check if version is 4.0, otherwise don't
280 if (wxSPropertySeg2 != wxT("4.0")){
281 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
282 _("Contact not supported"), wxICON_ERROR);
287 VersionProcessed = TRUE;
289 }/* if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
291 // See frmContactEditor-LoadGroup.cpp
293 LoadKind(wxSPropertySeg2);
295 }/* else if (wxSProperty == wxT("MEMBER")){
297 // See frmContactEditor-LoadGroup.cpp
299 LoadMember(wxSPropertySeg2, &GroupCount);
301 }*/ else if (wxSProperty == wxT("FN")){
303 // See frmContactEditor-LoadName.cpp
305 LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
307 }/* else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
309 // See frmContactEditor-LoadName.cpp
311 LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
313 }*/ else if (wxSProperty == wxT("NICKNAME")){
315 // See frmContactEditor-LoadNickname.cpp
317 LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
319 }/* else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
321 // See frmContactEditor-LoadGender.cpp
323 LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
325 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
327 // See frmContactEditor-LoadBADays.cpp
329 LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
331 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
333 // See frmContactEditor-LoadBADays.cpp
335 LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
337 }*/ else if (wxSProperty == wxT("TZ")){
339 // See frmContactEditor-LoadTimeZone.cpp
341 LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);
343 }/* else if (wxSProperty == wxT("ADR")){
345 // See frmContactEditor-LoadAddress.cpp
347 LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
349 }*/ else if (wxSProperty == wxT("EMAIL")){
351 // See frmContactEditor-LoadEmail.cpp
353 LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);
355 } else if (wxSProperty == wxT("IMPP")){
357 // See frmContactEditor-LoadIM.cpp
359 LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
361 } else if (wxSProperty == wxT("TEL")){
363 // See frmContactEditor-LoadTelephone.cpp
365 LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
367 } else if (wxSProperty == wxT("LANG")){
369 // See frmContactEditor-LoadLanguage.cpp
371 LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
373 } else if (wxSProperty == wxT("GEO")){
375 // See frmContactEditor-LoadGeo.cpp
377 LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);
379 } else if (wxSProperty == wxT("RELATED")){
381 // See fromContactEditor-LoadRelated.cpp
383 LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);
385 } else if (wxSProperty == wxT("URL")){
387 // See frmContactEditor-LoadURL.cpp
389 LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
391 } else if (wxSProperty == wxT("TITLE")) {
393 // See frmContactEditor-LoadTitle.cpp
395 LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
397 } else if (wxSProperty == wxT("ROLE")) {
399 // See frmContactEditor-LoadRole.cpp
401 LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
403 } else if (wxSProperty == wxT("ORG")) {
405 // See frmContactEditor-LoadOrg.cpp
407 LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
409 } else if (wxSProperty == wxT("NOTE")) {
411 // See frmContactEditor-LoadNote.cpp
413 LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount);
415 } else if (wxSProperty == wxT("CATEGORIES")) {
417 // See frmContactEditor-LoadCategory.cpp
419 LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount);
421 } else if (wxSProperty == wxT("PHOTO")) {
423 // See frmContactEditor-LoadPhoto.cpp
425 LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
427 } else if (wxSProperty == wxT("LOGO")) {
429 // See frmContactEditor-LoadLogo.cpp
431 LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
433 } else if (wxSProperty == wxT("SOUND")) {
435 // See frmContactEditor-LoadSound.cpp
437 LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
439 } else if (wxSProperty == wxT("CALURI")){
441 // See frmContactEditor-LoadCalendar.cpp
443 LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
445 } else if (wxSProperty == wxT("CALADRURI")){
447 // See frmContactEditor-LoadCalendar.cpp
449 LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
451 } else if (wxSProperty == wxT("FBURL")){
453 // See frmContactEditor-LoadCalendar.cpp
455 LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
457 } else if (wxSProperty == wxT("KEY")){
459 // See frmContactEditor-LoadKey.cpp
461 LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
463 }/* else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
465 UIDToken = wxSPropertySeg2;
468 }*/ else if (wxSProperty.Mid(0, 3) == wxT("VND")){
470 // Split the Vendor three ways.
472 wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
475 wxString wxSVNDPropName;
478 while (wSTVendorDetails.HasMoreTokens() == TRUE){
480 wSTVendorDetails.GetNextToken();
481 wxSVNDID = wSTVendorDetails.GetNextToken();
482 wxSVNDPropName = wSTVendorDetails.GetNextToken();
487 if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
489 // Setup the values for later processing.
491 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
492 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
493 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
495 // Add the data to the vendor variables.
499 coldata.SetId(intValueSeek);
500 coldata.SetData(intValueSeek);
501 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
503 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
505 VendorList.erase(intValueSeek);
506 VendorListPEN.erase(intValueSeek);
507 VendorListElement.erase(intValueSeek);
509 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
510 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
511 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
518 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
522 XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
523 XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
529 coldata.SetId(intValueSeek);
530 coldata.SetData(intValueSeek);
531 coldata.SetText(wxSPropertySeg1.Mid(2));
533 ListCtrlIndex = lboXToken->InsertItem(coldata);
541 // Reset the variables.
545 ExtraLineSeek = TRUE;
553 FMTimer.SetFilename(Filename);
554 FMTimer.Start(10000, FALSE);
561 void frmContactEditor::SplitValues(wxString *PropertyLine,
562 std::map<int,int> *SplitPoints,
563 std::map<int,int> *SplitLength,
566 size_t intPropertyLen = PropertyLine->Len();
567 int intSplitsFound = 0;
568 int intSplitSize = 0;
569 int intSplitSeek = 0;
571 for (int i = intSize; i <= intPropertyLen; i++){
575 if (PropertyLine->Mid(i, 1) == wxT(";") &&
576 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
578 if (intSplitsFound == 0){
580 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
584 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
588 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
598 if (intSplitsFound == 0){
600 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
601 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
605 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
606 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));