Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Implemented loading for TZ using ContactDataObject.
[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 address (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 timezone (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);
198         for (std::map<int,wxString>::iterator iter = ContactFileLines.begin(); 
199          iter != ContactFileLines.end(); ++iter){
200         
201                 // Find the colon which splits the start bit from the data part.
202                 
203                 ContactLine = iter->second;
204                 
205                 while (ExtraLineSeek == TRUE){
206                 
207                         // Check if there is extra data on the next line 
208                         // (indicated by space or tab at the start) and add data.
209                 
210                         iter++;
211                         
212                         if (iter == ContactFileLines.end()){
213                         
214                                 iter--;
215                                 break;
216                         
217                         }                       
218                 
219                         wxSPropertyNextLine = iter->second;
220                         
221                 
222                         if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
223                 
224                                 wxSPropertyNextLine.Remove(0, 1);
225                                 //wxSPropertyNextLine.Trim(FALSE);
226                                 //ContactLine.Trim();
227                                 ContactLine.Append(wxSPropertyNextLine);
228                 
229                         } else {
230                         
231                                 iter--;
232                                 ExtraLineSeek = FALSE;
233                         
234                         }
235                 
236                 }
238                 ContactLineLen = ContactLine.Len();
239                 
240                 // Make sure we are not in quotation mode.
241                 // Make sure colon does not have \ or \\ before it.
242                 
243                 for (int i = 0; i <= ContactLineLen; i++){
244                 
245                         if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
246                         
247                                 PropertyFind = FALSE;
248                         
249                         } else if (PropertyFind == TRUE){
250                         
251                                 wxSProperty.Append(ContactLine.Mid(i, 1));
252                         
253                         }               
254                 
255                         if (ContactLine.Mid(i, 1) == wxT("\"")){
256                         
257                                 if (QuoteMode == TRUE){
258                                 
259                                         QuoteMode = FALSE;
260                                 
261                                 } else {
262                         
263                                         QuoteMode = TRUE;
264                                         
265                                 }
266                         
267                         }
268                         
269                         if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
270                         
271                                 QuoteBreakPoint = i;
272                                 break;
273                         
274                         }
275                 
276                 }
277                 
278                 // Split that line at the point into two variables (ignore the colon).
279                 
280                 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
281                 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
282                 
283                 // Add the data into the contact editor depending on what it is.                                
284                 
285                 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
286                 
287                         // Check if version is 4.0, otherwise don't
288                         // load.
289                         
290                         if (wxSPropertySeg2 != wxT("4.0")){
291                                 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
292                                         _("Contact not supported"), wxICON_ERROR);
293                                 this->Close();
294                                 return FALSE;
295                         }
296                         
297                         VersionProcessed = TRUE;
298                 
299                 }/* if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
300                 
301                         // See frmContactEditor-LoadGroup.cpp
302                 
303                         LoadKind(wxSPropertySeg2);
304                 
305                 }/* else if (wxSProperty == wxT("MEMBER")){
307                         // See frmContactEditor-LoadGroup.cpp
309                         LoadMember(wxSPropertySeg2, &GroupCount);               
310                 
311                 }*/ else if (wxSProperty == wxT("FN")){
312                 
313                         // See frmContactEditor-LoadName.cpp
314                 
315                         LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
316                 
317                 }/* else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
318                 
319                         // See frmContactEditor-LoadName.cpp
320                 
321                         LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
322                 
323                 }*/ else if (wxSProperty == wxT("NICKNAME")){
324                         
325                         // See frmContactEditor-LoadNickname.cpp
326                         
327                         LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
328                         
329                 }/* else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
330                 
331                         // See frmContactEditor-LoadGender.cpp
332                 
333                         LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
334                 
335                 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
337                         // See frmContactEditor-LoadBADays.cpp
339                         LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
340                 
341                 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
342                         
343                         // See frmContactEditor-LoadBADays.cpp
344                         
345                         LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
346                 
347                 } else if (wxSProperty == wxT("TZ")){
348                 
349                         // See frmContactEditor-LoadTimeZone.cpp
350                 
351                         LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);       
352                 
353                 }  else if (wxSProperty == wxT("ADR")){
354                         
355                         // See frmContactEditor-LoadAddress.cpp
356                 
357                         LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
358                 
359                 }*/ else if (wxSProperty == wxT("EMAIL")){
360                 
361                         // See frmContactEditor-LoadEmail.cpp
362                         
363                         LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);       
364                 
365                 } else if (wxSProperty == wxT("IMPP")){
366                 
367                         // See frmContactEditor-LoadIM.cpp
368                 
369                         LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
370                 
371                 } else if (wxSProperty == wxT("TEL")){
372                 
373                         // See frmContactEditor-LoadTelephone.cpp
374                 
375                         LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
376                 
377                 } else if (wxSProperty == wxT("LANG")){
378                 
379                         // See frmContactEditor-LoadLanguage.cpp
380                         
381                         LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
382                 
383                 } else if (wxSProperty == wxT("GEO")){
384                 
385                         // See frmContactEditor-LoadGeo.cpp
386                         
387                         LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);   
388                 
389                 } else if (wxSProperty == wxT("RELATED")){
390                         
391                         // See fromContactEditor-LoadRelated.cpp
392                         
393                         LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);           
394                 
395                 } else if (wxSProperty == wxT("URL")){
397                         // See frmContactEditor-LoadURL.cpp
398                 
399                         LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
400                 
401                 } else if (wxSProperty == wxT("TITLE")) {
402                 
403                         // See frmContactEditor-LoadTitle.cpp
404                         
405                         LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
406                         
407                 } else if (wxSProperty == wxT("ROLE")) {
409                         // See frmContactEditor-LoadRole.cpp
410                 
411                         LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
412                         
413                 } else if (wxSProperty == wxT("ORG")) {
414                 
415                         // See frmContactEditor-LoadOrg.cpp
416                         
417                         LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
418                         
419                 } else if (wxSProperty == wxT("NOTE")) {
421                         // See frmContactEditor-LoadNote.cpp
423                         LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount); 
424                         
425                 } else if (wxSProperty == wxT("CATEGORIES")) {
426                 
427                         // See frmContactEditor-LoadCategory.cpp
428                 
429                         LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount); 
430                         
431                 } else if (wxSProperty == wxT("PHOTO")) {
432                 
433                         // See frmContactEditor-LoadPhoto.cpp
434                         
435                         LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
437                 } else if (wxSProperty == wxT("LOGO")) {
438                 
439                         // See frmContactEditor-LoadLogo.cpp
440                 
441                         LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
442                         
443                 } else if (wxSProperty == wxT("SOUND")) {
444                 
445                         // See frmContactEditor-LoadSound.cpp
446                         
447                         LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
448                         
449                 } else if (wxSProperty == wxT("CALURI")){
451                         // See frmContactEditor-LoadCalendar.cpp
452                         
453                         LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
454                 
455                 } else if (wxSProperty == wxT("CALADRURI")){
457                         // See frmContactEditor-LoadCalendar.cpp
458                         
459                         LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
460                 
461                 } else if (wxSProperty == wxT("FBURL")){
463                         // See frmContactEditor-LoadCalendar.cpp
465                         LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
467                 } else if (wxSProperty == wxT("KEY")){
468                 
469                         // See frmContactEditor-LoadKey.cpp
470                         
471                         LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
472                 
473                 }/* else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
474                 
475                         UIDToken = wxSPropertySeg2;
476                         UIDProcessed = TRUE;
477                 
478                 }*/ else if (wxSProperty.Mid(0, 3) == wxT("VND")){
479                 
480                         // Split the Vendor three ways.
481                         
482                         wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
483                         
484                         wxString wxSVNDID;
485                         wxString wxSVNDPropName;
486                         long ListCtrlIndex;                     
488                         while (wSTVendorDetails.HasMoreTokens() == TRUE){
489                         
490                                 wSTVendorDetails.GetNextToken();
491                                 wxSVNDID = wSTVendorDetails.GetNextToken();
492                                 wxSVNDPropName = wSTVendorDetails.GetNextToken();
493                                 break;
494                         
495                         }
496                         
497                         if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
498                         
499                                 // Setup the values for later processing.
500                         
501                                 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
502                                 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
503                                 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
504                         
505                                 // Add the data to the vendor variables.
506                         
507                                 wxListItem coldata;
508                 
509                                 coldata.SetId(intValueSeek);
510                                 coldata.SetData(intValueSeek);
511                                 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
513                                 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
514                                 
515                                 VendorList.erase(intValueSeek);
516                                 VendorListPEN.erase(intValueSeek);
517                                 VendorListElement.erase(intValueSeek);
518                         
519                                 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
520                                 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
521                                 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));                         
522                         
523                                 VendorCount++;
524                                 intValueSeek++;
525                         
526                         }       
527                 
528                 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
529                 
530                         long ListCtrlIndex;
531                         
532                         XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
533                         XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
534                         
535                         // Add to the form.
536                         
537                         wxListItem coldata;
538                 
539                         coldata.SetId(intValueSeek);
540                         coldata.SetData(intValueSeek);
541                         coldata.SetText(wxSPropertySeg1.Mid(2));
543                         ListCtrlIndex = lboXToken->InsertItem(coldata);
544                         
545                         XTokenCount++;
546                         intValueSeek++;
547                         
548                 
549                 }
550                 
551                 // Reset the variables.
552                 
553                 QuoteMode = FALSE;
554                 PropertyFind = TRUE;
555                 ExtraLineSeek = TRUE;
556                 ContactLineLen = 0;
557                 QuoteBreakPoint = 0;
558                 ContactLine.Clear();
559                 wxSProperty.Clear();    
560         
561         }
562         
563         FMTimer.SetFilename(Filename);
564         FMTimer.Start(10000, FALSE);
565         
566         EditMode = TRUE;
567         
568         return TRUE;
571 void frmContactEditor::SplitValues(wxString *PropertyLine, 
572         std::map<int,int> *SplitPoints, 
573         std::map<int,int> *SplitLength, 
574         int intSize){
575         
576         size_t intPropertyLen = PropertyLine->Len();
577         int intSplitsFound = 0;
578         int intSplitSize = 0;
579         int intSplitSeek = 0;
580         
581         for (int i = intSize; i <= intPropertyLen; i++){
583                 intSplitSize++;
584         
585                 if (PropertyLine->Mid(i, 1) == wxT(";") &&
586                     PropertyLine->Mid((i - 1), 1) != wxT("\\")){
587            
588                     if (intSplitsFound == 0){
589             
590                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
591           
592                     } else {
593            
594                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
595             
596                     }
597             
598                     SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
599             
600                     intSplitsFound++;
601                     intSplitSeek = i;
602                     intSplitSize = 0;
603             
604                 }
606         }
608         if (intSplitsFound == 0){
610                 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
611                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
613         } else {
615                 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
616                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
618         }
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