diff --git a/gui/v2/actions/menu_coordinator.py b/gui/v2/actions/menu_coordinator.py index 431402b..d46ce86 100644 --- a/gui/v2/actions/menu_coordinator.py +++ b/gui/v2/actions/menu_coordinator.py @@ -1,5 +1,5 @@ # core -from core.infrastructure.state_management.global_state_controller import connected_system_profile_id, connected_session_profile_ids +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.controllers.SessionStateController import SessionStateController @@ -8,7 +8,7 @@ from core.observers.Notifications import Notifications, EventType 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.services.profiles.profile_connections import enable_profile, disable_profile +from core.services.profiles.profile_connections import enable_profile, disable_profile, destroy_profile from core.controllers.ProfileController import ProfileController from core.models.Configuration import Configuration, ConnectionChoice from core.services.subscriptions.subscriptions_by_protocol import get_profile_subscription, start_new_payment_cycle, wipe_subscription, evaluate_repeat_cycle @@ -351,8 +351,35 @@ def sync_now(ui_caller: Callable): connection_observer=connection_observer ) -# def finish(result: Result): -# print("goodbye") +def delete_profile_action(profile_id: int, ui_caller: Callable) -> Result: + """Disconnect if active, then delete the profile in the background thread.""" + + if is_connected(profile_id): + # first disconnect on main thread: + logger.info("Disconnecting first..") + disconnected = literally_disconnect(profile_id) + if not disconnected.valid: + error_msg = "Couldn't get to delete, because we can't even disconnect." + logger.error(error_msg) + return disconnected.wrap(error_msg) + + got_profile = get_profile(profile_id) + if not got_profile.valid: + return got_profile.wrap("Failed to get the profile.") + profile = got_profile.data + + # threading for the delete: + logger.info("Disconnect done, now threading to destroy the files..") + take_action( + action=destroy_profile, + ui_caller=ui_caller, + profile=profile, + profile_observer=profile_observer, + ticket_observer=ticket_observer, + connection_observer=connection_observer + ) + return Result(valid=True) # this doesn't mean it deleted, just that it launched. + # atexit.register(disconnect_active(ui_caller=finish)) diff --git a/gui/v2/actions/menu_profiles.py b/gui/v2/actions/menu_profiles.py index c6571c8..2e0994d 100755 --- a/gui/v2/actions/menu_profiles.py +++ b/gui/v2/actions/menu_profiles.py @@ -335,7 +335,8 @@ def profile_status_message(profile: BaseProfile, is_enabled: bool, idle: bool = if profile_date is None: return "Offline. No subscription found." - expiration_date = profile_date.replace(tzinfo=timezone.utc) + expiration_date = profile_date + #.replace(tzinfo=timezone.utc) time_left = expiration_date - datetime.now(timezone.utc) days_left = time_left.days hours_left, _ = divmod(time_left.seconds, 3600) diff --git a/gui/v2/ui/pages/settings_page.py b/gui/v2/ui/pages/settings_page.py index 749c1e8..9755de8 100755 --- a/gui/v2/ui/pages/settings_page.py +++ b/gui/v2/ui/pages/settings_page.py @@ -24,6 +24,8 @@ from core.controllers.tickets.UseTicketController import ( get_unused_tickets, modify_random_tickets_setting, ) + +from core.models.Result import Result from core.models.session.SessionProfile import SessionProfile from core.models.system.SystemProfile import SystemProfile from core.Errors import ( @@ -54,7 +56,7 @@ from gui.v2.ui.widgets.terminal_widget import TerminalWidget from gui.v2.workers.page_data_worker import PageDataWorker from gui.v2.workers.ticketing_worker_thread import TicketingWorkerThread from gui.v2.workers.worker_thread import WorkerThread - +from gui.v2.actions.menu_coordinator import delete_profile_action class Settings(Page): def __init__(self, page_stack, main_window, parent=None, prepared=None): @@ -778,24 +780,36 @@ class Settings(Page): def handle_delete_confirmation(self, confirmed): if confirmed: - self.update_status.update_status(f'Deleting profile...') - self.worker_thread = WorkerThread('DESTROY_PROFILE', profile_data={ - 'id': self.selected_profile_id}) - self.worker_thread.text_output.connect(self.delete_status_update) - self.worker_thread.start() + profile_id = self.selected_profile_id + self.update_status.update_status(f'Deleting profile {profile_id}...') - def delete_status_update(self, message): - self.update_status.update_status(message) - if not message.startswith('Could not delete'): - menu_page = self.custom_window.navigator.get_cached("menu") - if menu_page is not None and hasattr(menu_page, 'clear_profile_details'): - menu_page.clear_profile_details() - if menu_page is not None and hasattr(menu_page, 'refresh_menu_buttons'): - menu_page.refresh_menu_buttons() - self.content_layout.removeWidget(self.delete_page) - self.delete_page = self.create_delete_page() - self.content_layout.addWidget(self.delete_page) - self.content_layout.setCurrentWidget(self.delete_page) + started_delete = delete_profile_action(profile_id=profile_id, ui_caller=self.delete_status_update) + if not started_delete.valid: + message = started_delete.message + self.update_status.update_status(message) + + def delete_status_update(self, result: Result): + """Recieve the result of the deletion.""" + if not result.valid: + self.update_status.update_status(result.message) + return + + self.update_status.update_status("Deleted") + + # # if not message.startswith('Could not delete'): + menu_page = self.custom_window.navigator.get_cached("menu") + if menu_page is not None and hasattr(menu_page, 'clear_profile_details'): + menu_page.clear_profile_details() + if menu_page is not None and hasattr(menu_page, 'refresh_menu_buttons'): + menu_page.refresh_menu_buttons() + + # back to the main page: + self.custom_window.navigator.navigate("menu") + + # self.content_layout.removeWidget(self.delete_page) + # self.delete_page = self.create_delete_page() + # self.content_layout.addWidget(self.delete_page) + # self.content_layout.setCurrentWidget(self.delete_page) def get_checkbox_style(self) -> str: return checkbox_style(self.font_style)