|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an object to load web site icons. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject |
|
11 from PyQt5.QtGui import QIcon, QPixmap, QImage |
|
12 from PyQt5.QtNetwork import QNetworkRequest, QSslConfiguration |
|
13 |
|
14 try: |
|
15 from PyQt5.QtNetwork import QSslConfiguration # __IGNORE_WARNING__ |
|
16 SSL_AVAILABLE = True |
|
17 except ImportError: |
|
18 SSL_AVAILABLE = False |
|
19 |
|
20 import WebBrowser.WebBrowserWindow |
|
21 |
|
22 |
|
23 class WebIconLoader(QObject): |
|
24 """ |
|
25 Class implementing a loader for web site icons. |
|
26 |
|
27 @signal iconLoaded(icon) emitted when the icon has been loaded |
|
28 @signal sslConfiguration(config) emitted to pass the SSL data |
|
29 @signal clearSslConfiguration() emitted to clear stored SSL data |
|
30 """ |
|
31 iconLoaded = pyqtSignal(QIcon) |
|
32 if SSL_AVAILABLE: |
|
33 sslConfiguration = pyqtSignal(QSslConfiguration) |
|
34 clearSslConfiguration = pyqtSignal() |
|
35 |
|
36 def __init__(self, url, parent=None): |
|
37 """ |
|
38 Constructor |
|
39 |
|
40 @param url URL to fetch the icon from |
|
41 @type QUrl |
|
42 @param parent reference to the parent object |
|
43 @type QObject |
|
44 """ |
|
45 super().__init__(parent) |
|
46 |
|
47 networkManager = ( |
|
48 WebBrowser.WebBrowserWindow.WebBrowserWindow.networkManager() |
|
49 ) |
|
50 self.__reply = networkManager.get(QNetworkRequest(url)) |
|
51 self.__reply.finished.connect(self.__finished) |
|
52 |
|
53 @pyqtSlot() |
|
54 def __finished(self): |
|
55 """ |
|
56 Private slot handling the downloaded icon. |
|
57 """ |
|
58 # ignore any errors and emit an empty icon in this case |
|
59 data = self.__reply.readAll() |
|
60 icon = QIcon(QPixmap.fromImage(QImage.fromData(data))) |
|
61 self.iconLoaded.emit(icon) |
|
62 |
|
63 if SSL_AVAILABLE: |
|
64 if self.__reply.url().scheme().lower() == "https": |
|
65 sslConfiguration = self.__reply.sslConfiguration() |
|
66 self.sslConfiguration.emit(sslConfiguration) |
|
67 else: |
|
68 self.clearSslConfiguration.emit() |
|
69 |
|
70 self.__reply.deleteLater() |
|
71 self.__reply = None |