swift
networkdetailscomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QToolButton>
7 
8 #include "ui_networkdetailscomponent.h"
9 
11 #include "core/webdataservices.h"
12 #include "gui/guiapplication.h"
13 #include "gui/uppercasevalidator.h"
14 
15 using namespace swift::misc::network;
16 using namespace swift::misc::aviation;
17 using namespace swift::core;
18 using namespace swift::core::data;
19 
20 namespace swift::gui::components
21 {
22  CNetworkDetailsComponent::CNetworkDetailsComponent(QWidget *parent)
23  : QFrame(parent), ui(new Ui::CNetworkDetailsComponent)
24  {
25  ui->setupUi(this);
26 
27  connect(ui->comp_OtherServers, &CServerListSelector::serverChanged, this,
28  &CNetworkDetailsComponent::onSelectedServerChanged);
29  connect(ui->comp_VatsimServers, &CServerListSelector::serverChanged, this,
30  &CNetworkDetailsComponent::onSelectedServerChanged);
31  connect(ui->tw_Network, &QTabWidget::currentChanged, this, &CNetworkDetailsComponent::onServerTabWidgetChanged);
32  connect(ui->pb_OtherServersGotoSettings, &QPushButton::pressed, this,
34  connect(&m_networkSetup, &CNetworkSetup::setupChanged, this, &CNetworkDetailsComponent::reloadOtherServersSetup,
36 
37  // web service data
38  if (sGui && sGui->getWebDataServices())
39  {
40  connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this,
41  &CNetworkDetailsComponent::onWebServiceDataRead, Qt::QueuedConnection);
42  }
43 
44  constexpr int MaxLength = 10;
45  constexpr int MinLength = 0;
46  auto *ucv = new CUpperCaseValidator(MinLength, MaxLength, ui->le_PartnerCallsign);
47  ucv->setAllowedCharacters09AZ();
48  ui->le_PartnerCallsign->setMaxLength(MaxLength);
49  ui->le_PartnerCallsign->setValidator(ucv);
50 
51  const int tab = m_networkSetup.wasLastUsedWithOtherServer() ? LoginOthers : LoginVATSIM;
52  ui->tw_Network->setCurrentIndex(tab);
53 
54  connect(
55  ui->tw_Network, &QTabWidget::currentChanged, this,
56  [this](int) { emit this->currentServerChanged(getCurrentServer()); }, Qt::QueuedConnection);
57  connect(ui->comp_OtherServers, &CServerListSelector::serverChanged, this,
59  connect(ui->comp_VatsimServers, &CServerListSelector::serverChanged, this,
61 
62  this->reloadOtherServersSetup();
63  this->onWebServiceDataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFinished, -1, {});
64  }
65 
67 
69  {
70  return ui->cb_observer->isChecked() ? CLoginMode::Observer : CLoginMode::Pilot;
71  }
72 
74  {
75  if (mode == CLoginMode::Observer) { ui->cb_observer->setChecked(true); }
76  else { ui->cb_observer->setChecked(false); }
77  }
78 
80  {
81  const bool vatsim = ui->tw_Network->currentWidget() == ui->tb_NetworkVatsim;
82  return vatsim;
83  }
84 
86  {
87  return ui->tw_Network->currentWidget() == ui->tb_OtherServers;
88  }
89 
90  void CNetworkDetailsComponent::setServerButtonsVisible(bool visible)
91  {
92  ui->wi_OtherServersButtons->setVisible(visible);
93  ui->wi_VatsimButtons->setVisible(visible);
94  }
95 
96  void CNetworkDetailsComponent::onServerTabWidgetChanged(int index)
97  {
98  Q_UNUSED(index)
99  if (!m_updatePilotOnServerChanges) { return; }
100  const bool vatsim = this->isVatsimServerSelected();
101  const CServer server = vatsim ? this->getCurrentVatsimServer() : this->getCurrentOtherServer();
102  emit this->overridePilot(server.getUser());
103  }
104 
105  void CNetworkDetailsComponent::onSelectedServerChanged(const CServer &server)
106  {
107  if (!m_updatePilotOnServerChanges) { return; }
108  emit this->overridePilot(server.getUser());
109  }
110 
111  void CNetworkDetailsComponent::onWebServiceDataRead(CEntityFlags::Entity entity, CEntityFlags::ReadState state,
112  int number, const QUrl &url)
113  {
114  if (!CEntityFlags::isFinishedReadState(state)) { return; }
115  Q_UNUSED(number)
116  Q_UNUSED(url)
117 
118  if (entity == CEntityFlags::VatsimDataFile)
119  {
120  CServerList vatsimFsdServers = sGui->getIContextNetwork()->getVatsimFsdServers();
121  if (vatsimFsdServers.isEmpty()) { return; }
122  vatsimFsdServers.sortBy(&CServer::getName);
123  const CServer currentServer = m_networkSetup.getLastVatsimServer();
124  ui->comp_VatsimServers->setServers(vatsimFsdServers);
125  ui->comp_VatsimServers->preSelect(currentServer.getName());
126  }
127  }
128 
130  {
131  CServer server = ui->comp_VatsimServers->currentServer();
132  if (!server.getUser().hasValidVatsimId())
133  {
134  // normally VATSIM server have no valid user associated
135  const CUser user = m_networkSetup.getLastVatsimServer().getUser();
136  server.setUser(user);
137  }
138  return server;
139  }
140 
141  CServer CNetworkDetailsComponent::getCurrentOtherServer() const { return ui->comp_OtherServers->currentServer(); }
142 
144  {
145  return this->isVatsimServerSelected() ? this->getCurrentVatsimServer() : this->getCurrentOtherServer();
146  }
147 
148  bool CNetworkDetailsComponent::hasPartnerCallsign() const { return !ui->le_PartnerCallsign->text().isEmpty(); }
149 
151  {
152  if (ui->le_PartnerCallsign->text().isEmpty()) { return {}; }
153  return { ui->le_PartnerCallsign->text(), CCallsign::Aircraft };
154  }
155 
156  void CNetworkDetailsComponent::reloadOtherServersSetup()
157  {
158  const CServerList otherServers(m_networkSetup.getOtherServersPlusPredefinedServers());
159  ui->comp_OtherServers->setServers(otherServers);
160  }
161 
162 } // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
CWebDataServices * getWebDataServices() const
Get the web data services.
virtual swift::misc::network::CServerList getVatsimFsdServers() const =0
Known FSD servers, available when data file was first read.
bool wasLastUsedWithOtherServer() const
Used with an other server (i.e. non VATSIM)
swift::misc::network::CServer getLastVatsimServer() const
Last VATSIM server (VATSIM only)
swift::misc::network::CServerList getOtherServersPlusPredefinedServers() const
The other servers plus test servers.
swift::misc::network::CServer getCurrentOtherServer() const
Selected server (others)
void requestNetworkSettings()
Request network settings.
bool hasPartnerCallsign() const
Pilot or Co-pilot callsign?
void setLoginMode(swift::misc::network::CLoginMode mode)
Login mode.
void currentServerChanged(const misc::network::CServer &server)
Current selected server changed.
void overridePilot(const swift::misc::network::CUser &user)
Override the pilot.
swift::misc::network::CServer getCurrentVatsimServer() const
Selected server (VATSIM)
swift::misc::aviation::CCallsign getPartnerCallsign() const
Pilot or Co-pilot callsign.
swift::misc::network::CServer getCurrentServer() const
Current server based on selected tab.
swift::misc::network::CLoginMode getLoginMode() const
Login mode.
void serverChanged(const swift::misc::network::CServer &server)
Server has been changed.
void sortBy(K1 key1, Keys... keys)
In-place sort by some particular key(s).
Definition: sequence.h:576
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Value object encapsulating information of a callsign.
Definition: callsign.h:30
Value object encapsulating information about login mode.
Definition: loginmode.h:18
Value object encapsulating information of a server.
Definition: server.h:28
const QString & getName() const
Get name.
Definition: server.h:92
void setUser(const CUser &user)
Set user.
Definition: server.h:89
const CUser & getUser() const
Get user.
Definition: server.h:86
Value object encapsulating a list of servers.
Definition: serverlist.h:23
Value object encapsulating information of a user.
Definition: user.h:28
bool hasValidVatsimId() const
Has a valid VATSIM id?
Definition: user.h:86
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Core data traits (aka cached values) and classes.
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
High level reusable GUI components.
Definition: aboutdialog.cpp:14
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QueuedConnection
void currentChanged(int index)