File indexing completed on 2024-12-15 04:13:43

0001 /*
0002     Kchmviewer - a CHM and EPUB file viewer with broad language support
0003     SPDX-FileCopyrightText: 2004-2014 George Yunaev gyunaev@ulduzsoft.com
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "helperxmlhandler_epubtoc.h"
0009 #include <QtDebug>
0010 
0011 HelperXmlHandler_EpubTOC::HelperXmlHandler_EpubTOC(EBook_EPUB *epub)
0012 {
0013     m_epub = epub;
0014     m_inNavMap = false;
0015     m_inText = false;
0016     m_indent = 0;
0017 }
0018 
0019 bool HelperXmlHandler_EpubTOC::startElement(const QString &, const QString &localName, const QString &, const QXmlAttributes &atts)
0020 {
0021     //  qDebug() << "startElement " << " " << localName;
0022 
0023     //  for ( int i = 0; i < atts.count(); i++ )
0024     //      qDebug() << "    " << atts.localName(i) << " " << atts.value(i);
0025 
0026     if (localName == QLatin1String("navMap")) {
0027         m_inNavMap = true;
0028         return true;
0029     }
0030 
0031     if (!m_inNavMap) {
0032         return true;
0033     }
0034 
0035     if (localName == QLatin1String("navPoint")) {
0036         m_indent++;
0037     }
0038 
0039     if (localName == QLatin1String("text")) {
0040         m_inText = true;
0041     }
0042 
0043     if (localName == QLatin1String("content")) {
0044         int idx = atts.index(QLatin1String("src"));
0045 
0046         if (idx == -1) {
0047             return false;
0048         }
0049 
0050         m_lastId = atts.value(idx);
0051         checkNewTocEntry();
0052     }
0053 
0054     return true;
0055 }
0056 
0057 bool HelperXmlHandler_EpubTOC::characters(const QString &ch)
0058 {
0059     //  qDebug() << "characters" << " " << ch;
0060     if (m_inText) {
0061         m_lastTitle = ch;
0062     }
0063 
0064     checkNewTocEntry();
0065     return true;
0066 }
0067 
0068 bool HelperXmlHandler_EpubTOC::endElement(const QString &, const QString &localName, const QString &)
0069 {
0070     //  qDebug() << "endElement" << " " << qName;
0071 
0072     if (localName == QLatin1String("navMap")) {
0073         m_inNavMap = false;
0074         return true;
0075     }
0076 
0077     if (localName == QLatin1String("navPoint")) {
0078         m_indent--;
0079     }
0080 
0081     if (localName == QLatin1String("text")) {
0082         m_inText = false;
0083     }
0084 
0085     return true;
0086 }
0087 
0088 void HelperXmlHandler_EpubTOC::checkNewTocEntry()
0089 {
0090     if (!m_lastId.isEmpty() && !m_lastTitle.isEmpty()) {
0091         EBookTocEntry entry;
0092         entry.name = m_lastTitle;
0093         entry.url = m_epub->pathToUrl(m_lastId);
0094         entry.iconid = EBookTocEntry::IMAGE_AUTO;
0095         entry.indent = m_indent - 1;
0096 
0097         entries.push_back(entry);
0098 
0099         // qDebug() << "TOC entry: " << m_lastId << " :" << m_lastTitle << " :" << m_indent - 1;
0100 
0101         m_lastId.clear();
0102         m_lastTitle.clear();
0103     }
0104 }