From 9805b43c85ee244d250abbdf16bd37a2841ee8d9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 30 Apr 2017 00:22:50 +0200 Subject: [PATCH] Handle private browsing in sessions --- qutebrowser/misc/sessions.py | 24 +++++++++++++++++------- tests/end2end/fixtures/quteprocess.py | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index 6a096c06a..9259f850b 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -213,7 +213,7 @@ class SessionManager(QObject): data['history'].append(item_data) return data - def _save_all(self, *, only_window=None): + def _save_all(self, *, only_window=None, with_private=False): """Get a dict with data for all windows/tabs.""" data = {'windows': []} if only_window is not None: @@ -231,12 +231,17 @@ class SessionManager(QObject): if sip.isdeleted(main_window): continue + if tabbed_browser.private and not with_private: + continue + win_data = {} active_window = QApplication.instance().activeWindow() if getattr(active_window, 'win_id', None) == win_id: win_data['active'] = True win_data['geometry'] = bytes(main_window.saveGeometry()) win_data['tabs'] = [] + if tabbed_browser.private: + win_data['private'] = True for i, tab in enumerate(tabbed_browser.widgets()): active = i == tabbed_browser.currentIndex() win_data['tabs'].append(self._save_tab(tab, active)) @@ -260,7 +265,7 @@ class SessionManager(QObject): return name def save(self, name, last_window=False, load_next_time=False, - only_window=None): + only_window=None, with_private=False): """Save a named session. Args: @@ -270,6 +275,7 @@ class SessionManager(QObject): instead of the currently open state. load_next_time: If set, prepares this session to be load next time. only_window: If set, only tabs in the specified window is saved. + with_private: Include private windows. Return: The name of the saved session. @@ -284,7 +290,8 @@ class SessionManager(QObject): log.sessions.error("last_window_session is None while saving!") return else: - data = self._save_all(only_window=only_window) + data = self._save_all(only_window=only_window, + with_private=with_private) log.sessions.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: @@ -380,7 +387,7 @@ class SessionManager(QObject): log.sessions.debug("Loading session {} from {}...".format(name, path)) for win in data['windows']: window = mainwindow.MainWindow(geometry=win['geometry'], - private=win.get('private', False)) + private=win.get('private', None)) window.show() tabbed_browser = objreg.get('tabbed-browser', scope='window', window=window.win_id) @@ -445,7 +452,8 @@ class SessionManager(QObject): @cmdutils.argument('name', completion=usertypes.Completion.sessions) @cmdutils.argument('win_id', win_id=True) def session_save(self, name: str = default, current=False, quiet=False, - force=False, only_active_window=False, win_id=None): + force=False, only_active_window=False, with_private=False, + win_id=None): """Save a session. Args: @@ -455,6 +463,7 @@ class SessionManager(QObject): quiet: Don't show confirmation message. force: Force saving internal sessions (starting with an underline). only_active_window: Saves only tabs of the currently active window. + with_private: Include private windows. """ if name is not default and name.startswith('_') and not force: raise cmdexc.CommandError("{} is an internal session, use --force " @@ -466,9 +475,10 @@ class SessionManager(QObject): assert not name.startswith('_') try: if only_active_window: - name = self.save(name, only_window=win_id) + name = self.save(name, only_window=win_id, + with_private=with_private) else: - name = self.save(name) + name = self.save(name, with_private=with_private) except SessionError as e: raise cmdexc.CommandError("Error while saving session: {}" .format(e)) diff --git a/tests/end2end/fixtures/quteprocess.py b/tests/end2end/fixtures/quteprocess.py index 040110e77..992714095 100644 --- a/tests/end2end/fixtures/quteprocess.py +++ b/tests/end2end/fixtures/quteprocess.py @@ -576,7 +576,7 @@ class QuteProc(testprocess.Process): """Save the session and get the parsed session data.""" with tempfile.TemporaryDirectory() as tmpdir: session = os.path.join(tmpdir, 'session.yml') - self.send_cmd(':session-save "{}"'.format(session)) + self.send_cmd(':session-save --with-private "{}"'.format(session)) self.wait_for(category='message', loglevel=logging.INFO, message='Saved session {}.'.format(session)) with open(session, encoding='utf-8') as f: