swift
contextnetwork.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include "config/buildconfig.h"
7 #include "core/application.h"
11 #include "misc/dbusserver.h"
12 
13 using namespace swift::config;
14 using namespace swift::core;
15 using namespace swift::misc;
16 
17 namespace swift::core::context
18 {
19  IContextNetwork *IContextNetwork::create(CCoreFacade *runtime, CCoreFacadeConfig::ContextMode mode,
20  CDBusServer *server, QDBusConnection &connection)
21  {
22  switch (mode)
23  {
24  case CCoreFacadeConfig::Local: return new CContextNetwork(mode, runtime);
25  case CCoreFacadeConfig::LocalInDBusServer:
26  {
27  auto *context = new CContextNetwork(mode, runtime);
28  context->registerWithDBus(ObjectPath(), server);
29  return context;
30  }
31  case CCoreFacadeConfig::Remote:
32  return new CContextNetworkProxy(CDBusServer::coreServiceName(connection), connection, mode, runtime);
33  case CCoreFacadeConfig::NotUsed:
34  default: return new CContextNetworkEmpty(runtime);
35  }
36  }
37 
38  const QList<QCommandLineOption> &IContextNetwork::getCmdLineOptions()
39  {
40  static const QList<QCommandLineOption> e;
41  static const QList<QCommandLineOption> opts {
42 #ifdef SWIFT_VATSIM_SUPPORT
43  QCommandLineOption({ "idAndKey", "clientIdAndKey" },
44  QCoreApplication::translate("CContextNetwork",
45  "Client id and key pair separated by ':', e.g. <id>:<key>."),
46  "clientIdAndKey")
47 #endif
48  };
49 
50  // only in not officially shipped versions
51  return (CBuildConfig::isLocalDeveloperDebugBuild()) ? opts : e;
52  }
53 
54 #ifdef SWIFT_VATSIM_SUPPORT
55  bool IContextNetwork::getCmdLineClientIdAndKey(int &id, QString &key)
56  {
57  // init values
58  id = 0;
59  key = "";
60 
61  // split parser values
62  if (IContextNetwork::getCmdLineOptions().isEmpty()) { return false; } // no such option, avoid warnings
63  if (!sApp) { return false; }
64  const QString clientIdAndKey = sApp->getParserValue("clientIdAndKey").toLower();
65  if (clientIdAndKey.isEmpty() || !clientIdAndKey.contains(':')) { return false; }
66  const QStringList stringList = clientIdAndKey.split(':');
67  const QString clientIdAsString = stringList[0];
68  bool ok = true;
69  id = clientIdAsString.toInt(&ok, 0); // base 0 means C convention
70  if (!ok || id == 0) { return false; }
71  key = stringList[1];
72  return true;
73  }
74 #endif
75 
76 } // namespace swift::core::context
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
static bool isLocalDeveloperDebugBuild()
Local build for developers.
QString getParserValue(const QString &option) const
Delegates to QCommandLineParser::value.
ContextMode
How to handle a given context.
The class providing facades (the contexts) for all DBus relevant operations.
Definition: corefacade.h:57
Empty context, used during shutdown/initialization.
Network context implementation.
Custom DBusServer.
Definition: dbusserver.h:34
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
Free functions in swift::misc.