Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Removed unused code from vcard/vcard.cpp
[xestiaab/.git] / source / vcard / vcard.cpp
1 // vcard.cpp - vCard Object
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 "vcard.h"
20 #include <wx/file.h>
21 #include <wx/ffile.h>
22 #include <wx/tokenzr.h>
23 #include <cmath>
25 // vcard.cpp - Deals with vCard 4.0 formatted files meeting the
26 // RFC 6350 specification.
28 vCard::vCard(){
29         
30         // Setup the vCard object.
31         
32         vCardBegin = FALSE;
33         vCardEnd = FALSE;
34         vCardFN = FALSE;
35         vCardVersion = 0.0;
36         SettingCount = 0;
37         
38 }
40 void vCard::Add(wxString SettingName, wxString SettingValue, bool ReplaceMode){  
41     
42         // Add data to vCard object.
43         
44         // Check for backslashes used for commas, newlines and
45         // backslashes used for values.
46     
47         if (ReplaceMode == TRUE){
48     
49                 SettingValue.Replace(wxT("\\n"), wxT("\n"));
50                 SettingValue.Replace(wxT("\\,"), wxT(","));
51                 SettingValue.Replace(wxT("\\:"), wxT(":"));
52                 SettingValue.Replace(wxT("\\\\"), wxT("\\"));
53     
54         } else {
56                 SettingValue.Replace(wxT("\\"), wxT("\\\\"));
57                 SettingValue.Replace(wxT("\n"), wxT("\\n"));
58                 SettingValue.Replace(wxT(","), wxT("\\,"));
59                 SettingValue.Replace(wxT(":"), wxT("\\:"));
60                 SettingValue = SettingValue + wxT("\n");
61     
62         }
63   
64         // Check data to make sure that it meets the required
65         // vCard 4.0 specifications.
66     
67         if (SettingName == wxT("BEGIN") && SettingValue == wxT("VCARD")){
68                 vCardBegin = TRUE;
69         }
70     
71         if (SettingName == wxT("END") && SettingValue == wxT("VCARD")){
72                 vCardEnd = TRUE;
73         }
75         if (SettingName.Mid(0,2) == wxT("FN")){
76                 vCardFN = TRUE;
77         }
78     
79         if (SettingName == wxT("VERSION") && SettingValue == wxT("4.0")){
80                 vCardVersion = 4.0;
81         }
82     
83         if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){
84                 vCardVersion = 3.0;
85         }
86     
87         if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){
88                 vCardVersion = 2.0;
89         }
90     
91         SettingValue.Trim();    
92     
93         if (SettingValue.Right(2) != wxT("\r\n")){
94     
95                 SettingValue.Append(wxT("\r\n"));
96     
97         }
98     
99         SettingNames.Add(SettingName, 1);
100         SettingValues.Add(SettingValue, 1);      
101     
102         ++SettingCount;
103         
106 void vCard::AddRaw(wxString SettingName, wxString SettingValue){  
107   
108         // Add data to the vCard in raw mode.
110         // Check data to make sure that it meets the required
111         // vCard 4.0 specifications.
112         
113         if (SettingName == wxT("BEGIN") && SettingValue == wxT("VCARD")){
114                 vCardBegin = TRUE;
115         }
116     
117         if (SettingName == wxT("END") && SettingValue == wxT("VCARD")){
118                 vCardEnd = TRUE;
119         }
120     
121         if (SettingName.Mid(0,2) == wxT("FN")){
122                 vCardFN = TRUE;
123         }
124     
125         if (SettingName == wxT("VERSION") && SettingValue == wxT("4.0")){   
126                 vCardVersion = 4.0;
127         }
129         if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){   
130                 vCardVersion = 3.0;
131         }
132     
133         if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){   
134                 vCardVersion = 2.0;
135         }
136     
137         SettingValue.Trim();
138     
139         if (SettingValue.Right(2) != wxT("\r\n")){
140     
141                 SettingValue.Append(wxT("\r\n"));
142     
143         }
144         
145         SettingNames.Add(SettingName, 1);
146         SettingValues.Add(SettingValue, 1);      
147     
148         ++SettingCount;
149         
152 wxString vCard::Get(wxString SettingName){
153   
154         // Get values from the vCard object.
155         
156         wxString SettingValue;
157     
158         // Look for the setting name.
160         for (int i = 0; i < SettingCount; i++){
161       
162                 if (SettingNames[i] == SettingName){
163         
164                         SettingValue = SettingValues[i];
165                         SettingValue.Trim(TRUE);
166                     
167                         while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
168                 
169                                 SettingValue.Trim();
170                                 SettingValue.Append(SettingValues[(i + 1)]);
171                 
172                                 i++;
173                         
174                         }
175             
176                         return SettingValue;
177                 
178                 }
179         
180         }
182         return wxEmptyString;
186 vCardName vCard::GetName(){
188         // Get the name from the vCard object.
189         
190         vCardName NameData;
191         ArrayvCardOutData NameArray = this->GetByPartial(wxT("N"));
192         //wxString NameDataGet = NameArray.PropValues[0];
193         wxString NameDataGet = NameArray.PropValues[0];
194         std::map<int, int> SplitPoints;
195         std::map<int, int> SplitLength;
196         std::map<int, int>::iterator SLiter;
197         
198         // Process the name data to get the required information.
199         
200         int intPropertyLen = NameDataGet.Len();
201         int intSplitSeek = 0;
202         int intSplitsFound = 0;
203         int intSplitSize = 0;
204         int intPrevValue = 0;                                   
205         
206         for (int i = 0; i <= intPropertyLen; i++){
208                 intSplitSize++;
209         
210                 if (NameDataGet.Mid(i, 1) == wxT(";") && NameDataGet.Mid((i - 1), 1) != wxT("\\")){
211         
212                         intSplitsFound++;
213                         SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
214                         
215                         if (intSplitsFound == 4){ 
216                         
217                                 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
218                                 break; 
219                                 
220                         } else {
221                         
222                                 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
223                         
224                         }
225                         
226                         intSplitSize = 0;                                       
227         
228                 }
230         }
231         
232         for (std::map<int, int>::iterator intiter = SplitPoints.begin(); 
233         intiter != SplitPoints.end(); ++intiter){
234         
235                 if (intiter->first == 1){
236                 
237                         // Deal with family name.
238                         
239                         SLiter = SplitLength.find(1);
241                         NameData.Surname = NameDataGet.Mid(0, SLiter->second);                          
242                         intPrevValue = intiter->second;
243                 
244                 } else if (intiter->first == 2){
245                 
246                         // Deal with given names.
247                         
248                         SLiter = SplitLength.find(2);
250                         NameData.Forename = NameDataGet.Mid(intPrevValue, SLiter->second);                      
251                         intPrevValue = intiter->second;
253                 
254                 } else if (intiter->first == 3){
255                 
256                         // Deal with additional names.
257                         
258                         SLiter = SplitLength.find(3);
260                         NameData.OtherNames = NameDataGet.Mid(intPrevValue, SLiter->second);
261                         intPrevValue = intiter->second;
262                 
263                 } else if (intiter->first == 4){
264                 
265                         // Deal with honorifix prefixes and suffixes.
266                         SLiter = SplitLength.find(4);
268                         NameData.Title = NameDataGet.Mid(intPrevValue, SLiter->second);                 
269                         intPrevValue = intiter->second;
270                         NameData.Suffix = NameDataGet.Mid(intPrevValue);
271                 
272                 }
273         
274         }
275         
276         return NameData;
277         
281 ArrayvCardOutData vCard::GetByPartial(wxString SettingName){
283         // Get data from the vCard object based on a partial match.
284         
285         ArrayvCardOutData vCardOutData;
286         wxArrayString SettingList;
287         wxString SettingValueCurrent;
288         wxString SettingValue;
289         int SettingNameLen;
290         int SettingNameSeek;
291         bool FirstToken = TRUE;
292     
293         SettingNameLen = SettingName.Len();
294         
295         for (int i = 0; i < SettingCount; i++){
296     
297                 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
298             
299                         SettingValue = SettingValues[i];
300                         SettingNameSeek = i;        
301             
302                 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
303                 
304                         if (FirstToken == TRUE){
305                 
306                                 SettingValue.Trim(FALSE);
307                                 SettingValue.Trim(TRUE);                                
308                                 FirstToken = FALSE;
309                 
310                         }
311                 
312                         SettingValueCurrent = SettingValues[(i + 1)];
313                         SettingValueCurrent.Trim(FALSE);
314                         SettingValueCurrent.Trim(TRUE);         
315                 
316                         SettingValue.Append(SettingValueCurrent);
317                 
318                         i++;
319                 
320                 }
321             
322                 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
323                 vCardOutData.PropData.Add(SettingNames[SettingNameSeek]);
324                 vCardOutData.PropValues.Add(SettingValue);
325                 vCardOutData.PropCount++;
326             
327                 }
328         }
329     
330     /*for (int i = 0; i < SettingCount; i++){
331         if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
332             
333             SettingValue = SettingValues[i];
334             SettingNameSeek = i;            
335             
336             while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
337                 
338                 SettingValueCurrent = SettingValues[(i + 1)];
339                 SettingValueCurrent.Trim(FALSE);
340                 SettingValueCurrent.Trim(TRUE);         
341                 
342                 SettingValue.Append(SettingValueCurrent);
343                 
344                 i++;
345                 
346             }
347             
348             //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
349             vCardOutData.PropData.Add(SettingName);
350             vCardOutData.PropData.Add(SettingValue);
351             
352         }
353     }*/
354     
355         return vCardOutData;
359 wxString vCard::GetById(int id){
360         
361         // Get data from the vCard object based on ID.
362         
363         // Unimplemented.
364         return wxT("");
365         
368 int vCard::WriteFile(wxString WriteFilename){
370         // Write the vCard to a file using the WriteFilename given.
371         
372         // Open the file and begin writing data into the file.
373     
374         wxString SettingName;
375         wxString SettingValue;
376         wxString SettingLine;
377     
378         SettingCount = SettingNames.GetCount();
379     
380         wxFile ContactFile;
381         if (ContactFile.Create(WriteFilename, TRUE, wxS_DEFAULT) == FALSE){
382                 return 1;
383         }
384     
385         for (int i = 0; i < SettingCount; i++){
386     
387                 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
388     
389                 int SettingLineLen = SettingLine.Len();
390                 int intDivider = 74;
391                 int intTimes = floor((SettingLine.Len() / intDivider));
392                 int intSeek = 0;
393                 int intLineSeek = 0;
394                 int intPrevLine;
395                 bool FirstLine = TRUE;
396     
397                 // Remember to round down the calculation.
399                 while (intSeek < SettingLineLen){
400         
401                         if ((intLineSeek == intDivider && FirstLine == TRUE) ||
402                                 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
403                 
404                                 SettingLine.insert(intSeek, wxT("\r\n "));
405                                 intSeek = intSeek + 3;
406                                 SettingLineLen = SettingLineLen + 3;
407                                 intLineSeek = 0;
408                                 intPrevLine = intSeek;
409                                 FirstLine = FALSE;
410                 
411                         }
412         
413                         intSeek++;
414                         intLineSeek++;
415         
416                 }
417         
418                 ContactFile.Write(SettingLine);
419         
420         }
421     
422         ContactFile.Close();
424         return 0;
428 int vCard::LoadFile(wxString LoadFilename){
430         // Load data from a file using the LoadFilename given.
431         
432         wxFFile ContactFile;
433   
434         wxString wxSContactString;
435   
436         vCardFilename = LoadFilename;
437         
438         // Check if we are using wxWidgets version 2.8 or less and
439         // execute the required command accordingly.
440     
441 #if wxABI_VERSION < 20900
442         ContactFile.Open(LoadFilename.c_str(), wxT("r"));
443 #else
444         ContactFile.Open(LoadFilename, wxT("r"));
445 #endif  
446         
447         if (ContactFile.IsOpened() == FALSE){
448         
449                 return 1;
450         
451         }
452         
453         ContactFile.ReadAll(&wxSContactString, wxConvAuto());
454     
455         ContactFile.Close();
456     
457         ProcessString(&wxSContactString);
459         return 0;
463 int vCard::LoadString(wxString ContactData){
465         // Load data from a wxString.
466         
467         ProcessString(&ContactData);
469         return 0;
473 void vCard::ProcessString(wxString *ContactDataInc){
475         // Process data from a wxString pointer.
476         
477         // Split the vCards (if there are more than one vCard in the file).
479         wxString ContactLine;
481         int ContactLineLen;
482         bool ExtraLineSeek = FALSE;
483         int QuoteBreakPoint = 0;
484     
485         bool PropertyFind = FALSE;
486         bool QuoteMode = FALSE;
487    
488         wxString wxSPropertyNextLine;
489         wxString wxSProperty;
490         wxString wxSPropertySeg1;
491         wxString wxSPropertySeg2;
492     
493         bool FoundBegin = FALSE;
494         bool FoundEnd = FALSE;
495         bool FirstContact = TRUE;
496         wxString FirstContactData;
497         wxString ContactData;
498         int ContactCount = 0;
499     
500         wxStringTokenizer wSTContactFileLines(*ContactDataInc, wxT("\r\n"));
501     
502         while(wSTContactFileLines.HasMoreTokens() == TRUE){
503     
504                 ContactLine = wSTContactFileLines.GetNextToken();
505         
506                 if (ContactLine == wxT("BEGIN:VCARD")){
507                 
508                         if (FoundBegin == TRUE){
509                 
510                                 // No END:VCARD was found so discard current data.
511                         
512                                 ContactData.Clear();
513                 
514                                 if (FirstContact == TRUE){
515                         
516                                         FirstContactData.Clear();
517                         
518                                 }
519                 
520                         }
521                 
522                         FoundBegin = TRUE;
523                 
524                         FirstContactData.Append(ContactLine + wxT("\r\n"));
525                         ContactData.Append(ContactLine + wxT("\r\n"));
526                 
527                 } else if (ContactLine == wxT("END:VCARD") && FoundBegin == TRUE){
528         
529                         if (FirstContact == TRUE){
530                 
531                                 FirstContact = FALSE;
532                                 FirstContactData.Append(ContactLine + wxT("\r\n"));
533                 
534                         }
535                 
536                         ContactData.Append(ContactLine + wxT("\r\n"));
537                                 
538                         Cards.insert(std::make_pair(ContactCount, ContactData));
539         
540                         ContactCount++;
541         
542                 } else if (FoundBegin == TRUE){
543         
544                         if (FirstContact == TRUE){
545                 
546                                 FirstContactData.Append(ContactLine + wxT("\r\n"));
547                 
548                         }
549                 
550                         ContactData.Append(ContactLine + wxT("\r\n"));
551         
552                 }
553     
554         }
556         ContactLine.Clear();
557     
558         // Split the lines.
559         
560         std::map<int, wxString> ContactFileLines;
561         std::map<int, wxString>::iterator striter;
562         
563         wxStringTokenizer wSTFirstContactLines(FirstContactData, wxT("\r\n"));
565         int ContactLineSeek = 0;
567         while (wSTFirstContactLines.HasMoreTokens() == TRUE){
569                 ContactLine = wSTFirstContactLines.GetNextToken();
570                 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
571                 ContactLineSeek++;              
572         
573         }
574     
575         for (std::map<int,wxString>::iterator iter = ContactFileLines.begin(); 
576                 iter != ContactFileLines.end(); ++iter){
578                 // Find the colon which splits the start bit from the data part.
579         
580                 ContactLine = iter->second;
581         
582                 while (ExtraLineSeek == TRUE){
583         
584                         // Check if there is extra data on the next line 
585                         // (indicated by space or tab at the start) and add data.
586         
587                         iter++;
588                 
589                         if (iter == ContactFileLines.end()){
590                 
591                                 iter--;
592                                 break;
593                 
594                         }
595         
596                         wxSPropertyNextLine = iter->second;
597                 
598                         if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
599                 
600                                 wxSPropertyNextLine.Remove(0, 1);
601                                 //wxSPropertyNextLine.Trim(FALSE);
602                                 //ContactLine.Trim();
603                                 ContactLine.Append(wxSPropertyNextLine);
604         
605                         } else {
606                 
607                                 iter--;
608                                 ExtraLineSeek = FALSE;
609                 
610                         }
611         
612                 }
614                 ContactLineLen = ContactLine.Len();
615         
616                 // Make sure we are not in quotation mode.
617                 // Make sure colon does not have \ or \\ before it.
618         
619                 for (int i = 0; i <= ContactLineLen; i++){
620         
621                         if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
622                 
623                                 PropertyFind = FALSE;
624                 
625                         } else if (PropertyFind == TRUE){
626                 
627                                 wxSProperty.Append(ContactLine.Mid(i, 1));
628                 
629                         }               
630         
631                         if (ContactLine.Mid(i, 1) == wxT("\"")){
632                 
633                                 if (QuoteMode == TRUE){
634                         
635                                         QuoteMode = FALSE;
636                         
637                                 } else {
638                 
639                                         QuoteMode = TRUE;
640                                 
641                                 }
642                 
643                         }
644                 
645                         if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
646                 
647                                 QuoteBreakPoint = i;
648                                 break;
649                 
650                         }
651         
652                 }
653         
654                 // Split that line at the point into two variables (ignore the colon).
655         
656                 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
657                 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
658         
659                 // Insert both into the vCard data file.
660         
661                 AddRaw(wxSPropertySeg1, wxSPropertySeg2);
662         
663                 QuoteMode = FALSE;
664                 PropertyFind = TRUE;
665                 ExtraLineSeek = TRUE;
666                 ContactLineLen = 0;
667                 QuoteBreakPoint = 0;
668                 ContactLine.Clear();
669                 wxSProperty.Clear();
670         
671         }
676 wxString vCard::WriteString(){
678         // Write the vCard file into a wxString.
679         
680         // Open the file and begin writing data into the file.
681     
682         wxString SettingName;
683         wxString SettingValue;
684         wxString SettingLine;
685         wxString SettingFinal;
686     
687         SettingCount = SettingNames.GetCount();
688     
689         for (int i = 0; i < SettingCount; i++){
690     
691                 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
692     
693                 int SettingLineLen = SettingLine.Len();
694                 int intDivider = 74;
695                 int intTimes = floor((SettingLine.Len() / intDivider));
696                 int intSeek = 0;
697                 int intLineSeek = 0;
698                 int intPrevLine;
699                 bool FirstLine = TRUE;
700     
701                 // Remember to round down the calculation.
703                 while (intSeek < SettingLineLen){
704         
705                         if ((intLineSeek == intDivider && FirstLine == TRUE) ||
706                         (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
707                 
708                                 SettingLine.insert(intSeek, wxT("\r\n "));
709                                 intSeek = intSeek + 3;
710                                 SettingLineLen = SettingLineLen + 3;
711                                 intLineSeek = 0;
712                                 intPrevLine = intSeek;
713                                 FirstLine = FALSE;
714                 
715                         }
716         
717                         intSeek++;
718                         intLineSeek++;
719         
720                 }
721         
722                 SettingFinal.Append(SettingLine);
723         
724         }
725     
726         return SettingFinal;
730 bool vCard::MeetBaseSpecification(){
731         
732         // Check and see if the vCard object meets the base specification
733         // of vCard 4.0.
734     
735         if (vCardBegin == TRUE && vCardEnd == TRUE && vCardFN == TRUE &&
736                 vCardVersion == 4.0){
738                 return TRUE;
740         } else {
742                 return FALSE;
744         }
745     
748 wxString vCard::Convert(wxString SettingValue, bool ReplaceMode){  
749     
750         // Check for backslashes used for commas, newlines and
751         // backslashes used for values.
752     
753         if (ReplaceMode == TRUE){
754     
755                 SettingValue.Replace(wxT("\\n"), wxT("\n"));
756                 SettingValue.Replace(wxT("\\,"), wxT(","));
757                 SettingValue.Replace(wxT("\\;"), wxT(";"));
758                 SettingValue.Replace(wxT("\\\\"), wxT("\\"));
759     
760         } else {
762                 SettingValue.Replace(wxT("\\"), wxT("\\\\"));
763                 SettingValue.Replace(wxT("\n"), wxT("\\n"));
764                 SettingValue.Replace(wxT(","), wxT("\\,"));
765                 SettingValue.Replace(wxT(";"), wxT("\\;"));
766                 SettingValue = SettingValue + wxT("\n");
767     
768         }
769     
770         return SettingValue;
771     
774 wxString vCard::GetFilename(){
776         // Get the filename associated with the vCard object.
777         
778         return vCardFilename;
782 std::map<int,wxString>* vCard::GetAllCards(){
784         // Get all of vCards within the vCard object.
785         
786         return &Cards;
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