File indexing completed on 2025-01-05 03:59:26

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Mohammed Nafees <nafees.technocool@gmail.com>
0004 
0005 #include "GpsbabelRunner.h"
0006 
0007 #include <QFile>
0008 #include <QProcess>
0009 #include <QFileInfo>
0010 #include <QTemporaryFile>
0011 #include <QDir>
0012 #include <QMap>
0013 
0014 #include "GeoDataParser.h"
0015 #include "GeoDataDocument.h"
0016 #include "digikam_debug.h"
0017 
0018 namespace Marble
0019 {
0020 
0021 GpsbabelRunner::GpsbabelRunner( QObject* parent )
0022     : ParsingRunner( parent )
0023 {
0024 }
0025 
0026 GeoDataDocument* GpsbabelRunner::parseFile(const QString &fileName, DocumentRole role, QString &error)
0027 {
0028     // Check and see if the file exists
0029 
0030     if ( !QFileInfo( fileName ).exists() )
0031     {
0032         error = QStringLiteral("File %1 does not exist").arg(fileName);
0033         qCDebug(DIGIKAM_MARBLE_LOG) << error;
0034         return nullptr;
0035     }
0036 
0037     // Inspect the filename suffix
0038 
0039     QString const fileSuffix = QFileInfo( fileName ).suffix();
0040 
0041     // Determine if fileName suffix is supported by this plugin
0042 
0043     QMap<QString,QString> fileTypes;
0044     fileTypes[QLatin1String("nmea")]     = QLatin1String("nmea");
0045     fileTypes[QLatin1String("igc")]      = QLatin1String("igc");
0046     fileTypes[QLatin1String("tiger")]    = QLatin1String("tiger");
0047     fileTypes[QLatin1String("ov2")]      = QLatin1String("tomtom");
0048     fileTypes[QLatin1String("garmin")]   = QLatin1String("garmin_txt");
0049     fileTypes[QLatin1String("magellan")] = QLatin1String("magellan");
0050     fileTypes[QLatin1String("csv")]      = QLatin1String("csv");
0051     QString const inputFileType = fileTypes[fileSuffix];
0052 
0053     if ( inputFileType.isEmpty() )
0054     {
0055         error = QStringLiteral("Unsupported file extension for").arg(fileName);
0056         qCDebug(DIGIKAM_MARBLE_LOG) << error;
0057         return nullptr;
0058     }
0059 
0060     // Set up temporary file to hold output KML from gpsbabel executable
0061 
0062     QTemporaryFile tempKmlFile(QDir::tempPath() + QLatin1String("/marble-gpsbabel-XXXXXX.kml"));
0063     tempKmlFile.open();
0064     QFile kmlFile( tempKmlFile.fileName() );
0065 
0066     // Set up gpsbabel command line
0067 
0068     const QString command = QLatin1String("gpsbabel");
0069     const QStringList args = QStringList()
0070         << QLatin1String("-i")
0071         << inputFileType
0072         << QLatin1String("-f")
0073         << fileName
0074         << QLatin1String("-o")
0075         << QLatin1String("kml")
0076         << QLatin1String("-F")
0077         << tempKmlFile.fileName()
0078     ;
0079 
0080     // Execute gpsbabel to parse the input file
0081 
0082     int const exitStatus = QProcess::execute( command, args );
0083 
0084     if ( exitStatus == 0 )
0085     {
0086         kmlFile.open( QIODevice::ReadWrite );
0087         GeoDataParser parser( GeoData_KML );
0088         parser.read( &kmlFile );
0089         GeoDataDocument *document = dynamic_cast<GeoDataDocument*>( parser.releaseDocument() );
0090 
0091         if ( !document )
0092         {
0093             error = parser.errorString();
0094             qCDebug(DIGIKAM_MARBLE_LOG) << error;
0095             return nullptr;
0096         }
0097 
0098         document->setDocumentRole( role );
0099         return document;
0100     }
0101     else
0102     {
0103         error = QStringLiteral("Gpsbabel returned error code %1").arg(exitStatus);
0104         qCDebug(DIGIKAM_MARBLE_LOG) << error;
0105         return nullptr;
0106     }
0107 }
0108 
0109 }
0110 
0111 #include "moc_GpsbabelRunner.cpp"