#include "connectionUiController.h" #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(MACOS_NE) #include #else #include #endif #include "amneziaApplication.h" #include "core/controllers/serversController.h" #include "core/models/containerConfig.h" #include "core/utils/containerEnum.h" ConnectionUiController::ConnectionUiController(ConnectionController* connectionController, ServersController* serversController, QObject *parent) : QObject(parent), m_connectionController(connectionController), m_serversController(serversController) { connect(m_connectionController, &ConnectionController::connectionStateChanged, this, &ConnectionUiController::onConnectionStateChanged); connect(m_connectionController, &ConnectionController::serverSwitchFailed, this, &ConnectionUiController::serverSwitchFailed); connect(this, &ConnectionUiController::connectButtonClicked, this, &ConnectionUiController::toggleConnection, Qt::QueuedConnection); m_state = Vpn::ConnectionState::Disconnected; } void ConnectionUiController::openConnection() { const QString serverId = m_serversController->getDefaultServerId(); if (serverId.isEmpty()) { return; } ErrorCode errorCode = m_connectionController->openConnection(serverId); if (errorCode != ErrorCode::NoError) { notifyConnectionBlocked(errorCode); return; } } void ConnectionUiController::closeConnection() { m_connectionController->closeConnection(); } ErrorCode ConnectionUiController::getLastConnectionError() { return m_connectionController->lastConnectionError(); } void ConnectionUiController::onConnectionStateChanged(Vpn::ConnectionState state) { m_state = state; m_isConnected = false; m_connectionStateText = tr("Connecting..."); switch (state) { case Vpn::ConnectionState::Connected: { amnApp->networkManager()->clearConnectionCache(); m_isConnectionInProgress = false; m_isConnected = true; m_connectionStateText = tr("Connected"); break; } case Vpn::ConnectionState::Switching: { m_isConnectionInProgress = true; m_isConnected = true; m_connectionStateText = tr("Switching..."); break; } case Vpn::ConnectionState::Connecting: { m_isConnectionInProgress = true; break; } case Vpn::ConnectionState::Reconnecting: { m_isConnectionInProgress = true; m_connectionStateText = tr("Reconnecting..."); break; } case Vpn::ConnectionState::Disconnected: { m_isConnectionInProgress = false; m_connectionStateText = tr("Connect"); break; } case Vpn::ConnectionState::Disconnecting: { m_isConnectionInProgress = true; m_connectionStateText = tr("Disconnecting..."); break; } case Vpn::ConnectionState::Preparing: { m_isConnectionInProgress = true; m_connectionStateText = tr("Preparing..."); break; } case Vpn::ConnectionState::Error: { m_isConnectionInProgress = false; m_connectionStateText = tr("Connect"); emit connectionErrorOccurred(getLastConnectionError()); break; } case Vpn::ConnectionState::Unknown: { m_isConnectionInProgress = false; m_connectionStateText = tr("Connect"); emit connectionErrorOccurred(getLastConnectionError()); break; } } emit connectionStateChanged(); } void ConnectionUiController::onTranslationsUpdated() { onConnectionStateChanged(getCurrentConnectionState()); } Vpn::ConnectionState ConnectionUiController::getCurrentConnectionState() { return m_state; } QString ConnectionUiController::connectionStateText() const { return m_connectionStateText; } void ConnectionUiController::toggleConnection() { if (m_state == Vpn::ConnectionState::Preparing) { emit preparingConfig(); return; } if (isConnectionInProgress()) { closeConnection(); } else if (isConnected()) { closeConnection(); } else { const QString serverId = m_serversController->getDefaultServerId(); if (serverId.isEmpty()) { return; } const ErrorCode errorCode = m_connectionController->isConnectionSupported(serverId); if (errorCode != ErrorCode::NoError) { notifyConnectionBlocked(errorCode); return; } emit prepareConfig(); } } void ConnectionUiController::notifyConnectionBlocked(ErrorCode errorCode) { if (errorCode == ErrorCode::LegacyApiV1NotSupportedError) { emit unsupportedConnectDrawerRequested(); return; } if (errorCode == ErrorCode::NoInstalledContainersError) { emit noInstalledContainers(); return; } emit connectionErrorOccurred(errorCode); } bool ConnectionUiController::isConnectionInProgress() const { return m_isConnectionInProgress; } bool ConnectionUiController::isConnected() const { return m_isConnected; } bool ConnectionUiController::isRevokeBlockedDuringActiveConnection(const QString &serverId, int containerIndex, const QString &clientId) const { if (clientId.isEmpty() || (!isConnected() && !isConnectionInProgress())) { return false; } if (m_serversController->getDefaultServerId() != serverId) { return false; } if (static_cast(m_serversController->getDefaultContainer(serverId)) != containerIndex) { return false; } const auto adminConfig = m_serversController->selfHostedAdminConfig(serverId); if (!adminConfig.has_value()) { return false; } const QString connectionClientId = adminConfig->containerConfig(static_cast(containerIndex)).protocolConfig.clientId(); if (connectionClientId.isEmpty()) { return false; } return connectionClientId == clientId || connectionClientId.contains(clientId); }