]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/restore/restore.cpp
144a17667a019a18605fcf20a2ccefda97590d0a
[bacula/bacula] / bacula / src / qt-console / restore / restore.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28  
29 /*
30  *   Version $Id$
31  *
32  *  Restore Class 
33  *
34  *   Kern Sibbald, February MMVII
35  *
36  */ 
37
38 #include "bat.h"
39 #include "restore.h"
40
41 restoreDialog::restoreDialog(Console *console)
42 {
43    QStringList titles;
44    m_console = console;
45   
46    m_console->setEnabled(false);
47    setupUi(this);
48    connect(fileWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), 
49            this, SLOT(fileDoubleClicked(QTreeWidgetItem *, int)));
50    connect(upButton, SIGNAL(pressed()), this, SLOT(upButtonPushed()));
51    connect(markButton, SIGNAL(pressed()), this, SLOT(markButtonPushed()));
52    connect(unmarkButton, SIGNAL(pressed()), this, SLOT(unmarkButtonPushed()));
53    setFont(m_console->get_font());
54    m_console->displayToPrompt();
55
56
57    titles << "Mark" << "File" << "Mode" << "User" << "Group" << "Size" << "Date";
58    fileWidget->setHeaderLabels(titles);
59
60    get_cwd();
61    fillDirectory();
62    this->show();
63 }
64
65 /*
66  * Fill the fileWidget box with the contents of the current directory
67  */
68 void restoreDialog::fillDirectory()
69 {
70    char modes[20], user[20], group[20], size[20], date[30];
71    char marked[10];
72    int pnl, fnl;
73    POOLMEM *file = get_pool_memory(PM_FNAME);
74    POOLMEM *path = get_pool_memory(PM_FNAME);
75
76    fileWidget->clear();
77    m_console->write_dir("dir");
78    QList<QTreeWidgetItem *> items;
79    QStringList item;
80    while (m_console->read() > 0) {
81       char *p = m_console->msg();
82       char *l;
83       strip_trailing_junk(p);
84       if (*p == '$' || !*p) {
85          continue;
86       }
87       l = p;
88       skip_nonspaces(&p);             /* permissions */
89       *p++ = 0;
90       bstrncpy(modes, l, sizeof(modes));
91       skip_spaces(&p);
92       skip_nonspaces(&p);             /* link count */
93       *p++ = 0;
94       skip_spaces(&p);
95       l = p;
96       skip_nonspaces(&p);             /* user */
97       *p++ = 0;
98       skip_spaces(&p);
99       bstrncpy(user, l, sizeof(user));
100       l = p;
101       skip_nonspaces(&p);             /* group */
102       *p++ = 0;
103       bstrncpy(group, l, sizeof(group));
104       skip_spaces(&p);
105       l = p;
106       skip_nonspaces(&p);             /* size */
107       *p++ = 0;
108       bstrncpy(size, l, sizeof(size));
109       skip_spaces(&p);
110       l = p;
111       skip_nonspaces(&p);             /* date/time */
112       skip_spaces(&p);
113       skip_nonspaces(&p);
114       *p++ = 0;
115       bstrncpy(date, l, sizeof(date));
116       skip_spaces(&p);
117       if (*p == '*') {
118          bstrncpy(marked, "*", sizeof(marked));
119          p++;
120       } else {
121          bstrncpy(marked, " ", sizeof(marked));
122       }
123       split_path_and_filename(p, &path, &pnl, &file, &fnl);
124       item.clear();
125       item << marked << file << modes << user << group << size << date;
126       QTreeWidgetItem *ti = new QTreeWidgetItem((QTreeWidget *)0, item);
127       ti->setTextAlignment(5, Qt::AlignRight); /* right align size */
128       items.append(ti);
129    }
130    fileWidget->clear();
131    fileWidget->insertTopLevelItems(0, items);
132
133    free_pool_memory(file);
134    free_pool_memory(path);
135 }
136
137 void restoreDialog::accept()
138 {
139    this->hide();
140    m_console->write("done");
141    delete this;
142    m_console->setEnabled(true);
143    mainWin->resetFocus();
144 }
145
146
147 void restoreDialog::reject()
148 {
149    this->hide();
150    m_console->write("quit");
151    mainWin->set_status("Canceled");
152    delete this;
153    m_console->setEnabled(true);
154    mainWin->resetFocus();
155 }
156
157 void restoreDialog::fileDoubleClicked(QTreeWidgetItem *item, int column)
158 {
159    char cmd[1000];
160    if (column == 0) {                 /* mark/unmark */
161       if (item->text(0) == "*") {
162          bsnprintf(cmd, sizeof(cmd), "unmark \"%s\"", item->text(1).toUtf8().data());
163          item->setText(0, " ");
164       } else {
165          bsnprintf(cmd, sizeof(cmd), "mark \"%s\"", item->text(1).toUtf8().data());
166          item->setText(0, "*");
167       }
168       m_console->write_dir(cmd);
169       if (m_console->read() > 0) {
170          strip_trailing_junk(m_console->msg());
171          statusLine->setText(m_console->msg());
172       }
173       m_console->displayToPrompt();
174       return;
175    }    
176    /* 
177     * Double clicking other than column 0 means to decend into
178     *  the directory -- or nothing if it is not a directory.
179     */
180    if (item->text(1).endsWith("/")) {
181       cwd(item->text(1).toUtf8().data());
182       fillDirectory();
183    }
184 }
185
186 void restoreDialog::upButtonPushed()
187 {
188    cwd("..");
189    fillDirectory();
190 }
191
192 void restoreDialog::markButtonPushed()
193 {
194    QList<QTreeWidgetItem *> items = fileWidget->selectedItems();
195    QTreeWidgetItem *item;
196    char cmd[1000];
197    foreach (item, items) {
198       bsnprintf(cmd, sizeof(cmd), "mark \"%s\"", item->text(1).toUtf8().data());
199       item->setText(0, "*");
200       m_console->write_dir(cmd);
201       if (m_console->read() > 0) {
202          strip_trailing_junk(m_console->msg());
203          statusLine->setText(m_console->msg());
204       }
205       Dmsg1(100, "cmd=%s\n", cmd);
206       m_console->discardToPrompt();
207    }
208 }
209
210 void restoreDialog::unmarkButtonPushed()
211 {
212    QList<QTreeWidgetItem *> items = fileWidget->selectedItems();
213    QTreeWidgetItem *item;
214    char cmd[1000];
215    foreach (item, items) {
216       bsnprintf(cmd, sizeof(cmd), "unmark \"%s\"", item->text(1).toUtf8().data());
217       item->setText(0, " ");
218       m_console->write_dir(cmd);
219       if (m_console->read() > 0) {
220          strip_trailing_junk(m_console->msg());
221          statusLine->setText(m_console->msg());
222       }
223       Dmsg1(100, "cmd=%s\n", cmd);
224       m_console->discardToPrompt();
225    }
226 }
227
228 /*
229  * Change current working directory 
230  */
231 bool restoreDialog::cwd(const char *dir)
232 {
233    int stat;
234    char cd_cmd[MAXSTRING];
235
236    bsnprintf(cd_cmd, sizeof(cd_cmd), "cd \"%s\"", dir);
237    Dmsg2(100, "dir=%s cmd=%s\n", dir, cd_cmd);
238    m_console->write_dir(cd_cmd);
239    if ((stat = m_console->read()) > 0) {
240       m_cwd = m_console->msg();
241       Dmsg2(100, "cwd=%s msg=%s\n", m_cwd.toUtf8().data(), m_console->msg());
242    } else {
243       Dmsg1(000, "stat=%d\n", stat);
244    }
245    m_console->discardToPrompt();
246    lineEdit->clear();
247    lineEdit->insert(m_cwd);
248    return true;  /* ***FIXME*** return real status */
249 }
250
251 /*
252  * Return cwd when in tree restore mode 
253  */
254 char *restoreDialog::get_cwd()
255 {
256    int stat;
257    m_console->write_dir(".pwd");
258    Dmsg0(100, "send: .pwd\n");
259    if ((stat = m_console->read()) > 0) {
260       m_cwd = m_console->msg();
261       Dmsg2(100, "cwd=%s msg=%s\n", m_cwd.toUtf8().data(), m_console->msg());
262    } else {
263       Dmsg1(000, "stat=%d\n", stat);
264    }
265    m_console->displayToPrompt(); 
266    return m_cwd.toUtf8().data();
267 }