|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Flash blocker plug-in. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtGui import QDialog |
|
11 from PyQt4.QtWebKit import QWebPluginFactory |
|
12 |
|
13 from ..WebPluginInterface import WebPluginInterface |
|
14 |
|
15 from .ClickToFlash import ClickToFlash |
|
16 from .ClickToFlashWhitelistDialog import ClickToFlashWhitelistDialog |
|
17 |
|
18 import Preferences |
|
19 |
|
20 |
|
21 class ClickToFlashPlugin(WebPluginInterface): |
|
22 """ |
|
23 Class implementing the flash blocker plug-in. |
|
24 """ |
|
25 ClickToFlashData = { |
|
26 "application/x-shockwave-flash" : { |
|
27 "extensions": ["swf"], |
|
28 "icon": "flashBlock-flash.png" |
|
29 }, |
|
30 "application/futuresplash" : { |
|
31 "extensions": ["spl"], |
|
32 "icon": "flashBlock-flash.png" |
|
33 }, |
|
34 "application/x-director" : { |
|
35 "extensions": ["dir", "dcr", "dxr"], |
|
36 "icon": "flashBlock-director.png" |
|
37 }, |
|
38 "application/x-authorware-map" : { |
|
39 "extensions": ["aam"], |
|
40 "icon": "flashBlock-authorware.png" |
|
41 }, |
|
42 "application/x-authorware-seg" : { |
|
43 "extensions": ["aas"], |
|
44 "icon": "flashBlock-authorware.png" |
|
45 }, |
|
46 "application/x-authorware-bin" : { |
|
47 "extensions": ["aab", "x32", "u32", "vox"], |
|
48 "icon": "flashBlock-authorware.png" |
|
49 }, |
|
50 "image/x-freehand" : { |
|
51 "extensions": ["fh4", "fh7", "fh5", "fhc", "fh"], |
|
52 "icon": "flashBlock-freehand.png" |
|
53 }, |
|
54 "application/x-silverlight-app" : { |
|
55 "extensions": ["xap"], |
|
56 "icon": "flashBlock-silverlight.png" |
|
57 }, |
|
58 "application/xaml+xml" : { |
|
59 "extensions": ["xaml"], |
|
60 "icon": "flashBlock-silverlight.png" |
|
61 }, |
|
62 "application/x-ms-xbap" : { |
|
63 "extensions": ["xbap"], |
|
64 "icon": "flashBlock-silverlight.png" |
|
65 }, |
|
66 "application/x-java-applet" : { |
|
67 "extensions": [], |
|
68 "icon": "flashBlock-java.png" |
|
69 }, |
|
70 } |
|
71 |
|
72 def __init__(self): |
|
73 """ |
|
74 Constructor |
|
75 """ |
|
76 self.__loaded = False |
|
77 self.__whitelist = [] |
|
78 |
|
79 def metaPlugin(self): |
|
80 """ |
|
81 Public method to create a meta plug-in object containing plug-in info. |
|
82 |
|
83 @return meta plug-in object (QWebPluginFactory.Plugin) |
|
84 """ |
|
85 plugin = QWebPluginFactory.Plugin() |
|
86 plugin.name = "ClickToFlashPlugin" |
|
87 mimeTypes = plugin.mimeTypes |
|
88 for mime, value in ClickToFlashPlugin.ClickToFlashData.items(): |
|
89 mimeType = QWebPluginFactory.MimeType() |
|
90 mimeType.name = mime |
|
91 extensions = value["extensions"] |
|
92 if extensions: |
|
93 fileExtensions = mimeType.fileExtensions |
|
94 for extension in extensions: |
|
95 fileExtensions.append(extension) |
|
96 mimeType.fileExtensions = fileExtensions |
|
97 mimeTypes.append(mimeType) |
|
98 plugin.mimeTypes = mimeTypes |
|
99 |
|
100 return plugin |
|
101 |
|
102 def create(self, mimeType, url, argumentNames, argumentValues): |
|
103 """ |
|
104 Public method to create a plug-in instance for the given data. |
|
105 |
|
106 @param mimeType MIME type for the plug-in (string) |
|
107 @param url URL for the plug-in (QUrl) |
|
108 @param argumentNames list of argument names (list of strings) |
|
109 @param argumentValues list of argument values (list of strings) |
|
110 @return reference to the created object (QWidget) |
|
111 """ |
|
112 self.__load() |
|
113 if not self.__enabled(): |
|
114 return None |
|
115 |
|
116 if self.onWhitelist(url.host()): |
|
117 return None |
|
118 |
|
119 if ClickToFlash.isAlreadyAccepted(url, argumentNames, argumentValues): |
|
120 return None |
|
121 |
|
122 ctf = ClickToFlash(self, mimeType, url, argumentNames, argumentValues) |
|
123 return ctf |
|
124 |
|
125 def configure(self): |
|
126 """ |
|
127 Public method to configure the plug-in. |
|
128 """ |
|
129 self.__load() |
|
130 dlg = ClickToFlashWhitelistDialog(self.__whitelist) |
|
131 if dlg.exec_() == QDialog.Accepted: |
|
132 self.__whitelist = dlg.getWhitelist() |
|
133 self.__save() |
|
134 |
|
135 def isAnonymous(self): |
|
136 """ |
|
137 Public method to indicate an anonymous plug-in. |
|
138 """ |
|
139 return True |
|
140 |
|
141 def onWhitelist(self, host): |
|
142 """ |
|
143 Public method to check, if a host is on the whitelist. |
|
144 |
|
145 @param host host to check for (string) |
|
146 @return flag indicating presence in the whitelist (boolean) |
|
147 """ |
|
148 return host in self.__whitelist or \ |
|
149 "www." + host in self.__whitelist or \ |
|
150 host.replace("www.", "") in self.__whitelist |
|
151 |
|
152 def addToWhitelist(self, host): |
|
153 """ |
|
154 Public method to add a host to the whitelist. |
|
155 |
|
156 @param host host to be added (string) |
|
157 """ |
|
158 if not self.onWhitelist(host): |
|
159 self.__whitelist.append(host) |
|
160 self.__save() |
|
161 |
|
162 def removeFromWhitelist(self, host): |
|
163 """ |
|
164 Public method to remove a host from the whitelist. |
|
165 |
|
166 @param host host to be removed (string) |
|
167 """ |
|
168 if self.onWhitelist(host): |
|
169 if host in self.__whitelist: |
|
170 self.__whitelist.remove(host) |
|
171 elif "www." + host in self.__whitelist: |
|
172 self.__whitelist.remove("www." + host) |
|
173 elif host.replace("www.", "") in self.__whitelist: |
|
174 self.__whitelist.remove(host.replace("www.", "")) |
|
175 self.__save() |
|
176 |
|
177 def __load(self): |
|
178 """ |
|
179 Private method to load the configuration. |
|
180 """ |
|
181 if self.__loaded: |
|
182 return |
|
183 |
|
184 self.__loaded = True |
|
185 self.__whitelist = Preferences.getHelp("ClickToFlashWhitelist") |
|
186 |
|
187 def __save(self): |
|
188 """ |
|
189 Private method to save the configuration. |
|
190 """ |
|
191 Preferences.setHelp("ClickToFlashWhitelist", self.__whitelist) |
|
192 |
|
193 def __enabled(self): |
|
194 """ |
|
195 Private method to check, if the plug-in is enabled. |
|
196 |
|
197 @return enabled status (boolean) |
|
198 """ |
|
199 return Preferences.getHelp("ClickToFlashEnabled") |
|
200 |
|
201 @classmethod |
|
202 def getIconName(cls, mimeType): |
|
203 """ |
|
204 Class method to get the icon name for the mime type. |
|
205 |
|
206 @param mimeType mime type to get the icon for (string) |
|
207 @return name of the icon file (string) |
|
208 """ |
|
209 if mimeType in cls.ClickToFlashData: |
|
210 return cls.ClickToFlashData[mimeType]["icon"] |
|
211 |
|
212 return "" |