diff -r b53fabc86f3c -r 79d06c98c5c9 WebBrowser/Session/SessionManager.py --- a/WebBrowser/Session/SessionManager.py Fri Jun 30 19:58:09 2017 +0200 +++ b/WebBrowser/Session/SessionManager.py Sat Jul 01 19:14:01 2017 +0200 @@ -12,8 +12,11 @@ import os import json -from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QTimer, QDir, QFile, \ - QFileInfo, QFileSystemWatcher +from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, \ + QFile, QFileInfo, QFileSystemWatcher, QByteArray +from PyQt5.QtWidgets import QMenu, QAction, QActionGroup, QApplication + +from E5Gui import E5MessageBox import Utilities import Preferences @@ -46,6 +49,7 @@ SwitchSession = 1 CloneSession = 2 ReplaceSession = SwitchSession | 4 + RestoreSession = 8 def __init__(self, parent=None): """ @@ -179,12 +183,29 @@ data["WindowGeometry"] = bytes(geometry.toBase64()).decode("ascii") sessionData["Windows"].append(data) - else: - return + + if sessionData["Windows"]: + sessionFile = open(sessionFileName, "w") + json.dump(sessionData, sessionFile, indent=2) + sessionFile.close() + + def __readSessionFromFile(self, sessionFileName): + """ + Private method to read the session data from a file. - sessionFile = open(sessionFileName, "w") - json.dump(sessionData, sessionFile, indent=2) - sessionFile.close() + @param sessionFileName file name of the session file + @type str + @return dictionary containing the session data + @rtype dict + """ + try: + sessionFile = open(sessionFileName, "r") + sessionData = json.load(sessionFile) + sessionFile.close() + except (IOError, OSError): + sessionData = {} + + return sessionData def __backupSavedSession(self): """ @@ -239,7 +260,7 @@ return sessionFilesInfoList = QDir(self.getSessionsDirectory()).entryInfoList( - QDir.Files, QDir.Time) + ["*.json"], QDir.Files, QDir.Time) for sessionFileInfo in sessionFilesInfoList: data = SessionMetaData() data.name = sessionFileInfo.baseName() @@ -274,9 +295,86 @@ self.sessionsMetaDataChanged.emit() - def openSession(self, sessionFilePath="", flags=None): - # TODO: implement this - pass + @pyqtSlot() + def aboutToShowSessionsMenu(self): + """ + Public slot to populate the sessions selection menu. + """ + menu = self.sender() + if isinstance(menu, QMenu): + menu.clear() + + actionGroup = QActionGroup(menu) + sessions = self.sessionMetaData(includeBackups=False) + for session in sessions: + act = menu.addAction(session.name) + act.setCheckable(True) + act.setChecked(session.isActive) + act.setData(session.filePath) + actionGroup.addAction(act) + act.triggered.connect(self.__sessionActTriggered) + + @pyqtSlot() + def __sessionActTriggered(self): + """ + Private slot to handle the menu selection of a session. + """ + act = self.sender() + if isinstance(act, QAction): + path = act.data() + self.switchToSession(path) + + def __openSession(self, sessionFilePath, flags=None): + """ + Private method to open a session from a given session file. + """ + if self.__isActive(sessionFilePath): + return + + sessionData = self.__readSessionFromFile(sessionFilePath) + if not sessionData or not sessionData["Windows"]: + return + + from WebBrowser.WebBrowserWindow import WebBrowserWindow + window = WebBrowserWindow.mainWindow() + if flags & SessionManager.SwitchSession: + # save the current session + self.writeCurrentSession(self.__lastActiveSession) + + # create new window for the new session + window = window.newWindow(restoreSession=True) + + # close all existing windows + for win in WebBrowserWindow.mainWindows(): + if win is not window: + win.forceClose() + + if not (flags & SessionManager.ReplaceSession): + self.__lastActiveSession = \ + QFileInfo(sessionFilePath).canonicalFilePath() + self.__sessionMetaData = [] + + QApplication.setOverrideCursor(Qt.WaitCursor) + # restore session for first window + data = sessionData["Windows"].pop(0) + window.tabWidget().loadFromSessionData(data) + if "WindowGeometry" in data: + geometry = QByteArray.fromBase64( + data["WindowGeometry"].encode("ascii")) + window.restoreGeometry(geometry) + QApplication.processEvents() + + # restore additional windows + for data in sessionData["Windows"]: + window = WebBrowserWindow.mainWindow()\ + .newWindow(restoreSession=True) + window.tabWidget().loadFromSessionData(data) + if "WindowGeometry" in data: + geometry = QByteArray.fromBase64( + data["WindowGeometry"].encode("ascii")) + window.restoreGeometry(geometry) + QApplication.processEvents() + QApplication.restoreOverrideCursor() def renameSession(self, sessionFilePath="", flags=None): # TODO: implement this @@ -286,26 +384,43 @@ # TODO: implement this pass - def __replaceSession(self, sessionFilePath): - # TODO: implement this - pass + def replaceSession(self, sessionFilePath): + """ + Public method to replace the current session with the given one. + + @param sessionFilePath file name of the session file to replace with + @type str + """ + from WebBrowser.WebBrowserWindow import WebBrowserWindow + res = E5MessageBox.yesNo( + WebBrowserWindow.mainWindow(), + self.tr("Restore Backup"), + self.tr("""Are you sure you want to replace current session?""")) + if res: + self.__openSession(sessionFilePath, SessionManager.ReplaceSession) - def __switchToSession(self, sessionFilePath): - # TODO: implement this - pass + def switchToSession(self, sessionFilePath): + """ + Public method to switch the current session to the given one. + + @param sessionFilePath file name of the session file to switch to + @type str + """ + self.__openSession(sessionFilePath, SessionManager.SwitchSession) - def __cloneSession(self, sessionFilePath): + def cloneSession(self, sessionFilePath): # TODO: implement this pass - def __deleteSession(self, sessionFilePath): + def deleteSession(self, sessionFilePath): # TODO: implement this pass - def __newSession(self): + def newSession(self): # TODO: implement this pass - def openSessionManagerDialog(self): + def showSessionManagerDialog(self): # TODO: implement this pass + print("showSessionManagerDialog()")