Upstream tarball 9964
[amule.git] / src / SearchFile.h
blobcde2c1bba1009ffc0dd4148216017cc3224cdab3
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 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.
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.
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
26 #ifndef SEARCHFILE_H
27 #define SEARCHFILE_H
29 #include "KnownFile.h" // Needed for CAbstractFile
32 class CMemFile;
33 class CMD4Hash;
34 class CSearchFile;
37 typedef std::vector<CSearchFile*> CSearchResultList;
40 /**
41 * Represents a search-result returned from a server or client.
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.
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.
55 * TODO: Server IP/Port are currently not used.
56 * TODO: Client ID/Port are currently not used.
57 * TODO: Directories are currently not used.
59 class CSearchFile : public CAbstractFile
61 public:
62 /** Constructor used to create results on the remote GUI. */
63 CSearchFile(class CEC_SearchFile_Tag* tag);
64 /** Copy constructor, also copies children. */
65 CSearchFile(const CSearchFile& other);
67 /**
68 * Normal constructor, reads a result from a packet.
70 * @param data Source of results-packet.
71 * @param optUTF8 Specifies if text-strings are to be read as UTF8.
72 * @param searchID searchID The
73 * @param serverIP The IP of the server that sent this result.
74 * @param serverPort The port of the server that sent this result.
75 * @param directory If from a clients shared files, the directory this file is in.
76 * @param kademlia Specifies if this was from a kad-search.
78 CSearchFile(
79 const CMemFile& data,
80 bool optUTF8,
81 wxUIntPtr searchID,
82 uint32_t serverIP = 0,
83 uint16_t serverPort = 0,
84 const wxString& directory = wxEmptyString,
85 bool kademlia = false);
88 /** Frees all children owned by this file. */
89 virtual ~CSearchFile();
92 /**
93 * Merges the two results into one.
95 * Merges the other result into this one, updating
96 * various informations.
98 * @param other The file to be merged into this.
100 void MergeResults(const CSearchFile& other);
102 /** Returns the total number of sources. */
103 uint32 GetSourceCount() const { return m_sourceCount; }
104 /** Returns the number of sources that have the entire file. */
105 uint32 GetCompleteSourceCount() const { return m_completeSourceCount; }
106 /** Returns the ID of the search, used to select the right list when displaying. */
107 wxUIntPtr GetSearchID() const { return m_searchID; }
108 /** Returns true if the result is from a Kademlia search. */
109 bool IsKademlia() const { return m_kademlia; }
111 // Possible download status of a file
112 enum DownloadStatus {
113 NEW, // not known
114 DOWNLOADED, // successfully downloaded or shared
115 QUEUED, // downloading (Partfile)
116 CANCELED, // canceled
117 QUEUEDCANCELED // canceled once, but now downloading again
120 /** Returns the download status. */
121 enum DownloadStatus GetDownloadStatus() const { return m_downloadStatus; }
122 /** Set download status according to the global lists of knownfile, partfiles, canceledfiles. */
123 void SetDownloadStatus();
124 /** Set download status directly. */
125 void SetDownloadStatus(enum DownloadStatus s) { m_downloadStatus = s; }
127 /** Returns the parent of this file. */
128 CSearchFile *GetParent() const { return m_parent; }
129 /** Returns the list of children belonging to this file. */
130 const CSearchResultList &GetChildren() const { return m_children; }
131 /** Returns true if this item has children. */
132 bool HasChildren() const { return !m_children.empty(); }
133 /** Returns true if children should be displayed. */
134 bool ShowChildren() const { return m_showChildren; }
135 /** Enable/Disable displaying of children (set in CSearchListCtrl). */
136 void SetShowChildren(bool show) { m_showChildren = show; }
139 * Adds the given file as a child of this file.
141 * Note that a file can either be a parent _or_
142 * a child, but not both. Also note that it is
143 * only legal to add children whose filesize and
144 * filehash matches the parent's. AddChild takes
145 * ownership of the file.
147 void AddChild(CSearchFile* file);
149 struct ClientStruct {
150 ClientStruct()
151 : m_ip(0), m_port(0), m_serverIP(0), m_serverPort(0)
154 ClientStruct(uint32_t ip, uint16_t port, uint32_t serverIP, uint16_t serverPort)
155 : m_ip(ip), m_port(port), m_serverIP(serverIP), m_serverPort(serverPort)
158 uint32_t m_ip;
159 uint16_t m_port;
160 uint32_t m_serverIP;
161 uint32_t m_serverPort;
164 void AddClient(const ClientStruct& client);
165 const std::list<ClientStruct>& GetClients() const { return m_clients; }
167 uint32_t GetClientID() const throw() { return m_clientID; }
168 void SetClientID(uint32_t clientID) throw() { m_clientID = clientID; }
169 uint16_t GetClientPort() const throw() { return m_clientPort; }
170 void SetClientPort(uint16_t port) throw() { m_clientPort = port; }
171 uint32_t GetClientServerIP() const throw() { return m_clientServerIP; }
172 void SetClientServerIP(uint32_t serverIP) throw() { m_clientServerIP = serverIP; }
173 uint16_t GetClientServerPort() const throw() { return m_clientServerPort; }
174 void SetClientServerPort(uint16_t port) throw() { m_clientServerPort = port; }
175 int GetClientsCount() const { return ((GetClientID() && GetClientPort()) ? 1 : 0) + m_clients.size(); }
177 void SetKadPublishInfo(uint32_t val) throw() { m_kadPublishInfo = val; }
178 uint32_t GetKadPublishInfo() const throw() { return m_kadPublishInfo; }
180 private:
181 //! CSearchFile is not assignable.
182 CSearchFile& operator=(const CSearchFile& other);
185 * Updates a parent file so that it shows various common traits.
187 * Currently, the most common filename is selected, and an average
188 * of fileratings is set, based on files that have a rating only.
190 void UpdateParent();
192 //! The parent of this result.
193 CSearchFile* m_parent;
194 //! Any children this result may have.
195 CSearchResultList m_children;
196 //! If true, children will be shown on the GUI.
197 bool m_showChildren;
198 //! The unique ID of this search owning this result.
199 wxUIntPtr m_searchID;
200 //! The total number of sources for this file.
201 uint32 m_sourceCount;
202 //! The number of sources that have the complete file.
203 uint32 m_completeSourceCount;
204 //! Specifies if the result is from a kademlia search.
205 bool m_kademlia;
206 //! The download status.
207 enum DownloadStatus m_downloadStatus;
209 //@{
210 //! TODO: Currently not used.
211 wxString m_directory;
212 //@}
214 std::list<ClientStruct> m_clients;
215 uint32_t m_clientID;
216 uint16_t m_clientPort;
217 uint32_t m_clientServerIP;
218 uint16_t m_clientServerPort;
220 //! Kademlia publish information.
221 uint32_t m_kadPublishInfo;
223 friend class CPartFile;
224 friend class CSearchListRem;
228 #endif // SEARCHLIST_H
229 // File_checked_for_headers