11 |
11 |
12 import os |
12 import os |
13 import json |
13 import json |
14 |
14 |
15 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, \ |
15 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, \ |
16 QFile, QFileInfo, QFileSystemWatcher, QByteArray |
16 QFile, QFileInfo, QFileSystemWatcher, QByteArray, QDateTime |
17 from PyQt5.QtWidgets import QMenu, QAction, QActionGroup, QApplication |
17 from PyQt5.QtWidgets import QMenu, QAction, QActionGroup, QApplication, \ |
|
18 QInputDialog, QLineEdit |
18 |
19 |
19 from E5Gui import E5MessageBox |
20 from E5Gui import E5MessageBox |
20 |
21 |
21 import Utilities |
22 import Utilities |
22 import Preferences |
23 import Preferences |
259 if self.__sessionMetaData: |
260 if self.__sessionMetaData: |
260 return |
261 return |
261 |
262 |
262 sessionFilesInfoList = QDir(self.getSessionsDirectory()).entryInfoList( |
263 sessionFilesInfoList = QDir(self.getSessionsDirectory()).entryInfoList( |
263 ["*.json"], QDir.Files, QDir.Time) |
264 ["*.json"], QDir.Files, QDir.Time) |
|
265 |
264 for sessionFileInfo in sessionFilesInfoList: |
266 for sessionFileInfo in sessionFilesInfoList: |
|
267 sessionData = self.__readSessionFromFile( |
|
268 sessionFileInfo.absoluteFilePath()) |
|
269 if not sessionData or not sessionData["Windows"]: |
|
270 continue |
|
271 |
265 data = SessionMetaData() |
272 data = SessionMetaData() |
266 data.name = sessionFileInfo.baseName() |
273 data.name = sessionFileInfo.baseName() |
267 data.filePath = sessionFileInfo.canonicalFilePath() |
274 data.filePath = sessionFileInfo.canonicalFilePath() |
268 |
275 |
269 if sessionFileInfo == QFileInfo(self.defaultSessionFile()): |
276 if sessionFileInfo == QFileInfo(self.defaultSessionFile()): |
322 act = self.sender() |
333 act = self.sender() |
323 if isinstance(act, QAction): |
334 if isinstance(act, QAction): |
324 path = act.data() |
335 path = act.data() |
325 self.switchToSession(path) |
336 self.switchToSession(path) |
326 |
337 |
327 def __openSession(self, sessionFilePath, flags=None): |
338 def __openSession(self, sessionFilePath, flags=0): |
328 """ |
339 """ |
329 Private method to open a session from a given session file. |
340 Private method to open a session from a given session file. |
|
341 |
|
342 @param sessionFilePath name of the session file to get session from |
|
343 @type str |
|
344 @param flags flags determining the open mode |
|
345 @type int |
330 """ |
346 """ |
331 if self.__isActive(sessionFilePath): |
347 if self.__isActive(sessionFilePath): |
332 return |
348 return |
333 |
349 |
334 sessionData = self.__readSessionFromFile(sessionFilePath) |
350 sessionData = self.__readSessionFromFile(sessionFilePath) |
335 if not sessionData or not sessionData["Windows"]: |
351 if not sessionData or not sessionData["Windows"]: |
336 return |
352 return |
337 |
353 |
338 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
354 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
339 window = WebBrowserWindow.mainWindow() |
355 window = WebBrowserWindow.mainWindow() |
340 if flags & SessionManager.SwitchSession: |
356 |
|
357 if ((flags & SessionManager.SwitchSession) == |
|
358 SessionManager.SwitchSession): |
341 # save the current session |
359 # save the current session |
342 self.writeCurrentSession(self.__lastActiveSession) |
360 self.writeCurrentSession(self.__lastActiveSession) |
343 |
361 |
344 # create new window for the new session |
362 # create new window for the new session |
345 window = window.newWindow(restoreSession=True) |
363 window = window.newWindow(restoreSession=True) |
374 data["WindowGeometry"].encode("ascii")) |
393 data["WindowGeometry"].encode("ascii")) |
375 window.restoreGeometry(geometry) |
394 window.restoreGeometry(geometry) |
376 QApplication.processEvents() |
395 QApplication.processEvents() |
377 QApplication.restoreOverrideCursor() |
396 QApplication.restoreOverrideCursor() |
378 |
397 |
379 def renameSession(self, sessionFilePath="", flags=None): |
398 def renameSession(self, sessionFilePath, flags=0): |
380 # TODO: implement this |
399 """ |
381 pass |
400 Public method to rename or clone a session |
|
401 |
|
402 @param sessionFilePath name of the session file |
|
403 @type str |
|
404 @param flags flags determining a rename or clone operation |
|
405 @type int |
|
406 """ |
|
407 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
408 |
|
409 suggestedName = QFileInfo(sessionFilePath).baseName() |
|
410 if flags & SessionManager.CloneSession: |
|
411 suggestedName += "_cloned" |
|
412 title = self.tr("Clone Session") |
|
413 else: |
|
414 suggestedName += "_renamed" |
|
415 title = self.tr("Rename Session") |
|
416 newName, ok = QInputDialog.getText( |
|
417 WebBrowserWindow.getWindow(), |
|
418 title, |
|
419 self.tr("Please enter a new name:"), |
|
420 QLineEdit.Normal, |
|
421 suggestedName) |
|
422 |
|
423 if not ok: |
|
424 return |
|
425 |
|
426 if not newName.endswith(".json"): |
|
427 newName += ".json" |
|
428 |
|
429 newSessionPath = os.path.join(self.getSessionsDirectory(), newName) |
|
430 if os.path.exists(newSessionPath): |
|
431 E5MessageBox.information( |
|
432 WebBrowserWindow.getWindow(), |
|
433 title, |
|
434 self.tr("""The session file "{0}" exists already. Please""" |
|
435 """ enter another name.""").format(newName)) |
|
436 self.renameSession(sessionFilePath, flags) |
|
437 return |
|
438 |
|
439 if flags & SessionManager.CloneSession: |
|
440 if not QFile.copy(sessionFilePath, newSessionPath): |
|
441 E5MessageBox.critical( |
|
442 WebBrowserWindow.getWindow(), |
|
443 title, |
|
444 self.tr("""An error occurred while cloning the session""" |
|
445 """ file.""")) |
|
446 return |
|
447 else: |
|
448 if not QFile.rename(sessionFilePath, newSessionPath): |
|
449 E5MessageBox.critical( |
|
450 WebBrowserWindow.getWindow(), |
|
451 title, |
|
452 self.tr("""An error occurred while renaming the session""" |
|
453 """ file.""")) |
|
454 return |
|
455 if self.__isActive(sessionFilePath): |
|
456 self.__lastActiveSession = newSessionPath |
|
457 self.__sessionMetaData = [] |
382 |
458 |
383 def saveSession(self): |
459 def saveSession(self): |
384 # TODO: implement this |
460 """ |
385 pass |
461 Public method to save the current session. |
|
462 """ |
|
463 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
464 newName, ok = QInputDialog.getText( |
|
465 WebBrowserWindow.getWindow(), |
|
466 self.tr("Save Session"), |
|
467 self.tr("Please enter a name for the session:"), |
|
468 QLineEdit.Normal, |
|
469 self.tr("Saved Session ({0})").format( |
|
470 QDateTime.currentDateTime().toString("yyyy-MM-dd HH-mm-ss"))) |
|
471 |
|
472 if not ok: |
|
473 return |
|
474 |
|
475 if not newName.endswith(".json"): |
|
476 newName += ".json" |
|
477 |
|
478 newSessionPath = os.path.join(self.getSessionsDirectory(), newName) |
|
479 if os.path.exists(newSessionPath): |
|
480 E5MessageBox.information( |
|
481 WebBrowserWindow.getWindow(), |
|
482 self.tr("Save Session"), |
|
483 self.tr("""The session file "{0}" exists already. Please""" |
|
484 """ enter another name.""").format(newName)) |
|
485 self.saveSession() |
|
486 return |
|
487 |
|
488 self.writeCurrentSession(newSessionPath) |
386 |
489 |
387 def replaceSession(self, sessionFilePath): |
490 def replaceSession(self, sessionFilePath): |
388 """ |
491 """ |
389 Public method to replace the current session with the given one. |
492 Public method to replace the current session with the given one. |
390 |
493 |
391 @param sessionFilePath file name of the session file to replace with |
494 @param sessionFilePath file name of the session file to replace with |
392 @type str |
495 @type str |
393 """ |
496 """ |
394 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
497 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
395 res = E5MessageBox.yesNo( |
498 res = E5MessageBox.yesNo( |
396 WebBrowserWindow.mainWindow(), |
499 WebBrowserWindow.getWindow(), |
397 self.tr("Restore Backup"), |
500 self.tr("Restore Backup"), |
398 self.tr("""Are you sure you want to replace current session?""")) |
501 self.tr("""Are you sure you want to replace the current""" |
|
502 """ session?""")) |
399 if res: |
503 if res: |
400 self.__openSession(sessionFilePath, SessionManager.ReplaceSession) |
504 self.__openSession(sessionFilePath, SessionManager.ReplaceSession) |
401 |
505 |
402 def switchToSession(self, sessionFilePath): |
506 def switchToSession(self, sessionFilePath): |
403 """ |
507 """ |
407 @type str |
511 @type str |
408 """ |
512 """ |
409 self.__openSession(sessionFilePath, SessionManager.SwitchSession) |
513 self.__openSession(sessionFilePath, SessionManager.SwitchSession) |
410 |
514 |
411 def cloneSession(self, sessionFilePath): |
515 def cloneSession(self, sessionFilePath): |
412 # TODO: implement this |
516 """ |
413 pass |
517 Public method to clone a session. |
|
518 |
|
519 @param sessionFilePath file name of the session file to be cloned |
|
520 @type str |
|
521 """ |
|
522 self.renameSession(sessionFilePath, SessionManager.CloneSession) |
414 |
523 |
415 def deleteSession(self, sessionFilePath): |
524 def deleteSession(self, sessionFilePath): |
416 # TODO: implement this |
525 """ |
417 pass |
526 Public method to delete a session. |
|
527 |
|
528 @param sessionFilePath file name of the session file to be deleted |
|
529 @type str |
|
530 """ |
|
531 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
532 res = E5MessageBox.yesNo( |
|
533 WebBrowserWindow.getWindow(), |
|
534 self.tr("Delete Session"), |
|
535 self.tr("""Are you sure you want to delete session "{0}"?""") |
|
536 .format(QFileInfo(sessionFilePath).baseName())) |
|
537 if res: |
|
538 QFile.remove(sessionFilePath) |
418 |
539 |
419 def newSession(self): |
540 def newSession(self): |
420 # TODO: implement this |
541 """ |
421 pass |
542 Public method to start a new session. |
|
543 """ |
|
544 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
545 newName, ok = QInputDialog.getText( |
|
546 WebBrowserWindow.getWindow(), |
|
547 self.tr("New Session"), |
|
548 self.tr("Please enter a name for the new session:"), |
|
549 QLineEdit.Normal, |
|
550 self.tr("New Session ({0})").format( |
|
551 QDateTime.currentDateTime().toString("yyyy-MM-dd HH-mm-ss"))) |
|
552 |
|
553 if not ok: |
|
554 return |
|
555 |
|
556 if not newName.endswith(".json"): |
|
557 newName += ".json" |
|
558 |
|
559 newSessionPath = os.path.join(self.getSessionsDirectory(), newName) |
|
560 if os.path.exists(newSessionPath): |
|
561 E5MessageBox.information( |
|
562 WebBrowserWindow.getWindow(), |
|
563 self.tr("New Session"), |
|
564 self.tr("""The session file "{0}" exists already. Please""" |
|
565 """ enter another name.""").format(newName)) |
|
566 self.newSession() |
|
567 return |
|
568 |
|
569 self.writeCurrentSession(self.__lastActiveSession) |
|
570 |
|
571 # create new window for the new session and close all existing windows |
|
572 window = WebBrowserWindow.mainWindow().newWindow() |
|
573 for win in WebBrowserWindow.mainWindows(): |
|
574 if win is not window: |
|
575 win.forceClose() |
|
576 |
|
577 self.__lastActiveSession = newSessionPath |
|
578 self.__autoSaveSession() |
422 |
579 |
423 def showSessionManagerDialog(self): |
580 def showSessionManagerDialog(self): |
424 # TODO: implement this |
581 """ |
425 pass |
582 Public method to show the session manager dialog. |
426 print("showSessionManagerDialog()") |
583 """ |
|
584 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
585 from .SessionManagerDialog import SessionManagerDialog |
|
586 |
|
587 dlg = SessionManagerDialog(WebBrowserWindow.getWindow()) |
|
588 dlg.open() |