]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
Change Groupbox name to Remote Director in Configuration page if Director is not...
[bacula/bacula] / bacula / src / wx-console / wxbconfigfileeditor.cpp
1 /*
2  *
3  *    Configuration file editor
4  *
5  *    Nicolas Boichat, May 2004
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2004-2006 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
22  */
23
24 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
25  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
26  * So we turn _DEBUG off since we aren't interested in things it enables.
27  */
28
29 #undef _DEBUG
30
31 #include "bacula.h"
32 #include "wxbconfigfileeditor.h"
33 #include <wx/file.h>
34 #include <wx/filename.h>
35
36
37 enum
38 {
39    Save = 1,
40    Quit = 2
41 };
42
43 BEGIN_EVENT_TABLE(wxbConfigFileEditor, wxDialog)
44    EVT_BUTTON(Save, wxbConfigFileEditor::OnSave)
45    EVT_BUTTON(Quit, wxbConfigFileEditor::OnQuit)
46 #ifdef HAVE_WIN32
47    EVT_PAINT (      wxbConfigFileEditor::OnPaint)
48 #endif
49 END_EVENT_TABLE()
50
51 wxbConfigFileEditor::wxbConfigFileEditor(wxWindow* parent, wxString filename):
52       wxDialog(parent, -1, _("Config file editor"), wxDefaultPosition, wxSize(500, 300),
53                    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
54    this->filename = filename;
55    
56    wxString strbuf;
57
58    wxFileName filen(filename);
59    
60    if (!filen.FileExists()) {
61       strbuf << wxT("#\n");
62       strbuf << _("# Bacula wx-console Configuration File\n");
63       strbuf << wxT("#\n");
64       strbuf << wxT("\n");
65       strbuf << wxT("Director {\n");
66       strbuf << wxT("  Name = <hostname>-dir\n");
67       strbuf << wxT("  DIRport = 9101\n");
68       strbuf << wxT("  address = <hostname>\n");
69       strbuf << wxT("  Password = \"<dir_password>\"\n");
70       strbuf << wxT("}\n");
71    }
72    else {
73       wxFile file(filename);
74       char buffer[2049];
75       off_t len;
76       while ((len = file.Read(buffer, 2048)) > -1) {
77          buffer[len] = 0;
78          strbuf << wxString(buffer,wxConvUTF8);
79          if (file.Eof())
80             break;
81       }
82       file.Close();
83    }
84
85    textCtrl = new wxTextCtrl(this,-1,strbuf,wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH2 | wxTE_DONTWRAP);
86    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
87 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
88    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
89 #endif
90    textCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
91    textCtrl->SetStyle(0, textCtrl->GetLastPosition(), wxTextAttr(*wxBLACK, wxNullColour, font));
92
93    wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0);
94    mainSizer->AddGrowableCol(0);
95    mainSizer->AddGrowableRow(0);
96    
97    wxBoxSizer *bottomsizer = new wxBoxSizer(wxHORIZONTAL);
98    bottomsizer->Add(new wxButton(this, Save, _("Save and close")), 0, wxALL, 10);
99    bottomsizer->Add(new wxButton(this, Quit, _("Close without saving")), 0, wxALL, 10);
100    
101    mainSizer->Add(textCtrl, 1, wxEXPAND);
102    mainSizer->Add(bottomsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
103    
104    this->SetSizer(mainSizer);
105
106    firstpaint = true;
107 }
108
109 wxbConfigFileEditor::~wxbConfigFileEditor() {
110    
111 }
112
113 /* Kludge for Win32, so the text control is not completely selected. */
114 void wxbConfigFileEditor::OnPaint(wxPaintEvent& event) {
115    wxPaintDC dc(this);
116
117    if (firstpaint) {
118       firstpaint = false;
119       textCtrl->SetSelection(0, 0);
120    }
121 }
122
123 void wxbConfigFileEditor::OnSave(wxCommandEvent& event) {
124    wxFile file(filename, wxFile::write);
125    if (!file.IsOpened()) {
126       wxMessageBox(wxString::Format(_("Unable to write to %s\n"), filename.c_str()),
127                         _("Error while saving"),
128                         wxOK | wxICON_ERROR, this);
129       EndModal(wxCANCEL);
130       return;
131    }
132    
133    file.Write(textCtrl->GetValue(),wxConvLocal);
134    
135    file.Flush();
136    file.Close();
137    
138    EndModal(wxOK);
139 }
140
141 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
142    EndModal(wxCANCEL);
143 }