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