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 intSplitsFound = 0;
202 int intSplitSize = 0;
203 int intPrevValue = 0;
205 for (int i = 0; i <= intPropertyLen; i++){
209 if (NameDataGet.Mid(i, 1) == wxT(";") && NameDataGet.Mid((i - 1), 1) != wxT("\\")){
212 SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
214 if (intSplitsFound == 4){
216 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
221 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
231 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
232 intiter != SplitPoints.end(); ++intiter){
234 if (intiter->first == 1){
236 // Deal with family name.
238 SLiter = SplitLength.find(1);
240 NameData.Surname = NameDataGet.Mid(0, SLiter->second);
241 intPrevValue = intiter->second;
243 } else if (intiter->first == 2){
245 // Deal with given names.
247 SLiter = SplitLength.find(2);
249 NameData.Forename = NameDataGet.Mid(intPrevValue, SLiter->second);
250 intPrevValue = intiter->second;
253 } else if (intiter->first == 3){
255 // Deal with additional names.
257 SLiter = SplitLength.find(3);
259 NameData.OtherNames = NameDataGet.Mid(intPrevValue, SLiter->second);
260 intPrevValue = intiter->second;
262 } else if (intiter->first == 4){
264 // Deal with honorifix prefixes and suffixes.
265 SLiter = SplitLength.find(4);
267 NameData.Title = NameDataGet.Mid(intPrevValue, SLiter->second);
268 intPrevValue = intiter->second;
269 NameData.Suffix = NameDataGet.Mid(intPrevValue);
280 ArrayvCardOutData vCard::GetByPartial(wxString SettingName){
282 // Get data from the vCard object based on a partial match.
284 ArrayvCardOutData vCardOutData;
285 wxArrayString SettingList;
286 wxString SettingValueCurrent;
287 wxString SettingValue;
290 bool FirstToken = TRUE;
292 SettingNameLen = SettingName.Len();
294 for (int i = 0; i < SettingCount; i++){
296 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
298 SettingValue = SettingValues[i];
301 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
303 if (FirstToken == TRUE){
305 SettingValue.Trim(FALSE);
306 SettingValue.Trim(TRUE);
311 SettingValueCurrent = SettingValues[(i + 1)];
312 SettingValueCurrent.Trim(FALSE);
313 SettingValueCurrent.Trim(TRUE);
315 SettingValue.Append(SettingValueCurrent);
321 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
322 vCardOutData.PropData.Add(SettingNames[SettingNameSeek]);
323 vCardOutData.PropValues.Add(SettingValue);
324 vCardOutData.PropCount++;
329 /*for (int i = 0; i < SettingCount; i++){
330 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
332 SettingValue = SettingValues[i];
335 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
337 SettingValueCurrent = SettingValues[(i + 1)];
338 SettingValueCurrent.Trim(FALSE);
339 SettingValueCurrent.Trim(TRUE);
341 SettingValue.Append(SettingValueCurrent);
347 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
348 vCardOutData.PropData.Add(SettingName);
349 vCardOutData.PropData.Add(SettingValue);
358 wxString vCard::GetById(int id){
360 // Get data from the vCard object based on ID.
367 int vCard::WriteFile(wxString WriteFilename){
369 // Write the vCard to a file using the WriteFilename given.
371 // Open the file and begin writing data into the file.
373 wxString SettingName;
374 wxString SettingValue;
375 wxString SettingLine;
377 SettingCount = SettingNames.GetCount();
380 if (ContactFile.Create(WriteFilename, TRUE, wxS_DEFAULT) == FALSE){
384 for (int i = 0; i < SettingCount; i++){
386 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
388 int SettingLineLen = SettingLine.Len();
392 bool FirstLine = TRUE;
394 // Remember to round down the calculation.
396 while (intSeek < SettingLineLen){
398 if ((intLineSeek == intDivider && FirstLine == TRUE) ||
399 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
401 SettingLine.insert(intSeek, wxT("\r\n "));
402 intSeek = intSeek + 3;
403 SettingLineLen = SettingLineLen + 3;
414 ContactFile.Write(SettingLine);
424 int vCard::LoadFile(wxString LoadFilename){
426 // Load data from a file using the LoadFilename given.
430 wxString wxSContactString;
432 vCardFilename = LoadFilename;
434 // Check if we are using wxWidgets version 2.8 or less and
435 // execute the required command accordingly.
437 #if wxABI_VERSION < 20900
438 ContactFile.Open(LoadFilename.c_str(), wxT("r"));
440 ContactFile.Open(LoadFilename, wxT("r"));
443 if (ContactFile.IsOpened() == FALSE){
449 ContactFile.ReadAll(&wxSContactString, wxConvAuto());
453 ProcessString(&wxSContactString);
459 int vCard::LoadString(wxString ContactData){
461 // Load data from a wxString.
463 ProcessString(&ContactData);
469 void vCard::ProcessString(wxString *ContactDataInc){
471 // Process data from a wxString pointer.
473 // Split the vCards (if there are more than one vCard in the file).
475 wxString ContactLine;
478 bool ExtraLineSeek = FALSE;
479 int QuoteBreakPoint = 0;
481 bool PropertyFind = FALSE;
482 bool QuoteMode = FALSE;
484 wxString wxSPropertyNextLine;
485 wxString wxSProperty;
486 wxString wxSPropertySeg1;
487 wxString wxSPropertySeg2;
489 bool FoundBegin = FALSE;
490 bool FirstContact = TRUE;
491 wxString FirstContactData;
492 wxString ContactData;
493 int ContactCount = 0;
495 wxStringTokenizer wSTContactFileLines(*ContactDataInc, wxT("\r\n"));
497 while(wSTContactFileLines.HasMoreTokens() == TRUE){
499 ContactLine = wSTContactFileLines.GetNextToken();
501 if (ContactLine == wxT("BEGIN:VCARD")){
503 if (FoundBegin == TRUE){
505 // No END:VCARD was found so discard current data.
509 if (FirstContact == TRUE){
511 FirstContactData.Clear();
519 FirstContactData.Append(ContactLine + wxT("\r\n"));
520 ContactData.Append(ContactLine + wxT("\r\n"));
522 } else if (ContactLine == wxT("END:VCARD") && FoundBegin == TRUE){
524 if (FirstContact == TRUE){
526 FirstContact = FALSE;
527 FirstContactData.Append(ContactLine + wxT("\r\n"));
531 ContactData.Append(ContactLine + wxT("\r\n"));
533 Cards.insert(std::make_pair(ContactCount, ContactData));
537 } else if (FoundBegin == TRUE){
539 if (FirstContact == TRUE){
541 FirstContactData.Append(ContactLine + wxT("\r\n"));
545 ContactData.Append(ContactLine + wxT("\r\n"));
555 std::map<int, wxString> ContactFileLines;
556 std::map<int, wxString>::iterator striter;
558 wxStringTokenizer wSTFirstContactLines(FirstContactData, wxT("\r\n"));
560 int ContactLineSeek = 0;
562 while (wSTFirstContactLines.HasMoreTokens() == TRUE){
564 ContactLine = wSTFirstContactLines.GetNextToken();
565 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
570 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
571 iter != ContactFileLines.end(); ++iter){
573 // Find the colon which splits the start bit from the data part.
575 ContactLine = iter->second;
577 while (ExtraLineSeek == TRUE){
579 // Check if there is extra data on the next line
580 // (indicated by space or tab at the start) and add data.
584 if (iter == ContactFileLines.end()){
591 wxSPropertyNextLine = iter->second;
593 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
595 wxSPropertyNextLine.Remove(0, 1);
596 //wxSPropertyNextLine.Trim(FALSE);
597 //ContactLine.Trim();
598 ContactLine.Append(wxSPropertyNextLine);
603 ExtraLineSeek = FALSE;
609 ContactLineLen = ContactLine.Len();
611 // Make sure we are not in quotation mode.
612 // Make sure colon does not have \ or \\ before it.
614 for (int i = 0; i <= ContactLineLen; i++){
616 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
618 PropertyFind = FALSE;
620 } else if (PropertyFind == TRUE){
622 wxSProperty.Append(ContactLine.Mid(i, 1));
626 if (ContactLine.Mid(i, 1) == wxT("\"")){
628 if (QuoteMode == TRUE){
640 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
649 // Split that line at the point into two variables (ignore the colon).
651 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
652 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
654 // Insert both into the vCard data file.
656 AddRaw(wxSPropertySeg1, wxSPropertySeg2);
660 ExtraLineSeek = TRUE;
671 wxString vCard::WriteString(){
673 // Write the vCard file into a wxString.
675 // Open the file and begin writing data into the file.
677 wxString SettingName;
678 wxString SettingValue;
679 wxString SettingLine;
680 wxString SettingFinal;
682 SettingCount = SettingNames.GetCount();
684 for (int i = 0; i < SettingCount; i++){
686 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
688 int SettingLineLen = SettingLine.Len();
692 bool FirstLine = TRUE;
694 // Remember to round down the calculation.
696 while (intSeek < SettingLineLen){
698 if ((intLineSeek == intDivider && FirstLine == TRUE) ||
699 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
701 SettingLine.insert(intSeek, wxT("\r\n "));
702 intSeek = intSeek + 3;
703 SettingLineLen = SettingLineLen + 3;
714 SettingFinal.Append(SettingLine);
722 bool vCard::MeetBaseSpecification(){
724 // Check and see if the vCard object meets the base specification
727 if (vCardBegin == TRUE && vCardEnd == TRUE && vCardFN == TRUE &&
728 vCardVersion == 4.0){
740 wxString vCard::Convert(wxString SettingValue, bool ReplaceMode){
742 // Check for backslashes used for commas, newlines and
743 // backslashes used for values.
745 if (ReplaceMode == TRUE){
747 SettingValue.Replace(wxT("\\n"), wxT("\n"));
748 SettingValue.Replace(wxT("\\,"), wxT(","));
749 SettingValue.Replace(wxT("\\;"), wxT(";"));
750 SettingValue.Replace(wxT("\\\\"), wxT("\\"));
754 SettingValue.Replace(wxT("\\"), wxT("\\\\"));
755 SettingValue.Replace(wxT("\n"), wxT("\\n"));
756 SettingValue.Replace(wxT(","), wxT("\\,"));
757 SettingValue.Replace(wxT(";"), wxT("\\;"));
758 SettingValue = SettingValue + wxT("\n");
766 wxString vCard::GetFilename(){
768 // Get the filename associated with the vCard object.
770 return vCardFilename;
774 std::map<int,wxString>* vCard::GetAllCards(){
776 // Get all of vCards within the vCard object.