Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added check to see if any accounts are selected.
[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_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)
53 END_EVENT_TABLE()
55 frmSearch::frmSearch( wxWindow* parent )
56 :
57 frmSearchADT( parent )
58 {
59         
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));
67         ScrollGen++;
68         
69         // Get the list of accounts and mark them as true.
71         wxString preffilename = GetUserPrefDir();
73         XABPreferences preferences(preffilename);       
74         
75         wxString AccDir;
76         wxString AccDirFull;
77         wxString AccDirFullSfx;
78         
79         for (int i = 0; i < preferences.accounts.GetCount(); i++){
80                 AccDir = preferences.accounts.GetAccountDirectory(i);
81                 AccDirFull = preferences.accounts.GetAccountDirectory(i);
82                 AccDirFull.Trim();
83                 AccDirFull.Append(wxT("."));
84                 AccDirFullSfx.Append(preferences.accounts.GetAccountType(i));
85                 AccDirFullSfx.LowerCase();
86                 AccDirFullSfx.Trim();
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)));
92                 AccDirFull.clear();
93                 AccDirFullSfx.clear();
94                 AccDir.clear();
95         }
96         
97         // Setup the columns.
98         
99         wxListItem item;
100         item.SetId(0);
101         item.SetText(_("Name"));
102         item.SetWidth(228);
103         
104         lstResults->InsertColumn(0, item);
105         
106         item.SetId(1);
107         item.SetText(_("Nickname"));
108         item.SetWidth(128);
109         
110         lstResults->InsertColumn(1, item);
111         
112         item.SetId(2);
113         item.SetText(_("Account"));
114         item.SetWidth(128);
115         
116         lstResults->InsertColumn(2, item);
117         
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.
137         
138         bool AccountsFound = false;
139         
140         for (std::map<wxString, bool>::iterator saiter = SearchAccounts.begin(); 
141                 saiter != SearchAccounts.end(); saiter++){
142                 
143                 if (saiter->second == true){
144                         
145                         AccountsFound = true;
146                         break;
147                         
148                 }
149                         
150         }
151         
152         if (AccountsFound == false){
153         
154                 wxMessageBox(_("No accounts have been selected to search contacts for."),
155                         _("No accounts selected"), 
156                         wxOK|wxICON_ERROR);
157                 return;
158                 
159         }
160         
161         // Change the button to stop.
162         
163         if (StopMode == FALSE){
164         
165                 // Clear the list of search results.
166                 
167                 lstResults->DeleteAllItems();
168                 SearchResultAccount.clear();
169                 SearchResultFilename.clear();
170         
171                 // Button clicked on as 'Search'.
172         
173                 DisableAllSearchSettings(TRUE);
174                 StopMode = TRUE;
175                 btnSearch->SetLabel(_("Stop")); 
176                 
177                 // Spawn a thread so that searching can proceed
178                 // without blocking the GUI (and allow the
179                 // search to stop when needed).
180                 
181                 std::thread SearchThread(&frmSearch::SearchContactsThread, this);
182                 SearchThread.detach();
183         
184         } else {
185         
186                 // Button clicked on as 'Stop'.
187         
188                 StopMode = FALSE;
189                 btnSearch->SetLabel(_("Search"));
190                 DisableAllSearchSettings(FALSE);
191         
192         }
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){
203          
204                 XABSearchPanel *XABSPPtr;
205          
206                 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
207                 szrSearch->Detach(XABSPPtr);
209                 // Remove the frame from the memory and the list.
210         
211                 delete XABSPPtr;
212                 XABSPPtr = NULL;
213                 
214                 SearchFrames.erase(iter->first);
215                  
216         }
217         
218         // Clear the list of search results.
219         
220         lstResults->DeleteAllItems();
221         
222         // Clear the account settings.
224         for (std::map<wxString, bool>::iterator iter = SearchAccounts.begin();
225                 iter != SearchAccounts.end(); iter++){
226         
227                 iter->second = TRUE;
228                 
229         }
231         // Add a search settings with the default things.
232         
233         ScrollGen = 0;
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));
242         ScrollGen++;
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);
258         szrSearch->Layout();
259         szrSearch->RecalcSizes();
260         tabSearch->Layout();
261         SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
262         ScrollGen++;
263         
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;
269         
270         if (SearchFrames.size() >= 15){
271         
272                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
273                 iter != SearchFrames.end(); ++iter){
274          
275                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
276          
277                         XABSPPtr->EnableButtons(FALSE, TRUE);
278          
279                 }
280         
281         } else if (SearchFrames.size() >= 2){
283                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
284                 iter != SearchFrames.end(); ++iter){
285          
286                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
287          
288                         XABSPPtr->EnableButtons(TRUE, TRUE);
289          
290                 }
291         
292         }
294         //tabSearch->SetScrollbar(wxVERTICAL, 32768, 25, 32768, false);
295         tabSearch->Update();
296         //tabSearch->ScrollToLine(tabSearch->GetLineCount());
297         
300 void frmSearch::RemoveSearchSetting( wxCommandEvent& event )
303         // Get the integer from the event.
304         
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);
312         
313         szrSearch->Detach(XABSPPtr);
315         // Remove the frame from the memory and the list.
317         SearchFrames.erase(event.GetInt());
318         
319         delete XABSPPtr;
320         XABSPPtr = NULL;
321         
322         //szrSearch->Fit(tabSearch);
323         
324         if (SearchFrames.size() < 15 && SearchFrames.size() > 1){
325         
326                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
327                 iter != SearchFrames.end(); ++iter){
328          
329                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
330          
331                         XABSPPtr->EnableButtons(TRUE, TRUE);
332          
333                 }
334         
335         } else if (SearchFrames.size() == 1){
337                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
338                 iter != SearchFrames.end(); ++iter){
339          
340                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
341          
342                         XABSPPtr->EnableButtons(TRUE, FALSE);
343          
344                 }
345         
346         }
348         szrSearch->FitInside(tabSearch);
349         szrSearch->Layout();
350         szrSearch->RecalcSizes();
351         tabSearch->Layout();
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);
366         
367         delete SBData;
368         SBData = NULL;
372 void frmSearch::SearchFinished( wxCommandEvent& event ){
374         StopMode = FALSE;
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++){
384         
385                 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
386                 
387                 if (Enable == FALSE){
388                 
389                         wxCommandEvent enboxes(XABSP_ENABLECONTROLS);
390                         wxPostEvent(XABSPPtr, enboxes);
391                 
392                 } else if (Enable == TRUE){
393                 
394                         wxCommandEvent disboxes(XABSP_DISABLECONTROLS);
395                         wxPostEvent(XABSPPtr, disboxes);
396                 
397                 }
398                 
399         }
403 void frmSearch::CloseWindow( wxCloseEvent& event ){
405         // Hide the window so users don't panic
406         // whilst clearing up.
408         this->Hide();
409         
410         // Clear up.
411         
412         for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
413          iter != SearchFrames.end(); ++iter){
414          
415                 XABSearchPanel *XABSPPtr;
416          
417                 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
418                 szrSearch->Detach(XABSPPtr);
420                 // Remove the frame from the memory and the list.
421         
422                 delete XABSPPtr;
423                 XABSPPtr = NULL;
424                  
425         }
427         SearchFrames.clear();
428         
429         // Close window.
430         
431         WindowData *WData = new WindowData;
433         WData->DataType = 2;
434         WData->WindowPointer = this;
435         WData->WindowID = SearchUID;
437         wxCommandEvent delevent(WINDOW_CLOSE);
438         delevent.SetClientData(WData);
439         wxPostEvent(GetParent(), delevent);
440                 
441         wxCommandEvent rs(CE_REMOVESEARCH);
442         wxPostEvent(this, rs);
443         
444         WData = NULL;
445         
446         this->Destroy();
450 void frmSearch::CloseWindow( wxCommandEvent& event ){
452         this->Close();
456 void frmSearch::SetUID(int UID){
458         SearchUID = UID;
462 void frmSearch::SetSearchMode(bool SearchModeIn){
464         SearchMode = SearchModeIn;
466         if (SearchMode == TRUE){
467         
468                 mnuContactEdit->Enable(FALSE);
469                 mnuContactReveal->Enable(FALSE);
470                 
471                 wxFileSystem::AddHandler(new wxMemoryFSHandler);
472                 wxImage ciicon_png;
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);
497         
498         } else {
499         
500                 mnuContactEdit->Enable(TRUE);
501                 mnuContactReveal->Enable(TRUE);
502         
503         }
504         
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