WebBrowser/Session/SessionManager.py

changeset 5780
79d06c98c5c9
parent 5779
b53fabc86f3c
child 5782
60874802161b
equal deleted inserted replaced
5779:b53fabc86f3c 5780:79d06c98c5c9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import os 12 import os
13 import json 13 import json
14 14
15 from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QTimer, QDir, QFile, \ 15 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, \
16 QFileInfo, QFileSystemWatcher 16 QFile, QFileInfo, QFileSystemWatcher, QByteArray
17 from PyQt5.QtWidgets import QMenu, QAction, QActionGroup, QApplication
18
19 from E5Gui import E5MessageBox
17 20
18 import Utilities 21 import Utilities
19 import Preferences 22 import Preferences
20 23
21 24
44 sessionsMetaDataChanged = pyqtSignal() 47 sessionsMetaDataChanged = pyqtSignal()
45 48
46 SwitchSession = 1 49 SwitchSession = 1
47 CloneSession = 2 50 CloneSession = 2
48 ReplaceSession = SwitchSession | 4 51 ReplaceSession = SwitchSession | 4
52 RestoreSession = 8
49 53
50 def __init__(self, parent=None): 54 def __init__(self, parent=None):
51 """ 55 """
52 Constructor 56 Constructor
53 57
177 # add window geometry 181 # add window geometry
178 geometry = window.saveGeometry() 182 geometry = window.saveGeometry()
179 data["WindowGeometry"] = bytes(geometry.toBase64()).decode("ascii") 183 data["WindowGeometry"] = bytes(geometry.toBase64()).decode("ascii")
180 184
181 sessionData["Windows"].append(data) 185 sessionData["Windows"].append(data)
182 else: 186
183 return 187 if sessionData["Windows"]:
184 188 sessionFile = open(sessionFileName, "w")
185 sessionFile = open(sessionFileName, "w") 189 json.dump(sessionData, sessionFile, indent=2)
186 json.dump(sessionData, sessionFile, indent=2) 190 sessionFile.close()
187 sessionFile.close() 191
192 def __readSessionFromFile(self, sessionFileName):
193 """
194 Private method to read the session data from a file.
195
196 @param sessionFileName file name of the session file
197 @type str
198 @return dictionary containing the session data
199 @rtype dict
200 """
201 try:
202 sessionFile = open(sessionFileName, "r")
203 sessionData = json.load(sessionFile)
204 sessionFile.close()
205 except (IOError, OSError):
206 sessionData = {}
207
208 return sessionData
188 209
189 def __backupSavedSession(self): 210 def __backupSavedSession(self):
190 """ 211 """
191 Private method to backup the most recently saved session. 212 Private method to backup the most recently saved session.
192 """ 213 """
237 """ 258 """
238 if self.__sessionMetaData: 259 if self.__sessionMetaData:
239 return 260 return
240 261
241 sessionFilesInfoList = QDir(self.getSessionsDirectory()).entryInfoList( 262 sessionFilesInfoList = QDir(self.getSessionsDirectory()).entryInfoList(
242 QDir.Files, QDir.Time) 263 ["*.json"], QDir.Files, QDir.Time)
243 for sessionFileInfo in sessionFilesInfoList: 264 for sessionFileInfo in sessionFilesInfoList:
244 data = SessionMetaData() 265 data = SessionMetaData()
245 data.name = sessionFileInfo.baseName() 266 data.name = sessionFileInfo.baseName()
246 data.filePath = sessionFileInfo.canonicalFilePath() 267 data.filePath = sessionFileInfo.canonicalFilePath()
247 268
272 """ 293 """
273 self.__sessionMetaData = [] 294 self.__sessionMetaData = []
274 295
275 self.sessionsMetaDataChanged.emit() 296 self.sessionsMetaDataChanged.emit()
276 297
277 def openSession(self, sessionFilePath="", flags=None): 298 @pyqtSlot()
278 # TODO: implement this 299 def aboutToShowSessionsMenu(self):
279 pass 300 """
301 Public slot to populate the sessions selection menu.
302 """
303 menu = self.sender()
304 if isinstance(menu, QMenu):
305 menu.clear()
306
307 actionGroup = QActionGroup(menu)
308 sessions = self.sessionMetaData(includeBackups=False)
309 for session in sessions:
310 act = menu.addAction(session.name)
311 act.setCheckable(True)
312 act.setChecked(session.isActive)
313 act.setData(session.filePath)
314 actionGroup.addAction(act)
315 act.triggered.connect(self.__sessionActTriggered)
316
317 @pyqtSlot()
318 def __sessionActTriggered(self):
319 """
320 Private slot to handle the menu selection of a session.
321 """
322 act = self.sender()
323 if isinstance(act, QAction):
324 path = act.data()
325 self.switchToSession(path)
326
327 def __openSession(self, sessionFilePath, flags=None):
328 """
329 Private method to open a session from a given session file.
330 """
331 if self.__isActive(sessionFilePath):
332 return
333
334 sessionData = self.__readSessionFromFile(sessionFilePath)
335 if not sessionData or not sessionData["Windows"]:
336 return
337
338 from WebBrowser.WebBrowserWindow import WebBrowserWindow
339 window = WebBrowserWindow.mainWindow()
340 if flags & SessionManager.SwitchSession:
341 # save the current session
342 self.writeCurrentSession(self.__lastActiveSession)
343
344 # create new window for the new session
345 window = window.newWindow(restoreSession=True)
346
347 # close all existing windows
348 for win in WebBrowserWindow.mainWindows():
349 if win is not window:
350 win.forceClose()
351
352 if not (flags & SessionManager.ReplaceSession):
353 self.__lastActiveSession = \
354 QFileInfo(sessionFilePath).canonicalFilePath()
355 self.__sessionMetaData = []
356
357 QApplication.setOverrideCursor(Qt.WaitCursor)
358 # restore session for first window
359 data = sessionData["Windows"].pop(0)
360 window.tabWidget().loadFromSessionData(data)
361 if "WindowGeometry" in data:
362 geometry = QByteArray.fromBase64(
363 data["WindowGeometry"].encode("ascii"))
364 window.restoreGeometry(geometry)
365 QApplication.processEvents()
366
367 # restore additional windows
368 for data in sessionData["Windows"]:
369 window = WebBrowserWindow.mainWindow()\
370 .newWindow(restoreSession=True)
371 window.tabWidget().loadFromSessionData(data)
372 if "WindowGeometry" in data:
373 geometry = QByteArray.fromBase64(
374 data["WindowGeometry"].encode("ascii"))
375 window.restoreGeometry(geometry)
376 QApplication.processEvents()
377 QApplication.restoreOverrideCursor()
280 378
281 def renameSession(self, sessionFilePath="", flags=None): 379 def renameSession(self, sessionFilePath="", flags=None):
282 # TODO: implement this 380 # TODO: implement this
283 pass 381 pass
284 382
285 def saveSession(self): 383 def saveSession(self):
286 # TODO: implement this 384 # TODO: implement this
287 pass 385 pass
288 386
289 def __replaceSession(self, sessionFilePath): 387 def replaceSession(self, sessionFilePath):
290 # TODO: implement this 388 """
291 pass 389 Public method to replace the current session with the given one.
292 390
293 def __switchToSession(self, sessionFilePath): 391 @param sessionFilePath file name of the session file to replace with
294 # TODO: implement this 392 @type str
295 pass 393 """
296 394 from WebBrowser.WebBrowserWindow import WebBrowserWindow
297 def __cloneSession(self, sessionFilePath): 395 res = E5MessageBox.yesNo(
298 # TODO: implement this 396 WebBrowserWindow.mainWindow(),
299 pass 397 self.tr("Restore Backup"),
300 398 self.tr("""Are you sure you want to replace current session?"""))
301 def __deleteSession(self, sessionFilePath): 399 if res:
302 # TODO: implement this 400 self.__openSession(sessionFilePath, SessionManager.ReplaceSession)
303 pass 401
304 402 def switchToSession(self, sessionFilePath):
305 def __newSession(self): 403 """
306 # TODO: implement this 404 Public method to switch the current session to the given one.
307 pass 405
308 406 @param sessionFilePath file name of the session file to switch to
309 def openSessionManagerDialog(self): 407 @type str
310 # TODO: implement this 408 """
311 pass 409 self.__openSession(sessionFilePath, SessionManager.SwitchSession)
410
411 def cloneSession(self, sessionFilePath):
412 # TODO: implement this
413 pass
414
415 def deleteSession(self, sessionFilePath):
416 # TODO: implement this
417 pass
418
419 def newSession(self):
420 # TODO: implement this
421 pass
422
423 def showSessionManagerDialog(self):
424 # TODO: implement this
425 pass
426 print("showSessionManagerDialog()")

eric ide

mercurial