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