Upstream tarball 10013
[amule.git] / src / CanceledFileList.cpp
blobfb94df39ee340f8005db51d300654b226bc80a84
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2010 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.
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
26 #include "CanceledFileList.h" // Interface declarations
28 #include <common/DataFileVersion.h>
29 #include "amule.h"
30 #include "CFile.h"
31 #include "Logger.h"
32 #include <common/Format.h>
35 CCanceledFileList::CCanceledFileList()
37 m_filename = wxT("canceled.met");
38 Init();
42 bool CCanceledFileList::Init()
44 CFile file;
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;
53 if (!file.Open(fullpath)) {
54 AddLogLineM(true, CFormat(_("WARNING: %s cannot be opened.")) % m_filename);
55 return false;
58 try {
59 uint8 version = file.ReadUInt8();
60 if (version != CANCELEDFILE_VERSION) {
61 AddLogLineM(true, _("WARNING: Canceled file list corrupted, contains invalid header."));
62 return false;
65 uint32 RecordsNumber = file.ReadUInt32();
66 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, logKnownFiles, CFormat(wxT("Canceled file read: %s")) % hash.Encode());
73 if (!hash.IsEmpty()) {
74 m_canceledFileList.insert(hash);
77 AddDebugLogLineM(false, logKnownFiles, wxT("Finished reading canceled files"));
79 return true;
80 } catch (const CSafeIOException& e) {
81 AddLogLineM(true, CFormat(_("IO error while reading %s file: %s")) % m_filename % e.what());
84 return false;
88 void CCanceledFileList::Save()
90 CFile file(theApp->ConfigDir + m_filename, CFile::write);
91 if (!file.IsOpened()) {
92 return;
95 try {
96 file.WriteUInt8(CANCELEDFILE_VERSION);
97 file.WriteUInt32(m_canceledFileList.size());
99 CanceledFileList::iterator it = m_canceledFileList.begin();
100 for (; it != m_canceledFileList.end(); ++it) {
101 file.Write(it->GetHash(), 16);
103 } catch (const CIOFailureException& e) {
104 AddLogLineM(true, CFormat(_("Error while saving %s file: %s")) % m_filename % e.what());
109 bool CCanceledFileList::IsCanceledFile(const CMD4Hash& hash) const
111 return !hash.IsEmpty() && m_canceledFileList.find(hash) != m_canceledFileList.end();
115 bool CCanceledFileList::Add(const CMD4Hash& hash)
117 return m_canceledFileList.insert(hash).second;
121 bool CCanceledFileList::Remove(const CMD4Hash& hash)
123 return m_canceledFileList.erase(hash) > 0;
127 // File_checked_for_headers