forked from Support/sp-hydra-veil-gui
Disconnect of profiles on program exit, now goes through the new action modules. And global state matches the repo's core changes
This commit is contained in:
parent
993c7f5ed9
commit
6dd040b5f8
4 changed files with 125 additions and 76 deletions
111
gui/main_ui.py
111
gui/main_ui.py
|
|
@ -24,6 +24,7 @@ from core.Errors import UnknownConnectionTypeError
|
|||
from core.errors.logger import logger as core_logger
|
||||
from core.models.Result import Result, ResultError
|
||||
from core.observers.Notifications import Notifications, EventType
|
||||
from core.infrastructure.global_state_coordinator import quantity_connected
|
||||
|
||||
core_logger.propagate = False
|
||||
|
||||
|
|
@ -71,6 +72,10 @@ from gui.v2.actions.ticket_failure import (
|
|||
clear_ticket_verification_failure,
|
||||
)
|
||||
from gui.v2.actions.should_be_synchronized import should_be_synchronized
|
||||
from gui.v2.actions.menu_coordinator import disconnect_active
|
||||
from gui.v2.ui.presenters.main_presenters import disconnect_message
|
||||
from gui.v2.ui.popups.confirmation_popup import ConfirmationPopup
|
||||
|
||||
|
||||
class CustomWindow(QMainWindow):
|
||||
def __init__(self, force_sync=False):
|
||||
|
|
@ -647,98 +652,84 @@ class CustomWindow(QMainWindow):
|
|||
|
||||
return f"rgb{current_rgb}"
|
||||
|
||||
def closeEvent(self, event=None):
|
||||
core_logger.info("HydraVeil application closing")
|
||||
|
||||
def closeEvent(self, event):
|
||||
core_logger.info("application closing")
|
||||
|
||||
# 1. Guard: Already in the final stage of closing
|
||||
if self._closing_after_disconnect:
|
||||
orm.stop()
|
||||
if event is not None:
|
||||
event.accept()
|
||||
self._perform_final_shutdown(event)
|
||||
return
|
||||
|
||||
# 2. Guard: Prevent multiple close attempts while a process is pending
|
||||
if self._close_confirmation_pending or self._close_disconnect_in_progress:
|
||||
if event is not None:
|
||||
event.ignore()
|
||||
event.ignore()
|
||||
return
|
||||
|
||||
connected_profiles = self._get_enabled_profile_ids()
|
||||
if connected_profiles:
|
||||
if event is not None:
|
||||
event.ignore()
|
||||
self._show_close_disconnect_confirmation(connected_profiles)
|
||||
# 3. Check for active connections
|
||||
quantity = quantity_connected()
|
||||
if quantity and quantity > 0:
|
||||
self._start_disconnect_workflow(quantity, event)
|
||||
return
|
||||
|
||||
# 4. Clean close (no connections)
|
||||
self._perform_final_shutdown(event)
|
||||
|
||||
def _perform_final_shutdown(self, event):
|
||||
"""Consolidated logic for stopping the ORM and accepting the close event."""
|
||||
orm.stop()
|
||||
if event is not None:
|
||||
event.accept()
|
||||
|
||||
def _get_enabled_profile_ids(self):
|
||||
try:
|
||||
profiles = ProfileController.get_all()
|
||||
except Exception:
|
||||
return self.connection_manager.get_connected_profiles()
|
||||
|
||||
connected_profiles = []
|
||||
self.connection_manager._connected_profiles.clear()
|
||||
for profile_id, profile in profiles.items():
|
||||
try:
|
||||
if is_profile_enabled_for_gui(profile):
|
||||
connected_profiles.append(profile_id)
|
||||
self.connection_manager.add_connected_profile(profile_id)
|
||||
except Exception:
|
||||
pass
|
||||
return connected_profiles
|
||||
|
||||
def _show_close_disconnect_confirmation(self, connected_profiles):
|
||||
from gui.v2.ui.popups.confirmation_popup import ConfirmationPopup
|
||||
event.accept()
|
||||
|
||||
def _start_disconnect_workflow(self, quantity, event):
|
||||
"""Handles the UI and state for disconnecting profiles before exit."""
|
||||
event.ignore()
|
||||
self._close_confirmation_pending = True
|
||||
message = self._create_close_disconnect_message(connected_profiles)
|
||||
|
||||
message = f"You still have {quantity} profiles connected. Disconnect them?"
|
||||
self.popup = ConfirmationPopup(
|
||||
self, message=message, action_button_text="Exit", cancel_button_text="Cancel")
|
||||
self,
|
||||
message=message,
|
||||
action_button_text="Exit",
|
||||
cancel_button_text="Cancel"
|
||||
)
|
||||
self.popup.setWindowModality(Qt.WindowModality.ApplicationModal)
|
||||
self.popup.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
|
||||
self.popup.finished.connect(
|
||||
lambda result: self._handle_close_disconnect_result(result, connected_profiles))
|
||||
self.popup.destroyed.connect(
|
||||
lambda *_: self._handle_close_disconnect_popup_closed())
|
||||
|
||||
# Pass the result directly to the handler
|
||||
self.popup.finished.connect(lambda result: self._on_disconnect_dialog_finished(confirmed=result, quantity=quantity))
|
||||
self.popup.destroyed.connect(self._handle_popup_destroyed)
|
||||
self.popup.show()
|
||||
|
||||
def _create_close_disconnect_message(self, connected_profiles):
|
||||
profile_numbers = ', '.join(str(profile_id)
|
||||
for profile_id in connected_profiles)
|
||||
is_are = "is" if len(connected_profiles) == 1 else "are"
|
||||
return f'Profile{"" if len(connected_profiles) == 1 else "s"} {profile_numbers} {is_are} still connected.\nAll connected profiles will be disconnected on exit.\nDo you want to proceed?'
|
||||
|
||||
def _handle_close_disconnect_result(self, result, connected_profiles):
|
||||
def _on_disconnect_dialog_finished(self, confirmed, quantity):
|
||||
"""Regardless of result, this runs."""
|
||||
self._close_confirmation_pending = False
|
||||
if not result:
|
||||
|
||||
# make sure user opted in to close:
|
||||
if not confirmed:
|
||||
return
|
||||
|
||||
self._close_disconnect_in_progress = True
|
||||
self.update_status('Disabling profiles...')
|
||||
self.worker_thread = WorkerThread(
|
||||
'DISABLE_ALL_PROFILES', profile_data=connected_profiles)
|
||||
self.worker_thread.text_output.connect(self.update_status)
|
||||
self.worker_thread.finished.connect(self._finish_close_disconnect)
|
||||
self.worker_thread.start()
|
||||
disconnect_active(ui_caller=self._finish_close_disconnect)
|
||||
|
||||
def _handle_close_disconnect_popup_closed(self):
|
||||
if self._close_confirmation_pending:
|
||||
self._close_confirmation_pending = False
|
||||
def _handle_popup_destroyed(self):
|
||||
# Ensure flag is reset if window is closed via 'X' instead of buttons
|
||||
self._close_confirmation_pending = False
|
||||
self.popup = None
|
||||
|
||||
def _finish_close_disconnect(self, _ok=True):
|
||||
def _finish_close_disconnect(self, result: Result):
|
||||
self._close_disconnect_in_progress = False
|
||||
connected_profiles = self._get_enabled_profile_ids()
|
||||
if connected_profiles:
|
||||
self.update_status('Could not disconnect all profiles. Exit canceled.')
|
||||
|
||||
if not result.valid:
|
||||
self.update_status(str(result.message))
|
||||
return
|
||||
|
||||
self.update_status('All profiles disabled. Bye!')
|
||||
self._closing_after_disconnect = True
|
||||
# Trigger closeEvent again; this time it will hit the first Guard
|
||||
QTimer.singleShot(0, self.close)
|
||||
|
||||
|
||||
def update_image(self, appearance_value="original"):
|
||||
image_path = os.path.join(self.btn_path, f"{appearance_value}.png")
|
||||
pixmap = QPixmap(image_path)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
# core
|
||||
from core.infrastructure.global_state_coordinator import connected_system_profile_id, connected_session_profile_ids
|
||||
|
||||
# from core.services.networking.systemwide.systemwide_wireguard import change_port_then_reconnect
|
||||
# from core.services.networking.ports.port_orchestrator import change_wg_port
|
||||
from core.controllers.ConfigurationController import ConfigurationController
|
||||
# from core.services.prepare_tickets.ticket_tracker import get_a_random_ticket
|
||||
from core.models.BaseProfile import BaseProfile as Profile
|
||||
# from core.models.BaseProfile import BaseProfile as Profile
|
||||
from core.services.profiles.profile_connections import enable_profile, disable_profile
|
||||
from core.controllers.ProfileController import ProfileController
|
||||
from core.models.Configuration import Configuration, ConnectionChoice
|
||||
|
|
@ -30,7 +32,7 @@ from gui.v2.infrastructure.setup_observers import (
|
|||
from gui.v2.actions.error_handlers import navigate_to_new_sub_page
|
||||
|
||||
# generic
|
||||
from typing import Callable, Optional
|
||||
from typing import Callable
|
||||
from functools import partial
|
||||
|
||||
@type_checker
|
||||
|
|
@ -154,7 +156,7 @@ def NOT_need_tor_warnings(profile_id: int) -> Result:
|
|||
@register_action(ResultError.RECONNECT)
|
||||
def literally_connect(profile_id: int) -> Result:
|
||||
|
||||
print(f"Literally Connecting...")
|
||||
print("Literally Connecting...")
|
||||
|
||||
# Step 1: Get the Profile
|
||||
profile_result = get_profile(profile_id)
|
||||
|
|
@ -201,7 +203,7 @@ def _connect_profile(profile_id: int, passed_ui_caller: Callable) -> Result:
|
|||
# Step 1: Subscription filter:
|
||||
sub_result = filter_subscription(profile_id=profile_id, passed_ui_caller=passed_ui_caller)
|
||||
if not sub_result.valid:
|
||||
return sub_result
|
||||
return sub_result.wrap("Sub filter failed.")
|
||||
|
||||
# Step 2: Tor filter check:
|
||||
result = NOT_need_tor_warnings(profile_id=profile_id)
|
||||
|
|
@ -245,6 +247,9 @@ def connect_with_billing_code(profile_id: int, billing_code: str) -> Result:
|
|||
def literally_disconnect(profile_id: int) -> Result:
|
||||
"""Get a profile and use core to disable it."""
|
||||
|
||||
if profile_id is None:
|
||||
return Result(valid=True)
|
||||
|
||||
# Step 1: Get the Profile
|
||||
profile_result = get_profile(profile_id)
|
||||
if not profile_result:
|
||||
|
|
@ -272,22 +277,67 @@ def literally_disconnect(profile_id: int) -> Result:
|
|||
return translate_error(error=error, profile_id=profile_id)
|
||||
|
||||
|
||||
|
||||
def disconnect_profile(profile_id: int, ui_caller: Callable) -> Result:
|
||||
def disconnect_profile(profile_id: int|list, ui_caller: Callable) -> Result:
|
||||
"""Disconnect a single profile"""
|
||||
print("GUI is disconnecting a profile")
|
||||
return literally_disconnect(profile_id)
|
||||
|
||||
# take_action(
|
||||
# action=literally_disconnect,
|
||||
# ui_caller=ui_caller,
|
||||
# profile_id=profile_id
|
||||
# )
|
||||
|
||||
def disconnect_many_profiles(profile_ids: list) -> Result:
|
||||
"""Disconnect a list of profiles entered."""
|
||||
|
||||
# turn into list if it's not:
|
||||
if not isinstance(profile_ids, list):
|
||||
profile_ids = [profile_ids]
|
||||
|
||||
failed_list = []
|
||||
|
||||
for each_profile_id in profile_ids:
|
||||
disconnected = literally_disconnect(each_profile_id)
|
||||
if disconnected.valid:
|
||||
continue
|
||||
else:
|
||||
failed_list.append(each_profile_id)
|
||||
|
||||
if not failed_list or len(failed_list) == 0:
|
||||
return Result(valid=True)
|
||||
else:
|
||||
return Result(valid=False, data=failed_list)
|
||||
|
||||
|
||||
def disconnect_active(ui_caller: Callable) -> Result:
|
||||
"""Disconnects active session profiles first, then system."""
|
||||
|
||||
# @register_action(ResultError.PORT)
|
||||
# def reconnect_with_port_change() -> Result:
|
||||
# SESSION
|
||||
session_ids = connected_session_profile_ids()
|
||||
|
||||
if session_ids:
|
||||
logger.info(f"Disconnecting session profiles {session_ids}")
|
||||
disconnected = disconnect_many_profiles(session_ids)
|
||||
if not disconnected.valid:
|
||||
logger.error(f"Failed disconnects are {disconnected.data}")
|
||||
error_msg = f'Could not disconnect profiles {disconnected.data}. Exit canceled.'
|
||||
disconnected.message = error_msg
|
||||
return disconnected
|
||||
|
||||
logger.info("Finished disconnecting session profiles")
|
||||
|
||||
# SYSTEM:
|
||||
system_profile_id = connected_system_profile_id()
|
||||
|
||||
if system_profile_id:
|
||||
logger.info(f"Disconnecting System profile {system_profile_id}")
|
||||
system_disconnected = literally_disconnect(system_profile_id)
|
||||
if not system_disconnected.valid:
|
||||
error_msg = f'Could not disconnect systemwide profile {system_profile_id}. Exit canceled.'
|
||||
disconnected.message = error_msg
|
||||
return disconnected
|
||||
|
||||
ui_caller(Result(valid=True))
|
||||
|
||||
|
||||
# def disconnect_active(ui_caller: Callable):
|
||||
# take_action(
|
||||
# action=change_port_then_reconnect,
|
||||
# action=disconnect_active_profiles,
|
||||
# ui_caller=ui_caller
|
||||
# )
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from core.infrastructure.global_state_coordinator import connected_session_profile_ids, connected_system_profile_id, is_systemwide_on, is_connected, evaluate_state_on_startup
|
||||
from core.infrastructure.global_state_coordinator import connected_session_profile_ids, verified_system_profile_id, is_systemwide_on, is_connected, evaluate_state_on_startup
|
||||
from gui.v2.infrastructure.take_plumbing_action import take_action
|
||||
from gui.v2.actions.error_handlers import empty_filler
|
||||
from core.controllers.ProfileController import ProfileController
|
||||
|
|
@ -869,7 +869,7 @@ class MenuPage(Page):
|
|||
|
||||
# 1. Collect current state
|
||||
sys_on = is_systemwide_on()
|
||||
sys_id = connected_system_profile_id()
|
||||
sys_id = verified_system_profile_id()
|
||||
sess_ids = connected_session_profile_ids() or []
|
||||
|
||||
# 2. Set the global background
|
||||
|
|
|
|||
8
gui/v2/ui/presenters/main_presenters.py
Normal file
8
gui/v2/ui/presenters/main_presenters.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
|
||||
def disconnect_message(connected_profiles: list):
|
||||
profile_numbers = ', '.join(str(profile_id)
|
||||
for profile_id in connected_profiles)
|
||||
is_are = "is" if len(connected_profiles) == 1 else "are"
|
||||
return f'Profile{"" if len(connected_profiles) == 1 else "s"} {profile_numbers} {is_are} still connected.\nAll connected profiles will be disconnected on exit.\nDo you want to proceed?'
|
||||
|
||||
Loading…
Reference in a new issue