9 |
9 |
10 import contextlib |
10 import contextlib |
11 |
11 |
12 from PyQt6.QtCore import pyqtSlot, Qt |
12 from PyQt6.QtCore import pyqtSlot, Qt |
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
|
14 |
14 with contextlib.suppress(ImportError): |
15 with contextlib.suppress(ImportError): |
15 from PyQt6.QtNetwork import QSslCertificate |
16 from PyQt6.QtNetwork import QSslCertificate |
16 |
17 |
17 from .Ui_EricSslCertificateSelectionDialog import ( |
18 from .Ui_EricSslCertificateSelectionDialog import Ui_EricSslCertificateSelectionDialog |
18 Ui_EricSslCertificateSelectionDialog |
|
19 ) |
|
20 |
19 |
21 import Utilities |
20 import Utilities |
22 import UI.PixmapCache |
21 import UI.PixmapCache |
23 |
22 |
24 |
23 |
25 class EricSslCertificateSelectionDialog(QDialog, |
24 class EricSslCertificateSelectionDialog(QDialog, Ui_EricSslCertificateSelectionDialog): |
26 Ui_EricSslCertificateSelectionDialog): |
|
27 """ |
25 """ |
28 Class implementing a dialog to select a SSL certificate. |
26 Class implementing a dialog to select a SSL certificate. |
29 """ |
27 """ |
|
28 |
30 CertRole = Qt.ItemDataRole.UserRole + 1 |
29 CertRole = Qt.ItemDataRole.UserRole + 1 |
31 |
30 |
32 def __init__(self, certificates, parent=None): |
31 def __init__(self, certificates, parent=None): |
33 """ |
32 """ |
34 Constructor |
33 Constructor |
35 |
34 |
36 @param certificates list of SSL certificates to select from |
35 @param certificates list of SSL certificates to select from |
37 @type list of QSslCertificate |
36 @type list of QSslCertificate |
38 @param parent reference to the parent widget |
37 @param parent reference to the parent widget |
39 @type QWidget |
38 @type QWidget |
40 """ |
39 """ |
41 super().__init__(parent) |
40 super().__init__(parent) |
42 self.setupUi(self) |
41 self.setupUi(self) |
43 |
42 |
44 self.viewButton.setIcon( |
43 self.viewButton.setIcon(UI.PixmapCache.getIcon("certificates")) |
45 UI.PixmapCache.getIcon("certificates")) |
44 |
46 |
|
47 self.buttonBox.button(QDialogButtonBox.OK).setEnabled(False) |
45 self.buttonBox.button(QDialogButtonBox.OK).setEnabled(False) |
48 self.viewButton.setEnabled(False) |
46 self.viewButton.setEnabled(False) |
49 |
47 |
50 self.__populateCertificatesTree(certificates) |
48 self.__populateCertificatesTree(certificates) |
51 |
49 |
52 def __populateCertificatesTree(self, certificates): |
50 def __populateCertificatesTree(self, certificates): |
53 """ |
51 """ |
54 Private slot to populate the certificates tree. |
52 Private slot to populate the certificates tree. |
55 |
53 |
56 @param certificates list of SSL certificates to select from |
54 @param certificates list of SSL certificates to select from |
57 @type list of QSslCertificate |
55 @type list of QSslCertificate |
58 """ |
56 """ |
59 for cert in certificates(): |
57 for cert in certificates(): |
60 self.__createCertificateEntry(cert) |
58 self.__createCertificateEntry(cert) |
61 |
59 |
62 self.certificatesTree.expandAll() |
60 self.certificatesTree.expandAll() |
63 for i in range(self.certificatesTree.columnCount()): |
61 for i in range(self.certificatesTree.columnCount()): |
64 self.certificatesTree.resizeColumnToContents(i) |
62 self.certificatesTree.resizeColumnToContents(i) |
65 self.certificatesTree.sortItems(0, Qt.SortOrder.AscendingOrder) |
63 self.certificatesTree.sortItems(0, Qt.SortOrder.AscendingOrder) |
66 |
64 |
67 def __createCaCertificateEntry(self, cert): |
65 def __createCaCertificateEntry(self, cert): |
68 """ |
66 """ |
69 Private method to create a certificate entry. |
67 Private method to create a certificate entry. |
70 |
68 |
71 @param cert certificate to insert |
69 @param cert certificate to insert |
72 @type QSslCertificate |
70 @type QSslCertificate |
73 """ |
71 """ |
74 # step 1: extract the info to be shown |
72 # step 1: extract the info to be shown |
75 organisation = Utilities.decodeString( |
73 organisation = Utilities.decodeString( |
76 ", ".join(cert.subjectInfo( |
74 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.Organization)) |
77 QSslCertificate.SubjectInfo.Organization))) |
75 ) |
78 commonName = Utilities.decodeString( |
76 commonName = Utilities.decodeString( |
79 ", ".join(cert.subjectInfo( |
77 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.CommonName)) |
80 QSslCertificate.SubjectInfo.CommonName))) |
78 ) |
81 if organisation is None or organisation == "": |
79 if organisation is None or organisation == "": |
82 organisation = self.tr("(Unknown)") |
80 organisation = self.tr("(Unknown)") |
83 if commonName is None or commonName == "": |
81 if commonName is None or commonName == "": |
84 commonName = self.tr("(Unknown common name)") |
82 commonName = self.tr("(Unknown common name)") |
85 expiryDate = cert.expiryDate().toString("yyyy-MM-dd") |
83 expiryDate = cert.expiryDate().toString("yyyy-MM-dd") |
86 |
84 |
87 # step 2: create the entry |
85 # step 2: create the entry |
88 items = self.certificatesTree.findItems( |
86 items = self.certificatesTree.findItems( |
89 organisation, |
87 organisation, |
90 Qt.MatchFlag.MatchFixedString | Qt.MatchFlag.MatchCaseSensitive) |
88 Qt.MatchFlag.MatchFixedString | Qt.MatchFlag.MatchCaseSensitive, |
|
89 ) |
91 if len(items) == 0: |
90 if len(items) == 0: |
92 parent = QTreeWidgetItem(self.certificatesTree, [organisation]) |
91 parent = QTreeWidgetItem(self.certificatesTree, [organisation]) |
93 parent.setFirstColumnSpanned(True) |
92 parent.setFirstColumnSpanned(True) |
94 else: |
93 else: |
95 parent = items[0] |
94 parent = items[0] |
96 |
95 |
97 itm = QTreeWidgetItem(parent, [commonName, expiryDate]) |
96 itm = QTreeWidgetItem(parent, [commonName, expiryDate]) |
98 itm.setData(0, self.CertRole, cert.toPem()) |
97 itm.setData(0, self.CertRole, cert.toPem()) |
99 |
98 |
100 @pyqtSlot() |
99 @pyqtSlot() |
101 def on_certificatesTree_itemSelectionChanged(self): |
100 def on_certificatesTree_itemSelectionChanged(self): |
102 """ |
101 """ |
103 Private slot to handle the selection of an item. |
102 Private slot to handle the selection of an item. |
104 """ |
103 """ |
105 enable = ( |
104 enable = ( |
106 len(self.certificatesTree.selectedItems()) > 0 and |
105 len(self.certificatesTree.selectedItems()) > 0 |
107 self.certificatesTree.selectedItems()[0].parent() is not None |
106 and self.certificatesTree.selectedItems()[0].parent() is not None |
108 ) |
107 ) |
109 self.buttonBox.button(QDialogButtonBox.OK).setEnabled(enable) |
108 self.buttonBox.button(QDialogButtonBox.OK).setEnabled(enable) |
110 self.viewButton.setEnabled(enable) |
109 self.viewButton.setEnabled(enable) |
111 |
110 |
112 @pyqtSlot() |
111 @pyqtSlot() |
113 def on_viewButton_clicked(self): |
112 def on_viewButton_clicked(self): |
114 """ |
113 """ |
115 Private slot to show data of the selected certificate. |
114 Private slot to show data of the selected certificate. |
116 """ |
115 """ |
117 with contextlib.suppress(ImportError): |
116 with contextlib.suppress(ImportError): |
118 from EricNetwork.EricSslCertificatesInfoDialog import ( |
117 from EricNetwork.EricSslCertificatesInfoDialog import ( |
119 EricSslCertificatesInfoDialog |
118 EricSslCertificatesInfoDialog, |
120 ) |
119 ) |
|
120 |
121 cert = QSslCertificate.fromData( |
121 cert = QSslCertificate.fromData( |
122 self.certificatesTree.selectedItems()[0].data( |
122 self.certificatesTree.selectedItems()[0].data(0, self.CertRole) |
123 0, self.CertRole)) |
123 ) |
124 dlg = EricSslCertificatesInfoDialog(cert, self) |
124 dlg = EricSslCertificatesInfoDialog(cert, self) |
125 dlg.exec() |
125 dlg.exec() |
126 |
126 |
127 def getSelectedCertificate(self): |
127 def getSelectedCertificate(self): |
128 """ |
128 """ |
129 Public method to get the selected certificate. |
129 Public method to get the selected certificate. |
130 |
130 |
131 @return selected certificate |
131 @return selected certificate |
132 @rtype QSslCertificate |
132 @rtype QSslCertificate |
133 """ |
133 """ |
134 valid = ( |
134 valid = ( |
135 len(self.certificatesTree.selectedItems()) > 0 and |
135 len(self.certificatesTree.selectedItems()) > 0 |
136 self.certificatesTree.selectedItems()[0].parent() is not None |
136 and self.certificatesTree.selectedItems()[0].parent() is not None |
137 ) |
137 ) |
138 |
138 |
139 certificate = ( |
139 certificate = ( |
140 QSslCertificate.fromData( |
140 QSslCertificate.fromData( |
141 self.certificatesTree.selectedItems()[0].data( |
141 self.certificatesTree.selectedItems()[0].data(0, self.CertRole) |
142 0, self.CertRole)) |
142 ) |
143 if valid else |
143 if valid |
144 None |
144 else None |
145 ) |
145 ) |
146 |
146 |
147 return certificate |
147 return certificate |