eric6/Helpviewer/FlashCookieManager/FlashCookieManager.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Flash cookie manager.
8 """
9
10 from __future__ import unicode_literals
11
12 try:
13 str = unicode # __IGNORE_EXCEPTION__
14 except NameError:
15 pass
16
17 import shutil
18
19 from PyQt5.QtCore import QObject, QTimer, QDir, QFileInfo, QFile
20
21 from .FlashCookie import FlashCookie
22 from .FlashCookieReader import FlashCookieReader, FlashCookieReaderError
23
24 import Helpviewer.HelpWindow
25
26 import Preferences
27
28
29 class FlashCookieManager(QObject):
30 """
31 Class implementing the Flash cookie manager object.
32 """
33 RefreshInterval = 60 * 1000
34
35 def __init__(self, parent=None):
36 """
37 Constructor
38
39 @param parent reference to the parent object
40 @type QObject
41 """
42 super(FlashCookieManager, self).__init__(parent)
43
44 self.__flashCookieManagerDialog = None
45 self.__flashCookies = [] # list of FlashCookie
46 self.__newCookiesList = [] # list of str
47 self.__whitelist = [] # list of str
48 self.__blacklist = [] # list of str
49
50 self.__timer = QTimer(self)
51 self.__timer.setInterval(FlashCookieManager.RefreshInterval)
52 self.__timer.timeout.connect(self.__autoRefresh)
53
54 # start the timer if needed
55 self.__startStopTimer()
56
57 if Preferences.getHelp("FlashCookiesDeleteOnStartExit"):
58 self.__loadFlashCookies()
59 self.__removeAllButWhitelisted()
60
61 def shutdown(self):
62 """
63 Public method to perform shutdown actions.
64 """
65 if self.__flashCookieManagerDialog is not None:
66 self.__flashCookieManagerDialog.close()
67
68 if Preferences.getHelp("FlashCookiesDeleteOnStartExit"):
69 self.__removeAllButWhitelisted()
70
71 def setFlashCookies(self, cookies):
72 """
73 Public method to set the list of cached Flash cookies.
74
75 @param cookies list of Flash cookies to store
76 @type list of FlashCookie
77 """
78 self.__flashCookies = cookies[:]
79
80 def flashCookies(self):
81 """
82 Public method to get the list of cached Flash cookies.
83
84 @return list of Flash cookies
85 @rtype list of FlashCookie
86 """
87 if not self.__flashCookies:
88 self.__loadFlashCookies()
89
90 return self.__flashCookies[:]
91
92 def newCookiesList(self):
93 """
94 Public method to get the list of newly detected Flash cookies.
95
96 @return list of newly detected Flash cookies
97 @rtype list of str
98 """
99 return self.__newCookiesList[:]
100
101 def clearNewOrigins(self):
102 """
103 Public method to clear the list of newly detected Flash cookies.
104 """
105 self.__newCookiesList = []
106
107 def clearCache(self):
108 """
109 Public method to clear the list of cached Flash cookies.
110 """
111 self.__flashCookies = []
112
113 def __isBlacklisted(self, cookie):
114 """
115 Private method to check for a blacklisted cookie.
116
117 @param cookie Flash cookie to be tested
118 @type FlashCookie
119 @return flag indicating a blacklisted cookie
120 @rtype bool
121 """
122 return cookie.origin in Preferences.getHelp("FlashCookiesBlacklist")
123
124 def __isWhitelisted(self, cookie):
125 """
126 Private method to check for a whitelisted cookie.
127
128 @param cookie Flash cookie to be tested
129 @type FlashCookie
130 @return flag indicating a whitelisted cookie
131 @rtype bool
132 """
133 return cookie.origin in Preferences.getHelp("FlashCookiesWhitelist")
134
135 def __removeAllButWhitelisted(self):
136 """
137 Private method to remove all non-whitelisted cookies.
138 """
139 for cookie in self.__flashCookies[:]:
140 if not self.__isWhitelisted(cookie):
141 self.removeCookie(cookie)
142
143 def __sharedObjectDirName(self):
144 """
145 Private slot to determine the path of the shared data objects.
146
147 @return path of the shared data objects
148 @rtype str
149 """
150 if "macromedia" in self.flashPlayerDataPath().lower() or \
151 "/.gnash" not in self.flashPlayerDataPath().lower():
152 return "/#SharedObjects/"
153 else:
154 return "/SharedObjects/"
155
156 def flashPlayerDataPath(self):
157 """
158 Public method to get the Flash Player data path.
159
160 @return Flash Player data path
161 @rtype str
162 """
163 return Preferences.getHelp("FlashCookiesDataPath")
164
165 def preferencesChanged(self):
166 """
167 Public slot to handle a change of preferences.
168 """
169 self.__startStopTimer()
170
171 def removeCookie(self, cookie):
172 """
173 Public method to remove a cookie of the list of cached cookies.
174
175 @param cookie Flash cookie to be removed
176 @type FlashCookie
177 """
178 if cookie in self.__flashCookies:
179 self.__flashCookies.remove(cookie)
180 shutil.rmtree(cookie.path, True)
181
182 def __autoRefresh(self):
183 """
184 Private slot to refresh the list of cookies.
185 """
186 if self.__flashCookieManagerDialog and \
187 self.__flashCookieManagerDialog.isVisible():
188 return
189
190 oldFlashCookies = self.__flashCookies[:]
191 self.__loadFlashCookies()
192 newCookieList = []
193
194 for cookie in self.__flashCookies[:]:
195 if self.__isBlacklisted(cookie):
196 self.removeCookie(cookie)
197 continue
198
199 if self.__isWhitelisted(cookie):
200 continue
201
202 newCookie = True
203 for oldCookie in oldFlashCookies:
204 if (oldCookie.path + oldCookie.name ==
205 cookie.path + cookie.name):
206 newCookie = False
207 break
208
209 if newCookie:
210 newCookieList.append(cookie.path + "/" + cookie.name)
211
212 if newCookieList and Preferences.getHelp("FlashCookieNotify"):
213 self.__newCookiesList.extend(newCookieList)
214 win = Helpviewer.HelpWindow.HelpWindow.mainWindow()
215 if win is None:
216 return
217
218 view = win.currentBrowser()
219 if view is None:
220 return
221
222 from .FlashCookieNotification import FlashCookieNotification
223 notification = FlashCookieNotification(
224 view, self, len(newCookieList))
225 notification.show()
226
227 def showFlashCookieManagerDialog(self):
228 """
229 Public method to show the Flash cookies management dialog.
230 """
231 if self.__flashCookieManagerDialog is None:
232 from .FlashCookieManagerDialog import FlashCookieManagerDialog
233 self.__flashCookieManagerDialog = FlashCookieManagerDialog(self)
234
235 self.__flashCookieManagerDialog.refreshView()
236 self.__flashCookieManagerDialog.showPage(0)
237 self.__flashCookieManagerDialog.show()
238 self.__flashCookieManagerDialog.raise_()
239
240 def __startStopTimer(self):
241 """
242 Private slot to start or stop the auto refresh timer.
243 """
244 if Preferences.getHelp("FlashCookieAutoRefresh"):
245 if not self.__timer.isActive():
246 if not bool(self.__flashCookies):
247 self.__loadFlashCookies()
248
249 self.__timer.start()
250 else:
251 self.__timer.stop()
252
253 def __loadFlashCookies(self):
254 """
255 Private slot to load the Flash cookies to be cached.
256 """
257 self.__flashCookies = []
258 self.__loadFlashCookiesFromPath(self.flashPlayerDataPath())
259
260 def __loadFlashCookiesFromPath(self, path):
261 """
262 Private slot to load the Flash cookies from a path.
263
264 @param path Flash cookies path
265 @type str
266 """
267 if path.endswith("#AppContainer"):
268 # specific to IE and Windows
269 return
270
271 path = path.replace("\\", "/")
272 solDir = QDir(path)
273 entryList = solDir.entryList()
274 for entry in entryList:
275 if entry == "." or entry == "..":
276 continue
277 entryInfo = QFileInfo(path + "/" + entry)
278 if entryInfo.isDir():
279 self.__loadFlashCookiesFromPath(entryInfo.filePath())
280 else:
281 self.__insertFlashCookie(entryInfo.filePath())
282
283 def __insertFlashCookie(self, path):
284 """
285 Private method to insert a Flash cookie into the cache.
286
287 @param path Flash cookies path
288 @type str
289 """
290 solFile = QFile(path)
291 if not solFile.open(QFile.ReadOnly):
292 return
293
294 dataStr = ""
295 data = bytes(solFile.readAll())
296 if data:
297 try:
298 reader = FlashCookieReader()
299 reader.setBytes(data)
300 reader.parse()
301 dataStr = reader.toString()
302 except FlashCookieReaderError as err:
303 dataStr = err.msg
304
305 solFileInfo = QFileInfo(solFile)
306
307 cookie = FlashCookie()
308 cookie.contents = dataStr
309 cookie.name = solFileInfo.fileName()
310 cookie.path = solFileInfo.canonicalPath()
311 cookie.size = int(solFile.size())
312 cookie.lastModified = solFileInfo.lastModified()
313 cookie.origin = self.__extractOriginFrom(path)
314
315 self.__flashCookies.append(cookie)
316
317 def __extractOriginFrom(self, path):
318 """
319 Private method to extract the cookie origin given its file name.
320
321 @param path file name of the cookie file
322 @type str
323 @return cookie origin
324 @rtype str
325 """
326 origin = path
327 if path.startswith(
328 self.flashPlayerDataPath() + self.__sharedObjectDirName()):
329 origin = origin.replace(
330 self.flashPlayerDataPath() + self.__sharedObjectDirName(), "")
331 if "/" in origin:
332 origin = origin.split("/", 1)[1]
333 elif path.startswith(
334 self.flashPlayerDataPath() +
335 "/macromedia.com/support/flashplayer/sys/"):
336 origin = origin.replace(
337 self.flashPlayerDataPath() +
338 "/macromedia.com/support/flashplayer/sys/", "")
339 if origin == "settings.sol":
340 return self.tr("!default")
341 elif origin.startswith("#"):
342 origin = origin[1:]
343 else:
344 origin = ""
345
346 index = origin.find("/")
347 if index == -1:
348 return self.tr("!other")
349
350 origin = origin[:index]
351 if origin in ["localhost", "local"]:
352 origin = "!localhost"
353
354 return origin

eric ide

mercurial