1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Remote help controller class
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
24 #include "wx/process.h"
25 #include "wx/confbase.h"
27 // Settings common to both executables: determines whether
28 // we're using TCP/IP or real DDE.
30 //#include "ddesetup.h"
31 //#define wxUSE_DDE_FOR_IPC 0
33 #ifndef wxHAS_IMAGES_IN_RESOURCES
34 #include "mondrian.xpm"
40 #if !defined(USE_REMOTE)
41 #include <wx/html/helpctrl.h>
48 rhhcClient::rhhcClient( bool *isconn_a
)
53 wxConnectionBase
*rhhcClient::OnMakeConnection()
55 return new rhhcConnection( isconn_2
);
58 rhhcConnection::rhhcConnection( bool *isconn_a
)
65 rhhcConnection::~rhhcConnection()
70 bool rhhcConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, char *data
, int size
, wxIPCFormat format
)
75 bool rhhcConnection::OnDisconnect()
79 return wxConnection::OnDisconnect();
82 //////////////////////////////////////////
83 /////////////////////////////////////////
85 // wxRemoteHtmlHelpController class
87 IMPLEMENT_CLASS(wxRemoteHtmlHelpController
, wxHelpControllerBase
)
89 wxRemoteHtmlHelpController::wxRemoteHtmlHelpController(int style
)
100 // server app is assumed to be local
102 // for MSW (DDE classes), a_service is 'service name', apparently an arbitrary string
103 // for Unix, should be a valid file name (for a nonexistent file)
104 // for nonMSW, nonUnix, must be port number, for example "4242" (TCP/IP based classes)
105 // should be unique to the client app
107 wxString thename
= wxGetApp().GetAppName();
108 #if defined(__WXMSW__)
109 m_appname
= wxT("helpview.exe");
110 m_service
= thename
+ wxString(wxT("_helpservice"));
111 #elif defined(__UNIX__)
112 m_appname
= wxT("./helpview");
113 m_service
= wxT("/tmp/") + thename
+ wxString(wxT("_helpservice"));
115 m_appname
= wxT("./helpview");
116 m_service
= wxT("4242");
119 m_book
= thename
+ wxT(".hhp"); // or .htb or .zip
120 m_windowname
= thename
+ wxT(" Help: %s");
121 //underscores for spaces
122 m_windowname
.Replace( wxT(" "), wxT("_") );
125 void wxRemoteHtmlHelpController::SetService(wxString
& a_service
)
127 m_service
= a_service
;
129 void wxRemoteHtmlHelpController::SetServer(wxString
& a_appname
)
131 m_appname
= a_appname
;
134 void wxRemoteHtmlHelpController::OnQuit()
136 //kill the Server here?
137 //this function is not called ?
140 wxRemoteHtmlHelpController::~wxRemoteHtmlHelpController()
144 // if (!m_connection->Poke( wxT("--YouAreDead"), wxT("") ) )
145 // wxLogError(wxT("wxRemoteHtmlHelpController - YouAreDead Failed"));
147 // Kill the server. This could be an option.
150 m_connection
->Disconnect();
157 delete m_client
; //should be automatic?
161 bool wxRemoteHtmlHelpController::DoConnection()
168 // ignored under DDE, host name in TCP/IP based classes
169 wxString hostName
= wxT("localhost");
171 // Create a new client
172 if( !m_client
) m_client
= new rhhcClient(&isconn_1
);
176 // suppress the log messages from MakeConnection()
180 //first try to connect assuming server is running
182 m_connection
= (rhhcConnection
*)m_client
->MakeConnection(hostName
, m_service
, wxT("HELP") );
184 //if not, start server
188 stylestr
.Printf( wxT("--Style%d"), m_style
);
190 cmd
= m_appname
+ blank
+ m_service
+ blank
+ m_windowname
+ blank
+ m_book
+ blank
+ stylestr
;
192 m_process
= new wxProcess(NULL
);
193 m_pid
= wxExecute( cmd
, false, m_process
);
194 // leaks - wxExecute itself (if not deleted) and in wxExecute at
195 // wxExecuteData *data = new wxExecuteData;
197 wxLogError( wxT("wxRemoteHtmlHelpController - Failed to start Help server") );
204 //try every second for a while, then leave it to user
207 if ( wxMessageBox( wxT("Failed to make connection to Help server.\nRetry?") ,
208 wxT("wxRemoteHtmlHelpController Error"),
209 wxICON_ERROR
| wxYES_NO
| wxCANCEL
) != wxYES
)
217 m_connection
= (rhhcConnection
*)m_client
->MakeConnection(hostName
, m_service
, wxT("HELP") );
221 if (!m_connection
->StartAdvise(wxT("Item"))) {
222 wxLogError(wxT("wxRemoteHtmlHelpController - StartAdvise failed") );
229 bool wxRemoteHtmlHelpController::LoadFile(const wxString
& WXUNUSED(file
))
233 bool wxRemoteHtmlHelpController::DisplaySection(int sectionNo
)
238 bool wxRemoteHtmlHelpController::DisplayBlock(long blockNo
)
240 return DisplaySection((int)blockNo
);
243 bool wxRemoteHtmlHelpController::Quit()
245 //this code from exec sample - branches left in for testing
246 // sig = 3, 6, 9 or 12 all kill server with no apparent problem
247 // but give error message on MSW - timout?
248 int sig
= 15; //3 = quit; 6 = abort; 9 = kill; 15 = terminate
254 wxFAIL_MSG( wxT("unexpected return value") );
283 if ( wxProcess::Exists(m_pid
) )
285 wxLogStatus(wxT("Process %ld is running."), m_pid
);
289 wxLogStatus(wxT("No process with pid = %ld."), m_pid
);
294 wxKillError rc
= wxProcess::Kill(m_pid
, (wxSignal
)sig
);
295 if ( rc
== wxKILL_OK
)
297 wxLogStatus(wxT("Process %ld killed with signal %d."), m_pid
, sig
);
301 static const wxChar
*errorText
[] =
304 wxT("signal not supported"),
305 wxT("permission denied"),
306 wxT("no such process"),
307 wxT("unspecified error"),
310 // sig = 3, 6, 9 or 12 all kill server with no apparent problem
311 // but give error message on MSW - timout?
313 //wxLogError(wxT("Failed to kill process %ld with signal %d: %s"),
314 // m_pid, sig, errorText[rc]);
322 void wxRemoteHtmlHelpController::Display(const wxString
& helpfile
)
325 if( !DoConnection() ) return;
328 if (!m_connection
->Execute( helpfile
, -1 ) )
329 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
333 void wxRemoteHtmlHelpController::Display(const int id
)
336 if( !DoConnection() ) return;
340 intstring
.Printf( "--intstring%d", id
);
342 if (!m_connection
->Execute( intstring
, -1 ) )
343 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
347 bool wxRemoteHtmlHelpController::AddBook(const wxString
& book
, bool show_wait_msg
)
349 //ignore show_wait_msg - there shouldn't be a delay in this step
350 //show_wait_msg = true could be transmitted with ++AddBook
354 if (!m_connection
->Poke( wxT("--AddBook"), (char*)book
.c_str() ) )
356 wxLogError(wxT("wxRemoteHtmlHelpController - AddBook Failed"));
364 bool wxRemoteHtmlHelpController::DisplayContents()
367 if (!m_connection
->Poke( wxT("--DisplayContents"), wxT("") ) ) {
368 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayContents Failed"));
374 void wxRemoteHtmlHelpController::DisplayIndex()
377 if (!m_connection
->Poke( wxT("--DisplayIndex"), wxT("") ) )
379 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayIndex Failed"));
383 bool wxRemoteHtmlHelpController::KeywordSearch(const wxString
& keyword
)
386 if (!m_connection
->Poke( wxT("--KeywordSearch"), (char*)keyword
.c_str() ) ) {
387 wxLogError(wxT("wxRemoteHtmlHelpController - KeywordSearch Failed"));
394 void wxRemoteHtmlHelpController::SetTitleFormat(const wxString
& format
)
396 m_windowname
= format
;
397 m_windowname
.Replace( wxT(" "), wxT("_") );
400 if (!m_connection
->Poke( wxT("--SetTitleFormat"), (char*)format
.c_str() ) )
402 wxLogError(wxT("wxRemoteHtmlHelpController - SetTitleFormat Failed"));
407 void wxRemoteHtmlHelpController::SetTempDir(const wxString
& path
)
410 if (!m_connection
->Poke( wxT("--SetTempDir"), (char*)path
.c_str() ) )
412 wxLogError(wxT("wxRemoteHtmlHelpController - SetTempDir Failed"));