]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbrestorepanel.cpp
Some Win32 fixes
[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          for (i = 0; i < tableparser->GetCount(); i++) {
818             if (jobname == (*tableparser)[i][1]) {
819                wxStringTokenizer jtkz((*tableparser)[i][2], " ", wxTOKEN_STRTOK);
820                if ((jobtime.ParseDate(jtkz.GetNextToken()) != NULL) && // Date
821                      (jobtime.ParseTime(jtkz.GetNextToken()) != NULL)) { // Time
822                   if (jobtime.IsLaterThan(currenttime)) {
823                      jobid = (*tableparser)[i][0];
824                      cmd << jobid << "\n";
825                      delete tableparser;
826                      tableparser = NULL;
827                      cancel->Enable(true);
828                      break;
829                   }
830                }
831             }
832          }
833    
834          if (tableparser == NULL) { //The job was found
835             break;
836          }
837          
838          delete tableparser;
839          
840          wxStopWatch sw2;
841          while (sw2.Time() < 2000) {
842             wxTheApp->Yield(true);
843             ::wxUsleep(100);
844          }
845          if (sw.Time() > 60000) {
846             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);
847             wxbMainFrame::GetInstance()->SetStatusText("The restore job has not been created within one minute, wx-console will not wait for its completion anymore.");
848             SetStatus(finished);
849             cancel->Enable(true);
850             return;
851          }
852       }
853       
854       long filemessages = 0;
855
856       while (true) {
857          tableparser = wxbUtils::CreateAndWaitForParser(cmd);
858          if ((*tableparser)[0][7] != "C") {
859             break;
860          }
861          delete tableparser;
862
863          dt = wxbUtils::WaitForEnd("messages\n", true);
864          
865          for (unsigned int i = 0; i < dt->GetCount(); i++) {
866             wxStringTokenizer tkz((*dt)[i], " ", wxTOKEN_STRTOK);
867    
868             wxDateTime datetime;
869    
870             //   Date    Time   name:   perm      ?   user   grp      size    date     time
871             //04-Apr-2004 17:19 Tom-fd: -rwx------   1 nicolas  None     514967 2004-03-20 20:03:42  filename
872    
873             if (datetime.ParseDate(tkz.GetNextToken()) != NULL) { // Date
874                if (datetime.ParseTime(tkz.GetNextToken()) != NULL) { // Time
875                   if (tkz.GetNextToken().Last() == ':') { // name:
876                   tkz.GetNextToken(); // perm
877                   tkz.GetNextToken(); // ?
878                   tkz.GetNextToken(); // user
879                   tkz.GetNextToken(); // grp
880                   tkz.GetNextToken(); // size
881                   if (datetime.ParseDate(tkz.GetNextToken()) != NULL) { //date
882                         if (datetime.ParseTime(tkz.GetNextToken()) != NULL) { //time
883                            filemessages++;
884                            //wxbMainFrame::GetInstance()->Print(wxString("(") << filemessages << ")", CS_DEBUG);
885                            gauge->SetValue(filemessages);
886                         }
887                      }
888                   }
889                }
890             }
891          }
892          
893          delete dt;
894
895          wxbMainFrame::GetInstance()->SetStatusText(wxString("Restoring, please wait (") << filemessages << " of " << totfilemessages << " files done)...");
896
897          wxStopWatch sw2;
898          while (sw2.Time() < 10000) {  
899             wxTheApp->Yield(true);
900             ::wxUsleep(100);
901          }
902       }
903
904       wxbUtils::WaitForEnd("messages\n");
905
906       gauge->SetValue(totfilemessages);
907
908       if ((*tableparser)[0][7] == "T") {
909          wxbMainFrame::GetInstance()->Print("Restore done successfully.\n", CS_DEBUG);
910          wxbMainFrame::GetInstance()->SetStatusText("Restore done successfully.");
911       }
912       else {
913          wxbMainFrame::GetInstance()->Print("Restore failed, please look at messages.\n", CS_DEBUG);
914          wxbMainFrame::GetInstance()->SetStatusText("Restore failed, please look at messages in console.");
915       }
916       delete tableparser;
917       SetStatus(finished);
918    }
919 }
920
921 /* The cancel button has been clicked */
922 void wxbRestorePanel::CmdCancel() {
923    cancelled = 1;
924    
925    if (status == restoring) {
926       if (jobid != "") {
927          wxbMainFrame::GetInstance()->Send(wxString("cancel job=") << jobid << "\n");
928       }
929       cancel->Enable(true);
930       return;
931    }
932    
933    wxStopWatch sw;
934    while ((IsWorking()) && (cancelled != 2)) {
935       wxTheApp->Yield(true);
936       ::wxUsleep(100);
937       if (sw.Time() > 30000) { /* 30 seconds timeout */
938          if (status == choosing) {
939             wxbMainFrame::GetInstance()->Send("quit\n");
940          }
941          else if (status == configuring) {
942             wxbMainFrame::GetInstance()->Send("no\n");
943          }
944          else if (status == restoring) {
945             
946          }
947          SetStatus(finished);
948          ::wxUsleep(1000);
949          return;
950       }
951    }
952    
953    switch (status) {
954    case choosing:
955       wxbMainFrame::GetInstance()->Send("quit\n");      
956       break;
957    case configuring:
958       wxbMainFrame::GetInstance()->Send("no\n");      
959       break;
960    default:
961       break;
962    }
963    ::wxUsleep(1000);
964    SetStatus(finished);
965 }
966
967 /* Apply configuration changes */
968
969 /*   1: Level (not appropriate)
970  *   2: Storage (yes)
971  *   3: Job (no)
972  *   4: FileSet (yes)
973  *   5: Client (yes)
974  *   6: When (yes : "Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now):")
975  *   7: Priority (yes : "Enter new Priority: (positive integer)")
976  *   8: Bootstrap (?)
977  *   9: Where (yes : "Please enter path prefix for restore (/ for none):")
978  *  10: Replace (yes : "Replace:\n 1: always\n 2: ifnewer\n 3: ifolder\n 4: never\n 
979  *         Select replace option (1-4):")
980  *  11: JobId (no)
981  */
982
983 void wxbRestorePanel::CmdConfigApply() {
984    if (cfgUpdated == 0) return;
985    
986    wxbMainFrame::GetInstance()->SetStatusText("Applying restore configuration changes...");
987    
988    EnableConfig(false);
989    
990    wxbDataTokenizer* dt = NULL;
991    
992    bool failed = false;
993    
994    while (cfgUpdated > 0) {
995       if (cancelled) {
996          cancelled = 2;
997          return;
998       }
999       wxString def; //String to send if can't use our data
1000       if ((cfgUpdated >> ConfigWhere) & 1) {
1001          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1002          wxbUtils::WaitForPrompt("9\n");
1003          dt = new wxbDataTokenizer(true);
1004          wxbUtils::WaitForPrompt(restorePanel->GetRowString("Where") + "\n");
1005          def = "/tmp";
1006          cfgUpdated = cfgUpdated & (~(1 << ConfigWhere));
1007       }
1008       else if ((cfgUpdated >> ConfigReplace) & 1) {
1009          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1010          wxbUtils::WaitForPrompt("10\n");
1011          dt = new wxbDataTokenizer(true);
1012          wxbUtils::WaitForPrompt(wxString() << (restorePanel->GetRowSelection("Replace")+1) << "\n");
1013          def = "1";
1014          cfgUpdated = cfgUpdated & (~(1 << ConfigReplace));
1015       }
1016       else if ((cfgUpdated >> ConfigWhen) & 1) {
1017          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1018          wxbUtils::WaitForPrompt("6\n");
1019          dt = new wxbDataTokenizer(true);
1020          wxbUtils::WaitForPrompt(restorePanel->GetRowString("When") + "\n");
1021          def = "";
1022          cfgUpdated = cfgUpdated & (~(1 << ConfigWhen));
1023       }
1024       else if ((cfgUpdated >> ConfigPriority) & 1) {
1025          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1026          wxbUtils::WaitForPrompt("7\n");
1027          dt = new wxbDataTokenizer(true);
1028          wxbUtils::WaitForPrompt(restorePanel->GetRowString("Priority") + "\n");
1029          def = "10";
1030          cfgUpdated = cfgUpdated & (~(1 << ConfigPriority));
1031       }
1032       else if ((cfgUpdated >> ConfigClient) & 1) {
1033          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1034          wxbPromptParser *pp = wxbUtils::WaitForPrompt("5\n", true);
1035          int client = pp->getChoices()->Index(restorePanel->GetRowString("Client"));
1036          if (client == wxNOT_FOUND) {
1037             wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected client.");
1038             failed = true;
1039             client = 1;
1040          }
1041          delete pp;
1042          dt = new wxbDataTokenizer(true);
1043          wxbUtils::WaitForPrompt(wxString() << client << "\n");
1044          def = "1";
1045          cfgUpdated = cfgUpdated & (~(1 << ConfigClient));
1046       }
1047       else if ((cfgUpdated >> ConfigFileset) & 1) {
1048          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1049          wxbPromptParser *pp = wxbUtils::WaitForPrompt("4\n", true);
1050          int fileset = pp->getChoices()->Index(restorePanel->GetRowString("Fileset"));
1051          if (fileset == wxNOT_FOUND) {
1052             wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected fileset.");
1053             failed = true;
1054             fileset = 1;
1055          }
1056          delete pp;
1057          dt = new wxbDataTokenizer(true);
1058          wxbUtils::WaitForPrompt(wxString() << fileset << "\n");
1059          def = "1";
1060          cfgUpdated = cfgUpdated & (~(1 << ConfigFileset));
1061       }
1062       else if ((cfgUpdated >> ConfigStorage) & 1) {
1063          wxbUtils::WaitForPrompt("mod\n"); /* TODO: check results */
1064          wxbPromptParser *pp = wxbUtils::WaitForPrompt("2\n", true);
1065          int fileset = pp->getChoices()->Index(restorePanel->GetRowString("Storage"));
1066          if (fileset == wxNOT_FOUND) {
1067             wxbMainFrame::GetInstance()->SetStatusText("Failed to find the selected storage.");
1068             failed = true;
1069             fileset = 1;
1070          }
1071          delete pp;
1072          dt = new wxbDataTokenizer(true);
1073          wxbUtils::WaitForPrompt(wxString() << fileset << "\n");
1074          def = "1";
1075          cfgUpdated = cfgUpdated & (~(1 << ConfigFileset));
1076       }
1077       else {
1078          cfgUpdated = 0;
1079          break;
1080       }
1081                  
1082       unsigned int i;
1083       for (i = 0; i < dt->GetCount(); i++) {
1084          if ((*dt)[i].Find("Run Restore job") == 0) {
1085             break;
1086          }
1087       }
1088       
1089       if (i == dt->GetCount()) {
1090          delete dt;   
1091          dt = wxbUtils::WaitForEnd(def + "\n", true);
1092          failed = true;
1093       }
1094    }
1095    UpdateSecondConfig(dt); /* TODO: Check result */
1096    
1097    EnableConfig(true);
1098
1099    if (!failed) {
1100       wxbMainFrame::GetInstance()->SetStatusText("Restore configuration changes were applied.");
1101    }
1102
1103    delete dt;
1104 }
1105
1106 /* Cancel restore */
1107 void wxbRestorePanel::CmdConfigCancel() {
1108    wxbUtils::WaitForEnd("no\n");
1109    wxbMainFrame::GetInstance()->Print("Restore cancelled.\n", CS_DEBUG);
1110    wxbMainFrame::GetInstance()->SetStatusText("Restore cancelled.");
1111    SetStatus(finished);
1112 }
1113
1114 /* List jobs for a specified client */
1115 void wxbRestorePanel::CmdListJobs() {
1116    if (status == entered) {
1117       configPanel->ClearRowChoices("Before");
1118       wxbUtils::WaitForPrompt("query\n");
1119       wxbUtils::WaitForPrompt("6\n");
1120       wxbTableParser* tableparser = new wxbTableParser();
1121       wxbDataTokenizer* dt = wxbUtils::WaitForEnd(configPanel->GetRowString("Client") + "\n", true);
1122
1123       if (!tableparser->hasFinished()) {
1124          for (unsigned int i = 0; i < dt->Count(); i++) {
1125             if ((*dt)[i].Find("No results to list.") == 0) {
1126                configPanel->AddRowChoice("Before", "No backup found for this client.");
1127                configPanel->SetRowSelection("Before", 0);
1128                configPanel->EnableApply(true); // Enabling the not existing apply button disables the ok button.
1129                delete tableparser;
1130                delete dt;
1131                return;
1132             }
1133             else if (((*dt)[i].Find("ERROR") > -1) || ((*dt)[i].Find("Query failed") > -1)) {
1134                configPanel->AddRowChoice("Before", "Cannot get previous backups list, see console.");
1135                configPanel->SetRowSelection("Before", 0);
1136                configPanel->EnableApply(true); // Enabling the not existing apply button disables the ok button.
1137                delete tableparser;
1138                delete dt;
1139                return;
1140             }
1141          }
1142       }
1143       
1144       while (!tableparser->hasFinished()) {
1145          wxTheApp->Yield(true);
1146          ::wxUsleep(100);
1147       }
1148       
1149       delete dt;
1150
1151       for (int i = tableparser->GetCount()-1; i > -1; i--) {
1152          wxString str = (*tableparser)[i][3];
1153          wxDateTime datetime;
1154          const char* chr;
1155          if ( ( (chr = datetime.ParseDate(str.GetData()) ) != NULL ) && ( datetime.ParseTime(++chr) != NULL ) ) {
1156             datetime += wxTimeSpan::Seconds(1);
1157             //wxbMainFrame::GetInstance()->Print(wxString("-") << datetime.Format("%Y-%m-%d %H:%M:%S"), CS_DEBUG);
1158             configPanel->AddRowChoice("Before", datetime.Format("%Y-%m-%d %H:%M:%S"));
1159          }
1160          /*else {
1161          jobChoice->Append("Invalid");
1162          }*/
1163       }
1164            
1165       delete tableparser;
1166
1167       configPanel->SetRowSelection("Before", 0);
1168       configPanel->EnableApply(false); // Disabling the not existing apply button enables the ok button.
1169    }
1170 }
1171
1172 /* List files and directories for a specified tree item */
1173 void wxbRestorePanel::CmdList(wxTreeItemId item) {
1174    if (status == choosing) {
1175       list->DeleteAllItems();
1176
1177       if (!item.IsOk()) {
1178          return;
1179       }
1180       UpdateTreeItem(item, true, false);
1181     
1182       if (list->GetItemCount() >= 1) {
1183          int firstwidth = list->GetSize().GetWidth(); 
1184          for (int i = 2; i < 7; i++) {
1185             list->SetColumnWidth(i, wxLIST_AUTOSIZE);
1186             firstwidth -= list->GetColumnWidth(i);
1187          }
1188        
1189          list->SetColumnWidth(0, 18);
1190          firstwidth -= 18;
1191          list->SetColumnWidth(1, wxLIST_AUTOSIZE);
1192          if (list->GetColumnWidth(1) < firstwidth) {
1193             list->SetColumnWidth(1, firstwidth-25);
1194          }
1195       }
1196    }
1197 }
1198
1199 /* Mark a treeitem (directory) or a listitem (file or directory) */
1200 void wxbRestorePanel::CmdMark(wxTreeItemId treeitem, long* listitems, int listsize, int state) {
1201    if (status == choosing) {
1202       wxbTreeItemData** itemdata;
1203       int itemdatasize = 0;
1204       if (listsize == 0) {
1205          itemdata = new wxbTreeItemData*[1];
1206          itemdatasize = 1;
1207       }
1208       else {
1209          itemdata = new wxbTreeItemData*[listsize];
1210          itemdatasize = listsize;
1211       }
1212       
1213       if (listitems != NULL) {
1214          for (int i = 0; i < listsize; i++) {
1215             itemdata[i] = (wxbTreeItemData*)list->GetItemData(listitems[i]);
1216          }
1217       }
1218       else if (treeitem.IsOk()) {
1219          itemdata[0] = (wxbTreeItemData*)tree->GetItemData(treeitem);
1220       }
1221       else {
1222          delete[] itemdata;
1223          return;
1224       }
1225
1226       if (itemdata[0] == NULL) { //Should never happen
1227          delete[] itemdata;
1228          return;
1229       }
1230
1231       wxString dir = itemdata[0]->GetPath();
1232       wxString file;
1233
1234       if (dir != "/") {
1235          if (dir.GetChar(dir.Length()-1) == '/') {
1236             dir.RemoveLast();
1237          }
1238
1239          int i = dir.Find('/', TRUE);
1240          if (i == -1) {
1241             file = dir;
1242             dir = "/";
1243          }
1244          else { /* first dir below root */
1245             file = dir.Mid(i+1);
1246             dir = dir.Mid(0, i+1);
1247          }
1248       }
1249       else {
1250          dir = "/";
1251          file = "*";
1252       }
1253
1254       if (state == -1) {
1255          bool marked = false;
1256          bool unmarked = false;
1257          state = 0;
1258          for (int i = 0; i < itemdatasize; i++) {
1259             switch(itemdata[i]->GetMarked()) {
1260             case 0:
1261                unmarked = true;
1262                break;
1263             case 1:
1264                marked = true;
1265                break;
1266             case 2:
1267                marked = true;
1268                unmarked = true;
1269                break;
1270             default:
1271                break;
1272             }
1273             if (marked && unmarked)
1274                break;
1275          }
1276          if (marked) {
1277             if (unmarked) {
1278                state = 1;
1279             }
1280             else {
1281                state = 0;
1282             }
1283          }
1284          else {
1285             state = 1;
1286          }
1287       }
1288
1289       wxbUtils::WaitForEnd(wxString("cd \"") << dir << "\"\n");
1290       wxbUtils::WaitForEnd(wxString((state==1) ? "mark" : "unmark") << " \"" << file << "\"\n");
1291
1292       /* TODO: Check commands results */
1293
1294       /*if ((dir == "/") && (file == "*")) {
1295             itemdata->SetMarked((itemdata->GetMarked() == 1) ? 0 : 1);
1296       }*/
1297
1298       if (listitems == NULL) { /* tree item state changed */
1299          SetTreeItemState(treeitem, state);
1300          /*treeitem = tree->GetSelection();
1301          UpdateTree(treeitem, true);
1302          treeitem = tree->GetItemParent(treeitem);*/
1303       }
1304       else {
1305          for (int i = 0; i < listsize; i++) {
1306             SetListItemState(listitems[i], state);
1307          }
1308          /*UpdateTree(treeitem, (tree->GetSelection() == treeitem));
1309          treeitem = tree->GetItemParent(treeitem);*/
1310       }
1311
1312       /*while (treeitem.IsOk()) {
1313          WaitForList(treeitem, false);
1314          treeitem = tree->GetItemParent(treeitem);
1315       }*/
1316       
1317       delete[] itemdata;
1318    }
1319 }
1320
1321 /*----------------------------------------------------------------------------
1322    General functions
1323   ----------------------------------------------------------------------------*/
1324
1325 /* Run a dir command, and waits until result is fully received. */
1326 void wxbRestorePanel::UpdateTreeItem(wxTreeItemId item, bool updatelist, bool recurse) {
1327 //   this->updatelist = updatelist;
1328    wxbDataTokenizer* dt;
1329
1330    dt = wxbUtils::WaitForEnd(wxString("cd \"") << 
1331       static_cast<wxbTreeItemData*>(tree->GetItemData(item))
1332          ->GetPath() << "\"\n", false);
1333
1334    /* TODO: check command result */
1335    
1336    //delete dt;
1337
1338    status = listing;
1339
1340    if (updatelist)
1341       list->DeleteAllItems();
1342    dt = wxbUtils::WaitForEnd("dir\n", true);
1343    
1344    wxString str;
1345    
1346    for (unsigned int i = 0; i < dt->GetCount(); i++) {
1347       str = (*dt)[i];
1348       
1349       if (str.Find("cwd is:") == 0) { // Sometimes cd command result "infiltrate" into listings.
1350          break;
1351       }
1352
1353       str.RemoveLast();
1354
1355       wxString* file = ParseList(str);
1356       
1357       if (file == NULL)
1358             break;
1359
1360       wxTreeItemId treeid;
1361
1362       if (file[8].GetChar(file[8].Length()-1) == '/') {
1363          wxString itemStr;
1364
1365          long cookie;
1366          treeid = tree->GetFirstChild(item, cookie);
1367
1368          bool updated = false;
1369
1370          while (treeid.IsOk()) {
1371             itemStr = tree->GetItemText(treeid);
1372             if (file[8] == itemStr) {
1373                int stat = wxbTreeItemData::GetMarkedStatus(file[6]);
1374                if (static_cast<wxbTreeItemData*>(tree->GetItemData(treeid))->GetMarked() != stat) {
1375                   tree->SetItemImage(treeid, stat, wxTreeItemIcon_Normal);
1376                   tree->SetItemImage(treeid, stat, wxTreeItemIcon_Selected);
1377                   static_cast<wxbTreeItemData*>(tree->GetItemData(treeid))->SetMarked(file[6]);
1378                }
1379                if ((recurse) && (tree->IsExpanded(treeid))) {
1380                   UpdateTreeItem(treeid, false, true);
1381                }
1382                updated = true;
1383                break;
1384             }
1385             treeid = tree->GetNextChild(item, cookie);
1386          }
1387
1388          if (!updated) {
1389             int img = wxbTreeItemData::GetMarkedStatus(file[6]);
1390             treeid = tree->AppendItem(item, file[8], img, img, new wxbTreeItemData(file[7], file[8], file[6]));
1391          }
1392       }
1393
1394       if (updatelist) {
1395          long ind = list->InsertItem(list->GetItemCount(), wxbTreeItemData::GetMarkedStatus(file[6]));
1396          wxbTreeItemData* data = new wxbTreeItemData(file[7], file[8], file[6], ind);
1397          data->SetId(treeid);
1398          list->SetItemData(ind, (long)data);
1399          list->SetItem(ind, 1, file[8]); // filename
1400          list->SetItem(ind, 2, file[4]); //Size
1401          list->SetItem(ind, 3, file[5]); //date
1402          list->SetItem(ind, 4, file[0]); //perm
1403          list->SetItem(ind, 5, file[2]); //user
1404          list->SetItem(ind, 6, file[3]); //grp
1405       }
1406
1407       delete[] file;
1408    }
1409    
1410    delete dt;
1411    
1412    tree->Refresh();
1413    status = choosing;
1414 }
1415
1416 /* Parse dir command results. */
1417 wxString* wxbRestorePanel::ParseList(wxString line) {
1418    /* See ls_output in dird/ua_tree.c */
1419   
1420    //drwxrwxrwx   1 root     root           0  2004-04-03 14:35:21  f:/tocd/NVSU 1.00.00/
1421    //+ 10     +  ++ +   8  + +   8  ++   8  +  +      19         + *+ ->
1422    //0           12 15       24      32        42                  62
1423
1424    if (line.Length() < 63)
1425       return NULL;
1426
1427    wxString* ret = new wxString[9];
1428
1429    ret[0] = line.Mid(0, 10).Trim();
1430    ret[1] = line.Mid(12, 2).Trim();
1431    ret[2] = line.Mid(15, 8).Trim();
1432    ret[3] = line.Mid(24, 8).Trim();
1433    ret[4] = line.Mid(32, 8).Trim();
1434    ret[5] = line.Mid(42, 19).Trim();
1435    ret[6] = line.Mid(62, 1);
1436    ret[7] = line.Mid(63).Trim();
1437
1438    if (ret[6] == " ") ret[6] = "";
1439
1440    if (ret[7].GetChar(ret[7].Length()-1) == '/') {
1441       ret[8] = ret[7];
1442       ret[8].RemoveLast();
1443       ret[8] = ret[7].Mid(ret[8].Find('/', true)+1);
1444    }
1445    else {
1446       ret[8] = ret[7].Mid(ret[7].Find('/', true)+1);
1447    }
1448
1449    return ret;
1450 }
1451
1452 /* Sets a list item state, and update its parents and children if it is a directory */
1453 void wxbRestorePanel::SetListItemState(long listitem, int newstate) {
1454    wxbTreeItemData* itemdata = (wxbTreeItemData*)list->GetItemData(listitem);
1455    
1456    wxTreeItemId treeitem;
1457    
1458    itemdata->SetMarked(newstate);
1459    list->SetItemImage(listitem, newstate, 0); /* TODO: Find what these ints are for */
1460    list->SetItemImage(listitem, newstate, 1);
1461       
1462    if ((treeitem = itemdata->GetId()).IsOk()) {
1463       SetTreeItemState(treeitem, newstate);
1464    }
1465    else {
1466       UpdateTreeItemState(tree->GetSelection());
1467    }
1468 }
1469
1470 /* Sets a tree item state, and update its children, parents and list (if necessary) */
1471 void wxbRestorePanel::SetTreeItemState(wxTreeItemId item, int newstate) {
1472    long cookie;
1473    wxTreeItemId currentChild = tree->GetFirstChild(item, cookie);
1474
1475    wxbTreeItemData* itemdata;
1476
1477    while (currentChild.IsOk()) {
1478       itemdata = (wxbTreeItemData*)tree->GetItemData(currentChild);
1479       int state = itemdata->GetMarked();
1480       
1481       if (state != newstate) {
1482          itemdata->SetMarked(newstate);
1483          tree->SetItemImage(currentChild, newstate, wxTreeItemIcon_Normal);
1484          tree->SetItemImage(currentChild, newstate, wxTreeItemIcon_Selected);
1485       }
1486       
1487       currentChild = tree->GetNextChild(item, cookie);
1488    }
1489      
1490    itemdata = (wxbTreeItemData*)tree->GetItemData(item);  
1491    itemdata->SetMarked(newstate);
1492    tree->SetItemImage(item, newstate, wxTreeItemIcon_Normal);
1493    tree->SetItemImage(item, newstate, wxTreeItemIcon_Selected);
1494    tree->Refresh();
1495    
1496    if (tree->GetSelection() == item) {
1497       for (long i = 0; i < list->GetItemCount(); i++) {
1498          list->SetItemImage(i, newstate, 0); /* TODO: Find what these ints are for */
1499          list->SetItemImage(i, newstate, 1);
1500       }
1501    }
1502    
1503    UpdateTreeItemState(tree->GetItemParent(item));
1504 }
1505
1506 /* Update a tree item state, and its parents' state */
1507 void wxbRestorePanel::UpdateTreeItemState(wxTreeItemId item) {  
1508    if (!item.IsOk()) {
1509       return;
1510    }
1511    
1512    int state = 0;
1513        
1514    long cookie;
1515    wxTreeItemId currentChild = tree->GetFirstChild(item, cookie);
1516
1517    bool onechildmarked = false;
1518    bool onechildunmarked = false;
1519
1520    while (currentChild.IsOk()) {
1521       state = ((wxbTreeItemData*)tree->GetItemData(currentChild))->GetMarked();
1522       switch (state) {
1523       case 0:
1524          onechildunmarked = true;
1525          break;
1526       case 1:
1527          onechildmarked = true;
1528          break;
1529       case 2:
1530          onechildmarked = true;
1531          onechildunmarked = true;
1532          break;
1533       }
1534       
1535       if (onechildmarked && onechildunmarked) {
1536          break;
1537       }
1538       
1539       currentChild = tree->GetNextChild(item, cookie);
1540    }
1541    
1542    if (tree->GetSelection() == item) {
1543       for (long i = 0; i < list->GetItemCount(); i++) {
1544          state = ((wxbTreeItemData*)list->GetItemData(i))->GetMarked();
1545          
1546          switch (state) {
1547          case 0:
1548             onechildunmarked = true;
1549             break;
1550          case 1:
1551             onechildmarked = true;
1552             break;
1553          case 2:
1554             onechildmarked = true;
1555             onechildunmarked = true;
1556             break;
1557          }
1558          
1559          if (onechildmarked && onechildunmarked) {
1560             break;
1561          }
1562       }
1563    }
1564    
1565    state = 0;
1566    
1567    if (onechildmarked && onechildunmarked) {
1568       state = 2;
1569    }
1570    else if (onechildmarked) {
1571       state = 1;
1572    }
1573    else if (onechildunmarked) {
1574       state = 0;
1575    }
1576    else { // no child, don't change anything
1577       UpdateTreeItemState(tree->GetItemParent(item));
1578       return;
1579    }
1580    
1581    wxbTreeItemData* itemdata = (wxbTreeItemData*)tree->GetItemData(item);
1582       
1583    itemdata->SetMarked(state);
1584    tree->SetItemImage(item, state, wxTreeItemIcon_Normal);
1585    tree->SetItemImage(item, state, wxTreeItemIcon_Selected);
1586    
1587    UpdateTreeItemState(tree->GetItemParent(item));
1588 }
1589
1590 /* Refresh the whole tree. */
1591 void wxbRestorePanel::RefreshTree() {
1592    /* Save current selection */
1593    wxArrayString current;
1594    
1595    wxTreeItemId item = currentTreeItem;
1596    
1597    while ((item.IsOk()) && (item != tree->GetRootItem())) {
1598       current.Add(tree->GetItemText(item));
1599       item = tree->GetItemParent(item);
1600    }
1601
1602    /* Update the tree */
1603    UpdateTreeItem(tree->GetRootItem(), false, true);
1604
1605    /* Reselect the former selected item */   
1606    item = tree->GetRootItem();
1607    
1608    if (current.Count() == 0) {
1609       tree->SelectItem(item);
1610       return;
1611    }
1612    
1613    bool match;
1614    
1615    for (int i = current.Count()-1; i >= 0; i--) {
1616       long cookie;
1617       wxTreeItemId currentChild = tree->GetFirstChild(item, cookie);
1618       
1619       match = false;
1620       
1621       while (currentChild.IsOk()) {
1622          if (tree->GetItemText(currentChild) == current[i]) {
1623             item = currentChild;
1624             match = true;
1625             break;
1626          }
1627    
1628          currentChild = tree->GetNextChild(item, cookie);
1629       }
1630       
1631       if (!match) break;
1632    }
1633    
1634    UpdateTreeItem(item, true, false); /* Update the list */
1635    
1636    tree->SelectItem(item);
1637 }
1638
1639 void wxbRestorePanel::RefreshList() {
1640    if (currentTreeItem.IsOk()) {
1641       UpdateTreeItem(currentTreeItem, true, false); /* Update the list */
1642    }
1643 }
1644
1645 /* Update first config, adapting settings to the job name selected */
1646 void wxbRestorePanel::UpdateFirstConfig() {
1647    configPanel->Enable(false);
1648    wxbDataTokenizer* dt = wxbUtils::WaitForEnd(wxString(".defaults job=") + configPanel->GetRowString("Job Name") + "\n", true, false);
1649    /* job=RestoreFiles
1650     * pool=Default
1651     * messages=Standard
1652     * client=***
1653     * storage=File
1654     * where=/tmp/bacula-restores
1655     * level=0
1656     * type=Restore
1657     * fileset=Full Set */
1658    
1659    wxString name, str;
1660    unsigned int i;
1661    int j;
1662    wxString client;
1663    bool dolistjobs = false;
1664    
1665    for (i = 0; i < dt->GetCount(); i++) {
1666       str = (*dt)[i];
1667       if ((j = str.Find('=')) > -1) {
1668          name = str.Mid(0, j);
1669          if (name == "pool") {
1670             configPanel->SetRowString("Pool", str.Mid(j+1));
1671          }
1672          else if (name == "client") {
1673             str = str.Mid(j+1);
1674             if ((str != configPanel->GetRowString("Client")) || (configPanel->GetRowString("Before") == "")) {
1675                configPanel->SetRowString("Client", str);
1676                dolistjobs = true;
1677             }
1678          }
1679          else if (name == "storage") {
1680             configPanel->SetRowString("Storage", str.Mid(j+1));
1681          }
1682          else if (name == "fileset") {
1683             configPanel->SetRowString("Fileset", str.Mid(j+1));
1684          }
1685       }
1686    }
1687       
1688    delete dt;
1689    
1690    if (dolistjobs) {
1691       //wxTheApp->Yield(false);
1692       CmdListJobs();
1693    }
1694    configPanel->Enable(true);
1695 }
1696
1697 /* 
1698  * Update second config.
1699  * 
1700  * Run Restore job
1701  * JobName:    RestoreFiles
1702  * Bootstrap:  /var/lib/bacula/restore.bsr
1703  * Where:      /tmp/bacula-restores
1704  * Replace:    always
1705  * FileSet:    Full Set
1706  * Client:     tom-fd
1707  * Storage:    File
1708  * When:       2004-04-18 01:18:56
1709  * Priority:   10
1710  * OK to run? (yes/mod/no):
1711  * 
1712  */
1713 bool wxbRestorePanel::UpdateSecondConfig(wxbDataTokenizer* dt) {
1714    unsigned int i;
1715    for (i = 0; i < dt->GetCount(); i++) {
1716       if ((*dt)[i].Find("Run Restore job") == 0)
1717          break;
1718    }
1719    
1720    if ((i + 10) > dt->GetCount()) {
1721       return false;
1722    }
1723    
1724    int k;
1725    
1726    if ((k = (*dt)[++i].Find("JobName:")) != 0) return false;
1727    restorePanel->SetRowString("Job Name", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1728    if ((k = (*dt)[++i].Find("Bootstrap:")) != 0) return false;
1729    restorePanel->SetRowString("Bootstrap", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1730    if ((k = (*dt)[++i].Find("Where:")) != 0) return false;
1731    restorePanel->SetRowString("Where", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1732    
1733    if ((k = (*dt)[++i].Find("Replace:")) != 0) return false;
1734    wxString str = (*dt)[i].Mid(10).Trim(false).RemoveLast();
1735    if (str == "always") restorePanel->SetRowSelection("Replace", 0);
1736    else if (str == "ifnewer") restorePanel->SetRowSelection("Replace", 1);
1737    else if (str == "ifolder") restorePanel->SetRowSelection("Replace", 2);
1738    else if (str == "never") restorePanel->SetRowSelection("Replace", 3);
1739    else restorePanel->SetRowSelection("Replace", 0);
1740
1741    if ((k = (*dt)[++i].Find("FileSet:")) != 0) return false;
1742    restorePanel->SetRowString("Fileset", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1743    if ((k = (*dt)[++i].Find("Client:")) != 0) return false;
1744    restorePanel->SetRowString("Client", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1745    if ((k = (*dt)[++i].Find("Storage:")) != 0) return false;
1746    restorePanel->SetRowString("Storage", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1747    if ((k = (*dt)[++i].Find("When:")) != 0) return false;
1748    restorePanel->SetRowString("When", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1749    if ((k = (*dt)[++i].Find("Priority:")) != 0) return false;
1750    restorePanel->SetRowString("Priority", (*dt)[i].Mid(10).Trim(false).RemoveLast());
1751    cfgUpdated = 0;
1752    
1753    restorePanel->Layout();
1754    
1755    return true;
1756 }
1757
1758 /*----------------------------------------------------------------------------
1759    Status function
1760   ----------------------------------------------------------------------------*/
1761
1762 /* Set current status by enabling/disabling components */
1763 void wxbRestorePanel::SetStatus(status_enum newstatus) {
1764    switch (newstatus) {
1765    case disabled:
1766       centerSizer->Remove(configPanel);
1767       centerSizer->Remove(restorePanel);
1768       centerSizer->Remove(treelistPanel);
1769       treelistPanel->Show(false);
1770       restorePanel->Show(false);
1771       centerSizer->Add(configPanel, 1, wxEXPAND);
1772       configPanel->Show(true);
1773       configPanel->Layout();
1774       centerSizer->Layout();
1775       this->Layout();
1776       start->SetLabel("Enter restore mode");
1777       start->Enable(false);
1778       configPanel->Enable(false);
1779       tree->Enable(false);
1780       list->Enable(false);
1781       gauge->Enable(false);
1782       cancel->Enable(false);
1783       cfgUpdated = 0;
1784       cancelled = 0;
1785       break;
1786    case finished:
1787       centerSizer->Remove(configPanel);
1788       centerSizer->Remove(restorePanel);
1789       centerSizer->Remove(treelistPanel);
1790       treelistPanel->Show(false);
1791       restorePanel->Show(false);
1792       centerSizer->Add(configPanel, 1, wxEXPAND);
1793       configPanel->Show(true);
1794       configPanel->Layout();
1795       centerSizer->Layout();
1796       this->Layout();
1797       tree->DeleteAllItems();
1798       list->DeleteAllItems();
1799       configPanel->ClearRowChoices("Client");
1800       configPanel->ClearRowChoices("Before");
1801       wxbMainFrame::GetInstance()->EnablePanels();
1802       newstatus = activable;
1803    case activable:
1804       cancelled = 0;
1805       start->SetLabel("Enter restore mode");
1806       start->Enable(true);
1807       configPanel->Enable(false);
1808       tree->Enable(false);
1809       list->Enable(false);
1810       gauge->Enable(false);
1811       cancel->Enable(false);
1812       cfgUpdated = 0;
1813       break;
1814    case entered:
1815       wxbMainFrame::GetInstance()->DisablePanels(this);
1816       gauge->SetValue(0);
1817       start->Enable(false);
1818       //start->SetLabel("Choose files to restore");
1819       configPanel->Enable(true);
1820       tree->Enable(false);
1821       list->Enable(false);
1822       cancel->Enable(true);
1823       cfgUpdated = 0;
1824       break;
1825    case listing:
1826       
1827       break;
1828    case choosing:
1829       start->Enable(true);
1830       start->SetLabel("Restore");
1831       centerSizer->Remove(configPanel);
1832       configPanel->Show(false);
1833       centerSizer->Add(treelistPanel, 1, wxEXPAND);
1834       treelistPanel->Show(true);
1835       treelistPanel->Layout();
1836       centerSizer->Layout();
1837       this->Layout();
1838       tree->Enable(true);
1839       list->Enable(true);
1840       SetWorking(false);
1841       break;
1842    case configuring:
1843       start->Enable(false);
1844       configPanel->Enable(false);
1845       tree->Enable(false);
1846       list->Enable(false);
1847       centerSizer->Remove(treelistPanel);
1848       treelistPanel->Show(false);
1849       centerSizer->Add(restorePanel, 1, wxEXPAND);
1850       restorePanel->Show(true);
1851       restorePanel->Layout();
1852       centerSizer->Layout();
1853       this->Layout();
1854       restorePanel->EnableApply(false);
1855       break;
1856    case restoring:
1857       start->SetLabel("Restoring...");
1858       gauge->Enable(true);
1859       gauge->SetValue(0);
1860       start->Enable(false);
1861       configPanel->Enable(false);
1862       tree->Enable(false);
1863       list->Enable(false);
1864       SetWorking(true);
1865       break;
1866    }
1867    status = newstatus;
1868 }
1869
1870 /*----------------------------------------------------------------------------
1871    UI related
1872   ----------------------------------------------------------------------------*/
1873
1874 void wxbRestorePanel::SetWorking(bool working) {
1875    this->working = working;
1876    if (working) {
1877       SetCursor(*wxHOURGLASS_CURSOR);
1878 //      SetEvtHandlerEnabled(false); //EVTQUEUE
1879    }
1880 //   else if (!processing) { /* Empty event queue if we aren't already doing this */ //EVTQUEUE
1881    else {
1882 //      processing = true; //EVTQUEUE
1883       SetCursor(*wxSTANDARD_CURSOR);
1884 //      SetEvtHandlerEnabled(true); //EVTQUEUE
1885 /*      wxNode *node = pendingEvents->First(); //EVTQUEUE
1886       while ( node ) {
1887          wxEvent *event = (wxEvent *)node->Data();
1888          delete node;
1889    
1890          wxEvtHandler::ProcessEvent(*event);
1891          delete event;
1892    
1893          node = pendingEvents->First();
1894       }
1895       processing = false;*/
1896    }
1897 }
1898
1899 bool wxbRestorePanel::IsWorking() {
1900    return this->working;
1901 }
1902
1903 void wxbRestorePanel::EnableConfig(bool enable) {
1904    restorePanel->Enable(enable);
1905 }
1906
1907 /*----------------------------------------------------------------------------
1908    Event handling
1909   ----------------------------------------------------------------------------*/
1910
1911
1912 //EVTQUEUE
1913 /*
1914 bool wxbRestorePanel::ProcessEvent(wxEvent& event) {
1915    if (IsWorking() || processing) {
1916       wxEvent *eventCopy = event.Clone();
1917       
1918       pendingEvents->Append(eventCopy);
1919       return TRUE;
1920    }
1921    else {
1922       return wxEvtHandler::ProcessEvent(event);
1923    }
1924 }
1925 */
1926
1927 void wxbRestorePanel::OnCancel(wxCommandEvent& event) {
1928    cancel->Enable(false);
1929    SetCursor(*wxHOURGLASS_CURSOR);
1930    CmdCancel();
1931    SetCursor(*wxSTANDARD_CURSOR);
1932 }
1933
1934 void wxbRestorePanel::OnStart(wxCommandEvent& event) {
1935    if (IsWorking()) {
1936       AddPendingEvent(event);
1937       return;
1938    }
1939    SetWorking(true);
1940    CmdStart();
1941    SetWorking(false);
1942 }
1943
1944 void wxbRestorePanel::OnTreeChanging(wxTreeEvent& event) {
1945    if (IsWorking()) {
1946       AddPendingEvent(event);
1947       event.Veto();
1948    }
1949 }
1950
1951 void wxbRestorePanel::OnTreeExpanding(wxTreeEvent& event) {
1952    if (IsWorking()) {
1953       AddPendingEvent(event);
1954       event.Veto();
1955       return;
1956    }
1957    //working = true;
1958    //CmdList(event.GetItem());
1959    if (tree->GetSelection() != event.GetItem()) {
1960       tree->SelectItem(event.GetItem());
1961    }
1962    //working = false;
1963 }
1964
1965 void wxbRestorePanel::OnTreeChanged(wxTreeEvent& event) {
1966    if (IsWorking()) {
1967       AddPendingEvent(event);
1968       return;
1969    }
1970    if (currentTreeItem == event.GetItem()) {
1971       return;
1972    }
1973    treeadd->Enable(false);
1974    treeremove->Enable(false);
1975    treerefresh->Enable(false);
1976    markWhenListingDone = false;
1977    SetWorking(true);
1978    currentTreeItem = event.GetItem();
1979    CmdList(event.GetItem());
1980    if (markWhenListingDone) {
1981       CmdMark(event.GetItem(), NULL, 0);
1982       tree->Refresh();
1983    }
1984    SetWorking(false);
1985    if (event.GetItem().IsOk()) {
1986       int status = ((wxbTreeItemData*)tree->GetItemData(event.GetItem()))->GetMarked();
1987       treeadd->Enable(status != 1);
1988       treeremove->Enable(status != 0);
1989    }
1990    treerefresh->Enable(true);
1991 }
1992
1993 void wxbRestorePanel::OnTreeMarked(wxbTreeMarkedEvent& event) {
1994    csprint("Tree marked", CS_DEBUG);
1995    if (IsWorking()) {
1996       if (tree->GetSelection() == event.GetItem()) {
1997          markWhenListingDone = !markWhenListingDone;
1998       }
1999       AddPendingEvent(event);
2000       return;
2001    }
2002    SetWorking(true);
2003    CmdMark(event.GetItem(), NULL, 0);
2004    //event.Skip();
2005    tree->Refresh();
2006    SetWorking(false);
2007    if (event.GetItem().IsOk()) {
2008       int status = ((wxbTreeItemData*)tree->GetItemData(event.GetItem()))->GetMarked();
2009       treeadd->Enable(status != 1);
2010       treeremove->Enable(status != 0);
2011    }
2012 }
2013
2014 void wxbRestorePanel::OnTreeAdd(wxCommandEvent& event) {
2015    if (IsWorking()) {
2016       AddPendingEvent(event);
2017       return;
2018    }
2019    
2020    if (currentTreeItem.IsOk()) {
2021       SetWorking(true);
2022       CmdMark(currentTreeItem, NULL, 0, 1);
2023       tree->Refresh();
2024       treeadd->Enable(0);
2025       treeremove->Enable(1);
2026       SetWorking(false);
2027    }
2028 }
2029
2030 void wxbRestorePanel::OnTreeRemove(wxCommandEvent& event) {
2031    if (IsWorking()) {
2032       AddPendingEvent(event);
2033       return;
2034    }
2035    
2036    if (currentTreeItem.IsOk()) {
2037       SetWorking(true);
2038       CmdMark(currentTreeItem, NULL, 0, 0);
2039       tree->Refresh();
2040       treeadd->Enable(1);
2041       treeremove->Enable(0);
2042       SetWorking(false);
2043    }
2044 }
2045
2046 void wxbRestorePanel::OnTreeRefresh(wxCommandEvent& event) {
2047    if (IsWorking()) {
2048       AddPendingEvent(event);
2049       return;
2050    }
2051    
2052    SetWorking(true);
2053    RefreshTree();
2054    SetWorking(false);
2055 }
2056
2057 void wxbRestorePanel::OnListMarked(wxbListMarkedEvent& event) {
2058    if (IsWorking()) {
2059       AddPendingEvent(event);
2060       //event.Skip();
2061       return;
2062    }
2063    
2064    if (list->GetSelectedItemCount() == 0) {
2065       return;
2066    }
2067    
2068    SetWorking(true);  
2069    
2070    long* items = new long[list->GetSelectedItemCount()];
2071    
2072    int num = 0;
2073    
2074    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2075    while (item != -1) {
2076       items[num] = item;
2077       num++;
2078       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2079    }
2080      
2081    CmdMark(wxTreeItemId(), items, num);
2082    
2083    delete[] items;
2084    
2085    wxListEvent listevt;
2086    
2087    OnListChanged(listevt);
2088    
2089    event.Skip();
2090    tree->Refresh();
2091    SetWorking(false);
2092 }
2093
2094 void wxbRestorePanel::OnListActivated(wxListEvent& event) {
2095    if (IsWorking()) {
2096       AddPendingEvent(event);
2097       //event.Skip();
2098       return;
2099    }
2100    SetWorking(true);
2101    long item = event.GetIndex();
2102 //   long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
2103    if (item > -1) {
2104       wxbTreeItemData* itemdata = (wxbTreeItemData*)list->GetItemData(item);
2105       wxString name = itemdata->GetName();
2106       event.Skip();
2107
2108       wxString itemStr;
2109
2110       long cookie;
2111
2112       if (name.GetChar(name.Length()-1) == '/') {
2113          wxTreeItemId currentChild = tree->GetFirstChild(currentTreeItem, cookie);
2114
2115          while (currentChild.IsOk()) {
2116             wxString name2 = tree->GetItemText(currentChild);
2117             if (name2 == name) {
2118                //tree->UnselectAll();
2119                SetWorking(false);
2120                tree->Expand(currentTreeItem);
2121                tree->SelectItem(currentChild);
2122                //tree->Refresh();
2123                return;
2124             }
2125             currentChild = tree->GetNextChild(currentTreeItem, cookie);
2126          }
2127       }
2128    }
2129    SetWorking(false);
2130 }
2131
2132 void wxbRestorePanel::OnListChanged(wxListEvent& event) {
2133    if (IsWorking()) {
2134       AddPendingEvent(event);
2135       return;
2136    }
2137  
2138    listadd->Enable(false);
2139    listremove->Enable(false);
2140    
2141    bool marked = false;
2142    bool unmarked = false;
2143    
2144    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2145    while (item != -1) {
2146       switch (((wxbTreeItemData*)list->GetItemData(item))->GetMarked()) {
2147       case 0:
2148          unmarked = true;
2149          break;
2150       case 1:
2151          marked = true;
2152          break;
2153       case 2:
2154          marked = true;
2155          unmarked = true;
2156          break;
2157       default:
2158          break;
2159          // Should never happen
2160       }
2161       if (marked && unmarked) break;
2162       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2163    }
2164    
2165    listadd->Enable(unmarked);
2166    listremove->Enable(marked);
2167 }
2168
2169 void wxbRestorePanel::OnListAdd(wxCommandEvent& event) {
2170    if (IsWorking()) {
2171       AddPendingEvent(event);
2172       return;
2173    }
2174    
2175    SetWorking(true);
2176    
2177    long* items = new long[list->GetSelectedItemCount()];
2178    
2179    int num = 0;
2180    
2181    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2182    while (item != -1) {
2183       items[num] = item;
2184       num++;
2185       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2186    }
2187      
2188    CmdMark(wxTreeItemId(), items, num, 1);
2189    
2190    delete[] items;
2191    
2192    tree->Refresh();
2193    SetWorking(false);
2194    
2195    listadd->Enable(false);
2196    listremove->Enable(true);
2197 }
2198
2199 void wxbRestorePanel::OnListRemove(wxCommandEvent& event) {
2200    if (IsWorking()) {
2201       AddPendingEvent(event);
2202       return;
2203    }
2204    
2205    SetWorking(true);
2206    
2207    long* items = new long[list->GetSelectedItemCount()];
2208    
2209    int num = 0;
2210    
2211    long item = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2212    while (item != -1) {
2213       items[num] = item;
2214       num++;
2215       item = list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
2216    }
2217      
2218    CmdMark(wxTreeItemId(), items, num, 0);
2219    
2220    delete[] items;
2221    
2222    tree->Refresh();
2223    SetWorking(false);
2224    
2225    listadd->Enable(true);
2226    listremove->Enable(false);
2227 }
2228
2229 void wxbRestorePanel::OnListRefresh(wxCommandEvent& event) {
2230    if (IsWorking()) {
2231       AddPendingEvent(event);
2232       return;
2233    }
2234    
2235    SetWorking(true);
2236    RefreshList();
2237    SetWorking(false);
2238 }
2239
2240 void wxbRestorePanel::OnConfigUpdated(wxCommandEvent& event) {
2241    if (status == entered) {
2242       if (event.GetId() == ConfigJobName) {
2243          if (IsWorking()) {
2244             return;
2245          }
2246          SetWorking(true);
2247          UpdateFirstConfig();
2248          SetWorking(false);
2249       }
2250       else if (event.GetId() == ConfigClient) {
2251          if (IsWorking()) {
2252             return;
2253          }
2254          SetWorking(true);
2255          configPanel->Enable(false);
2256          CmdListJobs();
2257          configPanel->Enable(true);
2258          SetWorking(false);
2259       }
2260       cfgUpdated = cfgUpdated | (1 << event.GetId());
2261    }
2262    else if (status == configuring) {
2263       restorePanel->EnableApply(true);
2264       cfgUpdated = cfgUpdated | (1 << event.GetId());
2265    }
2266 }
2267
2268 void wxbRestorePanel::OnConfigOk(wxCommandEvent& WXUNUSED(event)) {
2269    if (status != configuring) return;
2270    if (IsWorking()) {
2271       return;
2272    }
2273    SetWorking(true);
2274    CmdStart();
2275    SetWorking(false);
2276 }
2277
2278 void wxbRestorePanel::OnConfigApply(wxCommandEvent& WXUNUSED(event)) {
2279    if (status != configuring) return;
2280    if (IsWorking()) {
2281       return;
2282    }
2283    SetWorking(true);
2284    CmdConfigApply();
2285    if (cfgUpdated == 0) {
2286       restorePanel->EnableApply(false);
2287    }
2288    SetWorking(false);  
2289 }
2290
2291 void wxbRestorePanel::OnConfigCancel(wxCommandEvent& WXUNUSED(event)) {
2292    if (status != configuring) return;
2293    if (IsWorking()) {
2294       return;
2295    }
2296    SetWorking(true);
2297    CmdConfigCancel();
2298    SetWorking(false);
2299 }
2300