diff --git a/client/core/controllers/api/subscriptionController.cpp b/client/core/controllers/api/subscriptionController.cpp index d7f149f43..8efc14558 100644 --- a/client/core/controllers/api/subscriptionController.cpp +++ b/client/core/controllers/api/subscriptionController.cpp @@ -679,6 +679,27 @@ void SubscriptionController::removeApiConfig(const QString &serverId) serverConfigUtils::configTypeFromJson(apiV2->toJson())); } +bool SubscriptionController::removeServer(const QString &serverId) +{ + if (serverId.isEmpty()) { + return false; + } + + if (!m_serversRepository->apiV2Config(serverId).has_value()) { + qWarning().noquote() << "SubscriptionController::removeServer: not an Api V2 server, id" << serverId; + return false; + } + + const ErrorCode revokeError = deactivateDevice(serverId); + if (revokeError != ErrorCode::NoError && revokeError != ErrorCode::ApiNotFoundError) { + qWarning().noquote() << "SubscriptionController::removeServer: deactivateDevice failed (error" + << static_cast(revokeError) << "); removing locally anyway."; + } + + m_serversRepository->removeServer(serverId); + return true; +} + bool SubscriptionController::isApiKeyExpired(const QString &serverId) const { auto apiV2 = m_serversRepository->apiV2Config(serverId); diff --git a/client/core/controllers/api/subscriptionController.h b/client/core/controllers/api/subscriptionController.h index 5b0f65da6..a0ac5d24b 100644 --- a/client/core/controllers/api/subscriptionController.h +++ b/client/core/controllers/api/subscriptionController.h @@ -74,6 +74,8 @@ public: void removeApiConfig(const QString &serverId); + bool removeServer(const QString &serverId); + void setCurrentProtocol(const QString &serverId, const QString &protocolName); bool isVlessProtocol(const QString &serverId) const; diff --git a/client/ui/controllers/api/subscriptionUiController.cpp b/client/ui/controllers/api/subscriptionUiController.cpp index 5dee205dc..550334c3a 100644 --- a/client/ui/controllers/api/subscriptionUiController.cpp +++ b/client/ui/controllers/api/subscriptionUiController.cpp @@ -406,6 +406,15 @@ void SubscriptionUiController::removeApiConfig(const QString &serverId) emit apiConfigRemoved(tr("Api config removed")); } +void SubscriptionUiController::removeServer(const QString &serverId) +{ + const QString serverName = m_serversController->notificationDisplayName(serverId); + if (!m_subscriptionController->removeServer(serverId)) { + return; + } + emit apiServerRemoved(tr("Server '%1' was removed").arg(serverName)); +} + QList SubscriptionUiController::getQrCodes() { diff --git a/client/ui/controllers/api/subscriptionUiController.h b/client/ui/controllers/api/subscriptionUiController.h index e4be939a3..8d28b52c9 100644 --- a/client/ui/controllers/api/subscriptionUiController.h +++ b/client/ui/controllers/api/subscriptionUiController.h @@ -58,6 +58,8 @@ public slots: void removeApiConfig(const QString &serverId); + void removeServer(const QString &serverId); + bool getAccountInfo(const QString &serverId, bool reload); void getRenewalLink(const QString &serverId); @@ -78,6 +80,7 @@ signals: void subscriptionRefreshNeeded(); void apiConfigRemoved(const QString &message); + void apiServerRemoved(const QString &message); void vpnKeyExportReady(); diff --git a/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml b/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml index 07fb3089d..e1b8c7664 100644 --- a/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml +++ b/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml @@ -506,7 +506,7 @@ PageType { PageController.showNotificationMessage(qsTr("Cannot remove server during active connection")) } else { PageController.showBusyIndicator(true) - InstallController.removeServer(ServersUiController.getServerId(ServersUiController.processedServerIndex)) + SubscriptionUiController.removeServer(ServersUiController.getServerId(ServersUiController.processedServerIndex)) PageController.showBusyIndicator(false) } } diff --git a/client/ui/qml/Pages2/PageStart.qml b/client/ui/qml/Pages2/PageStart.qml index 00411b5e1..950a5a7d8 100644 --- a/client/ui/qml/Pages2/PageStart.qml +++ b/client/ui/qml/Pages2/PageStart.qml @@ -224,6 +224,16 @@ PageType { PageController.showNotificationMessage(message) } + function onApiServerRemoved(message) { + if (!ServersModel.getServersCount()) { + PageController.goToPageHome() + } else { + PageController.goToStartPage() + PageController.goToPage(PageEnum.PageSettingsServersList) + } + PageController.showNotificationMessage(message) + } + function onInstallServerFromApiFinished(message, preferredDefaultIndex) { if (!ConnectionController.isConnected) { if (preferredDefaultIndex !== undefined && preferredDefaultIndex >= 0) {