Helpviewer/HelpSingleApplication.py

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

eric ide

mercurial