]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/restore/prerestore.cpp
Fix some missed copyright changes
[bacula/bacula] / bacula / src / qt-console / restore / prerestore.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2010 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 three of the GNU Affero 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 Affero 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    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
67    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/restore.png")));
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(tr("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(mainWin->m_dtformat);
78    beforeDateTime->setDateTime(QDateTime::currentDateTime());
79    beforeDateTime->setEnabled(false);
80    selectFilesRadio->setChecked(true);
81    if (m_dataInType == R_NONE) {
82       selectJobRadio->setChecked(true);
83       selectJobIdsRadio->setChecked(false);
84       jobIdEdit->setText(tr("Comma separted list of Job Ids"));
85       jobIdEdit->setEnabled(false);
86    } else if (m_dataInType == R_JOBIDLIST) {
87       selectJobIdsRadio->setChecked(true);
88       selectJobRadio->setChecked(false);
89       jobIdEdit->setText(m_dataIn);
90       jobRadioClicked(false);
91       QStringList fieldlist;
92       if (jobdefsFromJob(fieldlist, m_dataIn) == 1) {
93          filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
94          clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
95          jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
96       }
97    } else if (m_dataInType == R_JOBDATETIME) {
98       selectJobRadio->setChecked(true);
99       selectJobIdsRadio->setChecked(false);
100       jobIdEdit->setText(tr("Comma separted list of Job Ids"));
101       jobIdEdit->setEnabled(false);
102       recentCheckBox->setCheckState(Qt::Unchecked);
103       jobRadioClicked(true);
104       QStringList fieldlist;
105       if (jobdefsFromJob(fieldlist, m_dataIn) == 1) {
106          filesetCombo->setCurrentIndex(filesetCombo->findText(fieldlist[2], Qt::MatchExactly));
107          clientCombo->setCurrentIndex(clientCombo->findText(fieldlist[1], Qt::MatchExactly));
108          jobCombo->setCurrentIndex(jobCombo->findText(fieldlist[0], Qt::MatchExactly));
109          beforeDateTime->setDateTime(QDateTime::fromString(fieldlist[3], mainWin->m_dtformat));
110      }
111    }
112    job_name_change(0);
113    connect(jobCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(job_name_change(int)));
114    connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
115    connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
116    connect(recentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(recentChanged(int)));
117    connect(selectJobRadio, SIGNAL(clicked(bool)), this, SLOT(jobRadioClicked(bool)));
118    connect(selectJobIdsRadio, SIGNAL(clicked(bool)), this, SLOT(jobidsRadioClicked(bool)));
119    connect(jobIdEdit, SIGNAL(editingFinished()), this, SLOT(jobIdEditFinished()));
120
121    dockPage();
122    setCurrent();
123    this->show();
124    if (mainWin->m_miscDebug) Pmsg0(000, "Leave preRestore\n");
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    }
139    QString cmd;
140
141    this->hide();
142
143    cmd = QString("restore");
144    cmd += " fileset=\"" + filesetCombo->currentText() + "\"";
145    cmd += " client=\"" + clientCombo->currentText() + "\"";
146    if (selectJobRadio->isChecked()) {
147       if (poolCombo->currentText() != tr("Any") ){
148          cmd += " pool=\"" + poolCombo->currentText() + "\"";
149       }
150       cmd += " storage=\"" + storageCombo->currentText() + "\"";
151       if (recentCheckBox->checkState() == Qt::Checked) {
152          cmd += " current";
153       } else {
154          QDateTime stamp = beforeDateTime->dateTime();
155          QString before = stamp.toString(mainWin->m_dtformat);
156          cmd += " before=\"" + before + "\"";
157       }
158    } else {
159       cmd += " jobid=\"" + jobIdEdit->text() + "\"";
160    }
161    if (selectFilesRadio->isChecked()) {
162       if (!selectJobIdsRadio->isChecked())
163          cmd += " select";
164    } else {
165       cmd += " all done";
166    }
167
168    if (mainWin->m_commandDebug) {
169       Pmsg1(000, "preRestore command \'%s\'\n", cmd.toUtf8().data());
170    }
171    /* 
172     * Send off command that looks something like:
173     *
174     * restore fileset="Full Set" client="timmy-fd" 
175     *        storage="File" current select
176     */
177    m_console->write_dir(m_conn, cmd.toUtf8().data());
178
179    /* Note, do not turn notifier back on here ... */
180    if (selectFilesRadio->isChecked()) {
181       setConsoleCurrent();
182       closeStackPage();
183       /* wait will be exited in the restore page constructor */
184       mainWin->waitEnter();
185    } else {
186       closeStackPage();
187       mainWin->resetFocus();
188    }
189    m_console->notify(m_conn, true);
190    if (mainWin->m_miscDebug) Pmsg0(000, "preRestore OK pressed\n");
191 }
192
193
194 /*
195  * Destroy the instace of the class
196  */
197 void prerestorePage::cancelButtonPushed()
198 {
199    mainWin->set_status(tr("Canceled"));
200    this->hide();
201    m_console->notify(m_conn, true);
202    closeStackPage();
203 }
204
205
206 /*
207  * Handle updating the other widget with job defaults when the job combo is changed.
208  */
209 void prerestorePage::job_name_change(int index)
210 {
211    job_defaults job_defs;
212
213    (void)index;
214    job_defs.job_name = jobCombo->currentText();
215    if (m_console->get_job_defaults(m_conn, job_defs)) {
216       filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
217       clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
218       poolCombo->setCurrentIndex(poolCombo->findText(tr("Any"), Qt::MatchExactly));
219       storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
220    }
221 }
222
223 /*
224  * Handle the change of enabled of input widgets when the recent checkbox state
225  * is changed.
226  */
227 void prerestorePage::recentChanged(int state)
228 {
229    if ((state == Qt::Unchecked) && (selectJobRadio->isChecked())) {
230       beforeDateTime->setEnabled(true);
231    } else {
232       beforeDateTime->setEnabled(false);
233    }
234 }
235
236
237 /*
238  * For when jobs list is to be used, return a list which is the needed items from
239  * the job record
240  */
241 int prerestorePage::jobdefsFromJob(QStringList &fieldlist, QString &jobId)
242 {
243    QString job, client, fileset;
244    QString query("");
245    query = "SELECT DISTINCT Job.Name AS JobName, Client.Name AS Client,"
246    " FileSet.FileSet AS FileSet, Job.EndTime AS JobEnd,"
247    " Job.Type AS JobType"
248    " From Job, Client, FileSet"
249    " WHERE Job.FileSetId=FileSet.FileSetId AND Job.ClientId=Client.ClientId"
250    " AND JobId=\'" + jobId + "\'";
251    if (mainWin->m_sqlDebug) { Pmsg1(000, "query = %s\n", query.toUtf8().data()); }
252    QStringList results;
253    if (m_console->sql_cmd(m_conn, query, results)) {
254       QString field;
255
256       /* Iterate through the lines of results, there should only be one. */
257       foreach (QString resultline, results) {
258          fieldlist = resultline.split("\t");
259       } /* foreach resultline */
260    } /* if results from query */
261
262    /* ***FIXME*** This should not ever be getting more than one */
263    return results.count() >= 1;
264 }
265
266 /*
267  * Function to handle when the jobidlist line edit input loses focus or is entered
268  */
269 void prerestorePage::jobIdEditFinished()
270 {
271    checkJobIdList();
272 }
273
274 bool prerestorePage::checkJobIdList()
275 {
276    /* Need to check and make sure the text is a comma separated list of integers */
277    QString line = jobIdEdit->text();
278    if (line.contains(" ")) {
279       QMessageBox::warning(this, "Bat",
280          tr("There can be no spaces in the text for the joblist.\n"
281          "Press OK to continue?"), QMessageBox::Ok );
282       return false;
283    }
284    QStringList joblist = line.split(",", QString::SkipEmptyParts);
285    bool allintokay = true, alljobok = true, allisjob = true;
286    QString jobName(""), clientName("");
287    foreach (QString job, joblist) {
288       bool intok;
289       job.toInt(&intok, 10);
290       if (intok) {
291          /* are the integers representing a list of jobs all with the same job
292           * and client */
293          QStringList fields;
294          if (jobdefsFromJob(fields, job) == 1) {
295             if (jobName == "")
296                jobName = fields[0];
297             else if (jobName != fields[0])
298                alljobok = false;
299             if (clientName == "")
300                clientName = fields[1];
301             else if (clientName != fields[1])
302                alljobok = false;
303          } else {
304             allisjob = false;
305          }
306       } else {
307          allintokay = false;
308       }
309    }
310    if (!allintokay){
311       QMessageBox::warning(this, "Bat",
312          tr("The string is not a comma separated list of integers.\n"
313          "Press OK to continue?"), QMessageBox::Ok );
314       return false;
315    }
316    if (!allisjob){
317       QMessageBox::warning(this, tr("Bat"),
318          tr("At least one of the jobs is not a valid job of type \"Backup\".\n"
319          "Press OK to continue?"), QMessageBox::Ok );
320       return false;
321    }
322    if (!alljobok){
323       QMessageBox::warning(this, "Bat",
324          tr("All jobs in the list must be of the same jobName and same client.\n"
325          "Press OK to continue?"), QMessageBox::Ok );
326       return false;
327    }
328    return true;
329 }
330
331 /*
332  * Handle the change of enabled of input widgets when the job radio buttons
333  * are changed.
334  */
335 void prerestorePage::jobRadioClicked(bool checked)
336 {
337    if (checked) {
338       jobCombo->setEnabled(true);
339       filesetCombo->setEnabled(true);
340       clientCombo->setEnabled(true);
341       poolCombo->setEnabled(true);
342       storageCombo->setEnabled(true);
343       recentCheckBox->setEnabled(true);
344       if (!recentCheckBox->isChecked()) {
345          beforeDateTime->setEnabled(true);
346       }
347       jobIdEdit->setEnabled(false);
348       selectJobRadio->setChecked(true);
349       selectJobIdsRadio->setChecked(false);
350    } else {
351       jobCombo->setEnabled(false);
352       filesetCombo->setEnabled(false);
353       clientCombo->setEnabled(false);
354       poolCombo->setEnabled(false);
355       storageCombo->setEnabled(false);
356       recentCheckBox->setEnabled(false);
357       beforeDateTime->setEnabled(false);
358       jobIdEdit->setEnabled(true);
359       selectJobRadio->setChecked(false);
360       selectJobIdsRadio->setChecked(true);
361    }
362    if (mainWin->m_miscDebug) {
363       Pmsg2(000, "jobRadio=%d jobidsRadio=%d\n", selectJobRadio->isChecked(), 
364          selectJobIdsRadio->isChecked());
365    }
366 }
367
368 void prerestorePage::jobidsRadioClicked(bool checked)
369 {
370    if (checked) {
371       jobCombo->setEnabled(false);
372       filesetCombo->setEnabled(false);
373       clientCombo->setEnabled(false);
374       poolCombo->setEnabled(false);
375       storageCombo->setEnabled(false);
376       recentCheckBox->setEnabled(false);
377       beforeDateTime->setEnabled(false);
378       jobIdEdit->setEnabled(true);
379       selectJobRadio->setChecked(false);
380       selectJobIdsRadio->setChecked(true);
381    } else {
382       jobCombo->setEnabled(true);
383       filesetCombo->setEnabled(true);
384       clientCombo->setEnabled(true);
385       poolCombo->setEnabled(true);
386       storageCombo->setEnabled(true);
387       recentCheckBox->setEnabled(true);
388       if (!recentCheckBox->isChecked()) {
389          beforeDateTime->setEnabled(true);
390       }
391       jobIdEdit->setEnabled(false);
392       selectJobRadio->setChecked(true);
393       selectJobIdsRadio->setChecked(false);
394    }
395    if (mainWin->m_miscDebug) {
396       Pmsg2(000, "jobRadio=%d jobidsRadio=%d\n", selectJobRadio->isChecked(), 
397          selectJobIdsRadio->isChecked());
398    }
399 }