]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/restore/prerestore.cpp
This condition lets the job selection work correctly by not including the
[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    jobCombo->addItems(m_console->job_list);
70    filesetCombo->addItems(m_console->fileset_list);
71    clientCombo->addItems(m_console->client_list);
72    poolCombo->addItem("Any");
73    poolCombo->addItems(m_console->pool_list);
74    storageCombo->addItems(m_console->storage_list);
75    /* current or before . .  Start out with current checked */
76    recentCheckBox->setCheckState(Qt::Checked);
77    beforeDateTime->setDisplayFormat(m_dtformat);
78    beforeDateTime->setDateTime(QDateTime::currentDateTime());
79    beforeDateTime->setEnabled(false);
80    selectFilesRadio->setChecked(true);
81    if (m_dataInType == R_NONE) {
82       selectJobsRadio->setChecked(true);
83       jobIdEdit->setText("Comma separted list of jobs id's");
84       jobIdEdit->setEnabled(false);
85    } else if (m_dataInType == R_JOBIDLIST) {
86       listJobsRadio->setChecked(true);
87       jobIdEdit->setText(m_dataIn);
88       jobsRadioClicked(false);
89       QStringList fieldlist;
90       jobdefsFromJob(fieldlist,m_dataIn);
91       filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
92       clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
93       jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
94    } else if (m_dataInType == R_JOBDATETIME) {
95       selectJobsRadio->setChecked(true);
96       jobIdEdit->setText("Comma separted list of jobs id's");
97       jobIdEdit->setEnabled(false);
98       recentCheckBox->setCheckState(Qt::Unchecked);
99       jobsRadioClicked(true);
100       QStringList fieldlist;
101       jobdefsFromJob(fieldlist,m_dataIn);
102       filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
103       clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
104       jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
105       beforeDateTime->setDateTime(QDateTime::fromString(fieldlist[3], m_dtformat));
106    }
107    job_name_change(0);
108    connect(jobCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(job_name_change(int)));
109    connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
110    connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
111    connect(recentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(recentChanged(int)));
112    connect(selectJobsRadio, SIGNAL(toggled(bool)), this, SLOT(jobsRadioClicked(bool)));
113    connect(jobIdEdit, SIGNAL(editingFinished()), this, SLOT(jobIdEditFinished()));
114
115    dockPage();
116    setCurrent();
117    this->show();
118 }
119
120
121 /*
122  * Check to make sure all is ok then start either the select window or the restore
123  * run window
124  */
125 void prerestorePage::okButtonPushed()
126 {
127    if (!selectJobsRadio->isChecked()) {
128       if (!checkJobIdList())
129          return;
130    }
131    QString cmd;
132
133    this->hide();
134
135    cmd = QString("restore");
136    cmd += " fileset=\"" + filesetCombo->currentText() + "\"";
137    cmd += " client=\"" + clientCombo->currentText() + "\"";
138    if (selectJobsRadio->isChecked()) {
139       if (poolCombo->currentText() != "Any" ){
140          cmd += " pool=\"" + poolCombo->currentText() + "\"";
141       }
142       cmd += " storage=\"" + storageCombo->currentText() + "\"";
143       if (recentCheckBox->checkState() == Qt::Checked) {
144          cmd += " current";
145       } else {
146          QDateTime stamp = beforeDateTime->dateTime();
147          QString before = stamp.toString(m_dtformat);
148          cmd += " before=\"" + before + "\"";
149       }
150    } else {
151       cmd += " jobid=\"" + jobIdEdit->text() + "\"";
152    }
153    if (selectFilesRadio->isChecked()) {
154       if (!listJobsRadio->isChecked())
155          cmd += " select";
156    } else {
157       cmd += " all done";
158    }
159
160    /* ***FIXME*** */
161    printf("preRestore command \'%s\'\n", cmd.toUtf8().data());
162    consoleCommand(cmd);
163    /* Note, do not turn notifier back on here ... */
164    if (selectFilesRadio->isChecked()) {
165       setConsoleCurrent();
166       new restorePage();
167       closeStackPage();
168    } else {
169       m_console->notify(true);
170       closeStackPage();
171       mainWin->resetFocus();
172    }
173 }
174
175
176 /*
177  * Destroy the instace of the class
178  */
179 void prerestorePage::cancelButtonPushed()
180 {
181    mainWin->set_status("Canceled");
182    this->hide();
183    m_console->notify(true);
184    closeStackPage();
185 }
186
187
188 /*
189  * Handle updating the other widget with job defaults when the job combo is changed.
190  */
191 void prerestorePage::job_name_change(int index)
192 {
193    job_defaults job_defs;
194
195    (void)index;
196    job_defs.job_name = jobCombo->currentText();
197    if (m_console->get_job_defaults(job_defs)) {
198       filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
199       clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
200       poolCombo->setCurrentIndex(poolCombo->findText(job_defs.pool_name, Qt::MatchExactly));
201       storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
202    }
203 }
204
205 /*
206  * Handle the change of enabled of input widgets when the recent checkbox state
207  * is changed.
208  */
209 void prerestorePage::recentChanged(int state)
210 {
211    if ((state == Qt::Unchecked) && (selectJobsRadio->isChecked())) {
212       beforeDateTime->setEnabled(true);
213    } else {
214       beforeDateTime->setEnabled(false);
215    }
216 }
217
218 /*
219  * Handle the change of enabled of input widgets when the job radio buttons
220  * are changed.
221  */
222 void prerestorePage::jobsRadioClicked(bool checked)
223 {
224    if (checked) {
225       jobCombo->setEnabled(true);
226       filesetCombo->setEnabled(true);
227       clientCombo->setEnabled(true);
228       poolCombo->setEnabled(true);
229       storageCombo->setEnabled(true);
230       recentCheckBox->setEnabled(true);
231       if (!recentCheckBox->isChecked()) {
232          beforeDateTime->setEnabled(true);
233       }
234       jobIdEdit->setEnabled(false);
235    } else {
236       jobCombo->setEnabled(false);
237       filesetCombo->setEnabled(false);
238       clientCombo->setEnabled(false);
239       poolCombo->setEnabled(false);
240       storageCombo->setEnabled(false);
241       recentCheckBox->setEnabled(false);
242       beforeDateTime->setEnabled(false);
243       jobIdEdit->setEnabled(true);
244    }
245 }
246
247 /*
248  * For when jobs list is to be used, return a list which is the needed items from
249  * the job record
250  */
251 void prerestorePage::jobdefsFromJob(QStringList &fieldlist, QString jobId)
252 {
253    QString job, client, fileset;
254    QString query("");
255    query = "SELECT DISTINCT Job.Name AS JobName, Client.Name AS Client,"
256    " FileSet.FileSet AS FileSet, Job.EndTime AS JobEnd"
257    " From Job, Client, FileSet"
258    " WHERE Job.FileSetId=FileSet.FileSetId AND Job.ClientId=Client.ClientId"
259    " AND JobId=\'" + jobId + "\'";
260    //printf("query = %s\n", query.toUtf8().data());
261    QStringList results;
262    if (m_console->sql_cmd(query, results)) {
263       QString field;
264
265       /* Iterate through the lines of results, there should only be one. */
266       foreach (QString resultline, results) {
267          fieldlist = resultline.split("\t");
268       } /* foreach resultline */
269    } /* if results from query */
270 }
271
272 /*
273  * Function to handle when the jobidlist line edit input loses focus or is entered
274  */
275 void prerestorePage::jobIdEditFinished()
276 {
277    checkJobIdList();
278 }
279
280 bool prerestorePage::checkJobIdList()
281 {
282    /* Need to check and make sure the text is a comma separated list of integers */
283    QString line = jobIdEdit->text();
284    if (line.contains(" ")) {
285       QMessageBox::warning(this, tr("Bat"),
286          tr("There can be no spaces in the text for the joblist.\n"
287          "Press OK to continue?"), QMessageBox::Ok );
288       return false;
289    }
290    QStringList joblist = line.split(",", QString::SkipEmptyParts);
291    bool allintokay = true, alljobok = true, allisjob = true;
292    QString jobName(""), clientName("");
293    foreach (QString job, joblist) {
294       bool intok;
295       job.toInt(&intok, 10);
296       if (intok) {
297          /* are the intergers representing a list of jobs all with the same job
298           * and client */
299          QStringList fields;
300          jobdefsFromJob(fields, job);
301          int count = fields.count();
302          if (count > 0) {
303             if (jobName == "")
304                jobName = fields[0];
305             else if (jobName != fields[0])
306                alljobok = false;
307             if (clientName == "")
308                clientName = fields[1];
309             else if (clientName != fields[1])
310                alljobok = false;
311          } else {
312             allisjob = false;
313          }
314       } else {
315          allintokay = false;
316       }
317    }
318    if (!allintokay){
319       QMessageBox::warning(this, tr("Bat"),
320          tr("The string is not a comma separated list if integers.\n"
321          "Press OK to continue?"), QMessageBox::Ok );
322       return false;
323    }
324    if (!allisjob){
325       QMessageBox::warning(this, tr("Bat"),
326          tr("At least one of the jobs is not a valid job.\n"
327          "Press OK to continue?"), QMessageBox::Ok );
328       return false;
329    }
330    if (!alljobok){
331       QMessageBox::warning(this, tr("Bat"),
332          tr("All jobs in the list must be of the same jobName and same client.\n"
333          "Press OK to continue?"), QMessageBox::Ok );
334       return false;
335    }
336    return true;
337 }