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