]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/joblist/joblist.cpp
jobs: add actions and slots for performing commands.
[bacula/bacula] / bacula / src / qt-console / joblist / joblist.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-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  *   Version $Id: joblist.h 4230 2007-02-21 20:07:37Z kerns $
30  *
31  *   Dirk Bartley, March 2007
32  */
33  
34 #include <QAbstractEventDispatcher>
35 #include <QTableWidgetItem>
36 #include "bat.h"
37 #include "joblist.h"
38 #include "restore.h"
39 #include "joblog/joblog.h"
40
41 /*
42  * Constructor for the class
43  */
44 JobList::JobList(const QString &mediaName, const QString &clientname,
45          QTreeWidgetItem *parentTreeWidgetItem)
46 {
47    setupUi(this);
48    m_name = ""; /* treeWidgetName has a virtual override in this class */
49    m_mediaName = mediaName;
50    m_clientName = clientname;
51    pgInitialize(parentTreeWidgetItem);
52    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
53    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/emblem-system.svg")));
54
55    m_resultCount = 0;
56    m_populated = false;
57    m_closeable = false;
58    if ((m_mediaName != "") || (m_clientName != "")) { m_closeable=true; }
59    m_checkCurrentWidget = true;
60    createConnections();
61
62    /* Set Defaults for check and spin for limits */
63    limitCheckBox->setCheckState(mainWin->m_recordLimitCheck ? Qt::Checked : Qt::Unchecked);
64    limitSpinBox->setValue(mainWin->m_recordLimitVal);
65    daysCheckBox->setCheckState(mainWin->m_daysLimitCheck ? Qt::Checked : Qt::Unchecked);
66    daysSpinBox->setValue(mainWin->m_daysLimitVal);
67    dockPage();
68 }
69
70 /*
71  * The Meat of the class.
72  * This function will populate the QTableWidget, mp_tablewidget, with
73  * QTableWidgetItems representing the results of a query for what jobs exist on
74  * the media name passed from the constructor stored in m_mediaName.
75  */
76 void JobList::populateTable()
77 {
78    QStringList results;
79    QString resultline;
80    QBrush blackBrush(Qt::black);
81
82    if (!m_console->preventInUseConnect())
83        return;
84
85    /* Can't do this in constructor because not neccesarily conected in constructor */
86    if (!m_populated) {
87       clientsComboBox->addItem("Any");
88       clientsComboBox->addItems(m_console->client_list);
89       int clientIndex = clientsComboBox->findText(m_clientName, Qt::MatchExactly);
90       if (clientIndex != -1)
91          clientsComboBox->setCurrentIndex(clientIndex);
92
93       QString query("SELECT VolumeName AS Media FROM Media ORDER BY Media");
94       if (mainWin->m_sqlDebug) {
95          Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
96       }
97       QStringList results, volumeList;
98       if (m_console->sql_cmd(query, results)) {
99          QString field;
100          QStringList fieldlist;
101          /* Iterate through the lines of results. */
102          foreach (QString resultline, results) {
103             fieldlist = resultline.split("\t");
104             volumeList.append(fieldlist[0]);
105          } /* foreach resultline */
106       } /* if results from query */
107       volumeComboBox->addItem("Any");
108       volumeComboBox->addItems(volumeList);
109       int volumeIndex = volumeComboBox->findText(m_mediaName, Qt::MatchExactly);
110       if (volumeIndex != -1) {
111          volumeComboBox->setCurrentIndex(volumeIndex);
112       }
113       jobComboBox->addItem("Any");
114       jobComboBox->addItems(m_console->job_list);
115       levelComboBox->addItem("Any");
116       levelComboBox->addItems( QStringList() << "F" << "D" << "I");
117       purgedComboBox->addItem("Any");
118       purgedComboBox->addItems( QStringList() << "0" << "1");
119       statusComboBox->addItem("Any");
120       fileSetComboBox->addItem("Any");
121       fileSetComboBox->addItems(m_console->fileset_list);
122       QString statusQuery("SELECT JobStatusLong FROM Status");
123       if (mainWin->m_sqlDebug) {
124          Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
125       }
126       QStringList statusResults, statusLongList;
127       if (m_console->sql_cmd(statusQuery, statusResults)) {
128          QString field;
129          QStringList fieldlist;
130          /* Iterate through the lines of results. */
131          foreach (QString resultline, statusResults) {
132             fieldlist = resultline.split("\t");
133             statusLongList.append(fieldlist[0]);
134          } /* foreach resultline */
135       } /* if results from statusquery */
136       statusComboBox->addItems(statusLongList);
137    }
138
139    /* Set up query */
140    QString query("");
141    int volumeIndex = volumeComboBox->currentIndex();
142    if (volumeIndex != -1)
143       m_mediaName = volumeComboBox->itemText(volumeIndex);
144    query += "SELECT DISTINCT Job.Jobid AS Id, Job.Name AS JobName, Client.Name AS Client,"
145             " Job.Starttime AS JobStart, Job.Type AS JobType,"
146             " Job.Level AS BackupLevel, Job.Jobfiles AS FileCount,"
147             " Job.JobBytes AS Bytes,"
148             " Job.JobStatus AS Status, Status.JobStatusLong AS StatusLong,"
149             " Job.PurgedFiles AS Purged, FileSet.FileSet"
150             " FROM Job"
151             " LEFT OUTER JOIN Client ON (Client.ClientId=Job.ClientId)"
152             " LEFT OUTER JOIN FileSet ON (FileSet.FileSetId=Job.FileSetId)"
153             " LEFT OUTER JOIN Status ON (Job.JobStatus=Status.JobStatus)"
154             " LEFT OUTER JOIN JobMedia ON (JobMedia.JobId=Job.JobId)"
155             " LEFT OUTER JOIN Media ON (JobMedia.MediaId=Media.MediaId)";
156    QStringList conditions;
157    if (m_mediaName != "Any") {
158       conditions.append("Media.VolumeName='" + m_mediaName + "'");
159    }
160    int clientIndex = clientsComboBox->currentIndex();
161    if (clientIndex != -1)
162       m_clientName = clientsComboBox->itemText(clientIndex);
163    if (m_clientName != "Any") {
164       conditions.append("Client.Name='" + m_clientName + "'");
165    }
166    int jobIndex = jobComboBox->currentIndex();
167    if ((jobIndex != -1) && (jobComboBox->itemText(jobIndex) != "Any")) {
168       conditions.append("Job.Name='" + jobComboBox->itemText(jobIndex) + "'");
169    }
170    int levelIndex = levelComboBox->currentIndex();
171    if ((levelIndex != -1) && (levelComboBox->itemText(levelIndex) != "Any")) {
172       conditions.append("Job.Level='" + levelComboBox->itemText(levelIndex) + "'");
173    }
174    int statusIndex = statusComboBox->currentIndex();
175    if ((statusIndex != -1) && (statusComboBox->itemText(statusIndex) != "Any")) {
176       conditions.append("Status.JobStatusLong='" + statusComboBox->itemText(statusIndex) + "'");
177    }
178    int purgedIndex = purgedComboBox->currentIndex();
179    if ((purgedIndex != -1) && (purgedComboBox->itemText(purgedIndex) != "Any")) {
180       conditions.append("Job.PurgedFiles='" + purgedComboBox->itemText(purgedIndex) + "'");
181    }
182    int fileSetIndex = fileSetComboBox->currentIndex();
183    if ((fileSetIndex != -1) && (fileSetComboBox->itemText(fileSetIndex) != "Any")) {
184       conditions.append("FileSet.FileSet='" + fileSetComboBox->itemText(fileSetIndex) + "'");
185    }
186    /* If Limit check box For limit by days is checked  */
187    if (daysCheckBox->checkState() == Qt::Checked) {
188       QDateTime stamp = QDateTime::currentDateTime().addDays(-daysSpinBox->value());
189       QString since = stamp.toString(Qt::ISODate);
190       conditions.append("Job.Starttime>'" + since + "'");
191    }
192    bool first = true;
193    foreach (QString condition, conditions) {
194       if (first) {
195          query += " WHERE " + condition;
196          first = false;
197       } else {
198          query += " AND " + condition;
199       }
200    }
201    /* Descending */
202    query += " ORDER BY Job.Starttime DESC, Job.JobId DESC";
203    /* If Limit check box for limit records returned is checked  */
204    if (limitCheckBox->checkState() == Qt::Checked) {
205       QString limit;
206       limit.setNum(limitSpinBox->value());
207       query += " LIMIT " + limit;
208    }
209
210    /* Set up the Header for the table */
211    QStringList headerlist = (QStringList()
212       << "Job Id" << "Job Name" << "Client" << "Job Starttime" << "Job Type" 
213       << "Job Level" << "Job Files" << "Job Bytes" << "Job Status"  << "Purged" << "File Set" );
214    m_purgedIndex = headerlist.indexOf("Purged");
215    m_typeIndex = headerlist.indexOf("Job Type");
216    m_statusIndex = headerlist.indexOf("Job Status");
217
218    /* Initialize the QTableWidget */
219    m_checkCurrentWidget = false;
220    mp_tableWidget->clear();
221    m_checkCurrentWidget = true;
222    mp_tableWidget->setColumnCount(headerlist.size());
223    mp_tableWidget->setHorizontalHeaderLabels(headerlist);
224
225    if (mainWin->m_sqlDebug) {
226       Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
227    }
228    if (m_console->sql_cmd(query, results)) {
229       m_resultCount = results.count();
230
231       QTableWidgetItem* p_tableitem;
232       QString field;
233       QStringList fieldlist;
234       mp_tableWidget->setRowCount(results.size());
235
236       int row = 0;
237       /* Iterate through the record returned from the query */
238       foreach (resultline, results) {
239          fieldlist = resultline.split("\t");
240          int column = 0;
241          bool m_statusIndexDone = false;
242          QString statusCode("");
243          /* Iterate through fields in the record */
244          foreach (field, fieldlist) {
245             field = field.trimmed();  /* strip leading & trailing spaces */
246             if ((column == m_statusIndex) && (!m_statusIndexDone)){
247                m_statusIndexDone = true;
248                statusCode = field;
249             } else {
250                p_tableitem = new QTableWidgetItem(field,1);
251                p_tableitem->setFlags(0);
252                p_tableitem->setForeground(blackBrush);
253                mp_tableWidget->setItem(row, column, p_tableitem);
254                if (column == m_statusIndex)
255                   setStatusColor(p_tableitem, statusCode);
256                column++;
257             }
258          }
259          row++;
260       }
261    } 
262    /* Resize the columns */
263    mp_tableWidget->resizeColumnsToContents();
264    mp_tableWidget->resizeRowsToContents();
265    mp_tableWidget->verticalHeader()->hide();
266    if ((m_mediaName != "Any") && (m_resultCount == 0)){
267       /* for context sensitive searches, let the user know if there were no
268        * results */
269       QMessageBox::warning(this, tr("Bat"),
270           tr("The Jobs query returned no results.\n"
271          "Press OK to continue?"), QMessageBox::Ok );
272    }
273 }
274
275 void JobList::setStatusColor(QTableWidgetItem *item, QString &field)
276 {
277    QString greenchars("TCR");
278    QString redchars("BEf");
279    QString yellowchars("eDAFSMmsjdctp");
280    if (greenchars.contains(field, Qt::CaseSensitive)) {
281       item->setBackground(Qt::green);
282    } else if (redchars.contains(field, Qt::CaseSensitive)) {
283       item->setBackground(Qt::red);
284    } else if (yellowchars.contains(field, Qt::CaseSensitive)){ 
285       item->setBackground(Qt::yellow);
286    }
287 }
288
289 /*
290  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
291  * The tree has been populated.
292  */
293 void JobList::PgSeltreeWidgetClicked()
294 {
295    if (!m_populated) {
296       populateTable();
297       m_populated=true;
298    }
299 }
300
301 /*
302  *  Virtual function override of pages function which is called when this page
303  *  is visible on the stack
304  */
305 void JobList::currentStackItem()
306 {
307    populateTable();
308    if (!m_populated) {
309       m_populated=true;
310    }
311 }
312
313 /*
314  * Virtual Function to return the name for the medialist tree widget
315  */
316 void JobList::treeWidgetName(QString &desc)
317 {
318    if ((m_mediaName == "") && (m_clientName == "")) {
319       desc = "JobList";
320    } else {
321       desc = "JobList ";
322       if (m_mediaName != "" ) {
323          desc += "of Volume " + m_mediaName;
324       }
325       if (m_clientName != "" ) {
326          desc += "of Client " + m_clientName;
327       }
328    }
329 }
330
331 /*
332  * This functions much line tableItemChanged for trees like the page selector,
333  * but I will do much less here
334  */
335 void JobList::tableItemChanged(QTableWidgetItem *currentItem, QTableWidgetItem * /*previousItem*/)
336 {
337    if (m_checkCurrentWidget) {
338       int row = currentItem->row();
339       QTableWidgetItem* jobitem = mp_tableWidget->item(row, 0);
340       m_currentJob = jobitem->text();
341
342       /* include purged action or not */
343       jobitem = mp_tableWidget->item(row, m_purgedIndex);
344       QString purged = jobitem->text();
345       mp_tableWidget->removeAction(actionPurgeFiles);
346       if (purged == "0") {
347          mp_tableWidget->addAction(actionPurgeFiles);
348       }
349       /* include restore from time and job action or not */
350       jobitem = mp_tableWidget->item(row, m_typeIndex);
351       QString type = jobitem->text();
352       mp_tableWidget->removeAction(actionRestoreFromJob);
353       mp_tableWidget->removeAction(actionRestoreFromTime);
354       if (type == "B") {
355          mp_tableWidget->addAction(actionRestoreFromJob);
356          mp_tableWidget->addAction(actionRestoreFromTime);
357       }
358       /* include cancel action or not */
359       jobitem = mp_tableWidget->item(row, m_statusIndex);
360       QString status = jobitem->text();
361       mp_tableWidget->removeAction(actionCancelJob);
362       if (status == "Running") {
363          mp_tableWidget->addAction(actionCancelJob);
364       }
365    }
366 }
367
368 /*
369  * Function to create connections for context sensitive menu for this and
370  * the page selector
371  */
372 void JobList::createConnections()
373 {
374    /* connect to the action specific to this pages class that shows up in the 
375     * page selector tree */
376    connect(actionRefreshJobList, SIGNAL(triggered()), this,
377                 SLOT(populateTable()));
378    connect(refreshButton, SIGNAL(pressed()), this, SLOT(populateTable()));
379    /* for the tableItemChanged to maintain m_currentJob */
380    connect(mp_tableWidget, SIGNAL(
381            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
382            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
383
384    /* Do what is required for the local context sensitive menu */
385
386
387    /* setContextMenuPolicy is required */
388    mp_tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
389
390    /* Add Actions */
391    mp_tableWidget->addAction(actionRefreshJobList);
392    mp_tableWidget->addAction(actionListJobid);
393    mp_tableWidget->addAction(actionListFilesOnJob);
394    mp_tableWidget->addAction(actionListJobMedia);
395    mp_tableWidget->addAction(actionListVolumes);
396    mp_tableWidget->addAction(actionDeleteJob);
397    mp_tableWidget->addAction(actionPurgeFiles);
398    mp_tableWidget->addAction(actionRestoreFromJob);
399    mp_tableWidget->addAction(actionRestoreFromTime);
400    mp_tableWidget->addAction(actionShowLogForJob);
401
402    /* Make Connections */
403    connect(actionListJobid, SIGNAL(triggered()), this,
404                 SLOT(consoleListJobid()));
405    connect(actionListFilesOnJob, SIGNAL(triggered()), this,
406                 SLOT(consoleListFilesOnJob()));
407    connect(actionListJobMedia, SIGNAL(triggered()), this,
408                 SLOT(consoleListJobMedia()));
409    connect(actionListVolumes, SIGNAL(triggered()), this,
410                 SLOT(consoleListVolumes()));
411    connect(actionDeleteJob, SIGNAL(triggered()), this,
412                 SLOT(consoleDeleteJob()));
413    connect(actionPurgeFiles, SIGNAL(triggered()), this,
414                 SLOT(consolePurgeFiles()));
415    connect(actionRestoreFromJob, SIGNAL(triggered()), this,
416                 SLOT(preRestoreFromJob()));
417    connect(actionRestoreFromTime, SIGNAL(triggered()), this,
418                 SLOT(preRestoreFromTime()));
419    connect(actionShowLogForJob, SIGNAL(triggered()), this,
420                 SLOT(showLogForJob()));
421    connect(actionCancelJob, SIGNAL(triggered()), this,
422                 SLOT(consoleCancelJob()));
423    connect(actionListJobTotals, SIGNAL(triggered()), this,
424                 SLOT(consoleListJobTotals()));
425
426    m_contextActions.append(actionRefreshJobList);
427    m_contextActions.append(actionListJobTotals);
428 }
429
430 /*
431  * Functions to respond to local context sensitive menu sending console commands
432  * If I could figure out how to make these one function passing a string, Yaaaaaa
433  */
434 void JobList::consoleListJobid()
435 {
436    QString cmd("list jobid=");
437    cmd += m_currentJob;
438    if (mainWin->m_longList) { cmd.prepend("l"); }
439    consoleCommand(cmd);
440 }
441 void JobList::consoleListFilesOnJob()
442 {
443    QString cmd("list files jobid=");
444    cmd += m_currentJob;
445    if (mainWin->m_longList) { cmd.prepend("l"); }
446    consoleCommand(cmd);
447 }
448 void JobList::consoleListJobMedia()
449 {
450    QString cmd("list jobmedia jobid=");
451    cmd += m_currentJob;
452    if (mainWin->m_longList) { cmd.prepend("l"); }
453    consoleCommand(cmd);
454 }
455 void JobList::consoleListVolumes()
456 {
457    QString cmd("list volumes jobid=");
458    cmd += m_currentJob;
459    if (mainWin->m_longList) { cmd.prepend("l"); }
460    consoleCommand(cmd);
461 }
462 void JobList::consoleListJobTotals()
463 {
464    QString cmd("list jobtotals");
465    cmd += m_currentJob;
466    if (mainWin->m_longList) { cmd.prepend("l"); }
467    consoleCommand(cmd);
468 }
469 void JobList::consoleDeleteJob()
470 {
471    if (QMessageBox::warning(this, tr("Bat"),
472       tr("Are you sure you want to delete??  !!!.\n"
473 "This delete command is used to delete a Job record and all associated catalog"
474 " records that were created. This command operates only on the Catalog"
475 " database and has no effect on the actual data written to a Volume. This"
476 " command can be dangerous and we strongly recommend that you do not use"
477 " it unless you know what you are doing.  The Job and all its associated"
478 " records (File and JobMedia) will be deleted from the catalog."
479       "Press OK to proceed with delete operation.?"),
480       QMessageBox::Ok | QMessageBox::Cancel)
481       == QMessageBox::Cancel) { return; }
482
483    QString cmd("delete job jobid=");
484    cmd += m_currentJob;
485    consoleCommand(cmd);
486 }
487 void JobList::consolePurgeFiles()
488 {
489    if (QMessageBox::warning(this, tr("Bat"),
490       tr("Are you sure you want to purge ??  !!!.\n"
491 "The Purge command will delete associated Catalog database records from Jobs and"
492 " Volumes without considering the retention period. Purge  works only on the"
493 " Catalog database and does not affect data written to Volumes. This command can"
494 " be dangerous because you can delete catalog records associated with current"
495 " backups of files, and we recommend that you do not use it unless you know what"
496 " you are doing.\n"
497       "Press OK to proceed with the purge operation?"),
498       QMessageBox::Ok | QMessageBox::Cancel)
499       == QMessageBox::Cancel) { return; }
500
501    QString cmd("purge files jobid=");
502    cmd += m_currentJob;
503    consoleCommand(cmd);
504 }
505
506 /*
507  * Subroutine to call preRestore to restore from a select job
508  */
509 void JobList::preRestoreFromJob()
510 {
511    new prerestorePage(m_currentJob, R_JOBIDLIST);
512 }
513
514 /*
515  * Subroutine to call preRestore to restore from a select job
516  */
517 void JobList::preRestoreFromTime()
518 {
519    new prerestorePage(m_currentJob, R_JOBDATETIME);
520 }
521
522 /*
523  * Subroutine to call class to show the log in the database from that job
524  */
525 void JobList::showLogForJob()
526 {
527    QTreeWidgetItem* pageSelectorTreeWidgetItem = mainWin->getFromHash(this);
528    new JobLog(m_currentJob, pageSelectorTreeWidgetItem);
529 }
530
531 /*
532  * Cancel a running job
533  */
534 void JobList::consoleCancelJob()
535 {
536    QString cmd("cancel jobid=");
537    cmd += m_currentJob;
538    consoleCommand(cmd);
539 }