Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Fixed issue where remove search setting appears after searching.
[xestiaab/.git] / source / search / frmSearch.cpp
1 // frmSearch.cpp - Search form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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"
27 #include <thread>
29 #define SEARCHSETTINGS_MAX 15
31 DEFINE_EVENT_TYPE(SE_ADDSEARCHSETTING);
32 DEFINE_EVENT_TYPE(SE_REMOVESEARCHSETTING);
33 DEFINE_EVENT_TYPE(SE_RELOADACCOUNTS);
34 DEFINE_EVENT_TYPE(SE_ADDRESULT);
35 DEFINE_EVENT_TYPE(SE_SBUPDATE);
36 DEFINE_EVENT_TYPE(SE_SEARCHFINISHED);
37 DEFINE_EVENT_TYPE(SE_UPDATERESULT);
38 DEFINE_EVENT_TYPE(SE_DELETERESULT);
39 DEFINE_EVENT_TYPE(SE_OPENCONTACT);
40 DEFINE_EVENT_TYPE(SE_EDITCONTACT);
41 DEFINE_EVENT_TYPE(SE_REVEALCONTACT);
43 BEGIN_EVENT_TABLE(frmSearch, wxFrame)
44   EVT_COMMAND(wxID_ANY, SE_ADDSEARCHSETTING, frmSearch::AddSearchSetting)
45   EVT_COMMAND(wxID_ANY, SE_REMOVESEARCHSETTING, frmSearch::RemoveSearchSetting)
46   EVT_COMMAND(wxID_ANY, SE_RELOADACCOUNTS, frmSearch::ReloadAccountList)
47   EVT_COMMAND(wxID_ANY, SE_ADDRESULT, frmSearch::AddResult)
48   EVT_COMMAND(wxID_ANY, SE_SBUPDATE, frmSearch::SearchBarUpdate)
49   EVT_COMMAND(wxID_ANY, SE_SEARCHFINISHED, frmSearch::SearchFinished)
50   EVT_COMMAND(wxID_ANY, SE_UPDATERESULT, frmSearch::UpdateResult)
51   EVT_COMMAND(wxID_ANY, SE_DELETERESULT, frmSearch::DeleteResult)
52   EVT_COMMAND(wxID_ANY, SE_OPENCONTACT, frmSearch::OpenContact)
53   EVT_COMMAND(wxID_ANY, SE_EDITCONTACT, frmSearch::EditContact)
54   EVT_COMMAND(wxID_ANY, SE_REVEALCONTACT, frmSearch::RevealContact)
55 END_EVENT_TABLE()
57 frmSearch::frmSearch( wxWindow* parent )
58 :
59 frmSearchADT( parent )
60 {
61         
62         XABSearchPanel *InitPanel = new XABSearchPanel( tabSearch );
63         InitPanel->EnableButtons(TRUE, FALSE);
64         InitPanel->SetupPointers(this);
65         InitPanel->SetupInteger(ScrollGen);
66         szrSearch->Add(InitPanel, 1, wxEXPAND, 0);
67         szrSearch->Fit(tabSearch);
68         SearchFrames.insert(std::make_pair(ScrollGen, InitPanel));
69         ScrollGen++;
70         
71         // Get the list of accounts and mark them as true.
73         wxString preffilename = GetUserPrefDir();
75         XABPreferences preferences(preffilename);       
76         
77         wxString AccDir;
78         wxString AccDirFull;
79         wxString AccDirFullSfx;
80         
81         for (int i = 0; i < preferences.accounts.GetCount(); i++){
82                 AccDir = preferences.accounts.GetAccountDirectory(i);
83                 AccDirFull = preferences.accounts.GetAccountDirectory(i);
84                 AccDirFull.Trim();
85                 AccDirFull.Append(wxT("."));
86                 AccDirFullSfx.Append(preferences.accounts.GetAccountType(i));
87                 AccDirFullSfx.LowerCase();
88                 AccDirFullSfx.Trim();
89                 AccDirFull.Append(AccDirFullSfx);
90                 SearchAccountsPaths.insert(std::make_pair(AccDir, AccDirFull));
91                 SearchAccounts.insert(std::make_pair(AccDir, TRUE));
92                 SearchAccountsNames.insert(std::make_pair(AccDir,
93                         preferences.accounts.GetAccountName(i)));
94                 AccDirFull.clear();
95                 AccDirFullSfx.clear();
96                 AccDir.clear();
97         }
98         
99         // Setup the columns.
100         
101         wxListItem item;
102         item.SetId(0);
103         item.SetText(_("Name"));
104         item.SetWidth(228);
105         
106         lstResults->InsertColumn(0, item);
107         
108         item.SetId(1);
109         item.SetText(_("Nickname"));
110         item.SetWidth(128);
111         
112         lstResults->InsertColumn(1, item);
113         
114         item.SetId(2);
115         item.SetText(_("Account"));
116         item.SetWidth(128);
117         
118         lstResults->InsertColumn(2, item);
119         
122 void frmSearch::SelectAccounts( wxCommandEvent& event )
125         // Create the form and display as modal.
127         frmSearchAccounts *frameSAccount = new frmSearchAccounts ( this );
128         frameSAccount->LoadSearchAccounts(&SearchAccounts, &SearchAccountsNames);
129         frameSAccount->ShowModal();
130         delete frameSAccount;
131         frameSAccount = NULL;
135 void frmSearch::SearchContacts( wxCommandEvent& event )
138         // Check if any accounts has been selected.
139         
140         bool AccountsFound = false;
141         
142         for (std::map<wxString, bool>::iterator saiter = SearchAccounts.begin(); 
143                 saiter != SearchAccounts.end(); saiter++){
144                 
145                 if (saiter->second == true){
146                         
147                         AccountsFound = true;
148                         break;
149                         
150                 }
151                         
152         }
153         
154         if (AccountsFound == false){
155         
156                 wxMessageBox(_("No accounts have been selected to search contacts for."),
157                         _("No accounts selected"), 
158                         wxOK|wxICON_ERROR);
159                 return;
160                 
161         }
162         
163         // Change the button to stop.
164         
165         if (StopMode == FALSE){
166         
167                 // Clear the list of search results.
168                 
169                 lstResults->DeleteAllItems();
170                 SearchResultAccount.clear();
171                 SearchResultFilename.clear();
172         
173                 // Button clicked on as 'Search'.
174         
175                 DisableAllSearchSettings(TRUE);
176                 StopMode = TRUE;
177                 btnSearch->SetLabel(_("Stop")); 
178                 
179                 // Spawn a thread so that searching can proceed
180                 // without blocking the GUI (and allow the
181                 // search to stop when needed).
182                 
183                 std::thread SearchThread(&frmSearch::SearchContactsThread, this);
184                 SearchThread.detach();
185         
186         } else {
187         
188                 // Button clicked on as 'Stop'.
189         
190                 StopMode = FALSE;
191                 btnSearch->SetLabel(_("Search"));
192                 DisableAllSearchSettings(FALSE);
193         
194         }
198 void frmSearch::ResetContacts( wxCommandEvent& event )
201         // Clear all the search settings.
203         for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
204          iter != SearchFrames.end(); ++iter){
205          
206                 XABSearchPanel *XABSPPtr;
207          
208                 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
209                 szrSearch->Detach(XABSPPtr);
211                 // Remove the frame from the memory and the list.
212         
213                 delete XABSPPtr;
214                 XABSPPtr = NULL;
215                 
216                 SearchFrames.erase(iter->first);
217                  
218         }
219         
220         // Clear the list of search results.
221         
222         lstResults->DeleteAllItems();
223         
224         // Clear the account settings.
226         for (std::map<wxString, bool>::iterator iter = SearchAccounts.begin();
227                 iter != SearchAccounts.end(); iter++){
228         
229                 iter->second = TRUE;
230                 
231         }
233         // Add a search settings with the default things.
234         
235         ScrollGen = 0;
237         XABSearchPanel *NewPanel = new XABSearchPanel( tabSearch );
238         NewPanel->EnableButtons(TRUE, FALSE);
239         NewPanel->SetupPointers(this);
240         NewPanel->SetupInteger(ScrollGen);
241         szrSearch->Add(NewPanel, 1, wxEXPAND, 0);
242         szrSearch->Fit(tabSearch);
243         SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
244         ScrollGen++;
248 void frmSearch::AddSearchSetting( wxCommandEvent& event )
251         // Add a search setting frame to the list.
253         XABSearchPanel *NewPanel = new XABSearchPanel( tabSearch );
254         NewPanel->EnableButtons(TRUE, TRUE);
255         NewPanel->SetupPointers(this);
256         NewPanel->SetupInteger(ScrollGen);
257         szrSearch->Add(NewPanel, 1, wxEXPAND|wxGROW, 5);
258         //szrSearch->Fit(tabSearch);
259         szrSearch->FitInside(tabSearch);
260         szrSearch->Layout();
261         szrSearch->RecalcSizes();
262         tabSearch->Layout();
263         SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
264         ScrollGen++;
265         
266         // Check if number of search settings is SEARCHSETTINGS_MAX (or over).
267         // If it is, disable the option of adding extra settings
268         // for all frames until one is removed.
270         XABSearchPanel *XABSPPtr;
271         
272         if (SearchFrames.size() >= SEARCHSETTINGS_MAX){
273         
274                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
275                 iter != SearchFrames.end(); ++iter){
276          
277                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
278          
279                         XABSPPtr->EnableButtons(FALSE, TRUE);
280          
281                 }
282         
283         } else if (SearchFrames.size() >= 2){
285                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
286                 iter != SearchFrames.end(); ++iter){
287          
288                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
289          
290                         XABSPPtr->EnableButtons(TRUE, TRUE);
291          
292                 }
293         
294         }
296         //tabSearch->SetScrollbar(wxVERTICAL, 32768, 25, 32768, false);
297         tabSearch->Update();
298         //tabSearch->ScrollToLine(tabSearch->GetLineCount());
299         
302 void frmSearch::RemoveSearchSetting( wxCommandEvent& event )
305         // Get the integer from the event.
306         
307         std::map<int,void*>::iterator iter;
308         iter = SearchFrames.find(event.GetInt());
310         // Remove a search setting frame from the list.
312         XABSearchPanel *XABSPPtr;
313         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
314         
315         szrSearch->Detach(XABSPPtr);
317         // Remove the frame from the memory and the list.
319         SearchFrames.erase(event.GetInt());
320         
321         delete XABSPPtr;
322         XABSPPtr = NULL;
323         
324         //szrSearch->Fit(tabSearch);
325         
326         if (SearchFrames.size() < SEARCHSETTINGS_MAX && SearchFrames.size() > 1){
327         
328                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
329                 iter != SearchFrames.end(); ++iter){
330          
331                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
332          
333                         XABSPPtr->EnableButtons(TRUE, TRUE);
334          
335                 }
336         
337         } else if (SearchFrames.size() == 1){
339                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
340                 iter != SearchFrames.end(); ++iter){
341          
342                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
343          
344                         XABSPPtr->EnableButtons(TRUE, FALSE);
345          
346                 }
347         
348         }
350         szrSearch->FitInside(tabSearch);
351         szrSearch->Layout();
352         szrSearch->RecalcSizes();
353         tabSearch->Layout();
357 void frmSearch::ReloadAccountList( wxCommandEvent& event ){
359         // Currently unimplemented.
363 void frmSearch::SearchBarUpdate( wxCommandEvent& event ){
365         wxString *SBData = (wxString*)event.GetClientData();
367         stbBottom->SetStatusText(*SBData, 0);
368         
369         delete SBData;
370         SBData = NULL;
374 void frmSearch::SearchFinished( wxCommandEvent& event ){
376         StopMode = FALSE;
377         btnSearch->SetLabel(_("Search"));
378         DisableAllSearchSettings(FALSE);
382 void frmSearch::DisableAllSearchSettings(bool Enable){
384         // Check if there is only one search value. If there is, only enable
385         // the add button.
386         
387         if (SearchFrames.size() == 1){
388         
389                 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(SearchFrames.begin()->second);
390                 XABSPPtr->EnableButtons(TRUE, FALSE);
391                 return;
392                 
393         }
394         
395         // More than one control, so process them.
396         
397         for (std::map<int, void*>::iterator siter = SearchFrames.begin();
398                 siter != SearchFrames.end(); siter++){
399         
400                 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
401                 
402                 if (Enable == FALSE){
403                 
404                         wxCommandEvent enboxes(XABSP_ENABLECONTROLS);
405                         wxPostEvent(XABSPPtr, enboxes);
406                 
407                 } else if (Enable == TRUE){
408                 
409                         wxCommandEvent disboxes(XABSP_DISABLECONTROLS);
410                         wxPostEvent(XABSPPtr, disboxes);
411                 
412                 }
413                 
414         }
418 void frmSearch::CloseWindow( wxCloseEvent& event ){
420         // Hide the window so users don't panic
421         // whilst clearing up.
423         this->Hide();
424         
425         // Clear up.
426         
427         for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
428          iter != SearchFrames.end(); ++iter){
429          
430                 XABSearchPanel *XABSPPtr;
431          
432                 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
433                 szrSearch->Detach(XABSPPtr);
435                 // Remove the frame from the memory and the list.
436         
437                 delete XABSPPtr;
438                 XABSPPtr = NULL;
439                  
440         }
442         SearchFrames.clear();
443         
444         // Close window.
445         
446         WindowData *WData = new WindowData;
448         WData->DataType = 2;
449         WData->WindowPointer = this;
450         WData->WindowID = SearchUID;
452         wxCommandEvent delevent(WINDOW_CLOSE);
453         delevent.SetClientData(WData);
454         wxPostEvent(GetParent(), delevent);
455                 
456         wxCommandEvent rs(CE_REMOVESEARCH);
457         wxPostEvent(this, rs);
458         
459         WData = NULL;
460         
461         this->Destroy();
465 void frmSearch::CloseWindow( wxCommandEvent& event ){
467         this->Close();
471 void frmSearch::SetUID(int UID){
473         SearchUID = UID;
477 void frmSearch::SetSearchMode(bool SearchModeIn){
479         SearchMode = SearchModeIn;
481         if (SearchMode == TRUE){
482         
483                 mnuContactEdit->Enable(FALSE);
484                 mnuContactReveal->Enable(FALSE);
485                 
486                 wxFileSystem::AddHandler(new wxMemoryFSHandler);
487                 wxImage ciicon_png;
489                 wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png));
490                 ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG);
491                 wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG);
493                 wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png));
494                 ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG);
495                 wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG);
497                 wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png));
498                 ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG);
499                 wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG);
501                 wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png));
502                 ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG);
503                 wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG);
505                 wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png));
506                 ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG);
507                 wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG);
509                 wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png));
510                 ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG);
511                 wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG);
512         
513         } else {
514         
515                 mnuContactEdit->Enable(TRUE);
516                 mnuContactReveal->Enable(TRUE);
517         
518         }
519         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy