Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added copyright and license header to the C++ source and header files in the contacte...
[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                         wxPuts(wxSPropertySeg2);
229                         if (wxSPropertySeg2 != wxT("4.0")){
230                                 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
231                                         _("Contact not supported"), wxICON_ERROR);
232                                 this->Close();
233                                 return FALSE;
234                         }
235                         
236                         VersionProcessed = TRUE;
237                 
238                 } if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
239                 
240                         // See frmContactEditor-LoadGroup.cpp
241                 
242                         LoadKind(wxSPropertySeg2);
243                 
244                 } else if (wxSProperty == wxT("MEMBER")){
246                         // See frmContactEditor-LoadGroup.cpp
248                         LoadMember(wxSPropertySeg2, &GroupCount);               
249                 
250                 } else if (wxSProperty == wxT("FN")){
251                 
252                         // See frmContactEditor-LoadName.cpp
253                 
254                         LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
255                 
256                 } else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
257                 
258                         // See frmContactEditor-LoadName.cpp
259                 
260                         LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
261                 
262                 } else if (wxSProperty == wxT("NICKNAME")){
263                         
264                         // See frmContactEditor-LoadNickname.cpp
265                         
266                         LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
267                         
268                 } else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
269                 
270                         // See frmContactEditor-LoadGender.cpp
271                 
272                         LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
273                 
274                 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
276                         // See frmContactEditor-LoadBADays.cpp
278                         LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
279                 
280                 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
281                         
282                         // See frmContactEditor-LoadBADays.cpp
283                         
284                         LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
285                 
286                 } else if (wxSProperty == wxT("TZ")){
287                 
288                         // See frmContactEditor-LoadTimeZone.cpp
289                 
290                         LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);       
291                 
292                 } else if (wxSProperty == wxT("ADR")){
293                         
294                         // See frmContactEditor-LoadAddress.cpp
295                 
296                         LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
297                 
298                 } else if (wxSProperty == wxT("EMAIL")){
299                 
300                         // See frmContactEditor-LoadEmail.cpp
301                         
302                         LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);       
303                 
304                 } else if (wxSProperty == wxT("IMPP")){
305                 
306                         // See frmContactEditor-LoadIM.cpp
307                 
308                         LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
309                 
310                 } else if (wxSProperty == wxT("TEL")){
311                 
312                         // See frmContactEditor-LoadTelephone.cpp
313                 
314                         LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
315                 
316                 } else if (wxSProperty == wxT("LANG")){
317                 
318                         // See frmContactEditor-LoadLanguage.cpp
319                         
320                         LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
321                 
322                 } else if (wxSProperty == wxT("GEO")){
323                 
324                         // See frmContactEditor-LoadGeo.cpp
325                         
326                         LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);   
327                 
328                 } else if (wxSProperty == wxT("RELATED")){
329                         
330                         // See fromContactEditor-LoadRelated.cpp
331                         
332                         LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);           
333                 
334                 } else if (wxSProperty == wxT("URL")){
336                         // See frmContactEditor-LoadURL.cpp
337                 
338                         LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
339                 
340                 } else if (wxSProperty == wxT("TITLE")) {
341                 
342                         // See frmContactEditor-LoadTitle.cpp
343                         
344                         LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
345                         
346                 } else if (wxSProperty == wxT("ROLE")) {
348                         // See frmContactEditor-LoadRole.cpp
349                 
350                         LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
351                         
352                 } else if (wxSProperty == wxT("ORG")) {
353                 
354                         // See frmContactEditor-LoadOrg.cpp
355                         
356                         LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
357                         
358                 } else if (wxSProperty == wxT("NOTE")) {
360                         // See frmContactEditor-LoadNote.cpp
362                         LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount); 
363                         
364                 } else if (wxSProperty == wxT("CATEGORIES")) {
365                 
366                         // See frmContactEditor-LoadCategory.cpp
367                 
368                         LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount); 
369                         
370                 } else if (wxSProperty == wxT("PHOTO")) {
371                 
372                         // See frmContactEditor-LoadPhoto.cpp
373                         
374                         LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
376                 } else if (wxSProperty == wxT("LOGO")) {
377                 
378                         // See frmContactEditor-LoadLogo.cpp
379                 
380                         LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
381                         
382                 } else if (wxSProperty == wxT("SOUND")) {
383                 
384                         // See frmContactEditor-LoadSound.cpp
385                         
386                         LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
387                         
388                 } else if (wxSProperty == wxT("CALURI")){
390                         // See frmContactEditor-LoadCalendar.cpp
391                         
392                         LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
393                 
394                 } else if (wxSProperty == wxT("CALADRURI")){
396                         // See frmContactEditor-LoadCalendar.cpp
397                         
398                         LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
399                 
400                 } else if (wxSProperty == wxT("FBURL")){
402                         // See frmContactEditor-LoadCalendar.cpp
404                         LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
406                 } else if (wxSProperty == wxT("KEY")){
407                 
408                         // See frmContactEditor-LoadKey.cpp
409                         
410                         LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
411                 
412                 } else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
413                 
414                         UIDToken = wxSPropertySeg2;
415                         UIDProcessed = TRUE;
416                 
417                 } else if (wxSProperty.Mid(0, 3) == wxT("VND")){
418                 
419                         // Split the Vendor three ways.
420                         
421                         wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
422                         
423                         wxString wxSVNDID;
424                         wxString wxSVNDPropName;
425                         long ListCtrlIndex;                     
427                         while (wSTVendorDetails.HasMoreTokens() == TRUE){
428                         
429                                 wSTVendorDetails.GetNextToken();
430                                 wxSVNDID = wSTVendorDetails.GetNextToken();
431                                 wxSVNDPropName = wSTVendorDetails.GetNextToken();
432                                 break;
433                         
434                         }
435                         
436                         if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
437                         
438                                 // Setup the values for later processing.
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                                 // Add the data to the vendor variables.
445                         
446                                 wxListItem coldata;
447                 
448                                 coldata.SetId(intValueSeek);
449                                 coldata.SetData(intValueSeek);
450                                 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
452                                 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
453                                 
454                                 VendorList.erase(intValueSeek);
455                                 VendorListPEN.erase(intValueSeek);
456                                 VendorListElement.erase(intValueSeek);
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                                 VendorCount++;
463                                 intValueSeek++;
464                         
465                         }       
466                 
467                 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
468                 
469                         long ListCtrlIndex;
470                         
471                         XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
472                         XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
473                         
474                         // Add to the form.
475                         
476                         wxListItem coldata;
477                 
478                         coldata.SetId(intValueSeek);
479                         coldata.SetData(intValueSeek);
480                         coldata.SetText(wxSPropertySeg1.Mid(2));
482                         ListCtrlIndex = lboXToken->InsertItem(coldata);
483                         
484                         XTokenCount++;
485                         intValueSeek++;
486                         
487                 
488                 }
489                 
490                 // Reset the variables.
491                 
492                 QuoteMode = FALSE;
493                 PropertyFind = TRUE;
494                 ExtraLineSeek = TRUE;
495                 ContactLineLen = 0;
496                 QuoteBreakPoint = 0;
497                 ContactLine.Clear();
498                 wxSProperty.Clear();    
499         
500         }
501         
502         FMTimer.SetFilename(Filename);
503         FMTimer.Start(10000, FALSE);
504         
505         EditMode = TRUE;
506         
507         return TRUE;
510 void frmContactEditor::SplitValues(wxString *PropertyLine, 
511         std::map<int,int> *SplitPoints, 
512         std::map<int,int> *SplitLength, 
513         int intSize){
514         
515         size_t intPropertyLen = PropertyLine->Len();
516         int intSplitsFound = 0;
517         int intSplitSize = 0;
518         int intSplitSeek = 0;
519         
520         for (int i = intSize; i <= intPropertyLen; i++){
522                 intSplitSize++;
523         
524                 if (PropertyLine->Mid(i, 1) == wxT(";") &&
525                     PropertyLine->Mid((i - 1), 1) != wxT("\\")){
526            
527                     if (intSplitsFound == 0){
528             
529                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
530           
531                     } else {
532            
533                         SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
534             
535                     }
536             
537                     SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
538             
539                     intSplitsFound++;
540                     intSplitSeek = i;
541                     intSplitSize = 0;
542             
543                 }
545         }
547         if (intSplitsFound == 0){
549                 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
550                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
552         } else {
554                 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
555                 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
557         }
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