Commit 39811ab3 authored by Stefan Brüns's avatar Stefan Brüns Committed by Torsten Rahn

Skip creation of temporary files for zipped KML (KMZ)

Instead of extracting all files in the ZIP archive extract only the
KML file into a QByteArray and feed it into the parser.

As there are no longer any temporary files to clean up, KmlDocument is
no longer needed, GeoDataDocument is sufficient.

This also avoids an error in the thumbnailer, which fails when it
tries to geocast the KmlDocument to a GeoDataDocument.

BUG: 428437
CCBUG: 420089
CCBUG: 416925
parent 5e2c369a
......@@ -7,6 +7,7 @@
//
// Copyright 2011 Thibaut Gridel <tgridel@free.fr>
#include "GeoDataDocument.h"
#include "KmlParser.h"
#include "KmlElementDictionary.h"
......@@ -41,7 +42,7 @@ bool KmlParser::isValidElement(const QString& tagName) const
GeoDocument* KmlParser::createDocument() const
{
return new KmlDocument;
return new GeoDataDocument;
}
}
......@@ -11,7 +11,6 @@
#define KMLPARSER_H
#include "GeoParser.h"
#include "KmlDocument.h"
namespace Marble {
......
......@@ -11,10 +11,10 @@
#include "GeoDataDocument.h"
#include "KmlParser.h"
#include "KmlDocument.h"
#include "MarbleDebug.h"
#include "KmzHandler.h"
#include <MarbleZipReader.h>
#include <QBuffer>
#include <QFile>
#include <QFileInfo>
......@@ -32,47 +32,53 @@ KmlRunner::~KmlRunner()
GeoDataDocument *KmlRunner::parseFile(const QString &fileName, DocumentRole role, QString &error)
{
QString kmlFileName = fileName;
QString kmzPath;
QStringList kmzFiles;
QFileInfo const kmzFile( fileName );
if (kmzFile.exists() && kmzFile.suffix().toLower() == QLatin1String("kmz")) {
KmzHandler kmzHandler;
if ( kmzHandler.open( fileName, error ) ) {
kmlFileName = kmzHandler.kmlFile();
kmzPath = kmzHandler.kmzPath();
kmzFiles = kmzHandler.kmzFiles();
} else {
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
error = QStringLiteral("Cannot open file %1").arg(fileName);
mDebug() << error;
return nullptr;
}
QBuffer buffer;
QIODevice* device = nullptr;
if (fileName.toLower().endsWith(QLatin1String(".kmz"))) {
MarbleZipReader zipReader(&file);
QStringList kmlFiles;
for(const MarbleZipReader::FileInfo &zipFileInfo : zipReader.fileInfoList()) {
if (zipFileInfo.filePath.toLower().endsWith(QLatin1String(".kml"))) {
kmlFiles.append(zipFileInfo.filePath);
}
}
if (kmlFiles.empty()) {
error = QStringLiteral("File %1 does not contain any KML files").arg(fileName);
mDebug() << error;
return nullptr;
} else if (kmlFiles.size() > 1) {
mDebug() << QStringLiteral("File %1 contains multiple KML files").arg(fileName);
}
}
QFile file( kmlFileName );
if ( !file.exists() ) {
error = QStringLiteral("File %1 does not exist").arg(kmlFileName);
mDebug() << error;
return nullptr;
QByteArray const data = zipReader.fileData(kmlFiles[0]);
buffer.setData(data);
buffer.open(QBuffer::ReadOnly);
device = &buffer;
} else {
device = &file;
}
// Open file in right mode
file.open( QIODevice::ReadOnly );
KmlParser parser;
if ( !parser.read( &file ) ) {
if (!parser.read(device)) {
error = parser.errorString();
mDebug() << error;
return nullptr;
}
GeoDocument* document = parser.releaseDocument();
Q_ASSERT( document );
KmlDocument* doc = static_cast<KmlDocument*>( document );
GeoDataDocument* doc = static_cast<GeoDataDocument*>(document);
doc->setDocumentRole( role );
doc->setFileName( fileName );
doc->setBaseUri( kmlFileName );
doc->setFiles( kmzPath, kmzFiles );
doc->setFileName(fileName);
file.close();
return doc;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment