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::audio;
17 using namespace swift::misc::aviation;
18 using namespace swift::core;
19 using namespace swift::core::data;
20 
21 namespace swift::gui::components
22 {
23  CNetworkDetailsComponent::CNetworkDetailsComponent(QWidget *parent)
24  : QFrame(parent), ui(new Ui::CNetworkDetailsComponent)
25  {
26  ui->setupUi(this);
27 
28  connect(ui->comp_OtherServers, &CServerListSelector::serverChanged, this,
29  &CNetworkDetailsComponent::onSelectedServerChanged);
30  connect(ui->comp_VatsimServers, &CServerListSelector::serverChanged, this,
31  &CNetworkDetailsComponent::onSelectedServerChanged);
32  connect(ui->tw_Network, &QTabWidget::currentChanged, this, &CNetworkDetailsComponent::onServerTabWidgetChanged);
33  connect(ui->pb_OtherServersGotoSettings, &QPushButton::pressed, this,
35  connect(&m_networkSetup, &CNetworkSetup::setupChanged, this, &CNetworkDetailsComponent::reloadOtherServersSetup,
36  Qt::QueuedConnection);
37 
38  // web service data
39  if (sGui && sGui->getWebDataServices())
40  {
41  connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this,
42  &CNetworkDetailsComponent::onWebServiceDataRead, Qt::QueuedConnection);
43  }
44 
45  constexpr int MaxLength = 10;
46  constexpr int MinLength = 0;
47  CUpperCaseValidator *ucv = new CUpperCaseValidator(MinLength, MaxLength, ui->le_PartnerCallsign);
49  ui->le_PartnerCallsign->setMaxLength(MaxLength);
50  ui->le_PartnerCallsign->setValidator(ucv);
51 
52  const int tab = m_networkSetup.wasLastUsedWithOtherServer() ? LoginOthers : LoginVATSIM;
53  ui->tw_Network->setCurrentIndex(tab);
54 
55  connect(
56  ui->tw_Network, &QTabWidget::currentChanged, this,
57  [this](int) { emit this->currentServerChanged(getCurrentServer()); }, Qt::QueuedConnection);
58  connect(ui->comp_OtherServers, &CServerListSelector::serverChanged, this,
59  &CNetworkDetailsComponent::currentServerChanged, Qt::QueuedConnection);
60  connect(ui->comp_VatsimServers, &CServerListSelector::serverChanged, this,
61  &CNetworkDetailsComponent::currentServerChanged, Qt::QueuedConnection);
62 
63  this->reloadOtherServersSetup();
64  this->onWebServiceDataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFinished, -1, {});
65  }
66 
68 
70  {
71  return ui->cb_observer->isChecked() ? CLoginMode::Observer : CLoginMode::Pilot;
72  }
73 
75  {
76  if (mode == CLoginMode::Observer) { ui->cb_observer->setChecked(true); }
77  else { ui->cb_observer->setChecked(false); }
78  }
79 
81  {
82  const bool vatsim = ui->tw_Network->currentWidget() == ui->tb_NetworkVatsim;
83  return vatsim;
84  }
85 
87  {
88  return ui->tw_Network->currentWidget() == ui->tb_OtherServers;
89  }
90 
91  void CNetworkDetailsComponent::setServerButtonsVisible(bool visible)
92  {
93  ui->wi_OtherServersButtons->setVisible(visible);
94  ui->wi_VatsimButtons->setVisible(visible);
95  }
96 
97  void CNetworkDetailsComponent::onServerTabWidgetChanged(int index)
98  {
99  Q_UNUSED(index)
100  if (!m_updatePilotOnServerChanges) { return; }
101  const bool vatsim = this->isVatsimServerSelected();
102  const CServer server = vatsim ? this->getCurrentVatsimServer() : this->getCurrentOtherServer();
103  emit this->overridePilot(server.getUser());
104  }
105 
106  void CNetworkDetailsComponent::onSelectedServerChanged(const CServer &server)
107  {
108  if (!m_updatePilotOnServerChanges) { return; }
109  emit this->overridePilot(server.getUser());
110  }
111 
112  void CNetworkDetailsComponent::onWebServiceDataRead(CEntityFlags::Entity entity, CEntityFlags::ReadState state,
113  int number, const QUrl &url)
114  {
115  if (!CEntityFlags::isFinishedReadState(state)) { return; }
116  Q_UNUSED(number)
117  Q_UNUSED(url)
118 
119  if (entity == CEntityFlags::VatsimDataFile)
120  {
121  CServerList vatsimFsdServers = sGui->getIContextNetwork()->getVatsimFsdServers();
122  if (vatsimFsdServers.isEmpty()) { return; }
123  vatsimFsdServers.sortBy(&CServer::getName);
124  const CServer currentServer = m_networkSetup.getLastVatsimServer();
125  ui->comp_VatsimServers->setServers(vatsimFsdServers);
126  ui->comp_VatsimServers->preSelect(currentServer.getName());
127  }
128  }
129 
131  {
132  CServer server = ui->comp_VatsimServers->currentServer();
133  if (!server.getUser().hasValidVatsimId())
134  {
135  // normally VATSIM server have no valid user associated
136  const CUser user = m_networkSetup.getLastVatsimServer().getUser();
137  server.setUser(user);
138  }
139  return server;
140  }
141 
142  CServer CNetworkDetailsComponent::getCurrentOtherServer() const { return ui->comp_OtherServers->currentServer(); }
143 
145  {
146  return this->isVatsimServerSelected() ? this->getCurrentVatsimServer() : this->getCurrentOtherServer();
147  }
148 
149  bool CNetworkDetailsComponent::hasPartnerCallsign() const { return !ui->le_PartnerCallsign->text().isEmpty(); }
150 
152  {
153  if (ui->le_PartnerCallsign->text().isEmpty()) { return {}; }
154  return CCallsign(ui->le_PartnerCallsign->text(), CCallsign::Aircraft);
155  }
156 
157  void CNetworkDetailsComponent::reloadOtherServersSetup()
158  {
159  const CServerList otherServers(m_networkSetup.getOtherServersPlusPredefinedServers());
160  ui->comp_OtherServers->setServers(otherServers);
161  }
162 
163 } // 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.
void setAllowedCharacters09AZ()
Set the allowed characters as 0-9 and A-Z.
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:13