1 // vcard.cpp - vCard Object
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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/>
22 #include <wx/tokenzr.h>
25 // vcard.cpp - Deals with vCard 4.0 formatted files meeting the
26 // RFC 6350 specification.
30 // Setup the vCard object.
40 void vCard::Add(wxString SettingName, wxString SettingValue, bool ReplaceMode){
42 // Add data to vCard object.
44 // Check for backslashes used for commas, newlines and
45 // backslashes used for values.
47 if (ReplaceMode == TRUE){
49 SettingValue.Replace(wxT("\\n"), wxT("\n"));
50 SettingValue.Replace(wxT("\\,"), wxT(","));
51 SettingValue.Replace(wxT("\\:"), wxT(":"));
52 SettingValue.Replace(wxT("\\\\"), wxT("\\"));
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");
64 // Check data to make sure that it meets the required
65 // vCard 4.0 specifications.
67 if (SettingName == wxT("BEGIN") && SettingValue == wxT("VCARD")){
71 if (SettingName == wxT("END") && SettingValue == wxT("VCARD")){
75 if (SettingName.Mid(0,2) == wxT("FN")){
79 if (SettingName == wxT("VERSION") && SettingValue == wxT("4.0")){
83 if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){
87 if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){
93 if (SettingValue.Right(2) != wxT("\r\n")){
95 SettingValue.Append(wxT("\r\n"));
99 SettingNames.Add(SettingName, 1);
100 SettingValues.Add(SettingValue, 1);
106 void vCard::AddRaw(wxString SettingName, wxString SettingValue){
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.
113 if (SettingName == wxT("BEGIN") && SettingValue == wxT("VCARD")){
117 if (SettingName == wxT("END") && SettingValue == wxT("VCARD")){
121 if (SettingName.Mid(0,2) == wxT("FN")){
125 if (SettingName == wxT("VERSION") && SettingValue == wxT("4.0")){
129 if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){
133 if (SettingName == wxT("VERSION") && SettingValue == wxT("3.0")){
139 if (SettingValue.Right(2) != wxT("\r\n")){
141 SettingValue.Append(wxT("\r\n"));
145 SettingNames.Add(SettingName, 1);
146 SettingValues.Add(SettingValue, 1);
152 wxString vCard::Get(wxString SettingName){
154 // Get values from the vCard object.
156 wxString SettingValue;
158 // Look for the setting name.
160 for (int i = 0; i < SettingCount; i++){
162 if (SettingNames[i] == SettingName){
164 SettingValue = SettingValues[i];
165 SettingValue.Trim(TRUE);
167 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
170 SettingValue.Append(SettingValues[(i + 1)]);
182 return wxEmptyString;
186 vCardName vCard::GetName(){
188 // Get the name from the vCard object.
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;
198 // Process the name data to get the required information.
200 int intPropertyLen = NameDataGet.Len();
201 int intSplitSeek = 0;
202 int intSplitsFound = 0;
203 int intSplitSize = 0;
204 int intPrevValue = 0;
206 for (int i = 0; i <= intPropertyLen; i++){
210 if (NameDataGet.Mid(i, 1) == wxT(";") && NameDataGet.Mid((i - 1), 1) != wxT("\\")){
213 SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
215 if (intSplitsFound == 4){
217 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
222 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
232 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
233 intiter != SplitPoints.end(); ++intiter){
235 if (intiter->first == 1){
237 // Deal with family name.
239 SLiter = SplitLength.find(1);
241 NameData.Surname = NameDataGet.Mid(0, SLiter->second);
242 intPrevValue = intiter->second;
244 } else if (intiter->first == 2){
246 // Deal with given names.
248 SLiter = SplitLength.find(2);
250 NameData.Forename = NameDataGet.Mid(intPrevValue, SLiter->second);
251 intPrevValue = intiter->second;
254 } else if (intiter->first == 3){
256 // Deal with additional names.
258 SLiter = SplitLength.find(3);
260 NameData.OtherNames = NameDataGet.Mid(intPrevValue, SLiter->second);
261 intPrevValue = intiter->second;
263 } else if (intiter->first == 4){
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);
281 ArrayvCardOutData vCard::GetByPartial(wxString SettingName){
283 // Get data from the vCard object based on a partial match.
285 ArrayvCardOutData vCardOutData;
286 wxArrayString SettingList;
287 wxString SettingValueCurrent;
288 wxString SettingValue;
291 bool FirstToken = TRUE;
293 SettingNameLen = SettingName.Len();
295 for (int i = 0; i < SettingCount; i++){
297 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
299 SettingValue = SettingValues[i];
302 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
304 if (FirstToken == TRUE){
306 SettingValue.Trim(FALSE);
307 SettingValue.Trim(TRUE);
312 SettingValueCurrent = SettingValues[(i + 1)];
313 SettingValueCurrent.Trim(FALSE);
314 SettingValueCurrent.Trim(TRUE);
316 SettingValue.Append(SettingValueCurrent);
322 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
323 vCardOutData.PropData.Add(SettingNames[SettingNameSeek]);
324 vCardOutData.PropValues.Add(SettingValue);
325 vCardOutData.PropCount++;
330 /*for (int i = 0; i < SettingCount; i++){
331 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
333 SettingValue = SettingValues[i];
336 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
338 SettingValueCurrent = SettingValues[(i + 1)];
339 SettingValueCurrent.Trim(FALSE);
340 SettingValueCurrent.Trim(TRUE);
342 SettingValue.Append(SettingValueCurrent);
348 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
349 vCardOutData.PropData.Add(SettingName);
350 vCardOutData.PropData.Add(SettingValue);
359 wxString vCard::GetById(int id){
361 // Get data from the vCard object based on ID.
368 int vCard::WriteFile(wxString WriteFilename){
370 // Write the vCard to a file using the WriteFilename given.
372 // Open the file and begin writing data into the file.
374 wxString SettingName;
375 wxString SettingValue;
376 wxString SettingLine;
378 SettingCount = SettingNames.GetCount();
381 if (ContactFile.Create(WriteFilename, TRUE, wxS_DEFAULT) == FALSE){
385 for (int i = 0; i < SettingCount; i++){
387 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
389 int SettingLineLen = SettingLine.Len();
391 int intTimes = floor((SettingLine.Len() / intDivider));
395 bool FirstLine = TRUE;
397 // Remember to round down the calculation.
399 while (intSeek < SettingLineLen){
401 if ((intLineSeek == intDivider && FirstLine == TRUE) ||
402 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
404 SettingLine.insert(intSeek, wxT("\r\n "));
405 intSeek = intSeek + 3;
406 SettingLineLen = SettingLineLen + 3;
408 intPrevLine = intSeek;
418 ContactFile.Write(SettingLine);
428 int vCard::LoadFile(wxString LoadFilename){
430 // Load data from a file using the LoadFilename given.
434 wxString wxSContactString;
436 vCardFilename = LoadFilename;
438 // Check if we are using wxWidgets version 2.8 or less and
439 // execute the required command accordingly.
441 #if wxABI_VERSION < 20900
442 ContactFile.Open(LoadFilename.c_str(), wxT("r"));
444 ContactFile.Open(LoadFilename, wxT("r"));
447 if (ContactFile.IsOpened() == FALSE){
453 ContactFile.ReadAll(&wxSContactString, wxConvAuto());
457 ProcessString(&wxSContactString);
463 int vCard::LoadString(wxString ContactData){
465 // Load data from a wxString.
467 ProcessString(&ContactData);
473 void vCard::ProcessString(wxString *ContactDataInc){
475 // Process data from a wxString pointer.
477 // Split the vCards (if there are more than one vCard in the file).
479 wxString ContactLine;
482 bool ExtraLineSeek = FALSE;
483 int QuoteBreakPoint = 0;
485 bool PropertyFind = FALSE;
486 bool QuoteMode = FALSE;
488 wxString wxSPropertyNextLine;
489 wxString wxSProperty;
490 wxString wxSPropertySeg1;
491 wxString wxSPropertySeg2;
493 bool FoundBegin = FALSE;
494 bool FoundEnd = FALSE;
495 bool FirstContact = TRUE;
496 wxString FirstContactData;
497 wxString ContactData;
498 int ContactCount = 0;
500 wxStringTokenizer wSTContactFileLines(*ContactDataInc, wxT("\r\n"));
502 while(wSTContactFileLines.HasMoreTokens() == TRUE){
504 ContactLine = wSTContactFileLines.GetNextToken();
506 if (ContactLine == wxT("BEGIN:VCARD")){
508 if (FoundBegin == TRUE){
510 // No END:VCARD was found so discard current data.
514 if (FirstContact == TRUE){
516 FirstContactData.Clear();
524 FirstContactData.Append(ContactLine + wxT("\r\n"));
525 ContactData.Append(ContactLine + wxT("\r\n"));
527 } else if (ContactLine == wxT("END:VCARD") && FoundBegin == TRUE){
529 if (FirstContact == TRUE){
531 FirstContact = FALSE;
532 FirstContactData.Append(ContactLine + wxT("\r\n"));
536 ContactData.Append(ContactLine + wxT("\r\n"));
538 Cards.insert(std::make_pair(ContactCount, ContactData));
542 } else if (FoundBegin == TRUE){
544 if (FirstContact == TRUE){
546 FirstContactData.Append(ContactLine + wxT("\r\n"));
550 ContactData.Append(ContactLine + wxT("\r\n"));
560 std::map<int, wxString> ContactFileLines;
561 std::map<int, wxString>::iterator striter;
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));
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.
580 ContactLine = iter->second;
582 while (ExtraLineSeek == TRUE){
584 // Check if there is extra data on the next line
585 // (indicated by space or tab at the start) and add data.
589 if (iter == ContactFileLines.end()){
596 wxSPropertyNextLine = iter->second;
598 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
600 wxSPropertyNextLine.Remove(0, 1);
601 //wxSPropertyNextLine.Trim(FALSE);
602 //ContactLine.Trim();
603 ContactLine.Append(wxSPropertyNextLine);
608 ExtraLineSeek = FALSE;
614 ContactLineLen = ContactLine.Len();
616 // Make sure we are not in quotation mode.
617 // Make sure colon does not have \ or \\ before it.
619 for (int i = 0; i <= ContactLineLen; i++){
621 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
623 PropertyFind = FALSE;
625 } else if (PropertyFind == TRUE){
627 wxSProperty.Append(ContactLine.Mid(i, 1));
631 if (ContactLine.Mid(i, 1) == wxT("\"")){
633 if (QuoteMode == TRUE){
645 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
654 // Split that line at the point into two variables (ignore the colon).
656 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
657 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
659 // Insert both into the vCard data file.
661 AddRaw(wxSPropertySeg1, wxSPropertySeg2);
665 ExtraLineSeek = TRUE;
676 wxString vCard::WriteString(){
678 // Write the vCard file into a wxString.
680 // Open the file and begin writing data into the file.
682 wxString SettingName;
683 wxString SettingValue;
684 wxString SettingLine;
685 wxString SettingFinal;
687 SettingCount = SettingNames.GetCount();
689 for (int i = 0; i < SettingCount; i++){
691 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
693 int SettingLineLen = SettingLine.Len();
695 int intTimes = floor((SettingLine.Len() / intDivider));
699 bool FirstLine = TRUE;
701 // Remember to round down the calculation.
703 while (intSeek < SettingLineLen){
705 if ((intLineSeek == intDivider && FirstLine == TRUE) ||
706 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
708 SettingLine.insert(intSeek, wxT("\r\n "));
709 intSeek = intSeek + 3;
710 SettingLineLen = SettingLineLen + 3;
712 intPrevLine = intSeek;
722 SettingFinal.Append(SettingLine);
730 bool vCard::MeetBaseSpecification(){
732 // Check and see if the vCard object meets the base specification
735 if (vCardBegin == TRUE && vCardEnd == TRUE && vCardFN == TRUE &&
736 vCardVersion == 4.0){
748 wxString vCard::Convert(wxString SettingValue, bool ReplaceMode){
750 // Check for backslashes used for commas, newlines and
751 // backslashes used for values.
753 if (ReplaceMode == TRUE){
755 SettingValue.Replace(wxT("\\n"), wxT("\n"));
756 SettingValue.Replace(wxT("\\,"), wxT(","));
757 SettingValue.Replace(wxT("\\;"), wxT(";"));
758 SettingValue.Replace(wxT("\\\\"), wxT("\\"));
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");
774 wxString vCard::GetFilename(){
776 // Get the filename associated with the vCard object.
778 return vCardFilename;
782 std::map<int,wxString>* vCard::GetAllCards(){
784 // Get all of vCards within the vCard object.