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