diff --git a/dzgui/init/migrate.py b/dzgui/init/migrate.py index 97c5587..2a5c0bc 100644 --- a/dzgui/init/migrate.py +++ b/dzgui/init/migrate.py @@ -1,3 +1,4 @@ +import json import shutil from pathlib import Path @@ -7,11 +8,10 @@ from dzgui.config.convert import rc2json from dzgui.util._json import read_json, write_json -def migrate_legacy_conf(config: Path) -> None: +def migrate_legacy_conf(config: Path) -> Any: old_conf = Path.home() / LEGACY_CONFIG_PATH j = rc2json(old_conf) - config.parent.mkdir(parents=True, exist_ok=True) - config.write_text(j) + return json.loads(j) def has_new_config(config: Path) -> bool: diff --git a/dzgui/views/dialogs/wizard.py b/dzgui/views/dialogs/wizard.py index 033f7a1..1326917 100644 --- a/dzgui/views/dialogs/wizard.py +++ b/dzgui/views/dialogs/wizard.py @@ -17,9 +17,7 @@ from dzgui.const.constants import ( ) from dzgui.const.boilerplate import config_boilerplate from dzgui.const.endpoints import STEAM_API_SETUP -from dzgui.const.enum import Preferences from dzgui.config import freedesktop -from dzgui.config.query import lookup from dzgui.init.migrate import migrate_legacy_conf from dzgui.managers.threading import call_on_thread, StoredFunc, ThreadingManager from dzgui.strings import wizard @@ -305,6 +303,7 @@ class ConfigMigrationPage(EnumeratedWizardPage): ) self.migrated = False + self.migrated_conf: dict[str, Any] = {} self.config = config self.page_type = Gtk.AssistantPageType.INTRO @@ -344,8 +343,7 @@ class ConfigMigrationPage(EnumeratedWizardPage): def _on_import_clicked(self, button: Gtk.Button) -> None: self.grid.set_sensitive(False) try: - # TODO: this could be deferred to the final page (prevents accidental destruction of dialog via ESC) - migrate_legacy_conf(self.config) + self.migrated_conf = migrate_legacy_conf(self.config) self.migrated = True self.success_box.set_visible(True) except Exception: @@ -444,13 +442,13 @@ class Assistant(Gtk.Assistant): self.setup_complete = False - self.page1 = IntroductionPage() - self.page2 = ConfigMigrationPage(XDG.config) - self.page3 = SteamPathPage() - self.page4 = SteamValidationPage() - self.page5 = PreferencesPage() - self.page6 = ShortcutCreationPage(XDG.shortcut) - self.page7 = CompletionPage() + self.page_intro = IntroductionPage() + self.page_migration = ConfigMigrationPage(XDG.config) + self.page_paths = SteamPathPage() + self.page_api = SteamValidationPage() + self.page_prefs = PreferencesPage() + self.page_shortcuts = ShortcutCreationPage(XDG.shortcut) + self.page_completion = CompletionPage() self.set_forward_page_func(self._advance_page) @@ -461,13 +459,13 @@ class Assistant(Gtk.Assistant): legacy_path = Path.home().joinpath(LEGACY_CONFIG_PATH) self.has_legacy_config = legacy_path.is_file() for page in ( - self.page1, - self.page2, - self.page3, - self.page4, - self.page5, - self.page6, - self.page7, + self.page_intro, + self.page_migration, + self.page_paths, + self.page_api, + self.page_prefs, + self.page_shortcuts, + self.page_completion, ): # NOTE: skip config migration page if no legacy config file if ( @@ -486,8 +484,17 @@ class Assistant(Gtk.Assistant): self.show_all() load_css() - def write_config(self) -> None: - write_json(self.config_values, self.config_path) + def write_config(self, config: dict[str, Any]) -> None: + write_json(config, self.config_path) + + def get_final_page(self) -> int: + pages = self.get_n_pages() + # TODO: might be better to check if path is writeable + # and selectively block certain shortcut options + if self.is_binary: + return pages - 2 + else: + return pages - 1 def _advance_page(self, index: int) -> int: page = self.get_nth_page(index) @@ -496,23 +503,25 @@ class Assistant(Gtk.Assistant): pass case ConfigMigrationPage(): if page.is_migrated(): - steam_path = lookup(self.config_path, Preferences.DEFAULT) - self.page6.set_steam_path(steam_path) - offset = 1 if not self.is_binary else 2 + sp = page.migrated_conf["default_steam_path"] + self.page_shortcuts.set_steam_path(sp) + self.write_config(self.page_migration.migrated_conf) self.setup_complete = True - return self.get_n_pages() - offset + return self.get_final_page() case SteamPathPage(): self.config_values["default_steam_path"] = page.get_path_from_radio() case SteamValidationPage(): self.config_values["steam_api"] = page.get_api_key() case PreferencesPage(): # NOTE: collects config values before advancing to last page - name, use_miles, client = self.page5.get_prefs() + name, use_miles, client = self.page_prefs.get_prefs() self.config_values["name"] = name self.config_values["use_miles"] = use_miles self.config_values["client"] = client - self.write_config() - self.page6.set_steam_path(self.config_values["default_steam_path"]) + self.write_config(self.config_values) + self.page_shortcuts.set_steam_path( + self.config_values["default_steam_path"] + ) self.setup_complete = True case ShortcutCreationPage(): page.create_shortcuts()