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
|
/*
* Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <ZLFile.h>
#include <ZLImage.h>
#include <ZLStringUtil.h>
#include <ZLUnicodeUtil.h>
#include <ZLDir.h>
#include <ZLInputStream.h>
#include <ZLLogger.h>
#include <ZLMimeType.h>
#include "OEBPlugin.h"
#include "OEBMetaInfoReader.h"
#include "OEBBookReader.h"
#include "OEBCoverReader.h"
#include "OEBTextStream.h"
#include "../../bookmodel/BookModel.h"
#include "../../library/Book.h"
static const std::string OPF = "opf";
static const std::string OEBZIP = "oebzip";
static const std::string EPUB = "epub";
class ContainerFileReader : public ZLXMLReader {
public:
const std::string &rootPath() const;
private:
void startElementHandler(const char *tag, const char **attributes);
private:
std::string myRootPath;
};
const std::string &ContainerFileReader::rootPath() const {
return myRootPath;
}
void ContainerFileReader::startElementHandler(const char *tag, const char **attributes) {
const std::string tagString = ZLUnicodeUtil::toLower(tag);
if (tagString == "rootfile") {
const char *path = attributeValue(attributes, "full-path");
if (path != 0) {
myRootPath = path;
interrupt();
}
}
}
OEBPlugin::~OEBPlugin() {
}
bool OEBPlugin::providesMetaInfo() const {
return true;
}
bool OEBPlugin::acceptsFile(const ZLFile &file) const {
shared_ptr<ZLMimeType> mimeType = file.mimeType();
const std::string &extension = file.extension();
if (!mimeType.isNull() && mimeType != ZLMimeType::EMPTY) {
return
mimeType == ZLMimeType::APPLICATION_EPUB_ZIP ||
(mimeType == ZLMimeType::APPLICATION_XML && extension == OPF) ||
(mimeType == ZLMimeType::APPLICATION_ZIP && extension == OEBZIP);
}
return extension == OPF || extension == OEBZIP || extension == EPUB;
}
ZLFile OEBPlugin::opfFile(const ZLFile &oebFile) {
//ZLLogger::Instance().registerClass("epub");
if (oebFile.extension() == OPF) {
return oebFile;
}
ZLLogger::Instance().println("epub", "Looking for opf file in " + oebFile.path());
shared_ptr<ZLDir> oebDir = oebFile.directory();
if (!oebDir.isNull()) {
const ZLFile containerInfoFile(oebDir->itemPath("META-INF/container.xml"));
if (containerInfoFile.exists()) {
ZLLogger::Instance().println("epub", "Found container file " + containerInfoFile.path());
ContainerFileReader reader;
reader.readDocument(containerInfoFile);
const std::string &opfPath = reader.rootPath();
ZLLogger::Instance().println("epub", "opf path = " + opfPath);
if (!opfPath.empty()) {
return ZLFile(oebDir->itemPath(opfPath));
}
}
}
oebFile.forceArchiveType(ZLFile::ZIP);
shared_ptr<ZLDir> zipDir = oebFile.directory(false);
if (zipDir.isNull()) {
ZLLogger::Instance().println("epub", "Couldn't open zip archive");
return ZLFile::NO_FILE;
}
std::vector<std::string> fileNames;
zipDir->collectFiles(fileNames, false);
for (std::vector<std::string>::const_iterator it = fileNames.begin(); it != fileNames.end(); ++it) {
ZLLogger::Instance().println("epub", "Item: " + *it);
if (ZLStringUtil::stringEndsWith(*it, ".opf")) {
return ZLFile(zipDir->itemPath(*it));
}
}
ZLLogger::Instance().println("epub", "Opf file not found");
return ZLFile::NO_FILE;
}
bool OEBPlugin::readMetaInfo(Book &book) const {
const ZLFile &file = book.file();
return OEBMetaInfoReader(book).readMetaInfo(opfFile(file));
}
bool OEBPlugin::readModel(BookModel &model) const {
const ZLFile &file = model.book()->file();
return OEBBookReader(model).readBook(opfFile(file));
}
shared_ptr<const ZLImage> OEBPlugin::coverImage(const ZLFile &file) const {
return OEBCoverReader().readCover(opfFile(file));
}
bool OEBPlugin::readLanguageAndEncoding(Book &book) const {
if (book.language().empty()) {
shared_ptr<ZLInputStream> oebStream = new OEBTextStream(opfFile(book.file()));
detectLanguage(book, *oebStream, book.encoding());
}
return true;
}
|