1 // frmSearch-process.cpp - Search process subroutines.
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/>
19 #include "frmSearch.h"
20 #include "../common/preferences.h"
21 #include "../common/dirs.h"
22 #include "../vcard/vcard.h"
24 void frmSearch::SearchContactsThread(){
26 // Go through each account and find contacts based on the
29 std::map<wxString,bool>::iterator SAiter;
30 std::map<wxString,wxString>::iterator SAPiter;
31 int intResultFound = 0;
33 for (std::map<wxString, wxString>::iterator SALiter = SearchAccountsNames.begin();
34 SALiter != SearchAccountsNames.end();
37 // Double check that we are searching in this account.
38 // If not, skip to the next one.
40 SAiter = SearchAccounts.find(SALiter->first);
41 SAPiter = SearchAccountsPaths.find(SAiter->first);
44 if (SAiter->second == FALSE){
46 // Not looking in this account. Go to next account.
52 wxCommandEvent sbupdate (SE_SBUPDATE);
54 wxString *SBData = new wxString;
55 *SBData = _("Searching ") + SALiter->second + _("...");
57 sbupdate.SetClientData(SBData);
58 wxPostEvent(this, sbupdate);
60 if (StopMode == FALSE){
62 wxCommandEvent sbstop (SE_SBUPDATE);
64 wxString *SBStopStr = new wxString;
65 *SBStopStr = _("Search stopped.");
67 sbstop.SetClientData(SBStopStr);
68 wxPostEvent(this, sbstop);
73 // Get the list of contact files and process each
76 wxString AccountDir = GetAccountDir(SAPiter->second, FALSE);
78 wxString vcardfilenamewxs;
79 wxStringTokenizer vcardfileline;
81 wxString setname, setvalue;
82 long ContactIndex = 1;
83 long ContactSeekPoint = 0;
84 wxString vCardFilename;
85 wxString vCardFilenameFull;
87 if (wxDirExists(AccountDir)){
89 wxDir vcardaccdir(AccountDir);
91 bool ProcFiles = vcardaccdir.GetFirst(&vCardFilename, wxEmptyString, wxDIR_FILES);
94 if (StopMode == FALSE){
96 wxCommandEvent sbstop(SE_SBUPDATE);
98 wxString *SBStopStr = new wxString;
99 *SBStopStr = _("Search stopped.");
101 sbstop.SetClientData(SBStopStr);
102 wxPostEvent(this, sbstop);
107 if (vCardFilename.Right(4) == wxT(".vcf") ||
108 vCardFilename.Right(4) == wxT(".VCF") ||
109 vCardFilename.Right(5) == wxT(".vcard") ||
110 vCardFilename.Right(5) == wxT(".VCARD")){
113 vCardFilenameFull.Append(AccountDir);
114 vCardFilenameFull.Append(vCardFilename);
116 Person.LoadFile(vCardFilenameFull);
118 vcardfilenamewxs = vCardFilenameFull;
120 // Check if file has version 4.0, first name (FN)
121 // begin and end vCard portions.
123 // Don't search unless these conditions have been met.
125 if (Person.MeetBaseSpecification()){
127 // Go through each of the search settings and check
128 // if they match with contact details.
130 // If it matches then add to the search results.
136 ArrayvCardOutData ArrayData;
137 ArrayvCardOutData NicknameArrayData;
138 vCardName NameArray = Person.GetName();
139 wxString ContactFinalNickname;
140 NicknameArrayData = Person.GetByPartial(wxT("NICKNAME"));
142 if (NicknameArrayData.PropCount != 0){
143 ContactFinalNickname = NicknameArrayData.PropValues[0];
146 for (std::map<int, void*>::iterator siter = SearchFrames.begin();
147 siter != SearchFrames.end(); siter++){
149 // Get the settings from the form.
151 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
153 // Get Selected Option and Setting.
155 SelOpt = XABSPPtr->GetSelectionOption();
159 StrOpt = XABSPPtr->GetStringSetting();
160 StrOptLen = StrOpt.Len();
162 if (SelOpt == 0 || SelOpt == 4 || SelOpt == 8){
164 // Matching string that beings with...
166 // Double check the std::map inserts for each section.
170 // Get the forename from the first N.
172 if (NameArray.Forename.Mid(0, StrOptLen) == StrOpt){
174 if (CheckDuplicate(vcardfilenamewxs,
176 &SearchResultAccount,
177 &SearchResultFilename) == TRUE){
183 ArrayData.PropData.Clear();
184 ArrayData.PropValues.Clear();
185 ArrayData.PropCount = 0;
190 SRNotif *sradd = new SRNotif;
191 sradd->ContactName = NameArray;
192 sradd->ContactNickname = ContactFinalNickname;
193 sradd->ContactAccount = SAPiter->second;
194 sradd->ContactAccountName = SALiter->second;
195 sradd->ContactFullFilename = vcardfilenamewxs;
196 sradd->SearchResultID = intResultFound;
198 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
199 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
203 wxCommandEvent sraddenv(SE_ADDRESULT);
204 sraddenv.SetClientData(sradd);
205 wxPostEvent(this, sraddenv);
209 } else if (SelOpt == 4){
211 if (NameArray.Surname.Mid(0, StrOptLen) == StrOpt){
213 if (CheckDuplicate(vcardfilenamewxs,
215 &SearchResultAccount,
216 &SearchResultFilename) == TRUE){
222 ArrayData.PropData.Clear();
223 ArrayData.PropValues.Clear();
224 ArrayData.PropCount = 0;
229 SRNotif *sradd = new SRNotif;
230 sradd->ContactName = NameArray;
231 sradd->ContactNickname = ContactFinalNickname;
232 sradd->ContactAccount = SAPiter->second;
233 sradd->ContactAccountName = SALiter->second;
234 sradd->ContactFullFilename = vcardfilenamewxs;
235 sradd->SearchResultID = intResultFound;
237 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
238 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
242 wxCommandEvent sraddenv(SE_ADDRESULT);
243 sraddenv.SetClientData(sradd);
244 wxPostEvent(this, sraddenv);
248 } else if (SelOpt == 8){
250 wxString NicknameData;
252 for (int i = 0; i < NicknameArrayData.PropCount; i++){
254 NicknameData = NicknameArrayData.PropValues[i];
256 if (NicknameData.Mid(0, StrOptLen) == StrOpt){
258 if (CheckDuplicate(vcardfilenamewxs,
260 &SearchResultAccount,
261 &SearchResultFilename) == TRUE){
267 ArrayData.PropData.Clear();
268 ArrayData.PropValues.Clear();
269 ArrayData.PropCount = 0;
274 SRNotif *sradd = new SRNotif;
275 sradd->ContactName = NameArray;
276 sradd->ContactNickname = ContactFinalNickname;
277 sradd->ContactAccount = SAPiter->second;
278 sradd->ContactAccountName = SALiter->second;
279 sradd->ContactFullFilename = vcardfilenamewxs;
280 sradd->SearchResultID = intResultFound;
282 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
283 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
287 wxCommandEvent sraddenv(SE_ADDRESULT);
288 sraddenv.SetClientData(sradd);
289 wxPostEvent(this, sraddenv);
299 } else if (SelOpt == 1 || SelOpt == 5 || SelOpt == 9){
301 // Matching string that ends with...
305 // Check the forename.
307 if (NameArray.Forename.Right(StrOptLen) == StrOpt){
309 if (CheckDuplicate(vcardfilenamewxs,
311 &SearchResultAccount,
312 &SearchResultFilename) == TRUE){
318 ArrayData.PropData.Clear();
319 ArrayData.PropValues.Clear();
320 ArrayData.PropCount = 0;
325 SRNotif *sradd = new SRNotif;
326 sradd->ContactName = NameArray;
327 sradd->ContactNickname = ContactFinalNickname;
328 sradd->ContactAccount = SAPiter->second;
329 sradd->ContactAccountName = SALiter->second;
330 sradd->ContactFullFilename = vcardfilenamewxs;
331 sradd->SearchResultID = intResultFound;
333 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
334 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
338 wxCommandEvent sraddenv(SE_ADDRESULT);
339 sraddenv.SetClientData(sradd);
340 wxPostEvent(this, sraddenv);
344 } else if (SelOpt == 5){
346 if (NameArray.Surname.Right(StrOptLen) == StrOpt){
348 if (CheckDuplicate(vcardfilenamewxs,
350 &SearchResultAccount,
351 &SearchResultFilename) == TRUE){
357 ArrayData.PropData.Clear();
358 ArrayData.PropValues.Clear();
359 ArrayData.PropCount = 0;
364 SRNotif *sradd = new SRNotif;
365 sradd->ContactName = NameArray;
366 sradd->ContactNickname = ContactFinalNickname;
367 sradd->ContactAccount = SAPiter->second;
368 sradd->ContactAccountName = SALiter->second;
369 sradd->ContactFullFilename = vcardfilenamewxs;
370 sradd->SearchResultID = intResultFound;
372 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
373 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
377 wxCommandEvent sraddenv(SE_ADDRESULT);
378 sraddenv.SetClientData(sradd);
379 wxPostEvent(this, sraddenv);
383 } else if (SelOpt == 9){
385 wxString NicknameData;
387 for (int i = 0; i < NicknameArrayData.PropCount; i++){
389 NicknameData = NicknameArrayData.PropValues[i];
392 if (NicknameData.Right(StrOptLen) == StrOpt){
394 if (CheckDuplicate(vcardfilenamewxs,
396 &SearchResultAccount,
397 &SearchResultFilename) == TRUE){
403 ArrayData.PropData.Clear();
404 ArrayData.PropValues.Clear();
405 ArrayData.PropCount = 0;
410 SRNotif *sradd = new SRNotif;
411 sradd->ContactName = NameArray;
412 sradd->ContactNickname = ContactFinalNickname;
413 sradd->ContactAccount = SAPiter->second;
414 sradd->ContactAccountName = SALiter->second;
415 sradd->ContactFullFilename = vcardfilenamewxs;
416 sradd->SearchResultID = intResultFound;
418 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
419 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
423 wxCommandEvent sraddenv(SE_ADDRESULT);
424 sraddenv.SetClientData(sradd);
425 wxPostEvent(this, sraddenv);
435 } else if (SelOpt == 2 || SelOpt == 6 || SelOpt == 10){
437 // Matching string that contains...
441 if (NameArray.Forename.Find(StrOpt) != wxNOT_FOUND){
443 if (CheckDuplicate(vcardfilenamewxs,
445 &SearchResultAccount,
446 &SearchResultFilename) == TRUE){
452 ArrayData.PropData.Clear();
453 ArrayData.PropValues.Clear();
454 ArrayData.PropCount = 0;
459 SRNotif *sradd = new SRNotif;
460 sradd->ContactName = NameArray;
461 sradd->ContactNickname = ContactFinalNickname;
462 sradd->ContactAccount = SAPiter->second;
463 sradd->ContactAccountName = SALiter->second;
464 sradd->ContactFullFilename = vcardfilenamewxs;
465 sradd->SearchResultID = intResultFound;
467 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
468 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
472 wxCommandEvent sraddenv(SE_ADDRESULT);
473 sraddenv.SetClientData(sradd);
474 wxPostEvent(this, sraddenv);
478 } else if (SelOpt == 6){
480 if (NameArray.Surname.Find(StrOpt) != wxNOT_FOUND){
482 if (CheckDuplicate(vcardfilenamewxs,
484 &SearchResultAccount,
485 &SearchResultFilename) == TRUE){
491 ArrayData.PropData.Clear();
492 ArrayData.PropValues.Clear();
493 ArrayData.PropCount = 0;
498 SRNotif *sradd = new SRNotif;
499 sradd->ContactName = NameArray;
500 sradd->ContactNickname = ContactFinalNickname;
501 sradd->ContactAccount = SAPiter->second;
502 sradd->ContactAccountName = SALiter->second;
503 sradd->ContactFullFilename = vcardfilenamewxs;
504 sradd->SearchResultID = intResultFound;
506 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
507 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
511 wxCommandEvent sraddenv(SE_ADDRESULT);
512 sraddenv.SetClientData(sradd);
513 wxPostEvent(this, sraddenv);
517 } else if (SelOpt == 10){
519 wxString NicknameData;
521 for (int i = 0; i < NicknameArrayData.PropCount; i++){
523 NicknameData = NicknameArrayData.PropValues[i];
526 if (NicknameData.Find(StrOpt) != wxNOT_FOUND){
528 if (CheckDuplicate(vcardfilenamewxs,
530 &SearchResultAccount,
531 &SearchResultFilename) == TRUE){
537 ArrayData.PropData.Clear();
538 ArrayData.PropValues.Clear();
539 ArrayData.PropCount = 0;
544 SRNotif *sradd = new SRNotif;
545 sradd->ContactName = NameArray;
546 sradd->ContactNickname = ContactFinalNickname;
547 sradd->ContactAccount = SAPiter->second;
548 sradd->ContactAccountName = SALiter->second;
549 sradd->ContactFullFilename = vcardfilenamewxs;
550 sradd->SearchResultID = intResultFound;
552 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
553 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
557 wxCommandEvent sraddenv(SE_ADDRESULT);
558 sraddenv.SetClientData(sradd);
559 wxPostEvent(this, sraddenv);
569 } else if (SelOpt == 3 || SelOpt == 7 || SelOpt == 11){
571 // Matching String that doesn't contain...
575 // Process the nickname data.
577 if (NameArray.Forename.Find(StrOpt) == wxNOT_FOUND){
579 if (CheckDuplicate(vcardfilenamewxs,
581 &SearchResultAccount,
582 &SearchResultFilename) == TRUE){
588 ArrayData.PropData.Clear();
589 ArrayData.PropValues.Clear();
590 ArrayData.PropCount = 0;
595 SRNotif *sradd = new SRNotif;
596 sradd->ContactName = NameArray;
597 sradd->ContactNickname = ContactFinalNickname;
598 sradd->ContactAccount = SAPiter->second;
599 sradd->ContactAccountName = SALiter->second;
600 sradd->ContactFullFilename = vcardfilenamewxs;
601 sradd->SearchResultID = intResultFound;
603 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
604 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
608 wxCommandEvent sraddenv(SE_ADDRESULT);
609 sraddenv.SetClientData(sradd);
610 wxPostEvent(this, sraddenv);
614 } else if (SelOpt == 7){
616 if (!NameArray.Surname.Find(StrOpt) == wxNOT_FOUND){
618 if (CheckDuplicate(vcardfilenamewxs,
620 &SearchResultAccount,
621 &SearchResultFilename) == TRUE){
627 ArrayData.PropData.Clear();
628 ArrayData.PropValues.Clear();
629 ArrayData.PropCount = 0;
634 SRNotif *sradd = new SRNotif;
635 sradd->ContactName = NameArray;
636 sradd->ContactNickname = ContactFinalNickname;
637 sradd->ContactAccount = SAPiter->second;
638 sradd->ContactAccountName = SALiter->second;
639 sradd->ContactFullFilename = vcardfilenamewxs;
640 sradd->SearchResultID = intResultFound;
642 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
643 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
647 wxCommandEvent sraddenv(SE_ADDRESULT);
648 sraddenv.SetClientData(sradd);
649 wxPostEvent(this, sraddenv);
653 } else if (SelOpt == 11){
655 wxString NicknameData;
657 for (int i = 0; i < NicknameArrayData.PropCount; i++){
659 NicknameData = NicknameArrayData.PropValues[i];
662 if (NicknameData.Find(StrOpt) == wxNOT_FOUND){
664 if (CheckDuplicate(vcardfilenamewxs,
666 &SearchResultAccount,
667 &SearchResultFilename) == TRUE){
673 ArrayData.PropData.Clear();
674 ArrayData.PropValues.Clear();
675 ArrayData.PropCount = 0;
680 SRNotif *sradd = new SRNotif;
681 sradd->ContactName = NameArray;
682 sradd->ContactNickname = ContactFinalNickname;
683 sradd->ContactAccount = SAPiter->second;
684 sradd->ContactAccountName = SALiter->second;
685 sradd->ContactFullFilename = vcardfilenamewxs;
686 sradd->SearchResultID = intResultFound;
688 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
689 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
693 wxCommandEvent sraddenv(SE_ADDRESULT);
694 sraddenv.SetClientData(sradd);
695 wxPostEvent(this, sraddenv);
709 chkOpt = XABSPPtr->GetCheckboxSetting();
713 // Search should check if it has a photo or not.
715 ArrayData = Person.GetByPartial(wxT("PHOTO"));
717 if ((ArrayData.PropCount >= 1 &&
719 (ArrayData.PropCount == 0 &&
722 if (CheckDuplicate(vcardfilenamewxs,
724 &SearchResultAccount,
725 &SearchResultFilename) == TRUE){
731 ArrayData.PropData.Clear();
732 ArrayData.PropValues.Clear();
733 ArrayData.PropCount = 0;
738 SRNotif *sradd = new SRNotif;
740 sradd->ContactName = NameArray;
741 sradd->ContactNickname = ContactFinalNickname;
742 sradd->ContactAccount = SAPiter->second;
743 sradd->ContactAccountName = SALiter->second;
744 sradd->ContactFullFilename = vcardfilenamewxs;
745 sradd->SearchResultID = intResultFound;
747 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
748 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
752 wxCommandEvent sraddenv(SE_ADDRESULT);
753 sraddenv.SetClientData(sradd);
754 wxPostEvent(this, sraddenv);
758 } else if (SelOpt == 13){
760 // Search should check if it has a logo or not.
762 ArrayData = Person.GetByPartial(wxT("LOGO"));
764 if ((ArrayData.PropCount >= 1 &&
766 (ArrayData.PropCount == 0 &&
769 if (CheckDuplicate(vcardfilenamewxs,
771 &SearchResultAccount,
772 &SearchResultFilename) == TRUE){
778 ArrayData.PropData.Clear();
779 ArrayData.PropValues.Clear();
780 ArrayData.PropCount = 0;
785 SRNotif *sradd = new SRNotif;
787 sradd->ContactName = NameArray;
788 sradd->ContactNickname = ContactFinalNickname;
789 sradd->ContactAccount = SAPiter->second;
790 sradd->ContactAccountName = SALiter->second;
791 sradd->ContactFullFilename = vcardfilenamewxs;
792 sradd->SearchResultID = intResultFound;
794 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
795 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
799 wxCommandEvent sraddenv(SE_ADDRESULT);
800 sraddenv.SetClientData(sradd);
801 wxPostEvent(this, sraddenv);
805 } else if (SelOpt == 14){
807 // Search should check if it has sound or not.
809 ArrayData = Person.GetByPartial(wxT("SOUND"));
811 if ((ArrayData.PropCount >= 1 &&
813 (ArrayData.PropCount == 0 &&
816 if (CheckDuplicate(vcardfilenamewxs,
818 &SearchResultAccount,
819 &SearchResultFilename) == TRUE){
825 ArrayData.PropData.Clear();
826 ArrayData.PropValues.Clear();
827 ArrayData.PropCount = 0;
832 SRNotif *sradd = new SRNotif;
834 sradd->ContactName = NameArray;
835 sradd->ContactNickname = ContactFinalNickname;
836 sradd->ContactAccount = SAPiter->second;
837 sradd->ContactAccountName = SALiter->second;
838 sradd->ContactFullFilename = vcardfilenamewxs;
839 sradd->SearchResultID = intResultFound;
841 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
842 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
846 wxCommandEvent sraddenv(SE_ADDRESULT);
847 sraddenv.SetClientData(sradd);
848 wxPostEvent(this, sraddenv);
860 ArrayData.PropData.Clear();
861 ArrayData.PropValues.Clear();
862 ArrayData.PropCount = 0;
872 vCardFilename.Clear();
873 vCardFilenameFull.Clear();
874 ProcFiles = vcardaccdir.GetNext(&vCardFilename);
884 // Finished searching... clear up.
886 wxCommandEvent sbfinstr (SE_SBUPDATE);
888 wxString *SBFinish = new wxString;
890 if (intResultFound == 0){
892 *SBFinish = _("No contacts found.");
894 } else if (intResultFound == 1){
896 *SBFinish = _("1 contact found.");
900 *SBFinish = wxString::Format(wxT("%i"), intResultFound) + _(" contacts found.");
904 sbfinstr.SetClientData(SBFinish);
905 wxPostEvent(this, sbfinstr);
907 wxCommandEvent sbfinish (SE_SEARCHFINISHED);
908 wxPostEvent(this, sbfinish);