]> git.saurik.com Git - wxWidgets.git/blob - utils/helpview/src/remhelp.cpp
Add wxNet dependency.
[wxWidgets.git] / utils / helpview / src / remhelp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: remhelp.cpp
3 // Purpose: Remote help controller class
4 // Author: Eric Dowty
5 // Modified by:
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 {
60 return new rhhcConnection( isconn_2 );
61 }
62
63 rhhcConnection::rhhcConnection( bool *isconn_a )
64 : wxConnection()
65 {
66 isconn_3 = isconn_a;
67 *isconn_3 = TRUE;
68 }
69
70 rhhcConnection::~rhhcConnection()
71 {
72 *isconn_3 = FALSE;
73 }
74
75 bool rhhcConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
76 {
77 return TRUE;
78 }
79
80 bool rhhcConnection::OnDisconnect()
81 {
82 *isconn_3 = FALSE;
83
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 {
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 //
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
111
112 wxString thename = wxGetApp().GetAppName();
113 #if defined(__WXMSW__)
114 m_appname = wxT("helpview.exe");
115 m_service = thename + wxString(wxT("_helpservice"));
116 #elif defined(__UNIX__)
117 m_appname = wxT("./helpview");
118 m_service = wxT("/tmp/") + thename + wxString(wxT("_helpservice"));
119 #else
120 m_appname = wxT("./helpview");
121 m_service = wxT("4242");
122 #endif
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("_") );
128 }
129
130 void wxRemoteHtmlHelpController::SetService(wxString& a_service)
131 {
132 m_service = a_service;
133 }
134 void wxRemoteHtmlHelpController::SetServer(wxString& a_appname)
135 {
136 m_appname = a_appname;
137 }
138
139 void wxRemoteHtmlHelpController::OnQuit()
140 {
141 //kill the Server here?
142 //this function is not called ?
143 }
144
145 wxRemoteHtmlHelpController::~wxRemoteHtmlHelpController()
146 {
147 if ( isconn_1 )
148 {
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();
156 delete m_connection;
157
158 delete m_process;
159 m_process = NULL;
160 }
161 if( m_client )
162 delete m_client; //should be automatic?
163
164 }
165
166 bool wxRemoteHtmlHelpController::DoConnection()
167 {
168 wxString cmd, blank;
169 int nsleep;
170
171 blank = wxT(" ");
172
173 // ignored under DDE, host name in TCP/IP based classes
174 wxString hostName = wxT("localhost");
175
176 // Create a new client
177 if( !m_client ) m_client = new rhhcClient(&isconn_1);
178
179 nsleep = 0;
180
181 // suppress the log messages from MakeConnection()
182 {
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 }
205 }
206
207 while ( !isconn_1 )
208 {
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") );
223 }
224 }
225
226 if (!m_connection->StartAdvise(wxT("Item"))) {
227 wxLogError(wxT("wxRemoteHtmlHelpController - StartAdvise failed") );
228 return FALSE;
229 }
230
231 return TRUE;
232 }
233
234 bool wxRemoteHtmlHelpController::LoadFile(const wxString& WXUNUSED(file))
235 {
236 return TRUE;
237 }
238 bool wxRemoteHtmlHelpController::DisplaySection(int sectionNo)
239 {
240 Display(sectionNo);
241 return TRUE;
242 }
243 bool wxRemoteHtmlHelpController::DisplayBlock(long blockNo)
244 {
245 return DisplaySection((int)blockNo);
246 }
247
248 bool wxRemoteHtmlHelpController::Quit()
249 {
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
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
305 _T("signal not supported"),
306 _T("permission denied"),
307 _T("no such process"),
308 _T("unspecified error"),
309 };
310
311 // sig = 3, 6, 9 or 12 all kill server with no apparent problem
312 // but give error message on MSW - timout?
313 //
314 //wxLogError(_T("Failed to kill process %ld with signal %d: %s"),
315 // m_pid, sig, errorText[rc]);
316 }
317 }
318
319
320 return TRUE;
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 ) )
330 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
331
332 }
333
334 void wxRemoteHtmlHelpController::Display(const int id)
335 {
336 if( !isconn_1 ) {
337 if( !DoConnection() ) return;
338 }
339
340 wxString intstring;
341 intstring.Printf( "--intstring%d", id );
342
343 if (!m_connection->Execute( intstring, -1 ) )
344 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
345
346 }
347
348 bool wxRemoteHtmlHelpController::AddBook(const wxString& book, bool show_wait_msg)
349 {
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;
361 }
362
363 bool wxRemoteHtmlHelpController::DisplayContents()
364 {
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;
372 }
373 void wxRemoteHtmlHelpController::DisplayIndex()
374 {
375 if( isconn_1 ) {
376 if (!m_connection->Poke( wxT("--DisplayIndex"), wxT("") ) )
377 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayIndex Failed"));
378 }
379 }
380 bool wxRemoteHtmlHelpController::KeywordSearch(const wxString& keyword)
381 {
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;
389 }
390
391 void wxRemoteHtmlHelpController::SetTitleFormat(const wxString& format)
392 {
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 }
400 }
401
402 void wxRemoteHtmlHelpController::SetTempDir(const wxString& path)
403 {
404 if( isconn_1 ) {
405 if (!m_connection->Poke( wxT("--SetTempDir"), (char*)path.c_str() ) )
406 wxLogError(wxT("wxRemoteHtmlHelpController - SetTempDir Failed"));
407 }
408 }
409