eric6/E5Network/E5SslCertificatesDialog.py

changeset 8143
2c730d5fd177
parent 7923
91e843545d9a
child 8218
7c09585bd960
equal deleted inserted replaced
8141:27f636beebad 8143:2c730d5fd177
30 30
31 class E5SslCertificatesDialog(QDialog, Ui_E5SslCertificatesDialog): 31 class E5SslCertificatesDialog(QDialog, Ui_E5SslCertificatesDialog):
32 """ 32 """
33 Class implementing a dialog to show and edit all certificates. 33 Class implementing a dialog to show and edit all certificates.
34 """ 34 """
35 CertRole = Qt.UserRole + 1 35 CertRole = Qt.ItemDataRole.UserRole + 1
36 36
37 def __init__(self, parent=None): 37 def __init__(self, parent=None):
38 """ 38 """
39 Constructor 39 Constructor
40 40
85 @param server server name of the certificate (string) 85 @param server server name of the certificate (string)
86 @param cert certificate to insert (QSslCertificate) 86 @param cert certificate to insert (QSslCertificate)
87 """ 87 """
88 # step 1: extract the info to be shown 88 # step 1: extract the info to be shown
89 organisation = Utilities.decodeString( 89 organisation = Utilities.decodeString(
90 ", ".join(cert.subjectInfo(QSslCertificate.Organization))) 90 ", ".join(cert.subjectInfo(
91 QSslCertificate.SubjectInfo.Organization)))
91 commonName = Utilities.decodeString( 92 commonName = Utilities.decodeString(
92 ", ".join(cert.subjectInfo(QSslCertificate.CommonName))) 93 ", ".join(cert.subjectInfo(
94 QSslCertificate.SubjectInfo.CommonName)))
93 if organisation is None or organisation == "": 95 if organisation is None or organisation == "":
94 organisation = self.tr("(Unknown)") 96 organisation = self.tr("(Unknown)")
95 if commonName is None or commonName == "": 97 if commonName is None or commonName == "":
96 commonName = self.tr("(Unknown common name)") 98 commonName = self.tr("(Unknown common name)")
97 expiryDate = cert.expiryDate().toString("yyyy-MM-dd") 99 expiryDate = cert.expiryDate().toString("yyyy-MM-dd")
98 100
99 # step 2: create the entry 101 # step 2: create the entry
100 items = self.serversCertificatesTree.findItems( 102 items = self.serversCertificatesTree.findItems(
101 organisation, 103 organisation,
102 Qt.MatchFixedString | Qt.MatchCaseSensitive) 104 Qt.MatchFlag.MatchFixedString | Qt.MatchFlag.MatchCaseSensitive)
103 if len(items) == 0: 105 if len(items) == 0:
104 parent = QTreeWidgetItem( 106 parent = QTreeWidgetItem(
105 self.serversCertificatesTree, [organisation]) 107 self.serversCertificatesTree, [organisation])
106 parent.setFirstColumnSpanned(True) 108 parent.setFirstColumnSpanned(True)
107 else: 109 else:
209 211
210 pems = QByteArray() 212 pems = QByteArray()
211 for cert in certs: 213 for cert in certs:
212 if cert in sCerts: 214 if cert in sCerts:
213 commonStr = ", ".join( 215 commonStr = ", ".join(
214 cert.subjectInfo(QSslCertificate.CommonName)) 216 cert.subjectInfo(
217 QSslCertificate.SubjectInfo.CommonName))
215 E5MessageBox.warning( 218 E5MessageBox.warning(
216 self, 219 self,
217 self.tr("Import Certificate"), 220 self.tr("Import Certificate"),
218 self.tr( 221 self.tr(
219 """<p>The certificate <b>{0}</b> already exists.""" 222 """<p>The certificate <b>{0}</b> already exists."""
281 self.__createCaCertificateEntry(cert) 284 self.__createCaCertificateEntry(cert)
282 285
283 self.caCertificatesTree.expandAll() 286 self.caCertificatesTree.expandAll()
284 for i in range(self.caCertificatesTree.columnCount()): 287 for i in range(self.caCertificatesTree.columnCount()):
285 self.caCertificatesTree.resizeColumnToContents(i) 288 self.caCertificatesTree.resizeColumnToContents(i)
286 self.caCertificatesTree.sortItems(0, Qt.AscendingOrder) 289 self.caCertificatesTree.sortItems(0, Qt.SortOrder.AscendingOrder)
287 290
288 def __createCaCertificateEntry(self, cert): 291 def __createCaCertificateEntry(self, cert):
289 """ 292 """
290 Private method to create a CA certificate entry. 293 Private method to create a CA certificate entry.
291 294
292 @param cert certificate to insert (QSslCertificate) 295 @param cert certificate to insert (QSslCertificate)
293 """ 296 """
294 # step 1: extract the info to be shown 297 # step 1: extract the info to be shown
295 organisation = Utilities.decodeString( 298 organisation = Utilities.decodeString(
296 ", ".join(cert.subjectInfo(QSslCertificate.Organization))) 299 ", ".join(cert.subjectInfo(
300 QSslCertificate.SubjectInfo.Organization)))
297 commonName = Utilities.decodeString( 301 commonName = Utilities.decodeString(
298 ", ".join(cert.subjectInfo(QSslCertificate.CommonName))) 302 ", ".join(cert.subjectInfo(
303 QSslCertificate.SubjectInfo.CommonName)))
299 if organisation is None or organisation == "": 304 if organisation is None or organisation == "":
300 organisation = self.tr("(Unknown)") 305 organisation = self.tr("(Unknown)")
301 if commonName is None or commonName == "": 306 if commonName is None or commonName == "":
302 commonName = self.tr("(Unknown common name)") 307 commonName = self.tr("(Unknown common name)")
303 expiryDate = cert.expiryDate().toString("yyyy-MM-dd") 308 expiryDate = cert.expiryDate().toString("yyyy-MM-dd")
304 309
305 # step 2: create the entry 310 # step 2: create the entry
306 items = self.caCertificatesTree.findItems( 311 items = self.caCertificatesTree.findItems(
307 organisation, 312 organisation,
308 Qt.MatchFixedString | Qt.MatchCaseSensitive) 313 Qt.MatchFlag.MatchFixedString | Qt.MatchFlag.MatchCaseSensitive)
309 if len(items) == 0: 314 if len(items) == 0:
310 parent = QTreeWidgetItem(self.caCertificatesTree, [organisation]) 315 parent = QTreeWidgetItem(self.caCertificatesTree, [organisation])
311 parent.setFirstColumnSpanned(True) 316 parent.setFirstColumnSpanned(True)
312 else: 317 else:
313 parent = items[0] 318 parent = items[0]
393 if certs: 398 if certs:
394 caCerts = self.__getSystemCaCertificates() 399 caCerts = self.__getSystemCaCertificates()
395 for cert in certs: 400 for cert in certs:
396 if cert in caCerts: 401 if cert in caCerts:
397 commonStr = ", ".join( 402 commonStr = ", ".join(
398 cert.subjectInfo(QSslCertificate.CommonName)) 403 cert.subjectInfo(
404 QSslCertificate.SubjectInfo.CommonName))
399 E5MessageBox.warning( 405 E5MessageBox.warning(
400 self, 406 self,
401 self.tr("Import Certificate"), 407 self.tr("Import Certificate"),
402 self.tr( 408 self.tr(
403 """<p>The certificate <b>{0}</b> already exists.""" 409 """<p>The certificate <b>{0}</b> already exists."""
461 icon=E5MessageBox.Warning) 467 icon=E5MessageBox.Warning)
462 if not res: 468 if not res:
463 return 469 return
464 470
465 f = QFile(fname) 471 f = QFile(fname)
466 if not f.open(QIODevice.WriteOnly): 472 if not f.open(QIODevice.OpenModeFlag.WriteOnly):
467 E5MessageBox.critical( 473 E5MessageBox.critical(
468 self, 474 self,
469 self.tr("Export Certificate"), 475 self.tr("Export Certificate"),
470 self.tr( 476 self.tr(
471 """<p>The certificate could not be written""" 477 """<p>The certificate could not be written"""
493 self.tr("Certificate Files (*.pem *.crt *.der *.cer *.ca);;" 499 self.tr("Certificate Files (*.pem *.crt *.der *.cer *.ca);;"
494 "All Files (*)")) 500 "All Files (*)"))
495 501
496 if fname: 502 if fname:
497 f = QFile(fname) 503 f = QFile(fname)
498 if not f.open(QIODevice.ReadOnly): 504 if not f.open(QIODevice.OpenModeFlag.ReadOnly):
499 E5MessageBox.critical( 505 E5MessageBox.critical(
500 self, 506 self,
501 self.tr("Export Certificate"), 507 self.tr("Export Certificate"),
502 self.tr( 508 self.tr(
503 """<p>The certificate could not be read from file""" 509 """<p>The certificate could not be read from file"""
505 .format(fname, f.errorString())) 511 .format(fname, f.errorString()))
506 return [] 512 return []
507 513
508 crt = f.readAll() 514 crt = f.readAll()
509 f.close() 515 f.close()
510 cert = QSslCertificate.fromData(crt, QSsl.Pem) 516 cert = QSslCertificate.fromData(crt, QSsl.EncodingFormat.Pem)
511 if not cert: 517 if not cert:
512 cert = QSslCertificate.fromData(crt, QSsl.Der) 518 cert = QSslCertificate.fromData(crt, QSsl.EncodingFormat.Der)
513 519
514 return cert 520 return cert
515 521
516 return [] 522 return []

eric ide

mercurial