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 SEARCHFILE_H
27 #define SEARCHFILE_H
28 
29 #include "KnownFile.h"	// Needed for CAbstractFile
30 
31 
32 class CMemFile;
33 class CMD4Hash;
34 class CSearchFile;
35 
36 
37 typedef std::vector<CSearchFile*> CSearchResultList;
38 
39 
40 /**
41  * Represents a search-result returned from a server or client.
42  *
43  * A file may have either a parent or any number of children.
44  * When a child is added to a result, the parent becomes a generic
45  * representation of all its children, which will include a copy
46  * of the original result. The parent object will contain the sum
47  * of sources (total/complete) and will have the most common
48  * filename. Children are owned by their parents, and can be
49  * displayed on CSearchListCtrl.
50  *
51  * Basic file parameters (hash, name, size, rating) can be read
52  * via the CAbstractFile functions. Tags pertaining to meta-data
53  * are stored in the taglist inherited from CAbstractFile.
54  *
55  * TODO: Server IP/Port are currently not used.
56  * TODO: Client ID/Port are currently not used.
57  */
58 class CSearchFile : public CAbstractFile, public CECID
59 {
60 public:
61 	/** Constructor used to create results on the remote GUI. */
62 	CSearchFile(const class CEC_SearchFile_Tag* tag);
63 	/** Copy constructor, also copies children. */
64 	CSearchFile(const CSearchFile& other);
65 
66 	/**
67 	 * Normal constructor, reads a result from a packet.
68 	 *
69 	 * @param data Source of results-packet.
70 	 * @param optUTF8 Specifies if text-strings are to be read as UTF8.
71 	 * @param searchID searchID The
72 	 * @param serverIP The IP of the server that sent this result.
73 	 * @param serverPort The port of the server that sent this result.
74 	 * @param directory If from a clients shared files, the directory this file is in.
75 	 * @param kademlia Specifies if this was from a kad-search.
76 	 */
77 	CSearchFile(
78 		const CMemFile& data,
79 		bool optUTF8,
80 		wxUIntPtr searchID,
81 		uint32_t serverIP = 0,
82 		uint16_t serverPort = 0,
83 		const wxString& directory = wxEmptyString,
84 		bool kademlia = false);
85 
86 
87 	/** Frees all children owned by this file. */
88 	virtual ~CSearchFile();
89 
90 
91 	/**
92 	 * Merges the two results into one.
93 	 *
94 	 * Merges the other result into this one, updating
95 	 * various informations.
96 	 *
97 	 * @param other The file to be merged into this.
98 	 */
99 	void MergeResults(const CSearchFile& other);
100 
101 	/** Returns the total number of sources. */
GetSourceCount()102 	uint32 GetSourceCount() const			{ return m_sourceCount; }
103 	/** Returns the number of sources that have the entire file. */
GetCompleteSourceCount()104 	uint32 GetCompleteSourceCount() const	{ return m_completeSourceCount; }
105 	/** Returns the ID of the search, used to select the right list when displaying. */
GetSearchID()106 	wxUIntPtr GetSearchID() const			{ return m_searchID; }
107 	/** Returns true if the result is from a Kademlia search. */
IsKademlia()108 	bool IsKademlia() const					{ return m_kademlia; }
109 
110 	// Possible download status of a file
111 	enum DownloadStatus {
112 		NEW,				// not known
113 		DOWNLOADED,			// successfully downloaded or shared
114 		QUEUED,				// downloading (Partfile)
115 		CANCELED,			// canceled
116 		QUEUEDCANCELED		// canceled once, but now downloading again
117 	};
118 
119 	/** Returns the download status. */
GetDownloadStatus()120 	enum DownloadStatus GetDownloadStatus() const	{ return m_downloadStatus; }
121 	/** Set download status according to the global lists of knownfile, partfiles, canceledfiles. */
122 	void SetDownloadStatus();
123 	/** Set download status directly. */
SetDownloadStatus(enum DownloadStatus s)124 	void SetDownloadStatus(enum DownloadStatus s)	{ m_downloadStatus = s; }
125 
126 	/** Returns the parent of this file. */
GetParent()127 	CSearchFile *GetParent() const			{ return m_parent; }
128 	/** Returns the list of children belonging to this file. */
GetChildren()129 	const CSearchResultList &GetChildren() const	{ return m_children; }
130 	/** Returns true if this item has children. */
HasChildren()131 	bool HasChildren() const				{ return !m_children.empty(); }
132 	/** Returns true if children should be displayed. */
ShowChildren()133 	bool ShowChildren() const				{ return m_showChildren; }
134 	/** Enable/Disable displaying of children (set in CSearchListCtrl). */
SetShowChildren(bool show)135 	void SetShowChildren(bool show)			{ m_showChildren = show; }
136 
137 	/**
138 	 * Adds the given file as a child of this file.
139 	 *
140 	 * Note that a file can either be a parent _or_
141 	 * a child, but not both. Also note that it is
142 	 * only legal to add children whose filesize and
143 	 * filehash matches the parent's. AddChild takes
144 	 * ownership of the file.
145 	 */
146 	void		AddChild(CSearchFile* file);
147 
148 	struct ClientStruct {
ClientStructClientStruct149 		ClientStruct()
150 			: m_ip(0), m_port(0), m_serverIP(0), m_serverPort(0)
151 		{}
152 
ClientStructClientStruct153 		ClientStruct(uint32_t ip, uint16_t port, uint32_t serverIP, uint16_t serverPort)
154 			: m_ip(ip), m_port(port), m_serverIP(serverIP), m_serverPort(serverPort)
155 		{}
156 
157 		uint32_t m_ip;
158 		uint16_t m_port;
159 		uint32_t m_serverIP;
160 		uint32_t m_serverPort;
161 	};
162 
163 	void	 AddClient(const ClientStruct& client);
GetClients()164 	const std::list<ClientStruct>& GetClients() const	{ return m_clients; }
165 
GetClientID()166 	uint32_t GetClientID() const throw()			{ return m_clientID; }
SetClientID(uint32_t clientID)167 	void	 SetClientID(uint32_t clientID) throw()		{ m_clientID = clientID; }
GetClientPort()168 	uint16_t GetClientPort() const throw()			{ return m_clientPort; }
SetClientPort(uint16_t port)169 	void	 SetClientPort(uint16_t port) throw()		{ m_clientPort = port; }
GetClientServerIP()170 	uint32_t GetClientServerIP() const throw()		{ return m_clientServerIP; }
SetClientServerIP(uint32_t serverIP)171 	void	 SetClientServerIP(uint32_t serverIP) throw()	{ m_clientServerIP = serverIP; }
GetClientServerPort()172 	uint16_t GetClientServerPort() const throw()		{ return m_clientServerPort; }
SetClientServerPort(uint16_t port)173 	void	 SetClientServerPort(uint16_t port) throw()	{ m_clientServerPort = port; }
GetClientsCount()174 	int	 GetClientsCount() const			{ return ((GetClientID() && GetClientPort()) ? 1 : 0) + m_clients.size(); }
175 
SetKadPublishInfo(uint32_t val)176 	void	 SetKadPublishInfo(uint32_t val) throw()	{ m_kadPublishInfo = val; }
GetKadPublishInfo()177 	uint32_t GetKadPublishInfo() const throw()		{ return m_kadPublishInfo; }
178 
GetDirectory()179 	const wxString& GetDirectory() const throw()	{ return m_directory; }
180 
181 private:
182 	//! CSearchFile is not assignable.
183 	CSearchFile& operator=(const CSearchFile& other);
184 
185 	/**
186 	 * Updates a parent file so that it shows various common traits.
187 	 *
188 	 * Currently, the most common filename is selected, and an average
189 	 * of fileratings is set, based on files that have a rating only.
190 	 */
191 	void	UpdateParent();
192 
193 	//! The parent of this result.
194 	CSearchFile*		m_parent;
195 	//! Any children this result may have.
196 	CSearchResultList	m_children;
197 	//! If true, children will be shown on the GUI.
198 	bool			m_showChildren;
199 	//! The unique ID of this search owning this result.
200 	wxUIntPtr		m_searchID;
201 	//! The total number of sources for this file.
202 	uint32			m_sourceCount;
203 	//! The number of sources that have the complete file.
204 	uint32			m_completeSourceCount;
205 	//! Specifies if the result is from a kademlia search.
206 	bool			m_kademlia;
207 	//! The download status.
208 	enum DownloadStatus m_downloadStatus;
209 
210 	//! Directory where file is stored (when it is part of a remote shared files list).
211 	wxString		m_directory;
212 
213 	std::list<ClientStruct>	m_clients;
214 	uint32_t		m_clientID;
215 	uint16_t		m_clientPort;
216 	uint32_t		m_clientServerIP;
217 	uint16_t		m_clientServerPort;
218 
219 	//! Kademlia publish information.
220 	uint32_t		m_kadPublishInfo;
221 
222 	friend class CPartFile;
223 	friend class CSearchListRem;
224 };
225 
226 
227 #endif // SEARCHLIST_H
228 // File_checked_for_headers
229