7 Module implementing a dialog to configure safe browsing support. |
7 Module implementing a dialog to configure safe browsing support. |
8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSlot, Qt |
12 from PyQt5.QtCore import pyqtSlot, Qt, QUrl |
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \ |
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \ |
14 QApplication |
14 QApplication |
15 |
15 |
16 from E5Gui import E5MessageBox |
16 from E5Gui import E5MessageBox |
17 |
17 |
40 |
40 |
41 self.__manager = manager |
41 self.__manager = manager |
42 self.__manager.progressMessage.connect(self.__setProgressMessage) |
42 self.__manager.progressMessage.connect(self.__setProgressMessage) |
43 self.__manager.progress.connect(self.__setProgress) |
43 self.__manager.progress.connect(self.__setProgress) |
44 |
44 |
45 self.__saveButton = self.buttonBox.addButton( |
|
46 self.tr("Save"), QDialogButtonBox.ActionRole) |
|
47 |
|
48 self.iconLabel.setPixmap( |
45 self.iconLabel.setPixmap( |
49 UI.PixmapCache.getPixmap("safeBrowsing48.png")) |
46 UI.PixmapCache.getPixmap("safeBrowsing48.png")) |
50 |
47 |
51 self.__gsbHelpDialog = None |
48 self.__gsbHelpDialog = None |
52 |
49 |
53 self.__enabled = Preferences.getWebBrowser("SafeBrowsingEnabled") |
50 self.__enabled = Preferences.getWebBrowser("SafeBrowsingEnabled") |
54 self.__apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey") |
51 self.__apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey") |
|
52 self.__filterPlatform = Preferences.getWebBrowser( |
|
53 "SafeBrowsingFilterPlatform") |
55 |
54 |
56 self.buttonBox.setFocus() |
55 self.buttonBox.setFocus() |
57 |
56 |
58 msh = self.minimumSizeHint() |
57 msh = self.minimumSizeHint() |
59 self.resize(max(self.width(), msh.width()), msh.height()) |
58 self.resize(max(self.width(), msh.width()), msh.height()) |
62 """ |
61 """ |
63 Public slot to show the dialog. |
62 Public slot to show the dialog. |
64 """ |
63 """ |
65 self.gsbGroupBox.setChecked(self.__enabled) |
64 self.gsbGroupBox.setChecked(self.__enabled) |
66 self.gsbApiKeyEdit.setText(self.__apiKey) |
65 self.gsbApiKeyEdit.setText(self.__apiKey) |
|
66 self.gsbFilterPlatformCheckBox.setChecked(self.__filterPlatform) |
67 |
67 |
68 self.__updateCacheButtons() |
68 self.__updateCacheButtons() |
69 |
69 |
70 super(SafeBrowsingDialog, self).show() |
70 super(SafeBrowsingDialog, self).show() |
71 |
71 |
106 @return flag indicating success |
104 @return flag indicating success |
107 @rtype bool |
105 @rtype bool |
108 """ |
106 """ |
109 self.__enabled = self.gsbGroupBox.isChecked() |
107 self.__enabled = self.gsbGroupBox.isChecked() |
110 self.__apiKey = self.gsbApiKeyEdit.text() |
108 self.__apiKey = self.gsbApiKeyEdit.text() |
|
109 self.__filterPlatform = self.gsbFilterPlatformCheckBox.isChecked() |
111 |
110 |
112 Preferences.setWebBrowser("SafeBrowsingEnabled", self.__enabled) |
111 Preferences.setWebBrowser("SafeBrowsingEnabled", self.__enabled) |
113 Preferences.setWebBrowser("SafeBrowsingApiKey", self.__apiKey) |
112 Preferences.setWebBrowser("SafeBrowsingApiKey", self.__apiKey) |
|
113 Preferences.setWebBrowser("SafeBrowsingFilterPlatform", |
|
114 self.__filterPlatform) |
114 |
115 |
115 self.__manager.configurationChanged() |
116 self.__manager.configurationChanged() |
116 |
117 |
117 self.__updateCacheButtons() |
118 self.__updateCacheButtons() |
118 |
119 |
137 @return flag indicating the presence of modified data |
138 @return flag indicating the presence of modified data |
138 @rtype bool |
139 @rtype bool |
139 """ |
140 """ |
140 return ( |
141 return ( |
141 self.__enabled != self.gsbGroupBox.isChecked() or |
142 self.__enabled != self.gsbGroupBox.isChecked() or |
142 self.__apiKey != self.gsbApiKeyEdit.text() |
143 self.__apiKey != self.gsbApiKeyEdit.text() or |
|
144 self.__filterPlatform != self.gsbFilterPlatformCheckBox.isChecked() |
143 ) |
145 ) |
144 |
146 |
145 def __okToClose(self): |
147 def __okToClose(self): |
146 """ |
148 """ |
147 Private method to check, if it is safe to close the dialog. |
149 Private method to check, if it is safe to close the dialog. |
148 |
150 |
149 @return flag indicating safe to close |
151 @return flag indicating safe to close |
150 @rtype bool |
152 @rtype bool |
151 """ |
153 """ |
|
154 QApplication.restoreOverrideCursor() |
152 if self.__isModified(): |
155 if self.__isModified(): |
153 res = E5MessageBox.okToClearData( |
156 res = E5MessageBox.okToClearData( |
154 self, |
157 self, |
155 self.tr("Safe Browsing Management"), |
158 self.tr("Safe Browsing Management"), |
156 self.tr("""The dialog contains unsaved changes."""), |
159 self.tr("""The dialog contains unsaved changes."""), |
171 @pyqtSlot() |
174 @pyqtSlot() |
172 def on_updateCacheButton_clicked(self): |
175 def on_updateCacheButton_clicked(self): |
173 """ |
176 """ |
174 Private slot to update the local cache database. |
177 Private slot to update the local cache database. |
175 """ |
178 """ |
|
179 E5MessageBox.information( |
|
180 self, |
|
181 self.tr("Update Safe Browsing Cache"), |
|
182 self.tr("""Updating the Safe Browsing cache might be a lengthy""" |
|
183 """ operation. Please be patient!""")) |
|
184 |
176 QApplication.setOverrideCursor(Qt.WaitCursor) |
185 QApplication.setOverrideCursor(Qt.WaitCursor) |
177 ok, error = self.__manager.updateHashPrefixCache() |
186 ok, error = self.__manager.updateHashPrefixCache() |
178 self.__resetProgress() |
187 self.__resetProgress() |
179 QApplication.restoreOverrideCursor() |
188 QApplication.restoreOverrideCursor() |
180 if not ok: |
189 if not ok: |
194 @pyqtSlot() |
203 @pyqtSlot() |
195 def on_clearCacheButton_clicked(self): |
204 def on_clearCacheButton_clicked(self): |
196 """ |
205 """ |
197 Private slot to clear the local cache database. |
206 Private slot to clear the local cache database. |
198 """ |
207 """ |
199 self.__manager.fullCacheCleanup() |
208 res = E5MessageBox.yesNo( |
|
209 self, |
|
210 self.tr("Clear Safe Browsing Cache"), |
|
211 self.tr("""Do you really want to clear the Safe Browsing cache?""" |
|
212 """ Re-populating it might take some time.""")) |
|
213 if res: |
|
214 QApplication.setOverrideCursor(Qt.WaitCursor) |
|
215 self.__manager.fullCacheCleanup() |
|
216 QApplication.restoreOverrideCursor() |
200 |
217 |
201 @pyqtSlot(str, int) |
218 @pyqtSlot(str, int) |
202 def __setProgressMessage(self, message, maximum): |
219 def __setProgressMessage(self, message, maximum): |
203 """ |
220 """ |
204 Private slot to set the progress message and the maximum value. |
221 Private slot to set the progress message and the maximum value. |
227 Private method to reset the progress info. |
244 Private method to reset the progress info. |
228 """ |
245 """ |
229 self.progressLabel.clear() |
246 self.progressLabel.clear() |
230 self.progressBar.setMaximum(100) |
247 self.progressBar.setMaximum(100) |
231 self.progressBar.setValue(0) |
248 self.progressBar.setValue(0) |
|
249 |
|
250 @pyqtSlot(str) |
|
251 def on_urlEdit_textChanged(self, text): |
|
252 """ |
|
253 Private slot to handle changes of the entered URL text. |
|
254 |
|
255 @param text entered URL text |
|
256 @type str |
|
257 """ |
|
258 url = QUrl.fromUserInput(text) |
|
259 enable = ( |
|
260 url.isValid() and |
|
261 bool(url.scheme()) and |
|
262 url.scheme() not in self.__manager.getIgnoreSchemes() |
|
263 ) |
|
264 self.urlCheckButton.setEnabled(enable) |
|
265 |
|
266 @pyqtSlot() |
|
267 def on_urlCheckButton_clicked(self): |
|
268 """ |
|
269 Private slot to check the entered URL. |
|
270 """ |
|
271 # Malicious URL for testing: |
|
272 # http://malware.testing.google.test/testing/malware/* |
|
273 # http://ianfette.org |
|
274 # |
|
275 urlStr = self.urlEdit.text() |
|
276 url = QUrl.fromUserInput(urlStr) |
|
277 threatLists = self.__manager.lookupUrl(url) |
|
278 |
|
279 if threatLists: |
|
280 threatMessages = self.__manager.getThreatMessages(threatLists) |
|
281 E5MessageBox.warning( |
|
282 self, |
|
283 self.tr("Check URL"), |
|
284 self.tr("<p>The URL <b>{0}</b> was found in the Safe Browsing" |
|
285 " Database.</p>{1}").format(urlStr, |
|
286 "".join(threatMessages)) |
|
287 ) |
|
288 else: |
|
289 E5MessageBox.information( |
|
290 self, |
|
291 self.tr("Check URL"), |
|
292 self.tr("<p>The URL <b>{0}</b> was not found in the Safe" |
|
293 " Browsing Database and may be considered safe.</p>") |
|
294 .format(urlStr) |
|
295 ) |
|
296 |
|
297 @pyqtSlot() |
|
298 def on_saveButton_clicked(self): |
|
299 """ |
|
300 Private slot to save the configuration data. |
|
301 """ |
|
302 self.__save() |