Marble

GeoDataDocumentWriter.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2016 Dennis Nienhüser <nienhueser@kde.org>
4//
5
6#include "GeoDataDocumentWriter.h"
7
8#include "GeoDataDocument.h"
9#include "GeoTagWriter.h"
10#include "GeoWriter.h"
11#include "GeoWriterBackend.h"
12#include "KmlElementDictionary.h"
13
14#include <MarbleDebug.h>
15#include <QFileInfo>
16
17namespace Marble
18{
19
20QSet<QPair<QString, GeoWriterBackend *>> GeoDataDocumentWriter::s_backends;
21
22bool GeoDataDocumentWriter::write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
23{
24 const GeoTagWriter *tagWriter = GeoTagWriter::recognizes(GeoTagWriter::QualifiedName(QString(), documentIdentifier));
25 if (tagWriter) {
26 GeoWriter writer;
27 writer.setDocumentType(documentIdentifier);
28 return writer.write(device, &document);
29 } else {
30 for (const auto &backend : s_backends) {
31 if (backend.first == documentIdentifier) {
32 backend.second->write(device, document);
33 return true;
34 }
35 }
36
37 mDebug() << "No writer registered to handle " << documentIdentifier;
38 return false;
39 }
40}
41
42bool GeoDataDocumentWriter::write(const QString &filename, const GeoDataDocument &document, const QString &documentIdentifier)
43{
44 QFile file(filename);
45 if (!file.open(QIODevice::WriteOnly)) {
46 mDebug() << "Cannot open" << filename << "for writing:" << file.errorString();
47 return false;
48 }
49
50 QString const docType = documentIdentifier.isEmpty() ? determineDocumentIdentifier(filename) : documentIdentifier;
51 return write(&file, document, docType);
52}
53
54void GeoDataDocumentWriter::registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
55{
56 s_backends << QPair<QString, GeoWriterBackend *>(fileExtension, writer);
57}
58
59void GeoDataDocumentWriter::unregisterWriter(GeoWriterBackend *writer, const QString &fileExtension)
60{
61 auto pair = QPair<QString, GeoWriterBackend *>(fileExtension, writer);
62 s_backends.remove(pair);
63 delete writer;
64}
65
66QString GeoDataDocumentWriter::determineDocumentIdentifier(const QString &filename)
67{
68 QString const fileExtension = QFileInfo(filename).suffix().toLower();
69 if (fileExtension == QLatin1StringView("kml")) {
70 return QString::fromLatin1(kml::kmlTag_nameSpaceOgc22);
71 }
72 if (fileExtension == QLatin1StringView("osm")) {
73 return QStringLiteral("0.6");
74 }
75
76 for (const auto &backend : s_backends) {
77 if (backend.first == fileExtension) {
78 return backend.first;
79 }
80 }
81
82 qDebug() << "Unable to determine document from file extension " << fileExtension << ", falling back to KML document type";
83 return QString::fromLatin1(kml::kmlTag_nameSpaceOgc22);
84}
85
86}
static bool write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
Write the content of the given GeoDataDocument to the given I/O device.
static void registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
Registers a plugin as a backend for writing a certain file extension.
A container for Features, Styles and in the future Schemas.
Base class intended to be subclassed by specific XML tag writers This class provides a base class tha...
QPair< QString, QString > QualifiedName
Object Name and Namespace Pair This type is intended to be used in a similar way to.
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition GeoWriter.h:26
void setDocumentType(const QString &documentType)
Set the current document type.
Definition GeoWriter.cpp:70
bool write(QIODevice *device, const GeoNode *feature)
The main API call to use the XML writer.
Definition GeoWriter.cpp:23
Binds a QML item to a specific geodetic location in screen coordinates.
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
QString suffix() const const
QString errorString() const const
bool remove(const T &value)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QString toLower() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.