]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/restore/prerestore.cpp
Use endtime to include the job that was clicked on.
[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       cmd += " select";
155    } else {
156       cmd += " all done";
157    }
158
159    /* ***FIXME*** */
160    printf("preRestore command \'%s\'\n", cmd.toUtf8().data());
161    consoleCommand(cmd);
162    /* Note, do not turn notifier back on here ... */
163    if (selectFilesRadio->isChecked()) {
164       setConsoleCurrent();
165       new restorePage();
166       closeStackPage();
167    } else {
168       m_console->notify(true);
169       closeStackPage();
170       mainWin->resetFocus();
171    }
172 }
173
174
175 /*
176  * Destroy the instace of the class
177  */
178 void prerestorePage::cancelButtonPushed()
179 {
180    mainWin->set_status("Canceled");
181    this->hide();
182    m_console->notify(true);
183    closeStackPage();
184 }
185
186
187 /*
188  * Handle updating the other widget with job defaults when the job combo is changed.
189  */
190 void prerestorePage::job_name_change(int index)
191 {
192    job_defaults job_defs;
193
194    (void)index;
195    job_defs.job_name = jobCombo->currentText();
196    if (m_console->get_job_defaults(job_defs)) {
197       filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
198       clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
199       poolCombo->setCurrentIndex(poolCombo->findText(job_defs.pool_name, Qt::MatchExactly));
200       storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
201    }
202 }
203
204 /*
205  * Handle the change of enabled of input widgets when the recent checkbox state
206  * is changed.
207  */
208 void prerestorePage::recentChanged(int state)
209 {
210    if ((state == Qt::Unchecked) && (selectJobsRadio->isChecked())) {
211       beforeDateTime->setEnabled(true);
212    } else {
213       beforeDateTime->setEnabled(false);
214    }
215 }
216
217 /*
218  * Handle the change of enabled of input widgets when the job radio buttons
219  * are changed.
220  */
221 void prerestorePage::jobsRadioClicked(bool checked)
222 {
223    if (checked) {
224       jobCombo->setEnabled(true);
225       filesetCombo->setEnabled(true);
226       clientCombo->setEnabled(true);
227       poolCombo->setEnabled(true);
228       storageCombo->setEnabled(true);
229       recentCheckBox->setEnabled(true);
230       if (!recentCheckBox->isChecked()) {
231          beforeDateTime->setEnabled(true);
232       }
233       jobIdEdit->setEnabled(false);
234    } else {
235       jobCombo->setEnabled(false);
236       filesetCombo->setEnabled(false);
237       clientCombo->setEnabled(false);
238       poolCombo->setEnabled(false);
239       storageCombo->setEnabled(false);
240       recentCheckBox->setEnabled(false);
241       beforeDateTime->setEnabled(false);
242       jobIdEdit->setEnabled(true);
243    }
244 }
245
246 /*
247  * For when jobs list is to be used, return a list which is the needed items from
248  * the job record
249  */
250 void prerestorePage::jobdefsFromJob(QStringList &fieldlist, QString jobId)
251 {
252    QString job, client, fileset;
253    QString query("");
254    query = "SELECT DISTINCT Job.Name AS JobName, Client.Name AS Client,"
255    " FileSet.FileSet AS FileSet, Job.EndTime AS JobEnd"
256    " From Job, Client, FileSet"
257    " WHERE Job.FileSetId=FileSet.FileSetId AND Job.ClientId=Client.ClientId"
258    " AND JobId=\'" + jobId + "\'";
259    //printf("query = %s\n", query.toUtf8().data());
260    QStringList results;
261    if (m_console->sql_cmd(query, results)) {
262       QString field;
263
264       /* Iterate through the lines of results, there should only be one. */
265       foreach (QString resultline, results) {
266          fieldlist = resultline.split("\t");
267       } /* foreach resultline */
268    } /* if results from query */
269 }
270
271 /*
272  * Function to handle when the jobidlist line edit input loses focus or is entered
273  */
274 void prerestorePage::jobIdEditFinished()
275 {
276    checkJobIdList();
277 }
278
279 bool prerestorePage::checkJobIdList()
280 {
281    /* Need to check and make sure the text is a comma separated list of integers */
282    QString line = jobIdEdit->text();
283    if (line.contains(" ")) {
284       QMessageBox::warning(this, tr("Bat"),
285          tr("There can be no spaces in the text for the joblist.\n"
286          "Press OK to continue?"), QMessageBox::Ok );
287       return false;
288    }
289    QStringList joblist = line.split(",", QString::SkipEmptyParts);
290    bool allintokay = true, alljobok = true, allisjob = true;
291    QString jobName(""), clientName("");
292    foreach (QString job, joblist) {
293       bool intok;
294       job.toInt(&intok, 10);
295       if (intok) {
296          /* are the intergers representing a list of jobs all with the same job
297           * and client */
298          QStringList fields;
299          jobdefsFromJob(fields, job);
300          int count = fields.count();
301          if (count > 0) {
302             if (jobName == "")
303                jobName = fields[0];
304             else if (jobName != fields[0])
305                alljobok = false;
306             if (clientName == "")
307                clientName = fields[1];
308             else if (clientName != fields[1])
309                alljobok = false;
310          } else {
311             allisjob = false;
312          }
313       } else {
314          allintokay = false;
315       }
316    }
317    if (!allintokay){
318       QMessageBox::warning(this, tr("Bat"),
319          tr("The string is not a comma separated list if integers.\n"
320          "Press OK to continue?"), QMessageBox::Ok );
321       return false;
322    }
323    if (!allisjob){
324       QMessageBox::warning(this, tr("Bat"),
325          tr("At least one of the jobs is not a valid job.\n"
326          "Press OK to continue?"), QMessageBox::Ok );
327       return false;
328    }
329    if (!alljobok){
330       QMessageBox::warning(this, tr("Bat"),
331          tr("All jobs in the list must be of the same jobName and same client.\n"
332          "Press OK to continue?"), QMessageBox::Ok );
333       return false;
334    }
335    return true;
336 }