/* Copyright (C) 2019 * swift project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, * or distributed except according to the terms contained in the LICENSE file. */ #include "networkdetailscomponent.h" #include "ui_networkdetailscomponent.h" #include "blackgui/uppercasevalidator.h" #include "blackgui/guiapplication.h" #include "blackcore/context/contextnetwork.h" #include "blackcore/webdataservices.h" #include using namespace BlackMisc::Network; using namespace BlackMisc::Audio; using namespace BlackCore; using namespace BlackCore::Data; namespace BlackGui { namespace Components { CNetworkDetailsComponent::CNetworkDetailsComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CNetworkDetailsComponent) { ui->setupUi(this); ui->tw_Details->setCurrentIndex(0); ui->sw_NetworkServerDetails->setCurrentIndex(PageServer); connect(ui->comp_OtherServers, &CServerListSelector::serverChanged, this, &CNetworkDetailsComponent::onSelectedServerChanged); connect(ui->comp_VatsimServers, &CServerListSelector::serverChanged, this, &CNetworkDetailsComponent::onSelectedServerChanged); connect(ui->tw_Network, &QTabWidget::currentChanged, this, &CNetworkDetailsComponent::onServerTabWidgetChanged); connect(ui->tw_Details, &QTabWidget::currentChanged, this, &CNetworkDetailsComponent::onDetailsTabChanged); connect(ui->pb_OtherServersGotoSettings, &QPushButton::pressed, this, &CNetworkDetailsComponent::requestNetworkSettings); connect(ui->pb_OverrideCredentialsVatsim, &QPushButton::clicked, this, &CNetworkDetailsComponent::onOverrideCredentialsToPilot); connect(ui->pb_OverrideCredentialsOtherServers, &QPushButton::clicked, this, &CNetworkDetailsComponent::onOverrideCredentialsToPilot); connect(ui->pb_DetailsVatsim, &QPushButton::clicked, this, &CNetworkDetailsComponent::onChangePage); connect(ui->pb_DetailsOtherServers, &QPushButton::clicked, this, &CNetworkDetailsComponent::onChangePage); connect(ui->pb_BackToServer, &QPushButton::clicked, this, &CNetworkDetailsComponent::onChangePage); connect(&m_networkSetup, &CNetworkSetup::setupChanged, this, &CNetworkDetailsComponent::reloadOtherServersSetup, Qt::QueuedConnection); // web service data if (sGui && sGui->getWebDataServices()) { connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CNetworkDetailsComponent::onWebServiceDataRead, Qt::QueuedConnection); } ui->form_FsdDetails->showEnableInfo(true); ui->form_FsdDetails->setFsdSetupEnabled(false); ui->form_Voice->showEnableInfo(true); ui->form_Voice->setVoiceSetupEnabled(false); constexpr int MaxLength = 10; constexpr int MinLength = 0; CUpperCaseValidator *ucv = new CUpperCaseValidator(MinLength, MaxLength, ui->le_Copilot); ucv->setAllowedCharacters09AZ(); ui->le_Copilot->setMaxLength(MaxLength); ui->le_Copilot->setValidator(ucv); const int tab = m_networkSetup.wasLastUsedWithOtherServer() ? LoginOthers : LoginVATSIM; ui->tw_Network->setCurrentIndex(tab); this->reloadOtherServersSetup(); this->onWebServiceDataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFinished, -1); } CNetworkDetailsComponent::~CNetworkDetailsComponent() { } INetwork::LoginMode CNetworkDetailsComponent::getLoginMode() const { return ui->frp_LoginMode->getLoginMode(); } void CNetworkDetailsComponent::setLoginMode(INetwork::LoginMode mode) { ui->frp_LoginMode->setLoginMode(mode); } bool CNetworkDetailsComponent::isVatsimServerSelected() const { const bool vatsim = ui->tw_Network->currentWidget() == ui->tb_NetworkVatsim; return vatsim; } bool CNetworkDetailsComponent::isOtherServerSelected() const { return ui->tw_Details->currentWidget() == ui->tb_OtherServers; } CVoiceSetup CNetworkDetailsComponent::getVoiceSetup() const { return ui->form_Voice->getValue(); } bool CNetworkDetailsComponent::isVoiceSetupOverrideEnabled() const { return ui->form_Voice->isVoiceSetupEnabled(); } CFsdSetup CNetworkDetailsComponent::getFsdSetup() const { return ui->form_FsdDetails->getValue(); } bool CNetworkDetailsComponent::isFsdSetupOverrideEnabled() const { return ui->form_FsdDetails->isFsdSetupEnabled(); } void CNetworkDetailsComponent::setServerButtonsVisible(bool visible) { ui->wi_OtherServersButtons->setVisible(visible); ui->wi_VatsimButtons->setVisible(visible); } void CNetworkDetailsComponent::onDetailsTabChanged(int index) { if (index == DetailsBack) { ui->sw_NetworkServerDetails->setCurrentIndex(PageServer); return; } Q_UNUSED(index); const CServer server = this->getCurrentServer(); // only override if not yet enabled if (!ui->form_FsdDetails->isFsdSetupEnabled()) { ui->form_FsdDetails->setValue(server.getFsdSetup()); } if (!ui->form_Voice->isVoiceSetupEnabled()) { ui->form_Voice->setValue(server.getVoiceSetup()); } } void CNetworkDetailsComponent::onOverrideCredentialsToPilot() { CServer server; const QObject *s = QObject::sender(); if (s == ui->pb_OverrideCredentialsOtherServers) { server = this->getCurrentOtherServer(); } else if (s == ui->pb_OverrideCredentialsVatsim) { // the VATSIM server selected has no valid user credentials server = m_networkSetup.getLastVatsimServer(); } else { return; } emit this->overridePilot(server.getUser()); } void CNetworkDetailsComponent::onServerTabWidgetChanged(int index) { Q_UNUSED(index); if (!m_updatePilotOnServerChanges) { return; } const bool vatsim = this->isVatsimServerSelected(); const CServer server = vatsim ? this->getCurrentVatsimServer() : this->getCurrentOtherServer(); emit this->overridePilot(server.getUser()); } void CNetworkDetailsComponent::onSelectedServerChanged(const CServer &server) { if (!m_updatePilotOnServerChanges) { return; } const bool vatsim = this->isVatsimServerSelected(); const CUser user = vatsim ? this->getCurrentVatsimServer().getUser() : server.getUser(); emit this->overridePilot(server.getUser()); } void CNetworkDetailsComponent::onWebServiceDataRead(CEntityFlags::Entity entity, CEntityFlags::ReadState state, int number) { if (!CEntityFlags::isFinishedReadState(state)) { return; } Q_UNUSED(number); if (entity == CEntityFlags::VatsimDataFile) { CServerList vatsimFsdServers = sGui->getIContextNetwork()->getVatsimFsdServers(); if (vatsimFsdServers.isEmpty()) { return; } vatsimFsdServers.sortBy(&CServer::getName); const CServer currentServer = m_networkSetup.getLastVatsimServer(); ui->comp_VatsimServers->setServers(vatsimFsdServers, true); ui->comp_VatsimServers->preSelect(currentServer.getName()); } } void CNetworkDetailsComponent::onChangePage() { const QObject *s = QObject::sender(); if (s == ui->pb_DetailsVatsim || s == ui->pb_DetailsOtherServers) { ui->sw_NetworkServerDetails->setCurrentIndex(PageDetails); ui->tw_Details->setCurrentIndex(DetailsServer); } else { ui->sw_NetworkServerDetails->setCurrentIndex(PageServer); } } CServer CNetworkDetailsComponent::getCurrentVatsimServer() const { CServer server = ui->comp_VatsimServers->currentServer(); if (!server.getUser().hasValidVatsimId()) { // normally VATSIM server have no valid user associated const CUser user = m_networkSetup.getLastVatsimServer().getUser(); server.setUser(user); } return server; } CServer CNetworkDetailsComponent::getCurrentOtherServer() const { return ui->comp_OtherServers->currentServer(); } CServer CNetworkDetailsComponent::getCurrentServer() const { return this->isVatsimServerSelected() ? this->getCurrentVatsimServer() : this->getCurrentOtherServer(); } void CNetworkDetailsComponent::reloadOtherServersSetup() { const CServerList otherServers(m_networkSetup.getOtherServersPlusPredefinedServers()); ui->comp_OtherServers->setServers(otherServers); } } // ns } // ns