File: remhelp.cpp

package info (click to toggle)
wxpython4.0 4.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 211,112 kB
  • sloc: cpp: 888,355; python: 223,130; makefile: 52,087; ansic: 45,780; sh: 3,012; xml: 1,534; perl: 264
file content (416 lines) | stat: -rw-r--r-- 11,101 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/////////////////////////////////////////////////////////////////////////////
// Name:        remhelp.cpp
// Purpose:     Remote help controller class
// Author:      Eric Dowty
// Modified by:
// Created:     2002-11-18
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <math.h>

#include "wx/process.h"
#include "wx/confbase.h"

// Settings common to both executables: determines whether
// we're using TCP/IP or real DDE.

//#include "ddesetup.h"
//#define wxUSE_DDE_FOR_IPC 0

#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "mondrian.xpm"
#endif

#include "remhelp.h"
#include "client.h"

#if !defined(USE_REMOTE)
#include <wx/html/helpctrl.h>
#endif

//////////////////
//////////////////
// helper classes

rhhcClient::rhhcClient( bool *isconn_a )
{
    isconn_2 = isconn_a;
}

wxConnectionBase *rhhcClient::OnMakeConnection()
{
    return new rhhcConnection( isconn_2 );
}

rhhcConnection::rhhcConnection( bool *isconn_a )
: wxConnection()
{
    isconn_3 = isconn_a;
    *isconn_3 = true;
}

rhhcConnection::~rhhcConnection()
{
    *isconn_3 = false;
}

bool rhhcConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
{
    return true;
}

bool rhhcConnection::OnDisconnect()
{
    *isconn_3 = false;

    return wxConnection::OnDisconnect();
}

//////////////////////////////////////////
/////////////////////////////////////////

// wxRemoteHtmlHelpController class

IMPLEMENT_CLASS(wxRemoteHtmlHelpController, wxHelpControllerBase)

wxRemoteHtmlHelpController::wxRemoteHtmlHelpController(int style )
{
    m_style = style;
    m_connection = NULL;
    m_client  = NULL;
    m_pid = 0;
    isconn_1 = false;
    m_process = NULL;

    // defaults
    //
    // server app is assumed to be local
    //
    // for MSW (DDE classes), a_service is 'service name', apparently an arbitrary string
    // for Unix, should be a valid file name (for a nonexistent file)
    // for nonMSW, nonUnix, must be port number, for example "4242" (TCP/IP based classes)
    // should be unique to the client app

    wxString thename = wxGetApp().GetAppName();
#if defined(__WXMSW__)
    m_appname = wxT("helpview.exe");
    m_service = thename + wxString(wxT("_helpservice"));
#elif defined(__UNIX__)
    m_appname = wxT("./helpview");
    m_service = wxT("/tmp/") + thename + wxString(wxT("_helpservice"));
#else
    m_appname = wxT("./helpview");
    m_service = wxT("4242");
#endif

    m_book = thename + wxT(".hhp");  // or .htb or .zip
    m_windowname = thename + wxT(" Help: %s");
    //underscores for spaces
    m_windowname.Replace( wxT(" "), wxT("_") );
}

void wxRemoteHtmlHelpController::SetService(wxString& a_service)
{
    m_service = a_service;
}
void wxRemoteHtmlHelpController::SetServer(wxString& a_appname)
{
    m_appname = a_appname;
}

void wxRemoteHtmlHelpController::OnQuit()
{
    //kill the Server here?
    //this function is not called ?
}

wxRemoteHtmlHelpController::~wxRemoteHtmlHelpController()
{
    if ( isconn_1 )
    {
        // if (!m_connection->Poke( wxT("--YouAreDead"), wxT("") ) )
        // wxLogError(wxT("wxRemoteHtmlHelpController - YouAreDead Failed"));

        // Kill the server.  This could be an option.
        Quit();

        m_connection->Disconnect();
        delete m_connection;

        delete m_process;
        m_process = NULL;
    }
    if( m_client )
        delete m_client; //should be automatic?

}

