Continued porting the web browser. QtWebEngine

Sun, 20 Mar 2016 20:13:29 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 20 Mar 2016 20:13:29 +0100
branch
QtWebEngine
changeset 4886
b56735d36159
parent 4885
362360f9a979
child 4895
3baaf8303a7f

Continued porting the web browser.

- fixed a few issues with the GreaseMonkey and the CookieJar

WebBrowser/CookieJar/CookieJar.py file | annotate | diff | comparison | revisions
WebBrowser/GreaseMonkey/GreaseMonkeyDownloader.py file | annotate | diff | comparison | revisions
WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py file | annotate | diff | comparison | revisions
WebBrowser/GreaseMonkey/GreaseMonkeyManager.py file | annotate | diff | comparison | revisions
WebBrowser/GreaseMonkey/GreaseMonkeyScript.py file | annotate | diff | comparison | revisions
WebBrowser/Network/NetworkManager.py file | annotate | diff | comparison | revisions
WebBrowser/Tools/Scripts.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserPage.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserView.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
WebBrowser/WebInspector.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/CookieJar/CookieJar.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/CookieJar/CookieJar.py	Sun Mar 20 20:13:29 2016 +0100
@@ -55,16 +55,15 @@
         self.__acceptCookies = self.AcceptOnlyFromSitesNavigatedTo
         self.__saveTimer = AutoSaver(self, self.__save)
         
-        self.__cookies = []
-        
         self.__cookiesFile = os.path.join(Utilities.getConfigDir(),
                                           "web_browser", "cookies.ini")
-        self.__load()
         
-        # TODO: Qt 5.6
-##        self.__store = WebBrowserWindow.webProfile.cookieStore()
-##        self.__store.cookieAdded.connect(self.__cookieAdded)
-##        self.__store.cookieRemoved.connect(self.__cookieRemoved)
+        self.__store = WebBrowserWindow.webProfile().cookieStore()
+        self.__store.cookieAdded.connect(self.__cookieAdded)
+        self.__store.cookieRemoved.connect(self.__cookieRemoved)
+        
+        self.__load()
+        self.__store.loadAllCookies()
     
     def close(self):
         """
@@ -84,22 +83,20 @@
         if not self.__loaded:
             self.__load()
         
-        self.__cookies = []
-        # TODO: Qt 5.6
-##        self.__store.deleteAllCookies()
+        self.setAllCookies([])
+        self.__store.deleteAllCookies()
         self.cookiesChanged.emit()
     
     def removeCookies(self, cookies):
         """
-        Public method to remove a cookie.
+        Public method to remove a list of cookies.
         
         @param cookies list of cookies to be removed
         @type list of QNetworkCookie
         """
         wasBlocked = self.blockSignals(True)
-        # TODO: Qt 5.6
-##        for cookie in cookies:
-##            self.__store.deleteCookie(cookie)
+        for cookie in cookies:
+            self.__store.deleteCookie(cookie)
         self.blockSignals(wasBlocked)
         
         self.cookiesChanged.emit()
@@ -163,11 +160,10 @@
         @type QNetworkCookie
         """
         if self.__rejectCookie(cookie, cookie.domain()):
-            # TODO: Qt 5.6
-##            self.__store.deleteCookie(cookie)
+            self.__store.deleteCookie(cookie)
             return
         
-        self.__cookies.append(cookie)
+        self.insertCookie(cookie)
         self.cookiesChanged.emit()
     
     @pyqtSlot(QNetworkCookie)
@@ -178,8 +174,7 @@
         @param cookie cookie which was removed
         @type QNetworkCookie
         """
-        if cookie in self.__cookies:
-            self.__cookies.remove(cookie)
+        if self.deleteCookie(cookie):
             self.cookiesChanged.emit()
     
     def __rejectCookie(self, cookie, cookieDomain):
@@ -432,4 +427,7 @@
         
         @return list of all cookies (list of QNetworkCookie)
         """
-        return self.__cookies
+        if not self.__loaded:
+            self.__load()
+        
+        return self.allCookies()
--- a/WebBrowser/GreaseMonkey/GreaseMonkeyDownloader.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyDownloader.py	Sun Mar 20 20:13:29 2016 +0100
@@ -41,7 +41,7 @@
         self.__manager = manager
         
         self.__reply = FollowRedirectReply(
-            url, WebBrowserWindow.networkAccessManager())
+            url, WebBrowserWindow.networkManager())
         self.__reply.finished.connect(self.__scriptDownloaded)
         
         self.__fileName = ""
