diff options
author | toofar <toofar@spalge.com> | 2024-01-28 19:24:24 +1300 |
---|---|---|
committer | toofar <toofar@spalge.com> | 2024-02-08 22:51:49 +1300 |
commit | 2bf20b5d1d5ddc70d8a13b3930f43e7beb54ffb5 (patch) | |
tree | 2774cd03bd18b5a8e93353309b481750d5125724 | |
parent | 89502baa203668c3e3d53be6185c31255a314d7d (diff) | |
download | qutebrowser-2bf20b5d1d5ddc70d8a13b3930f43e7beb54ffb5.tar.gz qutebrowser-2bf20b5d1d5ddc70d8a13b3930f43e7beb54ffb5.zip |
Support loading legacy format tree tab sessions.
In a2f98aa78b52 the session format for tree tab windows was changed to be
backwards compatible with the prior format. After that change we can load
non-tree tab windows fine but the browser crashes on startup when trying to
load session in the now-legacy tree tab session format.
Since people have been using the now-legacy session for years and there is no
way for them to easily migrate I figure we have a few options:
* provide a script to help them migrate
* handle the error encountered (`KeyError: 'tabs`) when trying to load a
legacy session better, eg give them a nice apologetic message
* support loading the old style of sessions
It didn't take much effort to support the prior format, so I propose we do
that. The data is all the same, we just changed it to nodes are childs of tabs
instead of the other way around.
-rw-r--r-- | qutebrowser/misc/sessions.py | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index 0ef4cf34b..d04f7f164 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -519,7 +519,7 @@ class SessionManager(QObject): except ValueError as e: raise SessionError(e) - def _load_tree(self, tabbed_browser, tree_data): + def _load_tree(self, tabbed_browser, tree_data, legacy=False): tree_keys = list(tree_data.keys()) if not tree_keys: return None @@ -536,8 +536,12 @@ class SessionManager(QObject): nonlocal tab_to_focus nonlocal index index += 1 - tab_data = tree_data[uid] - node_data = tab_data['treetab_node_data'] + if legacy: + node_data = tree_data[uid] + tab_data = node_data['tab'] + else: + tab_data = tree_data[uid] + node_data = tab_data['treetab_node_data'] children_uids = node_data['children'] if tab_data.get('active'): @@ -558,6 +562,32 @@ class SessionManager(QObject): return tab_to_focus + def _load_legacy_tree_tabs(self, win, tabbed_browser): + """Load the "legacy" tree session format. + + For a number of years (pull #4602) tree tabs used a session format + that wasn't backwards compatible with prior session formats. In that + tab data was a child of a tree node. Now it's been switched to the + other way around. This method (along with a conditional in + `_load_tree()`) handle loading the old format for early adopters who + have session they don't want to have to rebuild. + + Returns: + a. `(None, None)` if tree tabs where loaded, or + b. or a tuple of the tab index to focus and a flat list of tabs that + need loading if tree tabs is turned off. + """ + plain_tabs = None + tab_to_focus = None + tree_data = win.get('tree') + if tabbed_browser.is_treetabbedbrowser: + tab_to_focus = self._load_tree(tabbed_browser, tree_data, legacy=True) + tabbed_browser.widget.tree_tab_update() + else: + plain_tabs = [tree_data[i]['tab'] for i in tree_data if + tree_data[i]['tab']] + return tab_to_focus, plain_tabs + def _load_window(self, win): """Turn yaml data into windows.""" window = mainwindow.MainWindow(geometry=win['geometry'], @@ -566,7 +596,13 @@ class SessionManager(QObject): window=window.win_id) tab_to_focus = None - tabs = win['tabs'] + legacy_tree_loaded = False + if win.get('tree'): + tab_to_focus, tabs = self._load_legacy_tree_tabs(win, tabbed_browser) + legacy_tree_loaded = not tabs + else: + tabs = win['tabs'] + # restore a tab tree only if the session contains treetab # data and tree tabs are enabled. # Otherwise, restore tabs "flat" @@ -575,7 +611,7 @@ class SessionManager(QObject): if load_tree_tabs: tree_data = reconstruct_tree_data(win) self._load_tree(tabbed_browser, tree_data) - else: + elif not legacy_tree_loaded: for i, tab in enumerate(tabs): new_tab = tabbed_browser.tabopen(background=False) self._load_tab(new_tab, tab) |