--- a/src/eric7/Sessions/CrashedSessionsSelectionDialog.py Thu Apr 03 19:50:43 2025 +0200 +++ b/src/eric7/Sessions/CrashedSessionsSelectionDialog.py Sun Apr 06 11:01:28 2025 +0200 @@ -7,12 +7,18 @@ Module implementing a dialog to show a list of existing crash session files. """ +import contextlib import json import os import time from PyQt6.QtCore import Qt, pyqtSlot -from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem +from PyQt6.QtWidgets import ( + QAbstractItemView, + QDialog, + QDialogButtonBox, + QListWidgetItem, +) from eric7.EricWidgets import EricMessageBox @@ -24,12 +30,14 @@ Class implementing a dialog to show a list of existing crash session files. """ - def __init__(self, sessionFiles, parent=None): + def __init__(self, sessionFiles, deleteMode=False, parent=None): """ Constructor @param sessionFiles list of crash session file names @type list of str + @param deleteMode flag indicating the delete mode (defaults to False) + @type bool (optional) @param parent reference to the parent widget (defaults to None) @type QWidget (optional) """ @@ -38,21 +46,44 @@ self.crashedSessionsList.itemDoubleClicked.connect(self.accept) self.crashedSessionsList.itemActivated.connect(self.accept) - self.crashedSessionsList.itemSelectionChanged.connect(self.__updateOk) + self.crashedSessionsList.itemSelectionChanged.connect(self.__updateButtonStates) for sessionFile in sessionFiles: self.__addSessionFileEntry(sessionFile) - self.__updateOk() + self.__deleteMode = deleteMode + if deleteMode: + self.setWindowTitle(self.tr("Clean Crash Sessions")) + self.messageLabel.setText( + self.tr( + "These crash session files were found. Select the ones to be" + " deleted." + ) + ) + self.crashedSessionsList.setSelectionMode( + QAbstractItemView.SelectionMode.ExtendedSelection + ) + self.removeButton.hide() + else: + self.setWindowTitle(self.tr("Found Crash Sessions")) + self.messageLabel.setText( + self.tr( + "These crash session files were found. Select the one to open." + " Select 'Cancel' to not open a crash session." + ) + ) + + self.__updateButtonStates() @pyqtSlot() - def __updateOk(self): + def __updateButtonStates(self): """ - Private method to update the enabled state of the OK button. + Private method to update the enabled state of the buttons. """ self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( bool(self.crashedSessionsList.selectedItems()) ) + self.removeButton.setEnabled(bool(self.crashedSessionsList.selectedItems())) def __addSessionFileEntry(self, sessionFile): """ @@ -90,6 +121,18 @@ itm = QListWidgetItem(labelText, self.crashedSessionsList) itm.setData(Qt.ItemDataRole.UserRole, sessionFile) + @pyqtSlot() + def on_removeButton_clicked(self): + """ + Private slot to remove the selected crash session files. + """ + for itm in self.crashedSessionsList.selectedItems(): + crashSession = itm.data(Qt.ItemDataRole.UserRole) + with contextlib.suppress(OSError): + os.remove(crashSession) + self.crashedSessionsList.takeItem(self.crashedSessionsList.row(itm)) + del itm + def getSelectedCrashSession(self): """ Public method to get the selected crash session file name. @@ -97,10 +140,23 @@ @return file name of the selected crash session @rtype str """ - # TODO: not implemented yet selectedItems = self.crashedSessionsList.selectedItems() if selectedItems: return selectedItems[0].data(Qt.ItemDataRole.UserRole) else: return None + + def getSelectedCrashSessions(self): + """ + Public method to get the selected crash session file names. + + @return file names of the selected crash sessions + @rtype list of str + """ + selectedItems = self.crashedSessionsList.selectedItems() + + if selectedItems: + return [itm.data(Qt.ItemDataRole.UserRole) for itm in selectedItems] + else: + return []