Thu, 29 Jun 2017 19:21:52 +0200
Continued implementing session support for the new web browser.
# -*- coding: utf-8 -*- # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the session manager. """ from __future__ import unicode_literals import os import json from PyQt5.QtCore import pyqtSlot, QObject, QTimer, QDir import Utilities import Preferences class SessionManager(QObject): """ Class implementing the session manager. """ def __init__(self, parent=None): """ Constructor @param parent reference to the parent object @type QObject """ super(SessionManager, self).__init__(parent) sessionsDir = QDir(self.getSessionsDirectory()) if not sessionsDir.exists(): sessionsDir.mkpath(self.getSessionsDirectory()) self.__autoSaveTimer = QTimer() self.__autoSaveTimer.setSingleShot(True) self.__autoSaveTimer.timeout.connect(self.__autoSaveSession) self.__initSessionSaveTimer() def preferencesChanged(self): """ Public slot to react upon changes of the settings. """ self.__initSessionSaveTimer() # TODO: implement this def getSessionsDirectory(self): """ Public method to get the directory sessions are stored in. @return name of the sessions directory @rtype str """ return os.path.join(Utilities.getConfigDir(), "web_browser", "sessions") def defaultSessionFile(self): """ Public method to get the name of the default session file. @return name of the default session file @rtype str """ return os.path.join(self.getSessionsDirectory(), "session.json") def __initSessionSaveTimer(self): """ Private slot to initialize the auto save timer. """ self.__autoSaveInterval = Preferences.getWebBrowser( "SessionAutoSaveInterval") * 1000 if Preferences.getWebBrowser("SessionAutoSave"): if not self.__autoSaveTimer.isActive(): self.__autoSaveTimer.start(self.__autoSaveInterval) else: self.__autoSaveTimer.stop() @pyqtSlot() def __autoSaveSession(self): """ Private slot to save the current session state. """ from WebBrowser.WebBrowserWindow import WebBrowserWindow if not WebBrowserWindow.isPrivate(): self.writeCurrentSession(self.defaultSessionFile()) self.__autoSaveTimer.start(self.__autoSaveInterval) def writeCurrentSession(self, sessionFileName): """ Public method to write the current session to the given file name. @param sessionFileName file name of the session @type str """ from WebBrowser.WebBrowserWindow import WebBrowserWindow sessionData = {"Windows": []} for window in WebBrowserWindow.mainWindows(): data = window.tabWidget().getSessionData() # add window geometry geometry = window.saveGeometry() data["WindowGeometry"] = bytes(geometry.toBase64()).decode("ascii") sessionData["Windows"].append(data) sessionFile = open(sessionFileName, "w") json.dump(sessionData, sessionFile, indent=2) sessionFile.close()