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