File indexing completed on 2024-05-19 04:35:18
0001 /* 0002 SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "document.h" 0008 0009 #include <QFile> 0010 0011 #include <KLocalizedString> 0012 #include <kzip.h> 0013 0014 using namespace FictionBook; 0015 0016 Document::Document(const QString &fileName) 0017 : mFileName(fileName) 0018 { 0019 } 0020 0021 bool Document::open() 0022 { 0023 QIODevice *device; 0024 0025 QFile file(mFileName); 0026 KZip zip(mFileName); 0027 if (mFileName.endsWith(QLatin1String(".fb")) || mFileName.endsWith(QLatin1String(".fb2"))) { 0028 if (!file.open(QIODevice::ReadOnly)) { 0029 setError(i18n("Unable to open document: %1", file.errorString())); 0030 return false; 0031 } 0032 0033 device = &file; 0034 } else { 0035 if (!zip.open(QIODevice::ReadOnly)) { 0036 setError(i18n("Document is not a valid ZIP archive")); 0037 return false; 0038 } 0039 0040 const KArchiveDirectory *directory = zip.directory(); 0041 if (!directory) { 0042 setError(i18n("Invalid document structure (main directory is missing)")); 0043 return false; 0044 } 0045 0046 const QStringList entries = directory->entries(); 0047 0048 QString documentFile; 0049 for (int i = 0; i < entries.count(); ++i) { 0050 if (entries[i].endsWith(QLatin1String(".fb2"))) { 0051 documentFile = entries[i]; 0052 break; 0053 } 0054 } 0055 0056 if (documentFile.isEmpty()) { 0057 setError(i18n("No content found in the document")); 0058 return false; 0059 } 0060 0061 const KArchiveFile *entry = static_cast<const KArchiveFile *>(directory->entry(documentFile)); 0062 // FIXME delete 'deviceƬ somewhen 0063 device = entry->createDevice(); 0064 } 0065 0066 QString errorMsg; 0067 if (!mDocument.setContent(device, true, &errorMsg)) { 0068 setError(i18n("Invalid XML document: %1", errorMsg)); 0069 return false; 0070 } 0071 0072 return true; 0073 } 0074 0075 QDomDocument Document::content() const 0076 { 0077 return mDocument; 0078 } 0079 0080 QString Document::lastErrorString() const 0081 { 0082 return mErrorString; 0083 } 0084 0085 void Document::setError(const QString &error) 0086 { 0087 mErrorString = error; 0088 }