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];
194 if (NameArray.PropValues.Count() == 0)
196 // Use FN if there is no N values set.
197 wxString fullName = this->Get(wxT("FN"));
198 NameData.Forename = fullName;
202 wxString NameDataGet = NameArray.PropValues[0];
203 std::map<int, int> SplitPoints;
204 std::map<int, int> SplitLength;
205 std::map<int, int>::iterator SLiter;
207 // Process the name data to get the required information.
209 int intPropertyLen = NameDataGet.Len();
210 int intSplitsFound = 0;
211 int intSplitSize = 0;
212 int intPrevValue = 0;
214 for (int i = 0; i <= intPropertyLen; i++){
218 if (NameDataGet.Mid(i, 1) == wxT(";") && NameDataGet.Mid((i - 1), 1) != wxT("\\")){
221 SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
223 if (intSplitsFound == 4){
225 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
230 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
240 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
241 intiter != SplitPoints.end(); ++intiter){
243 if (intiter->first == 1){
245 // Deal with family name.
247 SLiter = SplitLength.find(1);
249 NameData.Surname = NameDataGet.Mid(0, SLiter->second);
250 intPrevValue = intiter->second;
252 } else if (intiter->first == 2){
254 // Deal with given names.
256 SLiter = SplitLength.find(2);
258 NameData.Forename = NameDataGet.Mid(intPrevValue, SLiter->second);
259 intPrevValue = intiter->second;
262 } else if (intiter->first == 3){
264 // Deal with additional names.
266 SLiter = SplitLength.find(3);
268 NameData.OtherNames = NameDataGet.Mid(intPrevValue, SLiter->second);
269 intPrevValue = intiter->second;
271 } else if (intiter->first == 4){
273 // Deal with honorifix prefixes and suffixes.
274 SLiter = SplitLength.find(4);
276 NameData.Title = NameDataGet.Mid(intPrevValue, SLiter->second);
277 intPrevValue = intiter->second;
278 NameData.Suffix = NameDataGet.Mid(intPrevValue);
289 ArrayvCardOutData vCard::GetByPartial(wxString SettingName){
291 // Get data from the vCard object based on a partial match.
293 ArrayvCardOutData vCardOutData;
294 wxArrayString SettingList;
295 wxString SettingValueCurrent;
296 wxString SettingValue;
299 bool FirstToken = TRUE;
301 SettingNameLen = SettingName.Len();
303 for (int i = 0; i < SettingCount; i++){
305 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
307 SettingValue = SettingValues[i];
310 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
312 if (FirstToken == TRUE){
314 SettingValue.Trim(FALSE);
315 SettingValue.Trim(TRUE);
320 SettingValueCurrent = SettingValues[(i + 1)];
321 SettingValueCurrent.Trim(FALSE);
322 SettingValueCurrent.Trim(TRUE);
324 SettingValue.Append(SettingValueCurrent);
330 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
331 vCardOutData.PropData.Add(SettingNames[SettingNameSeek]);
332 vCardOutData.PropValues.Add(SettingValue);
333 vCardOutData.PropCount++;
338 /*for (int i = 0; i < SettingCount; i++){
339 if (SettingNames[i].Mid(0, SettingNameLen) == SettingName){
341 SettingValue = SettingValues[i];
344 while (SettingValues[(i + 1)].Mid(0, 1) == wxT(" ") || SettingValues[(i + 1)].Mid(0, 1) == wxT("\t")){
346 SettingValueCurrent = SettingValues[(i + 1)];
347 SettingValueCurrent.Trim(FALSE);
348 SettingValueCurrent.Trim(TRUE);
350 SettingValue.Append(SettingValueCurrent);
356 //SettingList.Add(SettingNames[SettingNameSeek] + wxT(":") + SettingValue);
357 vCardOutData.PropData.Add(SettingName);
358 vCardOutData.PropData.Add(SettingValue);
367 wxString vCard::GetById(int id){
369 // Get data from the vCard object based on ID.
376 int vCard::WriteFile(wxString WriteFilename){
378 // Write the vCard to a file using the WriteFilename given.
380 // Open the file and begin writing data into the file.
382 wxString SettingName;
383 wxString SettingValue;
384 wxString SettingLine;
386 SettingCount = SettingNames.GetCount();
389 if (ContactFile.Create(WriteFilename, TRUE, wxS_DEFAULT) == FALSE){
393 for (int i = 0; i < SettingCount; i++){
395 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
397 int SettingLineLen = SettingLine.Len();
401 bool FirstLine = TRUE;
403 // Remember to round down the calculation.
405 while (intSeek < SettingLineLen){
407 if ((intLineSeek == intDivider && FirstLine == TRUE) ||
408 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
410 SettingLine.insert(intSeek, wxT("\r\n "));
411 intSeek = intSeek + 3;
412 SettingLineLen = SettingLineLen + 3;
423 ContactFile.Write(SettingLine);
433 int vCard::LoadFile(wxString LoadFilename){
435 // Load data from a file using the LoadFilename given.
439 wxString wxSContactString;
441 vCardFilename = LoadFilename;
443 // Check if we are using wxWidgets version 2.8 or less and
444 // execute the required command accordingly.
446 #if wxABI_VERSION < 20900
447 ContactFile.Open(LoadFilename.c_str(), wxT("r"));
449 ContactFile.Open(LoadFilename, wxT("r"));
452 if (ContactFile.IsOpened() == FALSE){
458 ContactFile.ReadAll(&wxSContactString, wxConvAuto());
462 ProcessString(&wxSContactString);
468 int vCard::LoadString(wxString ContactData){
470 // Load data from a wxString.
472 ProcessString(&ContactData);
478 void vCard::ProcessString(wxString *ContactDataInc){
480 // Process data from a wxString pointer.
482 // Split the vCards (if there are more than one vCard in the file).
484 wxString ContactLine;
487 bool ExtraLineSeek = FALSE;
488 int QuoteBreakPoint = 0;
490 bool PropertyFind = FALSE;
491 bool QuoteMode = FALSE;
493 wxString wxSPropertyNextLine;
494 wxString wxSProperty;
495 wxString wxSPropertySeg1;
496 wxString wxSPropertySeg2;
498 bool FoundBegin = FALSE;
499 bool FirstContact = TRUE;
500 wxString FirstContactData;
501 wxString ContactData;
502 int ContactCount = 0;
504 wxStringTokenizer wSTContactFileLines(*ContactDataInc, wxT("\r\n"));
506 while(wSTContactFileLines.HasMoreTokens() == TRUE){
508 ContactLine = wSTContactFileLines.GetNextToken();
510 if (ContactLine == wxT("BEGIN:VCARD")){
512 if (FoundBegin == TRUE){
514 // No END:VCARD was found so discard current data.
518 if (FirstContact == TRUE){
520 FirstContactData.Clear();
528 FirstContactData.Append(ContactLine + wxT("\r\n"));
529 ContactData.Append(ContactLine + wxT("\r\n"));
531 } else if (ContactLine == wxT("END:VCARD") && FoundBegin == TRUE){
533 if (FirstContact == TRUE){
535 FirstContact = FALSE;
536 FirstContactData.Append(ContactLine + wxT("\r\n"));
540 ContactData.Append(ContactLine + wxT("\r\n"));
542 Cards.insert(std::make_pair(ContactCount, ContactData));
546 } else if (FoundBegin == TRUE){
548 if (FirstContact == TRUE){
550 FirstContactData.Append(ContactLine + wxT("\r\n"));
554 ContactData.Append(ContactLine + wxT("\r\n"));
564 std::map<int, wxString> ContactFileLines;
565 std::map<int, wxString>::iterator striter;
567 wxStringTokenizer wSTFirstContactLines(FirstContactData, wxT("\r\n"));
569 int ContactLineSeek = 0;
571 while (wSTFirstContactLines.HasMoreTokens() == TRUE){
573 ContactLine = wSTFirstContactLines.GetNextToken();
574 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
579 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
580 iter != ContactFileLines.end(); ++iter){
582 // Find the colon which splits the start bit from the data part.
584 ContactLine = iter->second;
586 while (ExtraLineSeek == TRUE){
588 // Check if there is extra data on the next line
589 // (indicated by space or tab at the start) and add data.
593 if (iter == ContactFileLines.end()){
600 wxSPropertyNextLine = iter->second;
602 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
604 wxSPropertyNextLine.Remove(0, 1);
605 //wxSPropertyNextLine.Trim(FALSE);
606 //ContactLine.Trim();
607 ContactLine.Append(wxSPropertyNextLine);
612 ExtraLineSeek = FALSE;
618 ContactLineLen = ContactLine.Len();
620 // Make sure we are not in quotation mode.
621 // Make sure colon does not have \ or \\ before it.
623 for (int i = 0; i <= ContactLineLen; i++){
625 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
627 PropertyFind = FALSE;
629 } else if (PropertyFind == TRUE){
631 wxSProperty.Append(ContactLine.Mid(i, 1));
635 if (ContactLine.Mid(i, 1) == wxT("\"")){
637 if (QuoteMode == TRUE){
649 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
658 // Split that line at the point into two variables (ignore the colon).
660 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
661 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
663 // Insert both into the vCard data file.
665 AddRaw(wxSPropertySeg1, wxSPropertySeg2);
669 ExtraLineSeek = TRUE;
680 wxString vCard::WriteString(){
682 // Write the vCard file into a wxString.
684 // Open the file and begin writing data into the file.
686 wxString SettingName;
687 wxString SettingValue;
688 wxString SettingLine;
689 wxString SettingFinal;
691 SettingCount = SettingNames.GetCount();
693 for (int i = 0; i < SettingCount; i++){
695 SettingLine = SettingNames[i] + wxT(":") + SettingValues[i];
697 int SettingLineLen = SettingLine.Len();
701 bool FirstLine = TRUE;
703 // Remember to round down the calculation.
705 while (intSeek < SettingLineLen){
707 if ((intLineSeek == intDivider && FirstLine == TRUE) ||
708 (intLineSeek == (intDivider - 1) && FirstLine == FALSE)){
710 SettingLine.insert(intSeek, wxT("\r\n "));
711 intSeek = intSeek + 3;
712 SettingLineLen = SettingLineLen + 3;
723 SettingFinal.Append(SettingLine);
731 bool vCard::MeetBaseSpecification(){
733 // Check and see if the vCard object meets the base specification
736 if (vCardBegin == TRUE && vCardEnd == TRUE && vCardFN == TRUE &&
737 vCardVersion == 4.0){
749 wxString vCard::Convert(wxString SettingValue, bool ReplaceMode){
751 // Check for backslashes used for commas, newlines and
752 // backslashes used for values.
754 if (ReplaceMode == TRUE){
756 SettingValue.Replace(wxT("\\n"), wxT("\n"));
757 SettingValue.Replace(wxT("\\,"), wxT(","));
758 SettingValue.Replace(wxT("\\;"), wxT(";"));
759 SettingValue.Replace(wxT("\\\\"), wxT("\\"));
763 SettingValue.Replace(wxT("\\"), wxT("\\\\"));
764 SettingValue.Replace(wxT("\n"), wxT("\\n"));
765 SettingValue.Replace(wxT(","), wxT("\\,"));
766 SettingValue.Replace(wxT(";"), wxT("\\;"));
767 SettingValue = SettingValue + wxT("\n");
775 wxString vCard::GetFilename(){
777 // Get the filename associated with the vCard object.
779 return vCardFilename;
783 std::map<int,wxString>* vCard::GetAllCards(){
785 // Get all of vCards within the vCard object.