Added a progress bar to the management part and fine tuned the hash prefix update process. safe_browsing

Sun, 30 Jul 2017 19:56:04 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 30 Jul 2017 19:56:04 +0200
branch
safe_browsing
changeset 5821
6c7766cde4c1
parent 5820
b610cb5b501a
child 5829
d3448873ced3

Added a progress bar to the management part and fine tuned the hash prefix update process.

WebBrowser/SafeBrowsing/SafeBrowsingDialog.py file | annotate | diff | comparison | revisions
WebBrowser/SafeBrowsing/SafeBrowsingDialog.ui file | annotate | diff | comparison | revisions
WebBrowser/SafeBrowsing/SafeBrowsingManager.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/SafeBrowsing/SafeBrowsingDialog.py	Sat Jul 29 19:41:16 2017 +0200
+++ b/WebBrowser/SafeBrowsing/SafeBrowsingDialog.py	Sun Jul 30 19:56:04 2017 +0200
@@ -39,6 +39,8 @@
         self.setWindowFlags(Qt.Window)
         
         self.__manager = manager
+        self.__manager.progressMessage.connect(self.__setProgressMessage)
+        self.__manager.progress.connect(self.__setProgress)
         
         self.__saveButton = self.buttonBox.addButton(
             self.tr("Save"), QDialogButtonBox.ActionRole)
@@ -173,8 +175,8 @@
         """
         QApplication.setOverrideCursor(Qt.WaitCursor)
         ok, error = self.__manager.updateHashPrefixCache()
+        self.__resetProgress()
         QApplication.restoreOverrideCursor()
-##        QApplication.processEvents()
         if not ok:
             if error:
                 E5MessageBox.critical(
@@ -195,3 +197,35 @@
         Private slot to clear the local cache database.
         """
         self.__manager.fullCacheCleanup()
+    
+    @pyqtSlot(str, int)
+    def __setProgressMessage(self, message, maximum):
+        """
+        Private slot to set the progress message and the maximum value.
+        
+        @param message progress message to be set
+        @type str
+        @param maximum maximum value to be set
+        @type int
+        """
+        self.progressLabel.setText(message)
+        self.progressBar.setMaximum(maximum)
+        self.progressBar.setValue(0)
+    
+    @pyqtSlot(int)
+    def __setProgress(self, value):
+        """
+        Private slot to set the progress value.
+        
+        @param value progress value to be set
+        @type int
+        """
+        self.progressBar.setValue(value)
+    
+    def __resetProgress(self):
+        """
+        Private method to reset the progress info.
+        """
+        self.progressLabel.clear()
+        self.progressBar.setMaximum(100)
+        self.progressBar.setValue(0)
--- a/WebBrowser/SafeBrowsing/SafeBrowsingDialog.ui	Sat Jul 29 19:41:16 2017 +0200
+++ b/WebBrowser/SafeBrowsing/SafeBrowsingDialog.ui	Sun Jul 30 19:56:04 2017 +0200
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>650</width>
-    <height>187</height>
+    <height>288</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -16,7 +16,7 @@
   <property name="sizeGripEnabled">
    <bool>true</bool>
   </property>
-  <layout class="QVBoxLayout" name="verticalLayout">
+  <layout class="QVBoxLayout" name="verticalLayout_2">
    <item>
     <widget class="QGroupBox" name="gsbGroupBox">
      <property name="toolTip">
@@ -70,28 +70,57 @@
     </widget>
    </item>
    <item>
-    <layout class="QHBoxLayout" name="horizontalLayout">
-     <item>
-      <widget class="QPushButton" name="updateCacheButton">
-       <property name="toolTip">
-        <string>Press to update the local cache database</string>
-       </property>
-       <property name="text">
-        <string>Update Cache</string>
-       </property>
-      </widget>
-     </item>
-     <item>
-      <widget class="QPushButton" name="clearCacheButton">
-       <property name="toolTip">
-        <string>Press to clear the local cache database</string>
-       </property>
-       <property name="text">
-        <string>Clear Cache</string>
-       </property>
-      </widget>
-     </item>
-    </layout>
+    <widget class="QGroupBox" name="groupBox">
+     <property name="title">
+      <string>Manage Local Cache</string>
+     </property>
+     <layout class="QVBoxLayout" name="verticalLayout">
+      <item>
+       <layout class="QHBoxLayout" name="horizontalLayout">
+        <item>
+         <widget class="QPushButton" name="updateCacheButton">
+          <property name="toolTip">
+           <string>Press to update the local cache database</string>
+          </property>
+          <property name="text">
+           <string>Update Cache</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="clearCacheButton">
+          <property name="toolTip">
+           <string>Press to clear the local cache database</string>
+          </property>
+          <property name="text">
+           <string>Clear Cache</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </item>
+      <item>
+       <widget class="QLabel" name="progressLabel">
+        <property name="text">
+         <string notr="true"/>
+        </property>
+        <property name="wordWrap">
+         <bool>true</bool>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QProgressBar" name="progressBar">
+        <property name="value">
+         <number>0</number>
+        </property>
+        <property name="format">
+         <string>%v/%m</string>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
    </item>
    <item>
     <widget class="QDialogButtonBox" name="buttonBox">
