gui
[lbook_fbreader.git] / fbreader / src / formats / pdb / SimplePdbPlugin.cpp
blob9f8452a3a5f67d1007c38eb5e9f88627e0c2ae45
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <ZLFile.h>
21 #include <ZLInputStream.h>
23 #include "PdbPlugin.h"
24 #include "../../description/BookDescription.h"
25 #include "../txt/TxtBookReader.h"
26 #include "../html/HtmlBookReader.h"
27 #include "HtmlMetainfoReader.h"
28 #include "../util/TextFormatDetector.h"
30 bool SimplePdbPlugin::readDescription(const std::string &path, BookDescription &description) const {
31 ZLFile file(path);
33 shared_ptr<ZLInputStream> stream = createStream(file);
34 detectEncodingAndLanguage(description, *stream);
35 if (description.encoding().empty()) {
36 return false;
38 int readType = HtmlMetainfoReader::NONE;
39 if (description.title().empty()) {
40 readType |= HtmlMetainfoReader::TITLE;
42 if (description.author().isNull()) {
43 readType |= HtmlMetainfoReader::AUTHOR;
45 if ((readType != HtmlMetainfoReader::NONE) && TextFormatDetector().isHtml(*stream)) {
46 HtmlMetainfoReader metainfoReader(description, (HtmlMetainfoReader::ReadType)readType);
47 metainfoReader.readDocument(*stream);
50 return true;
53 bool SimplePdbPlugin::readModel(const BookDescription &description, BookModel &model) const {
54 ZLFile file(description.fileName());
55 shared_ptr<ZLInputStream> stream = createStream(file);
57 PlainTextFormat format(description.fileName());
58 if (!format.initialized()) {
59 PlainTextFormatDetector detector;
60 detector.detect(*stream, format);
63 readDocumentInternal(description.fileName(), model, format, description.encoding(), *stream);
64 return true;
67 void SimplePdbPlugin::readDocumentInternal(const std::string&, BookModel &model, const PlainTextFormat &format, const std::string &encoding, ZLInputStream &stream) const {
68 if (TextFormatDetector().isHtml(stream)) {
69 HtmlBookReader("", model, format, encoding).readDocument(stream);
70 } else {
71 TxtBookReader(model, format, encoding).readDocument(stream);