File indexing completed on 2025-01-05 03:59:27
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2011 Thibaut Gridel <tgridel@free.fr> 0004 0005 #include "KmlRunner.h" 0006 0007 #include <QBuffer> 0008 #include <QFile> 0009 #include <QFileInfo> 0010 0011 #include "GeoDataDocument.h" 0012 #include "KmlParser.h" 0013 #include "MarbleZipReader.h" 0014 0015 #include "digikam_debug.h" 0016 0017 namespace Marble 0018 { 0019 0020 KmlRunner::KmlRunner(QObject *parent) : 0021 ParsingRunner(parent) 0022 { 0023 } 0024 0025 KmlRunner::~KmlRunner() 0026 { 0027 } 0028 0029 GeoDataDocument *KmlRunner::parseFile(const QString &fileName, DocumentRole role, QString &error) 0030 { 0031 QFile file(fileName); 0032 if (!file.open(QFile::ReadOnly)) { 0033 error = QStringLiteral("Cannot open file %1").arg(fileName); 0034 qCDebug(DIGIKAM_MARBLE_LOG) << error; 0035 return nullptr; 0036 } 0037 0038 QBuffer buffer; 0039 QIODevice* device = nullptr; 0040 0041 if (fileName.toLower().endsWith(QLatin1String(".kmz"))) { 0042 MarbleZipReader zipReader(&file); 0043 0044 QStringList kmlFiles; 0045 for(const MarbleZipReader::FileInfo &zipFileInfo : zipReader.fileInfoList()) { 0046 if (zipFileInfo.filePath.toLower().endsWith(QLatin1String(".kml"))) { 0047 kmlFiles.append(zipFileInfo.filePath); 0048 } 0049 } 0050 if (kmlFiles.empty()) { 0051 error = QStringLiteral("File %1 does not contain any KML files").arg(fileName); 0052 qCDebug(DIGIKAM_MARBLE_LOG) << error; 0053 return nullptr; 0054 } else if (kmlFiles.size() > 1) { 0055 qCDebug(DIGIKAM_MARBLE_LOG) << QStringLiteral("File %1 contains multiple KML files").arg(fileName); 0056 } 0057 0058 QByteArray const data = zipReader.fileData(kmlFiles[0]); 0059 buffer.setData(data); 0060 buffer.open(QBuffer::ReadOnly); 0061 device = &buffer; 0062 } else { 0063 device = &file; 0064 } 0065 0066 KmlParser parser; 0067 if (!parser.read(device)) { 0068 error = parser.errorString(); 0069 qCDebug(DIGIKAM_MARBLE_LOG) << error; 0070 return nullptr; 0071 } 0072 0073 GeoDocument* document = parser.releaseDocument(); 0074 Q_ASSERT( document ); 0075 GeoDataDocument* doc = static_cast<GeoDataDocument*>(document); 0076 doc->setDocumentRole( role ); 0077 doc->setFileName(fileName); 0078 0079 file.close(); 0080 return doc; 0081 } 0082 0083 } 0084 0085 #include "moc_KmlRunner.cpp"