4 #include <wx/tokenzr.h>
5 #include <wx/datetime.h>
8 #include "frmContactEditor.h"
11 #include "../version.h"
12 #include "../vcard/vcard.h"
13 #include "../common/textprocessing.h"
14 #include "../common/dirs.h"
16 bool frmContactEditor::LoadContact(wxString Filename){
18 // Load the contact into the contact editor.
21 wxString wxSContactString;
25 if (StartupEditMode == FALSE){
26 XVMData = MainPtr->GetViewMode();
29 wxSContactFilename = Filename;
31 // Check if we are using wxWidgets version 2.8 or less and
32 // execute the required command accordingly.
34 #if wxABI_VERSION < 20900
35 ContactFile.Open(Filename.c_str(), wxT("r"));
37 ContactFile.Open(Filename, wxT("r"));
40 if (ContactFile.IsOpened() == FALSE){
46 ContactFile.ReadAll(&wxSContactString, wxConvAuto());
50 std::map<int, wxString> ContactFileLines;
51 std::map<int, wxString>::iterator striter;
53 wxStringTokenizer wSTContactFileLines(wxSContactString, wxT("\r\n"));
55 int ContactLineSeek = 0;
57 while (wSTContactFileLines.HasMoreTokens() == TRUE){
59 ContactLine = wSTContactFileLines.GetNextToken();
60 ContactFileLines.insert(std::make_pair(ContactLineSeek, ContactLine));
67 bool QuoteMode = FALSE;
68 bool PropertyFind = TRUE;
69 bool HasExtraNicknames = FALSE;
70 bool IgnoreGender = FALSE;
71 bool ExtraLineSeek = TRUE;
72 bool BirthdayProcessed = FALSE;
73 bool AnniversaryProcessed = FALSE;
74 bool FNProcessed = FALSE;
75 bool GenderProcessed = FALSE;
76 bool NameProcessed = FALSE;
77 bool UIDProcessed = FALSE;
78 bool KindProcessed = FALSE;
79 bool ETagFound = FALSE;
80 bool ETagOrigFound = FALSE;
81 bool VersionProcessed = FALSE;
82 int intExtraNickname = 0;
84 wxString wxSPropertySeg1;
85 wxString wxSPropertySeg2;
86 wxString wxSPropertyNextLine;
87 size_t ContactLineLen = 0;
88 int QuoteBreakPoint = 0;
91 int NicknameCount = 0;
100 int RelatedCount = 0;
105 int CategoryCount = 0;
111 int CalReqAdrCount = 0;
112 int FreeBusyCount = 0;
116 //int intValueSeek = 1;
118 for (std::map<int,wxString>::iterator iter = ContactFileLines.begin();
119 iter != ContactFileLines.end(); ++iter){
121 // Find the colon which splits the start bit from the data part.
123 ContactLine = iter->second;
125 while (ExtraLineSeek == TRUE){
127 // Check if there is extra data on the next line
128 // (indicated by space or tab at the start) and add data.
132 if (iter == ContactFileLines.end()){
139 wxSPropertyNextLine = iter->second;
142 if (wxSPropertyNextLine.Mid(0, 1) == wxT(" ") || wxSPropertyNextLine.Mid(0, 1) == wxT("\t")){
144 wxSPropertyNextLine.Remove(0, 1);
145 //wxSPropertyNextLine.Trim(FALSE);
146 //ContactLine.Trim();
147 ContactLine.Append(wxSPropertyNextLine);
152 ExtraLineSeek = FALSE;
158 ContactLineLen = ContactLine.Len();
160 // Make sure we are not in quotation mode.
161 // Make sure colon does not have \ or \\ before it.
163 for (int i = 0; i <= ContactLineLen; i++){
165 if ((ContactLine.Mid(i, 1) == wxT(";") || ContactLine.Mid(i, 1) == wxT(":")) && PropertyFind == TRUE){
167 PropertyFind = FALSE;
169 } else if (PropertyFind == TRUE){
171 wxSProperty.Append(ContactLine.Mid(i, 1));
175 if (ContactLine.Mid(i, 1) == wxT("\"")){
177 if (QuoteMode == TRUE){
189 if (ContactLine.Mid(i, 1) == wxT(":") && ContactLine.Mid((i - 1), 1) != wxT("\\") && QuoteMode == FALSE){
198 // Split that line at the point into two variables (ignore the colon).
200 wxSPropertySeg1 = ContactLine.Mid(0, QuoteBreakPoint);
201 wxSPropertySeg2 = ContactLine.Mid((QuoteBreakPoint + 1));
203 // Add the data into the contact editor depending on what it is.
205 if (wxSProperty == wxT("VERSION") && VersionProcessed == FALSE){
207 // Check if version is 4.0, otherwise don't
210 wxPuts(wxSPropertySeg2);
211 if (wxSPropertySeg2 != wxT("4.0")){
212 wxMessageBox(_("This file is not a vCard 4.0 contact and is not supported under Xestia Address Book."),
213 _("Contact not supported"), wxICON_ERROR);
218 VersionProcessed = TRUE;
220 } if (wxSProperty == wxT("KIND") && KindProcessed == FALSE){
222 // See frmContactEditor-LoadGroup.cpp
224 LoadKind(wxSPropertySeg2);
226 } else if (wxSProperty == wxT("MEMBER")){
228 // See frmContactEditor-LoadGroup.cpp
230 LoadMember(wxSPropertySeg2, &GroupCount);
232 } else if (wxSProperty == wxT("FN")){
234 // See frmContactEditor-LoadName.cpp
236 LoadFN(wxSPropertySeg1, wxSPropertySeg2, &FNCount, &FNProcessed, &ContactData);
238 } else if (wxSProperty == wxT("N") && NameProcessed == FALSE){
240 // See frmContactEditor-LoadName.cpp
242 LoadN(wxSPropertySeg1, wxSPropertySeg2, &NameProcessed, &ContactData);
244 } else if (wxSProperty == wxT("NICKNAME")){
246 // See frmContactEditor-LoadNickname.cpp
248 LoadNickname(wxSPropertySeg1, wxSPropertySeg2, &NicknameCount, &ContactData);
250 } else if (wxSProperty == wxT("GENDER") && GenderProcessed == FALSE){
252 // See frmContactEditor-LoadGender.cpp
254 LoadGender(wxSPropertySeg1, wxSPropertySeg2, &GenderProcessed, &ContactData);
256 } else if (wxSProperty == wxT("BDAY") && BirthdayProcessed == FALSE){
258 // See frmContactEditor-LoadBADays.cpp
260 LoadBDay(wxSPropertySeg1, wxSPropertySeg2, &BirthdayProcessed);
262 } else if (wxSProperty == wxT("ANNIVERSARY") && AnniversaryProcessed == FALSE){
264 // See frmContactEditor-LoadBADays.cpp
266 LoadAnniversary(wxSPropertySeg1, wxSPropertySeg2, &AnniversaryProcessed);
268 } else if (wxSProperty == wxT("TZ")){
270 // See frmContactEditor-LoadTimeZone.cpp
272 LoadTimeZone(wxSPropertySeg1, wxSPropertySeg2, &TZCount);
274 } else if (wxSProperty == wxT("ADR")){
276 // See frmContactEditor-LoadAddress.cpp
278 LoadADR(wxSPropertySeg1, wxSPropertySeg2, &ADRCount);
280 } else if (wxSProperty == wxT("EMAIL")){
282 // See frmContactEditor-LoadEmail.cpp
284 LoadEmail(wxSPropertySeg1, wxSPropertySeg2, &EmailCount);
286 } else if (wxSProperty == wxT("IMPP")){
288 // See frmContactEditor-LoadIM.cpp
290 LoadIM(wxSPropertySeg1, wxSPropertySeg2, &IMPPCount);
292 } else if (wxSProperty == wxT("TEL")){
294 // See frmContactEditor-LoadTelephone.cpp
296 LoadTelephone(wxSPropertySeg1, wxSPropertySeg2, &TelCount);
298 } else if (wxSProperty == wxT("LANG")){
300 // See frmContactEditor-LoadLanguage.cpp
302 LoadLanguage(wxSPropertySeg1, wxSPropertySeg2, &LangCount);
304 } else if (wxSProperty == wxT("GEO")){
306 // See frmContactEditor-LoadGeo.cpp
308 LoadGeo(wxSPropertySeg1, wxSPropertySeg2, &GeoCount);
310 } else if (wxSProperty == wxT("RELATED")){
312 // See fromContactEditor-LoadRelated.cpp
314 LoadRelated(wxSPropertySeg1, wxSPropertySeg2, &RelatedCount);
316 } else if (wxSProperty == wxT("URL")){
318 // See frmContactEditor-LoadURL.cpp
320 LoadURL(wxSPropertySeg1, wxSPropertySeg2, &URLCount);
322 } else if (wxSProperty == wxT("TITLE")) {
324 // See frmContactEditor-LoadTitle.cpp
326 LoadTitle(wxSPropertySeg1, wxSPropertySeg2, &TitleCount);
328 } else if (wxSProperty == wxT("ROLE")) {
330 // See frmContactEditor-LoadRole.cpp
332 LoadRole(wxSPropertySeg1, wxSPropertySeg2, &RoleCount);
334 } else if (wxSProperty == wxT("ORG")) {
336 // See frmContactEditor-LoadOrg.cpp
338 LoadOrg(wxSPropertySeg1, wxSPropertySeg2, &OrgCount);
340 } else if (wxSProperty == wxT("NOTE")) {
342 // See frmContactEditor-LoadNote.cpp
344 LoadNote(wxSPropertySeg1, wxSPropertySeg2, &NoteCount);
346 } else if (wxSProperty == wxT("CATEGORIES")) {
348 // See frmContactEditor-LoadCategory.cpp
350 LoadCategory(wxSPropertySeg1, wxSPropertySeg2, &CategoryCount);
352 } else if (wxSProperty == wxT("PHOTO")) {
354 // See frmContactEditor-LoadPhoto.cpp
356 LoadPhoto(wxSPropertySeg1, wxSPropertySeg2, &PhotoCount);
358 } else if (wxSProperty == wxT("LOGO")) {
360 // See frmContactEditor-LoadLogo.cpp
362 LoadLogo(wxSPropertySeg1, wxSPropertySeg2, &LogoCount);
364 } else if (wxSProperty == wxT("SOUND")) {
366 // See frmContactEditor-LoadSound.cpp
368 LoadSound(wxSPropertySeg1, wxSPropertySeg2, &SoundCount);
370 } else if (wxSProperty == wxT("CALURI")){
372 // See frmContactEditor-LoadCalendar.cpp
374 LoadCalURI(wxSPropertySeg1, wxSPropertySeg2, &CalAdrCount);
376 } else if (wxSProperty == wxT("CALADRURI")){
378 // See frmContactEditor-LoadCalendar.cpp
380 LoadCalAdrURI(wxSPropertySeg1, wxSPropertySeg2, &CalReqAdrCount);
382 } else if (wxSProperty == wxT("FBURL")){
384 // See frmContactEditor-LoadCalendar.cpp
386 LoadCalFreeBusy(wxSPropertySeg1, wxSPropertySeg2, &FreeBusyCount);
388 } else if (wxSProperty == wxT("KEY")){
390 // See frmContactEditor-LoadKey.cpp
392 LoadKey(wxSPropertySeg1, wxSPropertySeg2, &KeyCount);
394 } else if (wxSProperty == wxT("UID") && UIDProcessed == FALSE){
396 UIDToken = wxSPropertySeg2;
399 } else if (wxSProperty.Mid(0, 3) == wxT("VND")){
401 // Split the Vendor three ways.
403 wxStringTokenizer wSTVendorDetails(wxSPropertySeg1, wxT("-"));
406 wxString wxSVNDPropName;
409 while (wSTVendorDetails.HasMoreTokens() == TRUE){
411 wSTVendorDetails.GetNextToken();
412 wxSVNDID = wSTVendorDetails.GetNextToken();
413 wxSVNDPropName = wSTVendorDetails.GetNextToken();
418 if (!wxSVNDID.IsEmpty() && !wxSVNDPropName.IsEmpty()){
420 // Setup the values for later processing.
422 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
423 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
424 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
426 // Add the data to the vendor variables.
430 coldata.SetId(intValueSeek);
431 coldata.SetData(intValueSeek);
432 coldata.SetText(wxSVNDID + wxT("-") + wxSVNDPropName);
434 ListCtrlIndex = lboVendorNamespace->InsertItem(coldata);
436 VendorList.erase(intValueSeek);
437 VendorListPEN.erase(intValueSeek);
438 VendorListElement.erase(intValueSeek);
440 VendorList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
441 VendorListPEN.insert(std::make_pair(intValueSeek, wxSVNDID));
442 VendorListElement.insert(std::make_pair(intValueSeek, wxSVNDPropName));
449 } else if (wxSProperty.Mid(0, 2) == wxT("X-")){
453 XTokenList.insert(std::make_pair(intValueSeek, wxSPropertySeg2));
454 XTokenListTokens.insert(std::make_pair(intValueSeek, wxSPropertySeg1.Mid(2)));
460 coldata.SetId(intValueSeek);
461 coldata.SetData(intValueSeek);
462 coldata.SetText(wxSPropertySeg1.Mid(2));
464 ListCtrlIndex = lboXToken->InsertItem(coldata);
472 // Reset the variables.
476 ExtraLineSeek = TRUE;
484 FMTimer.SetFilename(Filename);
485 FMTimer.Start(10000, FALSE);
492 void frmContactEditor::SplitValues(wxString *PropertyLine,
493 std::map<int,int> *SplitPoints,
494 std::map<int,int> *SplitLength,
497 size_t intPropertyLen = PropertyLine->Len();
498 int intSplitsFound = 0;
499 int intSplitSize = 0;
500 int intSplitSeek = 0;
502 for (int i = intSize; i <= intPropertyLen; i++){
506 if (PropertyLine->Mid(i, 1) == wxT(";") &&
507 PropertyLine->Mid((i - 1), 1) != wxT("\\")){
509 if (intSplitsFound == 0){
511 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize)));
515 SplitLength->insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
519 SplitPoints->insert(std::make_pair(intSplitsFound, (i + 1)));
529 if (intSplitsFound == 0){
531 SplitPoints->insert(std::make_pair(intSplitsFound, (8 + 1)));
532 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));
536 SplitPoints->insert(std::make_pair(intSplitsFound, (intSplitSeek + 1)));
537 SplitLength->insert(std::make_pair(intSplitsFound, intSplitSize));