Continued implementing the certificate manager.

Tue, 14 Dec 2010 11:35:28 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 14 Dec 2010 11:35:28 +0100
changeset 753
e19a516f0a97
parent 752
2ec6ec7cd381
child 754
c3c3fec166f5

Continued implementing the certificate manager.

Helpviewer/Network/NetworkAccessManager.py file | annotate | diff | comparison | revisions
Helpviewer/SslCertificatesDialog.py file | annotate | diff | comparison | revisions
Helpviewer/SslInfoDialog.py file | annotate | diff | comparison | revisions
Helpviewer/UrlBar/UrlBar.py file | annotate | diff | comparison | revisions
Utilities/__init__.py file | annotate | diff | comparison | revisions
--- a/Helpviewer/Network/NetworkAccessManager.py	Tue Dec 14 09:37:17 2010 +0100
+++ b/Helpviewer/Network/NetworkAccessManager.py	Tue Dec 14 11:35:28 2010 +0100
@@ -281,13 +281,16 @@
         result = "<p>"
         
         result += self.trUtf8("Name: {0}")\
-            .format(cert.subjectInfo(QSslCertificate.CommonName))
+            .format(Utilities.decodeString(
+                cert.subjectInfo(QSslCertificate.CommonName)))
         
         result += self.trUtf8("<br/>Organization: {0}")\
-            .format(cert.subjectInfo(QSslCertificate.Organization))
+            .format(Utilities.decodeString(
+                cert.subjectInfo(QSslCertificate.Organization)))
         
         result += self.trUtf8("<br/>Issuer: {0}")\
