Continued porting the web browser. QtWebEngine

Mon, 14 Mar 2016 20:48:01 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 14 Mar 2016 20:48:01 +0100
branch
QtWebEngine
changeset 4859
36c4b21c9f7b
parent 4858
19dff9c9cf26
child 4860
0a44aff88bfa

Continued porting the web browser.

- continued porting the AdBlock code

Preferences/__init__.py file | annotate | diff | comparison | revisions
WebBrowser/AdBlock/AdBlockDialog.py file | annotate | diff | comparison | revisions
WebBrowser/AdBlock/AdBlockDialog.ui file | annotate | diff | comparison | revisions
WebBrowser/AdBlock/AdBlockManager.py file | annotate | diff | comparison | revisions
WebBrowser/AdBlock/AdBlockSubscription.py file | annotate | diff | comparison | revisions
WebBrowser/Network/EricSchemeHandler.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
--- a/Preferences/__init__.py	Sun Mar 13 20:54:42 2016 +0100
+++ b/Preferences/__init__.py	Mon Mar 14 20:48:01 2016 +0100
@@ -1070,6 +1070,7 @@
         "AdBlockSubscriptions": [],
         "AdBlockUpdatePeriod": 1,
         "AdBlockExceptions": [],
+        "AdBlockUseLimitedEasyList": True,
         # Flash Cookie Manager: identical to helpDefaults
         # PIM:                  identical to helpDefaults
         # VirusTotal:           identical to helpDefaults
@@ -2827,7 +2828,7 @@
                  "SyncEncryptData", "SyncEncryptPasswordsOnly",
                  "ShowPreview", "WebInspectorEnabled", "DiskCacheEnabled",
                  "DoNotTrack", "SendReferer", "FilterTrackingCookies",
-                 "AdBlockEnabled", 
+                 "AdBlockEnabled", "AdBlockUseLimitedEasyList",
                  ]:
         return toBool(prefClass.settings.value(
             "WebBrowser/" + key, prefClass.webBrowserDefaults[key]))
--- a/WebBrowser/AdBlock/AdBlockDialog.py	Sun Mar 13 20:54:42 2016 +0100
+++ b/WebBrowser/AdBlock/AdBlockDialog.py	Mon Mar 14 20:48:01 2016 +0100
@@ -24,24 +24,30 @@
     """
     Class implementing the AdBlock configuration dialog.
     """
-    def __init__(self, parent=None):
+    def __init__(self, manager, parent=None):
         """
         Constructor
         
+        @param manager reference to the AdBlock manager (AdBlockManager)
         @param parent reference to the parent object (QWidget)
         """
         super(AdBlockDialog, self).__init__(parent)
         self.setupUi(self)
         self.setWindowFlags(Qt.Window)
         
+        self.__manager = manager
+        
         self.iconLabel.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus48.png"))
         
-        self.updateSpinBox.setValue(Preferences.getHelp("AdBlockUpdatePeriod"))
+        self.updateSpinBox.setValue(
+            Preferences.getWebBrowser("AdBlockUpdatePeriod"))
+        
+        self.__useLimited = Preferences.getWebBrowser(
+            "AdBlockUseLimitedEasyList")
+        self.useLimitedEasyListCheckBox.setChecked(self.__useLimited)
         
         self.searchEdit.setInactiveText(self.tr("Search..."))
         
-        from WebBrowser.WebBrowserWindow import WebBrowserWindow
-        self.__manager = WebBrowserWindow.adBlockManager()
         self.adBlockGroup.setChecked(self.__manager.isEnabled())
         self.__manager.requiredSubscriptionLoaded.connect(self.addSubscription)
         
@@ -301,6 +307,11 @@
                 self.subscriptionsTabWidget.widget(index)
             self.__currentSubscription = \
                 self.__currentTreeWidget.subscription()
+            
+            isEasyList = \
+                self.__currentSubscription.url().toString().startswith(
+                    self.__manager.getDefaultSubscriptionUrl())
+            self.useLimitedEasyListCheckBox.setVisible(isEasyList)
     
     @pyqtSlot(str)
     def on_searchEdit_textChanged(self, filter):
