Skip to content

Commit 0a19b84

Browse files
committed
Fix epub files with toc using relative file path.
1 parent 7d3f116 commit 0a19b84

4 files changed

Lines changed: 45 additions & 19 deletions

File tree

lib/libebook/ebook_epub.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <QByteArray>
2727
#include <QChar>
28+
#include <QDir>
2829
#include <QIODevice>
2930
#include <QFileInfo>
3031
#include <QList>
@@ -235,17 +236,26 @@ bool EBook_EPUB::parseBookinfo()
235236
if ( content_parser.tocname.isEmpty() )
236237
return false;
237238

238-
// All the files, including TOC, are relative to the container_parser.contentPath
239-
m_documentRoot.clear();
239+
// TOC is relative to the container_parser.contentPath
240+
QString contentRoot;
240241
int sep = container_parser.contentPath.lastIndexOf( '/' );
241242

242243
if ( sep != -1 )
243-
m_documentRoot = container_parser.contentPath.left( sep + 1 ); // Keep the trailing slash
244+
contentRoot = container_parser.contentPath.left( sep + 1 ); // Keep the trailing slash
245+
246+
QString tocPath = combinePath( contentRoot, content_parser.tocname );
247+
248+
// All pages are relative to the container_parser.tocname
249+
QString tocRoot;
250+
sep = tocPath.lastIndexOf( '/' );
251+
252+
if ( sep != -1 )
253+
tocRoot = tocPath.left( sep + 1 ); // Keep the trailing slash
244254

245255
// Parse the TOC
246-
HelperXmlHandler_EpubTOC toc_parser( this );
256+
HelperXmlHandler_EpubTOC toc_parser( this, tocRoot );
247257

248-
if ( !parseXML( content_parser.tocname, &toc_parser ) )
258+
if ( !parseXML( tocPath, &toc_parser ) )
249259
return false;
250260

251261
// Get the data
@@ -256,12 +266,18 @@ bool EBook_EPUB::parseBookinfo()
256266

257267
// Move the manifest entries into the list
258268
Q_FOREACH ( QString f, content_parser.manifest.values() )
259-
m_ebookManifest.push_back( pathToUrl( f ) );
269+
{
270+
QString combined = combinePath( contentRoot, f );
271+
m_ebookManifest.push_back( pathToUrl( combined ) );
272+
}
260273

261274
for ( const auto& si : qAsConst( content_parser.spine ) )
262275
{
263276
if ( content_parser.manifest.contains( si ) )
264-
m_spinePath.push_back( content_parser.manifest[ si ] );
277+
{
278+
QString combined = combinePath( contentRoot, content_parser.manifest[ si ] );
279+
m_spinePath.push_back( combined );
280+
}
265281
}
266282

267283
// Copy the manifest information and fill up the other maps if we have it
@@ -372,12 +388,11 @@ bool EBook_EPUB::getFileAsBinary( QByteArray& data, const QString& path ) const
372388
{
373389
// Retrieve the file size
374390
struct zip_stat fileinfo;
375-
QString completeUrl;
391+
392+
QString completeUrl = path;
376393

377394
if ( !path.isEmpty() && path[0] == '/' )
378-
completeUrl = m_documentRoot + path.mid( 1 );
379-
else
380-
completeUrl = m_documentRoot + path;
395+
completeUrl = path.mid( 1 );
381396

382397
//qDebug("URL requested: %s (%s)", qPrintable(path), qPrintable(completeUrl));
383398

@@ -413,3 +428,9 @@ bool EBook_EPUB::getFileAsBinary( QByteArray& data, const QString& path ) const
413428
zip_fclose( file );
414429
return true;
415430
}
431+
432+
QString EBook_EPUB::combinePath( const QString& baseDir, const QString& path )
433+
{
434+
QString combined = QDir( baseDir ).filePath( path );
435+
return QDir::cleanPath( combined );
436+
}

lib/libebook/ebook_epub.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,14 @@ class EBook_EPUB : public EBook
190190
bool getFileAsString( QString& str, const QString& path ) const;
191191
bool getFileAsBinary( QByteArray& data, const QString& path ) const;
192192

193+
static QString combinePath( const QString& baseDir, const QString& path );
194+
193195
// ZIP archive fd and structs
194196
QFile m_epubFile;
195197
struct zip* m_zipFile;
196198

197199
// Ebook info
198200
QString m_title;
199-
QString m_documentRoot;
200201

201202
// List of files in the ebook
202203
QList<QUrl> m_ebookManifest;

lib/libebook/helperxmlhandler_epubtoc.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include <QDir>
1920
#include <QString>
2021
#include <QUrl>
2122
#include <QXmlAttributes>
@@ -24,12 +25,13 @@
2425
#include "helperxmlhandler_epubtoc.h"
2526

2627

27-
HelperXmlHandler_EpubTOC::HelperXmlHandler_EpubTOC( EBook_EPUB* epub )
28+
HelperXmlHandler_EpubTOC::HelperXmlHandler_EpubTOC( EBook_EPUB* epub, const QString& documentRoot )
29+
: m_inNavMap( false ),
30+
m_inText( false ),
31+
m_indent( 0 ),
32+
m_epub( epub ),
33+
m_documentRoot( documentRoot )
2834
{
29-
m_epub = epub;
30-
m_inNavMap = false;
31-
m_inText = false;
32-
m_indent = 0;
3335
}
3436

3537
bool HelperXmlHandler_EpubTOC::startElement( const QString&, const QString& localName, const QString&, const QXmlAttributes& atts )
@@ -103,7 +105,8 @@ void HelperXmlHandler_EpubTOC::checkNewTocEntry()
103105
{
104106
EBookTocEntry entry;
105107
entry.name = m_lastTitle;
106-
entry.url = m_epub->pathToUrl( m_lastId );
108+
QString combined = QDir( m_documentRoot ).filePath( m_lastId );
109+
entry.url = m_epub->pathToUrl( QDir::cleanPath( combined ) );
107110
entry.iconid = EBookTocEntry::IMAGE_AUTO;
108111
entry.indent = m_indent - 1;
109112

lib/libebook/helperxmlhandler_epubtoc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class EBook_EPUB;
3333
class HelperXmlHandler_EpubTOC : public QXmlDefaultHandler
3434
{
3535
public:
36-
HelperXmlHandler_EpubTOC( EBook_EPUB* epub );
36+
HelperXmlHandler_EpubTOC( EBook_EPUB* epub, const QString& documentRoot );
3737

3838
QList< EBookTocEntry > entries;
3939

@@ -50,6 +50,7 @@ class HelperXmlHandler_EpubTOC : public QXmlDefaultHandler
5050
QString m_lastId;
5151
QString m_lastTitle;
5252
EBook_EPUB* m_epub;
53+
QString m_documentRoot;
5354
};
5455

5556
#endif // HELPERXMLHANDLER_EPUBTOC_H

0 commit comments

Comments
 (0)