]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbrestorepanel.cpp
- wxbRestorePanel : Fixed crash when an error occurs while querying restore list...
[bacula/bacula] / bacula / src / wx-console / wxbrestorepanel.cpp
1 /*
2  *
3  *   wxbPanel for restoring files
4  *
5  *    Nicolas Boichat, April-July 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 /* Note concerning "done" output (modifiable marked with +)
26 Run Restore job
27 +JobName:    RestoreFiles
28 Bootstrap:  /var/lib/bacula/restore.bsr
29 +Where:      /tmp/bacula-restores
30 +Replace:    always
31 +FileSet:    Full Set
32 +Client:     tom-fd
33 +Storage:    File
34 +When:       2004-04-18 01:18:56
35 +Priority:   10
36 OK to run? (yes/mod/no):mod
37 Parameters to modify:
38      1: Level (not appropriate)
39      2: Storage (automatic ?)
40      3: Job (no)
41      4: FileSet (yes)
42      5: Client (yes : "The defined Client resources are:\n\t1: velours-fd\n\t2: tom-fd\nSelect Client (File daemon) resource (1-2):")
43      6: When (yes : "Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now):")
44      7: Priority (yes : "Enter new Priority: (positive integer)")
45      8: Bootstrap (?)
46      9: Where (yes : "Please enter path prefix for restore (/ for none):")
47     10: Replace (yes : "Replace:\n 1: always\n 2: ifnewer\n 3: ifolder\n 4: never\n 
48           Select replace option (1-4):")
49     11: JobId (no)
50 Select parameter to modify (1-11):
51        */
52
53 #include "wxbrestorepanel.h"
54
55 #include "wxbmainframe.h"
56
57 #include "csprint.h"
58
59 #include <wx/choice.h>
60 #include <wx/datetime.h>
61
62 #include <wx/timer.h>
63
64 #include "unmarked.xpm"
65 #include "marked.xpm"
66 #include "partmarked.xpm"
67
68 #include <wx/listimpl.cpp>
69
70 /* A macro named Yield is defined under MinGW */
71 #undef Yield
72
73 WX_DEFINE_LIST(wxbEventList);
74
75 /*
76  *  Class which is stored in the tree and in the list to keep informations
77  *  about the element.
78  */
79 class wxbTreeItemData : public wxTreeItemData {
80    public:
81       wxbTreeItemData(wxString path, wxString name, int marked, long listid = -1);
82       wxbTreeItemData(wxString path, wxString name, wxString marked, long listid = -1);
83       ~wxbTreeItemData();
84       wxString GetPath();
85       wxString GetName();
86       
87       int GetMarked();
88       void SetMarked(int marked);
89       void SetMarked(wxString marked);
90       
91       long GetListId();
92
93       static int GetMarkedStatus(wxString file);
94    private:
95       wxString* path; /* Full path */
96       wxString* name; /* File name */
97       int marked; /* 0 - Not Marked, 1 - Marked, 2 - Some file under is marked */
98       long listid; /* list ID : >-1 if this data is in the list (and/or on the tree) */
99 };
100
101 wxbTreeItemData::wxbTreeItemData(wxString path, wxString name, int marked, long listid): wxTreeItemData() {
102    this->path = new wxString(path);
103    this->name = new wxString(name);
104    this->marked = marked;
105    this->listid = listid;
106 }
107
108 wxbTreeItemData::wxbTreeItemData(wxString path, wxString name, wxString marked, long listid): wxTreeItemData() {
109    this->path = new wxString(path);
110    this->name = new wxString(name);
111    SetMarked(marked);
112    this->listid = listid;
113 }
114
115 wxbTreeItemData::~wxbTreeItemData() {
116    delete path;
117    delete name;
118 }
119
120 int wxbTreeItemData::GetMarked() {
121    return marked;
122 }
123
124 void wxbTreeItemData::SetMarked(wxString marked) {
125    if (marked == "*") {
126       this->marked = 1;
127    }
128    else if (marked == "+") {
129       this->marked = 2;
130    }
131    else {
132       this->marked = 0;
133    }
134 }
135
136 void wxbTreeItemData::SetMarked(int marked) {
137    this->marked = marked;
138 }
139
140 long wxbTreeItemData::GetListId() {
141    return listid;
142 }
143
144 wxString wxbTreeItemData::GetPath() {
145    return *path;
146 }
147
148 wxString wxbTreeItemData::GetName() {
149    return *name;
150 }
151
152 /*wxbTreeItemData* wxbTreeItemData::GetChild(wxString dirname) {
153    int marked = GetMarkedStatus(dirname);
154    return new wxbTreeItemData(path + (marked ? dirname.Mid(1) : dirname), marked);
155 }*/
156
157 int wxbTreeItemData::GetMarkedStatus(wxString file) {
158    if (file.Length() == 0)
159       return 0;
160    
161    switch (file.GetChar(0)) {
162        case '*':
163           return 1;
164        case '+':
165           return 2;
166        default:
167           return 0;
168     }
169 }
170
171 // ----------------------------------------------------------------------------
172 // event tables and other macros for wxWindows
173 // ----------------------------------------------------------------------------
174
175 enum
176 {
177    RestoreStart = 1,
178    RestoreCancel = 2,
179    TreeCtrl = 3,
180    ListCtrl = 4,
181    ConfigOk = 5,
182    ConfigApply = 6,
183    ConfigCancel = 7,
184    ConfigWhere = 8,
185    ConfigReplace = 9,
186    ConfigWhen = 10,
187    ConfigPriority = 11,
188    ConfigClient = 12,
189    ConfigFileset = 13,
190    ConfigStorage = 14,
191    ConfigJobName = 15,
192    ConfigPool = 16,
193    TreeAdd = 17,
194    TreeRemove = 18,
195    TreeRefresh = 19,
196    ListAdd = 20,
197    ListRemove = 21,
198    ListRefresh = 22
199 };
200
201 BEGIN_EVENT_TABLE(wxbRestorePanel, wxPanel)
202    EVT_BUTTON(RestoreStart, wxbRestorePanel::OnStart)
203    EVT_BUTTON(RestoreCancel, wxbRestorePanel::OnCancel)
204    
205    EVT_TREE_SEL_CHANGING(TreeCtrl, wxbRestorePanel::OnTreeChanging)
206    EVT_TREE_SEL_CHANGED(TreeCtrl, wxbRestorePanel::OnTreeChanged)
207    EVT_TREE_ITEM_EXPANDING(TreeCtrl, wxbRestorePanel::OnTreeExpanding)
208    EVT_TREE_MARKED_EVENT(TreeCtrl, wxbRestorePanel::OnTreeMarked)
209    EVT_BUTTON(TreeAdd, wxbRestorePanel::OnTreeAdd)
210    EVT_BUTTON(TreeRemove, wxbRestorePanel::OnTreeRemove)
211    EVT_BUTTON(TreeRefresh, wxbRestorePanel::OnTreeRefresh)
212
213    EVT_LIST_ITEM_ACTIVATED(ListCtrl, wxbRestorePanel::OnListActivated)
214    EVT_LIST_MARKED_EVENT(ListCtrl, wxbRestorePanel::OnListMarked)
215    EVT_LIST_ITEM_SELECTED(ListCtrl, wxbRestorePanel::OnListChanged)
216    EVT_LIST_ITEM_DESELECTED(ListCtrl, wxbRestorePanel::OnListChanged)
217    EVT_BUTTON(ListAdd, wxbRestorePanel::OnListAdd)
218    EVT_BUTTON(ListRemove, wxbRestorePanel::OnListRemove)
219    EVT_BUTTON(ListRefresh, wxbRestorePanel::OnListRefresh)
220   
221    EVT_TEXT(ConfigWhere, wxbRestorePanel::OnConfigUpdated)
222    EVT_TEXT(ConfigWhen, wxbRestorePanel::OnConfigUpdated)
223    EVT_TEXT(ConfigPriority, wxbRestorePanel::OnConfigUpdated)
224    EVT_CHOICE(ConfigWhen, wxbRestorePanel::OnConfigUpdated)
225    EVT_CHOICE(ConfigReplace, wxbRestorePanel::OnConfigUpdated)
226    EVT_CHOICE(ConfigClient, wxbRestorePanel::OnConfigUpdated)
227    EVT_CHOICE(ConfigFileset, wxbRestorePanel::OnConfigUpdated)
228    EVT_CHOICE(ConfigStorage, wxbRestorePanel::OnConfigUpdated)
229    EVT_CHOICE(ConfigJobName, wxbRestorePanel::OnConfigUpdated)
230    EVT_CHOICE(ConfigPool, wxbRestorePanel::OnConfigUpdated)
231    
232    EVT_BUTTON(ConfigOk, wxbRestorePanel::OnConfigOk)
233    EVT_BUTTON(ConfigApply, wxbRestorePanel::OnConfigApply)
234    EVT_BUTTON(ConfigCancel, wxbRestorePanel::OnConfigCancel)
235 END_EVENT_TABLE()
236
237 /*
238  *  wxbRestorePanel constructor
239  */
240 wxbRestorePanel::wxbRestorePanel(wxWindow* parent): wxbPanel(parent) {
241    //pendingEvents = new wxbEventList(); //EVTQUEUE
242    //processing = false; //EVTQUEUE
243    SetWorking(false);
244    
245    imagelist = new wxImageList(16, 16, TRUE, 3);
246    imagelist->Add(wxIcon(unmarked_xpm));
247    imagelist->Add(wxIcon(marked_xpm));
248    imagelist->Add(wxIcon(partmarked_xpm));
249
250    wxFlexGridSizer* mainSizer = new wxFlexGridSizer(3, 1, 10, 10);
251    mainSizer->AddGrowableCol(0);
252    mainSizer->AddGrowableRow(1);
253
254    wxFlexGridSizer *firstSizer = new wxFlexGridSizer(1, 2, 10, 10);
255
256    firstSizer->AddGrowableCol(0);
257    firstSizer->AddGrowableRow(0);
258
259    start = new wxButton(this, RestoreStart, "Enter restore mode", wxDefaultPosition, wxSize(150, 30));
260    firstSizer->Add(start, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL, 10);
261
262    cancel = new wxButton(this, RestoreCancel, "Cancel restore", wxDefaultPosition, wxSize(150, 30));
263    firstSizer->Add(cancel, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_RIGHT, 10);
264
265    wxString elist[1];
266
267 /*   clientChoice = new wxChoice(this, ClientChoice, wxDefaultPosition, wxSize(150, 30), 0, elist);
268    firstSizer->Add(clientChoice, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL, 10);
269
270    jobChoice = new wxChoice(this, -1, wxDefaultPosition, wxSize(150, 30), 0, elist);
271    firstSizer->Add(jobChoice, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL, 10);*/
272
273    mainSizer->Add(firstSizer, 1, wxEXPAND, 10);
274
275    treelistPanel = new wxSplitterWindow(this);
276
277    wxPanel* treePanel = new wxPanel(treelistPanel);
278    wxFlexGridSizer *treeSizer = new wxFlexGridSizer(2, 1, 0, 0);
279    treeSizer->AddGrowableCol(0);
280    treeSizer->AddGrowableRow(0);
281
282    tree = new wxbTreeCtrl(treePanel, this, TreeCtrl, wxDefaultPosition, wxSize(200,50));  
283    tree->SetImageList(imagelist);
284    
285    treeSizer->Add(tree, 1, wxEXPAND, 0);
286    
287    wxBoxSizer *treeCtrlSizer = new wxBoxSizer(wxHORIZONTAL);
288    treeadd = new wxButton(treePanel, TreeAdd, "Add", wxDefaultPosition, wxSize(60, 25));
289    treeCtrlSizer->Add(treeadd, 0, wxLEFT | wxRIGHT, 3);
290    treeremove = new wxButton(treePanel, TreeRemove, "Remove", wxDefaultPosition, wxSize(60, 25));
291    treeCtrlSizer->Add(treeremove, 0, wxLEFT | wxRIGHT, 3);
292    treerefresh = new wxButton(treePanel, TreeRefresh, "Refresh", wxDefaultPosition, wxSize(60, 25));
293    treeCtrlSizer->Add(treerefresh, 0, wxLEFT | wxRIGHT, 3);
294    
295    treeSizer->Add(treeCtrlSizer, 1, wxALIGN_CENTER_HORIZONTAL, 0);
296    
297    treePanel->SetSizer(treeSizer);
298    
299    wxPanel* listPanel = new wxPanel(treelistPanel);
300    wxFlexGridSizer *listSizer = new wxFlexGridSizer(2, 1, 0, 0);
301    listSizer->AddGrowableCol(0);
302    listSizer->AddGrowableRow(0);
303    
304    list = new wxbListCtrl(listPanel, this, ListCtrl, wxDefaultPosition, wxSize(200,50));
305    //treelistSizer->Add(list, 1, wxEXPAND, 10);
306
307    list->SetImageList(imagelist, wxIMAGE_LIST_SMALL);
308
309    wxListItem info;
310    info.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT);
311    info.SetText("M");
312    info.SetAlign(wxLIST_FORMAT_CENTER);
313    list->InsertColumn(0, info);
314    
315    info.SetText("Filename");
316    info.SetAlign(wxLIST_FORMAT_LEFT);
317    list->InsertColumn(1, info);
318
319    info.SetText("Size");
320    info.SetAlign(wxLIST_FORMAT_RIGHT);   
321    list->InsertColumn(2, info);
322
323    info.SetText("Date");
324    info.SetAlign(wxLIST_FORMAT_LEFT);
325    list->InsertColumn(3, info);
326
327    info.SetText("Perm.");
328    info.SetAlign(wxLIST_FORMAT_LEFT);
329    list->InsertColumn(4, info);
330    
331    info.SetText("User");
332    info.SetAlign(wxLIST_FORMAT_RIGHT);
333    list->InsertColumn(5, info);
334    
335    info.SetText("Group");
336    info.SetAlign(wxLIST_FORMAT_RIGHT);
337    list->InsertColumn(6, info);
338     
339    listSizer->Add(list, 1, wxEXPAND, 0);
340    
341    wxBoxSizer *listCtrlSizer = new wxBoxSizer(wxHORIZONTAL);
342    listadd = new wxButton(listPanel, ListAdd, "Add", wxDefaultPosition, wxSize(60, 25));
343    listCtrlSizer->Add(listadd, 0, wxLEFT | wxRIGHT, 5);
344    listremove = new wxButton(listPanel, ListRemove, "Remove", wxDefaultPosition, wxSize(60, 25));
345    listCtrlSizer->Add(listremove, 0, wxLEFT | wxRIGHT, 5);
346    listrefresh = new wxButton(listPanel, ListRefresh, "Refresh", wxDefaultPosition, wxSize(60, 25));
347    listCtrlSizer->Add(listrefresh, 0, wxLEFT | wxRIGHT, 5);
348    
349    listSizer->Add(listCtrlSizer, 1, wxALIGN_CENTER_HORIZONTAL, 0);
350    
351    listPanel->SetSizer(listSizer);
352    
353    treelistPanel->SplitVertically(treePanel, listPanel, 210);
354    
355    treelistPanel->SetMinimumPaneSize(210);
356      
357    treelistPanel->Show(false);
358    
359    wxbConfig* config = new wxbConfig();
360    config->Add(new wxbConfigParam("Job Name", ConfigJobName, choice, 0, elist));
361    config->Add(new wxbConfigParam("Client", ConfigClient, choice, 0, elist));
362    config->Add(new wxbConfigParam("Fileset", ConfigFileset, choice, 0, elist));
363    config->Add(new wxbConfigParam("Pool", ConfigPool, choice, 0, elist));
364    config->Add(new wxbConfigParam("Storage", ConfigStorage, choice, 0, elist));
365    config->Add(new wxbConfigParam("Before", ConfigWhen, choice, 0, elist));
366    
367    configPanel = new wxbConfigPanel(this, config, "Please configure parameters concerning files to restore :", RestoreStart, RestoreCancel, -1);
368    
369    configPanel->Show(true);
370    configPanel->Enable(false);
371    
372    config = new wxbConfig();
373    config->Add(new wxbConfigParam("Job Name", -1, text, ""));
374    config->Add(new wxbConfigParam("Bootstrap", -1, text, ""));
375    config->Add(new wxbConfigParam("Where", ConfigWhere, modifiableText, ""));
376    wxString erlist[] = {"always", "if newer", "if older", "never"};
377    config->Add(new wxbConfigParam("Replace", ConfigReplace, choice, 4, erlist));
378    config->Add(new wxbConfigParam("Fileset", ConfigFileset, choice, 0, erlist));
379    config->Add(new wxbConfigParam("Client", ConfigClient, choice, 0, erlist));
380    config->Add(new wxbConfigParam("Storage", ConfigStorage, choice, 0, erlist));
381    config->Add(new wxbConfigParam("When", ConfigWhen, modifiableText, ""));
382    config->Add(new wxbConfigParam("Priority", ConfigPriority, modifiableText, ""));
383    
384    restorePanel = new wxbConfigPanel(this, config, "Please configure parameters concerning files restoration :", ConfigOk, ConfigCancel, ConfigApply);
385     
386    restorePanel->Show(false);
387    
388    centerSizer = new wxBoxSizer(wxHORIZONTAL);
389    //centerSizer->Add(treelistPanel, 1, wxEXPAND | wxADJUST_MINSIZE);
390       
391    mainSizer->Add(centerSizer, 1, wxEXPAND, 10);
392
393    gauge = new wxGauge(this, -1, 1, wxDefaultPosition, wxSize(200,20));
394
395    mainSizer->Add(gauge, 1, wxEXPAND, 5);
396    gauge->SetValue(0);
397    gauge->Enable(false);
398
399    SetSizer(mainSizer);
400    mainSizer->SetSizeHints(this);
401
402    SetStatus(disabled);
403
404    for (int i = 0; i < 7; i++) {
405       list->SetColumnWidth(i, 70);
406    }
407
408    SetCursor(*wxSTANDARD_CURSOR);
409
410    markWhenListingDone = false;
411    
412    cancelled = 0;
413 }
414
415 /*
416  *  wxbRestorePanel destructor
417  */
418 wxbRestorePanel::~wxbRestorePanel() {
419    delete imagelist;
420 }
421
422 /*----------------------------------------------------------------------------
423    wxbPanel overloadings
424   ----------------------------------------------------------------------------*/
425
426 wxString wxbRestorePanel::GetTitle() {
427    return "Restore";
428 }
429
430 void wxbRestorePanel::EnablePanel(bool enable) {
431    if (enable) {
432       if (status == disabled) {
433          SetStatus(activable);
434       }
435    }
436    else {
437       SetStatus(disabled);
438    }
439 }
440
441 /*----------------------------------------------------------------------------
442    Commands called by events handler
443   ----------------------------------------------------------------------------*/
444
445 /* The main button has been clicked */
446 void wxbRestorePanel::CmdStart() {
447    unsigned int i;
448    if (status == activable) {
449       wxbMainFrame::GetInstance()->SetStatusText("Getting parameters list.");
450       wxbDataTokenizer* dt = wxbUtils::WaitForEnd(".clients\n", true, false);
451       wxString str;
452
453       configPanel->ClearRowChoices("Client");
454       restorePanel->ClearRowChoices("Client");
455       
456       if (dt->GetCount() == 0) {
457          wxbMainFrame::GetInstance()->SetStatusText("Error : no clients returned by the director.");
458          return;
459       }
460       
461       for (i = 0; i < dt->GetCount(); i++) {
462          str = (*dt)[i];
463          str.RemoveLast();
464          configPanel->AddRowChoice("Client", str);
465          restorePanel->AddRowChoice("Client", str);
466       }
467           
468       delete dt;
469       
470       if (cancelled) {
471          cancelled = 2;
472          return;
473       }
474       
475       dt = wxbUtils::WaitForEnd(".filesets\n", true, false);
476       
477       configPanel->ClearRowChoices("Fileset");
478       restorePanel->ClearRowChoices("Fileset");
479     
480       if (dt->GetCount() == 0) {
481          wxbMainFrame::GetInstance()->SetStatusText("Error : no filesets returned by the director.");
482          return;
483       }
484       
485       for (i = 0; i < dt->GetCount(); i++) {
486          str = (*dt)[i];
487          str.RemoveLast();
488          configPanel->AddRowChoice("Fileset", str);
489          restorePanel->AddRowChoice("Fileset", str);
490       }
491       
492       delete dt;
493       
494       if (cancelled) {
495          cancelled = 2;
496          return;
497       }
498       
499       dt = wxbUtils::WaitForEnd(".storage\n", true, false);
500     
501       configPanel->ClearRowChoices("Storage");
502       restorePanel->ClearRowChoices("Storage");
503     
504       if (dt->GetCount() == 0) {
505          wxbMainFrame::GetInstance()->SetStatusText("Error : no storage returned by the director.");
506          return;
507       }
508       
509       for (i = 0; i < dt->GetCount(); i++) {
510          str = (*dt)[i];
511          str.RemoveLast();
512          configPanel->AddRowChoice("Storage", str);
513          restorePanel->AddRowChoice("Storage", str);
514       }
515       
516       delete dt;
517       
518       if (cancelled) {
519          cancelled = 2;
520          return;
521       }
522       
523       dt = wxbUtils::WaitForEnd(".jobs\n", true, false);
524     
525       configPanel->ClearRowChoices("Job Name");
526     
527       if (dt->GetCount() == 0) {
528          wxbMainFrame::GetInstance()->SetStatusText("Error : no jobs returned by the director.");
529          return;
530       }
531       
532       for (i = 0; i < dt->GetCount(); i++) {
533          str = (*dt)[i];
534          str.RemoveLast();
535          configPanel->AddRowChoice("Job Name", str);
536       }
537       
538       configPanel->SetRowString("Job Name", "RestoreFiles");
539       
540       delete dt;
541       
542       if (cancelled) {
543          cancelled = 2;
544          return;
545       }
546       
547       dt = wxbUtils::WaitForEnd(".pools\n", true, false);
548     
549       configPanel->ClearRowChoices("Pool");
550     
551       if (dt->GetCount() == 0) {
552          wxbMainFrame::GetInstance()->SetStatusText("Error : no jobs returned by the director.");
553          return;
554       }
555       
556       for (i = 0; i < dt->GetCount(); i++) {
557          str = (*dt)[i];
558          str.RemoveLast();
559          configPanel->AddRowChoice("Pool", str);
560       }
561          
562       delete dt; 
563       
564       if (cancelled) {
565          cancelled = 2;
566          return;
567       }
568
569       SetStatus(entered);
570
571       UpdateFirstConfig();
572            
573       wxbMainFrame::GetInstance()->SetStatusText("Please configure your restore parameters.");
574    }
575    else if (status == entered) {
576 /*      if (clientChoice->GetStringSelection().Length() < 1) {
577          wxbMainFrame::GetInstance()->SetStatusText("Please select a client.");
578          return;
579       }
580       if (jobChoice->GetStringSelection().Length() < 1) {
581          wxbMainFrame::GetInstance()->SetStatusText("Please select a restore date.");
582          return;
583       }*/
584       wxbMainFrame::GetInstance()->SetStatusText("Building restore tree...");
585       
586       SetStatus(choosing);
587       
588       wxbUtils::WaitForPrompt(wxString("restore") <<
589          " client=\"" << configPanel->GetRowString("Client") <<
590          "\" fileset=\"" << configPanel->GetRowString("Fileset") <<
591          "\" pool=\"" << configPanel->GetRowString("Pool") <<
592          "\" storage=\"" << configPanel->GetRowString("Storage") << "\"\n");
593       wxbUtils::WaitForPrompt("6\n");
594       //WaitForEnd();
595       /*wxbPromptParser *pp = wxbUtils::WaitForPrompt(wxString() << configPanel->GetRowString("Before") << "\n", true);
596       int client = pp->getChoices()->Index(configPanel->GetRowString("Client"));
597       if (client == wxNOT_FOUND) {
598          wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected client.");
599          return;
600       }
601       delete pp;*/
602       
603       wxbTableParser* tableparser = new wxbTableParser();
604       wxbDataTokenizer* dt = new wxbDataTokenizer(true);
605       
606       wxbMainFrame::GetInstance()->Send(wxString() << configPanel->GetRowString("Before") << "\n");
607    
608       while (!tableparser->hasFinished() && !dt->hasFinished()) {
609          wxTheApp->Yield(true);
610          ::wxUsleep(100);
611       }
612       
613       wxString str;
614
615       if (dt->hasFinished() && !tableparser->hasFinished()) {
616          str = "";
617          if (dt->GetCount() > 1) {
618             str = (*dt)[dt->GetCount()-2];
619             str.RemoveLast();
620          }
621          wxbMainFrame::GetInstance()->SetStatusText(wxString("Error while starting restore: ") << str);
622          delete dt;
623          delete tableparser;
624          SetStatus(finished);
625          return;
626       }
627            
628       int tot = 0;
629       long l;
630       
631       for (i = 0; i < tableparser->GetCount(); i++) {
632          str = (*tableparser)[i][2];
633          str.Replace(",", "");
634          if (str.ToLong(&l)) {
635             tot += l;
636          }
637       }
638            
639       gauge->SetValue(0);
640       gauge->SetRange(tot);
641       
642       /*wxbMainFrame::GetInstance()->Print(
643                wxString("[") << tot << "]", CS_DEBUG);*/
644       
645       wxDateTime base = wxDateTime::Now();
646       wxDateTime newdate;
647       int done = 0;
648       int willdo = 0;
649       unsigned int lastindex = 0;
650       
651       int var = 0;
652       
653       while (!dt->hasFinished()) {
654          newdate = wxDateTime::Now();
655          if (newdate.Subtract(base).GetMilliseconds() > 10 ) {
656             base = newdate;
657             for (; lastindex < dt->GetCount(); lastindex++) {
658                if (((*dt)[lastindex].Find("Building directory tree for JobId ") == 0) && 
659                      ((i = (*dt)[lastindex].Find(" ...")) > 0)) {
660                   str = (*dt)[lastindex].Mid(34, i-34);
661                   for (i = 0; i < tableparser->GetCount(); i++) {
662                      if (str == (*tableparser)[i][0]) {
663                         str = (*tableparser)[i][2];
664                         str.Replace(",", "");
665                         if (str.ToLong(&l)) {
666                            done += willdo;
667                            willdo += l;
668                            var = (willdo-done)/3;
669                         }
670                         break;
671                      }
672                   }
673                }
674             }
675             
676             if (gauge->GetValue() <= done) {
677                gauge->SetValue(done);
678                if (var < 0)
679                   var = -var;
680             }
681             else if (gauge->GetValue() >= willdo) {
682                gauge->SetValue(willdo);
683                if (var > 0)
684                   var = -var;
685             }
686             
687             gauge->SetValue(gauge->GetValue()+var);
688             
689             /*wxbMainFrame::GetInstance()->Print(
690                wxString("[") << gauge->GetValue() << "/" << done
691                   << "-" << willdo << "]", CS_DEBUG);*/
692          }
693          wxTheApp->Yield(true);
694          ::wxUsleep(1);
695       }
696
697       gauge->SetValue(tot);
698       wxTheApp->Yield(true);
699       gauge->SetValue(0);
700       
701       delete dt;
702       delete tableparser;
703
704       if (cancelled) {
705          cancelled = 2;
706          return;
707       }
708
709       wxbUtils::WaitForEnd("unmark *\n");
710       wxTreeItemId root = tree->AddRoot(configPanel->GetRowString("Client"), -1, -1, new wxbTreeItemData("/", configPanel->GetRowString("Client"), 0));
711       currentTreeItem = root;
712       tree->Refresh();
713       tree->SelectItem(root);
714       CmdList(root);
715       wxbMainFrame::GetInstance()->SetStatusText("Right click on a file or on a directory, or double-click on its mark to add it to the restore list.");
716       tree->Expand(root);
717    }
718    else if (status == choosing) {
719       EnableConfig(false);
720       
721       totfilemessages = 0;
722       wxbDataTokenizer* dt;
723            
724       int j;
725       
726       dt = new wxbDataTokenizer(true);
727       wxbUtils::WaitForPrompt("done\n");
728
729       SetStatus(configuring);
730
731       for (i = 0; i < dt->GetCount(); i++) {
732          if ((j = (*dt)[i].Find(" files selected to be restored.")) > -1) {
733             (*dt)[i].Mid(0, j).ToLong(&totfilemessages);
734             break;
735          }
736
737          if ((j = (*dt)[i].Find(" file selected to be restored.")) > -1) {
738             (*dt)[i].Mid(0, j).ToLong(&totfilemessages);
739             break;
740          }
741       }
742       
743       wxbMainFrame::GetInstance()->SetStatusText(
744          wxString("Please configure your restore (") 
745             << totfilemessages <<  " files selected to be restored)...");
746       
747       UpdateSecondConfig(dt);
748       
749       delete dt;
750       
751       EnableConfig(true);
752       restorePanel->EnableApply(false);
753
754       if (totfilemessages == 0) {
755          wxbMainFrame::GetInstance()->Print("Restore failed : no file selected.\n", CS_DEBUG);
756          wxbMainFrame::GetInstance()->SetStatusText("Restore failed : no file selected.");
757          SetStatus(finished);
758          return;
759       }
760    }
761    else if (status == configuring) {
762       cancel->Enable(false);
763       jobid = "";
764       EnableConfig(false);
765     
766       wxbMainFrame::GetInstance()->SetStatusText("Restoring, please wait...");
767     
768       wxbDataTokenizer* dt;
769     
770       SetStatus(restoring);
771       wxbUtils::WaitForEnd("yes\n");
772
773       gauge->SetValue(0);
774       gauge->SetRange(totfilemessages);
775
776       wxDateTime currenttime;
777       
778       dt = wxbUtils::WaitForEnd("time\n", true);
779       wxStringTokenizer ttkz((*dt)[0], " ", wxTOKEN_STRTOK);
780       if ((currenttime.ParseDate(ttkz.GetNextToken()) == NULL) || // Date
781            (currenttime.ParseTime(ttkz.GetNextToken()) == NULL)) { // Time
782          currenttime.SetYear(1990); // If parsing fails, set currenttime to a dummy date
783       }
784       else {
785          currenttime -= wxTimeSpan::Seconds(30); //Adding a 30" tolerance
786       }
787       delete dt;
788     
789       wxDateTime scheduledtime;
790       wxStringTokenizer stkz(restorePanel->GetRowString("When"), " ", wxTOKEN_STRTOK);
791       
792       if ((scheduledtime.ParseDate(stkz.GetNextToken()) == NULL) || // Date
793            (scheduledtime.ParseTime(stkz.GetNextToken()) == NULL)) { // Time
794          scheduledtime.SetYear(2090); // If parsing fails, set scheduledtime to a dummy date
795       }
796
797       if (scheduledtime.Subtract(currenttime).IsLongerThan(wxTimeSpan::Seconds(150))) {
798          wxbMainFrame::GetInstance()->Print("Restore is scheduled in more than two minutes, wx-console will not wait for its completion.\n", CS_DEBUG);
799          wxbMainFrame::GetInstance()->SetStatusText("Restore is scheduled in more than two minutes, wx-console will not wait for its completion.");
800          SetStatus(finished);
801          return;
802       }
803
804       wxString cmd = "list jobid=";
805
806       wxString jobname = restorePanel->GetRowString("Job Name");
807
808       wxStopWatch sw;
809
810       wxbTableParser* tableparser;
811
812       while (true) {
813          tableparser = wxbUtils::CreateAndWaitForParser("list jobs\n");
814          
815          wxDateTime jobtime;
816          
817          if (jobname == (*tableparser)[tableparser->GetCount()-1][1]) {
818             wxStringTokenizer jtkz((*tableparser)[tableparser->GetCount()-1][2], " ", wxTOKEN_STRTOK);
819             if ((jobtime.ParseDate(jtkz.GetNextToken()) != NULL) && // Date
820                   (jobtime.ParseTime(jtkz.GetNextToken()) != NULL)) { // Time
821                if (jobtime.IsLaterThan(currenttime)) {
822                   jobid = (*tableparser)[tableparser->GetCount()-1][0];
823                   cmd << jobid << "\n";
824                   delete tableparser;
825                   cancel->Enable(true);
826                   break;
827                }
828             }
829          }
830    
831          delete tableparser;
832          
833          wxStopWatch sw2;
834          while (sw2.Time() < 2000) {
835             wxTheApp->Yield(true);
836             ::wxUsleep(100);
837          }
838          if (sw.Time() > 60000) {
839             wxbMainFrame::GetInstance()->Print("The restore job has not been created within one minute, wx-console will not wait for its completion anymore.\n", CS_DEBUG);
840             wxbMainFrame::GetInstance()->SetStatusText("The restore job has not been created within one minute, wx-console will not wait for its completion anymore.");
841             SetStatus(finished);
842             cancel->Enable(true);
843             return;
844          }
845       }
846       
847       long filemessages = 0;
848
849       while (true) {
850          tableparser = wxbUtils::CreateAndWaitForParser(cmd);
851          if ((*tableparser)[0][7] != "C") {
852             break;
853          }
854          delete tableparser;
855
856          dt = wxbUtils::WaitForEnd("messages\n", true);
857          
858          for (unsigned int i = 0; i < dt->GetCount(); i++) {
859             wxStringTokenizer tkz((*dt)[i], " ", wxTOKEN_STRTOK);
860    
861             wxDateTime datetime;
862    
863             //   Date    Time   name:   perm      ?   user   grp      size    date     time
864             //04-Apr-2004 17:19 Tom-fd: -rwx------   1 nicolas  None     514967 2004-03-20 20:03:42  filename
865    
866             if (datetime.ParseDate(tkz.GetNextToken()) != NULL) { // Date
867                if (datetime.ParseTime(tkz.GetNextToken()) != NULL) { // Time
868                   if (tkz.GetNextToken().Last() == ':') { // name:
869                   tkz.GetNextToken(); // perm
870                   tkz.GetNextToken(); // ?
871                   tkz.GetNextToken(); // user
872                   tkz.GetNextToken(); // grp
873                   tkz.GetNextToken(); // size
874                   if (datetime.ParseDate(tkz.GetNextToken()) != NULL) { //date
875                         if (datetime.ParseTime(tkz.GetNextToken()) != NULL) { //time
876                            filemessages++;
877                            //wxbMainFrame::GetInstance()->Print(wxString("(") << filemessages << ")", CS_DEBUG);
878                            gauge->SetValue(filemessages);
879                         }
880                      }
881                   }
882                }
883             }
884          }
885          
886          delete dt;
887
888          wxbMainFrame::GetInstance()->SetStatusText(wxString("Restoring, please wait (") << filemessages << " of " << totfilemessages << " files done)...");
889
890          wxStopWatch sw2;
891          while (sw2.Time() < 10000) {  
892             wxTheApp->Yield(true);
893             ::wxUsleep(100);
894          }
895       }
896
897       wxbUtils::WaitForEnd("messages\n");
898
899       gauge->SetValue(totfilemessages);
900
901       if ((*tableparser)[0][7] == "T") {
902          wxbMainFrame::GetInstance()->Print("Restore done successfully.\n", CS_DEBUG);
903          wxbMainFrame::GetInstance()->SetStatusText("Restore done successfully.");
904       }
905       else {
906          wxbMainFrame::GetInstance()->Print("Restore failed, please look at messages.\n", CS_DEBUG);
907          wxbMainFrame::GetInstance()->SetStatusText("Restore failed, please look at messages in console.");
908       }
909       delete tableparser;
910       SetStatus(finished);
911    }
912 }
913
914 /* The cancel button has been clicked */
915 void wxbRestorePanel::CmdCancel() {
916    cancelled = 1;
917    
918    if (status == restoring) {
919       if (jobid != "") {
920          wxbMainFrame::GetInstance()->Send(wxString("cancel job=") << jobid << "\n");
921       }
922       cancel->Enable(true);
923       return;
924    }
925    
926    wxStopWatch sw;
927    while ((IsWorking()) && (cancelled != 2)) {
928       wxTheApp->Yield(true);
929       ::wxUsleep(100);
930       if (sw.Time() > 30000) { /* 30 seconds timeout */
931          if (status == choosing) {
932             wxbMainFrame::GetInstance()->Send("quit\n");
933          }
934          else if (status == configuring) {
935             wxbMainFrame::GetInstance()->Send("no\n");
936          }
937          else if (status == restoring) {
938             
939          }
940          SetStatus(finished);
941          ::wxUsleep(1000);
942          return;
943       }
944    }
945    
946    switch (status) {
947    case choosing:
948       wxbMainFrame::GetInstance()->Send("quit\n");      
949       break;
950    case configuring:
951       wxbMainFrame::GetInstance()->Send("no\n");      
952       break;
953    default:
954       break;
955    }
956    ::wxUsleep(1000);
957    SetStatus(finished);
958 }
959
960 /* Apply configuration changes */
961
962 /*   1: Level (not appropriate)
963  *   2: Storage (yes)
964  *   3: Job (no)
965  *   4: FileSet (yes)
966  *   5: Client (yes)
967  *   6: When (yes : "Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now):")
968  *   7: Priority (yes : "Enter new Priority: (positive integer)")
969  *   8: Bootstrap (?)
970  *   9: Where (yes : "Please enter path prefix for restore (/ for none):")
971  *  10: Replace (yes : "Replace:\n 1: always\n 2: ifnewer\n 3: ifolder\n 4: never\n 
972  *         Select replace option (1-4):")
973  *  11: JobId (no)
974  */
975
976 void wxbRestorePanel::CmdConfigApply() {
977    if (cfgUpdated == 0) return;
978    
979    wxbMainFrame::GetInstance()->SetStatusText("Applying restore configuration changes...");
980    
981    EnableConfig(false);
982    
983    wxbDataTokenizer* dt = NULL;
984    
985    bool failed = false;
986    
987    while (cfgUpdated > 0) {
988       if (cancelled) {
989          cancelled = 2;
990          return;
991       }
992       wxString def; //String to send if can't use our data
993       if ((cfgUpdated >> ConfigWhere) & 1) {
994          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
995          wxbUtils::WaitForPrompt("9\n");
996          dt = new wxbDataTokenizer(true);
997          wxbUtils::WaitForPrompt(restorePanel->GetRowString("Where") + "\n");
998          def = "/tmp";
999          cfgUpdated = cfgUpdated & (~(1 << ConfigWhere));
1000       }
1001       else if ((cfgUpdated >> ConfigReplace) & 1) {
1002          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1003          wxbUtils::WaitForPrompt("10\n");
1004          dt = new wxbDataTokenizer(true);
1005          wxbUtils::WaitForPrompt(wxString() << (restorePanel->GetRowSelection("Replace")+1) << "\n");
1006          def = "1";
1007          cfgUpdated = cfgUpdated & (~(1 << ConfigReplace));
1008       }
1009       else if ((cfgUpdated >> ConfigWhen) & 1) {
1010          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1011          wxbUtils::WaitForPrompt("6\n");
1012          dt = new wxbDataTokenizer(true);
1013          wxbUtils::WaitForPrompt(restorePanel->GetRowString("When") + "\n");
1014          def = "";
1015          cfgUpdated = cfgUpdated & (~(1 << ConfigWhen));
1016       }
1017       else if ((cfgUpdated >> ConfigPriority) & 1) {
1018          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1019          wxbUtils::WaitForPrompt("7\n");
1020          dt = new wxbDataTokenizer(true);
1021          wxbUtils::WaitForPrompt(restorePanel->GetRowString("Priority") + "\n");
1022          def = "10";
1023          cfgUpdated = cfgUpdated & (~(1 << ConfigPriority));
1024       }
1025       else if ((cfgUpdated >> ConfigClient) & 1) {
1026          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1027          wxbPromptParser *pp = wxbUtils::WaitForPrompt("5\n", true);
1028          int client = pp->getChoices()->Index(restorePanel->GetRowString("Client"));
1029          if (client == wxNOT_FOUND) {
1030             wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected client.");
1031             failed = true;
1032             client = 1;
1033          }
1034          delete pp;
1035          dt = new wxbDataTokenizer(true);
1036          wxbUtils::WaitForPrompt(wxString() << client << "\n");
1037          def = "1";
1038          cfgUpdated = cfgUpdated & (~(1 << ConfigClient));
1039       }
1040       else if ((cfgUpdated >> ConfigFileset) & 1) {
1041          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1042          wxbPromptParser *pp = wxbUtils::WaitForPrompt("4\n", true);
1043          int fileset = pp->getChoices()->Index(restorePanel->GetRowString("Fileset"));
1044          if (fileset == wxNOT_FOUND) {
1045             wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected fileset.");
1046             failed = true;
1047             fileset = 1;
1048          }
1049          delete pp;
1050          dt = new wxbDataTokenizer(true);
1051          wxbUtils::WaitForPrompt(wxString() << fileset << "\n");
1052          def = "1";
1053          cfgUpdated = cfgUpdated & (~(1 << ConfigFileset));
1054       }
1055       else if ((cfgUpdated >> ConfigStorage) & 1) {
1056          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1057          wxbPromptParser *pp = wxbUtils::WaitForPrompt("2\n", true);
1058          int fileset = pp->getChoices()->Index(restorePanel->GetRowString("Storage"));
1059          if (fileset == wxNOT_FOUND) {
1060             wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected storage.");
1061             failed = true;
1062             fileset = 1;
1063          }
1064          delete pp;
1065          dt = new wxbDataTokenizer(true);
1066          wxbUtils::WaitForPrompt(wxString() << fileset << "\n");
1067          def = "1";
1068          cfgUpdated = cfgUpdated & (~(1 << ConfigFileset));
1069       }
1070       else {
1071          cfgUpdated = 0;
1072          break;
1073       }
1074                  
1075       unsigned int i;
1076       for (i = 0; i < dt->GetCount(); i++) {
1077          if ((*dt)[i].Find("Run Restore job") == 0) {
1078             break;
1079          }
1080       }
1081       
1082       if (i == dt->GetCount()) {
1083          delete dt;   
1084          dt = wxbUtils::WaitForEnd(def + "\n", true);
1085          failed = true;
1086       }
1087    }
1088    UpdateSecondConfig(dt); /* TODO: Check result */
1089    
1090    EnableConfig(true);
1091
1092    if (!failed) {
1093       wxbMainFrame::GetInstance()->SetStatusText("Restore configuration changes were applied.");
1094    }
1095
1096    delete dt;
1097 }
1098
1099 /* Cancel restore */
1100 void wxbRestorePanel::CmdConfigCancel() {
1101    wxbUtils::WaitForEnd("no\n");
1102    wxbMainFrame::GetInstance()->Print("Restore cancelled.\n", CS_DEBUG);
1103    wxbMainFrame::GetInstance()->SetStatusText("Restore cancelled.");
1104    SetStatus(finished);
1105 }
1106
1107 /* List jobs for a specified client */
1108 void wxbRestorePanel::CmdListJobs() {
1109    if (status == entered) {
1110       configPanel->ClearRowChoices("Before");
1111       wxbUtils::WaitForPrompt("query\n");
1112       wxbUtils::WaitForPrompt("6\n");
1113       wxbTableParser* tableparser = new wxbTableParser();
1114       wxbDataTokenizer* dt = wxbUtils::WaitForEnd(configPanel->GetRowString("Client") + "\n", true);
1115
1116       if (!tableparser->hasFinished()) {
1117          for (int i = 0; i < dt->Count(); i++) {
1118             if ((*dt)[i].Index("No results to list.") == 0) {
1119                configPanel->AddRowChoice("Before", "No backup found for this client.");
1120                configPanel->SetRowSelection("Before", 0);
1121                configPanel->EnableApply(true); // Enabling the not existing apply button disables the ok button.
1122                delete tableparser;
1123                delete dt;
1124                return;
1125             }
1126             else if (((*dt)[i].Index("ERROR") > -1) || ((*dt)[i].Index("Query failed") > -1)) {
1127                configPanel->AddRowChoice("Before", "Cannot get previous backups list, see console.");
1128                configPanel->SetRowSelection("Before", 0);
1129                configPanel->EnableApply(true); // Enabling the not existing apply button disables the ok button.
1130                delete tableparser;
1131                delete dt;
1132                return;
1133             }
1134          }
1135       }
1136       
1137       while (!tableparser->hasFinished()) {
1138          wxTheApp->Yield(true);
1139          ::wxUsleep(100);
1140       }
1141       
1142       delete dt;
1143
1144       for (int i = tableparser->GetCount()-1; i > -1; i--) {
1145          wxString str = (*tableparser)[i][3];
1146          wxDateTime datetime;
1147          const char* chr;
1148          if ( ( (chr = datetime.ParseDate(str.GetData()) ) != NULL ) && ( datetime.ParseTime(++chr) != NULL ) ) {
1149             datetime += wxTimeSpan::Seconds(1);
1150             //wxbMainFrame::GetInstance()->Print(wxString("-") << datetime.Format("%Y-%m-%d %H:%M:%S"), CS_DEBUG);
1151             configPanel->AddRowChoice("Before", datetime.Format("%Y-%m-%d %H:%M:%S"));
1152          }
1153          /*else {
1154          jobChoice->Append("Invalid");
1155          }*/
1156       }
1157            
1158       delete tableparser;
1159
1160       configPanel->SetRowSelection("Before", 0);
1161       configPanel->EnableApply(false); // Disabling the not existing apply button enables the ok button.
1162    }
1163 }
1164
1165 /* List files and directories for a specified tree item */
1166 void wxbRestorePanel::CmdList(wxTreeItemId item) {
1167    if (status == choosing) {
1168       list->DeleteAllItems();
1169
1170       if (!item.IsOk()) {
1171          return;
1172       }
1173       UpdateTreeItem(item, true, false);
1174     
1175       if (list->GetItemCount() >= 1) {
1176          int firstwidth = list->GetSize().GetWidth(); 
1177          for (int i = 2; i < 7; i++) {
1178             list->SetColumnWidth(i, wxLIST_AUTOSIZE);
1179             firstwidth -= list->GetColumnWidth(i);
1180          }
1181        
1182          list->SetColumnWidth(0, 18);
1183          firstwidth -= 18;
1184          list->SetColumnWidth(1, wxLIST_AUTOSIZE);
1185          if (list->GetColumnWidth(1) < firstwidth) {
1186             list->SetColumnWidth(1, firstwidth-25);
1187          }
1188       }
1189    }
1190 }
1191
1192 /* Mark a treeitem (directory) or a listitem (file or directory) */
1193 void wxbRestorePanel::CmdMark(wxTreeItemId treeitem, long* listitems, int listsize, int state) {
1194    if (status == choosing) {
1195       wxbTreeItemData** itemdata;
1196       int itemdatasize = 0;
1197       if (listsize == 0) {
1198          itemdata = new wxbTreeItemData*[1];
1199          itemdatasize = 1;
1200       }
1201       else {
1202          itemdata = new wxbTreeItemData*[listsize];
1203          itemdatasize = listsize;
1204       }
1205       
1206       if (listitems != NULL) {
1207          for (int i = 0; i < listsize; i++) {
1208             itemdata[i] = (wxbTreeItemData*)list->GetItemData(listitems[i]);
1209          }
1210       }
1211       else if (treeitem.IsOk()) {
1212          itemdata[0] = (wxbTreeItemData*)tree->GetItemData(treeitem);
1213       }
1214       else {
1215          delete[] itemdata;
1216          return;
1217       }
1218
1219       if (itemdata[0] == NULL) { //Should never happen
1220          delete[] itemdata;
1221          return;
1222       }
1223
1224       wxString dir = itemdata[0]->GetPath();
1225       wxString file;
1226
1227       if (dir != "/") {
1228          if (dir.GetChar(dir.Length()-1) == '/') {
1229             dir.RemoveLast();
1230          }
1231
1232          int i = dir.Find('/', TRUE);
1233          if (i == -1) {
1234             file = dir;
1235             dir = "/";
1236          }
1237          else { /* first dir below root */
1238             file = dir.Mid(i+1);
1239             dir = dir.Mid(0, i+1);
1240          }
1241       }
1242       else {
1243          dir = "/";
1244          file = "*";
1245       }
1246
1247       if (state == -1) {
1248          bool marked = false;
1249          bool unmarked = false;
1250          state = 0;
1251          for (int i = 0; i < itemdatasize; i++) {
1252             switch(itemdata[i]->GetMarked()) {
1253             case 0:
1254                unmarked = true;
1255                break;
1256             case 1:
1257                marked = true;
1258                break;
1259             case 2:
1260                marked = true;
1261                unmarked = true;
1262                break;
1263             default:
1264                break;
1265             }
1266             if (marked && unmarked)
1267                break;
1268          }
1269          if (marked) {
1270             if (unmarked) {
1271                state = 1;
1272             }
1273             else {
1274                state = 0;
1275             }
1276          }
1277          else {
1278             state = 1;
1279          }
1280       }
1281
1282       wxbUtils::WaitForEnd(wxString("cd \"") << dir << "\"\n");
1283       wxbUtils::WaitForEnd(wxString((state==1) ? "mark" : "unmark") << " \"" << file << "\"\n");
1284
1285       /* TODO: Check commands results */
1286
1287       /*if ((dir == "/") && (file == "*")) {
1288             itemdata->SetMarked((itemdata->GetMarked() == 1) ? 0 : 1);
1289       }*/
1290
1291       if (listitems == NULL) { /* tree item state changed */
1292          SetTreeItemState(treeitem, state);
1293          /*treeitem = tree->GetSelection();
1294          UpdateTree(treeitem, true);
1295          treeitem = tree->GetItemParent(treeitem);*/
1296       }
1297       else {
1298          for (int i = 0; i < listsize; i++) {
1299             SetListItemState(listitems[i], state);
1300          }
1301          /*UpdateTree(treeitem, (tree->GetSelection() == treeitem));
1302          treeitem = tree->GetItemParent(treeitem);*/
1303       }
1304
1305       /*while (treeitem.IsOk()) {
1306          WaitForList(treeitem, false);
1307          treeitem = tree->GetItemParent(treeitem);
1308       }*/
1309       
1310       delete[] itemdata;
1311    }
1312 }
1313
1314 /*----------------------------------------------------------------------------
1315    General functions
1316   ----------------------------------------------------------------------------*/
1317
1318 /* Run a dir command, and waits until result is fully received. */
1319 void wxbRestorePanel::UpdateTreeItem(wxTreeItemId item, bool updatelist, bool recurse) {
1320 //   this->updatelist = updatelist;
1321    wxbDataTokenizer* dt;
1322
1323    dt = wxbUtils::WaitForEnd(wxString("cd \"") << 
1324       static_cast<wxbTreeItemData*>(tree->GetItemData(item))
1325          ->GetPath() << "\"\n", false);
1326
1327    /* TODO: check command result */
1328    
1329    //delete dt;
1330
1331    status = listing;
1332
1333    if (updatelist)
1334       list->DeleteAllItems();
1335    dt = wxbUtils::WaitForEnd("dir\n", true);
1336    
1337    wxString str;
1338    
1339    for (unsigned int i = 0; i < dt->GetCount(); i++) {
1340       str = (*dt)[i];
1341       
1342       if (str.Find("cwd is:") == 0) { // Sometimes cd command result "infiltrate" into listings.
1343          break;
1344       }
1345
1346       str.RemoveLast();
1347
1348       wxString* file = ParseList(str);
1349       
1350       if (file == NULL)
1351             break;
1352
1353       wxTreeItemId treeid;
1354
1355       if (file[8].GetChar(file[8].Length()-1) == '/') {
1356          wxString itemStr;
1357
1358          long cookie;
1359          treeid = tree->GetFirstChild(item, cookie);
1360
1361          bool updated = false;
1362
1363          while (treeid.IsOk()) {
1364             itemStr = tree->GetItemText(treeid);
1365             if (file[8] == itemStr) {
1366                int stat = wxbTreeItemData::GetMarkedStatus(file[6]);
1367                if (static_cast<wxbTreeItemData*>(tree->GetItemData(treeid))->GetMarked() != stat) {
1368                   tree->SetItemImage(treeid, stat, wxTreeItemIcon_Normal);
1369                   tree->SetItemImage(treeid, stat, wxTreeItemIcon_Selected);
1370                   static_cast<wxbTreeItemData*>(tree->GetItemData(treeid))->SetMarked(file[6]);
1371                }
1372                if ((recurse) && (tree->IsExpanded(treeid))) {
1373                   UpdateTreeItem(treeid, false, true);
1374                }
1375                updated = true;
1376                break;
1377             }
1378             treeid = tree->GetNextChild(item, cookie);
1379          }
1380
1381          if (!updated) {
1382             int img = wxbTreeItemData::GetMarkedStatus(file[6]);
1383             treeid = tree->AppendItem(item, file[8], img, img, new wxbTreeItemData(file[7], file[8], file[6]));
1384          }
1385       }
1386
1387       if (updatelist) {
1388          long ind = list->InsertItem(list->GetItemCount(), wxbTreeItemData::GetMarkedStatus(file[6]));
1389          wxbTreeItemData* data = new wxbTreeItemData(file[7], file[8], file[6], ind);
1390          data->SetId(treeid);
1391          list->SetItemData(ind, (long)data);
1392          list->SetItem(ind, 1, file[8]); // filename
1393          list->SetItem(ind, 2, file[4]); //Size
1394          list->SetItem(ind, 3, file[5]); //date
1395          list->SetItem(ind, 4, file[0]); //perm
1396          list->SetItem(ind, 5, file[2]); //user
1397          list->SetItem(ind, 6, file[3]); //grp
1398       }
1399
1400       delete[] file;
1401    }
1402    
1403    delete dt;
1404    
1405    tree->Refresh();
1406    status = choosing;
1407 }
1408
1409 /* Parse dir command results. */
1410 wxString* wxbRestorePanel::ParseList(wxString line) {
1411    /* See ls_output in dird/ua_tree.c */
1412   
1413    //drwxrwxrwx   1 root     root           0  2004-04-03 14:35:21  f:/tocd/NVSU 1.00.00/
1414    //+ 10     +  ++ +   8  + +   8  ++   8  +  +      19         + *+ ->
1415    //0           12 15       24      32        42                  62
1416
1417    if (line.Length() < 63)
1418       return NULL;
1419
1420    wxString* ret = new wxString[9];
1421
1422    ret[0] = line.Mid(0, 10).Trim();
1423    ret[1] = line.Mid(12, 2).Trim();
1424    ret[2] = line.Mid(15, 8).Trim();
1425    ret[3] = line.Mid(24, 8).Trim();
1426    ret[4] = line.Mid(32, 8).Trim();
1427    ret[5] = line.Mid(42, 19).Trim();
1428    ret[6] = line.Mid(62, 1);
1429    ret[7] = line.Mid(63).Trim();
1430
1431    if (ret[6] == " ") ret[6] = "";
1432
1433    if (ret[7].GetChar(ret[7].Length()-1) == '/') {
1434       ret[8] = ret[7];
1435       ret[8].RemoveLast();
1436       ret[8] = ret[7].Mid(ret[8].Find('/', true)+1);
1437    }
1438    else {
1439       ret[8] = ret[7].Mid(ret[7].Find('/', true)+1);
1440    }
1441
1442    return ret;
1443 }
1444
1445 /* Sets a list item state, and update its parents and children if it is a directory */
1446 void wxbRestorePanel::SetListItemState(long listitem, int newstate) {
1447    wxbTreeItemData* itemdata = (wxbTreeItemData*)list->GetItemData(listitem);
1448    
1449    wxTreeItemId treeitem;
1450    
1451    itemdata->SetMarked(newstate);
1452    list->SetItemImage(listitem, newstate, 0); /* TODO: Find what these ints are for */
1453    list->SetItemImage(listitem, newstate, 1);
1454       
1455    if ((treeitem = itemdata->GetId()).IsOk()) {
1456       SetTreeItemState(treeitem, newstate);
1457    }
1458    else {
1459       UpdateTreeItemState(tree->GetSelection());
1460    }
1461 }
1462
1463 /* Sets a tree item state, and update its children, parents and list (if necessary) */
1464 void wxbRestorePanel::SetTreeItemState(wxTreeItemId item, int newstate) {
1465    long cookie;
1466    wxTreeItemId currentChild = tree->GetFirstChild(item, cookie);
1467
1468    wxbTreeItemData* itemdata;
1469
1470    while (currentChild.IsOk()) {
1471       itemdata = (wxbTreeItemData*)tree->GetItemData(currentChild);
1472       int state = itemdata->GetMarked();
1473       
1474       if (state != newstate) {
1475          itemdata->SetMarked(newstate);
1476          tree->SetItemImage(currentChild, newstate, wxTreeItemIcon_Normal);
1477          tree->SetItemImage(currentChild, newstate, wxTreeItemIcon_Selected);
1478       }
1479       
1480       currentChild = tree->GetNextChild(item, cookie);
1481    }
1482      
1483    itemdata = (wxbTreeItemData*)tree->GetItemData(item);  
1484    itemdata->SetMarked(newstate);
1485    tree->SetItemImage(item, newstate, wxTreeItemIcon_Normal);
1486    tree->SetItemImage(item, newstate, wxTreeItemIcon_Selected);
1487    tree->Refresh();
1488    
1489    if (tree->GetSelection() == item) {
1490       for (long i = 0; i < list->GetItemCount(); i++) {
1491          list->SetItemImage(i, newstate, 0); /* TODO: Find what these ints are for */
1492          list->SetItemImage(i, newstate, 1);
1493       }
1494    }
1495    
1496    UpdateTreeItemState(tree->GetItemParent(item));
1497 }
1498
1499 /* Update a tree item state, and its parents' state */
1500 void wxbRestorePanel::UpdateTreeItemState(wxTreeItemId item) {  
1501    if (!item.IsOk()) {
1502       return;
1503    }
1504    
1505    int state = 0;
1506        
1507    long cookie;
1508    wxTreeItemId currentChild = tree->GetFirstChild(item, cookie);
1509
1510    bool onechildmarked = false;
1511    bool onechildunmarked = false;
1512
1513    while (currentChild.IsOk()) {
1514       state = ((wxbTreeItemData*)tree->GetItemData(currentChild))->GetMarked();
1515       switch (state) {
1516       case 0:
1517          onechildunmarked = true;
1518          break;
1519       case 1:
1520          onechildmarked = true;
1521          break;
1522       case 2:
1523          onechildmarked = true;
1524          onechildunmarked = true;
1525          break;
1526       }
1527       
1528       if (onechildmarked && onechildunmarked) {
1529          break;
1530       }
1531       
1532       currentChild = tree->GetNextChild(item, cookie);
1533    }
1534    
1535    if (tree->GetSelection() == item) {
1536       for (long i = 0; i < list->GetItemCount(); i++) {
1537          state = ((wxbTreeItemData*)list->GetItemData(i))->GetMarked();
1538          
1539          switch (state) {
1540          case 0:
1541             onechildunmarked = true;
1542             break;
1543          case 1:
1544             onechildmarked = true;
1545             break;
1546          case 2:
1547             onechildmarked = true;
1548             onechildunmarked = true;
1549             break;
1550          }
1551          
1552          if (onechildmarked && onechildunmarked) {
1553             break;
1554          }
1555       }
1556    }
1557    
1558    state = 0;
1559    
1560    if (onechildmarked && onechildunmarked) {
1561       state = 2;
1562    }
1563    else if (onechildmarked) {
1564       state = 1;
1565    }
1566    else if (onechildunmarked) {
1567       state = 0;
1568    }
1569    else { // no child, don't change anything
1570       UpdateTreeItemState(tree->GetItemParent(item));
1571       return;
1572    }
1573    
1574    wxbTreeItemData* itemdata = (wxbTreeItemData*)tree->GetItemData(item);
1575       
1576    itemdata->SetMarked(state);
1577    tree->SetItemImage(item, state, wxTreeItemIcon_Normal);
1578    tree->SetItemImage(item, state, wxTreeItemIcon_Selected);
1579    
1580    UpdateTreeItemState(tree->GetItemParent(item));
1581 }
1582
1583 /* Refresh the whole tree. */
1584 void wxbRestorePanel::RefreshTree() {
1585    /* Save current selection */
1586    wxArrayString current;
1587    
1588    wxTreeItemId item = currentTreeItem;
1589    
1590    while ((item.IsOk()) && (item != tree->GetRootItem())) {
1591       current.Add(tree->GetItemText(item));
1592       item = tree->GetItemParent(item);
1593    }
1594
1595    /* Update the tree */
1596    UpdateTreeItem(tree->GetRootItem(), false, true);
1597
1598    /* Reselect the former selected item */   
1599    item = tree->GetRootItem();
1600    
1601    if (current.Count() == 0) {
1602       tree->SelectItem(item);
1603       return;
1604    }
1605    
1606    bool match;
1607    
1608    for (int i = current.Count()-1; i >= 0; i--) {
1609       long cookie;
1610       wxTreeItemId currentChild = tree->GetFirstChild(item, cookie);
1611       
1612       match = false;
1613       
1614       while (currentChild.IsOk()) {
1615          if (tree->GetItemText(currentChild) == current[i]) {
1616             item = currentChild;
1617             match = true;
1618             break;
1619          }
1620    
1621          currentChild = tree->GetNextChild(item, cookie);
1622       }
1623       
1624       if (!match) break;
1625    }
1626    
1627    UpdateTreeItem(item, true, false); /* Update the list */
1628    
1629    tree->SelectItem(item);
1630 }
1631
1632 void wxbRestorePanel::RefreshList() {
1633    if (currentTreeItem.IsOk()) {
1634       UpdateTreeItem(currentTreeItem, true, false); /* Update the list */
1635    }
1636 }
1637
1638 /* Update first config, adapting settings to the job name selected */
1639 void wxbRestorePanel::UpdateFirstConfig() {
1640    configPanel->Enable(false);
1641    wxbDataTokenizer* dt = wxbUtils::WaitForEnd(wxString(".defaults job=") + configPanel->GetRowString("Job Name") + "\n", true, false);
1642    /* job=RestoreFiles
1643     * pool=Default
1644     * messages=Standard
1645     * client=***
1646     * storage=File
1647     * where=/tmp/bacula-restores
1648     * level=0
1649     * type=Restore
1650     * fileset=Full Set */
1651    
1652    wxString name, str;
1653    unsigned int i;
1654    int j;
1655    wxString client;
1656    bool dolistjobs = false;
1657    
1658    for (i = 0; i < dt->GetCount(); i++) {
1659       str = (*dt)[i];
1660       if ((j = str.Find('=')) > -1) {
1661          name = str.Mid(0, j);
1662          if (name == "pool") {
1663             configPanel->SetRowString("Pool", str.Mid(j+1));
1664          }
1665          else if (name == "client") {
1666             str = str.Mid(j+1);
1667             if ((str != configPanel->GetRowString("Client")) || (configPanel->GetRowString("Before") == "")) {
1668                configPanel->SetRowString("Client", str);
1669                dolistjobs = true;
1670             }
1671          }
1672          else if (name == "storage") {
1673             configPanel->SetRowString("Storage", str.Mid(j+1));
1674          }
1675          else if (name == "fileset") {
1676             configPanel->SetRowString("Fileset", str.Mid(j+1));
1677          }
1678       }
1679    }
1680       
1681    delete dt;
1682    
1683    if (dolistjobs) {
1684       //wxTheApp->Yield(false);
1685       CmdListJobs();
1686    }
1687    configPanel->Enable(true);
1688 }
1689
1690 /* 
1691  * Update second config.
1692  * 
1693  * Run Restore job
1694  * JobName:    RestoreFiles
1695  * Bootstrap:  /var/lib/bacula/restore.bsr
1696  * Where:      /tmp/bacula-restores
1697  * Replace:    always
1698  * FileSet:    Full Set
1699  * Client:     tom-fd
1700  * Storage:    File
1701  * When:       2004-04-18 01:18:56
1702  * Priority:   10
1703  * OK to run? (yes/mod/no):
1704  * 
1705  */
1706 bool wxbRestorePanel::UpdateSecondConfig(wxbDataTokenizer* dt) {
1707    unsigned int i;
1708    for (i = 0; i < dt->GetCount(); i++) {
1709       if ((*dt)[i].Find("Run Restore job") == 0)
1710          break;
1711    }
1712    
1713    if ((i + 10) > dt->GetCount()) {
1714       return false;
1715    }
1716    
1717    int k;
1718    
1719    if ((k = (*dt)[++i].Find("JobName:")) != 0) return false;
1720    restorePanel->SetRowString("Job Name", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1721    if ((k = (*dt)[++i].Find("Bootstrap:")) != 0) return false;
1722    restorePanel->SetRowString("Bootstrap", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1723    if ((k = (*dt)[++i].Find("Where:")) != 0) return false;
1724    restorePanel->SetRowString("Where", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1725    
1726    if ((k = (*dt)[++i].Find("Replace:")) != 0) return false;
1727    wxString str = (*dt)[i].Mid(10).Trim(false).RemoveLast();
1728    if (str == "always") restorePanel->SetRowSelection("Replace", 0);
1729    else if (str == "ifnewer") restorePanel->SetRowSelection("Replace", 1);
1730    else if (str == "ifolder") restorePanel->SetRowSelection("Replace", 2);
1731    else if (str == "never") restorePanel->SetRowSelection("Replace", 3);
1732    else restorePanel->SetRowSelection("Replace", 0);
1733
1734    if ((k = (*dt)[++i].Find("FileSet:")) != 0) return false;
1735    restorePanel->SetRowString("Fileset", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1736    if ((k = (*dt)[++i].Find("Client:")) != 0) return false;
1737    restorePanel->SetRowString("Client", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1738    if ((k = (*dt)[++i].Find("Storage:")) != 0) return false;
1739    restorePanel->SetRowString("Storage", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1740    if ((k = (*dt)[++i].Find("When:")) != 0) return false;
1741    restorePanel->SetRowString("When", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1742    if ((k = (*dt)[++i].Find("Priority:")) != 0) return false;
1743    restorePanel->SetRowString("Priority", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1744    cfgUpdated = 0;
1745    
1746    restorePanel->Layout();
1747    
1748    return true;
1749 }
1750
1751 /*----------------------------------------------------------------------------
1752    Status function
1753   ----------------------------------------------------------------------------*/
1754
1755 /* Set current status by enabling/disabling components */
1756 void wxbRestorePanel::SetStatus(status_enum newstatus) {
1757    switch (newstatus) {
1758    case disabled:
1759       centerSizer->Remove(configPanel);
1760       centerSizer->Remove(restorePanel);
1761       centerSizer->Remove(treelistPanel);
1762       treelistPanel->Show(false);
1763       restorePanel->Show(false);
1764       centerSizer->Add(configPanel, 1, wxEXPAND);
1765       configPanel->Show(true);
1766       configPanel->Layout();
1767       centerSizer->Layout();
1768       this->Layout();
1769       start->SetLabel("Enter restore mode");
1770       start->Enable(false);
1771       configPanel->Enable(false);
1772       tree->Enable(false);
1773       list->Enable(false);
1774       gauge->Enable(false);
1775       cancel->Enable(false);
1776       cfgUpdated = 0;
1777       cancelled = 0;
1778       break;
1779    case finished:
1780       centerSizer->Remove(configPanel);
1781       centerSizer->Remove(restorePanel);
1782       centerSizer->Remove(treelistPanel);
1783       treelistPanel->Show(false);
1784       restorePanel->Show(false);
1785       centerSizer->Add(configPanel, 1, wxEXPAND);
1786       configPanel->Show(true);
1787       configPanel->Layout();
1788       centerSizer->Layout();
1789       this->Layout();
1790       tree->DeleteAllItems();
1791       list->DeleteAllItems();
1792       configPanel->ClearRowChoices("Client");
1793       configPanel->ClearRowChoices("Before");
1794       wxbMainFrame::GetInstance()->EnablePanels();
1795       newstatus = activable;
1796    case activable:
1797       cancelled = 0;
1798       start->SetLabel("Enter restore mode");
1799       start->Enable(true);
1800       configPanel->Enable(false);
1801       tree->Enable(false);
1802       list->Enable(false);
1803       gauge->Enable(false);
1804       cancel->Enable(false);
1805       cfgUpdated = 0;
1806       break;
1807    case entered:
1808       wxbMainFrame::GetInstance()->DisablePanels(this);
1809       gauge->SetValue(0);
1810       start->Enable(false);
1811       //start->SetLabel("Choose files to restore");
1812       configPanel->Enable(true);
1813       tree->Enable(false);
1814       list->Enable(false);
1815       cancel->Enable(true);
1816       cfgUpdated = 0;
1817       break;
1818    case listing:
1819       
1820       break;
1821    case choosing:
1822       start->Enable(true);
1823       start->SetLabel("Restore");
1824       centerSizer->Remove(configPanel);
1825       configPanel->Show(false);
1826       centerSizer->Add(treelistPanel, 1, wxEXPAND);
1827       treelistPanel->Show(true);
1828       treelistPanel->Layout();
1829       centerSizer->Layout();
1830       this->Layout();
1831       tree->Enable(true);
1832       list->Enable(true);
1833       SetWorking(false);
1834       break;
1835    case configuring:
1836       start->Enable(false);
1837       configPanel->Enable(false);
1838       tree->Enable(false);
1839       list->Enable(false);
1840       centerSizer->Remove(treelistPanel);
1841       treelistPanel->Show(false);
1842       centerSizer->Add(restorePanel, 1, wxEXPAND);
1843       restorePanel->Show(true);
1844       restorePanel->Layout();
1845       centerSizer->Layout();
1846       this->Layout();
1847       restorePanel->EnableApply(false);
1848       break;
1849    case restoring:
1850       start->SetLabel("Restoring...");
1851       gauge->Enable(true);
1852       gauge->SetValue(0);
1853       start->Enable(false);
1854       configPanel->Enable(false);
1855       tree->Enable(false);
1856       list->Enable(false);
1857       SetWorking(true);
1858       break;
1859    }
1860    status = newstatus;
1861 }
1862
1863 /*----------------------------------------------------------------------------
1864    UI related
1865   ----------------------------------------------------------------------------*/
1866
1867 void wxbRestorePanel::SetWorking(bool working) {
1868    this->working = working;
1869    if (working) {
1870       SetCursor(*wxHOURGLASS_CURSOR);
1871 //      SetEvtHandlerEnabled(false); //EVTQUEUE
1872    }
1873 //   else if (!processing) { /* Empty event queue if we aren't already doing this */ //EVTQUEUE
1874    else {
1875 //      processing = true; //EVTQUEUE
1876       SetCursor(*wxSTANDARD_CURSOR);
1877 //      SetEvtHandlerEnabled(true); //EVTQUEUE
1878 /*      wxNode *node = pendingEvents->First(); //EVTQUEUE
1879       while ( node ) {
1880          wxEvent *event = (wxEvent *)node->Data();
1881          delete node;
1882    
1883          wxEvtHandler::ProcessEvent(*event);
1884          delete event;
1885    
1886          node = pendingEvents->First();
1887       }
1888       processing = false;*/
1889    }
1890 }
1891
1892 bool wxbRestorePanel::IsWorking() {
1893    return this->working;
1894 }
1895
1896 void wxbRestorePanel::EnableConfig(bool enable) {
1897    restorePanel->Enable(enable);
1898 }
1899
1900 /*----------------------------------------------------------------------------
1901    Event handling
1902   ----------------------------------------------------------------------------*/
1903
1904
1905 //EVTQUEUE
1906 /*
1907 bool wxbRestorePanel::ProcessEvent(wxEvent& event) {
1908    if (IsWorking() || processing) {
1909       wxEvent *eventCopy = event.Clone();
1910       
1911       pendingEvents->Append(eventCopy);
1912       return TRUE;
1913    }
1914    else {
1915       return wxEvtHandler::ProcessEvent(event);
1916    }
1917 }
1918 */
1919
1920 void wxbRestorePanel::OnCancel(wxCommandEvent& event) {
1921    cancel->Enable(false);
1922    SetCursor(*wxHOURGLASS_CURSOR);
1923    CmdCancel();
1924    SetCursor(*wxSTANDARD_CURSOR);
1925 }
1926
1927 void wxbRestorePanel::OnStart(wxCommandEvent& event) {
1928    if (IsWorking()) {
1929       AddPendingEvent(event);
1930       return;
1931    }
1932    SetWorking(true);
1933    CmdStart();
1934    SetWorking(false);
1935 }
1936
1937 void wxbRestorePanel::OnTreeChanging(wxTreeEvent& event) {
1938    if (IsWorking()) {
1939       AddPendingEvent(event);
1940       event.Veto();
1941    }
1942 }
1943
1944 void wxbRestorePanel::OnTreeExpanding(wxTreeEvent& event) {
1945    if (IsWorking()) {
1946       AddPendingEvent(event);
1947       event.Veto();
1948       return;
1949    }
1950    //working = true;
1951    //CmdList(event.GetItem());
1952    if (tree->GetSelection() != event.GetItem()) {
1953       tree->SelectItem(event.GetItem());
1954    }
1955    //working = false;
1956 }
1957
1958 void wxbRestorePanel::OnTreeChanged(wxTreeEvent& event) {
1959    if (IsWorking()) {
1960       AddPendingEvent(event);
1961       return;
1962    }
1963    if (currentTreeItem == event.GetItem()) {
1964       return;
1965    }
1966    treeadd->Enable(false);
1967    treeremove->Enable(false);
1968    treerefresh->Enable(false);
1969    markWhenListingDone = false;
1970    SetWorking(true);
1971    currentTreeItem = event.GetItem();
1972    CmdList(event.GetItem());
1973    if (markWhenListingDone) {
1974       CmdMark(event.GetItem(), NULL, 0);
1975       tree->Refresh();
1976    }
1977    SetWorking(false);
1978    if (event.GetItem().IsOk()) {
1979       int status = ((wxbTreeItemData*)tree->GetItemData(event.GetItem()))->GetMarked();
1980       treeadd->Enable(status != 1);
1981       treeremove->Enable(status != 0);
1982    }
1983    treerefresh->Enable(true);
1984 }
1985
1986 void wxbRestorePanel::OnTreeMarked(wxbTreeMarkedEvent& event) {
1987    csprint("Tree marked", CS_DEBUG);
1988    if (IsWorking()) {
1989       if (tree->GetSelection() == event.GetItem()) {
1990          markWhenListingDone = !markWhenListingDone;
1991       }
1992       AddPendingEvent(event);
1993       return;
1994    }
1995    SetWorking(true);
1996    CmdMark(event.GetItem(), NULL, 0);
1997    //event.Skip();
1998    tree->Refresh();
1999    SetWorking(false);
2000    if (event.GetItem().IsOk()) {
2001       int status = ((wxbTreeItemData*)tree->GetItemData(event.GetItem()))->GetMarked();
2002       treeadd->Enable(status != 1);
2003       treeremove->Enable(status != 0);
2004    }
2005 }
2006
2007 void wxbRestorePanel::OnTreeAdd(wxCommandEvent& event) {
2008    if (IsWorking()) {
2009       AddPendingEvent(event);
2010       return;
2011    }
2012    
2013    if (currentTreeItem.IsOk()) {
2014       SetWorking(true);
2015       CmdMark(currentTreeItem, NULL, 0, 1);
2016       tree->Refresh();
2017       treeadd->Enable(0);
2018       treeremove->Enable(1);
2019       SetWorking(false);
2020    }
2021 }
2022
2023 void wxbRestorePanel::OnTreeRemove(wxCommandEvent& event) {
2024    if (IsWorking()) {
2025       AddPendingEvent(event);
2026       return;
2027    }
2028    
2029    if (currentTreeItem.IsOk()) {
2030       SetWorking(true);
2031       CmdMark(currentTreeItem, NULL, 0, 0);
2032       tree->Refresh();
2033       treeadd->Enable(1);
2034       treeremove->Enable(0);
2035       SetWorking(false);
2036    }
2037 }
2038
2039 void wxbRestorePanel::OnTreeRefresh(wxCommandEvent& event) {
2040    if (IsWorking()) {
2041       AddPendingEvent(event);
2042       return;
2043    }
2044    
2045    SetWorking(true);
2046    RefreshTree();
2047    SetWorking(false);
2048 }
2049
2050 void wxbRestorePanel::OnListMarked(wxbListMarkedEvent& event) {
2051    if (IsWorking()) {
2052       AddPendingEvent(event);
2053       //event.Skip();
2054       return;
2055    }
2056    
2057    if (list->GetSelectedItemCount() == 0) {
2058       return;
2059    }
2060    
2061    SetWorking(true);  
2062    
2063    long* items = new long[list->GetSelectedItemCount()];
2064    
2065    int num = 0;
2066    
2067    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2068    while (item != -1) {
2069       items[num] = item;
2070       num++;
2071       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2072    }
2073      
2074    CmdMark(wxTreeItemId(), items, num);
2075    
2076    delete[] items;
2077    
2078    wxListEvent listevt;
2079    
2080    OnListChanged(listevt);
2081    
2082    event.Skip();
2083    tree->Refresh();
2084    SetWorking(false);
2085 }
2086
2087 void wxbRestorePanel::OnListActivated(wxListEvent& event) {
2088    if (IsWorking()) {
2089       AddPendingEvent(event);
2090       //event.Skip();
2091       return;
2092    }
2093    SetWorking(true);
2094    long item = event.GetIndex();
2095 //   long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
2096    if (item > -1) {
2097       wxbTreeItemData* itemdata = (wxbTreeItemData*)list->GetItemData(item);
2098       wxString name = itemdata->GetName();
2099       event.Skip();
2100
2101       wxString itemStr;
2102
2103       long cookie;
2104
2105       if (name.GetChar(name.Length()-1) == '/') {
2106          wxTreeItemId currentChild = tree->GetFirstChild(currentTreeItem, cookie);
2107
2108          while (currentChild.IsOk()) {
2109             wxString name2 = tree->GetItemText(currentChild);
2110             if (name2 == name) {
2111                //tree->UnselectAll();
2112                SetWorking(false);
2113                tree->Expand(currentTreeItem);
2114                tree->SelectItem(currentChild);
2115                //tree->Refresh();
2116                return;
2117             }
2118             currentChild = tree->GetNextChild(currentTreeItem, cookie);
2119          }
2120       }
2121    }
2122    SetWorking(false);
2123 }
2124
2125 void wxbRestorePanel::OnListChanged(wxListEvent& event) {
2126    if (IsWorking()) {
2127       AddPendingEvent(event);
2128       return;
2129    }
2130  
2131    listadd->Enable(false);
2132    listremove->Enable(false);
2133    
2134    bool marked = false;
2135    bool unmarked = false;
2136    
2137    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2138    while (item != -1) {
2139       switch (((wxbTreeItemData*)list->GetItemData(item))->GetMarked()) {
2140       case 0:
2141          unmarked = true;
2142          break;
2143       case 1:
2144          marked = true;
2145          break;
2146       case 2:
2147          marked = true;
2148          unmarked = true;
2149          break;
2150       default:
2151          break;
2152          // Should never happen
2153       }
2154       if (marked && unmarked) break;
2155       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2156    }
2157    
2158    listadd->Enable(unmarked);
2159    listremove->Enable(marked);
2160 }
2161
2162 void wxbRestorePanel::OnListAdd(wxCommandEvent& event) {
2163    if (IsWorking()) {
2164       AddPendingEvent(event);
2165       return;
2166    }
2167    
2168    SetWorking(true);
2169    
2170    long* items = new long[list->GetSelectedItemCount()];
2171    
2172    int num = 0;
2173    
2174    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2175    while (item != -1) {
2176       items[num] = item;
2177       num++;
2178       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2179    }
2180      
2181    CmdMark(wxTreeItemId(), items, num, 1);
2182    
2183    delete[] items;
2184    
2185    tree->Refresh();
2186    SetWorking(false);
2187    
2188    listadd->Enable(false);
2189    listremove->Enable(true);
2190 }
2191
2192 void wxbRestorePanel::OnListRemove(wxCommandEvent& event) {
2193    if (IsWorking()) {
2194       AddPendingEvent(event);
2195       return;
2196    }
2197    
2198    SetWorking(true);
2199    
2200    long* items = new long[list->GetSelectedItemCount()];
2201    
2202    int num = 0;
2203    
2204    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2205    while (item != -1) {
2206       items[num] = item;
2207       num++;
2208       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2209    }
2210      
2211    CmdMark(wxTreeItemId(), items, num, 0);
2212    
2213    delete[] items;
2214    
2215    tree->Refresh();
2216    SetWorking(false);
2217    
2218    listadd->Enable(true);
2219    listremove->Enable(false);
2220 }
2221
2222 void wxbRestorePanel::OnListRefresh(wxCommandEvent& event) {
2223    if (IsWorking()) {
2224       AddPendingEvent(event);
2225       return;
2226    }
2227    
2228    SetWorking(true);
2229    RefreshList();
2230    SetWorking(false);
2231 }
2232
2233 void wxbRestorePanel::OnConfigUpdated(wxCommandEvent& event) {
2234    if (status == entered) {
2235       if (event.GetId() == ConfigJobName) {
2236          if (IsWorking()) {
2237             return;
2238          }
2239          SetWorking(true);
2240          UpdateFirstConfig();
2241          SetWorking(false);
2242       }
2243       else if (event.GetId() == ConfigClient) {
2244          if (IsWorking()) {
2245             return;
2246          }
2247          SetWorking(true);
2248          configPanel->Enable(false);
2249          CmdListJobs();
2250          configPanel->Enable(true);
2251          SetWorking(false);
2252       }
2253       cfgUpdated = cfgUpdated | (1 << event.GetId());
2254    }
2255    else if (status == configuring) {
2256       restorePanel->EnableApply(true);
2257       cfgUpdated = cfgUpdated | (1 << event.GetId());
2258    }
2259 }
2260
2261 void wxbRestorePanel::OnConfigOk(wxCommandEvent& WXUNUSED(event)) {
2262    if (status != configuring) return;
2263    if (IsWorking()) {
2264       return;
2265    }
2266    SetWorking(true);
2267    CmdStart();
2268    SetWorking(false);
2269 }
2270
2271 void wxbRestorePanel::OnConfigApply(wxCommandEvent& WXUNUSED(event)) {
2272    if (status != configuring) return;
2273    if (IsWorking()) {
2274       return;
2275    }
2276    SetWorking(true);
2277    CmdConfigApply();
2278    if (cfgUpdated == 0) {
2279       restorePanel->EnableApply(false);
2280    }
2281    SetWorking(false);  
2282 }
2283
2284 void wxbRestorePanel::OnConfigCancel(wxCommandEvent& WXUNUSED(event)) {
2285    if (status != configuring) return;
2286    if (IsWorking()) {
2287       return;
2288    }
2289    SetWorking(true);
2290    CmdConfigCancel();
2291    SetWorking(false);
2292 }
2293