1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Remote help controller class
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/process.h"
26 #include "wx/confbase.h"
28 // Settings common to both executables: determines whether
29 // we're using TCP/IP or real DDE.
31 //#include "ddesetup.h"
32 //#define wxUSE_DDE_FOR_IPC 0
34 #if defined(__WXGTK__) || defined(__WXMOTIF__)
35 #include "mondrian.xpm"
41 #if !defined(USE_REMOTE)
42 #include <wx/html/helpctrl.h>
49 rhhcClient::rhhcClient( bool *isconn_a
)
54 wxConnectionBase
*rhhcClient::OnMakeConnection()
56 return new rhhcConnection( isconn_2
);
59 rhhcConnection::rhhcConnection( bool *isconn_a
)
66 rhhcConnection::~rhhcConnection()
71 bool rhhcConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, char *data
, int size
, wxIPCFormat format
)
76 bool rhhcConnection::OnDisconnect()
80 return wxConnection::OnDisconnect();
83 //////////////////////////////////////////
84 /////////////////////////////////////////
86 // wxRemoteHtmlHelpController class
88 IMPLEMENT_CLASS(wxRemoteHtmlHelpController
, wxHelpControllerBase
)
90 wxRemoteHtmlHelpController::wxRemoteHtmlHelpController(int style
)
101 // server app is assumed to be local
103 // for MSW (DDE classes), a_service is 'service name', apparently an arbitrary string
104 // for Unix, should be a valid file name (for a nonexistent file)
105 // for nonMSW, nonUnix, must be port number, for example "4242" (TCP/IP based classes)
106 // should be unique to the client app
108 wxString thename
= wxGetApp().GetAppName();
109 #if defined(__WXMSW__)
110 m_appname
= wxT("helpview.exe");
111 m_service
= thename
+ wxString(wxT("_helpservice"));
112 #elif defined(__UNIX__)
113 m_appname
= wxT("./helpview");
114 m_service
= wxT("/tmp/") + thename
+ wxString(wxT("_helpservice"));
116 m_appname
= wxT("./helpview");
117 m_service
= wxT("4242");
120 m_book
= thename
+ wxT(".hhp"); // or .htb or .zip
121 m_windowname
= thename
+ wxT(" Help: %s");
122 //underscores for spaces
123 m_windowname
.Replace( wxT(" "), wxT("_") );
126 void wxRemoteHtmlHelpController::SetService(wxString
& a_service
)
128 m_service
= a_service
;
130 void wxRemoteHtmlHelpController::SetServer(wxString
& a_appname
)
132 m_appname
= a_appname
;
135 void wxRemoteHtmlHelpController::OnQuit()
137 //kill the Server here?
138 //this function is not called ?
141 wxRemoteHtmlHelpController::~wxRemoteHtmlHelpController()
145 // if (!m_connection->Poke( wxT("--YouAreDead"), wxT("") ) )
146 // wxLogError(wxT("wxRemoteHtmlHelpController - YouAreDead Failed"));
148 // Kill the server. This could be an option.
151 m_connection
->Disconnect();
158 delete m_client
; //should be automatic?
162 bool wxRemoteHtmlHelpController::DoConnection()
169 // ignored under DDE, host name in TCP/IP based classes
170 wxString hostName
= wxT("localhost");
172 // Create a new client
173 if( !m_client
) m_client
= new rhhcClient(&isconn_1
);
177 // suppress the log messages from MakeConnection()
181 //first try to connect assuming server is running
183 m_connection
= (rhhcConnection
*)m_client
->MakeConnection(hostName
, m_service
, wxT("HELP") );
185 //if not, start server
189 stylestr
.Printf( wxT("--Style%d"), m_style
);
191 cmd
= m_appname
+ blank
+ m_service
+ blank
+ m_windowname
+ blank
+ m_book
+ blank
+ stylestr
;
193 m_process
= new wxProcess(NULL
);
194 m_pid
= wxExecute( cmd
, false, m_process
);
195 // leaks - wxExecute itself (if not deleted) and in wxExecute at
196 // wxExecuteData *data = new wxExecuteData;
198 wxLogError( wxT("wxRemoteHtmlHelpController - Failed to start Help server") );
205 //try every second for a while, then leave it to user
208 if ( wxMessageBox( wxT("Failed to make connection to Help server.\nRetry?") ,
209 wxT("wxRemoteHtmlHelpController Error"),
210 wxICON_ERROR
| wxYES_NO
| wxCANCEL
) != wxYES
)
218 m_connection
= (rhhcConnection
*)m_client
->MakeConnection(hostName
, m_service
, wxT("HELP") );
222 if (!m_connection
->StartAdvise(wxT("Item"))) {
223 wxLogError(wxT("wxRemoteHtmlHelpController - StartAdvise failed") );
230 bool wxRemoteHtmlHelpController::LoadFile(const wxString
& WXUNUSED(file
))
234 bool wxRemoteHtmlHelpController::DisplaySection(int sectionNo
)
239 bool wxRemoteHtmlHelpController::DisplayBlock(long blockNo
)
241 return DisplaySection((int)blockNo
);
244 bool wxRemoteHtmlHelpController::Quit()
246 //this code from exec sample - branches left in for testing
247 // sig = 3, 6, 9 or 12 all kill server with no apparent problem
248 // but give error message on MSW - timout?
249 int sig
= 15; //3 = quit; 6 = abort; 9 = kill; 15 = terminate
255 wxFAIL_MSG( wxT("unexpected return value") );
284 if ( wxProcess::Exists(m_pid
) )
286 wxLogStatus(wxT("Process %ld is running."), m_pid
);
290 wxLogStatus(wxT("No process with pid = %ld."), m_pid
);
295 wxKillError rc
= wxProcess::Kill(m_pid
, (wxSignal
)sig
);
296 if ( rc
== wxKILL_OK
)
298 wxLogStatus(wxT("Process %ld killed with signal %d."), m_pid
, sig
);
302 static const wxChar
*errorText
[] =
305 wxT("signal not supported"),
306 wxT("permission denied"),
307 wxT("no such process"),
308 wxT("unspecified error"),
311 // sig = 3, 6, 9 or 12 all kill server with no apparent problem
312 // but give error message on MSW - timout?
314 //wxLogError(wxT("Failed to kill process %ld with signal %d: %s"),
315 // m_pid, sig, errorText[rc]);
323 void wxRemoteHtmlHelpController::Display(const wxString
& helpfile
)
326 if( !DoConnection() ) return;
329 if (!m_connection
->Execute( helpfile
, -1 ) )
330 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
334 void wxRemoteHtmlHelpController::Display(const int id
)
337 if( !DoConnection() ) return;
341 intstring
.Printf( "--intstring%d", id
);
343 if (!m_connection
->Execute( intstring
, -1 ) )
344 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
348 bool wxRemoteHtmlHelpController::AddBook(const wxString
& book
, bool show_wait_msg
)
350 //ignore show_wait_msg - there shouldn't be a delay in this step
351 //show_wait_msg = true could be transmitted with ++AddBook
355 if (!m_connection
->Poke( wxT("--AddBook"), (char*)book
.c_str() ) )
357 wxLogError(wxT("wxRemoteHtmlHelpController - AddBook Failed"));
365 bool wxRemoteHtmlHelpController::DisplayContents()
368 if (!m_connection
->Poke( wxT("--DisplayContents"), wxT("") ) ) {
369 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayContents Failed"));
375 void wxRemoteHtmlHelpController::DisplayIndex()
378 if (!m_connection
->Poke( wxT("--DisplayIndex"), wxT("") ) )
380 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayIndex Failed"));
384 bool wxRemoteHtmlHelpController::KeywordSearch(const wxString
& keyword
)
387 if (!m_connection
->Poke( wxT("--KeywordSearch"), (char*)keyword
.c_str() ) ) {
388 wxLogError(wxT("wxRemoteHtmlHelpController - KeywordSearch Failed"));
395 void wxRemoteHtmlHelpController::SetTitleFormat(const wxString
& format
)
397 m_windowname
= format
;
398 m_windowname
.Replace( wxT(" "), wxT("_") );
401 if (!m_connection
->Poke( wxT("--SetTitleFormat"), (char*)format
.c_str() ) )
403 wxLogError(wxT("wxRemoteHtmlHelpController - SetTitleFormat Failed"));
408 void wxRemoteHtmlHelpController::SetTempDir(const wxString
& path
)
411 if (!m_connection
->Poke( wxT("--SetTempDir"), (char*)path
.c_str() ) )
413 wxLogError(wxT("wxRemoteHtmlHelpController - SetTempDir Failed"));