eric6/WebBrowser/Tools/WebIconLoader.py

Tue, 06 Oct 2020 19:47:05 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 06 Oct 2020 19:47:05 +0200
changeset 7764
1804cdcedac7
parent 7565
928373562e36
child 7766
0af772bc14c4
permissions
-rw-r--r--

WebBrowser: started to implement retrieval of SSL data as a by-product of loading a site icon.

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

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

"""
Module implementing an object to load web site icons.
"""


from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
from PyQt5.QtGui import QIcon, QPixmap, QImage
from PyQt5.QtNetwork import QNetworkRequest, QSslConfiguration

import WebBrowser.WebBrowserWindow


class WebIconLoader(QObject):
    """
    Class implementing a loader for web site icons.
    
    @signal iconLoaded(icon) emitted when the icon has been loaded
    @signal sslConfiguration(config) emitted to pass the SSL data
    """
    iconLoaded = pyqtSignal(QIcon)
    sslConfiguration = pyqtSignal(QSslConfiguration)
    
    def __init__(self, url, parent=None):
        """
        Constructor
        
        @param url URL to fetch the icon from
        @type QUrl
        @param parent reference to the parent object
        @type QObject
        """
        super(WebIconLoader, self).__init__(parent)
        
        networkManager = (
            WebBrowser.WebBrowserWindow.WebBrowserWindow.networkManager()
        )
        self.__reply = networkManager.get(QNetworkRequest(url))
        self.__reply.finished.connect(self.__finished)
    
    @pyqtSlot()
    def __finished(self):
        """
        Private slot handling the downloaded icon.
        """
        # ignore any errors and emit an empty icon in this case
        data = self.__reply.readAll()
        icon = QIcon(QPixmap.fromImage(QImage.fromData(data)))
        self.iconLoaded.emit(icon)
        
        # TODO: extract SSL data as a by-product
        if self.__reply.url().scheme().lower() == "https":
            sslConfiguration = self.__reply.sslConfiguration()
            self.sslConfiguration.emit(sslConfiguration)
        
        self.__reply.deleteLater()
        self.__reply = None

eric ide

mercurial