|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show a list of existing crash session files. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import os |
|
12 import time |
|
13 |
|
14 from PyQt6.QtCore import Qt, pyqtSlot |
|
15 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem |
|
16 |
|
17 from eric7.EricWidgets import EricMessageBox |
|
18 |
|
19 from .Ui_CrashedSessionsSelectionDialog import Ui_CrashedSessionsSelectionDialog |
|
20 |
|
21 |
|
22 class CrashedSessionsSelectionDialog(QDialog, Ui_CrashedSessionsSelectionDialog): |
|
23 """ |
|
24 Class implementing a dialog to show a list of existing crash session files. |
|
25 """ |
|
26 |
|
27 def __init__(self, sessionFiles, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param sessionFiles list of crash session file names |
|
32 @type list of str |
|
33 @param parent reference to the parent widget (defaults to None) |
|
34 @type QWidget (optional) |
|
35 """ |
|
36 super().__init__(parent) |
|
37 self.setupUi(self) |
|
38 |
|
39 self.crashedSessionsList.itemDoubleClicked.connect(self.accept) |
|
40 self.crashedSessionsList.itemActivated.connect(self.accept) |
|
41 self.crashedSessionsList.itemSelectionChanged.connect(self.__updateOk) |
|
42 |
|
43 for sessionFile in sessionFiles: |
|
44 self.__addSessionFileEntry(sessionFile) |
|
45 |
|
46 self.__updateOk() |
|
47 |
|
48 @pyqtSlot() |
|
49 def __updateOk(self): |
|
50 """ |
|
51 Private method to update the enabled state of the OK button. |
|
52 """ |
|
53 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
54 bool(self.crashedSessionsList.selectedItems()) |
|
55 ) |
|
56 |
|
57 def __addSessionFileEntry(self, sessionFile): |
|
58 """ |
|
59 Private method to read the given session file and add a list entry for it. |
|
60 |
|
61 @param sessionFile file name of the session to be read |
|
62 @type str |
|
63 """ |
|
64 if os.path.exists(sessionFile): |
|
65 try: |
|
66 with open(sessionFile, "r") as f: |
|
67 jsonString = f.read() |
|
68 sessionDict = json.loads(jsonString) |
|
69 except (OSError, json.JSONDecodeError) as err: |
|
70 EricMessageBox.critical( |
|
71 None, |
|
72 self.tr("Read Crash Session"), |
|
73 self.tr( |
|
74 "<p>The crash session file <b>{0}</b> could not be read.</p>" |
|
75 "<p>Reason: {1}</p>" |
|
76 ).format(sessionFile, str(err)), |
|
77 ) |
|
78 |
|
79 mtime = os.path.getmtime(sessionFile) |
|
80 timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime)) |
|
81 if sessionDict["Project"]: |
|
82 labelText = self.tr( |
|
83 "{0}\nTimestamp: {1}\nProject: {2}", |
|
84 "Crash Session, Timestamp, Project Path", |
|
85 ).format(sessionFile, timestamp, sessionDict["Project"]) |
|
86 else: |
|
87 labelText = self.tr( |
|
88 "{0}\nTimestamp: {1}", "Crash Session, Timestamp" |
|
89 ).format(sessionFile, timestamp) |
|
90 itm = QListWidgetItem(labelText, self.crashedSessionsList) |
|
91 itm.setData(Qt.ItemDataRole.UserRole, sessionFile) |
|
92 |
|
93 def getSelectedCrashSession(self): |
|
94 """ |
|
95 Public method to get the selected crash session file name. |
|
96 |
|
97 @return file name of the selected crash session |
|
98 @rtype str |
|
99 """ |
|
100 # TODO: not implemented yet |
|
101 selectedItems = self.crashedSessionsList.selectedItems() |
|
102 |
|
103 if selectedItems: |
|
104 return selectedItems[0].data(Qt.ItemDataRole.UserRole) |
|
105 else: |
|
106 return None |