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 // Check if any accounts has been selected.
138 bool AccountsFound = false;
140 for (std::map<wxString, bool>::iterator saiter = SearchAccounts.begin();
141 saiter != SearchAccounts.end(); saiter++){
143 if (saiter->second == true){
145 AccountsFound = true;
152 if (AccountsFound == false){
154 wxMessageBox(_("No accounts have been selected to search contacts for."),
155 _("No accounts selected"),
161 // Change the button to stop.
163 if (StopMode == FALSE){
165 // Clear the list of search results.
167 lstResults->DeleteAllItems();
168 SearchResultAccount.clear();
169 SearchResultFilename.clear();
171 // Button clicked on as 'Search'.
173 DisableAllSearchSettings(TRUE);
175 btnSearch->SetLabel(_("Stop"));
177 // Spawn a thread so that searching can proceed
178 // without blocking the GUI (and allow the
179 // search to stop when needed).
181 std::thread SearchThread(&frmSearch::SearchContactsThread, this);
182 SearchThread.detach();
186 // Button clicked on as 'Stop'.
189 btnSearch->SetLabel(_("Search"));
190 DisableAllSearchSettings(FALSE);
196 void frmSearch::ResetContacts( wxCommandEvent& event )
199 // Clear all the search settings.
201 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
202 iter != SearchFrames.end(); ++iter){
204 XABSearchPanel *XABSPPtr;
206 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
207 szrSearch->Detach(XABSPPtr);
209 // Remove the frame from the memory and the list.
214 SearchFrames.erase(iter->first);
218 // Clear the list of search results.
220 lstResults->DeleteAllItems();
222 // Clear the account settings.
224 for (std::map<wxString, bool>::iterator iter = SearchAccounts.begin();
225 iter != SearchAccounts.end(); iter++){
231 // Add a search settings with the default things.
235 XABSearchPanel *NewPanel = new XABSearchPanel( tabSearch );
236 NewPanel->EnableButtons(TRUE, FALSE);
237 NewPanel->SetupPointers(this);
238 NewPanel->SetupInteger(ScrollGen);
239 szrSearch->Add(NewPanel, 1, wxEXPAND, 0);
240 szrSearch->Fit(tabSearch);
241 SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
246 void frmSearch::AddSearchSetting( wxCommandEvent& event )
249 // Add a search setting frame to the list.
251 XABSearchPanel *NewPanel = new XABSearchPanel( tabSearch );
252 NewPanel->EnableButtons(TRUE, TRUE);
253 NewPanel->SetupPointers(this);
254 NewPanel->SetupInteger(ScrollGen);
255 szrSearch->Add(NewPanel, 1, wxEXPAND|wxGROW, 5);
256 //szrSearch->Fit(tabSearch);
257 szrSearch->FitInside(tabSearch);
259 szrSearch->RecalcSizes();
261 SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
264 // Check if number of search settings is 15 (or over).
265 // If it is, disable the option of adding extra settings
266 // for all frames until one is removed.
268 XABSearchPanel *XABSPPtr;
270 if (SearchFrames.size() >= 15){
272 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
273 iter != SearchFrames.end(); ++iter){
275 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
277 XABSPPtr->EnableButtons(FALSE, TRUE);
281 } else if (SearchFrames.size() >= 2){
283 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
284 iter != SearchFrames.end(); ++iter){
286 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
288 XABSPPtr->EnableButtons(TRUE, TRUE);
294 //tabSearch->SetScrollbar(wxVERTICAL, 32768, 25, 32768, false);
296 //tabSearch->ScrollToLine(tabSearch->GetLineCount());
300 void frmSearch::RemoveSearchSetting( wxCommandEvent& event )
303 // Get the integer from the event.
305 std::map<int,void*>::iterator iter;
306 iter = SearchFrames.find(event.GetInt());
308 // Remove a search setting frame from the list.
310 XABSearchPanel *XABSPPtr;
311 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
313 szrSearch->Detach(XABSPPtr);
315 // Remove the frame from the memory and the list.
317 SearchFrames.erase(event.GetInt());
322 //szrSearch->Fit(tabSearch);
324 if (SearchFrames.size() < 15 && SearchFrames.size() > 1){
326 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
327 iter != SearchFrames.end(); ++iter){
329 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
331 XABSPPtr->EnableButtons(TRUE, TRUE);
335 } else if (SearchFrames.size() == 1){
337 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
338 iter != SearchFrames.end(); ++iter){
340 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
342 XABSPPtr->EnableButtons(TRUE, FALSE);
348 szrSearch->FitInside(tabSearch);
350 szrSearch->RecalcSizes();
355 void frmSearch::ReloadAccountList( wxCommandEvent& event ){
357 // Currently unimplemented.
361 void frmSearch::SearchBarUpdate( wxCommandEvent& event ){
363 wxString *SBData = (wxString*)event.GetClientData();
365 stbBottom->SetStatusText(*SBData, 0);
372 void frmSearch::SearchFinished( wxCommandEvent& event ){
375 btnSearch->SetLabel(_("Search"));
376 DisableAllSearchSettings(FALSE);
380 void frmSearch::DisableAllSearchSettings(bool Enable){
382 for (std::map<int, void*>::iterator siter = SearchFrames.begin();
383 siter != SearchFrames.end(); siter++){
385 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
387 if (Enable == FALSE){
389 wxCommandEvent enboxes(XABSP_ENABLECONTROLS);
390 wxPostEvent(XABSPPtr, enboxes);
392 } else if (Enable == TRUE){
394 wxCommandEvent disboxes(XABSP_DISABLECONTROLS);
395 wxPostEvent(XABSPPtr, disboxes);
403 void frmSearch::CloseWindow( wxCloseEvent& event ){
405 // Hide the window so users don't panic
406 // whilst clearing up.
412 for (std::map<int,void*>::iterator iter = SearchFrames.begin();
413 iter != SearchFrames.end(); ++iter){
415 XABSearchPanel *XABSPPtr;
417 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
418 szrSearch->Detach(XABSPPtr);
420 // Remove the frame from the memory and the list.
427 SearchFrames.clear();
431 WindowData *WData = new WindowData;
434 WData->WindowPointer = this;
435 WData->WindowID = SearchUID;
437 wxCommandEvent delevent(WINDOW_CLOSE);
438 delevent.SetClientData(WData);
439 wxPostEvent(GetParent(), delevent);
441 wxCommandEvent rs(CE_REMOVESEARCH);
442 wxPostEvent(this, rs);
450 void frmSearch::CloseWindow( wxCommandEvent& event ){
456 void frmSearch::SetUID(int UID){
462 void frmSearch::SetSearchMode(bool SearchModeIn){
464 SearchMode = SearchModeIn;
466 if (SearchMode == TRUE){
468 mnuContactEdit->Enable(FALSE);
469 mnuContactReveal->Enable(FALSE);
471 wxFileSystem::AddHandler(new wxMemoryFSHandler);
474 wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png));
475 ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG);
476 wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG);
478 wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png));
479 ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG);
480 wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG);
482 wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png));
483 ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG);
484 wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG);
486 wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png));
487 ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG);
488 wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG);
490 wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png));
491 ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG);
492 wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG);
494 wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png));
495 ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG);
496 wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG);
500 mnuContactEdit->Enable(TRUE);
501 mnuContactReveal->Enable(TRUE);