forked from SomeSupport/eMule
-
Notifications
You must be signed in to change notification settings - Fork 97
/
CollectionFile.cpp
196 lines (167 loc) · 5.51 KB
/
CollectionFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//this file is part of eMule
//Copyright (C)2002-2005 Merkur ( [email protected] / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "StdAfx.h"
#include "collectionfile.h"
#include "OtherFunctions.h"
#include "Packets.h"
#include "Ed2kLink.h"
#include "resource.h"
#include "Log.h"
#include "Kademlia/Kademlia/Entry.h"
#include "Kademlia/Kademlia/Tag.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNAMIC(CCollectionFile, CAbstractFile)
CCollectionFile::CCollectionFile(void)
{
}
CCollectionFile::CCollectionFile(CFileDataIO* in_data)
{
UINT tagcount = in_data->ReadUInt32();
for (UINT i = 0; i < tagcount; i++)
{
CTag* toadd = new CTag(in_data, true);
if (toadd)
taglist.Add(toadd);
}
CTag* pTagHash = GetTag(FT_FILEHASH);
if(pTagHash)
SetFileHash(pTagHash->GetHash());
else
ASSERT(0);
pTagHash = GetTag(FT_AICH_HASH);
if (pTagHash != NULL && pTagHash->IsStr())
{
CAICHHash hash;
if (DecodeBase32(pTagHash->GetStr(), hash) == (UINT)CAICHHash::GetHashSize())
m_FileIdentifier.SetAICHHash(hash);
else
ASSERT( false );
}
// here we have two choices
// - if the server/client sent us a filetype, we could use it (though it could be wrong)
// - we always trust our filetype list and determine the filetype by the extension of the file
//
// if we received a filetype from server, we use it.
// if we did not receive a filetype, we determine it by examining the file's extension.
//
// but, in no case, we will use the receive file type when adding this search result to the download queue, to avoid
// that we are using 'wrong' file types in part files. (this has to be handled when creating the part files)
const CString& rstrFileType = GetStrTagValue(FT_FILETYPE);
SetFileName(GetStrTagValue(FT_FILENAME), false, rstrFileType.IsEmpty());
SetFileSize(GetInt64TagValue(FT_FILESIZE));
if (!rstrFileType.IsEmpty())
{
if (_tcscmp(rstrFileType, _T(ED2KFTSTR_PROGRAM))==0)
{
CString strDetailFileType = GetFileTypeByName(GetFileName());
if (!strDetailFileType.IsEmpty())
SetFileType(strDetailFileType);
else
SetFileType(rstrFileType);
}
else
SetFileType(rstrFileType);
}
if(GetFileSize() == (uint64)0 || !GetFileName().Compare(_T("")))
ASSERT(0);
}
CCollectionFile::CCollectionFile(CAbstractFile* pAbstractFile) : CAbstractFile(pAbstractFile)
{
ClearTags();
taglist.Add(new CTag(FT_FILEHASH, pAbstractFile->GetFileHash()));
taglist.Add(new CTag(FT_FILESIZE, pAbstractFile->GetFileSize(), true));
taglist.Add(new CTag(FT_FILENAME, pAbstractFile->GetFileName()));
if (m_FileIdentifier.HasAICHHash())
taglist.Add(new CTag(FT_AICH_HASH, m_FileIdentifier.GetAICHHash().GetString()));
if(!pAbstractFile->GetFileComment().IsEmpty())
taglist.Add(new CTag(FT_FILECOMMENT, pAbstractFile->GetFileComment()));
if(pAbstractFile->GetFileRating())
taglist.Add(new CTag(FT_FILERATING, pAbstractFile->GetFileRating()));
UpdateFileRatingCommentAvail();
}
bool CCollectionFile::InitFromLink(CString sLink)
{
CED2KLink* pLink = NULL;
CED2KFileLink* pFileLink = NULL;
try
{
pLink = CED2KLink::CreateLinkFromUrl(sLink);
if(!pLink)
throw GetResString(IDS_ERR_NOTAFILELINK);
pFileLink = pLink->GetFileLink();
if (!pFileLink)
throw GetResString(IDS_ERR_NOTAFILELINK);
}
catch (CString error)
{
CString strBuffer;
strBuffer.Format(GetResString(IDS_ERR_INVALIDLINK),error);
LogError(LOG_STATUSBAR, GetResString(IDS_ERR_LINKERROR), strBuffer);
return false;
}
taglist.Add(new CTag(FT_FILEHASH, pFileLink->GetHashKey()));
m_FileIdentifier.SetMD4Hash(pFileLink->GetHashKey());
taglist.Add(new CTag(FT_FILESIZE, pFileLink->GetSize(), true));
SetFileSize(pFileLink->GetSize());
taglist.Add(new CTag(FT_FILENAME, pFileLink->GetName()));
SetFileName(pFileLink->GetName(), false, false);
if (pFileLink->HasValidAICHHash())
{
taglist.Add(new CTag(FT_AICH_HASH, pFileLink->GetAICHHash().GetString()));
m_FileIdentifier.SetAICHHash(pFileLink->GetAICHHash());
}
delete pLink;
return true;
}
CCollectionFile::~CCollectionFile(void)
{
}
void CCollectionFile::WriteCollectionInfo(CFileDataIO *out_data)
{
out_data->WriteUInt32(taglist.GetSize());
for (int i = 0; i < taglist.GetSize(); i++)
{
CTag tempTag(*taglist.GetAt(i));
tempTag.WriteNewEd2kTag(out_data, utf8strRaw);
}
}
void CCollectionFile::UpdateFileRatingCommentAvail(bool /*bForceUpdate*/)
{
m_bHasComment = false;
UINT uRatings = 0;
UINT uUserRatings = 0;
for(POSITION pos = m_kadNotes.GetHeadPosition(); pos != NULL; )
{
Kademlia::CEntry* entry = m_kadNotes.GetNext(pos);
if (!m_bHasComment && !entry->GetStrTagValue(TAG_DESCRIPTION).IsEmpty())
m_bHasComment = true;
UINT rating = (UINT)entry->GetIntTagValue(TAG_FILERATING);
if (rating != 0)
{
uRatings++;
uUserRatings += rating;
}
}
if (uRatings)
m_uUserRating = uUserRatings / uRatings;
else
m_uUserRating = 0;
}