|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 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 |
|
19 from Utilities.AutoSaver import AutoSaver |
|
20 |
|
21 import UI.PixmapCache |
|
22 |
|
23 |
|
24 class WebIconProvider(QObject): |
|
25 """ |
|
26 Class implementing a web site icon storage. |
|
27 """ |
|
28 changed = pyqtSignal() |
|
29 |
|
30 def __init__(self, parent=None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param parent reference to the parent object (QObject) |
|
35 """ |
|
36 super(WebIconProvider, self).__init__(parent) |
|
37 |
|
38 self.__encoding = "iso-8859-1" |
|
39 self.__iconsFileName = "web_site_icons.json" |
|
40 self.__iconDatabasePath = "" # saving of icons disabled |
|
41 |
|
42 self.__iconsDB = {} |
|
43 self.__loaded = False |
|
44 |
|
45 self.__saveTimer = AutoSaver(self, self.save) |
|
46 |
|
47 self.changed.connect(self.__saveTimer.changeOccurred) |
|
48 |
|
49 def setIconDatabasePath(self, path): |
|
50 """ |
|
51 Public method to set the path for the web site icons store. |
|
52 |
|
53 @param path path to store the icons file to |
|
54 @type str |
|
55 """ |
|
56 if path != self.__iconDatabasePath: |
|
57 self.close() |
|
58 |
|
59 self.__iconDatabasePath = path |
|
60 |
|
61 def iconDatabasePath(self): |
|
62 """ |
|
63 Public method o get the path for the web site icons store. |
|
64 |
|
65 @return path to store the icons file to |
|
66 @rtype str |
|
67 """ |
|
68 return self.__iconDatabasePath |
|
69 |
|
70 def close(self): |
|
71 """ |
|
72 Public method to close the web icon provider. |
|
73 """ |
|
74 self.__saveTimer.saveIfNeccessary() |
|
75 self.__loaded = False |
|
76 self.__iconsDB = {} |
|
77 |
|
78 def load(self): |
|
79 """ |
|
80 Public method to load the bookmarks. |
|
81 """ |
|
82 if self.__loaded: |
|
83 return |
|
84 |
|
85 if self.__iconDatabasePath: |
|
86 filename = os.path.join(self.__iconDatabasePath, |
|
87 self.__iconsFileName) |
|
88 try: |
|
89 f = open(filename, "r") |
|
90 db = json.load(f) |
|
91 f.close() |
|
92 except (IOError, OSError): |
|
93 # ignore silentyl |
|
94 db = {} |
|
95 |
|
96 self.__iconsDB = {} |
|
97 for url, data in db.items(): |
|
98 self.__iconsDB[url] = QIcon(QPixmap.fromImage(QImage.fromData( |
|
99 QByteArray(data.encode(self.__encoding))))) |
|
100 |
|
101 self.__loaded = True |
|
102 |
|
103 def save(self): |
|
104 """ |
|
105 Public method to save the zoom values. |
|
106 """ |
|
107 if not self.__loaded: |
|
108 return |
|
109 |
|
110 if self.__iconDatabasePath: |
|
111 db = {} |
|
112 for url, icon in self.__iconsDB.items(): |
|
113 ba = QByteArray() |
|
114 buffer = QBuffer(ba) |
|
115 buffer.open(QIODevice.WriteOnly) |
|
116 icon.pixmap(32).toImage().save(buffer, "PNG") |
|
117 db[url] = bytes(buffer.data()).decode(self.__encoding) |
|
118 |
|
119 filename = os.path.join(self.__iconDatabasePath, |
|
120 self.__iconsFileName) |
|
121 try: |
|
122 f = open(filename, "w") |
|
123 json.dump(db, f) |
|
124 f.close() |
|
125 except (IOError, OSError): |
|
126 # ignore silentyl |
|
127 pass |
|
128 |
|
129 def saveIcon(self, view): |
|
130 """ |
|
131 Public method to save a web site icon. |
|
132 |
|
133 @param view reference to the view object |
|
134 @type WebBrowserView |
|
135 """ |
|
136 scheme = view.url().scheme() |
|
137 if scheme in ["eric", "about", "qthelp", "file", "abp", "ftp"]: |
|
138 return |
|
139 |
|
140 self.load() |
|
141 |
|
142 if view.mainWindow().isPrivate(): |
|
143 return |
|
144 |
|
145 urlStr = self.__urlToString(view.url()) |
|
146 self.__iconsDB[urlStr] = view.icon() |
|
147 |
|
148 self.changed.emit() |
|
149 |
|
150 def __urlToString(self, url): |
|
151 """ |
|
152 Private method to convert an URL to a string. |
|
153 |
|
154 @param url URL to be converted |
|
155 @type QUrl |
|
156 @return string representation of the URL |
|
157 @rtype str |
|
158 """ |
|
159 return url.toString(QUrl.PrettyDecoded | QUrl.RemoveUserInfo | |
|
160 QUrl.RemoveFragment) |
|
161 |
|
162 def iconForUrl(self, url): |
|
163 """ |
|
164 Public method to get an icon for an URL. |
|
165 |
|
166 @param url URL to get icon for |
|
167 @type QUrl |
|
168 @return icon for the URL |
|
169 @rtype QIcon |
|
170 """ |
|
171 scheme = url.scheme() |
|
172 if scheme in ["eric", "about"]: |
|
173 return UI.PixmapCache.getIcon("ericWeb.png") |
|
174 elif scheme == "qthelp": |
|
175 return UI.PixmapCache.getIcon("qthelp.png") |
|
176 elif scheme == "file": |
|
177 return UI.PixmapCache.getIcon("fileMisc.png") |
|
178 elif scheme == "abp": |
|
179 return UI.PixmapCache.getIcon("adBlockPlus.png") |
|
180 elif scheme == "ftp": |
|
181 return UI.PixmapCache.getIcon("network-server.png") |
|
182 |
|
183 self.load() |
|
184 |
|
185 urlStr = self.__urlToString(url) |
|
186 if url in self.__iconsDB: |
|
187 return self.__iconsDB[urlStr] |
|
188 else: |
|
189 return UI.PixmapCache.getIcon("defaultIcon.png") |
|
190 |
|
191 |
|
192 __WebIconProvider = None |
|
193 |
|
194 |
|
195 def instance(): |
|
196 """ |
|
197 Global function to get a reference to the web icon provider and create it, |
|
198 if it hasn't been yet. |
|
199 |
|
200 @return reference to the web icon provider object |
|
201 @rtype WebIconProvider |
|
202 """ |
|
203 global __WebIconProvider |
|
204 |
|
205 if __WebIconProvider is None: |
|
206 __WebIconProvider = WebIconProvider() |
|
207 |
|
208 return __WebIconProvider |