]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
Fix header file includes.
[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 "bacula.h"
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 #ifdef HAVE_WIN32
41    EVT_PAINT (      wxbConfigFileEditor::OnPaint)
42 #endif
43 END_EVENT_TABLE()
44
45 wxbConfigFileEditor::wxbConfigFileEditor(wxWindow* parent, wxString filename):
46       wxDialog(parent, -1, _("Config file editor"), wxDefaultPosition, wxSize(500, 300),
47                    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
48    this->filename = filename;
49    
50    wxString strbuf;
51
52    wxFileName filen(filename);
53    
54    if (!filen.FileExists()) {
55       strbuf << wxT("#\n");
56       strbuf << _("# Bacula wx-console Configuration File\n");
57       strbuf << wxT("#\n");
58       strbuf << wxT("\n");
59       strbuf << wxT("Director {\n");
60       strbuf << wxT("  Name = <hostname>-dir\n");
61       strbuf << wxT("  DIRport = 9101\n");
62       strbuf << wxT("  address = <hostname>\n");
63       strbuf << wxT("  Password = \"<dir_password>\"\n");
64       strbuf << wxT("}\n");
65    }
66    else {
67       wxFile file(filename);
68       char buffer[2049];
69       off_t len;
70       while ((len = file.Read(buffer, 2048)) > -1) {
71          buffer[len] = 0;
72          strbuf << wxString(buffer,wxConvUTF8);
73          if (file.Eof())
74             break;
75       }
76       file.Close();
77    }
78
79    textCtrl = new wxTextCtrl(this,-1,strbuf,wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH2 | wxTE_DONTWRAP);
80    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
81 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
82    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
83 #endif
84    textCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
85    textCtrl->SetStyle(0, textCtrl->GetLastPosition(), wxTextAttr(*wxBLACK, wxNullColour, font));
86
87    wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0);
88    mainSizer->AddGrowableCol(0);
89    mainSizer->AddGrowableRow(0);
90    
91    wxBoxSizer *bottomsizer = new wxBoxSizer(wxHORIZONTAL);
92    bottomsizer->Add(new wxButton(this, Save, _("Save and close")), 0, wxALL, 10);
93    bottomsizer->Add(new wxButton(this, Quit, _("Close without saving")), 0, wxALL, 10);
94    
95    mainSizer->Add(textCtrl, 1, wxEXPAND);
96    mainSizer->Add(bottomsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
97    
98    this->SetSizer(mainSizer);
99
100    firstpaint = true;
101 }
102
103 wxbConfigFileEditor::~wxbConfigFileEditor() {
104    
105 }
106
107 /* Kludge for Win32, so the text control is not completely selected. */
108 void wxbConfigFileEditor::OnPaint(wxPaintEvent& event) {
109    wxPaintDC dc(this);
110
111    if (firstpaint) {
112       firstpaint = false;
113       textCtrl->SetSelection(0, 0);
114    }
115 }
116
117 void wxbConfigFileEditor::OnSave(wxCommandEvent& event) {
118    wxFile file(filename, wxFile::write);
119    if (!file.IsOpened()) {
120       wxMessageBox(wxString::Format(_("Unable to write to %s\n"), filename.c_str()),
121                         _("Error while saving"),
122                         wxOK | wxICON_ERROR, this);
123       EndModal(wxCANCEL);
124       return;
125    }
126    
127    file.Write(textCtrl->GetValue(),wxConvLocal);
128    
129    file.Flush();
130    file.Close();
131    
132    EndModal(wxOK);
133 }
134
135 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
136    EndModal(wxCANCEL);
137 }