swift
modelconverterx.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QDir>
7 #include <QFile>
8 
9 #include "config/buildconfig.h"
10 #include "misc/logmessage.h"
12 
13 using namespace swift::config;
14 using namespace swift::misc;
15 using namespace swift::misc::simulation::settings;
16 
17 namespace swift::misc::simulation
18 {
19  QProcess *CModelConverterX::s_proccess = nullptr;
20 
21  bool CModelConverterX::supportsModelConverterX()
22  {
23  if (!CBuildConfig::isRunningOnWindowsNtPlatform()) { return false; }
24  return !getBinary().isEmpty();
25  }
26 
27  QProcess *CModelConverterX::startModelConverterX(const CAircraftModel &model, QObject *parent)
28  {
29  // checks
30  if (model.getFileName().isEmpty()) { return nullptr; }
31  const QString modelConverterX = getBinary();
32  if (modelConverterX.isEmpty()) { return nullptr; }
33 
34  // delete other MCX
35  if (s_proccess)
36  {
37  QProcess *old = s_proccess;
38  s_proccess = nullptr;
39  if (old->state() == QProcess::Running)
40  {
41  // if still running, terminate and then delete
42  QObject::connect(old, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), old,
43  &QObject::deleteLater);
44  old->terminate();
45  }
46  else { old->deleteLater(); }
47  }
48 
49  QProcess *process = new QProcess(parent);
50  const QString argument = QDir::toNativeSeparators(model.getFileName());
51  process->setProgram(modelConverterX);
52  process->setArguments({ argument });
53  process->start();
54  s_proccess = process;
55  return process;
56  }
57 
58  QString CModelConverterX::getBinary()
59  {
60  static const swift::misc::CSettingReadOnly<TModelConverterXBinary> setting(new QObject());
61  const QString mcx = setting.get();
62  if (mcx.isEmpty()) return {};
63  const QFile f(mcx);
64  return (f.exists()) ? mcx : QString();
65  }
66 } // namespace swift::misc::simulation
static constexpr bool isRunningOnWindowsNtPlatform()
Running on Windows NT platform?
Class template for accessing a specific value in the CSettingsCache.
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
const QString & getFileName() const
File name (corresponding data for simulator, only available if representing simulator model.
Free functions in swift::misc.