File indexing completed on 2024-04-28 03:50:30

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 "GeoDataParser.h"
0008 #include "GeoDataDocument.h"
0009 #include "MarbleDebug.h"
0010 
0011 #include <QFile>
0012 #include <QProcess>
0013 #include <QFileInfo>
0014 #include <QTemporaryFile>
0015 #include <QDir>
0016 #include <QMap>
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     if ( !QFileInfo( fileName ).exists() ) {
0030         error = QStringLiteral("File %1 does not exist").arg(fileName);
0031         mDebug() << error;
0032         return nullptr;
0033     }
0034 
0035     // Inspect the filename suffix
0036     QString const fileSuffix = QFileInfo( fileName ).suffix();
0037 
0038     // Determine if fileName suffix is supported by this plugin
0039     QMap<QString,QString> fileTypes;
0040     fileTypes["nmea"]     = "nmea";
0041     fileTypes["igc"]      = "igc";
0042     fileTypes["tiger"]    = "tiger";
0043     fileTypes["ov2"]      = "tomtom";
0044     fileTypes["garmin"]   = "garmin_txt";
0045     fileTypes["magellan"] = "magellan";
0046     fileTypes["csv"]      = "csv";
0047     QString const inputFileType = fileTypes[fileSuffix];
0048     if ( inputFileType.isEmpty() ) {
0049         error = QStringLiteral("Unsupported file extension for").arg(fileName);
0050         mDebug() << error;
0051         return nullptr;
0052     }
0053 
0054     // Set up temporary file to hold output KML from gpsbabel executable
0055     QTemporaryFile tempKmlFile(QDir::tempPath() + QLatin1String("/marble-gpsbabel-XXXXXX.kml"));
0056     tempKmlFile.open();
0057     QFile kmlFile( tempKmlFile.fileName() );
0058 
0059     // Set up gpsbabel command line
0060     const QString command = QLatin1String("gpsbabel");
0061     const QStringList args = QStringList()
0062         << QLatin1String("-i")
0063         << inputFileType
0064         << QLatin1String("-f")
0065         << fileName
0066         << QLatin1String("-o")
0067         << QLatin1String("kml")
0068         << QLatin1String("-F")
0069         << tempKmlFile.fileName()
0070     ;
0071 
0072     // Execute gpsbabel to parse the input file
0073     int const exitStatus = QProcess::execute( command, args );
0074     if ( exitStatus == 0 ) {
0075         kmlFile.open( QIODevice::ReadWrite );
0076         GeoDataParser parser( GeoData_KML );
0077         parser.read( &kmlFile );
0078         GeoDataDocument *document = dynamic_cast<GeoDataDocument*>( parser.releaseDocument() );
0079         if ( !document ) {
0080             error = parser.errorString();
0081             mDebug() << error;
0082             return nullptr;
0083         }
0084 
0085         document->setDocumentRole( role );
0086         return document;
0087     } else {
0088         error = QStringLiteral("Gpsbabel returned error code %1").arg(exitStatus);
0089         mDebug() << error;
0090         return nullptr;
0091     }
0092 }
0093 
0094 }
0095 
0096 #include "moc_GpsbabelRunner.cpp"