]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
- wxbHistoryTextCtrl : Created a new text control that keep an history
[bacula/bacula] / bacula / src / wx-console / wxbconfigfileeditor.cpp
1 /*
2  *
3  *    Configuration file editor
4  *
5  *    Nicolas Boichat, May 2004
6  *
7  */
8 /*
9    Copyright (C) 2004 Kern Sibbald and John Walker
10
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.
15
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.
20
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.
24  */
25
26 #include "wxbconfigfileeditor.h"
27
28 #include <wx/file.h>
29 #include <wx/filename.h>
30
31 enum
32 {
33    Save = 1,
34    Quit = 2
35 };
36
37 BEGIN_EVENT_TABLE(wxbConfigFileEditor, wxDialog)
38    EVT_BUTTON(Save, wxbConfigFileEditor::OnSave)
39    EVT_BUTTON(Quit, wxbConfigFileEditor::OnQuit)
40 END_EVENT_TABLE()
41
42 wxbConfigFileEditor::wxbConfigFileEditor(wxWindow* parent, wxString filename):
43       wxDialog(parent, -1, "Config file editor", wxDefaultPosition, wxSize(500, 300),
44                    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
45    this->filename = filename;
46    
47    textCtrl = new wxTextCtrl(this,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH | wxTE_DONTWRAP);
48    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
49 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
50    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
51 #endif
52    textCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
53
54    wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0);
55    mainSizer->AddGrowableCol(0);
56    mainSizer->AddGrowableRow(0);
57    
58    wxBoxSizer *bottomsizer = new wxBoxSizer(wxHORIZONTAL);
59    bottomsizer->Add(new wxButton(this, Save, "Save and close"), 0, wxALL, 10);
60    bottomsizer->Add(new wxButton(this, Quit, "Close without saving"), 0, wxALL, 10);
61    
62    mainSizer->Add(textCtrl, 1, wxEXPAND);
63    mainSizer->Add(bottomsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
64    
65    this->SetSizer(mainSizer);
66    
67    wxFileName filen(filename);
68    
69    if (!filen.FileExists()) {
70       (*textCtrl) << "#\n";
71       (*textCtrl) << "# Bacula wx-console Configuration File\n";
72       (*textCtrl) << "#\n";
73       (*textCtrl) << "\n";
74       (*textCtrl) << "Director {\n";
75       (*textCtrl) << "  Name = <hostname>-dir\n";
76       (*textCtrl) << "  DIRport = 9101\n";
77       (*textCtrl) << "  address = <hostname>\n";
78       (*textCtrl) << "  Password = \"<dir_password>\"\n";
79       (*textCtrl) << "}\n";
80    }
81    else {
82       wxFile file(filename);
83       wxChar buffer[2049];
84       off_t len;
85       while ((len = file.Read(buffer, 2048)) > -1) {
86          buffer[len] = (wxChar)0;
87          (*textCtrl) << buffer;
88          if (file.Eof())
89             break;
90       }
91       file.Close();
92    }
93 }
94
95 wxbConfigFileEditor::~wxbConfigFileEditor() {
96    
97 }
98
99 void wxbConfigFileEditor::OnSave(wxCommandEvent& event) {
100    wxFile file(filename, wxFile::write);
101    if (!file.IsOpened()) {
102       wxMessageBox(wxString("Unable to write to ") << filename << "\n", "Error while saving",
103                         wxOK | wxICON_ERROR, this);
104       EndModal(wxCANCEL);
105       return;
106    }
107    
108    file.Write(textCtrl->GetValue());
109    
110    file.Flush();
111    file.Close();
112    
113    EndModal(wxOK);
114 }
115
116 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
117    EndModal(wxCANCEL);
118 }