diff --git a/gui/v2/actions/error_handlers.py b/gui/v2/actions/error_handlers.py index a3e2634..e056f8a 100644 --- a/gui/v2/actions/error_handlers.py +++ b/gui/v2/actions/error_handlers.py @@ -21,16 +21,6 @@ def post_state_evaluation(result: Result): poll_state.start_polling() - - # if not result.valid and result.error_type == ResultError.ZOMBIE_SESSION and result.data: - # active_session_profiles = result.data - # logger.info("Removing Zombie Session profiles on the main thread.") - # for each_profile in active_session_profiles: - # logger.info(f"Removing this profile {active_session_profiles} from session since it's a fresh restart.") - # SessionStateController.remove(each_profile.id) - - - @register_action(ResultError.ZOMBIE_SYSTEM) def evaluate_bad_state(): """Start core's evaluation of state with a GUI observer.""" @@ -64,3 +54,5 @@ def navigate_to_singbox_setup() -> Result: connection_observer=connection_observer ) return Result(valid=False, error_type=ResultError.NAVIGATE_TO, navigate_to="networking_setup") + + diff --git a/gui/v2/actions/menu_coordinator.py b/gui/v2/actions/menu_coordinator.py index b75552f..cb5e66b 100644 --- a/gui/v2/actions/menu_coordinator.py +++ b/gui/v2/actions/menu_coordinator.py @@ -1,4 +1,5 @@ # core +from core.models.manage.database_recovery import wipe_and_kill_entire_app from core.infrastructure.state_management.global_state_controller import connected_system_profile_id, connected_session_profile_ids, is_connected from core.observers.Notifications import Notifications, EventType from core.models.enum_models import ProtocolChoice @@ -37,9 +38,11 @@ from gui.v2.infrastructure.setup_observers import ( from gui.v2.actions.error_handlers import navigate_to_new_sub_page # generic +from typing import Optional from typing import Callable from functools import partial import atexit +import sys @type_checker def get_profile(profile_id: int): @@ -314,7 +317,7 @@ def disconnect_many_profiles(profile_ids: list) -> Result: return Result(valid=False, data=failed_list) -def disconnect_active(ui_caller: Callable) -> Result: +def disconnect_active(ui_caller: Optional[Callable] = None) -> Result: """Disconnects active session profiles first, then system.""" # SESSION @@ -342,8 +345,10 @@ def disconnect_active(ui_caller: Callable) -> Result: system_disconnected.message = error_msg return system_disconnected - ui_caller(Result(valid=True)) - + if ui_caller: + ui_caller(Result(valid=True)) + else: + return Result(valid=True) def sync_now(ui_caller: Callable): take_action( @@ -398,4 +403,18 @@ def edit_profile( new_value=new_value ) + +@register_action(ResultError.DELETE_DB) +def shut_down_and_wipe() -> Result: + """Attempt a graceful close of profiles, then wipe the database regardless.""" + logger.info("Shutting down profiles, and then wiping.") + try: + disconnect_active(ui_caller=None) + return wipe_and_kill_entire_app() + except Exception as e: + logger.error(f"Wiping anyway despite {str(e)}") + return wipe_and_kill_entire_app() + + + # atexit.register(disconnect_active(ui_caller=finish)) diff --git a/gui/v2/actions/menu_profiles.py b/gui/v2/actions/menu_profiles.py index 2e0994d..4704663 100755 --- a/gui/v2/actions/menu_profiles.py +++ b/gui/v2/actions/menu_profiles.py @@ -44,7 +44,10 @@ def load_profile(profile_id: int, on_error: StatusCallback) -> BaseProfile | Non def load_profiles(on_error: StatusCallback) -> dict[int, BaseProfile]: """Load all profiles here so list rendering is not responsible for controller failures.""" try: - return ProfileController.get_all() + profile_result = ProfileController.get_all() + if not profile_result.valid: + return profile_result + except Exception as error: _report_load_error( error, @@ -175,11 +178,16 @@ def _enabled_profile_kind(profile: BaseProfile, enabled: bool | None = None) -> def load_menu_profiles( - gui_config_file: str | None, on_error: StatusCallback, - selected_id: int | None = None, state_change: Mapping[str, Any] | None = None, + profiles: dict, + gui_config_file: str | None, + on_error: StatusCallback, + selected_id: int | None = None, + state_change: Mapping[str, Any] | None = None, ) -> dict[str, Any]: """Provide one menu-loading action so the page does not separately fetch, order, and classify profiles.""" - profiles = load_profiles(on_error) + + # profiles = load_profiles(on_error) + state_change = state_change or {} changed_id = state_change.get("profile_id") enabled_kinds = { diff --git a/gui/v2/actions/profile_actions.py b/gui/v2/actions/profile_actions.py index 3978ed7..417bef5 100644 --- a/gui/v2/actions/profile_actions.py +++ b/gui/v2/actions/profile_actions.py @@ -24,3 +24,9 @@ def edit_profile(profile_id: int, potential_data: dict, ui_caller: Callable): profile_id=profile_id, new_choices=potential_data ) + +def get_all_profiles(ui_caller: Callable): + take_action( + action=ProfileController.get_all, + ui_caller=ui_caller + ) diff --git a/gui/v2/actions/settings_data.py b/gui/v2/actions/settings_data.py index 27f697d..256610f 100755 --- a/gui/v2/actions/settings_data.py +++ b/gui/v2/actions/settings_data.py @@ -55,7 +55,10 @@ def read_bwrap_enabled(): def prepare(gui_config_file): try: - profiles = ProfileController.get_all() + profiles_result = ProfileController.get_all() + if not profiles_result.valid: + print(f"Fetch of all profiles failed: {profiles_result.message}") + profiles = profiles_result.data except Exception: profiles = {} profile_order = normalize_profile_order(gui_config_file, profiles.keys()) diff --git a/gui/v2/ui/pages/menu_page.py b/gui/v2/ui/pages/menu_page.py index 7ff9b06..5f717bc 100755 --- a/gui/v2/ui/pages/menu_page.py +++ b/gui/v2/ui/pages/menu_page.py @@ -3,6 +3,7 @@ from core.infrastructure.global_state_coordinator import evaluate_state_on_start from core.services.networking.general_connection_tools.disconnect import kill_profile_from_grave from gui.v2.infrastructure.take_plumbing_action import take_action from gui.v2.actions.error_handlers import post_state_evaluation +from gui.v2.actions.profile_actions import get_all_profiles from core.controllers.ProfileController import ProfileController from core.models.BaseProfile import ProfileType @@ -175,6 +176,12 @@ class MenuPage(Page): connected = is_connected(profile_id) p_type = ProfileController.what_type_is_profile(profile_id) + if p_type is None: + error_msg = "Can't create/modify buttons with a blank profile type" + print(error_msg) + self.update_status.update_status(error_msg) + return + # if it's connected, then it can't be edited. self.boton_edit.setEnabled(not connected) @@ -228,11 +235,23 @@ class MenuPage(Page): @mainthread def refresh_menu_buttons(self, state_change=None): - print("refresh_menu_buttons triggered") + print("refresh_menu_buttons triggered. First, getting all profiles..") + get_all_profiles(ui_caller=self.actually_load_profiles) + + def actually_load_profiles(self, result: Result): + if not result.valid: + print("Result is invalid, but core handled the error on it's own.") + return + + profiles = result.data + print("Got the data. Loading all menu profiles.") menu_data = load_menu_profiles( - getattr(self.update_status, 'gui_config_file', None), - self.update_status.update_status, self.reverse_id, state_change, + profiles=profiles, + gui_config_file=getattr(self.update_status, 'gui_config_file', None), + on_error=self.update_status.update_status, + selected_id=self.reverse_id, + state_change=None, # legacy variable to be refactored out ) self.profiles_data = menu_data["display_profiles"] self.profile_info = self.profiles_data diff --git a/gui/v2/ui/popups/error_popup.py b/gui/v2/ui/popups/error_popup.py index 2583171..f94ae67 100644 --- a/gui/v2/ui/popups/error_popup.py +++ b/gui/v2/ui/popups/error_popup.py @@ -75,7 +75,10 @@ class ErrorPopup(QDialog): ok_btn = QPushButton("✓ OK") ok_btn.setStyleSheet(CORPORATE_PUNK_DYING_ROT) - ok_btn.clicked.connect(self.accept) # Closes the dialog + if self.end_app: + ok_btn.clicked.connect(self._terminate_app) + else: + ok_btn.clicked.connect(self.accept) main_layout.addWidget(ok_btn) self.setLayout(main_layout)