@@ -150,7 +150,7 @@
         if self.__requireUrls:
             self.__reply = FollowRedirectReply(
                 self.__requireUrls.pop(0),
-                WebBrowserWindow.networkAccessManager())
+                WebBrowserWindow.networkManager())
             self.__reply.finished.connect(self.__requireDownloaded)
         else:
             from .GreaseMonkeyScript import GreaseMonkeyScript
--- a/WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyJavaScript.py	Sun Mar 20 20:13:29 2016 +0100
@@ -132,7 +132,7 @@
     localStorage.removeItem("{0}" + aKey);
 }}
 
-function GM_getValue(aKey, aDefault) 
+function GM_getValue(aKey, aDefault) {{
     var val = localStorage.getItem("{0}" + aKey)
     if (null === val && 'undefined' != typeof aDefault) return aDefault;
     return val;
--- a/WebBrowser/GreaseMonkey/GreaseMonkeyManager.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyManager.py	Sun Mar 20 20:13:29 2016 +0100
@@ -11,7 +11,8 @@
 
 import os
 
-from PyQt5.QtCore import pyqtSignal, QObject, QTimer, QFile, QDir, QSettings
+from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QTimer, QFile, QDir, \
+    QSettings, QMetaObject, QUrl, Q_ARG
 
 import Utilities
 import Preferences
@@ -41,7 +42,7 @@
         
         self.__interceptor = GreaseMonkeyUrlInterceptor(self)
         WebBrowserWindow.networkManager().installUrlInterceptor(
-            self.__interceptor)
+            GreaseMonkeyUrlInterceptor(self))
         
         QTimer.singleShot(0, self.__load)
     
@@ -70,10 +71,24 @@
         @param url URL to download script from
         @type QUrl
         """
+        QMetaObject.invokeMethod(
+            self, "doDownloadScript", Qt.QueuedConnection,
+            Q_ARG(QUrl, url))
+    
+    @pyqtSlot(QUrl)
+    def doDownloadScript(self, url):
         from .GreaseMonkeyDownloader import GreaseMonkeyDownloader
         downloader = GreaseMonkeyDownloader(url, self)
         downloader.finished.connect(self.__downloaderFinished)
         self.__downloaders.append(downloader)
+##void GM_Manager::downloadScript(const QUrl &url)
+##{
+##    QMetaObject::invokeMethod(this, "doDownloadScript", Qt::QueuedConnection, Q_ARG(QUrl, url));
+##}
+##void GM_Manager::doDownloadScript(const QUrl &url)
+##{
+##    new GM_Downloader(url, this);
+##}
     
     def __downloaderFinished(self):
         """
--- a/WebBrowser/GreaseMonkey/GreaseMonkeyScript.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyScript.py	Sun Mar 20 20:13:29 2016 +0100
@@ -208,12 +208,10 @@
             
             self.scriptChanged.emit()
     
-    def __parseScript(self, path):
+    def __parseScript(self):
         """
         Private method to parse the given script and populate the data
         structure.
-        
-        @param path path of the Javascript file (string)
         """
         self.__name = ""
         self.__namespace = "GreaseMonkeyNS"
@@ -233,7 +231,7 @@
         self.__noFrames = False
         
         try:
-            f = open(path, "r", encoding="utf-8")
+            f = open(self.__fileName, "r", encoding="utf-8")
             fileData = f.read()
             f.close()
         except (IOError, OSError):
@@ -339,6 +337,7 @@
                 self.__toJavaScriptList(self.__exclude[:]),
                 self.__toJavaScriptList(self.__include[:])
             )
