File indexing completed on 2025-01-05 03:59:11
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2016 Dennis Nienhüser <nienhueser@kde.org> 0004 // 0005 0006 #include "GeoDataDocumentWriter.h" 0007 0008 #include "GeoWriterBackend.h" 0009 #include "GeoWriter.h" 0010 #include "GeoTagWriter.h" 0011 #include "GeoDataDocument.h" 0012 #include "KmlElementDictionary.h" 0013 0014 #include <QFileInfo> 0015 #include <digikam_debug.h> 0016 0017 namespace Marble { 0018 0019 QSet<QPair<QString, GeoWriterBackend*> > GeoDataDocumentWriter::s_backends; 0020 0021 bool GeoDataDocumentWriter::write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier) 0022 { 0023 const GeoTagWriter* tagWriter = GeoTagWriter::recognizes(GeoTagWriter::QualifiedName(QString(), documentIdentifier)); 0024 if (tagWriter) { 0025 GeoWriter writer; 0026 writer.setDocumentType(documentIdentifier); 0027 return writer.write(device, &document); 0028 } else { 0029 for(const auto &backend: s_backends) { 0030 if (backend.first == documentIdentifier) { 0031 backend.second->write(device, document); 0032 return true; 0033 } 0034 } 0035 0036 qCDebug(DIGIKAM_MARBLE_LOG) << "No writer registered to handle " << documentIdentifier; 0037 return false; 0038 } 0039 } 0040 0041 bool GeoDataDocumentWriter::write(const QString &filename, const GeoDataDocument &document, const QString &documentIdentifier) 0042 { 0043 QFile file(filename); 0044 if (!file.open(QIODevice::WriteOnly)) { 0045 qCDebug(DIGIKAM_MARBLE_LOG) << "Cannot open" << filename << "for writing:" << file.errorString(); 0046 return false; 0047 } 0048 0049 QString const docType = documentIdentifier.isEmpty() ? determineDocumentIdentifier(filename) : documentIdentifier; 0050 return write(&file, document, docType); 0051 } 0052 0053 void GeoDataDocumentWriter::registerWriter(GeoWriterBackend *writer, const QString &fileExtension) 0054 { 0055 s_backends << QPair<QString, GeoWriterBackend*>(fileExtension, writer); 0056 } 0057 0058 void GeoDataDocumentWriter::unregisterWriter(GeoWriterBackend *writer, const QString &fileExtension) 0059 { 0060 auto pair = QPair<QString, GeoWriterBackend*>(fileExtension, writer); 0061 s_backends.remove(pair); 0062 delete writer; 0063 } 0064 0065 QString GeoDataDocumentWriter::determineDocumentIdentifier(const QString &filename) 0066 { 0067 QString const fileExtension = QFileInfo(filename).suffix().toLower(); 0068 if (fileExtension == QLatin1String("kml")) { 0069 return QString::fromUtf8(kml::kmlTag_nameSpaceOgc22); 0070 } 0071 if (fileExtension == QLatin1String("osm")) { 0072 return QLatin1String("0.6"); 0073 } 0074 0075 for(const auto &backend: s_backends) { 0076 if (backend.first == fileExtension) { 0077 return backend.first; 0078 } 0079 } 0080 0081 qCDebug(DIGIKAM_MARBLE_LOG) << "Unable to determine document from file extension " << fileExtension << ", falling back to KML document type"; 0082 return QString::fromUtf8(kml::kmlTag_nameSpaceOgc22); 0083 } 0084 0085 }