src/eric7/Sessions/CrashedSessionsSelectionDialog.py

Thu, 03 Apr 2025 19:50:43 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 03 Apr 2025 19:50:43 +0200
branch
eric7
changeset 11206
9271719f43a7
child 11207
7193db06924d
permissions
-rw-r--r--

Modified the display of the crash session dialog to show the time stamp of the found crash session file and the path of the project file (if a project was open) (see issue584).

# -*- coding: utf-8 -*-

# Copyright (c) 2025 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to show a list of existing crash session files.
"""

import json
import os
import time

from PyQt6.QtCore import Qt, pyqtSlot
from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem

from eric7.EricWidgets import EricMessageBox

from .Ui_CrashedSessionsSelectionDialog import Ui_CrashedSessionsSelectionDialog


class CrashedSessionsSelectionDialog(QDialog, Ui_CrashedSessionsSelectionDialog):
    """
    Class implementing a dialog to show a list of existing crash session files.
    """

    def __init__(self, sessionFiles, parent=None):
        """
        Constructor

        @param sessionFiles list of crash session file names
        @type list of str
        @param parent reference to the parent widget (defaults to None)
        @type QWidget (optional)
        """
        super().__init__(parent)
        self.setupUi(self)

        self.crashedSessionsList.itemDoubleClicked.connect(self.accept)
        self.crashedSessionsList.itemActivated.connect(self.accept)
        self.crashedSessionsList.itemSelectionChanged.connect(self.__updateOk)

        for sessionFile in sessionFiles:
            self.__addSessionFileEntry(sessionFile)

        self.__updateOk()

    @pyqtSlot()
    def __updateOk(self):
        """
        Private method to update the enabled state of the OK button.
        """
        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(
            bool(self.crashedSessionsList.selectedItems())
        )

    def __addSessionFileEntry(self, sessionFile):
        """
        Private method to read the given session file and add a list entry for it.

        @param sessionFile file name of the session to be read
        @type str
        """
        if os.path.exists(sessionFile):
            try:
                with open(sessionFile, "r") as f:
                    jsonString = f.read()
                sessionDict = json.loads(jsonString)
            except (OSError, json.JSONDecodeError) as err:
                EricMessageBox.critical(
                    None,
                    self.tr("Read Crash Session"),
                    self.tr(
                        "<p>The crash session file <b>{0}</b> could not be read.</p>"
                        "<p>Reason: {1}</p>"
                    ).format(sessionFile, str(err)),
                )

            mtime = os.path.getmtime(sessionFile)
            timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime))
            if sessionDict["Project"]:
                labelText = self.tr(
                    "{0}\nTimestamp: {1}\nProject: {2}",
                    "Crash Session, Timestamp, Project Path",
                ).format(sessionFile, timestamp, sessionDict["Project"])
            else:
                labelText = self.tr(
                    "{0}\nTimestamp: {1}", "Crash Session, Timestamp"
                ).format(sessionFile, timestamp)
            itm = QListWidgetItem(labelText, self.crashedSessionsList)
            itm.setData(Qt.ItemDataRole.UserRole, sessionFile)

    def getSelectedCrashSession(self):
        """
        Public method to get the selected crash session file name.

        @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

eric ide

mercurial