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 wxString vCardFilename;
83 wxString vCardFilenameFull;
85 if (wxDirExists(AccountDir)){
87 wxDir vcardaccdir(AccountDir);
89 bool ProcFiles = vcardaccdir.GetFirst(&vCardFilename, wxEmptyString, wxDIR_FILES);
92 if (StopMode == FALSE){
94 wxCommandEvent sbstop(SE_SBUPDATE);
96 wxString *SBStopStr = new wxString;
97 *SBStopStr = _("Search stopped.");
99 sbstop.SetClientData(SBStopStr);
100 wxPostEvent(this, sbstop);
105 if (vCardFilename.Right(4) == wxT(".vcf") ||
106 vCardFilename.Right(4) == wxT(".VCF") ||
107 vCardFilename.Right(5) == wxT(".vcard") ||
108 vCardFilename.Right(5) == wxT(".VCARD")){
111 vCardFilenameFull.Append(AccountDir);
112 vCardFilenameFull.Append(vCardFilename);
114 Person.LoadFile(vCardFilenameFull);
116 vcardfilenamewxs = vCardFilenameFull;
118 // Check if file has version 4.0, first name (FN)
119 // begin and end vCard portions.
121 // Don't search unless these conditions have been met.
123 if (Person.MeetBaseSpecification()){
125 // Go through each of the search settings and check
126 // if they match with contact details.
128 // If it matches then add to the search results.
134 ArrayvCardOutData ArrayData;
135 ArrayvCardOutData NicknameArrayData;
136 vCardName NameArray = Person.GetName();
137 wxString ContactFinalNickname;
138 NicknameArrayData = Person.GetByPartial(wxT("NICKNAME"));
140 if (NicknameArrayData.PropCount != 0){
141 ContactFinalNickname = NicknameArrayData.PropValues[0];
144 for (std::map<int, void*>::iterator siter = SearchFrames.begin();
145 siter != SearchFrames.end(); siter++){
147 // Get the settings from the form.
149 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
151 // Get Selected Option and Setting.
153 SelOpt = XABSPPtr->GetSelectionOption();
157 StrOpt = XABSPPtr->GetStringSetting();
158 StrOptLen = StrOpt.Len();
160 if (SelOpt == 0 || SelOpt == 4 || SelOpt == 8){
162 // Matching string that beings with...
164 // Double check the std::map inserts for each section.
168 // Get the forename from the first N.
170 if (NameArray.Forename.Mid(0, StrOptLen) == StrOpt){
172 if (CheckDuplicate(vcardfilenamewxs,
174 &SearchResultAccount,
175 &SearchResultFilename) == TRUE){
181 ArrayData.PropData.Clear();
182 ArrayData.PropValues.Clear();
183 ArrayData.PropCount = 0;
188 SRNotif *sradd = new SRNotif;
189 sradd->ContactName = NameArray;
190 sradd->ContactNickname = ContactFinalNickname;
191 sradd->ContactAccount = SAPiter->second;
192 sradd->ContactAccountName = SALiter->second;
193 sradd->ContactFullFilename = vcardfilenamewxs;
194 sradd->SearchResultID = intResultFound;
196 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
197 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
201 wxCommandEvent sraddenv(SE_ADDRESULT);
202 sraddenv.SetClientData(sradd);
203 wxPostEvent(this, sraddenv);
207 } else if (SelOpt == 4){
209 if (NameArray.Surname.Mid(0, StrOptLen) == StrOpt){
211 if (CheckDuplicate(vcardfilenamewxs,
213 &SearchResultAccount,
214 &SearchResultFilename) == TRUE){
220 ArrayData.PropData.Clear();
221 ArrayData.PropValues.Clear();
222 ArrayData.PropCount = 0;
227 SRNotif *sradd = new SRNotif;
228 sradd->ContactName = NameArray;
229 sradd->ContactNickname = ContactFinalNickname;
230 sradd->ContactAccount = SAPiter->second;
231 sradd->ContactAccountName = SALiter->second;
232 sradd->ContactFullFilename = vcardfilenamewxs;
233 sradd->SearchResultID = intResultFound;
235 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
236 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
240 wxCommandEvent sraddenv(SE_ADDRESULT);
241 sraddenv.SetClientData(sradd);
242 wxPostEvent(this, sraddenv);
246 } else if (SelOpt == 8){
248 wxString NicknameData;
250 for (int i = 0; i < NicknameArrayData.PropCount; i++){
252 NicknameData = NicknameArrayData.PropValues[i];
254 if (NicknameData.Mid(0, StrOptLen) == StrOpt){
256 if (CheckDuplicate(vcardfilenamewxs,
258 &SearchResultAccount,
259 &SearchResultFilename) == TRUE){
265 ArrayData.PropData.Clear();
266 ArrayData.PropValues.Clear();
267 ArrayData.PropCount = 0;
272 SRNotif *sradd = new SRNotif;
273 sradd->ContactName = NameArray;
274 sradd->ContactNickname = ContactFinalNickname;
275 sradd->ContactAccount = SAPiter->second;
276 sradd->ContactAccountName = SALiter->second;
277 sradd->ContactFullFilename = vcardfilenamewxs;
278 sradd->SearchResultID = intResultFound;
280 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
281 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
285 wxCommandEvent sraddenv(SE_ADDRESULT);
286 sraddenv.SetClientData(sradd);
287 wxPostEvent(this, sraddenv);
297 } else if (SelOpt == 1 || SelOpt == 5 || SelOpt == 9){
299 // Matching string that ends with...
303 // Check the forename.
305 if (NameArray.Forename.Right(StrOptLen) == StrOpt){
307 if (CheckDuplicate(vcardfilenamewxs,
309 &SearchResultAccount,
310 &SearchResultFilename) == TRUE){
316 ArrayData.PropData.Clear();
317 ArrayData.PropValues.Clear();
318 ArrayData.PropCount = 0;
323 SRNotif *sradd = new SRNotif;
324 sradd->ContactName = NameArray;
325 sradd->ContactNickname = ContactFinalNickname;
326 sradd->ContactAccount = SAPiter->second;
327 sradd->ContactAccountName = SALiter->second;
328 sradd->ContactFullFilename = vcardfilenamewxs;
329 sradd->SearchResultID = intResultFound;
331 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
332 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
336 wxCommandEvent sraddenv(SE_ADDRESULT);
337 sraddenv.SetClientData(sradd);
338 wxPostEvent(this, sraddenv);
342 } else if (SelOpt == 5){
344 if (NameArray.Surname.Right(StrOptLen) == StrOpt){
346 if (CheckDuplicate(vcardfilenamewxs,
348 &SearchResultAccount,
349 &SearchResultFilename) == TRUE){
355 ArrayData.PropData.Clear();
356 ArrayData.PropValues.Clear();
357 ArrayData.PropCount = 0;
362 SRNotif *sradd = new SRNotif;
363 sradd->ContactName = NameArray;
364 sradd->ContactNickname = ContactFinalNickname;
365 sradd->ContactAccount = SAPiter->second;
366 sradd->ContactAccountName = SALiter->second;
367 sradd->ContactFullFilename = vcardfilenamewxs;
368 sradd->SearchResultID = intResultFound;
370 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
371 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
375 wxCommandEvent sraddenv(SE_ADDRESULT);
376 sraddenv.SetClientData(sradd);
377 wxPostEvent(this, sraddenv);
381 } else if (SelOpt == 9){
383 wxString NicknameData;
385 for (int i = 0; i < NicknameArrayData.PropCount; i++){
387 NicknameData = NicknameArrayData.PropValues[i];
390 if (NicknameData.Right(StrOptLen) == StrOpt){
392 if (CheckDuplicate(vcardfilenamewxs,
394 &SearchResultAccount,
395 &SearchResultFilename) == TRUE){
401 ArrayData.PropData.Clear();
402 ArrayData.PropValues.Clear();
403 ArrayData.PropCount = 0;
408 SRNotif *sradd = new SRNotif;
409 sradd->ContactName = NameArray;
410 sradd->ContactNickname = ContactFinalNickname;
411 sradd->ContactAccount = SAPiter->second;
412 sradd->ContactAccountName = SALiter->second;
413 sradd->ContactFullFilename = vcardfilenamewxs;
414 sradd->SearchResultID = intResultFound;
416 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
417 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
421 wxCommandEvent sraddenv(SE_ADDRESULT);
422 sraddenv.SetClientData(sradd);
423 wxPostEvent(this, sraddenv);
433 } else if (SelOpt == 2 || SelOpt == 6 || SelOpt == 10){
435 // Matching string that contains...
439 if (NameArray.Forename.Find(StrOpt) != wxNOT_FOUND){
441 if (CheckDuplicate(vcardfilenamewxs,
443 &SearchResultAccount,
444 &SearchResultFilename) == TRUE){
450 ArrayData.PropData.Clear();
451 ArrayData.PropValues.Clear();
452 ArrayData.PropCount = 0;
457 SRNotif *sradd = new SRNotif;
458 sradd->ContactName = NameArray;
459 sradd->ContactNickname = ContactFinalNickname;
460 sradd->ContactAccount = SAPiter->second;
461 sradd->ContactAccountName = SALiter->second;
462 sradd->ContactFullFilename = vcardfilenamewxs;
463 sradd->SearchResultID = intResultFound;
465 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
466 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
470 wxCommandEvent sraddenv(SE_ADDRESULT);
471 sraddenv.SetClientData(sradd);
472 wxPostEvent(this, sraddenv);
476 } else if (SelOpt == 6){
478 if (NameArray.Surname.Find(StrOpt) != wxNOT_FOUND){
480 if (CheckDuplicate(vcardfilenamewxs,
482 &SearchResultAccount,
483 &SearchResultFilename) == TRUE){
489 ArrayData.PropData.Clear();
490 ArrayData.PropValues.Clear();
491 ArrayData.PropCount = 0;
496 SRNotif *sradd = new SRNotif;
497 sradd->ContactName = NameArray;
498 sradd->ContactNickname = ContactFinalNickname;
499 sradd->ContactAccount = SAPiter->second;
500 sradd->ContactAccountName = SALiter->second;
501 sradd->ContactFullFilename = vcardfilenamewxs;
502 sradd->SearchResultID = intResultFound;
504 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
505 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
509 wxCommandEvent sraddenv(SE_ADDRESULT);
510 sraddenv.SetClientData(sradd);
511 wxPostEvent(this, sraddenv);
515 } else if (SelOpt == 10){
517 wxString NicknameData;
519 for (int i = 0; i < NicknameArrayData.PropCount; i++){
521 NicknameData = NicknameArrayData.PropValues[i];
524 if (NicknameData.Find(StrOpt) != wxNOT_FOUND){
526 if (CheckDuplicate(vcardfilenamewxs,
528 &SearchResultAccount,
529 &SearchResultFilename) == TRUE){
535 ArrayData.PropData.Clear();
536 ArrayData.PropValues.Clear();
537 ArrayData.PropCount = 0;
542 SRNotif *sradd = new SRNotif;
543 sradd->ContactName = NameArray;
544 sradd->ContactNickname = ContactFinalNickname;
545 sradd->ContactAccount = SAPiter->second;
546 sradd->ContactAccountName = SALiter->second;
547 sradd->ContactFullFilename = vcardfilenamewxs;
548 sradd->SearchResultID = intResultFound;
550 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
551 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
555 wxCommandEvent sraddenv(SE_ADDRESULT);
556 sraddenv.SetClientData(sradd);
557 wxPostEvent(this, sraddenv);
567 } else if (SelOpt == 3 || SelOpt == 7 || SelOpt == 11){
569 // Matching String that doesn't contain...
573 // Process the nickname data.
575 if (NameArray.Forename.Find(StrOpt) == wxNOT_FOUND){
577 if (CheckDuplicate(vcardfilenamewxs,
579 &SearchResultAccount,
580 &SearchResultFilename) == TRUE){
586 ArrayData.PropData.Clear();
587 ArrayData.PropValues.Clear();
588 ArrayData.PropCount = 0;
593 SRNotif *sradd = new SRNotif;
594 sradd->ContactName = NameArray;
595 sradd->ContactNickname = ContactFinalNickname;
596 sradd->ContactAccount = SAPiter->second;
597 sradd->ContactAccountName = SALiter->second;
598 sradd->ContactFullFilename = vcardfilenamewxs;
599 sradd->SearchResultID = intResultFound;
601 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
602 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
606 wxCommandEvent sraddenv(SE_ADDRESULT);
607 sraddenv.SetClientData(sradd);
608 wxPostEvent(this, sraddenv);
612 } else if (SelOpt == 7){
614 if (!NameArray.Surname.Find(StrOpt) == wxNOT_FOUND){
616 if (CheckDuplicate(vcardfilenamewxs,
618 &SearchResultAccount,
619 &SearchResultFilename) == TRUE){
625 ArrayData.PropData.Clear();
626 ArrayData.PropValues.Clear();
627 ArrayData.PropCount = 0;
632 SRNotif *sradd = new SRNotif;
633 sradd->ContactName = NameArray;
634 sradd->ContactNickname = ContactFinalNickname;
635 sradd->ContactAccount = SAPiter->second;
636 sradd->ContactAccountName = SALiter->second;
637 sradd->ContactFullFilename = vcardfilenamewxs;
638 sradd->SearchResultID = intResultFound;
640 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
641 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
645 wxCommandEvent sraddenv(SE_ADDRESULT);
646 sraddenv.SetClientData(sradd);
647 wxPostEvent(this, sraddenv);
651 } else if (SelOpt == 11){
653 wxString NicknameData;
655 for (int i = 0; i < NicknameArrayData.PropCount; i++){
657 NicknameData = NicknameArrayData.PropValues[i];
660 if (NicknameData.Find(StrOpt) == wxNOT_FOUND){
662 if (CheckDuplicate(vcardfilenamewxs,
664 &SearchResultAccount,
665 &SearchResultFilename) == TRUE){
671 ArrayData.PropData.Clear();
672 ArrayData.PropValues.Clear();
673 ArrayData.PropCount = 0;
678 SRNotif *sradd = new SRNotif;
679 sradd->ContactName = NameArray;
680 sradd->ContactNickname = ContactFinalNickname;
681 sradd->ContactAccount = SAPiter->second;
682 sradd->ContactAccountName = SALiter->second;
683 sradd->ContactFullFilename = vcardfilenamewxs;
684 sradd->SearchResultID = intResultFound;
686 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
687 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
691 wxCommandEvent sraddenv(SE_ADDRESULT);
692 sraddenv.SetClientData(sradd);
693 wxPostEvent(this, sraddenv);
707 chkOpt = XABSPPtr->GetCheckboxSetting();
711 // Search should check if it has a photo or not.
713 ArrayData = Person.GetByPartial(wxT("PHOTO"));
715 if ((ArrayData.PropCount >= 1 &&
717 (ArrayData.PropCount == 0 &&
720 if (CheckDuplicate(vcardfilenamewxs,
722 &SearchResultAccount,
723 &SearchResultFilename) == TRUE){
729 ArrayData.PropData.Clear();
730 ArrayData.PropValues.Clear();
731 ArrayData.PropCount = 0;
736 SRNotif *sradd = new SRNotif;
738 sradd->ContactName = NameArray;
739 sradd->ContactNickname = ContactFinalNickname;
740 sradd->ContactAccount = SAPiter->second;
741 sradd->ContactAccountName = SALiter->second;
742 sradd->ContactFullFilename = vcardfilenamewxs;
743 sradd->SearchResultID = intResultFound;
745 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
746 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
750 wxCommandEvent sraddenv(SE_ADDRESULT);
751 sraddenv.SetClientData(sradd);
752 wxPostEvent(this, sraddenv);
756 } else if (SelOpt == 13){
758 // Search should check if it has a logo or not.
760 ArrayData = Person.GetByPartial(wxT("LOGO"));
762 if ((ArrayData.PropCount >= 1 &&
764 (ArrayData.PropCount == 0 &&
767 if (CheckDuplicate(vcardfilenamewxs,
769 &SearchResultAccount,
770 &SearchResultFilename) == TRUE){
776 ArrayData.PropData.Clear();
777 ArrayData.PropValues.Clear();
778 ArrayData.PropCount = 0;
783 SRNotif *sradd = new SRNotif;
785 sradd->ContactName = NameArray;
786 sradd->ContactNickname = ContactFinalNickname;
787 sradd->ContactAccount = SAPiter->second;
788 sradd->ContactAccountName = SALiter->second;
789 sradd->ContactFullFilename = vcardfilenamewxs;
790 sradd->SearchResultID = intResultFound;
792 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
793 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
797 wxCommandEvent sraddenv(SE_ADDRESULT);
798 sraddenv.SetClientData(sradd);
799 wxPostEvent(this, sraddenv);
803 } else if (SelOpt == 14){
805 // Search should check if it has sound or not.
807 ArrayData = Person.GetByPartial(wxT("SOUND"));
809 if ((ArrayData.PropCount >= 1 &&
811 (ArrayData.PropCount == 0 &&
814 if (CheckDuplicate(vcardfilenamewxs,
816 &SearchResultAccount,
817 &SearchResultFilename) == TRUE){
823 ArrayData.PropData.Clear();
824 ArrayData.PropValues.Clear();
825 ArrayData.PropCount = 0;
830 SRNotif *sradd = new SRNotif;
832 sradd->ContactName = NameArray;
833 sradd->ContactNickname = ContactFinalNickname;
834 sradd->ContactAccount = SAPiter->second;
835 sradd->ContactAccountName = SALiter->second;
836 sradd->ContactFullFilename = vcardfilenamewxs;
837 sradd->SearchResultID = intResultFound;
839 SearchResultAccount.insert(std::make_pair(sradd->SearchResultID, sradd->ContactAccount));
840 SearchResultFilename.insert(std::make_pair(sradd->SearchResultID, sradd->ContactFullFilename));
844 wxCommandEvent sraddenv(SE_ADDRESULT);
845 sraddenv.SetClientData(sradd);
846 wxPostEvent(this, sraddenv);
858 ArrayData.PropData.Clear();
859 ArrayData.PropValues.Clear();
860 ArrayData.PropCount = 0;
870 vCardFilename.Clear();
871 vCardFilenameFull.Clear();
872 ProcFiles = vcardaccdir.GetNext(&vCardFilename);
882 // Finished searching... clear up.
884 wxCommandEvent sbfinstr (SE_SBUPDATE);
886 wxString *SBFinish = new wxString;
888 if (intResultFound == 0){
890 *SBFinish = _("No contacts found.");
892 } else if (intResultFound == 1){
894 *SBFinish = _("1 contact found.");
898 *SBFinish = wxString::Format(wxT("%i"), intResultFound) + _(" contacts found.");
902 sbfinstr.SetClientData(SBFinish);
903 wxPostEvent(this, sbfinstr);
905 wxCommandEvent sbfinish (SE_SEARCHFINISHED);
906 wxPostEvent(this, sbfinish);