bool wxRemoteHtmlHelpController::DoConnection()
{
    wxString cmd, blank;
    int nsleep;

    blank = wxT("  ");

    // ignored under DDE, host name in TCP/IP based classes
    wxString hostName = wxT("localhost");

    // Create a new client
    if( !m_client ) m_client = new rhhcClient(&isconn_1);

    nsleep = 0;

    // suppress the log messages from MakeConnection()
    {
        wxLogNull nolog;

        //first try to connect assuming server is running
        if( !isconn_1 )
            m_connection = (rhhcConnection *)m_client->MakeConnection(hostName, m_service, wxT("HELP") );

        //if not, start server
        if( !isconn_1 ) {

            wxString stylestr;
            stylestr.Printf( wxT("--Style%d"), m_style );

            cmd = m_appname + blank + m_service + blank + m_windowname + blank + m_book + blank + stylestr;

            m_process = new wxProcess(NULL);
            m_pid = wxExecute( cmd, false, m_process );
            // leaks - wxExecute itself (if not deleted) and in wxExecute at
            // wxExecuteData *data = new wxExecuteData;
            if( m_pid <= 0 ) {
                wxLogError( wxT("wxRemoteHtmlHelpController - Failed to start Help server") );
                return false;
            }
        }

        while ( !isconn_1 )
        {
            //try every second for a while, then leave it to user
            wxSleep(1);
            if( nsleep > 4 ) {
                if ( wxMessageBox( wxT("Failed to make connection to Help server.\nRetry?") ,
                                   wxT("wxRemoteHtmlHelpController Error"),
                                   wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
                {
                    // no server
                    return false;
                }
            }
            nsleep++;

            m_connection = (rhhcConnection *)m_client->MakeConnection(hostName, m_service, wxT("HELP") );
        }
    }

    if (!m_connection->StartAdvise(wxT("Item"))) {
        wxLogError(wxT("wxRemoteHtmlHelpController - StartAdvise failed") );
        return false;
    }

    return true;
}

bool wxRemoteHtmlHelpController::LoadFile(const wxString& WXUNUSED(file))
{
    return true;
}
bool wxRemoteHtmlHelpController::DisplaySection(int sectionNo)
{
    Display(sectionNo);
    return true;
}
bool wxRemoteHtmlHelpController::DisplayBlock(long blockNo)
{
    return DisplaySection((int)blockNo);
}

bool wxRemoteHtmlHelpController::Quit()
{
    //this code from exec sample - branches left in for testing
    // sig = 3, 6, 9 or 12 all kill server with no apparent problem
    // but give error message on MSW - timout?
    int sig = 15;   //3 = quit; 6 = abort; 9 = kill;  15 = terminate

/*
                    switch ( sig )
                    {
                    default:
                    wxFAIL_MSG( wxT("unexpected return value") );
                    // fall through

                      case -1:
                      // cancelled
                      return false;

                        case wxSIGNONE:
                        case wxSIGHUP:
                        case wxSIGINT:
                        case wxSIGQUIT:
                        case wxSIGILL:
                        case wxSIGTRAP:
                        case wxSIGABRT:
                        case wxSIGEMT:
                        case wxSIGFPE:
                        case wxSIGKILL:
                        case wxSIGBUS:
                        case wxSIGSEGV:
                        case wxSIGSYS:
                        case wxSIGPIPE:
                        case wxSIGALRM:
                        case wxSIGTERM:
                        break;
                        }
*/

    if ( sig == 0 )
    {
        if ( wxProcess::Exists(m_pid) )
        {
            wxLogStatus(wxT("Process %ld is running."), m_pid);
        }
        else
        {
            wxLogStatus(wxT("No process with pid = %ld."), m_pid);
        }
    }
    else // not SIGNONE
    {
        wxKillError rc = wxProcess::Kill(m_pid, (wxSignal)sig);
        if ( rc == wxKILL_OK )
        {
            wxLogStatus(wxT("Process %ld killed with signal %d."), m_pid, sig);
        }
        else
        {
            static const wxChar *errorText[] =
            {
                wxT(""), // no error
                    wxT("signal not supported"),
                    wxT("permission denied"),
                    wxT("no such process"),
                    wxT("unspecified error"),
            };

            // sig = 3, 6, 9 or 12 all kill server with no apparent problem
            // but give error message on MSW - timout?
            //
            //wxLogError(wxT("Failed to kill process %ld with signal %d: %s"),
            //            m_pid, sig, errorText[rc]);
        }
    }


    return true;
}

void wxRemoteHtmlHelpController::Display(const wxString& helpfile)
{
    if( !isconn_1 ) {
        if( !DoConnection() ) return;
    }

    if (!m_connection->Execute( helpfile, -1 ) )
        wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));

}

void wxRemoteHtmlHelpController::Display(const int id)
{
    if( !isconn_1 ) {
        if( !DoConnection() ) return;
    }

    wxString intstring;
    intstring.Printf( "--intstring%d", id );

    if (!m_connection->Execute( intstring, -1 ) )
        wxLogError(wxT("wxRemoteHtmlHelpController - Display Failed"));

}

bool wxRemoteHtmlHelpController::AddBook(const wxString& book, bool show_wait_msg)
{
    //ignore show_wait_msg - there shouldn't be a delay in this step
    //show_wait_msg = true could be transmitted with ++AddBook
    m_book = book;

    if( isconn_1 ) {
        if (!m_connection->Poke( wxT("--AddBook"), (char*)book.c_str() ) )
        {
            wxLogError(wxT("wxRemoteHtmlHelpController - AddBook Failed"));
        }
        return false;
    }

    return true;
}

bool wxRemoteHtmlHelpController::DisplayContents()
{
    if( isconn_1 ) {
        if (!m_connection->Poke( wxT("--DisplayContents"), wxT("") ) ) {
            wxLogError(wxT("wxRemoteHtmlHelpController - DisplayContents Failed"));
            return false;
        }
    }
    return true;
}
void wxRemoteHtmlHelpController::DisplayIndex()
{
    if( isconn_1 ) {
        if (!m_connection->Poke( wxT("--DisplayIndex"), wxT("") ) )
        {
            wxLogError(wxT("wxRemoteHtmlHelpController - DisplayIndex Failed"));
        }
    }
}
bool wxRemoteHtmlHelpController::KeywordSearch(const wxString& keyword)
{
    if( isconn_1 ) {
        if (!m_connection->Poke( wxT("--KeywordSearch"), (char*)keyword.c_str() ) ) {
            wxLogError(wxT("wxRemoteHtmlHelpController - KeywordSearch Failed"));
            return false;
        }
    }
    return true;
}

void wxRemoteHtmlHelpController::SetTitleFormat(const wxString& format)
{
    m_windowname = format;
    m_windowname.Replace( wxT(" "), wxT("_") );

    if( isconn_1 ) {
        if (!m_connection->Poke( wxT("--SetTitleFormat"), (char*)format.c_str() ) )
        {
            wxLogError(wxT("wxRemoteHtmlHelpController - SetTitleFormat Failed"));
        }
    }
}

void wxRemoteHtmlHelpController::SetTempDir(const wxString& path)
{
    if( isconn_1 ) {
        if (!m_connection->Poke( wxT("--SetTempDir"), (char*)path.c_str() ) )
        {
            wxLogError(wxT("wxRemoteHtmlHelpController - SetTempDir Failed"));
        }
    }
}