* @brief Enables or disables the TCP API server. */
| 419 | * @brief Enables or disables the TCP API server. |
| 420 | */ |
| 421 | void API::Server::setEnabled(const bool enabled) |
| 422 | { |
| 423 | Q_ASSERT(m_worker); |
| 424 | |
| 425 | bool effectiveEnabled = enabled; |
| 426 | bool closeResources = false; |
| 427 | |
| 428 | if (enabled) { |
| 429 | if (!m_server.isListening()) { |
| 430 | m_server.setMaxPendingConnections(kMaxApiClients); |
| 431 | const auto address = m_externalConnections ? QHostAddress::Any : QHostAddress::LocalHost; |
| 432 | if (!m_server.listen(address, API_TCP_PORT)) { |
| 433 | Misc::Utilities::showMessageBox( |
| 434 | tr("Unable to start API TCP server"), m_server.errorString(), QMessageBox::Warning); |
| 435 | m_server.close(); |
| 436 | effectiveEnabled = false; |
| 437 | closeResources = true; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | else { |
| 443 | m_server.close(); |
| 444 | closeResources = true; |
| 445 | |
| 446 | if (m_externalConnections) { |
| 447 | m_externalConnections = false; |
| 448 | m_settings.setValue("API/ExternalConnections", false); |
| 449 | Q_EMIT externalConnectionsChanged(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (closeResources) { |
| 454 | m_connections.clear(); |
| 455 | auto* worker = static_cast<ServerWorker*>(m_worker); |
| 456 | QMetaObject::invokeMethod(worker, "closeResources", Qt::QueuedConnection); |
| 457 | } |
| 458 | |
| 459 | if (m_enabled != effectiveEnabled) { |
| 460 | m_enabled = effectiveEnabled; |
| 461 | Q_EMIT enabledChanged(); |
| 462 | } else if (enabled != effectiveEnabled) |
| 463 | Q_EMIT enabledChanged(); |
| 464 | |
| 465 | m_settings.setValue("API/Enabled", effectiveEnabled); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * @brief Sets whether the server accepts connections from external hosts. |
no test coverage detected