]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigpanel.cpp
Fix some trivial errors and implemented the restore of IRIX xattrs.
[bacula/bacula] / bacula / src / wx-console / wxbconfigpanel.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *   Config panel, used to specify parameters (for example clients, filesets... in restore)
31  *
32  *    Nicolas Boichat, April 2004
33  *
34  *    Version $Id$
35  */
36
37 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
38  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
39  * So we turn _DEBUG off since we aren't interested in things it enables.
40  */
41
42 #undef _DEBUG
43
44 #include "bacula.h"
45 #include "wxbconfigpanel.h"
46 #include <wx/arrimpl.cpp>
47
48
49 WX_DEFINE_OBJARRAY(wxbConfig);
50
51 /* Create a new config parameter */
52 wxbConfigParam::wxbConfigParam(wxString title, wxWindowID id, wxbConfigType type, wxString value) {
53    this->title = title;
54    this->id = id;
55    this->type = type;
56    this->value = value;
57    this->values = NULL;
58    this->nvalues = 0;
59    this->choicectrl = NULL;
60    this->textctrl = NULL;
61    this->statictext = NULL;
62 }
63
64 wxbConfigParam::wxbConfigParam(wxString title, wxWindowID id, wxbConfigType type, int n, wxString* values) {
65    this->title = title;
66    this->id = id;
67    this->type = type;
68    this->values = new wxString[n];
69    for (int i = 0; i < n; i++) {
70       this->values[i] = values[i];
71    }
72    this->nvalues = n;
73    this->choicectrl = NULL;
74    this->textctrl = NULL;
75    this->statictext = NULL;
76 }
77
78 wxbConfigParam::~wxbConfigParam() {
79    if (values) delete[] values;
80    if (choicectrl) delete choicectrl;
81    if (textctrl) delete textctrl;
82    if (statictext) delete statictext;
83 }
84   
85 void wxbConfigParam::AddControl(wxWindow* parent, wxSizer* sizer) {
86    sizer->Add(new wxStaticText(parent, -1, title + wxT(": "), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT), 0, wxALIGN_CENTER_VERTICAL);
87    switch (type) {
88    case text:
89       statictext = new wxStaticText(parent, -1, value, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
90       sizer->Add(statictext, 1, wxEXPAND | wxADJUST_MINSIZE);
91       break;
92    case modifiableText:
93       textctrl = new wxTextCtrl(parent, id, value, wxDefaultPosition, wxDefaultSize);
94       sizer->Add(textctrl, 1, wxEXPAND | wxADJUST_MINSIZE);
95       break;
96    case choice:
97 #if defined __WXGTK20__ /* Choices are taller under GTK+-2.0 */
98       wxSize size = wxSize(150, 25);
99 #else
100       wxSize size = wxSize(150, 20);
101 #endif
102       choicectrl = new wxChoice(parent, id, wxDefaultPosition, size, nvalues, values);
103       sizer->Add(choicectrl, 1, wxEXPAND);
104       break;
105    }
106 }
107
108 wxString wxbConfigParam::GetTitle() {
109    return title;
110 }
111
112 wxString wxbConfigParam::GetValue() {
113    switch (type) {
114    case text:
115       return (statictext != NULL) ? statictext->GetLabel() : wxString(wxT(""));
116       break;
117    case modifiableText:
118       return (textctrl != NULL) ? textctrl->GetValue() : wxString(wxT(""));      
119       break;
120    case choice:
121       return (choicectrl != NULL) ? choicectrl->GetStringSelection() : wxString(wxT(""));
122       break;      
123    }
124    return wxT("");
125 }
126
127 void wxbConfigParam::SetValue(wxString str) {
128    switch (type) {
129    case text:
130       if (statictext)
131          statictext->SetLabel(str);
132       break;
133    case modifiableText:
134       if (textctrl)
135          textctrl->SetValue(str);      
136       break;
137    case choice:
138       if (choicectrl) {
139          int k;
140          for (k = 0; k < (int)choicectrl->GetCount(); k++) {
141             if (str == choicectrl->GetString(k)) {
142                choicectrl->SetSelection(k);
143                break;
144             }
145          }
146          if (k == (int)choicectrl->GetCount()) { // Should never happen
147             choicectrl->Append(str);
148             choicectrl->SetSelection(k);
149          }
150       }
151       break;      
152    }
153 }
154
155 int wxbConfigParam::GetIndex() {
156    if (choicectrl) {
157       return choicectrl->GetSelection();
158    }
159    return -1;
160 }
161
162 int wxbConfigParam::GetCount() {
163    if (choicectrl) {
164       return choicectrl->GetCount();
165    }
166    return -1;
167 }
168    
169
170 void wxbConfigParam::SetIndex(int ind) {
171    if (choicectrl) {
172       choicectrl->SetSelection(ind);
173    }
174 }
175
176 void wxbConfigParam::Clear() {
177    if (choicectrl) {
178       choicectrl->Clear();
179    }
180 }
181
182 void wxbConfigParam::Add(wxString value) {
183    if (choicectrl) {
184       choicectrl->Append(value);
185    }
186 }
187
188 /* Creates a new config panel, config must be allocated with new */
189 wxbConfigPanel::wxbConfigPanel(wxWindow* parent, wxbConfig* config, wxString title,
190       wxWindowID ok, wxWindowID cancel, wxWindowID apply): wxPanel(parent) {
191
192    this->config = config;
193
194    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
195    
196    mainSizer->Add(
197       new wxStaticText(this, -1, title, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER),
198             0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
199
200    wxFlexGridSizer* cfgSizer = new wxFlexGridSizer(config->GetCount()+1, 2, 8, 5);
201    unsigned int i;
202    for (i = 0; i < config->GetCount(); i++) {
203       (*config)[i].AddControl(this, cfgSizer);
204    }
205    mainSizer->Add(cfgSizer, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
206    
207    wxBoxSizer* restoreBottomSizer = new wxBoxSizer(wxHORIZONTAL);
208    
209    cfgOk = new wxButton(this, ok, _("OK"), wxDefaultPosition, wxSize(70, 25));
210    restoreBottomSizer->Add(cfgOk, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
211
212    if (apply != -1) {
213       cfgApply = new wxButton(this, apply, _("Apply"), wxDefaultPosition, wxSize(70, 25));
214       restoreBottomSizer->Add(cfgApply, 1, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 10);
215    }
216    else {
217       cfgApply = NULL;
218    }
219
220    cfgCancel = new wxButton(this, cancel, _("Cancel"), wxDefaultPosition, wxSize(70, 25));
221    restoreBottomSizer->Add(cfgCancel, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 10);
222    
223    mainSizer->Add(restoreBottomSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
224    
225    SetSizer(mainSizer);
226    mainSizer->SetSizeHints(this);
227    
228    last = 0;
229 }
230
231 wxbConfigPanel::~wxbConfigPanel() {
232    delete config;
233 }
234    
235 void wxbConfigPanel::SetRowString(const wxChar* title, wxString value) {
236    int i;
237    if ((i = FindRow(title)) > -1) {
238       (*config)[i].SetValue(value);
239    }
240 }
241
242 wxString wxbConfigPanel::GetRowString(const wxChar* title) {
243    int i;
244    if ((i = FindRow(title)) > -1) {
245       return (*config)[i].GetValue();
246    }
247    return wxT("");
248 }
249
250 void wxbConfigPanel::SetRowSelection(const wxChar* title, int ind) {
251    int i;
252    if ((i = FindRow(title)) > -1) {
253       (*config)[i].SetIndex(ind);
254    }
255 }
256
257 int wxbConfigPanel::GetRowSelection(const wxChar* title) {
258    int i;
259    if ((i = FindRow(title)) > -1) {
260       return (*config)[i].GetIndex();
261    }
262    return -1;
263 }
264
265 void wxbConfigPanel::ClearRowChoices(const wxChar* title) {
266    int i;
267    if ((i = FindRow(title)) > -1) {
268       (*config)[i].Clear();
269    }  
270 }
271
272 void wxbConfigPanel::AddRowChoice(const wxChar* title, wxString value) {
273    int i;
274    if ((i = FindRow(title)) > -1) {
275       (*config)[i].Add(value);
276    }  
277 }
278
279 int wxbConfigPanel::FindRow(const wxChar* title) {
280    unsigned int i;
281    
282    for (i = last; i < config->GetCount(); i++) {
283       if ((*config)[i].GetTitle() == title) {
284          last = i;
285          return i;
286       }
287    }
288    
289    for (i = 0; i < last; i++) {
290       if ((*config)[i].GetTitle() == title) {
291          last = i;
292          return i;
293       }
294    }
295    
296    last = 0;
297    return -1;
298 }
299
300 int wxbConfigPanel::GetRowCount(const wxChar* title)
301 {
302    int i;
303    if ((i = FindRow(title)) > -1) {
304       return (*config)[i].GetCount();
305    }  
306
307    return -1;
308 }
309
310 void wxbConfigPanel::EnableApply(bool enable) {
311    cfgOk->Enable(!enable);
312    if (cfgApply) cfgApply->Enable(enable);
313 }