Save session to load in state file.

Before, we always loaded the default session (if it existed) and then deleted
it. This was surprising as the default session was deleted even when another
session was loaded.

Now we don't delete it at all, and save the session to load in the state file.

See #523.
This commit is contained in:
Florian Bruhin 2015-03-24 07:12:35 +01:00
parent 1425d306bc
commit 1d1ac1ef6f
3 changed files with 45 additions and 15 deletions

View File

@ -205,12 +205,6 @@ class Application(QApplication):
objreg.register('cache', diskcache)
log.init.debug("Initializing completions...")
completionmodels.init()
if not session_manager.exists(self._args.session):
log.init.debug("Initializing main window...")
window = mainwindow.MainWindow()
if not self._args.nowindow:
window.show()
self.setActiveWindow(window)
def _init_icon(self):
"""Initialize the icon of qutebrowser."""
@ -266,7 +260,16 @@ class Application(QApplication):
except (configexc.Error, configparser.Error) as e:
message.error('current', "set: {} - {}".format(
e.__class__.__name__, e))
self._load_session(self._args.session)
session_manager = objreg.get('session-manager')
if not session_manager.did_load:
log.init.debug("Initializing main window...")
window = mainwindow.MainWindow()
if not self._args.nowindow:
window.show()
self.setActiveWindow(window)
self.process_pos_args(self._args.command)
self._open_startpage()
self._open_quickstart()
@ -275,17 +278,28 @@ class Application(QApplication):
"""Load the default session.
Args:
name: The name of the session to load.
name: The name of the session to load, or None to read state file.
"""
state_config = objreg.get('state-config')
if name is None:
try:
name = state_config['general']['session']
except KeyError:
# No session given as argument and none in the session file ->
# start without loading a session
return
session_manager = objreg.get('session-manager')
try:
session_manager.load(name)
except sessions.SessionNotFoundError:
message.error('current', "Session {} not found!".format(name))
except sessions.SessionError as e:
message.error('current', "Failed to load session {}: {}".format(
name, e))
try:
del state_config['general']['session']
except KeyError:
pass
except sessions.SessionError:
log.init.exception("Failed to load default session")
else:
session_manager.delete('default')
def _get_window(self, via_ipc, force_window=False, force_tab=False):
"""Helper function for process_pos_args to get a window id.
@ -794,11 +808,15 @@ class Application(QApplication):
self._shutting_down = True
log.destroy.debug("Shutting down with status {}, session {}..."
.format(status, session))
session_manager = objreg.get('session-manager')
if session is not None:
session_manager.save(session, last_window=last_window)
session_manager.save(session, last_window=last_window,
load_next_time=True)
elif config.get('general', 'save-session'):
session_manager.save('default', last_window=last_window)
session_manager.save('default', last_window=last_window,
load_next_time=True)
deferrer = False
for win_id in objreg.window_registry:
prompter = objreg.get('prompter', None, scope='window',

View File

@ -21,6 +21,7 @@
import os
import os.path
import configparser
from PyQt5.QtCore import pyqtSignal, QUrl, QObject, QPoint, QTimer
from PyQt5.QtWidgets import QApplication
@ -54,6 +55,7 @@ class SessionManager(QObject):
_base_path: The path to store sessions under.
_last_window_session: The session data of the last window which was
closed.
did_load: Set when a session was loaded.
Signals:
update_completion: Emitted when the session completion should get
@ -66,6 +68,7 @@ class SessionManager(QObject):
super().__init__(parent)
self._base_path = os.path.join(standarddir.data(), 'sessions')
self._last_window_session = None
self.did_load = False
if not os.path.exists(self._base_path):
os.mkdir(self._base_path)
@ -154,12 +157,13 @@ class SessionManager(QObject):
data['windows'].append(win_data)
return data
def save(self, name, last_window=False):
def save(self, name, last_window=False, load_next_time=False):
"""Save a named session.
Args:
last_window: If set, saves the saved self._last_window_session
instead of the currently open state.
load_next_time: If set, prepares this session to be load next time.
"""
path = self._get_session_path(name)
@ -178,6 +182,13 @@ class SessionManager(QObject):
raise SessionError(e)
else:
self.update_completion.emit()
if load_next_time:
state_config = objreg.get('state-config')
try:
state_config.add_section('general')
except configparser.DuplicateSectionError:
pass
state_config['general']['session'] = name
def save_last_window_session(self):
"""Temporarily save the session for the last closed window."""
@ -235,6 +246,7 @@ class SessionManager(QObject):
tabbed_browser.setCurrentIndex(tab_to_focus)
if win.get('active', False):
QTimer.singleShot(0, tabbed_browser.activateWindow)
self.did_load = True
def delete(self, name):
"""Delete a session."""

View File

@ -54,7 +54,7 @@ def get_argparser():
dest='temp_settings', default=[],
metavar=('SECTION', 'OPTION', 'VALUE'))
parser.add_argument('-r', '--restore', help="Restore a named session.",
dest='session', default='default')
dest='session')
debug = parser.add_argument_group('debug arguments')
debug.add_argument('-l', '--loglevel', dest='loglevel',