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 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
170 iter != ContactFileLines.end(); ++iter){
172 // Find the colon which splits the start bit from the data part.
174 ContactLine = iter->second;
176 while (ExtraLineSeek == TRUE){
178 // Check if there is extra data on the next line
179 // (indicated by space or tab at the start) and add data.
183 if (iter == ContactFileLines.end()){
190 wxSPropertyNextLine = iter->second;
193 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
195 wxSPropertyNextLine.Remove(0, 1);
196 //wxSPropertyNextLine.Trim(FALSE);
197 //ContactLine.Trim();
198 ContactLine.Append(wxSPropertyNextLine);
203 ExtraLineSeek = FALSE;
209 ContactLineLen = ContactLine.Len();
211 // Make sure we are not in quotation mode.
212 // Make sure colon does not have \ or \\ before it.
214 for (int i = 0; i <= ContactLineLen; i++){
216 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
218 PropertyFind = FALSE;
220 } else if (PropertyFind == TRUE){
222 wxSProperty.Append(ContactLine.Mid(i, 1));
226 if (ContactLine.Mid(i, 1) == wxT("\"")){
228 if (QuoteMode == TRUE){
240 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
249 // Split that line at the point into two variables (ignore the colon).
251 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
252 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
254 // Add the data into the contact editor depending on what it is.
256 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
258 // Check if version is 4.0, otherwise don't
261 if (wxSPropertySeg2 != wxT("4.0")){
262 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
263 _("Contact not supported"), wxICON_ERROR);
268 VersionProcessed = TRUE;
270 }/* if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
272 // See frmContactEditor-LoadGroup.cpp
274 LoadKind(wxSPropertySeg2);
276 }/* else if (wxSProperty == wxT("MEMBER")){
278 // See frmContactEditor-LoadGroup.cpp
280 LoadMember(wxSPropertySeg2, &GroupCount);
282 }*/ else if (wxSProperty == wxT("FN")){
284 // See frmContactEditor-LoadName.cpp
286 LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
288 }/* else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
290 // See frmContactEditor-LoadName.cpp
292 LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
294 }*/ else if (wxSProperty == wxT("NICKNAME")){
296 // See frmContactEditor-LoadNickname.cpp
298 LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
300 }/* else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
302 // See frmContactEditor-LoadGender.cpp
304 LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
306 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
308 // See frmContactEditor-LoadBADays.cpp
310 LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
312 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
314 // See frmContactEditor-LoadBADays.cpp
316 LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
318 }*/ else if (wxSProperty == wxT("TZ")){
320 // See frmContactEditor-LoadTimeZone.cpp
322 LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);
324 } else if (wxSProperty == wxT("ADR")){
326 // See frmContactEditor-LoadAddress.cpp
328 LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
330 } else if (wxSProperty == wxT("EMAIL")){
332 // See frmContactEditor-LoadEmail.cpp
334 LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);
336 } else if (wxSProperty == wxT("IMPP")){
338 // See frmContactEditor-LoadIM.cpp
340 LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
342 } else if (wxSProperty == wxT("TEL")){
344 // See frmContactEditor-LoadTelephone.cpp
346 LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
348 } else if (wxSProperty == wxT("LANG")){
350 // See frmContactEditor-LoadLanguage.cpp
352 LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
354 } else if (wxSProperty == wxT("GEO")){
356 // See frmContactEditor-LoadGeo.cpp
358 LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);
360 } else if (wxSProperty == wxT("RELATED")){
362 // See fromContactEditor-LoadRelated.cpp
364 LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);
366 } else if (wxSProperty == wxT("URL")){
368 // See frmContactEditor-LoadURL.cpp
370 LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
372 } else if (wxSProperty == wxT("TITLE")) {
374 // See frmContactEditor-LoadTitle.cpp
376 LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
378 } else if (wxSProperty == wxT("ROLE")) {
380 // See frmContactEditor-LoadRole.cpp
382 LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
384 } else if (wxSProperty == wxT("ORG")) {
386 // See frmContactEditor-LoadOrg.cpp
388 LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
390 } else if (wxSProperty == wxT("NOTE")) {
392 // See frmContactEditor-LoadNote.cpp
394 LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount);
396 } else if (wxSProperty == wxT("CATEGORIES")) {
398 // See frmContactEditor-LoadCategory.cpp
400 LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount);
402 } else if (wxSProperty == wxT("PHOTO")) {
404 // See frmContactEditor-LoadPhoto.cpp
406 LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
408 } else if (wxSProperty == wxT("LOGO")) {
410 // See frmContactEditor-LoadLogo.cpp
412 LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
414 } else if (wxSProperty == wxT("SOUND")) {
416 // See frmContactEditor-LoadSound.cpp
418 LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
420 } else if (wxSProperty == wxT("CALURI")){
422 // See frmContactEditor-LoadCalendar.cpp
424 LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
426 } else if (wxSProperty == wxT("CALADRURI")){
428 // See frmContactEditor-LoadCalendar.cpp
430 LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
432 } else if (wxSProperty == wxT("FBURL")){
434 // See frmContactEditor-LoadCalendar.cpp
436 LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
438 } else if (wxSProperty == wxT("KEY")){
440 // See frmContactEditor-LoadKey.cpp
442 LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
444 }/* else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
446 UIDToken = wxSPropertySeg2;
449 }*/ else if (wxSProperty.Mid(0, 3) == wxT("VND")){
451 // Split the Vendor three ways.
453 wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
456 wxString wxSVNDPropName;
459 while (wSTVendorDetails.HasMoreTokens() == TRUE){
461 wSTVendorDetails.GetNextToken();
462 wxSVNDID = wSTVendorDetails.GetNextToken();
463 wxSVNDPropName = wSTVendorDetails.GetNextToken();
468 if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
470 // Setup the values for later processing.
472 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
473 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
474 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
476 // Add the data to the vendor variables.
480 coldata.SetId(intValueSeek);
481 coldata.SetData(intValueSeek);
482 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
484 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
486 VendorList.erase(intValueSeek);
487 VendorListPEN.erase(intValueSeek);
488 VendorListElement.erase(intValueSeek);
490 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
491 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
492 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
499 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
503 XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
504 XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
510 coldata.SetId(intValueSeek);
511 coldata.SetData(intValueSeek);
512 coldata.SetText(wxSPropertySeg1.Mid(2));
514 ListCtrlIndex = lboXToken->InsertItem(coldata);
522 // Reset the variables.
526 ExtraLineSeek = TRUE;
534 FMTimer.SetFilename(Filename);
535 FMTimer.Start(10000, FALSE);
542 void frmContactEditor::SplitValues(wxString *PropertyLine,
543 std::map<int,int> *SplitPoints,
544 std::map<int,int> *SplitLength,
547 size_t intPropertyLen = PropertyLine->Len();
548 int intSplitsFound = 0;
549 int intSplitSize = 0;
550 int intSplitSeek = 0;
552 for (int i = intSize; i <= intPropertyLen; i++){
556 if (PropertyLine->Mid(i, 1) == wxT(";") &&
557 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
559 if (intSplitsFound == 0){
561 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
565 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
569 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
579 if (intSplitsFound == 0){
581 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
582 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
586 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
587 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));