@@ -323,3 +334,17 @@
         
         if state:
             self.__load()
+    
+    def closeEvent(self, evt):
+        """
+        Public method handling a close event.
+        
+        @param evt reference to the close event
+        @type QCloseEvent
+        """
+        if self.useLimitedEasyListCheckBox.isChecked() != \
+                self.__useLimited:
+            self.__manager.setUseLimitedEasyList(
+                self.useLimitedEasyListCheckBox.isChecked())
+        
+        super(AdBlockDialog, self).closeEvent(evt)
--- a/WebBrowser/AdBlock/AdBlockDialog.ui	Sun Mar 13 20:54:42 2016 +0100
+++ b/WebBrowser/AdBlock/AdBlockDialog.ui	Mon Mar 14 20:48:01 2016 +0100
@@ -126,6 +126,16 @@
         </item>
        </layout>
       </item>
+      <item>
+       <widget class="QCheckBox" name="useLimitedEasyListCheckBox">
+        <property name="toolTip">
+         <string/>
+        </property>
+        <property name="text">
+         <string>Use only essential part of EasyList (for performance reasons)</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
--- a/WebBrowser/AdBlock/AdBlockManager.py	Sun Mar 13 20:54:42 2016 +0100
+++ b/WebBrowser/AdBlock/AdBlockManager.py	Mon Mar 14 20:48:01 2016 +0100
@@ -52,6 +52,8 @@
         self.__subscriptions = []
         self.__exceptedHosts = Preferences.getWebBrowser("AdBlockExceptions")
         self.__saveTimer = AutoSaver(self, self.save)
+        self.__limitedEasyList = Preferences.getWebBrowser(
+            "AdBlockUseLimitedEasyList")
         
         self.__defaultSubscriptionUrlString = \
             "abp:subscribe?location=" \
@@ -461,7 +463,7 @@
         """
         if self.__adBlockDialog is None:
             from .AdBlockDialog import AdBlockDialog
-            self.__adBlockDialog = AdBlockDialog()
+            self.__adBlockDialog = AdBlockDialog(self)
         
         self.__adBlockDialog.show()
         return self.__adBlockDialog
@@ -576,3 +578,37 @@
         self.__adBlockExceptionsDialog.load(self.__exceptedHosts)
         self.__adBlockExceptionsDialog.show()
         return self.__adBlockExceptionsDialog
+    
+    def useLimitedEasyList(self):
+        """
+        Public method to test, if limited EasyList rules shall be used.
+        
+        @return flag indicating limited EasyList rules
+        @rtype bool
+        """
+        return self.__limitedEasyList
+    
+    def setUseLimitedEasyList(self, limited):
+        """
+        Public method to set the limited EasyList flag.
+        
+        @param limited flag indicating to use limited EasyList
+        @type bool
+        """
+        self.__limitedEasyList = limited
+        
+        for subscription in self.__subscriptions:
+            if subscription.url().toString().startswith(
+                    self.__defaultSubscriptionUrlString):
+                subscription.updateNow()
+        
+        Preferences.setWebBrowser("AdBlockUseLimitedEasyList", limited)
+    
+    def getDefaultSubscriptionUrl(self):
+        """
+        Public method to get the default subscription URL.
+        
+        @return default subscription URL
+        @rtype str
+        """
+        return self.__defaultSubscriptionUrlString
--- a/WebBrowser/AdBlock/AdBlockSubscription.py	Sun Mar 13 20:54:42 2016 +0100
+++ b/WebBrowser/AdBlock/AdBlockSubscription.py	Mon Mar 14 20:48:01 2016 +0100
@@ -411,7 +411,26 @@
                     """Unable to open AdBlock file '{0}' for writing.""")
                 .file(fileName))
             return
-        f.write(response)
+        
+        # ToDo:AdBlock debug this
+        from WebBrowser.WebBrowserWindow import WebBrowserWindow
+        if WebBrowserWindow.adBlockManager().useLimitedEasyList() and \
+            self.url().toString().startswith(
+                WebBrowserWindow.adBlockManager().getDefaultSubscriptionUrl()):
+            # ignore Third-party advertisers rules for performance
+            # whitelist rules at the end will be used
+            part1 = response.left(response.indexOf(
+                "!---------------------------"
+                "Third-party advertisers"
+                "---------------------------!"))
+            part2 = response.mid(response.indexOf(
+                "!-----------------------"
+                "Whitelists to fix broken sites"
+                "------------------------!"))
+            f.write(part1)
+            f.write(part2)
+        else:
+            f.write(response)
         f.close()
         self.__lastUpdate = QDateTime.currentDateTime()
         if self.__validateCheckSum(fileName):
--- a/WebBrowser/Network/EricSchemeHandler.py	Sun Mar 13 20:54:42 2016 +0100
+++ b/WebBrowser/Network/EricSchemeHandler.py	Mon Mar 14 20:48:01 2016 +0100
@@ -9,8 +9,8 @@
 
 from __future__ import unicode_literals
 
-from PyQt5.QtCore import QByteArray, QBuffer, QIODevice, QTextStream, \
-    QUrlQuery
+from PyQt5.QtCore import pyqtSignal, QByteArray, QBuffer, QIODevice, \
+    QTextStream, QUrlQuery
 from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler
 
 from ..Tools.WebBrowserTools import readAllFileContents
@@ -31,6 +31,8 @@
         @type QObject
         """
         super(EricSchemeHandler, self).__init__(parent)
