WebBrowser/SafeBrowsing/SafeBrowsingManager.py

changeset 6093
5ed91fe54bc9
parent 6048
82ad8ec9548c
child 6187
2cc7e3629784
equal deleted inserted replaced
6092:f0d60c3de700 6093:5ed91fe54bc9
42 @signal progress(current) emitted to signal the current progress 42 @signal progress(current) emitted to signal the current progress
43 """ 43 """
44 progressMessage = pyqtSignal(str, int) 44 progressMessage = pyqtSignal(str, int)
45 progress = pyqtSignal(int) 45 progress = pyqtSignal(int)
46 46
47 enabled = (
48 Preferences.getWebBrowser("SafeBrowsingEnabled") and
49 bool(Preferences.getWebBrowser("SafeBrowsingApiKey"))
50 )
51
47 def __init__(self): 52 def __init__(self):
48 """ 53 """
49 Constructor 54 Constructor
50 """ 55 """
51 super(SafeBrowsingManager, self).__init__() 56 super(SafeBrowsingManager, self).__init__()
54 if self.__apiKey: 59 if self.__apiKey:
55 self.__apiClient = SafeBrowsingAPIClient(self.__apiKey, 60 self.__apiClient = SafeBrowsingAPIClient(self.__apiKey,
56 parent=self) 61 parent=self)
57 else: 62 else:
58 self.__apiClient = None 63 self.__apiClient = None
59
60 self.__enabled = (
61 Preferences.getWebBrowser("SafeBrowsingEnabled") and
62 bool(self.__apiKey))
63 64
64 gsbCachePath = os.path.join( 65 gsbCachePath = os.path.join(
65 Utilities.getConfigDir(), "web_browser", "safe_browsing") 66 Utilities.getConfigDir(), "web_browser", "safe_browsing")
66 self.__cache = SafeBrowsingCache(gsbCachePath, self) 67 self.__cache = SafeBrowsingCache(gsbCachePath, self)
67 68
87 self.__apiClient.setApiKey(self.__apiKey) 88 self.__apiClient.setApiKey(self.__apiKey)
88 else: 89 else:
89 self.__apiClient = SafeBrowsingAPIClient(self.__apiKey, 90 self.__apiClient = SafeBrowsingAPIClient(self.__apiKey,
90 parent=self) 91 parent=self)
91 92
92 self.__enabled = ( 93 SafeBrowsingManager.enabled = (
93 Preferences.getWebBrowser("SafeBrowsingEnabled") and 94 Preferences.getWebBrowser("SafeBrowsingEnabled") and
94 bool(self.__apiKey)) 95 bool(self.__apiKey))
95 96
96 self.__setPlatforms() 97 self.__setPlatforms()
97 self.__setAutoUpdateThreatLists() 98 self.__setAutoUpdateThreatLists()
109 else: 110 else:
110 # treat all other platforms like linux 111 # treat all other platforms like linux
111 platform = "linux" 112 platform = "linux"
112 self.__platforms = SafeBrowsingAPIClient.getPlatformTypes(platform) 113 self.__platforms = SafeBrowsingAPIClient.getPlatformTypes(platform)
113 114
114 def isEnabled(self): 115 @classmethod
115 """ 116 def isEnabled(cls):
116 Public method to check, if safe browsing is enabled. 117 """
118 Class method to check, if safe browsing is enabled.
117 119
118 @return flag indicating the enabled state 120 @return flag indicating the enabled state
119 @rtype bool 121 @rtype bool
120 """ 122 """
121 return self.__enabled 123 return cls.enabled
122 124
123 def close(self): 125 def close(self):
124 """ 126 """
125 Public method to close the safe browsing interface. 127 Public method to close the safe browsing interface.
126 """ 128 """
131 Public method to check, if the fair use wait period has expired. 133 Public method to check, if the fair use wait period has expired.
132 134
133 @return flag indicating expiration 135 @return flag indicating expiration
134 @rtype bool 136 @rtype bool
135 """ 137 """
136 return self.__enabled and self.__apiClient.fairUseDelayExpired() 138 return self.isEnabled() and self.__apiClient.fairUseDelayExpired()
137 139
138 def __showNotificationMessage(self, message, timeout=5): 140 def __showNotificationMessage(self, message, timeout=5):
139 """ 141 """
140 Private method to show some message in a notification widget. 142 Private method to show some message in a notification widget.
141 143
166 def __setAutoUpdateThreatLists(self): 168 def __setAutoUpdateThreatLists(self):
167 """ 169 """
168 Private method to set auto update for the threat lists. 170 Private method to set auto update for the threat lists.
169 """ 171 """
170 autoUpdateEnabled = Preferences.getWebBrowser("SafeBrowsingAutoUpdate") 172 autoUpdateEnabled = Preferences.getWebBrowser("SafeBrowsingAutoUpdate")
171 if autoUpdateEnabled and self.__enabled: 173 if autoUpdateEnabled and self.isEnabled():
172 nextUpdateDateTime = Preferences.getWebBrowser( 174 nextUpdateDateTime = Preferences.getWebBrowser(
173 "SafeBrowsingUpdateDateTime") 175 "SafeBrowsingUpdateDateTime")
174 if nextUpdateDateTime.isValid(): 176 if nextUpdateDateTime.isValid():
175 interval = \ 177 interval = \
176 QDateTime.currentDateTime().secsTo(nextUpdateDateTime) + 2 178 QDateTime.currentDateTime().secsTo(nextUpdateDateTime) + 2
191 def __threatListsUpdateTimerTimeout(self): 193 def __threatListsUpdateTimerTimeout(self):
192 """ 194 """
193 Private slot to perform the auto update of the threat lists. 195 Private slot to perform the auto update of the threat lists.
194 """ 196 """
195 ok = False 197 ok = False
196 if self.__enabled: 198 if self.isEnabled():
197 self.__showNotificationMessage( 199 self.__showNotificationMessage(
198 self.tr("Updating threat lists..."), 0) 200 self.tr("Updating threat lists..."), 0)
199 ok = self.updateHashPrefixCache()[0] 201 ok = self.updateHashPrefixCache()[0]
200 if ok: 202 if ok:
201 self.__showNotificationMessage( 203 self.__showNotificationMessage(
222 Public method to load or update the locally cached threat lists. 224 Public method to load or update the locally cached threat lists.
223 225
224 @return flag indicating success and an error message 226 @return flag indicating success and an error message
225 @rtype tuple of (bool, str) 227 @rtype tuple of (bool, str)
226 """ 228 """
227 if not self.__enabled: 229 if not self.isEnabled():
228 return False, self.tr("Safe Browsing is disabled.") 230 return False, self.tr("Safe Browsing is disabled.")
229 231
230 if not self.__apiClient.fairUseDelayExpired(): 232 if not self.__apiClient.fairUseDelayExpired():
231 return False, \ 233 return False, \
232 self.tr("The fair use wait period has not expired yet." 234 self.tr("The fair use wait period has not expired yet."
367 @type str or QUrl 369 @type str or QUrl
368 @return list of threat lists the URL was found in 370 @return list of threat lists the URL was found in
369 @rtype list of ThreatList 371 @rtype list of ThreatList
370 @exception ValueError raised for an invalid URL 372 @exception ValueError raised for an invalid URL
371 """ 373 """
372 if self.__enabled: 374 if self.isEnabled():
373 if isinstance(url, QUrl): 375 if isinstance(url, QUrl):
374 urlStr = url.toString().strip() 376 urlStr = url.toString().strip()
375 else: 377 else:
376 urlStr = url.strip() 378 urlStr = url.strip()
377 379

eric ide

mercurial