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