9 |
9 |
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 |
15 |
15 from PyQt6.QtCore import ( |
16 from PyQt6.QtCore import ( |
16 pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, QFile, QFileInfo, |
17 pyqtSlot, pyqtSignal, Qt, QObject, QTimer, QDir, QFile, QFileSystemWatcher, |
17 QFileSystemWatcher, QByteArray, QDateTime |
18 QByteArray, QDateTime |
18 ) |
19 ) |
19 from PyQt6.QtGui import QActionGroup |
20 from PyQt6.QtGui import QActionGroup |
20 from PyQt6.QtWidgets import ( |
21 from PyQt6.QtWidgets import ( |
21 QApplication, QInputDialog, QLineEdit, QDialog, QDialogButtonBox, QLabel, |
22 QApplication, QInputDialog, QLineEdit, QDialog, QDialogButtonBox, QLabel, |
22 QComboBox, QVBoxLayout |
23 QComboBox, QVBoxLayout |
307 it is empty (i.e. it is populated on demand). |
308 it is empty (i.e. it is populated on demand). |
308 """ |
309 """ |
309 if self.__sessionMetaData: |
310 if self.__sessionMetaData: |
310 return |
311 return |
311 |
312 |
312 sessionFilesInfoList = QDir(self.getSessionsDirectory()).entryInfoList( |
313 sessionFiles = pathlib.Path(self.getSessionsDirectory()).glob("*.json") |
313 ["*.json"], QDir.Filter.Files, QDir.SortFlag.Time) |
314 |
314 |
315 for sessionFile in sessionFiles: |
315 for sessionFileInfo in sessionFilesInfoList: |
316 sessionData = self.readSessionFromFile(sessionFile.resolve()) |
316 sessionData = self.readSessionFromFile( |
|
317 sessionFileInfo.absoluteFilePath()) |
|
318 if not sessionData or not sessionData["Windows"]: |
317 if not sessionData or not sessionData["Windows"]: |
319 continue |
318 continue |
320 |
319 |
321 data = SessionMetaData() |
320 data = SessionMetaData() |
322 data.name = sessionFileInfo.baseName() |
321 data.name = sessionFile.stem |
323 data.filePath = sessionFileInfo.canonicalFilePath() |
322 data.filePath = sessionFile.resolve() |
324 |
323 |
325 if sessionFileInfo == QFileInfo(self.defaultSessionFile()): |
324 if sessionFile == pathlib.Path(self.defaultSessionFile()): |
326 data.name = self.tr("Default Session") |
325 data.name = self.tr("Default Session") |
327 data.isDefault = True |
326 data.isDefault = True |
328 |
327 |
329 if self.__isActive(sessionFileInfo): |
328 if self.__isActive(sessionFile): |
330 data.isActive = True |
329 data.isActive = True |
331 |
330 |
332 if data.isDefault: |
331 if data.isDefault: |
333 # default session is always first |
332 # default session is always first |
334 self.__sessionMetaData.insert(0, data) |
333 self.__sessionMetaData.insert(0, data) |
338 def __isActive(self, filePath): |
337 def __isActive(self, filePath): |
339 """ |
338 """ |
340 Private method to check, if a given file is the active one. |
339 Private method to check, if a given file is the active one. |
341 |
340 |
342 @param filePath path of the session file to be checked |
341 @param filePath path of the session file to be checked |
343 @type str or QFileInfo |
342 @type str or pathlib.Path |
344 @return flag indicating the active file |
343 @return flag indicating the active file |
345 @rtype bool |
344 @rtype bool |
346 """ |
345 """ |
347 return QFileInfo(filePath) == QFileInfo(self.__lastActiveSession) |
346 return pathlib.Path(filePath) == pathlib.Path(self.__lastActiveSession) |
348 |
347 |
349 @pyqtSlot() |
348 @pyqtSlot() |
350 def __sessionDirectoryChanged(self): |
349 def __sessionDirectoryChanged(self): |
351 """ |
350 """ |
352 Private slot handling changes of the sessions directory. |
351 Private slot handling changes of the sessions directory. |
422 if ( |
421 if ( |
423 (flags & SessionManager.ReplaceSession) != |
422 (flags & SessionManager.ReplaceSession) != |
424 SessionManager.ReplaceSession |
423 SessionManager.ReplaceSession |
425 ): |
424 ): |
426 self.__lastActiveSession = ( |
425 self.__lastActiveSession = ( |
427 QFileInfo(sessionFilePath).canonicalFilePath() |
426 pathlib.Path(sessionFilePath).resolve() |
428 ) |
427 ) |
429 self.__sessionMetaData = [] |
428 self.__sessionMetaData = [] |
430 |
429 |
431 self.restoreSessionFromData(window, sessionData) |
430 self.restoreSessionFromData(window, sessionData) |
432 |
431 |
483 @param flags flags determining a rename or clone operation |
482 @param flags flags determining a rename or clone operation |
484 @type int |
483 @type int |
485 """ |
484 """ |
486 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
485 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
487 |
486 |
488 suggestedName = QFileInfo(sessionFilePath).baseName() |
487 suggestedName = pathlib.Path(sessionFilePath).stem |
489 if flags & SessionManager.CloneSession: |
488 if flags & SessionManager.CloneSession: |
490 suggestedName += "_cloned" |
489 suggestedName += "_cloned" |
491 title = self.tr("Clone Session") |
490 title = self.tr("Clone Session") |
492 else: |
491 else: |
493 suggestedName += "_renamed" |
492 suggestedName += "_renamed" |
618 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
617 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
619 res = EricMessageBox.yesNo( |
618 res = EricMessageBox.yesNo( |
620 WebBrowserWindow.getWindow(), |
619 WebBrowserWindow.getWindow(), |
621 self.tr("Delete Session"), |
620 self.tr("Delete Session"), |
622 self.tr("""Are you sure you want to delete session "{0}"?""") |
621 self.tr("""Are you sure you want to delete session "{0}"?""") |
623 .format(QFileInfo(sessionFilePath).baseName())) |
622 .format(pathlib.Path(sessionFilePath).stem)) |
624 if res: |
623 if res: |
625 QFile.remove(sessionFilePath) |
624 QFile.remove(sessionFilePath) |
626 |
625 |
627 def newSession(self): |
626 def newSession(self): |
628 """ |
627 """ |
702 layout.addWidget(lbl) |
701 layout.addWidget(lbl) |
703 layout.addWidget(combo) |
702 layout.addWidget(combo) |
704 layout.addWidget(buttonBox) |
703 layout.addWidget(buttonBox) |
705 dlg.setLayout(layout) |
704 dlg.setLayout(layout) |
706 |
705 |
707 lastActiveSessionFileInfo = QFileInfo(self.__lastActiveSession) |
706 lastActiveSessionFilePath = pathlib.Path(self.__lastActiveSession) |
708 |
707 |
709 for metaData in self.__sessionMetaData: |
708 for metaData in self.__sessionMetaData: |
710 if QFileInfo(metaData.filePath) != lastActiveSessionFileInfo: |
709 if ( |
|
710 pathlib.Path(metaData.filePath) != |
|
711 lastActiveSessionFilePath |
|
712 ): |
711 combo.addItem(metaData.name, metaData.filePath) |
713 combo.addItem(metaData.name, metaData.filePath) |
712 else: |
714 else: |
713 combo.insertItem( |
715 combo.insertItem( |
714 0, |
716 0, |
715 self.tr("{0} (last session)").format(metaData.name), |
717 self.tr("{0} (last session)").format(metaData.name), |