1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2010-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5//
6// Any parts of this program derived from the xMule, lMule or eMule project,
7// or contributed by third-party developers are copyrighted by their
8// respective authors.
9//
10// This program is free software; you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation; either version 2 of the License, or
13// (at your option) any later version.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
23//
24
25
26#include "CanceledFileList.h"	// Interface declarations
27
28#include <common/DataFileVersion.h>
29#include "amule.h"
30#include "CFile.h"
31#include "Logger.h"
32#include <common/Format.h>
33
34
35CCanceledFileList::CCanceledFileList()
36{
37	m_filename = wxT("canceled.met");
38	Init();
39}
40
41
42bool CCanceledFileList::Init()
43{
44	CFile file;
45
46	CPath fullpath = CPath(theApp->ConfigDir + m_filename);
47	if (!fullpath.FileExists()) {
48		// This is perfectly normal. The file was probably either
49		// deleted, or this is the first time running aMule.
50		return false;
51	}
52
53	if (!file.Open(fullpath)) {
54		AddLogLineC(CFormat(_("WARNING: %s cannot be opened.")) % m_filename);
55		return false;
56	}
57
58	try {
59		uint8 version = file.ReadUInt8();
60		if (version != CANCELEDFILE_VERSION) {
61			AddLogLineC(_("WARNING: Canceled file list corrupted, contains invalid header."));
62			return false;
63		}
64
65		uint32 RecordsNumber = file.ReadUInt32();
66		AddDebugLogLineN(logKnownFiles,
67			CFormat(wxT("Reading %i canceled files from file format 0x%02x."))
68			% RecordsNumber % version);
69		for (uint32 i = 0; i < RecordsNumber; i++) {
70			CMD4Hash hash;
71			file.Read(hash.GetHash(), 16);
72			AddDebugLogLineN(logKnownFiles, CFormat(wxT("Canceled file read: %s")) % hash.Encode());
73			if (!hash.IsEmpty()) {
74				m_canceledFileList.insert(hash);
75			}
76		}
77		AddDebugLogLineN(logKnownFiles, wxT("Finished reading canceled files"));
78
79		return true;
80	} catch (const CSafeIOException& e) {
81		AddLogLineC(CFormat(_("IO error while reading %s file: %s")) % m_filename % e.what());
82	}
83
84	return false;
85}
86
87
88void CCanceledFileList::Save()
89{
90	CFile file(theApp->ConfigDir + m_filename, CFile::write);
91	if (!file.IsOpened()) {
92		return;
93	}
94
95	try {
96		file.WriteUInt8(CANCELEDFILE_VERSION);
97		file.WriteUInt32(m_canceledFileList.size());
98
99		CanceledFileList::iterator it = m_canceledFileList.begin();
100		for (; it != m_canceledFileList.end(); ++it) {
101			file.Write(it->GetHash(), 16);
102		}
103	} catch (const CIOFailureException& e) {
104		AddLogLineC(CFormat(_("Error while saving %s file: %s")) % m_filename % e.what());
105	}
106}
107
108
109bool CCanceledFileList::IsCanceledFile(const CMD4Hash& hash) const
110{
111	return !hash.IsEmpty() && m_canceledFileList.find(hash) != m_canceledFileList.end();
112}
113
114
115bool CCanceledFileList::Add(const CMD4Hash& hash)
116{
117	return m_canceledFileList.insert(hash).second;
118}
119
120
121bool CCanceledFileList::Remove(const CMD4Hash& hash)
122{
123	return m_canceledFileList.erase(hash) > 0;
124}
125
126
127// File_checked_for_headers
128