Marble

GeoDataDocumentWriter.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2016 Dennis Nienhüser <[email protected]>
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 
17 namespace Marble {
18 
19 QSet<QPair<QString, GeoWriterBackend*> > GeoDataDocumentWriter::s_backends;
20 
21 bool 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 
41 bool 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 
53 void GeoDataDocumentWriter::registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
54 {
55  s_backends << QPair<QString, GeoWriterBackend*>(fileExtension, writer);
56 }
57 
58 void 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 
65 QString 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 }
QString errorString() const const
bool remove(const T &value)
virtual bool open(QIODevice::OpenMode mode) override
QString suffix() const const
static bool write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
Write the content of the given GeoDataDocument to the given I/O device.
void setDocumentType(const QString &documentType)
Set the current document type.
Definition: GeoWriter.cpp:73
Base class intended to be subclassed by specific XML tag writers This class provides a base class tha...
Definition: GeoTagWriter.h:27
static void registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
Registers a plugin as a backend for writing a certain file extension.
bool isEmpty() const const
A container for Features, Styles and in the future Schemas.
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.
QString toLower() const const
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition: GeoWriter.h:25
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:10:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.