File indexing completed on 2024-04-28 15:35:51

0001 // SPDX-FileCopyrightText: 2018 Martin T. H. Sandsmark <martin.sandsmark@kde.org>
0002 // SPDX-License-Identifier: BSD-3-Clause
0003 
0004 #pragma once
0005 
0006 #include <KZip>
0007 #include <QDomNode>
0008 #include <QHash>
0009 #include <QMimeDatabase>
0010 #include <QObject>
0011 #include <QSet>
0012 #include <QVector>
0013 #include <memory>
0014 
0015 class KArchiveDirectory;
0016 class KArchiveFile;
0017 class QXmlStreamReader;
0018 
0019 struct EpubItem {
0020     QString path;
0021     QByteArray mimetype;
0022 };
0023 
0024 struct EpubPageReference {
0025     enum StandardType {
0026         CoverPage,
0027         TitlePage,
0028         TableOfContents,
0029         Index,
0030         Glossary,
0031         Acknowledgements,
0032         Bibliography,
0033         Colophon,
0034         CopyrightPage,
0035         Dedication,
0036         Epigraph,
0037         Foreword,
0038         ListOfIllustrations,
0039         ListOfTables,
0040         Notes,
0041         Preface,
0042         Text,
0043         Other
0044     };
0045 
0046     static StandardType typeFromString(const QString &name);
0047 
0048     QString target;
0049     QString title;
0050 };
0051 
0052 struct Collection {
0053     enum Type {
0054         Set,
0055         Series,
0056         Unknow,
0057     };
0058 
0059     QString name;
0060     Type type;
0061     size_t position;
0062 };
0063 
0064 class EPubContainer : public QObject
0065 {
0066     Q_OBJECT
0067 public:
0068     explicit EPubContainer(QObject *parent);
0069     ~EPubContainer();
0070 
0071     bool openFile(const QString &path);
0072 
0073     EpubItem getEpubItem(const QString &id) const
0074     {
0075         return m_items.value(id);
0076     }
0077 
0078     QSharedPointer<QIODevice> getIoDevice(const QString &path);
0079     QImage getImage(const QString &id);
0080     QList<Collection> collections() const;
0081     QStringList getMetadata(const QString &key);
0082     QStringList getItems()
0083     {
0084         return m_orderedItems;
0085     }
0086 
0087     QString getStandardPage(EpubPageReference::StandardType type)
0088     {
0089         return m_standardReferences.value(type).target;
0090     }
0091 
0092 Q_SIGNALS:
0093     void errorOccured(const QString &error);
0094 
0095 private:
0096     bool parseMimetype();
0097     bool parseContainer();
0098     bool parseContentFile(const QString &filepath);
0099     bool parseMetadataItem(const QDomNode &metadataNode, const QDomNodeList &nodeList);
0100     bool parseMetadataPropertyItem(const QDomElement &metadataElemenent, const QDomNodeList &nodeList);
0101     bool parseManifestItem(const QDomNode &manifestNodes, const QString &currentFolder);
0102     bool parseSpineItem(const QDomNode &spineNode);
0103     bool parseGuideItem(const QDomNode &guideItem);
0104 
0105     const KArchiveFile *getFile(const QString &path);
0106 
0107     std::unique_ptr<KZip> m_archive;
0108     const KArchiveDirectory *m_rootFolder;
0109 
0110     QHash<QString, QStringList> m_metadata;
0111     QList<Collection> m_collections;
0112 
0113     QHash<QString, EpubItem> m_items;
0114     QStringList m_orderedItems;
0115     QSet<QString> m_unorderedItems;
0116 
0117     QHash<EpubPageReference::StandardType, EpubPageReference> m_standardReferences;
0118     QHash<QString, EpubPageReference> m_otherReferences;
0119     QMimeDatabase m_mimeDatabase;
0120 };