1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
24 //
25 
26 #ifndef SEARCHLISTCTRL_H
27 #define SEARCHLISTCTRL_H
28 
29 
30 #include <wx/colour.h>		// Needed for wxColour
31 #include <wx/regex.h>		// Needed for wxRegExp
32 
33 #include "MuleListCtrl.h"	// Needed for CMuleListCtrl
34 
35 
36 class CSearchList;
37 class CSearchFile;
38 
39 
40 /**
41  * This class is used to display search results.
42  *
43  * Results on added to the list will be colored according to
44  * the number of sources and other parameters (see UpdateColor).
45  *
46  * To display results, first use the ShowResults function, which will display
47  * all current results with the specified id and afterwards you can use the
48  * AddResult function to add new results or the UpdateResult function to update
49  * already present results. Please note that it is not possible to add results
50  * with the AddResult function before calling ShowResults.
51  */
52 class CSearchListCtrl : public CMuleListCtrl
53 {
54 public:
55 	/**
56 	 * Constructor.
57 	 *
58 	 * @see CMuleListCtrl::CMuleListCtrl for documentation of parameters.
59 	 */
60 	 CSearchListCtrl(
61 	            wxWindow *parent,
62                 wxWindowID winid = -1,
63                 const wxPoint &pos = wxDefaultPosition,
64                 const wxSize &size = wxDefaultSize,
65                 long style = wxLC_ICON,
66                 const wxValidator& validator = wxDefaultValidator,
67                 const wxString &name = wxT("mulelistctrl") );
68 
69 	/**
70 	 * Destructor.
71 	 */
72 	virtual ~CSearchListCtrl();
73 
74 	/**
75 	 * Adds ths specified file to the list.
76 	 *
77 	 * @param The new result to be shown.
78 	 *
79 	 * Please note that no duplicates checking is done, so the pointer should
80 	 * point to a new file in order to avoid problems. Also note that the result
81 	 * will be inserted sorted according to current sort-type, so there is no
82 	 * need to resort the list after adding new items.
83 	 */
84 	void	AddResult(CSearchFile* toshow);
85 
86 	/**
87 	 * Removes the specified file from the list.
88 	 */
89 	void	RemoveResult(CSearchFile* toshow);
90 
91 	/**
92 	 * Updates the specified source.
93 	 *
94 	 * @param The search result to be updated.
95 	 */
96 	void	UpdateResult(CSearchFile* toupdate);
97 
98 	/**
99 	 * Clears the list and inserts all results with the specified Id instead.
100 	 *
101 	 * @param nResult The ID of the results or Zero to simply reset the list.
102 	 */
103 	void	ShowResults( long ResultsId );
104 
105 	/**
106 	 * Updates the colors of item at the specified index.
107 	 *
108 	 * @param index The zero-based index of the item.
109 	 *
110 	 * This function sets the color of the item based on the following:
111 	 *  - Downloading files are marked in red.
112 	 *  - Known (shared/completed) files are marked in green.
113 	 *  - New files are marked in blue depending on the number of sources.
114 	 *  - Canceled files are marked in magenta.
115 	 */
116 	void UpdateItemColor(long index);
117 
118 	/**
119 	 * Returns the current Search Id.
120 	 *
121 	 * @return The Search Id of the displayed results (set through ShowResults()).
122 	 */
123 	wxUIntPtr	GetSearchId();
124 
125 	/**
126 	 * Sets the filter which decides which results should be shown.
127 	 *
128 	 * @param regExp A regular expression targeting the filenames.
129 	 * @param invert If true, invert the results of the filter-test.
130 	 * @param filterKnown Should files that are queued or known be filtered out.
131 	 *
132 	 * An invalid regExp will result in all results being displayed.
133 	 */
134 	void	SetFilter(const wxString& regExp, bool invert, bool filterKnown);
135 
136 	/**
137 	 * Toggles the use of filtering on and off.
138 	 */
139 	void	EnableFiltering(bool enabled);
140 
141 	/**
142 	 * Returns the number of items hidden due to filtering.
143 	 */
144 	size_t	GetHiddenItemCount() const;
145 
146 	/**
147 	 * Attempts to download all selected items, updating color-scheme as needed.
148 	 *
149 	 * @param category The target category, or -1 to use the drop-down selection.
150 	 */
151 	void	DownloadSelected(int category = -1);
152 
153 	static wxString DetermineStatusPrintable(CSearchFile *toshow);
154 
155 protected:
156 	/// Return old column order.
157 	wxString GetOldColumnOrder() const;
158 
159 	/**
160 	 * Set the sort column
161 	 *
162 	 * @param column The column with which the list should be sorted.
163 	 * @param order The order in which to sort the column.
164 	 *
165 	 * Note that attempting to sort a column in an unsupported order
166 	 * is an illegal operation.
167 	 */
168 	void SetSorting(unsigned column, unsigned order);
169 
170 protected:
171 	typedef std::list<CSearchFile*> ResultList;
172 
173 	//! List used to store results that are hidden due to matching the filter.
174 	ResultList	m_filteredOut;
175 
176 	//! The current filter reg-exp.
177 	wxRegEx		m_filter;
178 
179 	//! The text from which the filter is compiled.
180 	wxString	m_filterText;
181 
182 	//! Controls if shared/queued results should be shown.
183 	bool		m_filterKnown;
184 
185 	//! Controls if the result of filter-hits should be inverted
186 	bool		m_invert;
187 
188 	//! Specifies if filtering should be used
189 	bool		m_filterEnabled;
190 
191 	/**
192 	 * Returns true if the filename is filtered.
193 	 */
194 	bool	IsFiltered(const CSearchFile* file);
195 
196 	/**
197 	 * Sorter function used by wxListCtrl::SortItems function.
198 	 *
199 	 * @see CMuleListCtrl::SetSortFunc
200 	 * @see wxListCtrl::SortItems
201 	 */
202 	static int wxCALLBACK SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData);
203 
204 	/** @see CMuleListCtrl::AltSortAllowed */
205 	virtual bool AltSortAllowed(unsigned column) const;
206 
207 	/** @see CMuleListCtrl::GetTTSText */
208 	virtual wxString GetTTSText(unsigned item) const;
209 
210 	/**
211 	 * Helper function which syncs two lists.
212 	 *
213 	 * @param src The source list.
214 	 * @param dst The list to be synced with the source list.
215 	 *
216 	 * This function syncronises the following settings of two lists:
217 	 *  - Sort column
218 	 *  - Sort direction
219 	 *  - Column widths
220 	 *
221 	 * If either sort column or direction is changed, then the dst list will
222 	 * be resorted. This function is used to ensure that all results list act
223 	 * as one, while still allowing individual selection.
224 	 */
225 	static void SyncLists( CSearchListCtrl* src, CSearchListCtrl* dst );
226 
227 	/**
228 	 * Helper function which syncs all other lists against the specified one.
229 	 *
230 	 * @param src The list which all other lists should be synced against.
231 	 *
232 	 * This function just calls SyncLists() on all lists in s_lists, using
233 	 * the src argument as the src argument of the SyncLists function.
234 	 */
235 	static void SyncOtherLists( CSearchListCtrl* src );
236 
237 	//! This list contains pointers to all current instances of CSearchListCtrl.
238 	static std::list<CSearchListCtrl*> s_lists;
239 
240 	//! The ID of the search-results which the list is displaying or zero if unset.
241 	wxUIntPtr m_nResultsID;
242 
243 	//! Custom drawing, needed to display children of search-results.
244 	void OnDrawItem(int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted);
245 
246 	/**
247 	 * Removes or adds child-entries for the given file.
248 	 */
249 	void ShowChildren(CSearchFile* file, bool show);
250 
251 	/**
252 	 * Event handler for right mouse clicks.
253 	 */
254 	void OnRightClick( wxListEvent& event );
255 
256 	/**
257 	 * Event handler for double-clicks or enter.
258 	 */
259 	void OnItemActivated( wxListEvent& event );
260 
261 	/**
262 	 * Event handler for left-clicks on the column headers.
263 	 *
264 	 * This eventhandler takes care of sync'ing all the other lists with this one.
265 	 */
266 	void OnColumnLClick( wxListEvent& event );
267 
268 	/**
269 	 * Event handler for resizing of the columns.
270 	 *
271 	 * This eventhandler takes care of sync'ing all the other lists with this one.
272 	 */
273 	void OnColumnResize( wxListEvent& event );
274 
275 	/**
276 	 * Event handler for get-url menu items.
277 	 */
278 	void OnPopupGetUrl( wxCommandEvent& event );
279 
280 	/**
281 	 * Event handler for Razorback 2 stats menu items.
282 	 */
283 	void OnRazorStatsCheck( wxCommandEvent& event );
284 
285 	/**
286 	 * Event handler for related search.
287 	 */
288 	void OnRelatedSearch( wxCommandEvent& event );
289 
290 	/**
291 	 * Event handler for "mark as known".
292 	 */
293 	void OnMarkAsKnown( wxCommandEvent& event );
294 
295 	/**
296 	 * Event handler for download-file(s) menu item.
297 	 */
298 	void OnPopupDownload( wxCommandEvent& event );
299 
300 	DECLARE_EVENT_TABLE()
301 };
302 
303 #endif // SEARCHLISTCTRL_H
304 // File_checked_for_headers
305