1 // frmSearch.cpp - Search form.
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 "../frmMain.h"
21 #include "../frmContact.h"
22 #include "frmSearchAccounts.h"
23 #include "../common/preferences.h"
24 #include "../common/dirs.h"
25 #include "../vcard/vcard.h"
29 DEFINE_EVENT_TYPE(SE_ADDSEARCHSETTING);
30 DEFINE_EVENT_TYPE(SE_REMOVESEARCHSETTING);
31 DEFINE_EVENT_TYPE(SE_RELOADACCOUNTS);
32 DEFINE_EVENT_TYPE(SE_ADDRESULT);
33 DEFINE_EVENT_TYPE(SE_SBUPDATE);
34 DEFINE_EVENT_TYPE(SE_SEARCHFINISHED);
35 DEFINE_EVENT_TYPE(SE_UPDATERESULT);
36 DEFINE_EVENT_TYPE(SE_DELETERESULT);
37 DEFINE_EVENT_TYPE(SE_OPENCONTACT);
38 DEFINE_EVENT_TYPE(SE_EDITCONTACT);
39 DEFINE_EVENT_TYPE(SE_REVEALCONTACT);
41 BEGIN_EVENT_TABLE(frmSearch, wxFrame)
42 EVT_COMMAND(wxID_ANY, SE_ADDSEARCHSETTING, frmSearch::AddSearchSetting)
43 EVT_COMMAND(wxID_ANY, SE_REMOVESEARCHSETTING, frmSearch::RemoveSearchSetting)
44 EVT_COMMAND(wxID_ANY, SE_RELOADACCOUNTS, frmSearch::ReloadAccountList)
45 EVT_COMMAND(wxID_ANY, SE_ADDRESULT, frmSearch::AddResult)
46 EVT_COMMAND(wxID_ANY, SE_SBUPDATE, frmSearch::SearchBarUpdate)
47 EVT_COMMAND(wxID_ANY, SE_SEARCHFINISHED, frmSearch::SearchFinished)
48 EVT_COMMAND(wxID_ANY, SE_UPDATERESULT, frmSearch::UpdateResult)
49 EVT_COMMAND(wxID_ANY, SE_DELETERESULT, frmSearch::DeleteResult)
50 EVT_COMMAND(wxID_ANY, SE_OPENCONTACT, frmSearch::OpenContact)
51 EVT_COMMAND(wxID_ANY, SE_EDITCONTACT, frmSearch::EditContact)
52 EVT_COMMAND(wxID_ANY, SE_REVEALCONTACT, frmSearch::RevealContact)
55 frmSearch::frmSearch( wxWindow* parent )
57 frmSearchADT( parent )
60 XABSearchPanel *InitPanel = new XABSearchPanel( tabSearch );
61 InitPanel->EnableButtons(TRUE, FALSE);
62 InitPanel->SetupPointers(this);
63 InitPanel->SetupInteger(ScrollGen);
64 szrSearch->Add(InitPanel, 1, wxEXPAND, 0);
65 szrSearch->Fit(tabSearch);
66 SearchFrames.insert(std::make_pair(ScrollGen, InitPanel));
69 // Get the list of accounts and mark them as true.
71 wxString preffilename = GetUserPrefDir();
73 XABPreferences preferences(preffilename);
77 wxString AccDirFullSfx;
79 for (int i = 0; i < preferences.accounts.GetCount(); i++){
80 AccDir = preferences.accounts.GetAccountDirectory(i);
81 AccDirFull = preferences.accounts.GetAccountDirectory(i);
83 AccDirFull.Append(wxT("."));
84 AccDirFullSfx.Append(preferences.accounts.GetAccountType(i));
85 AccDirFullSfx.LowerCase();
87 AccDirFull.Append(AccDirFullSfx);
88 SearchAccountsPaths.insert(std::make_pair(AccDir, AccDirFull));
89 SearchAccounts.insert(std::make_pair(AccDir, TRUE));
90 SearchAccountsNames.insert(std::make_pair(AccDir,
91 preferences.accounts.GetAccountName(i)));
93 AccDirFullSfx.clear();
101 item.SetText(_("Name"));
104 lstResults->InsertColumn(0, item);
107 item.SetText(_("Nickname"));
110 lstResults->InsertColumn(1, item);
113 item.SetText(_("Account"));
116 lstResults->InsertColumn(2, item);
120 void frmSearch::SelectAccounts( wxCommandEvent& event )
123 // Create the form and display as modal.
125 frmSearchAccounts *frameSAccount = new frmSearchAccounts ( this );
126 frameSAccount->LoadSearchAccounts(&SearchAccounts, &SearchAccountsNames);
127 frameSAccount->ShowModal();
128 delete frameSAccount;
129 frameSAccount = NULL;
133 void frmSearch::SearchContacts( wxCommandEvent& event )
136 // Change the button to stop.
138 if (StopMode == FALSE){
140 // Clear the list of search results.
142 lstResults->DeleteAllItems();
143 SearchResultAccount.clear();
144 SearchResultFilename.clear();
146 // Button clicked on as 'Search'.
148 DisableAllSearchSettings(TRUE);
150 btnSearch->SetLabel(_("Stop"));
152 // Spawn a thread so that searching can proceed
153 // without blocking the GUI (and allow the
154 // search to stop when needed).
156 std::thread SearchThread(&frmSearch::SearchContactsThread, this);
157 SearchThread.detach();
161 // Button clicked on as 'Stop'.
164 btnSearch->SetLabel(_("Search"));
165 DisableAllSearchSettings(FALSE);
171 void frmSearch::ResetContacts( wxCommandEvent& event )
174 // Clear all the search settings.
176 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
177 iter != SearchFrames.end(); ++iter){
179 XABSearchPanel *XABSPPtr;
181 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
182 szrSearch->Detach(XABSPPtr);
184 // Remove the frame from the memory and the list.
189 SearchFrames.erase(iter->first);
193 // Clear the list of search results.
195 lstResults->DeleteAllItems();
197 // Clear the account settings.
199 for (std::map<wxString, bool>::iterator iter = SearchAccounts.begin();
200 iter != SearchAccounts.end(); iter++){
206 // Add a search settings with the default things.
210 XABSearchPanel *NewPanel = new XABSearchPanel( tabSearch );
211 NewPanel->EnableButtons(TRUE, FALSE);
212 NewPanel->SetupPointers(this);
213 NewPanel->SetupInteger(ScrollGen);
214 szrSearch->Add(NewPanel, 1, wxEXPAND, 0);
215 szrSearch->Fit(tabSearch);
216 SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
221 void frmSearch::AddSearchSetting( wxCommandEvent& event )
224 // Add a search setting frame to the list.
226 XABSearchPanel *NewPanel = new XABSearchPanel( tabSearch );
227 NewPanel->EnableButtons(TRUE, TRUE);
228 NewPanel->SetupPointers(this);
229 NewPanel->SetupInteger(ScrollGen);
230 szrSearch->Add(NewPanel, 1, wxEXPAND|wxGROW, 5);
231 //szrSearch->Fit(tabSearch);
232 szrSearch->FitInside(tabSearch);
234 szrSearch->RecalcSizes();
236 SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
239 // Check if number of search settings is 15 (or over).
240 // If it is, disable the option of adding extra settings
241 // for all frames until one is removed.
243 XABSearchPanel *XABSPPtr;
245 if (SearchFrames.size() >= 15){
247 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
248 iter != SearchFrames.end(); ++iter){
250 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
252 XABSPPtr->EnableButtons(FALSE, TRUE);
256 } else if (SearchFrames.size() >= 2){
258 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
259 iter != SearchFrames.end(); ++iter){
261 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
263 XABSPPtr->EnableButtons(TRUE, TRUE);
269 //tabSearch->SetScrollbar(wxVERTICAL, 32768, 25, 32768, false);
271 //tabSearch->ScrollToLine(tabSearch->GetLineCount());
275 void frmSearch::RemoveSearchSetting( wxCommandEvent& event )
278 // Get the integer from the event.
280 std::map<int,void*>::iterator iter;
281 iter = SearchFrames.find(event.GetInt());
283 // Remove a search setting frame from the list.
285 XABSearchPanel *XABSPPtr;
286 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
288 szrSearch->Detach(XABSPPtr);
290 // Remove the frame from the memory and the list.
292 SearchFrames.erase(event.GetInt());
297 //szrSearch->Fit(tabSearch);
299 if (SearchFrames.size() < 15 && SearchFrames.size() > 1){
301 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
302 iter != SearchFrames.end(); ++iter){
304 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
306 XABSPPtr->EnableButtons(TRUE, TRUE);
310 } else if (SearchFrames.size() == 1){
312 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
313 iter != SearchFrames.end(); ++iter){
315 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
317 XABSPPtr->EnableButtons(TRUE, FALSE);
323 szrSearch->FitInside(tabSearch);
325 szrSearch->RecalcSizes();
330 void frmSearch::ReloadAccountList( wxCommandEvent& event ){
332 // Currently unimplemented.
336 void frmSearch::SearchBarUpdate( wxCommandEvent& event ){
338 wxString *SBData = (wxString*)event.GetClientData();
340 stbBottom->SetStatusText(*SBData, 0);
347 void frmSearch::SearchFinished( wxCommandEvent& event ){
350 btnSearch->SetLabel(_("Search"));
351 DisableAllSearchSettings(FALSE);
355 void frmSearch::DisableAllSearchSettings(bool Enable){
357 for (std::map<int, void*>::iterator siter = SearchFrames.begin();
358 siter != SearchFrames.end(); siter++){
360 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
362 if (Enable == FALSE){
364 wxCommandEvent enboxes(XABSP_ENABLECONTROLS);
365 wxPostEvent(XABSPPtr, enboxes);
367 } else if (Enable == TRUE){
369 wxCommandEvent disboxes(XABSP_DISABLECONTROLS);
370 wxPostEvent(XABSPPtr, disboxes);
378 void frmSearch::CloseWindow( wxCloseEvent& event ){
380 // Hide the window so users don't panic
381 // whilst clearing up.
387 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
388 iter != SearchFrames.end(); ++iter){
390 XABSearchPanel *XABSPPtr;
392 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
393 szrSearch->Detach(XABSPPtr);
395 // Remove the frame from the memory and the list.
402 SearchFrames.clear();
406 WindowData *WData = new WindowData;
409 WData->WindowPointer = this;
410 WData->WindowID = SearchUID;
412 wxCommandEvent delevent(WINDOW_CLOSE);
413 delevent.SetClientData(WData);
414 wxPostEvent(GetParent(), delevent);
416 wxCommandEvent rs(CE_REMOVESEARCH);
417 wxPostEvent(this, rs);
425 void frmSearch::CloseWindow( wxCommandEvent& event ){
431 void frmSearch::SetUID(int UID){
437 void frmSearch::SetSearchMode(bool SearchModeIn){
439 SearchMode = SearchModeIn;
441 if (SearchMode == TRUE){
443 mnuContactEdit->Enable(FALSE);
444 mnuContactReveal->Enable(FALSE);
446 wxFileSystem::AddHandler(new wxMemoryFSHandler);
449 wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png));
450 ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG);
451 wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG);
453 wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png));
454 ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG);
455 wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG);
457 wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png));
458 ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG);
459 wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG);
461 wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png));
462 ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG);
463 wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG);
465 wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png));
466 ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG);
467 wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG);
469 wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png));
470 ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG);
471 wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG);
475 mnuContactEdit->Enable(TRUE);
476 mnuContactReveal->Enable(TRUE);