|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 |
|
7 """ |
|
8 Module implementing the single application server and client for the web |
|
9 browser. |
|
10 """ |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal |
|
13 |
|
14 from Toolbox.SingleApplication import ( |
|
15 SingleApplicationClient, SingleApplicationServer |
|
16 ) |
|
17 |
|
18 import Globals |
|
19 |
|
20 ########################################################################### |
|
21 ## define some module global stuff |
|
22 ########################################################################### |
|
23 |
|
24 SAFile = "eric6_browser" |
|
25 |
|
26 # define the protocol tokens |
|
27 SALoadUrl = 'LoadUrl' |
|
28 SANewTab = 'NewTab' |
|
29 SASearch = 'Search' |
|
30 SAShutdown = 'Shutdown' |
|
31 |
|
32 |
|
33 class WebBrowserSingleApplicationServer(SingleApplicationServer): |
|
34 """ |
|
35 Class implementing the single application server embedded within the |
|
36 Web Browser. |
|
37 |
|
38 @signal loadUrl(str) emitted to load an URL |
|
39 @signal newTab(str) emitted to load an URL in a new tab |
|
40 @signal search(str) emitted to search for a given word |
|
41 @signal shutdown() emitted to shut down the browser |
|
42 """ |
|
43 loadUrl = pyqtSignal(str) |
|
44 newTab = pyqtSignal(str) |
|
45 search = pyqtSignal(str) |
|
46 shutdown = pyqtSignal() |
|
47 |
|
48 def __init__(self, name=""): |
|
49 """ |
|
50 Constructor |
|
51 |
|
52 @param name name to be used by the single application server |
|
53 @type str |
|
54 """ |
|
55 if not name: |
|
56 name = SAFile |
|
57 |
|
58 SingleApplicationServer.__init__(self, name) |
|
59 |
|
60 def handleCommand(self, command, arguments): |
|
61 """ |
|
62 Public slot to handle the command sent by the client. |
|
63 |
|
64 @param command command sent by the client |
|
65 @type str |
|
66 @param arguments list of command arguments |
|
67 @type list of str |
|
68 """ |
|
69 if command == SALoadUrl: |
|
70 self.__saLoadUrl(arguments[0]) |
|
71 |
|
72 elif command == SANewTab: |
|
73 self.__saNewTab(arguments[0]) |
|
74 |
|
75 elif command == SASearch: |
|
76 self.__saSearch(arguments[0]) |
|
77 |
|
78 elif command == SAShutdown: |
|
79 self.__saShutdown() |
|
80 |
|
81 def __saLoadUrl(self, url): |
|
82 """ |
|
83 Private method to load an URL in a new tab. |
|
84 |
|
85 @param url URL to be loaded |
|
86 @type str |
|
87 """ |
|
88 self.loadUrl.emit(url) |
|
89 |
|
90 def __saNewTab(self, url): |
|
91 """ |
|
92 Private method to load an URL . |
|
93 |
|
94 @param url URL to be loaded |
|
95 @type str |
|
96 """ |
|
97 self.newTab.emit(url) |
|
98 |
|
99 def __saSearch(self, word): |
|
100 """ |
|
101 Private method to search for a given word. |
|
102 |
|
103 @param word word to be searched for |
|
104 @type str |
|
105 """ |
|
106 self.search.emit(word) |
|
107 |
|
108 def __saShutdown(self): |
|
109 """ |
|
110 Private method to shut down the web browser. |
|
111 """ |
|
112 self.shutdown.emit() |
|
113 |
|
114 |
|
115 class WebBrowserSingleApplicationClient(SingleApplicationClient): |
|
116 """ |
|
117 Class implementing the single application client of the web browser. |
|
118 """ |
|
119 def __init__(self, name=""): |
|
120 """ |
|
121 Constructor |
|
122 |
|
123 @param name name to be used by the single application server |
|
124 @type str |
|
125 """ |
|
126 if not name: |
|
127 name = SAFile |
|
128 |
|
129 SingleApplicationClient.__init__(self, name) |
|
130 |
|
131 def processArgs(self, args, disconnect=True): |
|
132 """ |
|
133 Public method to process the command line args passed to the UI. |
|
134 |
|
135 @param args list of command line arguments |
|
136 @type list of str |
|
137 @param disconnect flag indicating to disconnect when done |
|
138 @type bool |
|
139 """ |
|
140 # no args, return |
|
141 if args is None: |
|
142 return |
|
143 |
|
144 argChars = ('-', '/') if Globals.isWindowsPlatform() else ('-', ) |
|
145 |
|
146 for arg in args: |
|
147 if arg.startswith("--search="): |
|
148 self.__search(arg.replace("--search=", "")) |
|
149 elif arg.startswith("--newtab="): |
|
150 self.__newTab(arg.replace("--newtab=", "")) |
|
151 elif arg == "--shutdown": |
|
152 self.__shutdown() |
|
153 elif not arg.startswith(argChars): |
|
154 # it is an URL |
|
155 self.__loadUrl(arg) |
|
156 |
|
157 if disconnect: |
|
158 self.disconnect() |
|
159 |
|
160 def __loadUrl(self, url): |
|
161 """ |
|
162 Private method to send an URL to be loaded. |
|
163 |
|
164 @param url URL to be loaded |
|
165 @type str |
|
166 """ |
|
167 self.sendCommand(SALoadUrl, [url]) |
|
168 |
|
169 def __newTab(self, url): |
|
170 """ |
|
171 Private method to send an URL to be loaded in a new tab. |
|
172 |
|
173 @param url URL to be loaded |
|
174 @type str |
|
175 """ |
|
176 self.sendCommand(SANewTab, [url]) |
|
177 |
|
178 def __search(self, word): |
|
179 """ |
|
180 Private method to send a word to search for. |
|
181 |
|
182 @param word to to be searched for |
|
183 @type str |
|
184 """ |
|
185 self.sendCommand(SASearch, [word]) |
|
186 |
|
187 def __shutdown(self): |
|
188 """ |
|
189 Private method to signal a shutdown request to the browser. |
|
190 """ |
|
191 self.sendCommand(SAShutdown, []) |