]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
Fix some trivial errors and implemented the restore of IRIX xattrs.
[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    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
13
14    The main author of Bacula is Kern Sibbald, with contributions from
15    many others, a complete list can be found in the file AUTHORS.
16    This program is Free Software; you can redistribute it and/or
17    modify it under the terms of version three of the GNU Affero General Public
18    License as published by the Free Software Foundation and included
19    in the file LICENSE.
20
21    This program is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU Affero General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.
30
31    Bacula® is a registered trademark of Kern Sibbald.
32    The licensor of Bacula is the Free Software Foundation Europe
33    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34    Switzerland, email:ftf@fsfeurope.org.
35 */
36
37 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
38  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
39  * So we turn _DEBUG off since we aren't interested in things it enables.
40  */
41
42 #undef _DEBUG
43
44 #include "bacula.h"
45 #include "wxbconfigfileeditor.h"
46 #include <wx/file.h>
47 #include <wx/filename.h>
48
49
50 enum
51 {
52    Save = 1,
53    Quit = 2
54 };
55
56 BEGIN_EVENT_TABLE(wxbConfigFileEditor, wxDialog)
57    EVT_BUTTON(Save, wxbConfigFileEditor::OnSave)
58    EVT_BUTTON(Quit, wxbConfigFileEditor::OnQuit)
59 #ifdef HAVE_WIN32
60    EVT_PAINT (      wxbConfigFileEditor::OnPaint)
61 #endif
62 END_EVENT_TABLE()
63
64 wxbConfigFileEditor::wxbConfigFileEditor(wxWindow* parent, wxString filename):
65       wxDialog(parent, -1, _("Config file editor"), wxDefaultPosition, wxSize(500, 300),
66                    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
67    this->filename = filename;
68    
69    wxString strbuf;
70
71    wxFileName filen(filename);
72    
73    if (!filen.FileExists()) {
74       strbuf << wxT("#\n");
75       strbuf << _("# Bacula bwx-console Configuration File\n");
76       strbuf << wxT("#\n");
77       strbuf << wxT("\n");
78       strbuf << wxT("Director {\n");
79       strbuf << wxT("  Name = <hostname>-dir\n");
80       strbuf << wxT("  DIRport = 9101\n");
81       strbuf << wxT("  address = <hostname>\n");
82       strbuf << wxT("  Password = \"<dir_password>\"\n");
83       strbuf << wxT("}\n");
84    }
85    else {
86       wxFile file(filename);
87       char buffer[2049];
88       off_t len;
89       while ((len = file.Read(buffer, 2048)) > -1) {
90          buffer[len] = 0;
91          strbuf << wxString(buffer,wxConvUTF8);
92          if (file.Eof())
93             break;
94       }
95       file.Close();
96    }
97
98    textCtrl = new wxTextCtrl(this,-1,strbuf,wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH2 | wxTE_DONTWRAP);
99    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
100 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
101    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
102 #endif
103    textCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
104    textCtrl->SetStyle(0, textCtrl->GetLastPosition(), wxTextAttr(*wxBLACK, wxNullColour, font));
105
106    wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0);
107    mainSizer->AddGrowableCol(0);
108    mainSizer->AddGrowableRow(0);
109    
110    wxBoxSizer *bottomsizer = new wxBoxSizer(wxHORIZONTAL);
111    bottomsizer->Add(new wxButton(this, Save, _("Save and close")), 0, wxALL, 10);
112    bottomsizer->Add(new wxButton(this, Quit, _("Close without saving")), 0, wxALL, 10);
113    
114    mainSizer->Add(textCtrl, 1, wxEXPAND);
115    mainSizer->Add(bottomsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
116    
117    this->SetSizer(mainSizer);
118
119    firstpaint = true;
120 }
121
122 wxbConfigFileEditor::~wxbConfigFileEditor() {
123    
124 }
125
126 /* Kludge for Win32, so the text control is not completely selected. */
127 void wxbConfigFileEditor::OnPaint(wxPaintEvent& event) {
128    wxPaintDC dc(this);
129
130    if (firstpaint) {
131       firstpaint = false;
132       textCtrl->SetSelection(0, 0);
133    }
134 }
135
136 void wxbConfigFileEditor::OnSave(wxCommandEvent& event) {
137    wxFile file(filename, wxFile::write);
138    if (!file.IsOpened()) {
139       wxMessageBox(wxString::Format(_("Unable to write to %s\n"), filename.c_str()),
140                         _("Error while saving"),
141                         wxOK | wxICON_ERROR, this);
142       EndModal(wxCANCEL);
143       return;
144    }
145    
146    file.Write(textCtrl->GetValue(),wxConvLocal);
147    
148    file.Flush();
149    file.Close();
150    
151    EndModal(wxOK);
152 }
153
154 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
155    EndModal(wxCANCEL);
156 }