swift
xswiftbusconfigwriter.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
7 
8 #include <QFile>
9 #include <QTextStream>
10 
11 #include "misc/dbusserver.h"
13 #include "misc/stringutils.h"
14 
15 namespace swift::misc::simulation::xplane
16 {
17  CXSwiftBusConfigWriter::CXSwiftBusConfigWriter(QObject *parent) : QObject(parent) {}
18 
19  void CXSwiftBusConfigWriter::setDBusAddress(const QString &dBusAddress)
20  {
21  if (CDBusServer::isSessionOrSystemAddress(dBusAddress)) { m_dbusMode = "session"; }
22  else { m_dbusMode = "p2p"; }
23 
24  if (m_dbusMode == "p2p") { CDBusServer::dBusAddressToHostAndPort(dBusAddress, m_dbusAddress, m_dbusPort); }
25  }
26 
27  void CXSwiftBusConfigWriter::setDebugMode(bool on) { m_debug = on; }
28 
29  void CXSwiftBusConfigWriter::setTcasEnabled(bool on) { m_tcas = on; }
30 
32  {
36  }
37 
39  {
41  if (!path.isEmpty()) { writeTo(path); }
42  }
43 
45  {
47  if (!path.isEmpty()) { writeTo(path); }
48  }
49 
51  {
53  if (!path.isEmpty()) { writeTo(path); }
54  }
55 
56  void CXSwiftBusConfigWriter::writeTo(const QString &filePath)
57  {
58  // writing the file here (UI side) allows to overwrite the values
59  // before XPlane is started
60  // TCAS/log values MUST be changed before startup of the plugin
61  QString configFilePath = filePath + "/xswiftbus.conf";
62  QFile configFile(configFilePath);
63  configFile.remove();
64  if (configFile.open(QIODevice::WriteOnly))
65  {
66  // this code should be similar to xswiftbus config.cpp
67  QTextStream ts(&configFile);
68  ts << "# DBus Mode - Options: p2p, session" << Qt::endl;
69  ts << "dbusMode = " << m_dbusMode << Qt::endl;
70  ts << Qt::endl;
71  ts << "# DBus server address - relevant for P2P mode only" << Qt::endl;
72  ts << "dbusAddress = " << m_dbusAddress << Qt::endl;
73  ts << Qt::endl;
74  ts << "# DBus server port - relevant for P2P mode only" << Qt::endl;
75  ts << "dbusPort = " << m_dbusPort << Qt::endl;
76  ts << Qt::endl;
77  ts << "# Render phase debugging - to help diagnose crashes" << Qt::endl;
78  ts << "debug = " << boolToOnOff(m_debug) << Qt::endl;
79  ts << Qt::endl;
80  ts << "# TCAS traffic - to disable in case of crashes" << Qt::endl;
81  ts << "tcas = " << boolToOnOff(m_tcas) << Qt::endl;
82 
83  // add comment as information
84  ts << Qt::endl;
85  ts << "# Updated by CXSwiftBusConfigWriter " << QDateTime::currentDateTimeUtc().toString("yyyyMMddHHmmss")
86  << " ";
87  ts << Qt::endl;
88  }
89  }
90 } // namespace swift::misc::simulation::xplane
static bool dBusAddressToHostAndPort(const QString &dbusAddress, QString &o_host, int &o_port)
Extract host and port from a DBus address.
Definition: dbusserver.cpp:124
static bool isSessionOrSystemAddress(const QString &address)
True if address is session or system bus address.
Definition: dbusserver.cpp:178
static QString xswiftbusPluginDir(const QString &xplaneRootDir=CXPlaneUtil::xplaneRootDir())
xswiftbus plugin directory
Definition: xplaneutil.cpp:172
static QString xplane11Dir()
XPlane 11 directory.
Definition: xplaneutil.cpp:77
static QString xplane10Dir()
XPlane 10 directory.
Definition: xplaneutil.cpp:70
static QString xplane9Dir()
XPlane 9 directory.
Definition: xplaneutil.cpp:63
void updateInAllXPlaneVersions()
Update xswiftbus.conf in all known X-Plane versions (XP9 - XP11 are supported)
void writeTo(const QString &filePath)
Write new xswiftbus.conf to filePath. Existing files are removed.
void updateInXPlane10()
Update xswiftbus.conf in X-Plane 10.
void updateInXPlane9()
Update xswiftbus.conf in X-Plane 9.
void setDBusAddress(const QString &dBusAddress)
Set new DBus address.
CXSwiftBusConfigWriter(QObject *parent=nullptr)
Default constructor.
void updateInXPlane11()
Update xswiftbus.conf in X-Plane 11.
SWIFT_MISC_EXPORT const QString & boolToOnOff(bool v)
Bool to on/off.