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 SEARCHLIST_H
27#define SEARCHLIST_H
28
29#include "Timer.h"				// Needed for CTimer
30#include "ObservableQueue.h"	// Needed for CQueueObserver
31#include "SearchFile.h"			// Needed for CSearchFile
32#include <memory>		// Do_not_auto_remove (lionel's Mac, 10.3)
33
34
35class CMemFile;
36class CMD4Hash;
37class CPacket;
38class CServer;
39class CSearchFile;
40
41namespace Kademlia {
42	class CUInt128;
43}
44
45
46enum SearchType {
47	LocalSearch,
48	GlobalSearch,
49	KadSearch
50};
51
52
53typedef std::vector<CSearchFile*> CSearchResultList;
54
55
56class CSearchList : public wxEvtHandler
57{
58public:
59	//! Structure used to pass search-parameters.
60	struct CSearchParams
61	{
62		/** Prevents accidential use of uninitialized variables. */
63		CSearchParams() { minSize = maxSize = availability = 0; }
64
65		//! The actual string to search for.
66		wxString searchString;
67		//! The type of files to search for (may be empty), one of ED2KFTSTR_*
68		wxString typeText;
69		//! The filename extension. May be empty.
70		wxString extension;
71		//! The smallest filesize in bytes to accept, zero for any.
72		uint64_t minSize;
73		//! The largest filesize in bytes to accept, zero for any.
74		uint64_t maxSize;
75		//! The minumum available (source-count), zero for any.
76		uint32_t availability;
77	};
78
79	/** Constructor. */
80	CSearchList();
81
82	/** Frees any remaining search-results. */
83	~CSearchList();
84
85	/**
86	 * Starts a new search.
87	 *
88	 * @param searchID The ID of the search, which may be modified.
89	 * @param type The type of search, see SearchType.
90	 * @param params The search parameters, see CSearchParams.
91	 * @return An empty string on success, otherwise an error-message.
92	 */
93	wxString StartNewSearch(uint32* searchID, SearchType type, const CSearchParams& params);
94
95	/** Stops the current search (global or Kad), if any is in progress. */
96	void StopSearch(bool globalOnly = false);
97
98	/** Returns the completion percentage of the current search. */
99	uint32 GetSearchProgress() const;
100
101	/** This function is called once the local (ed2k) search has ended. */
102	void	LocalSearchEnd();
103
104
105	/**
106	 * Returns the list of results for the specified search.
107	 *
108	 * If the search is not valid, an empty list is returned.
109	 */
110	const 	CSearchResultList& GetSearchResults(long searchID) const;
111
112	/** Removes all results for the specified search. */
113	void	RemoveResults(long searchID);
114
115
116	/** Finds the search-result (by hash) and downloads it in the given category. */
117	void	AddFileToDownloadByHash(const CMD4Hash& hash, uint8 category = 0);
118
119
120	/**
121	 * Processes a list of shared files from a client.
122	 *
123	 * @param packet The raw packet received from the client.
124	 * @param size the length of the packet.
125	 * @param sender The sender of the packet.
126	 * @param moreResultsAvailable Set to a value specifying if more results are available.
127	 * @param directory The directory containing the shared files.
128	 */
129	void	ProcessSharedFileList(const byte* packet, uint32 size, CUpDownClient* sender, bool* moreResultsAvailable, const wxString& directory);
130
131	/**
132	 * Processes a search-result sent via TCP from the local server. All results are added.
133	 *
134	 * @param packet The packet containing one or more search-results.
135	 * @param size the length of the packet.
136	 * @param optUTF8 Specifies if the server supports UTF8.
137	 * @param serverIP The IP of the server sending the results.
138	 * @param serverPort The Port of the server sending the results.
139	 */
140	void	ProcessSearchAnswer(const uint8_t* packet, uint32_t size, bool optUTF8, uint32_t serverIP, uint16_t serverPort);
141
142	/**
143	 * Processes a search-result sent via UDP. Only one result is read from the packet.
144	 *
145	 * @param packet The packet containing one or more search-results.
146	 * @param optUTF8 Specifies if the server supports UTF8.
147	 * @param serverIP The IP of the server sending the results.
148	 * @param serverPort The Port of the server sending the results.
149	 */
150	void	ProcessUDPSearchAnswer(const CMemFile& packet, bool optUTF8, uint32 serverIP, uint16 serverPort);
151
152
153	/**
154	 * Adds a result in the form of a kad search-keyword to the specified result-list.
155	 *
156	 * @param searchID The search to which this result belongs.
157	 * @param fileID The hash of the result-file.
158	 * @param name The filename of the result.
159	 * @param size The filesize of the result.
160	 * @param type The filetype of the result (TODO: Not used?)
161	 * @param kadPublishInfo The kademlia publish information of the result.
162	 * @param taglist List of additional tags associated with the search-result.
163	 */
164	void	KademliaSearchKeyword(uint32_t searchID, const Kademlia::CUInt128 *fileID, const wxString& name, uint64_t size, const wxString& type, uint32_t kadPublishInfo, const TagPtrList& taglist);
165
166	/** Update a certain search result in all lists */
167	void UpdateSearchFileByHash(const CMD4Hash& hash);
168
169	/** Mark current KAD search as finished */
170	void SetKadSearchFinished() { m_KadSearchFinished = true; }
171
172private:
173	/** Event-handler for global searches. */
174	void OnGlobalSearchTimer(CTimerEvent& evt);
175
176	/**
177	 * Adds the specified file to the current search's results.
178	 *
179	 * @param toadd The result to add.
180	 * @param clientResponse Is the result sent by a client (shared-files list).
181	 * @return True if the results were added, false otherwise.
182	 *
183	 * Note that this function takes ownership of the CSearchFile object,
184	 * regardless of whenever or not it was actually added to the results list.
185	 */
186	bool AddToList(CSearchFile* toadd, bool clientResponse = false);
187
188	//! This auto-pointer is used to safely prevent leaks.
189	typedef std::auto_ptr<CMemFile> CMemFilePtr;
190
191	/** Create a basic search-packet for the given search-type. */
192	CMemFilePtr CreateSearchData(const CSearchParams& params, SearchType type, bool supports64bit, bool& packetUsing64bit);
193
194
195	//! Timer used for global search intervals.
196	CTimer	m_searchTimer;
197
198	//! The current search-type, regarding the last/current search.
199	SearchType	m_searchType;
200
201	//! Specifies if a search is being performed.
202	bool		m_searchInProgress;
203
204	//! The ID of the current search.
205	long		m_currentSearch;
206
207	//! The current packet used for searches.
208	CPacket*	m_searchPacket;
209
210	//! Does the current search packet contain 64bit values?
211	bool		m_64bitSearchPacket;
212
213	//! If the current search is a KAD search this signals if it is finished.
214	bool		m_KadSearchFinished;
215
216	//! Queue of servers to ask when doing global searches.
217	//! TODO: Replace with 'cookie' system.
218	CQueueObserver<CServer*> m_serverQueue;
219
220	//! Shorthand for the map of results (key is a SearchID).
221	typedef std::map<long, CSearchResultList> ResultMap;
222
223	//! Map of all search-results added.
224	ResultMap	m_results;
225
226	//! Contains the results type desired in the current search.
227	//! If not empty, results of different types are filtered.
228	wxString	m_resultType;
229
230
231	DECLARE_EVENT_TABLE()
232};
233
234
235#endif // SEARCHLIST_H
236// File_checked_for_headers
237