-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathOPFReader.cpp
172 lines (154 loc) · 4.79 KB
/
OPFReader.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
#include <QString>
#include <QStringList>
#include <QMap>
#include <QUrl>
#include <QXmlStreamReader>
#include <QTextStream>
#include <QLatin1String>
#include <QDebug>
#include "Utility.h"
#include "OPFReader.h"
#define DBG if(0)
const QStringList IMAGE_MIMETYPES = QStringList() << "image/gif" << "image/jpeg"
<< "image/png" << "image/webp"
<< "image/tiff" << "image/bmp";
const QStringList SVG_MIMETYPES = QStringList() << "image/svg+xml";
const QStringList AUDIO_MIMETYPES = QStringList() << "audio/mpeg" << "audio/mp3"
<< "audio/ogg" << "audio/mp4";
const QStringList VIDEO_MIMETYPES = QStringList() << "video/mp4" << "video/ogg"
<< "video/webm";
OPFReader::OPFReader()
:
m_opfDir(QDir()),
m_ManifestFilePaths(QStringList()),
m_IDMap(QMap<QString,QString>()),
m_FileMimeTypes(QMap<QString,QString>()),
m_SpineFilePaths(QStringList()),
m_opfpath(QString())
{
}
void OPFReader::parseOPF(const QString& opfpath)
{
QFileInfo fi(opfpath);
m_opfpath = fi.absoluteFilePath();
m_opfpath = Utility::resolveRelativeSegmentsInFilePath(m_opfpath, "/");
m_opfDir = QFileInfo(m_opfpath).dir();
QString opf_text = Utility::ReadUnicodeTextFile(opfpath);
QXmlStreamReader opf_reader(opf_text);
while (!opf_reader.atEnd()) {
opf_reader.readNext();
if (!opf_reader.isStartElement()) {
continue;
}
else if (opf_reader.name().compare(QLatin1String("item")) == 0) {
ReadManifestItemElement(&opf_reader);
}
else if (opf_reader.name().compare(QLatin1String("itemref")) == 0) {
ReadSpineItemRef(&opf_reader);
}
}
if (opf_reader.hasError()) {
const QString error = QString(QObject::tr("Unable to read OPF file.\nLine: %1 Column %2 - %3"))
.arg(opf_reader.lineNumber())
.arg(opf_reader.columnNumber())
.arg(opf_reader.errorString());
qDebug() << error;
}
}
void OPFReader::ReadManifestItemElement(QXmlStreamReader *opf_reader)
{
QString id = opf_reader->attributes().value("", "id").toString();
QString href = opf_reader->attributes().value("", "href").toString();
QString type = opf_reader->attributes().value("", "media-type").toString();
QUrl url = QUrl(href);
// skip any non-relative urls
if (!url.scheme().isEmpty()) return;
href = Utility::URLDecodePath(href);
QString file_path = m_opfDir.absolutePath() + "/" + href;
DBG qDebug() << "file path as built from opf info: " << file_path;
file_path = Utility::resolveRelativeSegmentsInFilePath(file_path, "/");
DBG qDebug() << "file path after resolving relative segments: " << file_path;
if (!m_ManifestFilePaths.contains(file_path)) {
m_IDMap[ id ] = file_path;
m_FileMimeTypes[ id ] = type;
m_ManifestFilePaths << file_path;
}
}
void OPFReader::ReadSpineItemRef(QXmlStreamReader *opf_reader)
{
QString idref = opf_reader->attributes().value("", "idref").toString();
if (m_IDMap.contains(idref)) {
if (m_FileMimeTypes[idref] == "application/xhtml+xml") {
m_SpineFilePaths << m_IDMap[idref];
}
} else {
qDebug() << "Warning idref is missing from Manifest" << idref;
}
}
QStringList OPFReader::GetSpineFilePathList()
{
return m_SpineFilePaths;
}
QStringList OPFReader::GetAudioFilePathList()
{
QStringList res;
foreach(QString id, m_IDMap.keys()) {
QString mtype = m_FileMimeTypes.value(id, "");
if (AUDIO_MIMETYPES.contains(mtype)) {
res << m_IDMap[id];
}
}
return res;
}
QStringList OPFReader::GetVideoFilePathList()
{
QStringList res;
foreach(QString id, m_IDMap.keys()) {
QString mtype = m_FileMimeTypes.value(id, "");
if (VIDEO_MIMETYPES.contains(mtype)) {
res << m_IDMap[id];
}
}
return res;
}
QStringList OPFReader::GetImageFilePathList()
{
QStringList res;
foreach(QString id, m_IDMap.keys()) {
QString mtype = m_FileMimeTypes.value(id, "");
if (IMAGE_MIMETYPES.contains(mtype)) {
res << m_IDMap[id];
}
}
return res;
}
QStringList OPFReader::GetSVGFilePathList()
{
QStringList res;
foreach(QString id, m_IDMap.keys()) {
QString mtype = m_FileMimeTypes.value(id, "");
if (SVG_MIMETYPES.contains(mtype)) {
res << m_IDMap[id];
}
}
return res;
}
QStringList OPFReader::GetCSSFilePathList()
{
QStringList res;
foreach(QString id, m_IDMap.keys()) {
QString mtype = m_FileMimeTypes.value(id, "");
if (mtype == "text/css") {
res << m_IDMap[id];
}
}
return res;
}
QStringList OPFReader::GetManifestFilePathList()
{
QStringList res;
foreach(QString id, m_IDMap.keys()) {
res << m_IDMap[id];
}
return res;
}