]>
Commit | Line | Data |
---|---|---|
2b5f62a0 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: remhelp.cpp | |
3 | // Purpose: Remote help controller class | |
4 | // Author: Eric Dowty | |
4fe30bce | 5 | // Modified by: |
2b5f62a0 VZ |
6 | // Created: 2002-11-18 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(__APPLE__) | |
13 | #pragma implementation remhelp.h | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include <math.h> | |
28 | ||
29 | #include "wx/process.h" | |
30 | #include "wx/confbase.h" | |
31 | ||
32 | // Settings common to both executables: determines whether | |
33 | // we're using TCP/IP or real DDE. | |
34 | ||
35 | //#include "ddesetup.h" | |
36 | //#define wxUSE_DDE_FOR_IPC 0 | |
37 | ||
38 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
39 | #include "mondrian.xpm" | |
40 | #endif | |
41 | ||
42 | #include "remhelp.h" | |
43 | #include "client.h" | |
44 | ||
45 | #if !defined(USE_REMOTE) | |
46 | #include <wx/html/helpctrl.h> | |
47 | #endif | |
48 | ||
49 | ////////////////// | |
50 | ////////////////// | |
51 | // helper classes | |
52 | ||
53 | rhhcClient::rhhcClient( bool *isconn_a ) | |
54 | { | |
55 | isconn_2 = isconn_a; | |
56 | } | |
57 | ||
58 | wxConnectionBase *rhhcClient::OnMakeConnection() | |
59 | { | |
4fe30bce | 60 | return new rhhcConnection( isconn_2 ); |
2b5f62a0 VZ |
61 | } |
62 | ||
63 | rhhcConnection::rhhcConnection( bool *isconn_a ) | |
64 | : wxConnection() | |
65 | { | |
4fe30bce WS |
66 | isconn_3 = isconn_a; |
67 | *isconn_3 = true; | |
2b5f62a0 VZ |
68 | } |
69 | ||
70 | rhhcConnection::~rhhcConnection() | |
71 | { | |
4fe30bce | 72 | *isconn_3 = false; |
2b5f62a0 VZ |
73 | } |
74 | ||
75 | bool rhhcConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) | |
76 | { | |
4fe30bce | 77 | return true; |
2b5f62a0 VZ |
78 | } |
79 | ||
80 | bool rhhcConnection::OnDisconnect() | |
81 | { | |
4fe30bce WS |
82 | *isconn_3 = false; |
83 | ||
2b5f62a0 VZ |
84 | return wxConnection::OnDisconnect(); |
85 | } | |
86 | ||
87 | ////////////////////////////////////////// | |
88 | ///////////////////////////////////////// | |
89 | ||
90 | // wxRemoteHtmlHelpController class | |
91 | ||
92 | IMPLEMENT_CLASS(wxRemoteHtmlHelpController, wxHelpControllerBase) | |
93 | ||
94 | wxRemoteHtmlHelpController::wxRemoteHtmlHelpController(int style ) | |
95 | { | |
4fe30bce WS |
96 | m_style = style; |
97 | m_connection = NULL; | |
98 | m_client = NULL; | |
99 | m_pid = 0; | |
100 | isconn_1 = false; | |
101 | m_process = NULL; | |
102 | ||
103 | // defaults | |
104 | // | |
105 | // server app is assumed to be local | |
106 | // | |
2b5f62a0 VZ |
107 | // for MSW (DDE classes), a_service is 'service name', apparently an arbitrary string |
108 | // for Unix, should be a valid file name (for a nonexistent file) | |
109 | // for nonMSW, nonUnix, must be port number, for example "4242" (TCP/IP based classes) | |
110 | // should be unique to the client app | |
4fe30bce WS |
111 | |
112 | wxString thename = wxGetApp().GetAppName(); | |
2b5f62a0 | 113 | #if defined(__WXMSW__) |
4fe30bce WS |
114 | m_appname = wxT("helpview.exe"); |
115 | m_service = thename + wxString(wxT("_helpservice")); | |
2b5f62a0 | 116 | #elif defined(__UNIX__) |
4fe30bce WS |
117 | m_appname = wxT("./helpview"); |
118 | m_service = wxT("/tmp/") + thename + wxString(wxT("_helpservice")); | |
2b5f62a0 | 119 | #else |
4fe30bce WS |
120 | m_appname = wxT("./helpview"); |
121 | m_service = wxT("4242"); | |
2b5f62a0 | 122 | #endif |
4fe30bce WS |
123 | |
124 | m_book = thename + wxT(".hhp"); // or .htb or .zip | |
125 | m_windowname = thename + wxT(" Help: %s"); | |
126 | //underscores for spaces | |
127 | m_windowname.Replace( wxT(" "), wxT("_") ); | |
2b5f62a0 VZ |
128 | } |
129 | ||
130 | void wxRemoteHtmlHelpController::SetService(wxString& a_service) | |
131 | { | |
4fe30bce | 132 | m_service = a_service; |
2b5f62a0 VZ |
133 | } |
134 | void wxRemoteHtmlHelpController::SetServer(wxString& a_appname) | |
135 | { | |
4fe30bce | 136 | m_appname = a_appname; |
2b5f62a0 VZ |
137 | } |
138 | ||
139 | void wxRemoteHtmlHelpController::OnQuit() | |
140 | { | |
4fe30bce WS |
141 | //kill the Server here? |
142 | //this function is not called ? | |
2b5f62a0 VZ |
143 | } |
144 | ||
145 | wxRemoteHtmlHelpController::~wxRemoteHtmlHelpController() | |
146 | { | |
147 | if ( isconn_1 ) | |
148 | { | |
4fe30bce WS |
149 | // if (!m_connection->Poke( wxT("--YouAreDead"), wxT("") ) ) |
150 | // wxLogError(wxT("wxRemoteHtmlHelpController - YouAreDead Failed")); | |
151 | ||
152 | // Kill the server. This could be an option. | |
153 | Quit(); | |
154 | ||
155 | m_connection->Disconnect(); | |
2b5f62a0 | 156 | delete m_connection; |
4fe30bce | 157 | |
2b5f62a0 VZ |
158 | delete m_process; |
159 | m_process = NULL; | |
160 | } | |
161 | if( m_client ) | |
4fe30bce WS |
162 | delete m_client; //should be automatic? |
163 | ||
2b5f62a0 VZ |
164 | } |
165 | ||
166 | bool wxRemoteHtmlHelpController::DoConnection() | |
167 | { | |
168 | wxString cmd, blank; | |
169 | int nsleep; | |
4fe30bce | 170 | |
2b5f62a0 | 171 | blank = wxT(" "); |
4fe30bce | 172 | |
2b5f62a0 VZ |
173 | // ignored under DDE, host name in TCP/IP based classes |
174 | wxString hostName = wxT("localhost"); | |
4fe30bce | 175 | |
2b5f62a0 VZ |
176 | // Create a new client |
177 | if( !m_client ) m_client = new rhhcClient(&isconn_1); | |
4fe30bce | 178 | |
2b5f62a0 | 179 | nsleep = 0; |
4fe30bce WS |
180 | |
181 | // suppress the log messages from MakeConnection() | |
2b5f62a0 | 182 | { |
4fe30bce WS |
183 | wxLogNull nolog; |
184 | ||
185 | //first try to connect assuming server is running | |
186 | if( !isconn_1 ) | |
187 | m_connection = (rhhcConnection *)m_client->MakeConnection(hostName, m_service, wxT("HELP") ); | |
188 | ||
189 | //if not, start server | |
190 | if( !isconn_1 ) { | |
191 | ||
192 | wxString stylestr; | |
193 | stylestr.Printf( wxT("--Style%d"), m_style ); | |
194 | ||
195 | cmd = m_appname + blank + m_service + blank + m_windowname + blank + m_book + blank + stylestr; | |
196 | ||
197 | m_process = new wxProcess(NULL); | |
198 | m_pid = wxExecute( cmd, false, m_process ); | |
199 | // leaks - wxExecute itself (if not deleted) and in wxExecute at | |
200 | // wxExecuteData *data = new wxExecuteData; | |
201 | if( m_pid <= 0 ) { | |
202 | wxLogError( wxT("wxRemoteHtmlHelpController - Failed to start Help server") ); | |
203 | return false; | |
204 | } | |
2b5f62a0 | 205 | } |
4fe30bce | 206 | |
2b5f62a0 VZ |
207 | while ( !isconn_1 ) |
208 | { | |
4fe30bce WS |
209 | //try every second for a while, then leave it to user |
210 | wxSleep(1); | |
211 | if( nsleep > 4 ) { | |
212 | if ( wxMessageBox( wxT("Failed to make connection to Help server.\nRetry?") , | |
213 | wxT("wxRemoteHtmlHelpController Error"), | |
214 | wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES ) | |
215 | { | |
216 | // no server | |
217 | return false; | |
218 | } | |
219 | } | |
220 | nsleep++; | |
221 | ||
222 | m_connection = (rhhcConnection *)m_client->MakeConnection(hostName, m_service, wxT("HELP") ); | |
2b5f62a0 VZ |
223 | } |
224 | } | |
4fe30bce | 225 | |
2b5f62a0 | 226 | if (!m_connection->StartAdvise(wxT("Item"))) { |
4fe30bce WS |
227 | wxLogError(wxT("wxRemoteHtmlHelpController - StartAdvise failed") ); |
228 | return false; | |
2b5f62a0 | 229 | } |
4fe30bce WS |
230 | |
231 | return true; | |
2b5f62a0 VZ |
232 | } |
233 | ||
234 | bool wxRemoteHtmlHelpController::LoadFile(const wxString& WXUNUSED(file)) | |
235 | { | |
4fe30bce | 236 | return true; |
2b5f62a0 VZ |
237 | } |
238 | bool wxRemoteHtmlHelpController::DisplaySection(int sectionNo) | |
239 | { | |
240 | Display(sectionNo); | |
4fe30bce | 241 | return true; |
2b5f62a0 VZ |
242 | } |
243 | bool wxRemoteHtmlHelpController::DisplayBlock(long blockNo) | |
244 | { | |
4fe30bce | 245 | return DisplaySection((int)blockNo); |
2b5f62a0 VZ |
246 | } |
247 | ||
248 | bool wxRemoteHtmlHelpController::Quit() | |
249 | { | |
4fe30bce WS |
250 | //this code from exec sample - branches left in for testing |
251 | // sig = 3, 6, 9 or 12 all kill server with no apparent problem | |
252 | // but give error message on MSW - timout? | |
253 | int sig = 15; //3 = quit; 6 = abort; 9 = kill; 15 = terminate | |
254 | ||
255 | /* | |
256 | switch ( sig ) | |
257 | { | |
258 | default: | |
259 | wxFAIL_MSG( _T("unexpected return value") ); | |
260 | // fall through | |
261 | ||
262 | case -1: | |
263 | // cancelled | |
264 | return false; | |
265 | ||
266 | case wxSIGNONE: | |
267 | case wxSIGHUP: | |
268 | case wxSIGINT: | |
269 | case wxSIGQUIT: | |
270 | case wxSIGILL: | |
271 | case wxSIGTRAP: | |
272 | case wxSIGABRT: | |
273 | case wxSIGEMT: | |
274 | case wxSIGFPE: | |
275 | case wxSIGKILL: | |
276 | case wxSIGBUS: | |
277 | case wxSIGSEGV: | |
278 | case wxSIGSYS: | |
279 | case wxSIGPIPE: | |
280 | case wxSIGALRM: | |
281 | case wxSIGTERM: | |
282 | break; | |
283 | } | |
284 | */ | |
285 | ||
2b5f62a0 VZ |
286 | if ( sig == 0 ) |
287 | { | |
288 | if ( wxProcess::Exists(m_pid) ) | |
289 | wxLogStatus(_T("Process %ld is running."), m_pid); | |
290 | else | |
291 | wxLogStatus(_T("No process with pid = %ld."), m_pid); | |
292 | } | |
293 | else // not SIGNONE | |
294 | { | |
295 | wxKillError rc = wxProcess::Kill(m_pid, (wxSignal)sig); | |
296 | if ( rc == wxKILL_OK ) | |
297 | { | |
298 | wxLogStatus(_T("Process %ld killed with signal %d."), m_pid, sig); | |
299 | } | |
300 | else | |
301 | { | |
302 | static const wxChar *errorText[] = | |
303 | { | |
304 | _T(""), // no error | |
4fe30bce WS |
305 | _T("signal not supported"), |
306 | _T("permission denied"), | |
307 | _T("no such process"), | |
308 | _T("unspecified error"), | |
2b5f62a0 | 309 | }; |
4fe30bce WS |
310 | |
311 | // sig = 3, 6, 9 or 12 all kill server with no apparent problem | |
312 | // but give error message on MSW - timout? | |
313 | // | |
2b5f62a0 VZ |
314 | //wxLogError(_T("Failed to kill process %ld with signal %d: %s"), |
315 | // m_pid, sig, errorText[rc]); | |
316 | } | |
317 | } | |
4fe30bce WS |
318 | |
319 | ||
320 | return true; | |
2b5f62a0 VZ |
321 | } |
322 | ||
323 | void wxRemoteHtmlHelpController::Display(const wxString& helpfile) | |
324 | { | |
325 | if( !isconn_1 ) { | |
326 | if( !DoConnection() ) return; | |
327 | } | |
328 | ||
329 | if (!m_connection->Execute( helpfile, -1 ) ) | |
4fe30bce WS |
330 | wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed")); |
331 | ||
2b5f62a0 VZ |
332 | } |
333 | ||
334 | void wxRemoteHtmlHelpController::Display(const int id) | |
335 | { | |
336 | if( !isconn_1 ) { | |
337 | if( !DoConnection() ) return; | |
338 | } | |
4fe30bce WS |
339 | |
340 | wxString intstring; | |
341 | intstring.Printf( "--intstring%d", id ); | |
2b5f62a0 VZ |
342 | |
343 | if (!m_connection->Execute( intstring, -1 ) ) | |
4fe30bce WS |
344 | wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed")); |
345 | ||
2b5f62a0 VZ |
346 | } |
347 | ||
348 | bool wxRemoteHtmlHelpController::AddBook(const wxString& book, bool show_wait_msg) | |
349 | { | |
4fe30bce WS |
350 | //ignore show_wait_msg - there shouldn't be a delay in this step |
351 | //show_wait_msg = true could be transmitted with ++AddBook | |
352 | m_book = book; | |
353 | ||
354 | if( isconn_1 ) { | |
355 | if (!m_connection->Poke( wxT("--AddBook"), (char*)book.c_str() ) ) | |
356 | wxLogError(wxT("wxRemoteHtmlHelpController - AddBook Failed")); | |
357 | return false; | |
358 | } | |
359 | ||
360 | return true; | |
2b5f62a0 VZ |
361 | } |
362 | ||
363 | bool wxRemoteHtmlHelpController::DisplayContents() | |
364 | { | |
4fe30bce WS |
365 | if( isconn_1 ) { |
366 | if (!m_connection->Poke( wxT("--DisplayContents"), wxT("") ) ) { | |
367 | wxLogError(wxT("wxRemoteHtmlHelpController - DisplayContents Failed")); | |
368 | return false; | |
369 | } | |
370 | } | |
371 | return true; | |
2b5f62a0 VZ |
372 | } |
373 | void wxRemoteHtmlHelpController::DisplayIndex() | |
374 | { | |
4fe30bce WS |
375 | if( isconn_1 ) { |
376 | if (!m_connection->Poke( wxT("--DisplayIndex"), wxT("") ) ) | |
377 | wxLogError(wxT("wxRemoteHtmlHelpController - DisplayIndex Failed")); | |
378 | } | |
2b5f62a0 VZ |
379 | } |
380 | bool wxRemoteHtmlHelpController::KeywordSearch(const wxString& keyword) | |
381 | { | |
4fe30bce WS |
382 | if( isconn_1 ) { |
383 | if (!m_connection->Poke( wxT("--KeywordSearch"), (char*)keyword.c_str() ) ) { | |
384 | wxLogError(wxT("wxRemoteHtmlHelpController - KeywordSearch Failed")); | |
385 | return false; | |
386 | } | |
387 | } | |
388 | return true; | |
2b5f62a0 VZ |
389 | } |
390 | ||
391 | void wxRemoteHtmlHelpController::SetTitleFormat(const wxString& format) | |
392 | { | |
4fe30bce WS |
393 | m_windowname = format; |
394 | m_windowname.Replace( wxT(" "), wxT("_") ); | |
395 | ||
396 | if( isconn_1 ) { | |
397 | if (!m_connection->Poke( wxT("--SetTitleFormat"), (char*)format.c_str() ) ) | |
398 | wxLogError(wxT("wxRemoteHtmlHelpController - SetTitleFormat Failed")); | |
399 | } | |
2b5f62a0 VZ |
400 | } |
401 | ||
402 | void wxRemoteHtmlHelpController::SetTempDir(const wxString& path) | |
403 | { | |
4fe30bce WS |
404 | if( isconn_1 ) { |
405 | if (!m_connection->Poke( wxT("--SetTempDir"), (char*)path.c_str() ) ) | |
406 | wxLogError(wxT("wxRemoteHtmlHelpController - SetTempDir Failed")); | |
407 | } | |
2b5f62a0 VZ |
408 | } |
409 |