-            .format(cert.issuerInfo(QSslCertificate.CommonName))
+            .format(Utilities.decodeString(
+                cert.issuerInfo(QSslCertificate.CommonName)))
         
         result += self.trUtf8("<br/>Not valid before: {0}<br/>Valid Until: {1}")\
             .format(cert.effectiveDate().toString("yyyy-MM-dd"), 
--- a/Helpviewer/SslCertificatesDialog.py	Tue Dec 14 09:37:17 2010 +0100
+++ b/Helpviewer/SslCertificatesDialog.py	Tue Dec 14 11:35:28 2010 +0100
@@ -7,7 +7,7 @@
 Module implementing a dialog to show and edit all certificates.
 """
 
-from PyQt4.QtCore import pyqtSlot, Qt, QByteArray
+from PyQt4.QtCore import pyqtSlot, Qt
 from PyQt4.QtGui import QDialog, QTreeWidgetItem
 try:
     from PyQt4.QtNetwork import QSslCertificate, QSslSocket, QSslConfiguration
@@ -21,6 +21,7 @@
 from .SslInfoDialog import SslInfoDialog
 
 import Preferences
+import Utilities
 
 class SslCertificatesDialog(QDialog, Ui_SslCertificatesDialog):
     """
@@ -62,10 +63,12 @@
         @param cert certificate to insert (QSslCertificate)
         """
         # step 1: extract the info to be shown
-        organisation = cert.subjectInfo(QSslCertificate.Organization)
+        organisation = Utilities.decodeString(
+            cert.subjectInfo(QSslCertificate.Organization))
         if organisation is None or organisation == "":
             organisation = self.trUtf8("(Unknown)")
-        commonName = cert.subjectInfo(QSslCertificate.CommonName)
+        commonName = Utilities.decodeString(
+            cert.subjectInfo(QSslCertificate.CommonName))
         if commonName is None or commonName == "":
             commonName = self.trUtf8("(Unknown common name)")
         expiryDate = cert.expiryDate().toString("yyyy-MM-dd")
@@ -167,12 +170,12 @@
         @param cert certificate to insert (QSslCertificate)
         """
         # step 1: extract the info to be shown
-        organisation = str(
-            QByteArray(cert.subjectInfo(QSslCertificate.Organization)), 
-            encoding = "utf-8")
+        organisation = Utilities.decodeString(
+            cert.subjectInfo(QSslCertificate.Organization))
         if organisation is None or organisation == "":
             organisation = self.trUtf8("(Unknown)")
-        commonName = cert.subjectInfo(QSslCertificate.CommonName)
+        commonName = Utilities.decodeString(
+            cert.subjectInfo(QSslCertificate.CommonName))
         if commonName is None or commonName == "":
             commonName = self.trUtf8("(Unknown common name)")
         expiryDate = cert.expiryDate().toString("yyyy-MM-dd")
--- a/Helpviewer/SslInfoDialog.py	Tue Dec 14 09:37:17 2010 +0100
+++ b/Helpviewer/SslInfoDialog.py	Tue Dec 14 11:35:28 2010 +0100
@@ -13,6 +13,8 @@
 
 from .Ui_SslInfoDialog import Ui_SslInfoDialog
 
+import Utilities
+
 class SslInfoDialog(QDialog, Ui_SslInfoDialog):
     """
     Class implementing a dialog to show SSL certificate infos.
@@ -57,7 +59,7 @@
         if txt is None or txt == "":
             return self.trUtf8("<not part of the certificate>")
         
-        return txt
+        return Utilities.decodeString(txt)
     
     def __serialNumber(self, cert):
         """
--- a/Helpviewer/UrlBar/UrlBar.py	Tue Dec 14 09:37:17 2010 +0100
+++ b/Helpviewer/UrlBar/UrlBar.py	Tue Dec 14 11:35:28 2010 +0100
@@ -23,6 +23,7 @@
 
 import UI.PixmapCache
 import Preferences
+import Utilities
 
 class UrlBar(E5LineEdit):
     """
@@ -139,9 +140,11 @@
         if ok and self.__browser.url().scheme() == "https":
             sslInfo = self.__browser.page().getSslInfo()
             if sslInfo is not None:
-                org = sslInfo.subjectInfo(QSslCertificate.Organization)
+                org = Utilities.decodeString(
+                    sslInfo.subjectInfo(QSslCertificate.Organization))
                 if org == "":
-                    cn = sslInfo.subjectInfo(QSslCertificate.CommonName)
+                    cn = Utilities.decodeString(
+                        sslInfo.subjectInfo(QSslCertificate.CommonName))
                     if cn != "":
                         org = cn.split(".", 1)[1]
                     if org == "":
--- a/Utilities/__init__.py	Tue Dec 14 09:37:17 2010 +0100
+++ b/Utilities/__init__.py	Tue Dec 14 11:35:28 2010 +0100
@@ -299,7 +299,46 @@
                         etext, encoding = text.encode('utf-8'), 'utf-8'
     
     return etext, encoding
+
+def decodeString(text):
+    """
+    Function to decode a string containing Unicode encoded characters.
     
+    @param text text containing encoded chars (string)
+    @return decoded text (string)
+    """
+    buf = b""
+    index = 0
+    while index < len(text):
+        if text[index] == "\\":
+            qb = QByteArray.fromHex(text[index:index + 4])
+            buf += bytes(qb)
+            index += 4
+        else:
+            buf += bytes(text[index], encoding="ascii")
+            index += 1
+    buf = buf.replace(b"\x00", b"")
+    
+    # try UTF-8
+    try:
+        return str(buf, encoding="utf-8")
+    except UnicodeError:
+        pass
+    
+    # try codec detection
+    try:
+        import ThirdParty.CharDet.chardet
+        guess = ThirdParty.CharDet.chardet.detect(buf)
+        if guess and guess['encoding'] is not None:
+            codec = guess['encoding'].lower()
+            return str(buf, codec)
+    except (UnicodeError, LookupError):
+        pass
+    except ImportError:
+        pass
+    
+    return str(text, "utf-8", "ignore")
+
 _escape = re.compile("[&<>\"\u0080-\uffff]")
 
 _escape_map = {

eric ide

mercurial