--- a/WebBrowser/SafeBrowsing/SafeBrowsingManager.py	Sat Jul 29 19:41:16 2017 +0200
+++ b/WebBrowser/SafeBrowsing/SafeBrowsingManager.py	Sun Jul 30 19:56:04 2017 +0200
@@ -19,7 +19,7 @@
 import os
 import base64
 
-from PyQt5.QtCore import QObject
+from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication
 
 import Preferences
 import Utilities
@@ -31,7 +31,14 @@
 class SafeBrowsingManager(QObject):
     """
     Class implementing the interface for Google Safe Browsing.
+    
+    @signal progressMessage(message,maximum) emitted to give a message for the
+        action about to be performed and the maximum value
+    @signal progress(current) emitted to signal the current progress
     """
+    progressMessage = pyqtSignal(str, int)
+    progress = pyqtSignal(int)
+    
     def __init__(self):
         """
         Constructor
@@ -128,7 +135,13 @@
         for threatList, clientState in self.__cache.getThreatLists():
             threatListsForRemove[repr(threatList)] = threatList
         threatLists = self.__apiClient.getThreatLists()
+        maximum = len(threatLists)
+        current = 0
+        self.progressMessage.emit(self.tr("Updating threat lists"), maximum)
         for entry in threatLists:
+            current += 1
+            self.progress.emit(current)
+            QCoreApplication.processEvents()
             threatList = ThreatList.fromApiEntry(entry)
             if self.__platforms is None or \
                     threatList.platformType in self.__platforms:
@@ -136,7 +149,14 @@
                 key = repr(threatList)
                 if key in threatListsForRemove:
                     del threatListsForRemove[key]
+        maximum = len(threatListsForRemove.values())
+        current = 0
+        self.progressMessage.emit(self.tr("Deleting obsolete threat lists"),
+                                  maximum)
         for threatList in threatListsForRemove.values():
+            current += 1
+            self.progress.emit(current)
+            QCoreApplication.processEvents()
             self.__cache.deleteHashPrefixList(threatList)
             self.__cache.deleteThreatList(threatList)
         del threatListsForRemove
@@ -148,19 +168,27 @@
             clientStates[threatList.asTuple()] = clientState
         threatsUpdateResponses = \
             self.__apiClient.getThreatsUpdate(clientStates)
+        maximum = len(threatsUpdateResponses)
+        current = 0
+        self.progressMessage.emit(self.tr("Updating hash prefixes"), maximum)
         for response in threatsUpdateResponses:
+            current += 1
+            self.progress.emit(current)
+            QCoreApplication.processEvents()
             responseThreatList = ThreatList.fromApiEntry(response)
             if response["responseType"] == "FULL_UPDATE":
                 self.__cache.deleteHashPrefixList(responseThreatList)
             for removal in response.get("removals", []):
                 self.__cache.removeHashPrefixIndices(
                     responseThreatList, removal["rawIndices"]["indices"])
+                QCoreApplication.processEvents()
             for addition in response.get("additions", []):
                 hashPrefixList = HashPrefixList(
                     addition["rawHashes"]["prefixSize"],
                     base64.b64decode(addition["rawHashes"]["rawHashes"]))
                 self.__cache.populateHashPrefixList(responseThreatList,
                                                     hashPrefixList)
+                QCoreApplication.processEvents()
             expectedChecksum = base64.b64decode(response["checksum"]["sha256"])
             if self.__verifyThreatListChecksum(responseThreatList,
                                                expectedChecksum):

eric ide

mercurial