]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/restore/prerestore.cpp
Prevent connecting with the Console::m_at_main_prompt member.
[bacula/bacula] / bacula / src / qt-console / restore / prerestore.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: restore.cpp 4307 2007-03-04 10:24:39Z kerns $
31  *
32  *  preRestore -> dialog put up to determine the restore type
33  *
34  *   Kern Sibbald, February MMVII
35  *
36  */ 
37
38 #include "bat.h"
39 #include "restore.h"
40
41 /* Constructor to have job id list default in */
42 prerestorePage::prerestorePage(QString &data, unsigned int datatype)
43 {
44    m_dataIn = data;
45    m_dataInType = datatype;
46    buildPage();
47 }
48
49 /* Basic Constructor */
50 prerestorePage::prerestorePage()
51 {
52    m_dataIn = "";
53    m_dataInType = R_NONE;
54    buildPage();
55 }
56
57 /*
58  * This is really the constructor
59  */
60 void prerestorePage::buildPage()
61 {
62    m_dtformat = "yyyy-MM-dd HH:mm:ss";
63    m_name = "Restore";
64    setupUi(this);
65    pgInitialize();
66    m_console->notify(false);
67    m_closeable = true;
68
69    if (!m_console->preventInUseConnect())
70        return;
71
72    jobCombo->addItems(m_console->job_list);
73    filesetCombo->addItems(m_console->fileset_list);
74    clientCombo->addItems(m_console->client_list);
75    poolCombo->addItem("Any");
76    poolCombo->addItems(m_console->pool_list);
77    storageCombo->addItems(m_console->storage_list);
78    /* current or before . .  Start out with current checked */
79    recentCheckBox->setCheckState(Qt::Checked);
80    beforeDateTime->setDisplayFormat(m_dtformat);
81    beforeDateTime->setDateTime(QDateTime::currentDateTime());
82    beforeDateTime->setEnabled(false);
83    selectFilesRadio->setChecked(true);
84    if (m_dataInType == R_NONE) {
85       selectJobRadio->setChecked(true);
86       selectJobIdsRadio->setChecked(false);
87       jobIdEdit->setText("Comma separted list of jobs id's");
88       jobIdEdit->setEnabled(false);
89    } else if (m_dataInType == R_JOBIDLIST) {
90       selectJobIdsRadio->setChecked(true);
91       selectJobRadio->setChecked(false);
92       jobIdEdit->setText(m_dataIn);
93       jobRadioClicked(false);
94       QStringList fieldlist;
95       jobdefsFromJob(fieldlist,m_dataIn);
96       filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
97       clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
98       jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
99    } else if (m_dataInType == R_JOBDATETIME) {
100       selectJobRadio->setChecked(true);
101       selectJobIdsRadio->setChecked(false);
102       jobIdEdit->setText("Comma separted list of jobs id's");
103       jobIdEdit->setEnabled(false);
104       recentCheckBox->setCheckState(Qt::Unchecked);
105       jobRadioClicked(true);
106       QStringList fieldlist;
107       jobdefsFromJob(fieldlist,m_dataIn);
108       filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
109       clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
110       jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
111       beforeDateTime->setDateTime(QDateTime::fromString(fieldlist[3], m_dtformat));
112    }
113    job_name_change(0);
114    connect(jobCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(job_name_change(int)));
115    connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
116    connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
117    connect(recentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(recentChanged(int)));
118    connect(selectJobRadio, SIGNAL(clicked(bool)), this, SLOT(jobRadioClicked(bool)));
119    connect(selectJobIdsRadio, SIGNAL(clicked(bool)), this, SLOT(jobidsRadioClicked(bool)));
120    connect(jobIdEdit, SIGNAL(editingFinished()), this, SLOT(jobIdEditFinished()));
121
122    dockPage();
123    setCurrent();
124    this->show();
125 }
126
127
128 /*
129  * Check to make sure all is ok then start either the select window or the restore
130  * run window
131  */
132 void prerestorePage::okButtonPushed()
133 {
134    if (!selectJobRadio->isChecked()) {
135       if (!checkJobIdList())
136          return;
137    }
138    QString cmd;
139
140    this->hide();
141
142    cmd = QString("restore");
143    cmd += " fileset=\"" + filesetCombo->currentText() + "\"";
144    cmd += " client=\"" + clientCombo->currentText() + "\"";
145    if (selectJobRadio->isChecked()) {
146       if (poolCombo->currentText() != "Any" ){
147          cmd += " pool=\"" + poolCombo->currentText() + "\"";
148       }
149       cmd += " storage=\"" + storageCombo->currentText() + "\"";
150       if (recentCheckBox->checkState() == Qt::Checked) {
151          cmd += " current";
152       } else {
153          QDateTime stamp = beforeDateTime->dateTime();
154          QString before = stamp.toString(m_dtformat);
155          cmd += " before=\"" + before + "\"";
156       }
157    } else {
158       cmd += " jobid=\"" + jobIdEdit->text() + "\"";
159    }
160    if (selectFilesRadio->isChecked()) {
161       if (!selectJobIdsRadio->isChecked())
162          cmd += " select";
163    } else {
164       cmd += " all done";
165    }
166
167    /* ***FIXME*** */
168    printf("preRestore command \'%s\'\n", cmd.toUtf8().data());
169    consoleCommand(cmd);
170    /* Note, do not turn notifier back on here ... */
171    if (selectFilesRadio->isChecked()) {
172       setConsoleCurrent();
173       new restorePage();
174       closeStackPage();
175    } else {
176       m_console->notify(true);
177       closeStackPage();
178       mainWin->resetFocus();
179    }
180 }
181
182
183 /*
184  * Destroy the instace of the class
185  */
186 void prerestorePage::cancelButtonPushed()
187 {
188    mainWin->set_status("Canceled");
189    this->hide();
190    m_console->notify(true);
191    closeStackPage();
192 }
193
194
195 /*
196  * Handle updating the other widget with job defaults when the job combo is changed.
197  */
198 void prerestorePage::job_name_change(int index)
199 {
200    job_defaults job_defs;
201
202    (void)index;
203    job_defs.job_name = jobCombo->currentText();
204    if (m_console->get_job_defaults(job_defs)) {
205       filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
206       clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
207       poolCombo->setCurrentIndex(poolCombo->findText(job_defs.pool_name, Qt::MatchExactly));
208       storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
209    }
210 }
211
212 /*
213  * Handle the change of enabled of input widgets when the recent checkbox state
214  * is changed.
215  */
216 void prerestorePage::recentChanged(int state)
217 {
218    if ((state == Qt::Unchecked) && (selectJobRadio->isChecked())) {
219       beforeDateTime->setEnabled(true);
220    } else {
221       beforeDateTime->setEnabled(false);
222    }
223 }
224
225
226 /*
227  * For when jobs list is to be used, return a list which is the needed items from
228  * the job record
229  */
230 void prerestorePage::jobdefsFromJob(QStringList &fieldlist, QString jobId)
231 {
232    QString job, client, fileset;
233    QString query("");
234    query = "SELECT DISTINCT Job.Name AS JobName, Client.Name AS Client,"
235    " FileSet.FileSet AS FileSet, Job.EndTime AS JobEnd"
236    " From Job, Client, FileSet"
237    " WHERE Job.FileSetId=FileSet.FileSetId AND Job.ClientId=Client.ClientId"
238    " AND JobId=\'" + jobId + "\'";
239    //printf("query = %s\n", query.toUtf8().data());
240    QStringList results;
241    if (m_console->sql_cmd(query, results)) {
242       QString field;
243
244       /* Iterate through the lines of results, there should only be one. */
245       foreach (QString resultline, results) {
246          fieldlist = resultline.split("\t");
247       } /* foreach resultline */
248    } /* if results from query */
249 }
250
251 /*
252  * Function to handle when the jobidlist line edit input loses focus or is entered
253  */
254 void prerestorePage::jobIdEditFinished()
255 {
256    checkJobIdList();
257 }
258
259 bool prerestorePage::checkJobIdList()
260 {
261    /* Need to check and make sure the text is a comma separated list of integers */
262    QString line = jobIdEdit->text();
263    if (line.contains(" ")) {
264       QMessageBox::warning(this, tr("Bat"),
265          tr("There can be no spaces in the text for the joblist.\n"
266          "Press OK to continue?"), QMessageBox::Ok );
267       return false;
268    }
269    QStringList joblist = line.split(",", QString::SkipEmptyParts);
270    bool allintokay = true, alljobok = true, allisjob = true;
271    QString jobName(""), clientName("");
272    foreach (QString job, joblist) {
273       bool intok;
274       job.toInt(&intok, 10);
275       if (intok) {
276          /* are the intergers representing a list of jobs all with the same job
277           * and client */
278          QStringList fields;
279          jobdefsFromJob(fields, job);
280          int count = fields.count();
281          if (count > 0) {
282             if (jobName == "")
283                jobName = fields[0];
284             else if (jobName != fields[0])
285                alljobok = false;
286             if (clientName == "")
287                clientName = fields[1];
288             else if (clientName != fields[1])
289                alljobok = false;
290          } else {
291             allisjob = false;
292          }
293       } else {
294          allintokay = false;
295       }
296    }
297    if (!allintokay){
298       QMessageBox::warning(this, tr("Bat"),
299          tr("The string is not a comma separated list if integers.\n"
300          "Press OK to continue?"), QMessageBox::Ok );
301       return false;
302    }
303    if (!allisjob){
304       QMessageBox::warning(this, tr("Bat"),
305          tr("At least one of the jobs is not a valid job.\n"
306          "Press OK to continue?"), QMessageBox::Ok );
307       return false;
308    }
309    if (!alljobok){
310       QMessageBox::warning(this, tr("Bat"),
311          tr("All jobs in the list must be of the same jobName and same client.\n"
312          "Press OK to continue?"), QMessageBox::Ok );
313       return false;
314    }
315    return true;
316 }
317
318 /*
319  * Handle the change of enabled of input widgets when the job radio buttons
320  * are changed.
321  */
322 void prerestorePage::jobRadioClicked(bool checked)
323 {
324    if (checked) {
325       jobCombo->setEnabled(true);
326       filesetCombo->setEnabled(true);
327       clientCombo->setEnabled(true);
328       poolCombo->setEnabled(true);
329       storageCombo->setEnabled(true);
330       recentCheckBox->setEnabled(true);
331       if (!recentCheckBox->isChecked()) {
332          beforeDateTime->setEnabled(true);
333       }
334       jobIdEdit->setEnabled(false);
335       selectJobRadio->setChecked(true);
336       selectJobIdsRadio->setChecked(false);
337    } else {
338       jobCombo->setEnabled(false);
339       filesetCombo->setEnabled(false);
340       clientCombo->setEnabled(false);
341       poolCombo->setEnabled(false);
342       storageCombo->setEnabled(false);
343       recentCheckBox->setEnabled(false);
344       beforeDateTime->setEnabled(false);
345       jobIdEdit->setEnabled(true);
346       selectJobRadio->setChecked(false);
347       selectJobIdsRadio->setChecked(true);
348    }
349    Dmsg2(200, "jobRadio=%d jobidsRadio=%d\n", selectJobRadio->isChecked(), 
350          selectJobIdsRadio->isChecked());
351 }
352
353 void prerestorePage::jobidsRadioClicked(bool checked)
354 {
355    if (checked) {
356       jobCombo->setEnabled(false);
357       filesetCombo->setEnabled(false);
358       clientCombo->setEnabled(false);
359       poolCombo->setEnabled(false);
360       storageCombo->setEnabled(false);
361       recentCheckBox->setEnabled(false);
362       beforeDateTime->setEnabled(false);
363       jobIdEdit->setEnabled(true);
364       selectJobRadio->setChecked(false);
365       selectJobIdsRadio->setChecked(true);
366    } else {
367       jobCombo->setEnabled(true);
368       filesetCombo->setEnabled(true);
369       clientCombo->setEnabled(true);
370       poolCombo->setEnabled(true);
371       storageCombo->setEnabled(true);
372       recentCheckBox->setEnabled(true);
373       if (!recentCheckBox->isChecked()) {
374          beforeDateTime->setEnabled(true);
375       }
376       jobIdEdit->setEnabled(false);
377       selectJobRadio->setChecked(true);
378       selectJobIdsRadio->setChecked(false);
379    }
380    Dmsg2(200, "jobRadio=%d jobidsRadio=%d\n", selectJobRadio->isChecked(), 
381          selectJobIdsRadio->isChecked());
382 }