]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
- Add Date, Job, level to updates to .bsr file in
[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 ammended 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, wxT("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, wxT("Save and close")), 0, wxALL, 10);
58    bottomsizer->Add(new wxButton(this, Quit, wxT("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) << wxT("# 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(wxT("Unable to write to ")) << filename << wxT("\n"), wxT("Error while saving"),
101                         wxOK | wxICON_ERROR, this);
102       EndModal(wxCANCEL);
103       return;
104    }
105    
106    file.Write(textCtrl->GetValue(),wxConvLocal);
107    
108    file.Flush();
109    file.Close();
110    
111    EndModal(wxOK);
112 }
113
114 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
115    EndModal(wxCANCEL);
116 }