|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to configure safe browsing support. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt |
|
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \ |
|
14 QApplication |
|
15 |
|
16 from E5Gui import E5MessageBox |
|
17 |
|
18 from .Ui_SafeBrowsingDialog import Ui_SafeBrowsingDialog |
|
19 |
|
20 import UI.PixmapCache |
|
21 import Preferences |
|
22 |
|
23 |
|
24 class SafeBrowsingDialog(QDialog, Ui_SafeBrowsingDialog): |
|
25 """ |
|
26 Class implementing a dialog to configure safe browsing support. |
|
27 """ |
|
28 def __init__(self, manager, parent=None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param manager reference to the safe browsing manager |
|
33 @type SafeBrowsingManager |
|
34 @param parent reference to the parent widget |
|
35 @type QWidget |
|
36 """ |
|
37 super(SafeBrowsingDialog, self).__init__(parent) |
|
38 self.setupUi(self) |
|
39 self.setWindowFlags(Qt.Window) |
|
40 |
|
41 self.__manager = manager |
|
42 |
|
43 self.__saveButton = self.buttonBox.addButton( |
|
44 self.tr("Save"), QDialogButtonBox.ActionRole) |
|
45 |
|
46 self.iconLabel.setPixmap( |
|
47 UI.PixmapCache.getPixmap("safeBrowsing48.png")) |
|
48 |
|
49 self.__gsbHelpDialog = None |
|
50 |
|
51 self.__enabled = Preferences.getWebBrowser("SafeBrowsingEnabled") |
|
52 self.__apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey") |
|
53 |
|
54 self.buttonBox.setFocus() |
|
55 |
|
56 msh = self.minimumSizeHint() |
|
57 self.resize(max(self.width(), msh.width()), msh.height()) |
|
58 |
|
59 def show(self): |
|
60 """ |
|
61 Public slot to show the dialog. |
|
62 """ |
|
63 self.gsbGroupBox.setChecked(self.__enabled) |
|
64 self.gsbApiKeyEdit.setText(self.__apiKey) |
|
65 |
|
66 self.__updateCacheButtons() |
|
67 |
|
68 super(SafeBrowsingDialog, self).show() |
|
69 |
|
70 @pyqtSlot() |
|
71 def on_gsbHelpButton_clicked(self): |
|
72 """ |
|
73 Private slot to show some help text "How to create a safe |
|
74 browsing API key.". |
|
75 """ |
|
76 if self.__gsbHelpDialog is None: |
|
77 from E5Gui.E5SimpleHelpDialog import E5SimpleHelpDialog |
|
78 from . import SafeBrowsingHelp |
|
79 |
|
80 helpStr = SafeBrowsingHelp() |
|
81 self.__gsbHelpDialog = E5SimpleHelpDialog( |
|
82 title=self.tr("Google Safe Browsing API Help"), |
|
83 helpStr=helpStr, parent=self) |
|
84 |
|
85 self.__gsbHelpDialog.show() |
|
86 |
|
87 @pyqtSlot(QAbstractButton) |
|
88 def on_buttonBox_clicked(self, button): |
|
89 """ |
|
90 Private slot called by a button of the button box clicked. |
|
91 |
|
92 @param button button that was clicked (QAbstractButton) |
|
93 """ |
|
94 if button == self.buttonBox.button(QDialogButtonBox.Close): |
|
95 self.close() |
|
96 elif button == self.__saveButton: |
|
97 self.__save() |
|
98 |
|
99 @pyqtSlot() |
|
100 def __save(self): |
|
101 """ |
|
102 Private slot to save the configuration. |
|
103 |
|
104 @return flag indicating success |
|
105 @rtype bool |
|
106 """ |
|
107 self.__enabled = self.gsbGroupBox.isChecked() |
|
108 self.__apiKey = self.gsbApiKeyEdit.text() |
|
109 |
|
110 Preferences.setWebBrowser("SafeBrowsingEnabled", self.__enabled) |
|
111 Preferences.setWebBrowser("SafeBrowsingApiKey", self.__apiKey) |
|
112 |
|
113 self.__manager.configurationChanged() |
|
114 |
|
115 self.__updateCacheButtons() |
|
116 |
|
117 return True |
|
118 |
|
119 def closeEvent(self, evt): |
|
120 """ |
|
121 Protected method to handle close events. |
|
122 |
|
123 @param evt reference to the close event |
|
124 @type QCloseEvent |
|
125 """ |
|
126 if self.__okToClose(): |
|
127 evt.accept() |
|
128 else: |
|
129 evt.ignore() |
|
130 |
|
131 def __isModified(self): |
|
132 """ |
|
133 Private method to check, if the dialog contains modified data. |
|
134 |
|
135 @return flag indicating the presence of modified data |
|
136 @rtype bool |
|
137 """ |
|
138 return ( |
|
139 self.__enabled != self.gsbGroupBox.isChecked() or |
|
140 self.__apiKey != self.gsbApiKeyEdit.text() |
|
141 ) |
|
142 |
|
143 def __okToClose(self): |
|
144 """ |
|
145 Private method to check, if it is safe to close the dialog. |
|
146 |
|
147 @return flag indicating safe to close |
|
148 @rtype bool |
|
149 """ |
|
150 if self.__isModified(): |
|
151 res = E5MessageBox.okToClearData( |
|
152 self, |
|
153 self.tr("Safe Browsing Management"), |
|
154 self.tr("""The dialog contains unsaved changes."""), |
|
155 self.__save) |
|
156 if not res: |
|
157 return False |
|
158 return True |
|
159 |
|
160 def __updateCacheButtons(self): |
|
161 """ |
|
162 Private method to set enabled state of the cache buttons. |
|
163 """ |
|
164 enable = self.__enabled and bool(self.__apiKey) |
|
165 |
|
166 self.updateCacheButton.setEnabled(enable) |
|
167 self.clearCacheButton.setEnabled(enable) |
|
168 |
|
169 @pyqtSlot() |
|
170 def on_updateCacheButton_clicked(self): |
|
171 """ |
|
172 Private slot to update the local cache database. |
|
173 """ |
|
174 QApplication.setOverrideCursor(Qt.WaitCursor) |
|
175 ok, error = self.__manager.updateHashPrefixCache() |
|
176 QApplication.restoreOverrideCursor() |
|
177 ## QApplication.processEvents() |
|
178 if not ok: |
|
179 if error: |
|
180 E5MessageBox.critical( |
|
181 self, |
|
182 self.tr("Update Safe Browsing Cache"), |
|
183 self.tr("""<p>Updating the Safe Browsing cache failed.""" |
|
184 """</p><p>Reason: {0}</p>""").format(error)) |
|
185 else: |
|
186 E5MessageBox.critical( |
|
187 self, |
|
188 self.tr("Update Safe Browsing Cache"), |
|
189 self.tr("""<p>Updating the Safe Browsing cache failed.""" |
|
190 """</p>""")) |
|
191 |
|
192 @pyqtSlot() |
|
193 def on_clearCacheButton_clicked(self): |
|
194 """ |
|
195 Private slot to clear the local cache database. |
|
196 """ |
|
197 self.__manager.fullCacheCleanup() |