+        
+        self.__replies = []
     
     def requestStarted(self, job):
         """
@@ -40,17 +42,32 @@
         @type QWebEngineUrlRequestJob
         """
         if job.requestUrl().path() in self.SupportedPages:
-            job.reply(b"text/html", EricSchemeReply(job))
+            reply = EricSchemeReply(job)
+            reply.closed.connect(self.__replyClosed)
+            self.__replies.append(reply)
+            job.reply(b"text/html", reply)
         else:
             job.reply(QByteArray(), QBuffer())
             # job.fail(QWebEngineUrlRequestJob.UrlNotFound)
+    
+    def __replyClosed(self):
+        """
+        Private slot handling the closed signal of a reply.
+        """
+        object = self.sender()
+        if object and object in self.__replies:
+            self.__replies.remove(object)
 
 
 class EricSchemeReply(QIODevice):
     """
     Class implementing a reply for a requested eric: page.
+    
+    @signal closed emitted to signal that the web engine has read
+        the data
     """
-    # TODO: SchemeHandler: debug this further
+    closed = pyqtSignal()
+    
     def __init__(self, job, parent=None):
         """
         Constructor
@@ -96,7 +113,6 @@
         @return number of available bytes
         @rtype int
         """
-        print("bytesAvailable", self.__buffer.bytesAvailable())
         return self.__buffer.bytesAvailable()
     
     def readData(self, maxlen):
@@ -106,12 +122,14 @@
         @param maxlen maximum number of bytes to read (integer)
         @return string containing the data (bytes)
         """
-        print("readData")
-        self.__loadPage()
         return self.__buffer.read(maxlen)
     
-    def writeData(self, data, len):
-        return 0
+    def close(self):
+        """
+        Public method used to cloase the reply.
+        """
+        super(EricSchemeReply, self).close()
+        self.closed.emit()
     
     def __adBlockPage(self):
         """
--- a/WebBrowser/WebBrowserWindow.py	Sun Mar 13 20:54:42 2016 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Mon Mar 14 20:48:01 2016 +0100
@@ -51,8 +51,8 @@
 import UI.PixmapCache
 import UI.Config
 from UI.Info import Version
-##
-##from .data import icons_rc          # __IGNORE_WARNING__
+
+from .data import icons_rc          # __IGNORE_WARNING__
 from .data import html_rc           # __IGNORE_WARNING__
 from .data import javascript_rc     # __IGNORE_WARNING__
 

eric ide

mercurial