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+ }
0 commit comments