+        runCheck = ""
         self.__script = "(function(){{{0}\n{1}\n{2}\n{3}\n}})();".format(
             runCheck, valuesScript,
             self.__manager.requireScripts(requireList), fileData
--- a/WebBrowser/Network/NetworkManager.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/Network/NetworkManager.py	Sun Mar 20 20:13:29 2016 +0100
@@ -74,7 +74,6 @@
         self.authenticationRequired.connect(
             lambda reply, auth: self.authentication(reply.url(), auth))
         
-        # TODO: install network scheme handlers
         from .EricSchemeHandler import EricSchemeHandler
         self.__ericSchemeHandler = EricSchemeHandler()
         WebBrowserWindow.webProfile().installUrlSchemeHandler(
--- a/WebBrowser/Tools/Scripts.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/Tools/Scripts.py	Sun Mar 20 20:13:29 2016 +0100
@@ -80,47 +80,6 @@
     return source.format(style)
 
 
-def toggleMediaPause(pos):
-    """
-    Function generating a script to toggle the paused state of a media element.
-    
-    @param pos position of the media element
-    @type QPoint
-    @return script to toggle the element paused state
-    @rtype str
-    """
-    source = """
-        (function() {{
-            var e = document.elementFromPoint({0}, {1});
-            if (!e)
-                return;
-            if (e.paused)
-                e.play();
-            else
-                e.pause();
-        }})()"""
-    return source.format(pos.x(), pos.y())
-
-
-def toggleMediaMute(pos):
-    """
-    Function generating a script to toggle the mute state of a media element.
-    
-    @param pos position of the media element
-    @type QPoint
-    @return script to toggle the element mute state
-    @rtype str
-    """
-    source = """
-        (function() {[
-            var e = document.elementFromPoint({0}, {1});
-            if (!e)
-                return;
-            e.muted = !e.muted;
-        }})()"""
-    return source.format(pos.x(), pos.y())
-
-
 def getFormData(pos):
     """
     Function generating a script to extract data for a form element.
--- a/WebBrowser/WebBrowserPage.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/WebBrowserPage.py	Sun Mar 20 20:13:29 2016 +0100
@@ -123,6 +123,7 @@
         if url.scheme() == "abp":
             if WebBrowserWindow.adBlockManager().addSubscriptionFromUrl(url):
                 return False
+        
 ##        
 ##        if type_ == QWebPage.NavigationTypeFormResubmitted:
 ##            res = E5MessageBox.yesNo(
--- a/WebBrowser/WebBrowserView.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/WebBrowserView.py	Sun Mar 20 20:13:29 2016 +0100
@@ -812,7 +812,6 @@
 ##        menu.addAction(self.__mw.saveAsAct)
 ##        menu.addSeparator()
         
-        # TODO: Speed Dial
         if self.url().toString() == "eric:speeddial":
             # special menu for the spedd dial page
             menu.addAction(self.__mw.backAct)
@@ -1055,25 +1054,17 @@
         """
         self.triggerPageAction(QWebEnginePage.DownloadMediaToDisk)
     
-    # TODO: Qt 5.6: do this with triggerPageAction()
     def __pauseMedia(self):
         """
         Private slot to pause or play the selected media.
         """
         self.triggerPageAction(QWebEnginePage.ToggleMediaPlayPause)
-##        from .Tools import Scripts
-##        script = Scripts.toggleMediaPause(self.__clickedPos)
-##        self.page().runJavaScript(script)
     
-    # TODO: Qt 5.6: do this with triggerPageAction()
     def __muteMedia(self):
         """
         Private slot to (un)mute the selected media.
         """
         self.triggerPageAction(QWebEnginePage.ToggleMediaMute)
-##        from .Tools import Scripts
-##        script = Scripts.toggleMediaMute(self.__clickedPos)
-##        self.page().runJavaScript(script)
     
     def __virusTotal(self):
         """
--- a/WebBrowser/WebBrowserWindow.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Sun Mar 20 20:13:29 2016 +0100
@@ -431,10 +431,6 @@
         settings.setAttribute(
             QWebEngineSettings.AutoLoadImages,
             Preferences.getWebBrowser("AutoLoadImages"))
-        # TODO: Qt 5.6
-##        settings.setAttribute(
-##            QWebSettings.JavaEnabled,
-##            Preferences.getWebBrowser("JavaEnabled"))
         settings.setAttribute(
             QWebEngineSettings.JavascriptEnabled,
             Preferences.getWebBrowser("JavaScriptEnabled"))
@@ -444,42 +440,10 @@
         settings.setAttribute(
             QWebEngineSettings.JavascriptCanAccessClipboard,
             Preferences.getWebBrowser("JavaScriptCanAccessClipboard"))
-        # TODO: Qt 5.6
-##        settings.setAttribute(
-##            QWebSettings.PluginsEnabled,
-##            Preferences.getWebBrowser("PluginsEnabled"))
-        
-##        if hasattr(QWebSettings, "PrintElementBackgrounds"):
-##            settings.setAttribute(
-##                QWebSettings.PrintElementBackgrounds,
-##                Preferences.getWebBrowser("PrintBackgrounds"))
-##        
-##        if hasattr(QWebSettings, "setOfflineStoragePath"):
-##            settings.setAttribute(
-##                QWebSettings.OfflineStorageDatabaseEnabled,
-##                Preferences.getWebBrowser("OfflineStorageDatabaseEnabled"))
-##            webDatabaseDir = os.path.join(
-##                Utilities.getConfigDir(), "web_browser", "webdatabases")
-##            if not os.path.exists(webDatabaseDir):
-##                os.makedirs(webDatabaseDir)
-##            settings.setOfflineStoragePath(webDatabaseDir)
-##            settings.setOfflineStorageDefaultQuota(
-##                Preferences.getWebBrowser("OfflineStorageDatabaseQuota") *
-##                1024 * 1024)
-##        
-##        if hasattr(QWebSettings, "OfflineWebApplicationCacheEnabled"):
-##            settings.setAttribute(
-##                QWebSettings.OfflineWebApplicationCacheEnabled,
-##                Preferences.getWebBrowser("OfflineWebApplicationCacheEnabled"))
-##            appCacheDir = os.path.join(
-##                Utilities.getConfigDir(), "web_browser", "webappcaches")
-##            if not os.path.exists(appCacheDir):
-##                os.makedirs(appCacheDir)
-##            settings.setOfflineWebApplicationCachePath(appCacheDir)
-##            settings.setOfflineWebApplicationCacheQuota(
-##                Preferences.getWebBrowser("OfflineWebApplicationCacheQuota") *
-##                1024 * 1024)
-##        
+        settings.setAttribute(
+            QWebEngineSettings.PluginsEnabled,
+            Preferences.getWebBrowser("PluginsEnabled"))
+        
         if self.isPrivate():
             settings.setAttribute(
                 QWebEngineSettings.LocalStorageEnabled, False)
@@ -487,18 +451,6 @@
             settings.setAttribute(
                 QWebEngineSettings.LocalStorageEnabled,
                 Preferences.getWebBrowser("LocalStorageEnabled"))
-        # TODO: Local Storage
-##        localStorageDir = os.path.join(
-##            Utilities.getConfigDir(), "web_browser", "weblocalstorage")
-##        if not os.path.exists(localStorageDir):
-##            os.makedirs(localStorageDir)
-##        settings.setLocalStoragePath(localStorageDir)
-##        
-##        if hasattr(QWebSettings, "DnsPrefetchEnabled"):
-##            settings.setAttribute(
-##                QWebSettings.DnsPrefetchEnabled,
-##                Preferences.getWebBrowser("DnsPrefetchEnabled"))
-##        
         settings.setDefaultTextEncoding(
             Preferences.getWebBrowser("DefaultTextEncoding"))
         
@@ -3342,16 +3294,9 @@
                     shutil.rmtree(cachePath)
             if cookies:
                 self.cookieJar().clear()
+                self.webProfile().cookieStore().deleteAllCookies()
             if passwords:
                 self.passwordManager().clear()
-            # TODO: Web Databases
-##            if databases:
-##                if hasattr(QWebDatabase, "removeAllDatabases"):
-##                    QWebDatabase.removeAllDatabases()
-##                else:
-##                    for securityOrigin in QWebSecurityOrigin.allOrigins():
-##                        for database in securityOrigin.databases():
-##                            QWebDatabase.removeDatabase(database)
             if flashCookies:
                 self.flashCookieManager().removeAllCookies()
             if zoomValues:
--- a/WebBrowser/WebInspector.py	Sun Mar 20 15:17:31 2016 +0100
+++ b/WebBrowser/WebInspector.py	Sun Mar 20 20:13:29 2016 +0100
@@ -137,8 +137,7 @@
         Private slot handling the finished signal.
         """
         if self.__inspectElement:
-            # TODO: Qt 5.6
-##            self.__view.triggerPageAction(QWebEnginePage.InspectElement)
+            self.__view.triggerPageAction(QWebEnginePage.InspectElement)
             self.__inspectElement = False
 
 

eric ide

mercurial