OSDN Git Service

okular: deal with FIXME in fictionbook generator
[kde/kde-extraapps.git] / okular / generators / fictionbook / document.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org>                   *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  ***************************************************************************/
9
10 #include "document.h"
11
12 #include <QtCore/QFile>
13
14 #include <klocale.h>
15 #include <karchive.h>
16
17 using namespace FictionBook;
18
19 Document::Document( const QString &fileName )
20     : mFileName( fileName )
21 {
22 }
23
24 bool Document::open()
25 {
26     if ( mFileName.endsWith( ".fb" ) || mFileName.endsWith( ".fb2" ) ) {
27         QFile file( mFileName );
28         if ( !file.open( QIODevice::ReadOnly ) ) {
29             setError( i18n( "Unable to open document: %1", file.errorString() ) );
30             return false;
31         }
32
33         QString errorMsg;
34         if ( !mDocument.setContent( &file, true, &errorMsg ) ) {
35             setError( i18n( "Invalid XML document: %1", errorMsg ) );
36             return false;
37         }
38         return true;
39     }
40
41     KArchive zip( mFileName );
42     if ( !zip.isReadable() ) {
43         setError( i18n( "Document is not a valid ZIP archive" ) );
44         return false;
45     }
46
47     const QList<KArchiveEntry> entries = zip.list();
48
49     QString documentFile;
50     for ( int i = 0; i < entries.count(); ++i ) {
51         if ( entries[ i ].pathname.endsWith( ".fb2" ) ) {
52             documentFile = QFile::decodeName(entries[ i ].pathname);
53             break;
54         }
55     }
56
57     if ( documentFile.isEmpty() ) {
58         setError( i18n( "No content found in the document" ) );
59         return false;
60     }
61
62     QString errorMsg;
63     if ( !mDocument.setContent( zip.data( documentFile ), true, &errorMsg ) ) {
64         setError( i18n( "Invalid XML document: %1", errorMsg ) );
65         return false;
66     }
67     return true;
68 }
69
70 QDomDocument Document::content() const
71 {
72     return mDocument;
73 }
74
75 QString Document::lastErrorString() const
76 {
77     return mErrorString;
78 }
79
80 void Document::setError( const QString &error )
81 {
82     mErrorString = error;
83 }