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