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