10 import os |
10 import os |
11 import json |
11 import json |
12 import functools |
12 import functools |
13 import contextlib |
13 import contextlib |
14 import pathlib |
14 import pathlib |
|
15 import shutil |
15 |
16 |
16 from PyQt6.QtCore import ( |
17 from PyQt6.QtCore import ( |
17 pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, QFile, QFileSystemWatcher, |
18 pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, QFileSystemWatcher, |
18 QByteArray, QDateTime |
19 QByteArray, QDateTime |
19 ) |
20 ) |
20 from PyQt6.QtGui import QActionGroup |
21 from PyQt6.QtGui import QActionGroup |
21 from PyQt6.QtWidgets import ( |
22 from PyQt6.QtWidgets import ( |
22 QApplication, QInputDialog, QLineEdit, QDialog, QDialogButtonBox, QLabel, |
23 QApplication, QInputDialog, QLineEdit, QDialog, QDialogButtonBox, QLabel, |
82 self.__sessionBackup2 = os.path.join(sessionsDirName, |
83 self.__sessionBackup2 = os.path.join(sessionsDirName, |
83 "session.json.old1") |
84 "session.json.old1") |
84 |
85 |
85 self.__lastActiveSession = Preferences.getWebBrowser( |
86 self.__lastActiveSession = Preferences.getWebBrowser( |
86 "SessionLastActivePath") |
87 "SessionLastActivePath") |
87 if not QFile.exists(self.__lastActiveSession): |
88 if not os.path.exists(self.__lastActiveSession): |
88 self.__lastActiveSession = self.__sessionDefault |
89 self.__lastActiveSession = self.__sessionDefault |
89 |
90 |
90 self.__sessionsDirectoryWatcher = QFileSystemWatcher( |
91 self.__sessionsDirectoryWatcher = QFileSystemWatcher( |
91 [self.getSessionsDirectory()], self) |
92 [self.getSessionsDirectory()], self) |
92 self.__sessionsDirectoryWatcher.directoryChanged.connect( |
93 self.__sessionsDirectoryWatcher.directoryChanged.connect( |
260 |
261 |
261 def __backupSavedSession(self): |
262 def __backupSavedSession(self): |
262 """ |
263 """ |
263 Private method to backup the most recently saved session. |
264 Private method to backup the most recently saved session. |
264 """ |
265 """ |
265 if QFile.exists(self.__lastActiveSession): |
266 if os.path.exists(self.__lastActiveSession): |
266 |
267 |
267 if QFile.exists(self.__sessionBackup1): |
268 if os.path.exists(self.__sessionBackup1): |
268 QFile.remove(self.__sessionBackup2) |
269 os.unlink(self.__sessionBackup2) |
269 QFile.copy(self.__sessionBackup1, self.__sessionBackup2) |
270 shutil.copy(self.__sessionBackup1, self.__sessionBackup2) |
270 |
271 |
271 QFile.remove(self.__sessionBackup1) |
272 os.unlink(self.__sessionBackup1) |
272 QFile.copy(self.__lastActiveSession, self.__sessionBackup1) |
273 shutil.copy(self.__lastActiveSession, self.__sessionBackup1) |
273 |
274 |
274 def sessionMetaData(self, includeBackups=False): |
275 def sessionMetaData(self, includeBackups=False): |
275 """ |
276 """ |
276 Public method to get the sessions meta data. |
277 Public method to get the sessions meta data. |
277 |
278 |
282 """ |
283 """ |
283 self.__fillMetaDataList() |
284 self.__fillMetaDataList() |
284 |
285 |
285 metaDataList = self.__sessionMetaData[:] |
286 metaDataList = self.__sessionMetaData[:] |
286 |
287 |
287 if includeBackups and QFile.exists(self.__sessionBackup1): |
288 if includeBackups and os.path.exists(self.__sessionBackup1): |
288 data = SessionMetaData() |
289 data = SessionMetaData() |
289 data.name = self.tr("Backup 1") |
290 data.name = self.tr("Backup 1") |
290 data.filePath = self.__sessionBackup1 |
291 data.filePath = self.__sessionBackup1 |
291 data.isBackup = True |
292 data.isBackup = True |
292 metaDataList.append(data) |
293 metaDataList.append(data) |
293 |
294 |
294 if includeBackups and QFile.exists(self.__sessionBackup2): |
295 if includeBackups and os.path.exists(self.__sessionBackup2): |
295 data = SessionMetaData() |
296 data = SessionMetaData() |
296 data.name = self.tr("Backup 2") |
297 data.name = self.tr("Backup 2") |
297 data.filePath = self.__sessionBackup2 |
298 data.filePath = self.__sessionBackup2 |
298 data.isBackup = True |
299 data.isBackup = True |
299 metaDataList.append(data) |
300 metaDataList.append(data) |
513 """ enter another name.""").format(newName)) |
514 """ enter another name.""").format(newName)) |
514 self.renameSession(sessionFilePath, flags) |
515 self.renameSession(sessionFilePath, flags) |
515 return |
516 return |
516 |
517 |
517 if flags & SessionManager.CloneSession: |
518 if flags & SessionManager.CloneSession: |
518 if not QFile.copy(sessionFilePath, newSessionPath): |
519 if not shutil.copy(sessionFilePath, newSessionPath): |
519 EricMessageBox.critical( |
520 EricMessageBox.critical( |
520 WebBrowserWindow.getWindow(), |
521 WebBrowserWindow.getWindow(), |
521 title, |
522 title, |
522 self.tr("""An error occurred while cloning the session""" |
523 self.tr("""An error occurred while cloning the session""" |
523 """ file.""")) |
524 """ file.""")) |
524 return |
525 return |
525 else: |
526 else: |
526 if not QFile.rename(sessionFilePath, newSessionPath): |
527 try: |
|
528 os.rename(sessionFilePath, newSessionPath) |
|
529 except OSError: |
527 EricMessageBox.critical( |
530 EricMessageBox.critical( |
528 WebBrowserWindow.getWindow(), |
531 WebBrowserWindow.getWindow(), |
529 title, |
532 title, |
530 self.tr("""An error occurred while renaming the session""" |
533 self.tr("""An error occurred while renaming the session""" |
531 """ file.""")) |
534 """ file.""")) |
613 |
616 |
614 @param sessionFilePath file name of the session file to be deleted |
617 @param sessionFilePath file name of the session file to be deleted |
615 @type str |
618 @type str |
616 """ |
619 """ |
617 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
620 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
621 sfp = pathlib.Path(sessionFilePath) |
618 res = EricMessageBox.yesNo( |
622 res = EricMessageBox.yesNo( |
619 WebBrowserWindow.getWindow(), |
623 WebBrowserWindow.getWindow(), |
620 self.tr("Delete Session"), |
624 self.tr("Delete Session"), |
621 self.tr("""Are you sure you want to delete session "{0}"?""") |
625 self.tr("""Are you sure you want to delete session "{0}"?""") |
622 .format(pathlib.Path(sessionFilePath).stem)) |
626 .format(sfp.stem)) |
623 if res: |
627 if res: |
624 QFile.remove(sessionFilePath) |
628 sfp.unlink() |
625 |
629 |
626 def newSession(self): |
630 def newSession(self): |
627 """ |
631 """ |
628 Public method to start a new session. |
632 Public method to start a new session. |
629 """ |
633 """ |