5 * Nicolas Boichat, April 2004
9 Copyright (C) 2004 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "wxbmainframe.h" // class's header file
28 #include "wxbrestorepanel.h"
32 #include "wxwin16x16.xpm"
34 #include <wx/arrimpl.cpp>
36 #include <wx/stattext.h>
38 // ----------------------------------------------------------------------------
39 // event tables and other macros for wxWindows
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // IDs for the controls and the menu commands
52 // it is important for the id corresponding to the "About" command to have
53 // this standard value as otherwise it won't be handled properly under Mac
54 // (where it is special and put into the "Apple" menu)
55 Minimal_About = wxID_ABOUT,
62 * wxbTHREAD_EVENT declaration, used by csprint
64 BEGIN_DECLARE_EVENT_TYPES()
65 DECLARE_EVENT_TYPE(wxbTHREAD_EVENT, 1)
66 END_DECLARE_EVENT_TYPES()
68 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
70 // the event tables connect the wxWindows events with the functions (event
71 // handlers) which process them. It can be also done at run-time, but for the
72 // simple menu events like this the static method is much simpler.
73 BEGIN_EVENT_TABLE(wxbMainFrame, wxFrame)
74 EVT_MENU(Minimal_Quit, wxbMainFrame::OnQuit)
75 EVT_MENU(Minimal_About, wxbMainFrame::OnAbout)
76 EVT_TEXT_ENTER(TypeText, wxbMainFrame::OnEnter)
77 EVT_BUTTON(SendButton, wxbMainFrame::OnEnter)
78 EVT_CUSTOM(wxbTHREAD_EVENT, Thread, wxbMainFrame::OnPrint)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 * wxbThreadEvent constructor
88 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
93 * wxbThreadEvent destructor
95 wxbThreadEvent::~wxbThreadEvent()
97 if (m_eventObject != NULL) {
103 * wxbThreadEvent copy constructor
105 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
107 this->m_eventType = te.m_eventType;
108 this->m_id = te.m_id;
109 if (te.m_eventObject != NULL) {
110 this->m_eventObject = new wxbPrintObject(*((wxbPrintObject*)te.m_eventObject));
113 this->m_eventObject = NULL;
115 this->m_skipped = te.m_skipped;
116 this->m_timeStamp = te.m_timeStamp;
120 * Must be implemented (abstract in wxEvent)
122 wxEvent* wxbThreadEvent::Clone() const
124 return new wxbThreadEvent(*this);
128 * Gets the wxbPrintObject attached to this event, containing data sent by director
130 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
132 return (wxbPrintObject*)m_eventObject;
136 * Sets the wxbPrintObject attached to this event
138 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
140 m_eventObject = (wxObject*)object;
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 wxbMainFrame *wxbMainFrame::frame = NULL;
150 * Singleton constructor
152 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
154 frame = new wxbMainFrame(title, pos, size, style);
159 * Returns singleton instance
161 wxbMainFrame* wxbMainFrame::GetInstance()
169 wxbMainFrame::~wxbMainFrame()
171 if (ct != NULL) { // && (!ct->IsRunning())
178 * Private constructor
180 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
181 : wxFrame(NULL, -1, title, pos, size, style)
185 // set the frame icon
186 SetIcon(wxIcon(wxwin16x16_xpm));
190 wxMenu *menuFile = new wxMenu;
192 // the "About" item should be in the help menu
193 wxMenu *helpMenu = new wxMenu;
194 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
196 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
198 // now append the freshly created menu to the menu bar...
199 wxMenuBar *menuBar = new wxMenuBar();
200 menuBar->Append(menuFile, _T("&File"));
201 menuBar->Append(helpMenu, _T("&Help"));
203 // ... and attach this menu bar to the frame
205 #endif // wxUSE_MENUS
208 SetStatusText(wxString("Welcome to bacula wx-console ") << VERSION << " (" << BDATE << ")!\n");
210 wxPanel* global = new wxPanel(this, -1);
212 notebook = new wxNotebook(global, -1);
216 wxPanel* consolePanel = new wxPanel(notebook, -1);
217 notebook->AddPage(consolePanel, "Console");
219 consoleCtrl = new wxTextCtrl(consolePanel,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
220 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, wxFont(10, wxMODERN, wxNORMAL, wxNORMAL)));
222 wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(2, 1, 0, 0);
223 consoleSizer->AddGrowableCol(0);
224 consoleSizer->AddGrowableRow(0);
226 typeCtrl = new wxTextCtrl(consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20), wxTE_PROCESS_ENTER);
227 sendButton = new wxButton(consolePanel, SendButton, "Send");
229 wxFlexGridSizer *typeSizer = new wxFlexGridSizer(1, 3, 0, 0);
230 typeSizer->AddGrowableCol(1);
231 typeSizer->AddGrowableRow(0);
233 typeSizer->Add(new wxStaticText(consolePanel, -1, "Command: "), 0, wxALIGN_CENTER | wxALL, 0);
234 typeSizer->Add(typeCtrl, 1, wxEXPAND | wxALL, 0);
235 typeSizer->Add(sendButton, 1, wxEXPAND | wxLEFT, 5);
237 consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
238 consoleSizer->Add(typeSizer, 0, wxEXPAND | wxALL, 0);
240 consolePanel->SetAutoLayout( TRUE );
241 consolePanel->SetSizer( consoleSizer );
242 consoleSizer->SetSizeHints( consolePanel );
244 // Creates the list of panels which are included in notebook, and that need to receive director information
246 panels = new wxbPanel* [2];
247 panels[0] = new wxbRestorePanel(notebook);
250 for (int i = 0; panels[i] != NULL; i++) {
251 notebook->AddPage(panels[i], panels[i]->GetTitle());
254 wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
256 globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
258 global->SetSizer( globalSizer );
259 globalSizer->SetSizeHints( global );
261 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
263 sizer->Add(global, 1, wxEXPAND | wxALL, 0);
266 sizer->SetSizeHints( this );
268 EnableConsole(false);
270 lockedbyconsole = false;
276 * Starts the thread interacting with the director
278 void wxbMainFrame::StartConsoleThread()
284 promptparser = new wxbPromptParser();
286 ct = new console_thread();
289 SetStatusText("Connecting to the director...");
292 /* Register a new wxbDataParser */
293 void wxbMainFrame::Register(wxbDataParser* dp) {
297 /* Unregister a wxbDataParser */
298 void wxbMainFrame::Unregister(wxbDataParser* dp) {
300 if ((index = parsers.Index(dp)) != wxNOT_FOUND) {
301 parsers.RemoveAt(index);
304 Print("Failed to unregister a data parser !", CS_DEBUG);
310 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
312 Print("Quitting.\n", CS_DEBUG);
321 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
324 msg.Printf( _T("Welcome to Bacula wx-console.\nWritten by Nicolas Boichat <nicolas@boichat.ch>\n(C) 2004 Kern Sibbald and John Walker\n"));
326 wxMessageBox(msg, _T("About Bacula wx-console"), wxOK | wxICON_INFORMATION, this);
329 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
331 lockedbyconsole = true;
333 wxString str = typeCtrl->GetValue() + "\n";
338 * Called when data is arriving from director
340 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
341 wxbPrintObject* po = event.GetEventPrintObject();
343 Print(po->str, po->status);
347 * Prints data received from director to the console, and forwards it to the panels
349 void wxbMainFrame::Print(wxString str, int status)
351 if (lockedbyconsole) {
352 EnableConsole(false);
355 if (status == CS_TERMINATED) {
356 consoleCtrl->AppendText(consoleBuffer);
358 SetStatusText("Console thread terminated.");
361 int answer = wxMessageBox("Connection to the director lost. Quit program?", "Connection lost",
362 wxYES_NO | wxICON_EXCLAMATION, this);
363 if (answer == wxYES) {
370 if (status == CS_CONNECTED) {
371 SetStatusText("Connected to the director.");
375 if (status == CS_DISCONNECTED) {
376 consoleCtrl->AppendText(consoleBuffer);
378 SetStatusText("Disconnected of the director.");
383 // CS_DEBUG is often sent by panels,
384 // and resend it to them would sometimes cause infinite loops
386 /* One promptcaught is normal, so we must have two true Print values to be
387 * sure that the prompt has effectively been caught.
389 int promptcaught = -1;
391 if (status != CS_DEBUG) {
392 for (unsigned int i = 0; i < parsers.GetCount(); i++) {
393 promptcaught += parsers[i]->Print(str, status) ? 1 : 0;
396 if ((status == CS_PROMPT) && (promptcaught < 1) && (promptparser->isPrompt())) {
397 Print("Unexpected question has been received.\n", CS_DEBUG);
398 // Print(wxString("(") << promptparser->getIntroString() << "/-/" << promptparser->getQuestionString() << ")\n", CS_DEBUG);
401 if (promptparser->getIntroString() != "") {
402 message << promptparser->getIntroString() << "\n";
404 message << promptparser->getQuestionString();
406 if (promptparser->getChoices()) {
407 wxString *choices = new wxString[promptparser->getChoices()->GetCount()];
408 int *numbers = new int[promptparser->getChoices()->GetCount()];
411 for (unsigned int i = 0; i < promptparser->getChoices()->GetCount(); i++) {
412 if ((*promptparser->getChoices())[i] != "") {
413 choices[n] = (*promptparser->getChoices())[i];
419 int res = ::wxGetSingleChoiceIndex(message,
420 "wx-console: unexpected director's question.", n, choices, this);
425 Send(wxString() << numbers[res] << "\n");
429 Send(::wxGetTextFromUser(message,
430 "wx-console: unexpected director's question.", "", this) + "\n");
435 if (status == CS_END) {
436 if (lockedbyconsole) {
438 lockedbyconsole = false;
443 if (status == CS_DEBUG) {
444 consoleCtrl->AppendText(consoleBuffer);
446 consoleCtrl->SetDefaultStyle(wxTextAttr(wxColour(0, 128, 0)));
449 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
451 consoleBuffer << str;
452 if (status == CS_PROMPT) {
453 if (lockedbyconsole) {
456 consoleBuffer << "<P>";
459 if ((status == CS_END) || (status == CS_PROMPT) || (str.Find("\n") > -1)) {
460 consoleCtrl->AppendText(consoleBuffer);
463 consoleCtrl->ScrollLines(3);
466 // consoleCtrl->ShowPosition(consoleCtrl->GetLastPosition());
468 /*if (status != CS_DEBUG) {
469 consoleCtrl->AppendText("@");
471 //consoleCtrl->SetInsertionPointEnd();
473 /* if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
474 nlines = consoleCtrl->GetNumberOfLines()-1;
477 if (status == CS_END) {
478 consoleCtrl->ShowPosition(nlines);
483 * Sends data to the director
485 void wxbMainFrame::Send(wxString str)
488 ct->Write((const char*)str);
489 typeCtrl->SetValue("");
490 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
491 consoleCtrl->AppendText(str);
492 consoleCtrl->ScrollLines(3);
495 /* if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
496 nlines = consoleCtrl->GetNumberOfLines()-1;
499 consoleCtrl->ShowPosition(nlines);*/
503 void wxbMainFrame::EnablePanels() {
504 for (int i = 0; panels[i] != NULL; i++) {
505 panels[i]->EnablePanel(true);
510 /* Disable panels, except the one passed as parameter */
511 void wxbMainFrame::DisablePanels(void* except) {
512 for (int i = 0; panels[i] != NULL; i++) {
513 if (panels[i] != except) {
514 panels[i]->EnablePanel(false);
517 panels[i]->EnablePanel(true);
520 if (this != except) {
521 EnableConsole(false);
525 /* Enable or disable console typing */
526 void wxbMainFrame::EnableConsole(bool enable) {
527 typeCtrl->Enable(enable);
528 sendButton->Enable(enable);
530 typeCtrl->SetFocus();
535 * Used by csprint, which is called by console thread.
537 * In GTK and perhaps X11, only the main thread is allowed to interact with
538 * graphical components, so by firing an event, the main loop will call OnPrint.
540 * Calling OnPrint directly from console thread produces "unexpected async replies".
542 void firePrintEvent(wxString str, int status)
544 wxbPrintObject* po = new wxbPrintObject(str, status);
546 wxbThreadEvent evt(Thread);
547 evt.SetEventPrintObject(po);
549 if (wxbMainFrame::GetInstance()) {
550 wxbMainFrame::GetInstance()->AddPendingEvent(evt);
554 //wxString csBuffer; /* Temporary buffer for receiving data from console thread */
557 * Called by console thread, this function forwards data line by line and end
558 * signals to the GUI.
560 void csprint(const char* str, int status)
563 firePrintEvent(wxString(str), status);
566 firePrintEvent("", status);