|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module containing a web site icon storage object. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import json |
|
13 import os |
|
14 |
|
15 from PyQt5.QtCore import pyqtSignal, QObject, QByteArray, QBuffer, QIODevice, \ |
|
16 QUrl |
|
17 from PyQt5.QtGui import QIcon, QPixmap, QImage |
|
18 from PyQt5.QtWidgets import QDialog |
|
19 |
|
20 from Utilities.AutoSaver import AutoSaver |
|
21 |
|
22 import UI.PixmapCache |
|
23 |
|
24 |
|
25 class WebIconProvider(QObject): |
|
26 """ |
|
27 Class implementing a web site icon storage. |
|
28 |
|
29 @signal changed() emitted to indicate a change of the icons database |
|
30 """ |
|
31 changed = pyqtSignal() |
|
32 |
|
33 def __init__(self, parent=None): |
|
34 """ |
|
35 Constructor |
|
36 |
|
37 @param parent reference to the parent object (QObject) |
|
38 """ |
|
39 super(WebIconProvider, self).__init__(parent) |
|
40 |
|
41 self.__encoding = "iso-8859-1" |
|
42 self.__iconsFileName = "web_site_icons.json" |
|
43 self.__iconDatabasePath = "" # saving of icons disabled |
|
44 |
|
45 self.__iconsDB = {} |
|
46 self.__loaded = False |
|
47 |
|
48 self.__saveTimer = AutoSaver(self, self.save) |
|
49 |
|
50 self.changed.connect(self.__saveTimer.changeOccurred) |
|
51 |
|
52 def setIconDatabasePath(self, path): |
|
53 """ |
|
54 Public method to set the path for the web site icons store. |
|
55 |
|
56 @param path path to store the icons file to |
|
57 @type str |
|
58 """ |
|
59 if path != self.__iconDatabasePath: |
|
60 self.close() |
|
61 |
|
62 self.__iconDatabasePath = path |
|
63 |
|
64 def iconDatabasePath(self): |
|
65 """ |
|
66 Public method o get the path for the web site icons store. |
|
67 |
|
68 @return path to store the icons file to |
|
69 @rtype str |
|
70 """ |
|
71 return self.__iconDatabasePath |
|
72 |
|
73 def close(self): |
|
74 """ |
|
75 Public method to close the web icon provider. |
|
76 """ |
|
77 self.__saveTimer.saveIfNeccessary() |
|
78 self.__loaded = False |
|
79 self.__iconsDB = {} |
|
80 |
|
81 def load(self): |
|
82 """ |
|
83 Public method to load the bookmarks. |
|
84 """ |
|
85 if self.__loaded: |
|
86 return |
|
87 |
|
88 if self.__iconDatabasePath: |
|
89 filename = os.path.join(self.__iconDatabasePath, |
|
90 self.__iconsFileName) |
|
91 try: |
|
92 f = open(filename, "r") |
|
93 db = json.load(f) |
|
94 f.close() |
|
95 except (IOError, OSError): |
|
96 # ignore silentyl |
|
97 db = {} |
|
98 |
|
99 self.__iconsDB = {} |
|
100 for url, data in db.items(): |
|
101 self.__iconsDB[url] = QIcon(QPixmap.fromImage(QImage.fromData( |
|
102 QByteArray(data.encode(self.__encoding))))) |
|
103 |
|
104 self.__loaded = True |
|
105 |
|
106 def save(self): |
|
107 """ |
|
108 Public method to save the zoom values. |
|
109 """ |
|
110 if not self.__loaded: |
|
111 return |
|
112 |
|
113 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
114 if not WebBrowserWindow.isPrivate() and bool(self.__iconDatabasePath): |
|
115 db = {} |
|
116 for url, icon in self.__iconsDB.items(): |
|
117 ba = QByteArray() |
|
118 buffer = QBuffer(ba) |
|
119 buffer.open(QIODevice.WriteOnly) |
|
120 icon.pixmap(32).toImage().save(buffer, "PNG") |
|
121 db[url] = bytes(buffer.data()).decode(self.__encoding) |
|
122 |
|
123 filename = os.path.join(self.__iconDatabasePath, |
|
124 self.__iconsFileName) |
|
125 try: |
|
126 f = open(filename, "w") |
|
127 json.dump(db, f) |
|
128 f.close() |
|
129 except (IOError, OSError): |
|
130 # ignore silentyl |
|
131 pass |
|
132 |
|
133 def saveIcon(self, view): |
|
134 """ |
|
135 Public method to save a web site icon. |
|
136 |
|
137 @param view reference to the view object |
|
138 @type WebBrowserView |
|
139 """ |
|
140 scheme = view.url().scheme() |
|
141 if scheme in ["eric", "about", "qthelp", "file", "abp", "ftp"]: |
|
142 return |
|
143 |
|
144 self.load() |
|
145 |
|
146 if view.mainWindow().isPrivate(): |
|
147 return |
|
148 |
|
149 urlStr = self.__urlToString(view.url()) |
|
150 self.__iconsDB[urlStr] = view.icon() |
|
151 |
|
152 self.changed.emit() |
|
153 |
|
154 def __urlToString(self, url): |
|
155 """ |
|
156 Private method to convert an URL to a string. |
|
157 |
|
158 @param url URL to be converted |
|
159 @type QUrl |
|
160 @return string representation of the URL |
|
161 @rtype str |
|
162 """ |
|
163 return url.toString(QUrl.PrettyDecoded | QUrl.RemoveUserInfo | |
|
164 QUrl.RemoveFragment) |
|
165 |
|
166 def iconForUrl(self, url): |
|
167 """ |
|
168 Public method to get an icon for an URL. |
|
169 |
|
170 @param url URL to get icon for |
|
171 @type QUrl |
|
172 @return icon for the URL |
|
173 @rtype QIcon |
|
174 """ |
|
175 scheme = url.scheme() |
|
176 if scheme in ["eric", "about"]: |
|
177 return UI.PixmapCache.getIcon("ericWeb.png") |
|
178 elif scheme == "qthelp": |
|
179 return UI.PixmapCache.getIcon("qthelp.png") |
|
180 elif scheme == "file": |
|
181 return UI.PixmapCache.getIcon("fileMisc.png") |
|
182 elif scheme == "abp": |
|
183 return UI.PixmapCache.getIcon("adBlockPlus.png") |
|
184 elif scheme == "ftp": |
|
185 return UI.PixmapCache.getIcon("network-server.png") |
|
186 |
|
187 self.load() |
|
188 |
|
189 urlStr = self.__urlToString(url) |
|
190 for iconUrlStr in self.__iconsDB: |
|
191 if iconUrlStr.startswith(urlStr): |
|
192 return self.__iconsDB[iconUrlStr] |
|
193 |
|
194 # try replacing http scheme with https scheme |
|
195 url = QUrl(url) |
|
196 if url.scheme() == "http": |
|
197 url.setScheme("https") |
|
198 urlStr = self.__urlToString(url) |
|
199 for iconUrlStr in self.__iconsDB: |
|
200 if iconUrlStr.startswith(urlStr): |
|
201 return self.__iconsDB[iconUrlStr] |
|
202 |
|
203 if scheme == "https": |
|
204 return UI.PixmapCache.getIcon("securityHigh32.png") |
|
205 else: |
|
206 return UI.PixmapCache.getIcon("defaultIcon.png") |
|
207 |
|
208 def clear(self): |
|
209 """ |
|
210 Public method to clear the icons cache. |
|
211 """ |
|
212 self.load() |
|
213 self.__iconsDB = {} |
|
214 self.changed.emit() |
|
215 self.__saveTimer.saveIfNeccessary() |
|
216 |
|
217 def showWebIconDialog(self): |
|
218 """ |
|
219 Public method to show a dialog to manage the Favicons. |
|
220 """ |
|
221 self.load() |
|
222 |
|
223 from .WebIconDialog import WebIconDialog |
|
224 dlg = WebIconDialog(self.__iconsDB) |
|
225 if dlg.exec_() == QDialog.Accepted: |
|
226 changed = False |
|
227 urls = dlg.getUrls() |
|
228 for url in list(self.__iconsDB.keys())[:]: |
|
229 if url not in urls: |
|
230 del self.__iconsDB[url] |
|
231 changed = True |
|
232 if changed: |
|
233 self.changed.emit() |
|
234 |
|
235 |
|
236 _WebIconProvider = None |
|
237 |
|
238 |
|
239 def instance(): |
|
240 """ |
|
241 Global function to get a reference to the web icon provider and create it, |
|
242 if it hasn't been yet. |
|
243 |
|
244 @return reference to the web icon provider object |
|
245 @rtype WebIconProvider |
|
246 """ |
|
247 global _WebIconProvider |
|
248 |
|
249 if _WebIconProvider is None: |
|
250 _WebIconProvider = WebIconProvider() |
|
251 |
|
252 return _WebIconProvider |