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