eric6/Helpviewer/WebPlugins/ClickToFlash/ClickToFlashPlugin.py

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

eric ide

mercurial