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