Upstream tarball 9445
[amule.git] / src / KnownFile.h
blob9d53225822387b0d158e99d83feba20111170654
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
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
55 #define PS_ALLOCATING 10
58 #define PR_VERYLOW 4 // I Had to change this because
59 // it didn't save negative number
60 // correctly.. Had to modify the
61 // sort function for this change..
62 #define PR_LOW 0 //*
63 #define PR_NORMAL 1 // Don't change this - needed for
64 // edonkey clients and server!
65 #define PR_HIGH 2 //*
66 #define PR_VERYHIGH 3
67 #define PR_AUTO 5
68 #define PR_POWERSHARE 6 //added for powershare (deltaHF)
71 class CUpDownClient;
72 class CFileDataIO;
73 class CPacket;
74 class CTag;
77 namespace Kademlia
79 class CEntry;
83 typedef vector<CMD4Hash> ArrayOfCMD4Hash;
86 typedef vector<CTag> ArrayOfCTag;
89 class CFileStatistic
91 friend class CKnownFile;
92 friend class CSharedFilesRem;
94 public:
95 CFileStatistic();
96 void AddRequest();
97 void AddAccepted();
98 void AddTransferred(uint64 bytes);
99 uint16 GetRequests() const {return requested;}
100 uint16 GetAccepts() const {return accepted;}
101 uint64 GetTransferred() const {return transferred;}
102 uint32 GetAllTimeRequests() const {return alltimerequested;}
103 void SetAllTimeRequests(uint32 new_value) { alltimerequested = new_value; };
104 uint32 GetAllTimeAccepts() const {return alltimeaccepted;}
105 void SetAllTimeAccepts(uint32 new_value) { alltimeaccepted = new_value; };
106 uint64 GetAllTimeTransferred() const {return alltimetransferred;}
107 void SetAllTimeTransferred(uint64 new_value) { alltimetransferred = new_value; };
108 CKnownFile* fileParent;
110 private:
111 uint16 requested;
112 uint64 transferred;
113 uint16 accepted;
114 uint32 alltimerequested;
115 uint64 alltimetransferred;
116 uint32 alltimeaccepted;
120 CPartFile
122 CKnownFile
124 CAbstractFile
126 CSearchFile
128 class CAbstractFile
130 public:
131 CAbstractFile();
132 explicit CAbstractFile(const CAbstractFile& other);
133 virtual ~CAbstractFile() {}
135 const CPath& GetFileName() const { return m_fileName; }
136 const CMD4Hash& GetFileHash() const { return m_abyFileHash; }
138 uint64 GetFileSize() const { return m_nFileSize;}
139 bool IsLargeFile() const { return m_nFileSize > (uint64)OLD_MAX_FILE_SIZE; }
141 virtual void SetFileSize(uint64 nFileSize) { m_nFileSize = nFileSize; }
142 virtual void SetFileName(const CPath& filename);
144 /* Tags and Notes handling */
145 uint32 GetIntTagValue(uint8 tagname) const;
146 uint32 GetIntTagValue(const wxString& tagname) const;
147 bool GetIntTagValue(uint8 tagname, uint32& ruValue) const;
148 void SetIntTagValue(uint8 tagname, uint32 ruValue) const;
149 const wxString& GetStrTagValue(uint8 tagname) const;
150 const wxString& GetStrTagValue(const wxString& tagname) const;
151 const CTag *GetTag(const wxString& tagname) const;
152 const CTag *GetTag(const wxString& tagname, uint8 tagtype) const;
153 const CTag *GetTag(uint8 tagname) const;
154 const CTag *GetTag(uint8 tagname, uint8 tagtype) const;
155 void AddTagUnique(const CTag &pTag);
156 const ArrayOfCTag& GetTags() const { return m_taglist; }
157 void AddNote(Kademlia::CEntry* pEntry);
158 const CKadEntryPtrList& getNotes() const { return m_kadNotes; }
160 /* Comment and rating */
161 virtual const wxString& GetFileComment() const { return m_strComment; }
162 virtual int8 GetFileRating() const { return m_iRating; }
164 bool HasComment() const { return m_hasComment; }
165 bool HasRating() const { return (m_iUserRating != 0); }
166 int8 UserRating() const { return m_iUserRating; }
167 void UpdateFileRatingCommentAvail();
169 protected:
170 //! CAbstractFile is not assignable.
171 CAbstractFile& operator=(const CAbstractFile);
173 CMD4Hash m_abyFileHash;
174 wxString m_strComment;
175 int8 m_iRating;
176 bool m_hasComment;
177 int8 m_iUserRating;
178 ArrayOfCTag m_taglist;
179 CKadEntryPtrList m_kadNotes;
181 private:
182 uint64 m_nFileSize;
183 CPath m_fileName;
187 class CSearchFile;
188 class CFile;
191 class CKnownFile : public CAbstractFile
193 friend class CHashingTask;
194 public:
195 CKnownFile();
196 explicit CKnownFile(const CSearchFile &searchFile);
198 virtual ~CKnownFile();
200 void SetFilePath(const CPath& filePath);
201 const CPath& GetFilePath() const { return m_filePath; }
203 virtual bool IsPartFile() const {return false;}
204 virtual bool LoadFromFile(const CFileDataIO* file); //load date, hashset and tags from a .met file
205 virtual uint8 GetStatus(bool WXUNUSED(ignorepause) = false) const { return PS_COMPLETE; }
206 bool WriteToFile(CFileDataIO* file);
207 time_t GetLastChangeDatetime() const { return m_lastDateChanged; }
209 virtual void SetFileSize(uint64 nFileSize);
211 // local available part hashs
212 size_t GetHashCount() const {return m_hashlist.size();}
213 const CMD4Hash& GetPartHash(uint16 part) const;
215 // nr. of part hashs according the file size wrt ED2K protocol
216 uint32 GetED2KPartHashCount() const { return m_iED2KPartHashCount; }
218 // nr. of 9MB parts (file data)
219 inline uint16 GetPartCount() const { return m_iPartCount; }
221 // nr. of 9MB parts according the file size wrt ED2K protocol (OP_FILESTATUS)
222 inline uint16 GetED2KPartCount() const { return m_iED2KPartCount; }
224 // size of a certain part, last is different, all others are PARTSIZE
225 uint32 GetPartSize(uint16 part) const { return part == m_iPartCount - 1 ? m_sizeLastPart : PARTSIZE; }
227 // file upload priority
228 uint8 GetUpPriority() const {return m_iUpPriority;}
229 void SetUpPriority(uint8 newUpPriority, bool bSave=true);
230 bool IsAutoUpPriority() const {return m_bAutoUpPriority;}
231 void SetAutoUpPriority(bool flag) {m_bAutoUpPriority = flag;}
232 void UpdateAutoUpPriority();
233 size_t GetQueuedCount() const {return m_ClientUploadList.size();}
235 bool LoadHashsetFromFile(const CFileDataIO* file, bool checkhash);
236 void AddUploadingClient(CUpDownClient* client);
237 void RemoveUploadingClient(CUpDownClient* client);
239 // comment
240 const wxString& GetFileComment() { if (!m_bCommentLoaded) LoadComment(); return m_strComment; }
241 int8 GetFileRating() { if (!m_bCommentLoaded) LoadComment(); return m_iRating; }
243 void SetFileComment(const wxString& strNewComment);
244 void SetFileRating(int8 iNewRating);
245 void SetPublishedED2K( bool val );
246 bool GetPublishedED2K() const {return m_PublishedED2K;}
248 /* Kad stuff */
249 uint32 GetKadFileSearchID() const { return kadFileSearchID; }
250 // KAD TODO: This must be used on KadSearchListCtrl too once imported
251 void SetKadFileSearchID(uint32 id) { kadFileSearchID = id; } // John - Don't use this unless you know what your are DOING!! (Hopefully I do.. :)
252 const Kademlia::WordList& GetKadKeywords() const { return wordlist; }
253 // KAD TODO: If we add the proper column to SharedFilesCtrl, this is the funtion.
254 uint32 GetLastPublishTimeKadSrc() const { return m_lastPublishTimeKadSrc; }
255 void SetLastPublishTimeKadSrc(uint32 time, uint32 buddyip) { m_lastPublishTimeKadSrc = time; m_lastBuddyIP = buddyip;}
256 // Another unused function, useful for the shared files control column
257 uint32 GetLastPublishBuddy() const { return m_lastBuddyIP; }
258 void SetLastPublishTimeKadNotes(uint32 time) {m_lastPublishTimeKadNotes = time;}
259 uint32 GetLastPublishTimeKadNotes() const { return m_lastPublishTimeKadNotes; }
261 bool PublishSrc();
262 bool PublishNotes();
264 // TODO: This must be implemented if we ever want to have metadata.
265 uint32 GetMetaDataVer() const { return /*m_uMetaDataVer*/ 0; }
267 // file sharing
268 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
270 virtual void UpdatePartsInfo();
273 CFileStatistic statistic;
275 time_t m_nCompleteSourcesTime;
276 uint16 m_nCompleteSourcesCount;
277 uint16 m_nCompleteSourcesCountLo;
278 uint16 m_nCompleteSourcesCountHi;
280 // Maybe find a common place for this typedef?
281 typedef std::set<CUpDownClient*> SourceSet;
282 SourceSet m_ClientUploadList;
283 ArrayOfUInts16 m_AvailPartFrequency;
285 /**
286 * Returns a base-16 encoding of the master hash, or
287 * an empty string if no such hash exists.
289 wxString GetAICHMasterHash() const;
290 /** Returns true if the AICH-Hashset is valid, and verified or complete. */
291 bool HasProperAICHHashSet() const;
294 * Updates the requency of uploading parts from with the data the client provides.
296 * @param client The clients whoose uploading parts should be considered.
297 * @param increment If true, the counts are incremented, otherwise they are decremented.
299 * This functions updates the frequency list of file-upparts, using the clients
300 * upparts-status. This function should be called by clients every time they update their
301 * upparts-status, or when they are added or removed from the file.
303 void UpdateUpPartsFrequency( CUpDownClient* client, bool increment );
305 static void CreateHashFromHashlist(const ArrayOfCMD4Hash& hashes, CMD4Hash* Output);
307 void ClearPriority();
309 time_t m_lastDateChanged;
311 virtual wxString GetFeedback() const;
314 #ifdef CLIENT_GUI
315 CKnownFile(CEC_SharedFile_Tag *);
316 friend class CSharedFilesRem;
318 protected:
319 //! The AICH master-hash, if it is known.
320 wxString m_AICHMasterHash;
321 #else
322 virtual void SetFileName(const CPath& filename);
324 // AICH
325 CAICHHashSet* GetAICHHashset() const { return m_pAICHHashSet; }
326 void SetAICHHashset(CAICHHashSet* val) { m_pAICHHashSet = val; }
328 protected:
329 CAICHHashSet* m_pAICHHashSet;
330 #endif
332 bool LoadTagsFromFile(const CFileDataIO* file);
333 bool LoadDateFromFile(const CFileDataIO* file);
334 void LoadComment();//comment
335 ArrayOfCMD4Hash m_hashlist;
336 CPath m_filePath;
338 static void CreateHashFromFile(CFile& file, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut);
339 static void CreateHashFromInput(const byte* input, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut);
341 bool m_bCommentLoaded;
342 uint16 m_iPartCount;
343 uint16 m_iED2KPartCount;
344 uint16 m_iED2KPartHashCount;
345 uint32 m_sizeLastPart; // size of the last part
346 uint8 m_iUpPriority;
347 bool m_bAutoUpPriority;
348 bool m_PublishedED2K;
350 /* Kad stuff */
351 Kademlia::WordList wordlist;
352 uint32 kadFileSearchID;
353 uint32 m_lastPublishTimeKadSrc;
354 uint32 m_lastPublishTimeKadNotes;
355 uint32 m_lastBuddyIP;
357 private:
358 /** Common initializations for constructors. */
359 void Init();
362 #endif // KNOWNFILE_H
363 // File_checked_for_headers