2 // This file is part of the aMule Project.
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 )
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
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.
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.
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
29 #include "Timer.h" // Needed for CTimer
30 #include "ObservableQueue.h" // Needed for CQueueObserver
31 #include "SearchFile.h" // Needed for CSearchFile
32 #include <common/SmartPtr.h> // Needed for CSmartPtr
53 typedef std::vector
<CSearchFile
*> CSearchResultList
;
56 class CSearchList
: public wxEvtHandler
59 //! Structure used to pass search-parameters.
62 /** Prevents accidential use of uninitialized variables. */
63 CSearchParams() { minSize
= maxSize
= availability
= 0; }
65 //! The actual string to search for.
66 wxString searchString
;
67 //! The keyword selected for Kad search
69 //! The type of files to search for (may be empty), one of ED2KFTSTR_*
71 //! The filename extension. May be empty.
73 //! The smallest filesize in bytes to accept, zero for any.
75 //! The largest filesize in bytes to accept, zero for any.
77 //! The minumum available (source-count), zero for any.
78 uint32_t availability
;
84 /** Frees any remaining search-results. */
88 * Starts a new search.
90 * @param searchID The ID of the search, which may be modified.
91 * @param type The type of search, see SearchType.
92 * @param params The search parameters, see CSearchParams.
93 * @return An empty string on success, otherwise an error-message.
95 wxString
StartNewSearch(uint32
* searchID
, SearchType type
, CSearchParams
& params
);
97 /** Stops the current search (global or Kad), if any is in progress. */
98 void StopSearch(bool globalOnly
= false);
100 /** Returns the completion percentage of the current search. */
101 uint32
GetSearchProgress() const;
103 /** This function is called once the local (ed2k) search has ended. */
104 void LocalSearchEnd();
108 * Returns the list of results for the specified search.
110 * If the search is not valid, an empty list is returned.
112 const CSearchResultList
& GetSearchResults(long searchID
) const;
114 /** Removes all results for the specified search. */
115 void RemoveResults(long searchID
);
118 /** Finds the search-result (by hash) and downloads it in the given category. */
119 void AddFileToDownloadByHash(const CMD4Hash
& hash
, uint8 category
= 0);
123 * Processes a list of shared files from a client.
125 * @param packet The raw packet received from the client.
126 * @param size the length of the packet.
127 * @param sender The sender of the packet.
128 * @param moreResultsAvailable Set to a value specifying if more results are available.
129 * @param directory The directory containing the shared files.
131 void ProcessSharedFileList(const uint8_t* packet
, uint32 size
, CUpDownClient
* sender
, bool* moreResultsAvailable
, const wxString
& directory
);
134 * Processes a search-result sent via TCP from the local server. All results are added.
136 * @param packet The packet containing one or more search-results.
137 * @param size the length of the packet.
138 * @param optUTF8 Specifies if the server supports UTF8.
139 * @param serverIP The IP of the server sending the results.
140 * @param serverPort The Port of the server sending the results.
142 void ProcessSearchAnswer(const uint8_t* packet
, uint32_t size
, bool optUTF8
, uint32_t serverIP
, uint16_t serverPort
);
145 * Processes a search-result sent via UDP. Only one result is read from the packet.
147 * @param packet The packet containing one or more search-results.
148 * @param optUTF8 Specifies if the server supports UTF8.
149 * @param serverIP The IP of the server sending the results.
150 * @param serverPort The Port of the server sending the results.
152 void ProcessUDPSearchAnswer(const CMemFile
& packet
, bool optUTF8
, uint32 serverIP
, uint16 serverPort
);
156 * Adds a result in the form of a kad search-keyword to the specified result-list.
158 * @param searchID The search to which this result belongs.
159 * @param fileID The hash of the result-file.
160 * @param name The filename of the result.
161 * @param size The filesize of the result.
162 * @param type The filetype of the result (TODO: Not used?)
163 * @param kadPublishInfo The kademlia publish information of the result.
164 * @param taglist List of additional tags associated with the search-result.
166 void KademliaSearchKeyword(uint32_t searchID
, const Kademlia::CUInt128
*fileID
, const wxString
& name
, uint64_t size
, const wxString
& type
, uint32_t kadPublishInfo
, const TagPtrList
& taglist
);
168 /** Update a certain search result in all lists */
169 void UpdateSearchFileByHash(const CMD4Hash
& hash
);
171 /** Mark current KAD search as finished */
172 void SetKadSearchFinished() { m_KadSearchFinished
= true; }
175 /** Event-handler for global searches. */
176 void OnGlobalSearchTimer(CTimerEvent
& evt
);
179 * Adds the specified file to the current search's results.
181 * @param toadd The result to add.
182 * @param clientResponse Is the result sent by a client (shared-files list).
183 * @return True if the results were added, false otherwise.
185 * Note that this function takes ownership of the CSearchFile object,
186 * regardless of whenever or not it was actually added to the results list.
188 bool AddToList(CSearchFile
* toadd
, bool clientResponse
= false);
190 //! This smart pointer is used to safely prevent leaks.
191 typedef CSmartPtr
<CMemFile
> CMemFilePtr
;
193 /** Create a basic search-packet for the given search-type. */
194 CMemFilePtr
CreateSearchData(CSearchParams
& params
, SearchType type
, bool supports64bit
, bool& packetUsing64bit
);
197 //! Timer used for global search intervals.
198 CTimer m_searchTimer
;
200 //! The current search-type, regarding the last/current search.
201 SearchType m_searchType
;
203 //! Specifies if a search is being performed.
204 bool m_searchInProgress
;
206 //! The ID of the current search.
207 long m_currentSearch
;
209 //! The current packet used for searches.
210 CPacket
* m_searchPacket
;
212 //! Does the current search packet contain 64bit values?
213 bool m_64bitSearchPacket
;
215 //! If the current search is a KAD search this signals if it is finished.
216 bool m_KadSearchFinished
;
218 //! Queue of servers to ask when doing global searches.
219 //! TODO: Replace with 'cookie' system.
220 CQueueObserver
<CServer
*> m_serverQueue
;
222 //! Shorthand for the map of results (key is a SearchID).
223 typedef std::map
<long, CSearchResultList
> ResultMap
;
225 //! Map of all search-results added.
228 //! Contains the results type desired in the current search.
229 //! If not empty, results of different types are filtered.
230 wxString m_resultType
;
233 DECLARE_EVENT_TABLE()
237 #endif // SEARCHLIST_H
238 // File_checked_for_headers