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 contact type (KIND) (frmContactEditor-LoadGroup.cpp)
141 LoadKind(&ContactEditorData.ContactKind);
143 // Process the Birthday (BDAY) (frmContactEditor-LoadBADays.cpp)
145 LoadBirthday(&ContactEditorData.Birthday, &ContactEditorData.BirthdayText);
147 // Process the Anniversary (ANNIVERSARY) (frmContactEditor-LoadBADays.cpp)
149 LoadAnniversary(&ContactEditorData.Anniversary, &ContactEditorData.AnniversaryText);
151 // Process the Gender (GENDER) (frmContactEditor-LoadGender.cpp)
153 LoadGender(&ContactEditorData.Gender, &ContactEditorData.GenderDetails);
155 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
156 iter != ContactFileLines.end(); ++iter){
158 // Find the colon which splits the start bit from the data part.
160 ContactLine = iter->second;
162 while (ExtraLineSeek == TRUE){
164 // Check if there is extra data on the next line
165 // (indicated by space or tab at the start) and add data.
169 if (iter == ContactFileLines.end()){
176 wxSPropertyNextLine = iter->second;
179 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
181 wxSPropertyNextLine.Remove(0, 1);
182 //wxSPropertyNextLine.Trim(FALSE);
183 //ContactLine.Trim();
184 ContactLine.Append(wxSPropertyNextLine);
189 ExtraLineSeek = FALSE;
195 ContactLineLen = ContactLine.Len();
197 // Make sure we are not in quotation mode.
198 // Make sure colon does not have \ or \\ before it.
200 for (int i = 0; i <= ContactLineLen; i++){
202 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
204 PropertyFind = FALSE;
206 } else if (PropertyFind == TRUE){
208 wxSProperty.Append(ContactLine.Mid(i, 1));
212 if (ContactLine.Mid(i, 1) == wxT("\"")){
214 if (QuoteMode == TRUE){
226 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
235 // Split that line at the point into two variables (ignore the colon).
237 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
238 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
240 // Add the data into the contact editor depending on what it is.
242 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
244 // Check if version is 4.0, otherwise don't
247 if (wxSPropertySeg2 != wxT("4.0")){
248 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
249 _("Contact not supported"), wxICON_ERROR);
254 VersionProcessed = TRUE;
256 }/* if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
258 // See frmContactEditor-LoadGroup.cpp
260 LoadKind(wxSPropertySeg2);
262 }*/ else if (wxSProperty == wxT("MEMBER")){
264 // See frmContactEditor-LoadGroup.cpp
266 LoadMember(wxSPropertySeg2, &GroupCount);
268 } else if (wxSProperty == wxT("FN")){
270 // See frmContactEditor-LoadName.cpp
272 LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
274 } else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
276 // See frmContactEditor-LoadName.cpp
278 LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
280 } else if (wxSProperty == wxT("NICKNAME")){
282 // See frmContactEditor-LoadNickname.cpp
284 LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
286 }/* else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
288 // See frmContactEditor-LoadGender.cpp
290 LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
292 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
294 // See frmContactEditor-LoadBADays.cpp
296 LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
298 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
300 // See frmContactEditor-LoadBADays.cpp
302 LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
304 }*/ else if (wxSProperty == wxT("TZ")){
306 // See frmContactEditor-LoadTimeZone.cpp
308 LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);
310 } else if (wxSProperty == wxT("ADR")){
312 // See frmContactEditor-LoadAddress.cpp
314 LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
316 } else if (wxSProperty == wxT("EMAIL")){
318 // See frmContactEditor-LoadEmail.cpp
320 LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);
322 } else if (wxSProperty == wxT("IMPP")){
324 // See frmContactEditor-LoadIM.cpp
326 LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
328 } else if (wxSProperty == wxT("TEL")){
330 // See frmContactEditor-LoadTelephone.cpp
332 LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
334 } else if (wxSProperty == wxT("LANG")){
336 // See frmContactEditor-LoadLanguage.cpp
338 LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
340 } else if (wxSProperty == wxT("GEO")){
342 // See frmContactEditor-LoadGeo.cpp
344 LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);
346 } else if (wxSProperty == wxT("RELATED")){
348 // See fromContactEditor-LoadRelated.cpp
350 LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);
352 } else if (wxSProperty == wxT("URL")){
354 // See frmContactEditor-LoadURL.cpp
356 LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
358 } else if (wxSProperty == wxT("TITLE")) {
360 // See frmContactEditor-LoadTitle.cpp
362 LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
364 } else if (wxSProperty == wxT("ROLE")) {
366 // See frmContactEditor-LoadRole.cpp
368 LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
370 } else if (wxSProperty == wxT("ORG")) {
372 // See frmContactEditor-LoadOrg.cpp
374 LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
376 } else if (wxSProperty == wxT("NOTE")) {
378 // See frmContactEditor-LoadNote.cpp
380 LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount);
382 } else if (wxSProperty == wxT("CATEGORIES")) {
384 // See frmContactEditor-LoadCategory.cpp
386 LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount);
388 } else if (wxSProperty == wxT("PHOTO")) {
390 // See frmContactEditor-LoadPhoto.cpp
392 LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
394 } else if (wxSProperty == wxT("LOGO")) {
396 // See frmContactEditor-LoadLogo.cpp
398 LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
400 } else if (wxSProperty == wxT("SOUND")) {
402 // See frmContactEditor-LoadSound.cpp
404 LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
406 } else if (wxSProperty == wxT("CALURI")){
408 // See frmContactEditor-LoadCalendar.cpp
410 LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
412 } else if (wxSProperty == wxT("CALADRURI")){
414 // See frmContactEditor-LoadCalendar.cpp
416 LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
418 } else if (wxSProperty == wxT("FBURL")){
420 // See frmContactEditor-LoadCalendar.cpp
422 LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
424 } else if (wxSProperty == wxT("KEY")){
426 // See frmContactEditor-LoadKey.cpp
428 LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
430 } else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
432 UIDToken = wxSPropertySeg2;
435 } else if (wxSProperty.Mid(0, 3) == wxT("VND")){
437 // Split the Vendor three ways.
439 wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
442 wxString wxSVNDPropName;
445 while (wSTVendorDetails.HasMoreTokens() == TRUE){
447 wSTVendorDetails.GetNextToken();
448 wxSVNDID = wSTVendorDetails.GetNextToken();
449 wxSVNDPropName = wSTVendorDetails.GetNextToken();
454 if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
456 // Setup the values for later processing.
458 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
459 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
460 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
462 // Add the data to the vendor variables.
466 coldata.SetId(intValueSeek);
467 coldata.SetData(intValueSeek);
468 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
470 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
472 VendorList.erase(intValueSeek);
473 VendorListPEN.erase(intValueSeek);
474 VendorListElement.erase(intValueSeek);
476 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
477 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
478 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
485 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
489 XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
490 XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
496 coldata.SetId(intValueSeek);
497 coldata.SetData(intValueSeek);
498 coldata.SetText(wxSPropertySeg1.Mid(2));
500 ListCtrlIndex = lboXToken->InsertItem(coldata);
508 // Reset the variables.
512 ExtraLineSeek = TRUE;
520 FMTimer.SetFilename(Filename);
521 FMTimer.Start(10000, FALSE);
528 void frmContactEditor::SplitValues(wxString *PropertyLine,
529 std::map<int,int> *SplitPoints,
530 std::map<int,int> *SplitLength,
533 size_t intPropertyLen = PropertyLine->Len();
534 int intSplitsFound = 0;
535 int intSplitSize = 0;
536 int intSplitSeek = 0;
538 for (int i = intSize; i <= intPropertyLen; i++){
542 if (PropertyLine->Mid(i, 1) == wxT(";") &&
543 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
545 if (intSplitsFound == 0){
547 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
551 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
555 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
565 if (intSplitsFound == 0){
567 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
568 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
572 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
573 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));