]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/restore/prerestore.cpp
Put debug level on variable
[bacula/bacula] / bacula / src / qt-console / restore / prerestore.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28  
29 /*
30  *   Version $Id$
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_name = tr("Restore");
63    setupUi(this);
64    pgInitialize();
65    m_conn = m_console->notifyOff();
66    m_closeable = true;
67    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
68    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/restore.png")));
69
70    jobCombo->addItems(m_console->job_list);
71    filesetCombo->addItems(m_console->fileset_list);
72    clientCombo->addItems(m_console->client_list);
73    poolCombo->addItem(tr("Any"));
74    poolCombo->addItems(m_console->pool_list);
75    storageCombo->addItems(m_console->storage_list);
76    /* current or before . .  Start out with current checked */
77    recentCheckBox->setCheckState(Qt::Checked);
78    beforeDateTime->setDisplayFormat(mainWin->m_dtformat);
79    beforeDateTime->setDateTime(QDateTime::currentDateTime());
80    beforeDateTime->setEnabled(false);
81    selectFilesRadio->setChecked(true);
82    if (m_dataInType == R_NONE) {
83       selectJobRadio->setChecked(true);
84       selectJobIdsRadio->setChecked(false);
85       jobIdEdit->setText(tr("Comma separted list of Job Ids"));
86       jobIdEdit->setEnabled(false);
87    } else if (m_dataInType == R_JOBIDLIST) {
88       selectJobIdsRadio->setChecked(true);
89       selectJobRadio->setChecked(false);
90       jobIdEdit->setText(m_dataIn);
91       jobRadioClicked(false);
92       QStringList fieldlist;
93       if (jobdefsFromJob(fieldlist, m_dataIn) == 1) {
94          filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
95          clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
96          jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
97       }
98    } else if (m_dataInType == R_JOBDATETIME) {
99       selectJobRadio->setChecked(true);
100       selectJobIdsRadio->setChecked(false);
101       jobIdEdit->setText(tr("Comma separted list of Job Ids"));
102       jobIdEdit->setEnabled(false);
103       recentCheckBox->setCheckState(Qt::Unchecked);
104       jobRadioClicked(true);
105       QStringList fieldlist;
106       if (jobdefsFromJob(fieldlist, m_dataIn) == 1) {
107          filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
108          clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
109          jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
110          beforeDateTime->setDateTime(QDateTime::fromString(fieldlist[3], mainWin->m_dtformat));
111      }
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() != tr("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(mainWin->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    if (mainWin->m_commandDebug) {
168       Pmsg1(000, "preRestore command \'%s\'\n", cmd.toUtf8().data());
169    }
170    m_console->write_dir(m_conn, cmd.toUtf8().data());
171
172    /* Note, do not turn notifier back on here ... */
173    if (selectFilesRadio->isChecked()) {
174       setConsoleCurrent();
175       closeStackPage();
176    } else {
177       closeStackPage();
178       mainWin->resetFocus();
179    }
180    m_console->notify(m_conn, true);
181 }
182
183
184 /*
185  * Destroy the instace of the class
186  */
187 void prerestorePage::cancelButtonPushed()
188 {
189    mainWin->set_status(tr("Canceled"));
190    this->hide();
191    m_console->notify(m_conn, true);
192    closeStackPage();
193 }
194
195
196 /*
197  * Handle updating the other widget with job defaults when the job combo is changed.
198  */
199 void prerestorePage::job_name_change(int index)
200 {
201    job_defaults job_defs;
202
203    (void)index;
204    job_defs.job_name = jobCombo->currentText();
205    if (m_console->get_job_defaults(job_defs)) {
206       filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
207       clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
208       poolCombo->setCurrentIndex(poolCombo->findText(tr("Any"), Qt::MatchExactly));
209       storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
210    }
211 }
212
213 /*
214  * Handle the change of enabled of input widgets when the recent checkbox state
215  * is changed.
216  */
217 void prerestorePage::recentChanged(int state)
218 {
219    if ((state == Qt::Unchecked) && (selectJobRadio->isChecked())) {
220       beforeDateTime->setEnabled(true);
221    } else {
222       beforeDateTime->setEnabled(false);
223    }
224 }
225
226
227 /*
228  * For when jobs list is to be used, return a list which is the needed items from
229  * the job record
230  */
231 int prerestorePage::jobdefsFromJob(QStringList &fieldlist, QString &jobId)
232 {
233    QString job, client, fileset;
234    QString query("");
235    query = "SELECT DISTINCT Job.Name AS JobName, Client.Name AS Client,"
236    " FileSet.FileSet AS FileSet, Job.EndTime AS JobEnd,"
237    " Job.Type AS JobType"
238    " From Job, Client, FileSet"
239    " WHERE Job.FileSetId=FileSet.FileSetId AND Job.ClientId=Client.ClientId"
240    " AND JobId=\'" + jobId + "\'";
241    if (mainWin->m_sqlDebug) { Pmsg1(000, "query = %s\n", query.toUtf8().data()); }
242    QStringList results;
243    if (m_console->sql_cmd(query, results)) {
244       QString field;
245
246       /* Iterate through the lines of results, there should only be one. */
247       foreach (QString resultline, results) {
248          fieldlist = resultline.split("\t");
249       } /* foreach resultline */
250    } /* if results from query */
251
252    /* FIXME This should not be getting more than one ever */
253    if(results.count() >= 1) return 1;
254    else return 0;
255 }
256
257 /*
258  * Function to handle when the jobidlist line edit input loses focus or is entered
259  */
260 void prerestorePage::jobIdEditFinished()
261 {
262    checkJobIdList();
263 }
264
265 bool prerestorePage::checkJobIdList()
266 {
267    /* Need to check and make sure the text is a comma separated list of integers */
268    QString line = jobIdEdit->text();
269    if (line.contains(" ")) {
270       QMessageBox::warning(this, "Bat",
271          tr("There can be no spaces in the text for the joblist.\n"
272          "Press OK to continue?"), QMessageBox::Ok );
273       return false;
274    }
275    QStringList joblist = line.split(",", QString::SkipEmptyParts);
276    bool allintokay = true, alljobok = true, allisjob = true;
277    QString jobName(""), clientName("");
278    foreach (QString job, joblist) {
279       bool intok;
280       job.toInt(&intok, 10);
281       if (intok) {
282          /* are the integers representing a list of jobs all with the same job
283           * and client */
284          QStringList fields;
285          if (jobdefsFromJob(fields, job) == 1) {
286             if (jobName == "")
287                jobName = fields[0];
288             else if (jobName != fields[0])
289                alljobok = false;
290             if (clientName == "")
291                clientName = fields[1];
292             else if (clientName != fields[1])
293                alljobok = false;
294          } else {
295             allisjob = false;
296          }
297       } else {
298          allintokay = false;
299       }
300    }
301    if (!allintokay){
302       QMessageBox::warning(this, "Bat",
303          tr("The string is not a comma separated list of integers.\n"
304          "Press OK to continue?"), QMessageBox::Ok );
305       return false;
306    }
307    if (!allisjob){
308       QMessageBox::warning(this, tr("Bat"),
309          tr("At least one of the jobs is not a valid job of type \"Backup\".\n"
310          "Press OK to continue?"), QMessageBox::Ok );
311       return false;
312    }
313    if (!alljobok){
314       QMessageBox::warning(this, "Bat",
315          tr("All jobs in the list must be of the same jobName and same client.\n"
316          "Press OK to continue?"), QMessageBox::Ok );
317       return false;
318    }
319    return true;
320 }
321
322 /*
323  * Handle the change of enabled of input widgets when the job radio buttons
324  * are changed.
325  */
326 void prerestorePage::jobRadioClicked(bool checked)
327 {
328    if (checked) {
329       jobCombo->setEnabled(true);
330       filesetCombo->setEnabled(true);
331       clientCombo->setEnabled(true);
332       poolCombo->setEnabled(true);
333       storageCombo->setEnabled(true);
334       recentCheckBox->setEnabled(true);
335       if (!recentCheckBox->isChecked()) {
336          beforeDateTime->setEnabled(true);
337       }
338       jobIdEdit->setEnabled(false);
339       selectJobRadio->setChecked(true);
340       selectJobIdsRadio->setChecked(false);
341    } else {
342       jobCombo->setEnabled(false);
343       filesetCombo->setEnabled(false);
344       clientCombo->setEnabled(false);
345       poolCombo->setEnabled(false);
346       storageCombo->setEnabled(false);
347       recentCheckBox->setEnabled(false);
348       beforeDateTime->setEnabled(false);
349       jobIdEdit->setEnabled(true);
350       selectJobRadio->setChecked(false);
351       selectJobIdsRadio->setChecked(true);
352    }
353    Dmsg2(200, "jobRadio=%d jobidsRadio=%d\n", selectJobRadio->isChecked(), 
354          selectJobIdsRadio->isChecked());
355 }
356
357 void prerestorePage::jobidsRadioClicked(bool checked)
358 {
359    if (checked) {
360       jobCombo->setEnabled(false);
361       filesetCombo->setEnabled(false);
362       clientCombo->setEnabled(false);
363       poolCombo->setEnabled(false);
364       storageCombo->setEnabled(false);
365       recentCheckBox->setEnabled(false);
366       beforeDateTime->setEnabled(false);
367       jobIdEdit->setEnabled(true);
368       selectJobRadio->setChecked(false);
369       selectJobIdsRadio->setChecked(true);
370    } else {
371       jobCombo->setEnabled(true);
372       filesetCombo->setEnabled(true);
373       clientCombo->setEnabled(true);
374       poolCombo->setEnabled(true);
375       storageCombo->setEnabled(true);
376       recentCheckBox->setEnabled(true);
377       if (!recentCheckBox->isChecked()) {
378          beforeDateTime->setEnabled(true);
379       }
380       jobIdEdit->setEnabled(false);
381       selectJobRadio->setChecked(true);
382       selectJobIdsRadio->setChecked(false);
383    }
384    Dmsg2(200, "jobRadio=%d jobidsRadio=%d\n", selectJobRadio->isChecked(), 
385          selectJobIdsRadio->isChecked());
386 }