Upstream tarball 20080405
[amule.git] / src / KnownFile.h
blob0d500b2719e15ad946f50d525014a3cc9ce939a7
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 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
27 #ifndef KNOWNFILE_H
28 #define KNOWNFILE_H
31 #include "MD4Hash.h"
32 #include "SHAHashSet.h"
34 #include <protocol/ed2k/Constants.h>
35 #include <common/Path.h>
37 #include "kademlia/kademlia/Indexed.h"
40 #ifdef CLIENT_GUI
41 #include <ec/cpp/ECSpecialTags.h>
42 #endif
45 #define PS_READY 0
46 #define PS_EMPTY 1
47 #define PS_WAITINGFORHASH 2
48 #define PS_HASHING 3
49 #define PS_ERROR 4
50 #define PS_INSUFFICIENT 5
51 #define PS_UNKNOWN 6
52 #define PS_PAUSED 7
53 #define PS_COMPLETING 8
54 #define PS_COMPLETE 9
57 #define PR_VERYLOW 4 // I Had to change this because
58 // it didn't save negative number
59 // correctly.. Had to modify the
60 // sort function for this change..
61 #define PR_LOW 0 //*
62 #define PR_NORMAL 1 // Don't change this - needed for
63 // edonkey clients and server!
64 #define PR_HIGH 2 //*
65 #define PR_VERYHIGH 3
66 #define PR_AUTO 5
67 #define PR_POWERSHARE 6 //added for powershare (deltaHF)
70 class CUpDownClient;
71 class CFileDataIO;
72 class CPacket;
73 class CTag;
76 namespace Kademlia
78 class CEntry;
82 typedef vector<CMD4Hash> ArrayOfCMD4Hash;
85 typedef vector<CTag> ArrayOfCTag;
88 class CFileStatistic
90 friend class CKnownFile;
91 friend class CSharedFilesRem;
93 public:
94 CFileStatistic();
95 void AddRequest();
96 void AddAccepted();
97 void AddTransferred(uint64 bytes);
98 uint16 GetRequests() const {return requested;}
99 uint16 GetAccepts() const {return accepted;}
100 uint64 GetTransferred() const {return transferred;}
101 uint32 GetAllTimeRequests() const {return alltimerequested;}
102 void SetAllTimeRequests(uint32 new_value) { alltimerequested = new_value; };
103 uint32 GetAllTimeAccepts() const {return alltimeaccepted;}
104 void SetAllTimeAccepts(uint32 new_value) { alltimeaccepted = new_value; };
105 uint64 GetAllTimeTransferred() const {return alltimetransferred;}
106 void SetAllTimeTransferred(uint64 new_value) { alltimetransferred = new_value; };
107 CKnownFile* fileParent;
109 private:
110 uint16 requested;
111 uint64 transferred;
112 uint16 accepted;
113 uint32 alltimerequested;
114 uint64 alltimetransferred;
115 uint32 alltimeaccepted;
119 CPartFile
121 CKnownFile
123 CAbstractFile
125 CSearchFile
127 class CAbstractFile
129 public:
130 CAbstractFile();
131 explicit CAbstractFile(const CAbstractFile& other);
132 virtual ~CAbstractFile() {}
134 const CPath& GetFileName() const { return m_fileName; }
135 const CMD4Hash& GetFileHash() const { return m_abyFileHash; }
137 uint64 GetFileSize() const { return m_nFileSize;}
138 bool IsLargeFile() const { return m_nFileSize > (uint64)OLD_MAX_FILE_SIZE; }
140 virtual void SetFileSize(uint64 nFileSize) { m_nFileSize = nFileSize; }
141 virtual void SetFileName(const CPath& filename);
143 /* Tags and Notes handling */
144 uint32 GetIntTagValue(uint8 tagname) const;
145 uint32 GetIntTagValue(const wxString& tagname) const;
146 bool GetIntTagValue(uint8 tagname, uint32& ruValue) const;
147 void SetIntTagValue(uint8 tagname, uint32 ruValue) const;
148 const wxString& GetStrTagValue(uint8 tagname) const;
149 const wxString& GetStrTagValue(const wxString& tagname) const;
150 const CTag *GetTag(const wxString& tagname) const;
151 const CTag *GetTag(const wxString& tagname, uint8 tagtype) const;
152 const CTag *GetTag(uint8 tagname) const;
153 const CTag *GetTag(uint8 tagname, uint8 tagtype) const;
154 void AddTagUnique(const CTag &pTag);
155 const ArrayOfCTag& GetTags() const { return m_taglist; }
156 void AddNote(Kademlia::CEntry* pEntry);
157 const CKadEntryPtrList& getNotes() const { return m_kadNotes; }
159 /* Comment and rating */
160 virtual const wxString& GetFileComment() const { return m_strComment; }
161 virtual int8 GetFileRating() const { return m_iRating; }
163 bool HasComment() const { return m_hasComment; }
164 bool HasRating() const { return (m_iUserRating != 0); }
165 int8 UserRating() const { return m_iUserRating; }
166 void UpdateFileRatingCommentAvail();
168 protected:
169 //! CAbstractFile is not assignable.
170 CAbstractFile& operator=(const CAbstractFile);
172 CMD4Hash m_abyFileHash;
173 wxString m_strComment;
174 int8 m_iRating;
175 bool m_hasComment;
176 int8 m_iUserRating;
177 ArrayOfCTag m_taglist;
178 CKadEntryPtrList m_kadNotes;
180 private:
181 uint64 m_nFileSize;
182 CPath m_fileName;
186 class CSearchFile;
189 class CKnownFile : public CAbstractFile
191 friend class CHashingTask;
192 public:
193 CKnownFile();
194 explicit CKnownFile(const CSearchFile &searchFile);
196 virtual ~CKnownFile();
198 void SetFilePath(const CPath& filePath);
199 const CPath& GetFilePath() const { return m_filePath; }
201 virtual bool IsPartFile() const {return false;}
202 virtual bool LoadFromFile(const CFileDataIO* file); //load date, hashset and tags from a .met file
203 virtual uint8 GetStatus(bool WXUNUSED(ignorepause) = false) const { return PS_COMPLETE; }
204 bool WriteToFile(CFileDataIO* file);
205 time_t GetLastChangeDatetime() const { return m_lastDateChanged; }
207 virtual void SetFileSize(uint64 nFileSize);
209 // local available part hashs
210 size_t GetHashCount() const {return m_hashlist.size();}
211 const CMD4Hash& GetPartHash(uint16 part) const;
213 // nr. of part hashs according the file size wrt ED2K protocol
214 uint32 GetED2KPartHashCount() const { return m_iED2KPartHashCount; }
216 // nr. of 9MB parts (file data)
217 inline uint16 GetPartCount() const { return m_iPartCount; }
219 // nr. of 9MB parts according the file size wrt ED2K protocol (OP_FILESTATUS)
220 inline uint16 GetED2KPartCount() const { return m_iED2KPartCount; }
222 // file upload priority
223 uint8 GetUpPriority() const {return m_iUpPriority;}
224 void SetUpPriority(uint8 newUpPriority, bool bSave=true);
225 bool IsAutoUpPriority() const {return m_bAutoUpPriority;}
226 void SetAutoUpPriority(bool flag) {m_bAutoUpPriority = flag;}
227 void UpdateAutoUpPriority();
228 size_t GetQueuedCount() const {return m_ClientUploadList.size();}
230 bool LoadHashsetFromFile(const CFileDataIO* file, bool checkhash);
231 void AddUploadingClient(CUpDownClient* client);
232 void RemoveUploadingClient(CUpDownClient* client);
234 // comment
235 const wxString& GetFileComment() { if (!m_bCommentLoaded) LoadComment(); return m_strComment; }
236 int8 GetFileRating() { if (!m_bCommentLoaded) LoadComment(); return m_iRating; }
238 void SetFileComment(const wxString& strNewComment);
239 void SetFileRating(int8 iNewRating);
240 void SetPublishedED2K( bool val );
241 bool GetPublishedED2K() const {return m_PublishedED2K;}
243 /* Kad stuff */
244 uint32 GetKadFileSearchID() const { return kadFileSearchID; }
245 // KAD TODO: This must be used on KadSearchListCtrl too once imported
246 void SetKadFileSearchID(uint32 id) { kadFileSearchID = id; } // John - Don't use this unless you know what your are DOING!! (Hopefully I do.. :)
247 const Kademlia::WordList& GetKadKeywords() const { return wordlist; }
248 // KAD TODO: If we add the proper column to SharedFilesCtrl, this is the funtion.
249 uint32 GetLastPublishTimeKadSrc() const { return m_lastPublishTimeKadSrc; }
250 void SetLastPublishTimeKadSrc(uint32 time, uint32 buddyip) { m_lastPublishTimeKadSrc = time; m_lastBuddyIP = buddyip;}
251 // Another unused function, useful for the shared files control column
252 uint32 GetLastPublishBuddy() const { return m_lastBuddyIP; }
253 void SetLastPublishTimeKadNotes(uint32 time) {m_lastPublishTimeKadNotes = time;}
254 uint32 GetLastPublishTimeKadNotes() const { return m_lastPublishTimeKadNotes; }
256 bool PublishSrc();
257 bool PublishNotes();
259 // TODO: This must be implemented if we ever want to have metadata.
260 uint32 GetMetaDataVer() const { return /*m_uMetaDataVer*/ 0; }
262 // file sharing
263 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
265 virtual void UpdatePartsInfo();
268 CFileStatistic statistic;
270 time_t m_nCompleteSourcesTime;
271 uint16 m_nCompleteSourcesCount;
272 uint16 m_nCompleteSourcesCountLo;
273 uint16 m_nCompleteSourcesCountHi;
275 // Maybe find a common place for this typedef?
276 typedef std::set<CUpDownClient*> SourceSet;
277 SourceSet m_ClientUploadList;
278 ArrayOfUInts16 m_AvailPartFrequency;
280 /**
281 * Returns a base-16 encoding of the master hash, or
282 * an empty string if no such hash exists.
284 wxString GetAICHMasterHash() const;
285 /** Returns true if the AICH-Hashset is valid, and verified or complete. */
286 bool HasProperAICHHashSet() const;
289 * Updates the requency of uploading parts from with the data the client provides.
291 * @param client The clients whoose uploading parts should be considered.
292 * @param increment If true, the counts are incremented, otherwise they are decremented.
294 * This functions updates the frequency list of file-upparts, using the clients
295 * upparts-status. This function should be called by clients every time they update their
296 * upparts-status, or when they are added or removed from the file.
298 void UpdateUpPartsFrequency( CUpDownClient* client, bool increment );
300 static void CreateHashFromHashlist(const ArrayOfCMD4Hash& hashes, CMD4Hash* Output);
302 void ClearPriority();
304 time_t m_lastDateChanged;
307 #ifdef CLIENT_GUI
308 CKnownFile(CEC_SharedFile_Tag *);
309 friend class CSharedFilesRem;
311 protected:
312 //! The AICH master-hash, if it is known.
313 wxString m_AICHMasterHash;
314 #else
315 virtual void SetFileName(const CPath& filename);
317 // AICH
318 CAICHHashSet* GetAICHHashset() const { return m_pAICHHashSet; }
319 void SetAICHHashset(CAICHHashSet* val) { m_pAICHHashSet = val; }
321 protected:
322 CAICHHashSet* m_pAICHHashSet;
323 #endif
325 bool LoadTagsFromFile(const CFileDataIO* file);
326 bool LoadDateFromFile(const CFileDataIO* file);
327 void LoadComment();//comment
328 ArrayOfCMD4Hash m_hashlist;
329 CPath m_filePath;
331 static void CreateHashFromFile(CFileDataIO* file, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut);
332 static void CreateHashFromInput(const byte* input, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut);
334 bool m_bCommentLoaded;
335 uint16 m_iPartCount;
336 uint16 m_iED2KPartCount;
337 uint16 m_iED2KPartHashCount;
338 uint8 m_iUpPriority;
339 bool m_bAutoUpPriority;
340 bool m_PublishedED2K;
342 /* Kad stuff */
343 Kademlia::WordList wordlist;
344 uint32 kadFileSearchID;
345 uint32 m_lastPublishTimeKadSrc;
346 uint32 m_lastPublishTimeKadNotes;
347 uint32 m_lastBuddyIP;
349 private:
350 /** Common initializations for constructors. */
351 void Init();
354 #endif // KNOWNFILE_H
355 // File_checked_for_headers