Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
aa5b0e9d845df99ea4c13a6cf4896ef566726efb
[xestiaab/.git] / source / contacteditor / frmContactEditor-Load.cpp
1 // frmContactEditor-Load.cpp - frmContactEditor load contact subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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/>
19 #include <map>
21 #include <wx/ffile.h>
22 #include <wx/tokenzr.h>
23 #include <wx/datetime.h>
24 #include <wx/dir.h>
26 #include "frmContactEditor.h"
28 #include "../enums.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.
38         
39         wxFFile ContactFile;
40         wxString wxSContactString;
41         wxString ContactLine;
42         vCard ContactData;
43         XABViewMode XVMData;
44         if (StartupEditMode == FALSE){
45                 XVMData = MainPtr->GetViewMode();
46         }
47         
48         wxSContactFilename = Filename;
49         
50         // Check if we are using wxWidgets version 2.8 or less and
51         // execute the required command accordingly.
52         
53 #if wxABI_VERSION < 20900
54         ContactFile.Open(Filename.c_str(), wxT("r"));
55 #else
56         ContactFile.Open(Filename, wxT("r"));
57 #endif  
58         
59         if (ContactFile.IsOpened() == FALSE){
60         
61                 return FALSE;
62         
63         }
64         
65         ContactEditorData.LoadFile(Filename);
66         
67         ContactFile.ReadAll(&wxSContactString, wxConvAuto());
68         
69         // Split the lines.
70         
71         std::map<int, wxString> ContactFileLines;
72         std::map<int, wxString>::iterator striter;
73         
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));
82                 ContactLineSeek++;              
83         
84         }
86         // Get the line.
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;
110         int FNCount = 0;
111         int NameCount = 0;
112         int NicknameCount = 0;
113         int ADRCount = 0;
114         int EmailCount = 0;
115         int IMPPCount = 0;
116         int TelCount = 0;
117         int LangCount = 0;
118         int TZCount = 0;
119         int GeoCount = 0;
120         int URLCount = 0;
121         int RelatedCount = 0;
122         int TitleCount = 0;
123         int RoleCount = 0;
124         int OrgCount = 0;
125         int NoteCount = 0;
126         int CategoryCount = 0;
127         int GroupCount = 0;
128         int PhotoCount = 0;
129         int LogoCount = 0;
130         int SoundCount = 0;
131         int CalAdrCount = 0;
132         int CalReqAdrCount = 0;
133         int FreeBusyCount = 0;
134         int KeyCount = 0;
135         int VendorCount = 0;
136         int XTokenCount = 0;
137         //int intValueSeek = 1;
139         // Process the unique ID (UID)
140         
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)
148         
149         LoadBirthday(&ContactEditorData.Birthday, &ContactEditorData.BirthdayText);
151         // Process the Anniversary (ANNIVERSARY) (frmContactEditor-LoadBADays.cpp)
152         
153         LoadAnniversary(&ContactEditorData.Anniversary, &ContactEditorData.AnniversaryText);
155         // Process the Gender (GENDER) (frmContactEditor-LoadGender.cpp)
156         
157         LoadGender(&ContactEditorData.Gender, &ContactEditorData.GenderDetails);
158         
159         // Process the Name (N) (frmContactEditor-LoadName.cpp)
160         
161         LoadName(&ContactEditorData.NameTitle, &ContactEditorData.NameForename,
162                 &ContactEditorData.NameSurname, &ContactEditorData.NameOtherNames,
163                 &ContactEditorData.NameSuffix);
164                 
165         // Process the group members (MEMBER) (frmContactEditor-LoadGroup.cpp)
166                 
167         LoadMember(&ContactEditorData.GroupsList);
169         // Process the addresses (ADR) (frmContactEditor-LoadAddress.cpp)
170         
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,
186                 &ADRCount);
188         // Process the timezones (TZ) (frmContactEditor-LoadTimeZone.cpp)
189         
190         LoadTimeZone(&ContactEditorData.GeneralTZList,
191                 &ContactEditorData.GeneralTZListPref,
192                 &ContactEditorData.HomeTZList,
193                 &ContactEditorData.HomeTZListPref,
194                 &ContactEditorData.BusinessTZList,
195                 &ContactEditorData.BusinessTZListPref,
196                 &TZCount);
197                 
198         // Process the emails (frmContactEditor-LoadEmail.cpp)
199         
200         LoadEmail(&ContactEditorData.GeneralEmailList,
201                 &ContactEditorData.GeneralEmailListPref,
202                 &ContactEditorData.HomeEmailList,
203                 &ContactEditorData.HomeEmailListPref,
204                 &ContactEditorData.BusinessEmailList,
205                 &ContactEditorData.BusinessEmailListPref,
206                 &EmailCount);   
208         for (std::map<int,wxString>::iterator iter = ContactFileLines.begin(); 
209          iter != ContactFileLines.end(); ++iter){
210         
211                 // Find the colon which splits the start bit from the data part.
212                 
213                 ContactLine = iter->second;
214                 
215                 while (ExtraLineSeek == TRUE){
216                 
217                         // Check if there is extra data on the next line 
218                         // (indicated by space or tab at the start) and add data.
219                 
220                         iter++;
221                         
222                         if (iter == ContactFileLines.end()){
223                         
224                                 iter--;
225                                 break;
226                         
227                         }                       
228                 
229                         wxSPropertyNextLine = iter->second;
230                         
231                 
232                         if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
233                 
234                                 wxSPropertyNextLine.Remove(0, 1);
235                                 //wxSPropertyNextLine.Trim(FALSE);
236                                 //ContactLine.Trim();
237                                 ContactLine.Append(wxSPropertyNextLine);
238                 
239                         } else {
240                         
241                                 iter--;
242                                 ExtraLineSeek = FALSE;
243                         
244                         }
245                 
246                 }
248                 ContactLineLen = ContactLine.Len();
249                 
250                 // Make sure we are not in quotation mode.
251                 // Make sure colon does not have \ or \\ before it.
252                 
253                 for (int i = 0; i <= ContactLineLen; i++){
254                 
255                         if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
256                         
257                                 PropertyFind = FALSE;
258                         
259                         } else if (PropertyFind == TRUE){
260                         
261                                 wxSProperty.Append(ContactLine.Mid(i, 1));
262                         
263                         }               
264                 
265                         if (ContactLine.Mid(i, 1) == wxT("\"")){
266                         
267                                 if (QuoteMode == TRUE){
268                                 
269                                         QuoteMode = FALSE;
270                                 
271                                 } else {
272                         
273                                         QuoteMode = TRUE;
274                                         
275                                 }
276                         
277                         }
278                         
279                         if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
280                         
281                                 QuoteBreakPoint = i;
282                                 break;
283                         
284                         }
285                 
286                 }
287                 
288                 // Split that line at the point into two variables (ignore the colon).
289                 
290                 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
291                 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
292                 
293                 // Add the data into the contact editor depending on what it is.                                
294                 
295                 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
296                 
297                         // Check if version is 4.0, otherwise don't
298                         // load.
299                         
300                         if (wxSPropertySeg2 != wxT("4.0")){
301                                 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
302                                         _("Contact not supported"), wxICON_ERROR);
303                                 this->Close();
304                                 return FALSE;
305                         }
306                         
307                         VersionProcessed = TRUE;
308                 
309                 }/* if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
310                 
311                         // See frmContactEditor-LoadGroup.cpp
312                 
313                         LoadKind(wxSPropertySeg2);
314                 
315                 }/* else if (wxSProperty == wxT("MEMBER")){
317                         // See frmContactEditor-LoadGroup.cpp
319                         LoadMember(wxSPropertySeg2, &GroupCount);               
320                 
321                 }*/ else if (wxSProperty == wxT("FN")){
322                 
323                         // See frmContactEditor-LoadName.cpp
324                 
325                         LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
326                 
327                 }/* else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
328                 
329                         // See frmContactEditor-LoadName.cpp
330                 
331                         LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
332                 
333                 }*/ else if (wxSProperty == wxT("NICKNAME")){
334                         
335                         // See frmContactEditor-LoadNickname.cpp
336                         
337                         LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
338                         
339                 }/* else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
340                 
341                         // See frmContactEditor-LoadGender.cpp
342                 
343                         LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
344                 
345                 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
347                         // See frmContactEditor-LoadBADays.cpp
349                         LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
350                 
351                 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
352                         
353                         // See frmContactEditor-LoadBADays.cpp
354                         
355                         LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
356                 
357                 } else if (wxSProperty == wxT("TZ")){
358                 
359                         // See frmContactEditor-LoadTimeZone.cpp
360                 
361                         LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);       
362                 
363                 }  else if (wxSProperty == wxT("ADR")){
364                         
365                         // See frmContactEditor-LoadAddress.cpp
366                 
367                         LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
368                 
369                 } else if (wxSProperty == wxT("EMAIL")){
370                 
371                         // See frmContactEditor-LoadEmail.cpp
372                         
373                         LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);       
374                 
375                 }*/ else if (wxSProperty == wxT("IMPP")){
376                 
377                         // See frmContactEditor-LoadIM.cpp
378                 
379                         LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
380                 
381                 } else if (wxSProperty == wxT("TEL")){
382                 
383                         // See frmContactEditor-LoadTelephone.cpp
384                 
385                         LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
386                 
387                 } else if (wxSProperty == wxT("LANG")){
388                 
389                         // See frmContactEditor-LoadLanguage.cpp
390                         
391                         LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
392                 
393                 } else if (wxSProperty == wxT("GEO")){
394                 
395                         // See frmContactEditor-LoadGeo.cpp
396                         
397                         LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);   
398                 
399                 } else if (wxSProperty == wxT("RELATED")){
400                         
401                         // See fromContactEditor-LoadRelated.cpp
402                         
403                         LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);           
404                 
405                 } else if (wxSProperty == wxT("URL")){
407                         // See frmContactEditor-LoadURL.cpp
408                 
409                         LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
410                 
411                 } else if (wxSProperty == wxT("TITLE")) {
412                 
413                         // See frmContactEditor-LoadTitle.cpp
414                         
415                         LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
416                         
417                 } else if (wxSProperty == wxT("ROLE")) {
419                         // See frmContactEditor-LoadRole.cpp
420                 
421                         LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
422                         
423                 } else if (wxSProperty == wxT("ORG")) {
424                 
425                         // See frmContactEditor-LoadOrg.cpp
426                         
427                         LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
428                         
429                 } else if (wxSProperty == wxT("NOTE")) {
431                         // See frmContactEditor-LoadNote.cpp
433                         LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount); 
434                         
435                 } else if (wxSProperty == wxT("CATEGORIES")) {
436                 
437                         // See frmContactEditor-LoadCategory.cpp
438                 
439                         LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount); 
440                         
441                 } else if (wxSProperty == wxT("PHOTO")) {
442                 
443                         // See frmContactEditor-LoadPhoto.cpp
444                         
445                         LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
447                 } else if (wxSProperty == wxT("LOGO")) {
448                 
449                         // See frmContactEditor-LoadLogo.cpp
450                 
451                         LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
452                         
453                 } else if (wxSProperty == wxT("SOUND")) {
454                 
455                         // See frmContactEditor-LoadSound.cpp
456                         
457                         LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
458                         
459                 } else if (wxSProperty == wxT("CALURI")){
461                         // See frmContactEditor-LoadCalendar.cpp
462                         
463                         LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
464                 
465                 } else if (wxSProperty == wxT("CALADRURI")){
467                         // See frmContactEditor-LoadCalendar.cpp
468                         
469                         LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
470                 
471                 } else if (wxSProperty == wxT("FBURL")){
473                         // See frmContactEditor-LoadCalendar.cpp
475                         LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
477                 } else if (wxSProperty == wxT("KEY")){
478                 
479                         // See frmContactEditor-LoadKey.cpp
480                         
481                         LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
482                 
483                 }/* else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
484                 
485                         UIDToken = wxSPropertySeg2;
486                         UIDProcessed = TRUE;
487                 
488                 }*/ else if (wxSProperty.Mid(0, 3) == wxT("VND")){
489                 
490                         // Split the Vendor three ways.
491                         
492                         wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
493                         
494                         wxString wxSVNDID;
495                         wxString wxSVNDPropName;
496                         long ListCtrlIndex;                     
498                         while (wSTVendorDetails.HasMoreTokens() == TRUE){
499                         
500                                 wSTVendorDetails.GetNextToken();
501                                 wxSVNDID = wSTVendorDetails.GetNextToken();
502                                 wxSVNDPropName = wSTVendorDetails.GetNextToken();
503                                 break;
504                         
505                         }
506                         
507                         if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
508                         
509                                 // Setup the values for later processing.
510                         
511                                 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
512                                 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
513                                 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
514                         
515                                 // Add the data to the vendor variables.
516                         
517                                 wxListItem coldata;
518                 
519                                 coldata.SetId(intValueSeek);
520                                 coldata.SetData(intValueSeek);
521                                 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
523                                 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
524                                 
525                                 VendorList.erase(intValueSeek);
526                                 VendorListPEN.erase(intValueSeek);
527                                 VendorListElement.erase(intValueSeek);
528                         
529                                 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
530                                 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
531                                 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));                         
532                         
533                                 VendorCount++;
534                                 intValueSeek++;
535                         
536                         }       
537                 
538                 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
539                 
540                         long ListCtrlIndex;
541                         
542                         XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
543                         XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
544                         
545                         // Add to the form.
546                         
547                         wxListItem coldata;
548                 
549                         coldata.SetId(intValueSeek);
550                         coldata.SetData(intValueSeek);
551                         coldata.SetText(wxSPropertySeg1.Mid(2));
553                         ListCtrlIndex = lboXToken->InsertItem(coldata);
554                         
555                         XTokenCount++;
556                         intValueSeek++;
557                         
558                 
559                 }
560                 
561                 // Reset the variables.
562                 
563                 QuoteMode = FALSE;
564                 PropertyFind = TRUE;
565                 ExtraLineSeek = TRUE;
566                 ContactLineLen = 0;
567                 QuoteBreakPoint = 0;
568                 ContactLine.Clear();
569                 wxSProperty.Clear();    
570         
571         }
572         
573         FMTimer.SetFilename(Filename);
574         FMTimer.Start(10000, FALSE);
575         
576         EditMode = TRUE;
577         
578         return TRUE;
581 void frmContactEditor::SplitValues(wxString *PropertyLine, 
582         std::map<int,int> *SplitPoints, 
583         std::map<int,int> *SplitLength, 
584         int intSize){
585         
586         size_t intPropertyLen = PropertyLine->Len();
587         int intSplitsFound = 0;
588         int intSplitSize = 0;
589         int intSplitSeek = 0;
590         
591         for (int i = intSize; i <= intPropertyLen; i++){
593                 intSplitSize++;
594         
595                 if (PropertyLine->Mid(i, 1) == wxT(";") &&
596                     PropertyLine->Mid((i - 1), 1) != wxT("\\")){
597            
598                     if (intSplitsFound == 0){
599             
600                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
601           
602                     } else {
603            
604                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
605             
606                     }
607             
608                     SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
609             
610                     intSplitsFound++;
611                     intSplitSeek = i;
612                     intSplitSize = 0;
613             
614                 }
616         }
618         if (intSplitsFound == 0){
620                 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
621                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
623         } else {
625                 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
626                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
628         }
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy