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( _T("unexpected return value") );
284 if ( wxProcess::Exists(m_pid
) )
285 wxLogStatus(_T("Process %ld is running."), m_pid
);
287 wxLogStatus(_T("No process with pid = %ld."), m_pid
);
291 wxKillError rc
= wxProcess::Kill(m_pid
, (wxSignal
)sig
);
292 if ( rc
== wxKILL_OK
)
294 wxLogStatus(_T("Process %ld killed with signal %d."), m_pid
, sig
);
298 static const wxChar
*errorText
[] =
301 _T("signal not supported"),
302 _T("permission denied"),
303 _T("no such process"),
304 _T("unspecified error"),
307 // sig = 3, 6, 9 or 12 all kill server with no apparent problem
308 // but give error message on MSW - timout?
310 //wxLogError(_T("Failed to kill process %ld with signal %d: %s"),
311 // m_pid, sig, errorText[rc]);
319 void wxRemoteHtmlHelpController::Display(const wxString
& helpfile
)
322 if( !DoConnection() ) return;
325 if (!m_connection
->Execute( helpfile
, -1 ) )
326 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
330 void wxRemoteHtmlHelpController::Display(const int id
)
333 if( !DoConnection() ) return;
337 intstring
.Printf( "--intstring%d", id
);
339 if (!m_connection
->Execute( intstring
, -1 ) )
340 wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));
344 bool wxRemoteHtmlHelpController::AddBook(const wxString
& book
, bool show_wait_msg
)
346 //ignore show_wait_msg - there shouldn't be a delay in this step
347 //show_wait_msg = true could be transmitted with ++AddBook
351 if (!m_connection
->Poke( wxT("--AddBook"), (char*)book
.c_str() ) )
352 wxLogError(wxT("wxRemoteHtmlHelpController - AddBook Failed"));
359 bool wxRemoteHtmlHelpController::DisplayContents()
362 if (!m_connection
->Poke( wxT("--DisplayContents"), wxT("") ) ) {
363 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayContents Failed"));
369 void wxRemoteHtmlHelpController::DisplayIndex()
372 if (!m_connection
->Poke( wxT("--DisplayIndex"), wxT("") ) )
373 wxLogError(wxT("wxRemoteHtmlHelpController - DisplayIndex Failed"));
376 bool wxRemoteHtmlHelpController::KeywordSearch(const wxString
& keyword
)
379 if (!m_connection
->Poke( wxT("--KeywordSearch"), (char*)keyword
.c_str() ) ) {
380 wxLogError(wxT("wxRemoteHtmlHelpController - KeywordSearch Failed"));
387 void wxRemoteHtmlHelpController::SetTitleFormat(const wxString
& format
)
389 m_windowname
= format
;
390 m_windowname
.Replace( wxT(" "), wxT("_") );
393 if (!m_connection
->Poke( wxT("--SetTitleFormat"), (char*)format
.c_str() ) )
394 wxLogError(wxT("wxRemoteHtmlHelpController - SetTitleFormat Failed"));
398 void wxRemoteHtmlHelpController::SetTempDir(const wxString
& path
)
401 if (!m_connection
->Poke( wxT("--SetTempDir"), (char*)path
.c_str() ) )
402 wxLogError(wxT("wxRemoteHtmlHelpController - SetTempDir Failed"));