|
21 | 21 | #include "common/constants.h"
|
22 | 22 | #include "common/aircrafttrack.h"
|
23 | 23 | #include "route/route.h"
|
| 24 | +#include "io/fileroller.h" |
24 | 25 | #include "options/optiondata.h"
|
25 | 26 | #include "gui/dialog.h"
|
26 | 27 | #include "ui_mainwindow.h"
|
|
37 | 38 | #include <QDir>
|
38 | 39 | #include <QMessageBox>
|
39 | 40 | #include <QStandardPaths>
|
| 41 | +#include <QXmlStreamReader> |
40 | 42 | #include <exception.h>
|
41 | 43 |
|
42 | 44 | RouteExport::RouteExport(MainWindow *parent)
|
@@ -290,7 +292,7 @@ bool RouteExport::routeExportCorteIn()
|
290 | 292 | {
|
291 | 293 | if(exportFlighplanAsCorteIn(routeFile))
|
292 | 294 | {
|
293 |
| - mainWindow->setStatusMessage(tr("Flight plan saved to corte.in.")); |
| 295 | + mainWindow->setStatusMessage(tr("Flight plan added to corte.in.")); |
294 | 296 | return true;
|
295 | 297 | }
|
296 | 298 | }
|
@@ -366,8 +368,27 @@ bool RouteExport::routeExportUFmc()
|
366 | 368 |
|
367 | 369 | bool RouteExport::routeExportProSim()
|
368 | 370 | {
|
369 |
| - qDebug() << Q_FUNC_INFO; |
370 | 371 | // companyroutes.xml
|
| 372 | + qDebug() << Q_FUNC_INFO; |
| 373 | + |
| 374 | + if(routeValidate(false /* validate parking */, true /* validate departure and destination */)) |
| 375 | + { |
| 376 | + QString routeFile = dialog->saveFileDialog( |
| 377 | + tr("Save Flight Plan to companyroutes.xml for ProSim"), |
| 378 | + tr("companyroutes.xml Files %1;;All Files (*)").arg(lnm::FILE_PATTERN_COMPANYROUTES_XML), |
| 379 | + ".xml", "Route/CompanyRoutesXml", |
| 380 | + QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first(), "companyroutes.xml", |
| 381 | + true /* dont confirm overwrite */); |
| 382 | + |
| 383 | + if(!routeFile.isEmpty()) |
| 384 | + { |
| 385 | + if(exportFlighplanAsCompanyroutesXml(routeFile)) |
| 386 | + { |
| 387 | + mainWindow->setStatusMessage(tr("Flight plan added to companyroutes.xml.")); |
| 388 | + return true; |
| 389 | + } |
| 390 | + } |
| 391 | + } |
371 | 392 | return false;
|
372 | 393 | }
|
373 | 394 |
|
@@ -740,6 +761,105 @@ bool RouteExport::exportFlighplanAsCorteIn(const QString& filename)
|
740 | 761 | }
|
741 | 762 | }
|
742 | 763 |
|
| 764 | +bool RouteExport::exportFlighplanAsCompanyroutesXml(const QString& filename) |
| 765 | +{ |
| 766 | + qDebug() << Q_FUNC_INFO << filename; |
| 767 | + |
| 768 | + // <?xml version="1.0" encoding="UTF-8"?> |
| 769 | + // <companyroutes> |
| 770 | + // <route name="KDSMKOKC">KDSM DSM J25 TUL KOKC </route> |
| 771 | + // <route name="EDDHEDDS">EDDH IDEKO Y900 TIMEN UL126 WRB UN850 KRH T128 BADSO EDDS</route> |
| 772 | + // <route name="EDDSEDDH">EDDS KRH UZ210 NOSPA EDDL</route> |
| 773 | + // </companyroutes> |
| 774 | + |
| 775 | + // Read the XML file and keep all routes =========================================== |
| 776 | + QVector<std::pair<QString, QString> > routes; |
| 777 | + QSet<QString> routeNames; |
| 778 | + |
| 779 | + QFile file(filename); |
| 780 | + if(file.exists() && file.size() > 0) |
| 781 | + { |
| 782 | + if(file.open(QFile::ReadOnly | QIODevice::Text)) |
| 783 | + { |
| 784 | + QXmlStreamReader reader(&file); |
| 785 | + |
| 786 | + while(!reader.atEnd()) |
| 787 | + { |
| 788 | + if(reader.error() != QXmlStreamReader::NoError) |
| 789 | + throw atools::Exception("Error reading \"" + filename + "\": " + reader.errorString()); |
| 790 | + |
| 791 | + QXmlStreamReader::TokenType token = reader.readNext(); |
| 792 | + |
| 793 | + if(token == QXmlStreamReader::StartElement && reader.name() == "route") |
| 794 | + { |
| 795 | + QString name = reader.attributes().value("name").toString(); |
| 796 | + QString route = reader.readElementText(); |
| 797 | + routes.append(std::make_pair(name, route)); |
| 798 | + routeNames.insert(name); |
| 799 | + } |
| 800 | + } |
| 801 | + file.close(); |
| 802 | + } |
| 803 | + else |
| 804 | + { |
| 805 | + atools::gui::ErrorHandler(mainWindow).handleIOError(file, tr("While reading from companyroutes.xml file:")); |
| 806 | + return false; |
| 807 | + } |
| 808 | + } |
| 809 | + |
| 810 | + // Create maximum of two backup files |
| 811 | + QString backupFile = filename + "_lnm_backup"; |
| 812 | + atools::io::FileRoller roller(1); |
| 813 | + roller.rollFile(backupFile); |
| 814 | + |
| 815 | + // Copy file to backup before opening |
| 816 | + bool result = QFile(filename).copy(backupFile); |
| 817 | + qDebug() << Q_FUNC_INFO << "Copied" << filename << "to" << backupFile << result; |
| 818 | + |
| 819 | + // Create route string |
| 820 | + QString route = RouteString::createStringForRoute(routeAdjustedToProcedureOptions(), 0.f, rs::START_AND_DEST); |
| 821 | + QString name = buildDefaultFilenameShort(QString(), QString()); |
| 822 | + |
| 823 | + // Find a unique name between all loaded |
| 824 | + QString newname = name; |
| 825 | + int i = 1; |
| 826 | + while(routeNames.contains(newname) && i < 99) |
| 827 | + newname = QString("%1%2").arg(name).arg(i++, 2, 10, QChar('0')); |
| 828 | + |
| 829 | + // Add new route |
| 830 | + routes.append(std::make_pair(newname, route)); |
| 831 | + |
| 832 | + // Save and overwrite new file ==================================================== |
| 833 | + if(file.open(QFile::WriteOnly | QIODevice::Text)) |
| 834 | + { |
| 835 | + QXmlStreamWriter writer(&file); |
| 836 | + writer.setAutoFormatting(true); |
| 837 | + writer.setAutoFormattingIndent(2); |
| 838 | + writer.writeStartDocument(); |
| 839 | + |
| 840 | + writer.writeStartElement("companyroutes"); |
| 841 | + |
| 842 | + for(const std::pair<QString, QString>& entry : routes) |
| 843 | + { |
| 844 | + // <route name="KDSMKOKC">KDSM DSM J25 TUL KOKC </route> |
| 845 | + writer.writeStartElement("route"); |
| 846 | + writer.writeAttribute("name", entry.first); |
| 847 | + writer.writeCharacters(entry.second); |
| 848 | + writer.writeEndElement(); // route |
| 849 | + } |
| 850 | + |
| 851 | + writer.writeEndElement(); // companyroutes |
| 852 | + writer.writeEndDocument(); |
| 853 | + file.close(); |
| 854 | + return true; |
| 855 | + } |
| 856 | + else |
| 857 | + { |
| 858 | + atools::gui::ErrorHandler(mainWindow).handleIOError(file, tr("While saving to companyroutes.xml file:")); |
| 859 | + return false; |
| 860 | + } |
| 861 | +} |
| 862 | + |
743 | 863 | bool RouteExport::exportFlightplanAsGpx(const QString& filename)
|
744 | 864 | {
|
745 | 865 | qDebug() << Q_FUNC_INFO << filename;
|
|
0 commit comments