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 "GeoWriterBackend.h"
9#include "GeoWriter.h"
10#include "GeoTagWriter.h"
11#include "GeoDataDocument.h"
12#include "KmlElementDictionary.h"
13
14#include <QFileInfo>
15#include <MarbleDebug.h>
16
17namespace Marble {
18
19QSet<QPair<QString, GeoWriterBackend*> > GeoDataDocumentWriter::s_backends;
20
21bool GeoDataDocumentWriter::write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
22{
23 const GeoTagWriter* tagWriter = GeoTagWriter::recognizes(GeoTagWriter::QualifiedName(QString(), documentIdentifier));
24 if (tagWriter) {
25 GeoWriter writer;
26 writer.setDocumentType(documentIdentifier);
27 return writer.write(device, &document);
28 } else {
29 for(const auto &backend: s_backends) {
30 if (backend.first == documentIdentifier) {
31 backend.second->write(device, document);
32 return true;
33 }
34 }
35
36 mDebug() << "No writer registered to handle " << documentIdentifier;
37 return false;
38 }
39}
40
41bool GeoDataDocumentWriter::write(const QString &filename, const GeoDataDocument &document, const QString &documentIdentifier)
42{
43 QFile file(filename);
44 if (!file.open(QIODevice::WriteOnly)) {
45 mDebug() << "Cannot open" << filename << "for writing:" << file.errorString();
46 return false;
47 }
48
49 QString const docType = documentIdentifier.isEmpty() ? determineDocumentIdentifier(filename) : documentIdentifier;
50 return write(&file, document, docType);
51}
52
53void GeoDataDocumentWriter::registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
54{
55 s_backends << QPair<QString, GeoWriterBackend*>(fileExtension, writer);
56}
57
58void GeoDataDocumentWriter::unregisterWriter(GeoWriterBackend *writer, const QString &fileExtension)
59{
60 auto pair = QPair<QString, GeoWriterBackend*>(fileExtension, writer);
61 s_backends.remove(pair);
62 delete writer;
63}
64
65QString GeoDataDocumentWriter::determineDocumentIdentifier(const QString &filename)
66{
67 QString const fileExtension = QFileInfo(filename).suffix().toLower();
68 if (fileExtension == QLatin1String("kml")) {
69 return kml::kmlTag_nameSpaceOgc22;
70 }
71 if (fileExtension == QLatin1String("osm")) {
72 return "0.6";
73 }
74
75 for(const auto &backend: s_backends) {
76 if (backend.first == fileExtension) {
77 return backend.first;
78 }
79 }
80
81 qDebug() << "Unable to determine document from file extension " << fileExtension << ", falling back to KML document type";
82 return kml::kmlTag_nameSpaceOgc22;
83}
84
85}
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:73
bool write(QIODevice *device, const GeoNode *feature)
The main API call to use the XML writer.
Definition GeoWriter.cpp:24
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 first(qsizetype n) const const
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 Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.