WebBrowser/WebBrowserSingleApplication.py

changeset 6623
c0882a599e18
child 6625
a67fee7bc09c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebBrowser/WebBrowserSingleApplication.py	Sun Dec 09 16:39:35 2018 +0100
@@ -0,0 +1,134 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+
+"""
+Module implementing the single application server and client for the web
+browser.
+"""
+
+from __future__ import unicode_literals
+
+from PyQt5.QtCore import pyqtSignal
+
+from Toolbox.SingleApplication import SingleApplicationClient, \
+    SingleApplicationServer
+
+import Globals
+
+###########################################################################
+## define some module global stuff
+###########################################################################
+
+SAFile = "eric6_browser"
+
+# define the protocol tokens
+SALoadUrl = 'LoadUrl'
+SASearch = 'Search'
+
+
+class WebBrowserSingleApplicationServer(SingleApplicationServer):
+    """
+    Class implementing the single application server embedded within the
+    Web Browser.
+    
+    @signal loadUrl(str) emitted to load a URL
+    @signal search(str) emitted to search for a given word
+    """
+    loadUrl = pyqtSignal(str)
+    search = pyqtSignal(str)
+    
+    def __init__(self):
+        """
+        Constructor
+        """
+        SingleApplicationServer.__init__(self, SAFile)
+
+    def handleCommand(self, command, arguments):
+        """
+        Public slot to handle the command sent by the client.
+        
+        @param command command sent by the client
+        @type str
+        @param arguments list of command arguments
+        @type list of str
+        """
+        if command == SALoadUrl:
+            self.__saLoadUrl(arguments[0])
+
+        elif command == SASearch:
+            self.__saSearch(arguments[0])
+    
+    def __saLoadUrl(self, url):
+        """
+        Private method to load an URL.
+        
+        @param url URL to be loaded
+        @type str
+        """
+        self.loadUrl.emit(url)
+    
+    def __saSearch(self, word):
+        """
+        Private method to search for a given word.
+        
+        @param word word to be searched for
+        @type str
+        """
+        self.search.emit(word)
+
+
+class WebBrowserSingleApplicationClient(SingleApplicationClient):
+    """
+    Class implementing the single application client of the Translations
+    Previewer.
+    """
+    def __init__(self):
+        """
+        Constructor
+        """
+        SingleApplicationClient.__init__(self, SAFile)
+        
+    def processArgs(self, args):
+        """
+        Public method to process the command line args passed to the UI.
+        
+        @param args list of files to open
+        """
+        # no args, return
+        if args is None:
+            return
+        
+        if Globals.isWindowsPlatform():
+            argChars = ('-', '/')
+        else:
+            argChars = ('-', )
+        
+        for arg in args:
+            if arg.startswith("--search="):
+                self.__search(arg.replace("--search=", ""))
+            elif not arg.startswith(argChars):
+                # it is an URL
+                self.__loadUrl(arg)
+        
+        self.disconnect()
+    
+    def __loadUrl(self, url):
+        """
+        Private method to send an URL to be loaded.
+        
+        @param url URL to be loaded
+        @type str
+        """
+        self.sendCommand(SALoadUrl, [url])
+    
+    def __search(self, word):
+        """
+        Private method to send a word to search for.
+        
+        @param word to to be searched for
+        @type str
+        """
+        self.sendCommand(SASearch, [word])

eric ide

mercurial