Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added source code, header and unit testing for the EMAIL vCard property for the Conta...
[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         // Change the button to stop.
137         
138         if (StopMode == FALSE){
139         
140                 // Clear the list of search results.
141                 
142                 lstResults->DeleteAllItems();
143                 SearchResultAccount.clear();
144                 SearchResultFilename.clear();
145         
146                 // Button clicked on as 'Search'.
147         
148                 DisableAllSearchSettings(TRUE);
149                 StopMode = TRUE;
150                 btnSearch->SetLabel(_("Stop")); 
151                 
152                 // Spawn a thread so that searching can proceed
153                 // without blocking the GUI (and allow the
154                 // search to stop when needed).
155                 
156                 std::thread SearchThread(&frmSearch::SearchContactsThread, this);
157                 SearchThread.detach();
158         
159         } else {
160         
161                 // Button clicked on as 'Stop'.
162         
163                 StopMode = FALSE;
164                 btnSearch->SetLabel(_("Search"));
165                 DisableAllSearchSettings(FALSE);
166         
167         }
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){
178          
179                 XABSearchPanel *XABSPPtr;
180          
181                 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
182                 szrSearch->Detach(XABSPPtr);
184                 // Remove the frame from the memory and the list.
185         
186                 delete XABSPPtr;
187                 XABSPPtr = NULL;
188                 
189                 SearchFrames.erase(iter->first);
190                  
191         }
192         
193         // Clear the list of search results.
194         
195         lstResults->DeleteAllItems();
196         
197         // Clear the account settings.
199         for (std::map<wxString, bool>::iterator iter = SearchAccounts.begin();
200                 iter != SearchAccounts.end(); iter++){
201         
202                 iter->second = TRUE;
203                 
204         }
206         // Add a search settings with the default things.
207         
208         ScrollGen = 0;
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));
217         ScrollGen++;
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);
233         szrSearch->Layout();
234         szrSearch->RecalcSizes();
235         tabSearch->Layout();
236         SearchFrames.insert(std::make_pair(ScrollGen, NewPanel));
237         ScrollGen++;
238         
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;
244         
245         if (SearchFrames.size() >= 15){
246         
247                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
248                 iter != SearchFrames.end(); ++iter){
249          
250                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
251          
252                         XABSPPtr->EnableButtons(FALSE, TRUE);
253          
254                 }
255         
256         } else if (SearchFrames.size() >= 2){
258                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
259                 iter != SearchFrames.end(); ++iter){
260          
261                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
262          
263                         XABSPPtr->EnableButtons(TRUE, TRUE);
264          
265                 }
266         
267         }
269         //tabSearch->SetScrollbar(wxVERTICAL, 32768, 25, 32768, false);
270         tabSearch->Update();
271         //tabSearch->ScrollToLine(tabSearch->GetLineCount());
272         
275 void frmSearch::RemoveSearchSetting( wxCommandEvent& event )
278         // Get the integer from the event.
279         
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);
287         
288         szrSearch->Detach(XABSPPtr);
290         // Remove the frame from the memory and the list.
292         SearchFrames.erase(event.GetInt());
293         
294         delete XABSPPtr;
295         XABSPPtr = NULL;
296         
297         //szrSearch->Fit(tabSearch);
298         
299         if (SearchFrames.size() < 15 && SearchFrames.size() > 1){
300         
301                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
302                 iter != SearchFrames.end(); ++iter){
303          
304                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
305          
306                         XABSPPtr->EnableButtons(TRUE, TRUE);
307          
308                 }
309         
310         } else if (SearchFrames.size() == 1){
312                 for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
313                 iter != SearchFrames.end(); ++iter){
314          
315                         XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
316          
317                         XABSPPtr->EnableButtons(TRUE, FALSE);
318          
319                 }
320         
321         }
323         szrSearch->FitInside(tabSearch);
324         szrSearch->Layout();
325         szrSearch->RecalcSizes();
326         tabSearch->Layout();
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);
341         
342         delete SBData;
343         SBData = NULL;
347 void frmSearch::SearchFinished( wxCommandEvent& event ){
349         StopMode = FALSE;
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++){
359         
360                 XABSearchPanel *XABSPPtr = static_cast<XABSearchPanel*>(siter->second);
361                 
362                 if (Enable == FALSE){
363                 
364                         wxCommandEvent enboxes(XABSP_ENABLECONTROLS);
365                         wxPostEvent(XABSPPtr, enboxes);
366                 
367                 } else if (Enable == TRUE){
368                 
369                         wxCommandEvent disboxes(XABSP_DISABLECONTROLS);
370                         wxPostEvent(XABSPPtr, disboxes);
371                 
372                 }
373                 
374         }
378 void frmSearch::CloseWindow( wxCloseEvent& event ){
380         // Hide the window so users don't panic
381         // whilst clearing up.
383         this->Hide();
384         
385         // Clear up.
386         
387         for (std::map<int,void*>::iterator iter = SearchFrames.begin(); 
388          iter != SearchFrames.end(); ++iter){
389          
390                 XABSearchPanel *XABSPPtr;
391          
392                 XABSPPtr = static_cast<XABSearchPanel*>(iter->second);
393                 szrSearch->Detach(XABSPPtr);
395                 // Remove the frame from the memory and the list.
396         
397                 delete XABSPPtr;
398                 XABSPPtr = NULL;
399                  
400         }
402         SearchFrames.clear();
403         
404         // Close window.
405         
406         WindowData *WData = new WindowData;
408         WData->DataType = 2;
409         WData->WindowPointer = this;
410         WData->WindowID = SearchUID;
412         wxCommandEvent delevent(WINDOW_CLOSE);
413         delevent.SetClientData(WData);
414         wxPostEvent(GetParent(), delevent);
415                 
416         wxCommandEvent rs(CE_REMOVESEARCH);
417         wxPostEvent(this, rs);
418         
419         WData = NULL;
420         
421         this->Destroy();
425 void frmSearch::CloseWindow( wxCommandEvent& event ){
427         this->Close();
431 void frmSearch::SetUID(int UID){
433         SearchUID = UID;
437 void frmSearch::SetSearchMode(bool SearchModeIn){
439         SearchMode = SearchModeIn;
441         if (SearchMode == TRUE){
442         
443                 mnuContactEdit->Enable(FALSE);
444                 mnuContactReveal->Enable(FALSE);
445                 
446                 wxFileSystem::AddHandler(new wxMemoryFSHandler);
447                 wxImage ciicon_png;
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);
472         
473         } else {
474         
475                 mnuContactEdit->Enable(TRUE);
476                 mnuContactReveal->Enable(TRUE);
477         
478         }
479         
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