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"
34 bool frmContactEditor::LoadContact(wxString Filename){
36 // Load the contact into the contact editor.
39 wxString wxSContactString;
43 if (StartupEditMode == FALSE){
44 XVMData = MainPtr->GetViewMode();
47 wxSContactFilename = Filename;
49 // Check if we are using wxWidgets version 2.8 or less and
50 // execute the required command accordingly.
52 #if wxABI_VERSION < 20900
53 ContactFile.Open(Filename.c_str(), wxT("r"));
55 ContactFile.Open(Filename, wxT("r"));
58 if (ContactFile.IsOpened() == FALSE){
64 ContactFile.ReadAll(&wxSContactString, wxConvAuto());
68 std::map<int, wxString> ContactFileLines;
69 std::map<int, wxString>::iterator striter;
71 wxStringTokenizer wSTContactFileLines(wxSContactString, wxT("\r\n"));
73 int ContactLineSeek = 0;
75 while (wSTContactFileLines.HasMoreTokens() == TRUE){
77 ContactLine = wSTContactFileLines.GetNextToken();
78 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
85 bool QuoteMode = FALSE;
86 bool PropertyFind = TRUE;
87 bool HasExtraNicknames = FALSE;
88 bool IgnoreGender = FALSE;
89 bool ExtraLineSeek = TRUE;
90 bool BirthdayProcessed = FALSE;
91 bool AnniversaryProcessed = FALSE;
92 bool FNProcessed = FALSE;
93 bool GenderProcessed = FALSE;
94 bool NameProcessed = FALSE;
95 bool UIDProcessed = FALSE;
96 bool KindProcessed = FALSE;
97 bool ETagFound = FALSE;
98 bool ETagOrigFound = FALSE;
99 bool VersionProcessed = FALSE;
100 int intExtraNickname = 0;
101 wxString wxSProperty;
102 wxString wxSPropertySeg1;
103 wxString wxSPropertySeg2;
104 wxString wxSPropertyNextLine;
105 size_t ContactLineLen = 0;
106 int QuoteBreakPoint = 0;
109 int NicknameCount = 0;
118 int RelatedCount = 0;
123 int CategoryCount = 0;
129 int CalReqAdrCount = 0;
130 int FreeBusyCount = 0;
134 //int intValueSeek = 1;
136 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
137 iter != ContactFileLines.end(); ++iter){
139 // Find the colon which splits the start bit from the data part.
141 ContactLine = iter->second;
143 while (ExtraLineSeek == TRUE){
145 // Check if there is extra data on the next line
146 // (indicated by space or tab at the start) and add data.
150 if (iter == ContactFileLines.end()){
157 wxSPropertyNextLine = iter->second;
160 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
162 wxSPropertyNextLine.Remove(0, 1);
163 //wxSPropertyNextLine.Trim(FALSE);
164 //ContactLine.Trim();
165 ContactLine.Append(wxSPropertyNextLine);
170 ExtraLineSeek = FALSE;
176 ContactLineLen = ContactLine.Len();
178 // Make sure we are not in quotation mode.
179 // Make sure colon does not have \ or \\ before it.
181 for (int i = 0; i <= ContactLineLen; i++){
183 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
185 PropertyFind = FALSE;
187 } else if (PropertyFind == TRUE){
189 wxSProperty.Append(ContactLine.Mid(i, 1));
193 if (ContactLine.Mid(i, 1) == wxT("\"")){
195 if (QuoteMode == TRUE){
207 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
216 // Split that line at the point into two variables (ignore the colon).
218 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
219 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
221 // Add the data into the contact editor depending on what it is.
223 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
225 // Check if version is 4.0, otherwise don't
228 wxPuts(wxSPropertySeg2);
229 if (wxSPropertySeg2 != wxT("4.0")){
230 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
231 _("Contact not supported"), wxICON_ERROR);
236 VersionProcessed = TRUE;
238 } if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
240 // See frmContactEditor-LoadGroup.cpp
242 LoadKind(wxSPropertySeg2);
244 } else if (wxSProperty == wxT("MEMBER")){
246 // See frmContactEditor-LoadGroup.cpp
248 LoadMember(wxSPropertySeg2, &GroupCount);
250 } else if (wxSProperty == wxT("FN")){
252 // See frmContactEditor-LoadName.cpp
254 LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
256 } else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
258 // See frmContactEditor-LoadName.cpp
260 LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
262 } else if (wxSProperty == wxT("NICKNAME")){
264 // See frmContactEditor-LoadNickname.cpp
266 LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
268 } else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
270 // See frmContactEditor-LoadGender.cpp
272 LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
274 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
276 // See frmContactEditor-LoadBADays.cpp
278 LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
280 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
282 // See frmContactEditor-LoadBADays.cpp
284 LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
286 } else if (wxSProperty == wxT("TZ")){
288 // See frmContactEditor-LoadTimeZone.cpp
290 LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);
292 } else if (wxSProperty == wxT("ADR")){
294 // See frmContactEditor-LoadAddress.cpp
296 LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
298 } else if (wxSProperty == wxT("EMAIL")){
300 // See frmContactEditor-LoadEmail.cpp
302 LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);
304 } else if (wxSProperty == wxT("IMPP")){
306 // See frmContactEditor-LoadIM.cpp
308 LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
310 } else if (wxSProperty == wxT("TEL")){
312 // See frmContactEditor-LoadTelephone.cpp
314 LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
316 } else if (wxSProperty == wxT("LANG")){
318 // See frmContactEditor-LoadLanguage.cpp
320 LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
322 } else if (wxSProperty == wxT("GEO")){
324 // See frmContactEditor-LoadGeo.cpp
326 LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);
328 } else if (wxSProperty == wxT("RELATED")){
330 // See fromContactEditor-LoadRelated.cpp
332 LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);
334 } else if (wxSProperty == wxT("URL")){
336 // See frmContactEditor-LoadURL.cpp
338 LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
340 } else if (wxSProperty == wxT("TITLE")) {
342 // See frmContactEditor-LoadTitle.cpp
344 LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
346 } else if (wxSProperty == wxT("ROLE")) {
348 // See frmContactEditor-LoadRole.cpp
350 LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
352 } else if (wxSProperty == wxT("ORG")) {
354 // See frmContactEditor-LoadOrg.cpp
356 LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
358 } else if (wxSProperty == wxT("NOTE")) {
360 // See frmContactEditor-LoadNote.cpp
362 LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount);
364 } else if (wxSProperty == wxT("CATEGORIES")) {
366 // See frmContactEditor-LoadCategory.cpp
368 LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount);
370 } else if (wxSProperty == wxT("PHOTO")) {
372 // See frmContactEditor-LoadPhoto.cpp
374 LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
376 } else if (wxSProperty == wxT("LOGO")) {
378 // See frmContactEditor-LoadLogo.cpp
380 LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
382 } else if (wxSProperty == wxT("SOUND")) {
384 // See frmContactEditor-LoadSound.cpp
386 LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
388 } else if (wxSProperty == wxT("CALURI")){
390 // See frmContactEditor-LoadCalendar.cpp
392 LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
394 } else if (wxSProperty == wxT("CALADRURI")){
396 // See frmContactEditor-LoadCalendar.cpp
398 LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
400 } else if (wxSProperty == wxT("FBURL")){
402 // See frmContactEditor-LoadCalendar.cpp
404 LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
406 } else if (wxSProperty == wxT("KEY")){
408 // See frmContactEditor-LoadKey.cpp
410 LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
412 } else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
414 UIDToken = wxSPropertySeg2;
417 } else if (wxSProperty.Mid(0, 3) == wxT("VND")){
419 // Split the Vendor three ways.
421 wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
424 wxString wxSVNDPropName;
427 while (wSTVendorDetails.HasMoreTokens() == TRUE){
429 wSTVendorDetails.GetNextToken();
430 wxSVNDID = wSTVendorDetails.GetNextToken();
431 wxSVNDPropName = wSTVendorDetails.GetNextToken();
436 if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
438 // Setup the values for later processing.
440 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
441 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
442 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
444 // Add the data to the vendor variables.
448 coldata.SetId(intValueSeek);
449 coldata.SetData(intValueSeek);
450 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
452 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
454 VendorList.erase(intValueSeek);
455 VendorListPEN.erase(intValueSeek);
456 VendorListElement.erase(intValueSeek);
458 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
459 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
460 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
467 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
471 XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
472 XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
478 coldata.SetId(intValueSeek);
479 coldata.SetData(intValueSeek);
480 coldata.SetText(wxSPropertySeg1.Mid(2));
482 ListCtrlIndex = lboXToken->InsertItem(coldata);
490 // Reset the variables.
494 ExtraLineSeek = TRUE;
502 FMTimer.SetFilename(Filename);
503 FMTimer.Start(10000, FALSE);
510 void frmContactEditor::SplitValues(wxString *PropertyLine,
511 std::map<int,int> *SplitPoints,
512 std::map<int,int> *SplitLength,
515 size_t intPropertyLen = PropertyLine->Len();
516 int intSplitsFound = 0;
517 int intSplitSize = 0;
518 int intSplitSeek = 0;
520 for (int i = intSize; i <= intPropertyLen; i++){
524 if (PropertyLine->Mid(i, 1) == wxT(";") &&
525 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
527 if (intSplitsFound == 0){
529 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
533 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
537 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
547 if (intSplitsFound == 0){
549 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
550 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
554 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
555 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));