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