]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
Correct a minor build problem with wx-console.
[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 Kern Sibbald and John Walker
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    as published by the Free Software Foundation; either version 2
15    of the License, or (at your option) any later version.
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    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include "wxbconfigfileeditor.h"
28
29 #include <wx/file.h>
30 #include <wx/filename.h>
31
32 enum
33 {
34    Save = 1,
35    Quit = 2
36 };
37
38 BEGIN_EVENT_TABLE(wxbConfigFileEditor, wxDialog)
39    EVT_BUTTON(Save, wxbConfigFileEditor::OnSave)
40    EVT_BUTTON(Quit, wxbConfigFileEditor::OnQuit)
41 END_EVENT_TABLE()
42
43 wxbConfigFileEditor::wxbConfigFileEditor(wxWindow* parent, wxString filename):
44       wxDialog(parent, -1, "Config file editor", wxDefaultPosition, wxSize(500, 300),
45                    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
46    this->filename = filename;
47    
48    textCtrl = new wxTextCtrl(this,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH | wxTE_DONTWRAP);
49    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
50 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
51    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
52 #endif
53    textCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
54
55    wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0);
56    mainSizer->AddGrowableCol(0);
57    mainSizer->AddGrowableRow(0);
58    
59    wxBoxSizer *bottomsizer = new wxBoxSizer(wxHORIZONTAL);
60    bottomsizer->Add(new wxButton(this, Save, "Save and close"), 0, wxALL, 10);
61    bottomsizer->Add(new wxButton(this, Quit, "Close without saving"), 0, wxALL, 10);
62    
63    mainSizer->Add(textCtrl, 1, wxEXPAND);
64    mainSizer->Add(bottomsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
65    
66    this->SetSizer(mainSizer);
67    
68    wxFileName filen(filename);
69    
70    if (!filen.FileExists()) {
71       (*textCtrl) << "#\n";
72       (*textCtrl) << "# Bacula wx-console Configuration File\n";
73       (*textCtrl) << "#\n";
74       (*textCtrl) << "\n";
75       (*textCtrl) << "Director {\n";
76       (*textCtrl) << "  Name = <hostname>-dir\n";
77       (*textCtrl) << "  DIRport = 9101\n";
78       (*textCtrl) << "  address = <hostname>\n";
79       (*textCtrl) << "  Password = \"<dir_password>\"\n";
80       (*textCtrl) << "}\n";
81    }
82    else {
83       wxFile file(filename);
84       wxChar buffer[2049];
85       off_t len;
86       while ((len = file.Read(buffer, 2048)) > -1) {
87          buffer[len] = (wxChar)0;
88          (*textCtrl) << buffer;
89          if (file.Eof())
90             break;
91       }
92       file.Close();
93    }
94 }
95
96 wxbConfigFileEditor::~wxbConfigFileEditor() {
97    
98 }
99
100 void wxbConfigFileEditor::OnSave(wxCommandEvent& event) {
101    wxFile file(filename, wxFile::write);
102    if (!file.IsOpened()) {
103       wxMessageBox(wxString("Unable to write to ") << filename << "\n", "Error while saving",
104                         wxOK | wxICON_ERROR, this);
105       EndModal(wxCANCEL);
106       return;
107    }
108    
109    file.Write(textCtrl->GetValue());
110    
111    file.Flush();
112    file.Close();
113    
114    EndModal(wxOK);
115 }
116
117 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
118    EndModal(wxCANCEL);
119 }