Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Extra commit for -e command switch support.
[xestiaab/.git] / source / contacteditor / frmContactEditor-Load.cpp
1 #include <map>
3 #include <wx/ffile.h>
4 #include <wx/tokenzr.h>
5 #include <wx/datetime.h>
6 #include <wx/dir.h>
8 #include "frmContactEditor.h"
10 #include "../enums.h"
11 #include "../version.h"
12 #include "../vcard/vcard.h"
13 #include "../common/textprocessing.h"
14 #include "../common/dirs.h"
16 bool frmContactEditor::LoadContact(wxString Filename){
18         // Load the contact into the contact editor.
19         
20         wxFFile ContactFile;
21         wxString wxSContactString;
22         wxString ContactLine;
23         vCard ContactData;
24         XABViewMode XVMData;
25         if (StartupEditMode == FALSE){
26                 XVMData = MainPtr->GetViewMode();
27         }
28         
29         wxSContactFilename = Filename;
30         
31         // Check if we are using wxWidgets version 2.8 or less and
32         // execute the required command accordingly.
33         
34 #if wxABI_VERSION < 20900
35         ContactFile.Open(Filename.c_str(), wxT("r"));
36 #else
37         ContactFile.Open(Filename, wxT("r"));
38 #endif  
39         
40         if (ContactFile.IsOpened() == FALSE){
41         
42                 return FALSE;
43         
44         }
45         
46         ContactFile.ReadAll(&wxSContactString, wxConvAuto());
47         
48         // Split the lines.
49         
50         std::map<int, wxString> ContactFileLines;
51         std::map<int, wxString>::iterator striter;
52         
53         wxStringTokenizer wSTContactFileLines(wxSContactString, wxT("\r\n"));
55         int ContactLineSeek = 0;
57         while (wSTContactFileLines.HasMoreTokens() == TRUE){
59                 ContactLine = wSTContactFileLines.GetNextToken();
60                 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
61                 ContactLineSeek++;              
62         
63         }
65         // Get the line.
67         bool QuoteMode = FALSE;
68         bool PropertyFind = TRUE;
69         bool HasExtraNicknames = FALSE;
70         bool IgnoreGender = FALSE;
71         bool ExtraLineSeek = TRUE;
72         bool BirthdayProcessed = FALSE;
73         bool AnniversaryProcessed = FALSE;
74         bool FNProcessed = FALSE;
75         bool GenderProcessed = FALSE;
76         bool NameProcessed = FALSE;
77         bool UIDProcessed = FALSE;
78         bool KindProcessed = FALSE;
79         bool ETagFound = FALSE;
80         bool ETagOrigFound = FALSE;
81         bool VersionProcessed = FALSE;
82         int intExtraNickname = 0;
83         wxString wxSProperty;
84         wxString wxSPropertySeg1;
85         wxString wxSPropertySeg2;
86         wxString wxSPropertyNextLine;
87         size_t ContactLineLen = 0;
88         int QuoteBreakPoint = 0;
89         int FNCount = 0;
90         int NameCount = 0;
91         int NicknameCount = 0;
92         int ADRCount = 0;
93         int EmailCount = 0;
94         int IMPPCount = 0;
95         int TelCount = 0;
96         int LangCount = 0;
97         int TZCount = 0;
98         int GeoCount = 0;
99         int URLCount = 0;
100         int RelatedCount = 0;
101         int TitleCount = 0;
102         int RoleCount = 0;
103         int OrgCount = 0;
104         int NoteCount = 0;
105         int CategoryCount = 0;
106         int GroupCount = 0;
107         int PhotoCount = 0;
108         int LogoCount = 0;
109         int SoundCount = 0;
110         int CalAdrCount = 0;
111         int CalReqAdrCount = 0;
112         int FreeBusyCount = 0;
113         int KeyCount = 0;
114         int VendorCount = 0;
115         int XTokenCount = 0;
116         //int intValueSeek = 1;
118         for (std::map<int,wxString>::iterator iter = ContactFileLines.begin(); 
119          iter != ContactFileLines.end(); ++iter){
120         
121                 // Find the colon which splits the start bit from the data part.
122                 
123                 ContactLine = iter->second;
124                 
125                 while (ExtraLineSeek == TRUE){
126                 
127                         // Check if there is extra data on the next line 
128                         // (indicated by space or tab at the start) and add data.
129                 
130                         iter++;
131                         
132                         if (iter == ContactFileLines.end()){
133                         
134                                 iter--;
135                                 break;
136                         
137                         }                       
138                 
139                         wxSPropertyNextLine = iter->second;
140                         
141                 
142                         if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
143                 
144                                 wxSPropertyNextLine.Remove(0, 1);
145                                 //wxSPropertyNextLine.Trim(FALSE);
146                                 //ContactLine.Trim();
147                                 ContactLine.Append(wxSPropertyNextLine);
148                 
149                         } else {
150                         
151                                 iter--;
152                                 ExtraLineSeek = FALSE;
153                         
154                         }
155                 
156                 }
158                 ContactLineLen = ContactLine.Len();
159                 
160                 // Make sure we are not in quotation mode.
161                 // Make sure colon does not have \ or \\ before it.
162                 
163                 for (int i = 0; i <= ContactLineLen; i++){
164                 
165                         if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
166                         
167                                 PropertyFind = FALSE;
168                         
169                         } else if (PropertyFind == TRUE){
170                         
171                                 wxSProperty.Append(ContactLine.Mid(i, 1));
172                         
173                         }               
174                 
175                         if (ContactLine.Mid(i, 1) == wxT("\"")){
176                         
177                                 if (QuoteMode == TRUE){
178                                 
179                                         QuoteMode = FALSE;
180                                 
181                                 } else {
182                         
183                                         QuoteMode = TRUE;
184                                         
185                                 }
186                         
187                         }
188                         
189                         if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
190                         
191                                 QuoteBreakPoint = i;
192                                 break;
193                         
194                         }
195                 
196                 }
197                 
198                 // Split that line at the point into two variables (ignore the colon).
199                 
200                 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
201                 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
202                 
203                 // Add the data into the contact editor depending on what it is.                                
204                 
205                 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
206                 
207                         // Check if version is 4.0, otherwise don't
208                         // load.
209                         
210                         wxPuts(wxSPropertySeg2);
211                         if (wxSPropertySeg2 != wxT("4.0")){
212                                 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
213                                         _("Contact not supported"), wxICON_ERROR);
214                                 this->Close();
215                                 return FALSE;
216                         }
217                         
218                         VersionProcessed = TRUE;
219                 
220                 } if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
221                 
222                         // See frmContactEditor-LoadGroup.cpp
223                 
224                         LoadKind(wxSPropertySeg2);
225                 
226                 } else if (wxSProperty == wxT("MEMBER")){
228                         // See frmContactEditor-LoadGroup.cpp
230                         LoadMember(wxSPropertySeg2, &GroupCount);               
231                 
232                 } else if (wxSProperty == wxT("FN")){
233                 
234                         // See frmContactEditor-LoadName.cpp
235                 
236                         LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
237                 
238                 } else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
239                 
240                         // See frmContactEditor-LoadName.cpp
241                 
242                         LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
243                 
244                 } else if (wxSProperty == wxT("NICKNAME")){
245                         
246                         // See frmContactEditor-LoadNickname.cpp
247                         
248                         LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
249                         
250                 } else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
251                 
252                         // See frmContactEditor-LoadGender.cpp
253                 
254                         LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
255                 
256                 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
258                         // See frmContactEditor-LoadBADays.cpp
260                         LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
261                 
262                 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
263                         
264                         // See frmContactEditor-LoadBADays.cpp
265                         
266                         LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
267                 
268                 } else if (wxSProperty == wxT("TZ")){
269                 
270                         // See frmContactEditor-LoadTimeZone.cpp
271                 
272                         LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);       
273                 
274                 } else if (wxSProperty == wxT("ADR")){
275                         
276                         // See frmContactEditor-LoadAddress.cpp
277                 
278                         LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
279                 
280                 } else if (wxSProperty == wxT("EMAIL")){
281                 
282                         // See frmContactEditor-LoadEmail.cpp
283                         
284                         LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);       
285                 
286                 } else if (wxSProperty == wxT("IMPP")){
287                 
288                         // See frmContactEditor-LoadIM.cpp
289                 
290                         LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
291                 
292                 } else if (wxSProperty == wxT("TEL")){
293                 
294                         // See frmContactEditor-LoadTelephone.cpp
295                 
296                         LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
297                 
298                 } else if (wxSProperty == wxT("LANG")){
299                 
300                         // See frmContactEditor-LoadLanguage.cpp
301                         
302                         LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
303                 
304                 } else if (wxSProperty == wxT("GEO")){
305                 
306                         // See frmContactEditor-LoadGeo.cpp
307                         
308                         LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);   
309                 
310                 } else if (wxSProperty == wxT("RELATED")){
311                         
312                         // See fromContactEditor-LoadRelated.cpp
313                         
314                         LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);           
315                 
316                 } else if (wxSProperty == wxT("URL")){
318                         // See frmContactEditor-LoadURL.cpp
319                 
320                         LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
321                 
322                 } else if (wxSProperty == wxT("TITLE")) {
323                 
324                         // See frmContactEditor-LoadTitle.cpp
325                         
326                         LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
327                         
328                 } else if (wxSProperty == wxT("ROLE")) {
330                         // See frmContactEditor-LoadRole.cpp
331                 
332                         LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
333                         
334                 } else if (wxSProperty == wxT("ORG")) {
335                 
336                         // See frmContactEditor-LoadOrg.cpp
337                         
338                         LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
339                         
340                 } else if (wxSProperty == wxT("NOTE")) {
342                         // See frmContactEditor-LoadNote.cpp
344                         LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount); 
345                         
346                 } else if (wxSProperty == wxT("CATEGORIES")) {
347                 
348                         // See frmContactEditor-LoadCategory.cpp
349                 
350                         LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount); 
351                         
352                 } else if (wxSProperty == wxT("PHOTO")) {
353                 
354                         // See frmContactEditor-LoadPhoto.cpp
355                         
356                         LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
358                 } else if (wxSProperty == wxT("LOGO")) {
359                 
360                         // See frmContactEditor-LoadLogo.cpp
361                 
362                         LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
363                         
364                 } else if (wxSProperty == wxT("SOUND")) {
365                 
366                         // See frmContactEditor-LoadSound.cpp
367                         
368                         LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
369                         
370                 } else if (wxSProperty == wxT("CALURI")){
372                         // See frmContactEditor-LoadCalendar.cpp
373                         
374                         LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
375                 
376                 } else if (wxSProperty == wxT("CALADRURI")){
378                         // See frmContactEditor-LoadCalendar.cpp
379                         
380                         LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
381                 
382                 } else if (wxSProperty == wxT("FBURL")){
384                         // See frmContactEditor-LoadCalendar.cpp
386                         LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
388                 } else if (wxSProperty == wxT("KEY")){
389                 
390                         // See frmContactEditor-LoadKey.cpp
391                         
392                         LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
393                 
394                 } else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
395                 
396                         UIDToken = wxSPropertySeg2;
397                         UIDProcessed = TRUE;
398                 
399                 } else if (wxSProperty.Mid(0, 3) == wxT("VND")){
400                 
401                         // Split the Vendor three ways.
402                         
403                         wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
404                         
405                         wxString wxSVNDID;
406                         wxString wxSVNDPropName;
407                         long ListCtrlIndex;                     
409                         while (wSTVendorDetails.HasMoreTokens() == TRUE){
410                         
411                                 wSTVendorDetails.GetNextToken();
412                                 wxSVNDID = wSTVendorDetails.GetNextToken();
413                                 wxSVNDPropName = wSTVendorDetails.GetNextToken();
414                                 break;
415                         
416                         }
417                         
418                         if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
419                         
420                                 // Setup the values for later processing.
421                         
422                                 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
423                                 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
424                                 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
425                         
426                                 // Add the data to the vendor variables.
427                         
428                                 wxListItem coldata;
429                 
430                                 coldata.SetId(intValueSeek);
431                                 coldata.SetData(intValueSeek);
432                                 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
434                                 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
435                                 
436                                 VendorList.erase(intValueSeek);
437                                 VendorListPEN.erase(intValueSeek);
438                                 VendorListElement.erase(intValueSeek);
439                         
440                                 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
441                                 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
442                                 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));                         
443                         
444                                 VendorCount++;
445                                 intValueSeek++;
446                         
447                         }       
448                 
449                 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
450                 
451                         long ListCtrlIndex;
452                         
453                         XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
454                         XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
455                         
456                         // Add to the form.
457                         
458                         wxListItem coldata;
459                 
460                         coldata.SetId(intValueSeek);
461                         coldata.SetData(intValueSeek);
462                         coldata.SetText(wxSPropertySeg1.Mid(2));
464                         ListCtrlIndex = lboXToken->InsertItem(coldata);
465                         
466                         XTokenCount++;
467                         intValueSeek++;
468                         
469                 
470                 }
471                 
472                 // Reset the variables.
473                 
474                 QuoteMode = FALSE;
475                 PropertyFind = TRUE;
476                 ExtraLineSeek = TRUE;
477                 ContactLineLen = 0;
478                 QuoteBreakPoint = 0;
479                 ContactLine.Clear();
480                 wxSProperty.Clear();    
481         
482         }
483         
484         FMTimer.SetFilename(Filename);
485         FMTimer.Start(10000, FALSE);
486         
487         EditMode = TRUE;
488         
489         return TRUE;
492 void frmContactEditor::SplitValues(wxString *PropertyLine, 
493         std::map<int,int> *SplitPoints, 
494         std::map<int,int> *SplitLength, 
495         int intSize){
496         
497         size_t intPropertyLen = PropertyLine->Len();
498         int intSplitsFound = 0;
499         int intSplitSize = 0;
500         int intSplitSeek = 0;
501         
502         for (int i = intSize; i <= intPropertyLen; i++){
504                 intSplitSize++;
505         
506                 if (PropertyLine->Mid(i, 1) == wxT(";") &&
507                     PropertyLine->Mid((i - 1), 1) != wxT("\\")){
508            
509                     if (intSplitsFound == 0){
510             
511                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
512           
513                     } else {
514            
515                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
516             
517                     }
518             
519                     SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
520             
521                     intSplitsFound++;
522                     intSplitSeek = i;
523                     intSplitSize = 0;
524             
525                 }
527         }
529         if (intSplitsFound == 0){
531                 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
532                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
534         } else {
536                 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
537                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
539         }
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