2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
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.
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.
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
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.
29 * Version $Id: joblist.h 4230 2007-02-21 20:07:37Z kerns $
31 * Dirk Bartley, March 2007
34 #include <QAbstractEventDispatcher>
35 #include <QTableWidgetItem>
39 #include "joblog/joblog.h"
42 * Constructor for the class
44 JobList::JobList(QString &mediaName, QString &clientname,
45 QTreeWidgetItem *parentTreeWidgetItem)
49 m_mediaName = mediaName;
50 m_clientName = clientname;
51 pgInitialize(parentTreeWidgetItem);
55 if ((m_mediaName != "") || (m_clientName != "")) { m_closeable=true; }
56 m_checkCurrentWidget = true;
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);
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.
72 void JobList::populateTable()
76 QBrush blackBrush(Qt::black);
78 /* Can't do this in constructor because not neccesarily conected in constructor */
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);
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)) {
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);
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");
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";
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 + "'";
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 + "'";
140 int jobIndex = jobComboBox->currentIndex();
141 if ((jobIndex != -1) && (jobComboBox->itemText(jobIndex) != "Any")) {
142 query += " AND Job.Name='" + jobComboBox->itemText(jobIndex) + "'";
144 int levelIndex = levelComboBox->currentIndex();
145 if ((levelIndex != -1) && (levelComboBox->itemText(levelIndex) != "Any")) {
146 query += " AND Job.Level='" + levelComboBox->itemText(levelIndex) + "'";
148 int statusIndex = statusComboBox->currentIndex();
149 if ((statusIndex != -1) && (statusComboBox->itemText(statusIndex) != "Any")) {
150 query += " AND Job.JobStatus='" + statusComboBox->itemText(statusIndex) + "'";
152 int purgedIndex = purgedComboBox->currentIndex();
153 if ((purgedIndex != -1) && (purgedComboBox->itemText(purgedIndex) != "Any")) {
154 query += " AND Job.PurgedFiles='" + purgedComboBox->itemText(purgedIndex) + "'";
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 + "'";
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) {
167 limit.setNum(limitSpinBox->value());
168 query += " LIMIT " + limit;
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");
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);
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();
190 QTableWidgetItem* p_tableitem;
192 QStringList fieldlist;
193 mp_tableWidget->setRowCount(results.size());
196 /* Iterate through the record returned from the query */
197 foreach (resultline, results) {
198 fieldlist = resultline.split("\t");
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;
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);
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
228 QMessageBox::warning(this, tr("Bat"),
229 tr("The Jobs query returned no results.\n"
230 "Press OK to continue?"), QMessageBox::Ok );
234 void JobList::setStatusColor(QTableWidgetItem *item, QString &field)
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);
249 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
250 * The tree has been populated.
252 void JobList::PgSeltreeWidgetClicked()
261 * Virtual function override of pages function which is called when this page
262 * is visible on the stack
264 void JobList::currentStackItem()
268 m_contextActions.append(actionRefreshJobList);
274 * Virtual Function to return the name for the medialist tree widget
276 void JobList::treeWidgetName(QString &desc)
278 if ((m_mediaName == "") && (m_clientName == "")) {
282 if (m_mediaName != "" ) {
283 desc += "on Volume " + m_mediaName;
285 if (m_clientName != "" ) {
286 desc += "of Client " + m_clientName;
292 * This functions much line tableItemChanged for trees like the page selector,
293 * but I will do much less here
295 void JobList::tableItemChanged(QTableWidgetItem *currentItem, QTableWidgetItem * /*previousItem*/)
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);
305 mp_tableWidget->addAction(actionPurgeFiles);
311 * Function to create connections for context sensitive menu for this and
314 void JobList::createConnections()
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 *)));
326 /* Do what is required for the local context sensitive menu */
329 /* setContextMenuPolicy is required */
330 mp_tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
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);
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()));
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
372 void JobList::consoleLongListJob()
374 QString cmd("llist jobid=");
378 void JobList::consoleListJobid()
380 QString cmd("list jobid=");
384 void JobList::consoleListFilesOnJob()
386 QString cmd("list files jobid=");
390 void JobList::consoleListJobMedia()
392 QString cmd("list jobmedia jobid=");
396 void JobList::consoleListVolumes()
398 QString cmd("list volumes jobid=");
402 void JobList::consoleDeleteJob()
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; }
416 QString cmd("delete job jobid=");
420 void JobList::consolePurgeFiles()
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"
430 "Press OK to proceed with the purge operation?"),
431 QMessageBox::Ok | QMessageBox::Cancel)
432 == QMessageBox::Cancel) { return; }
434 QString cmd("purge files jobid=");
440 * Subroutine to call preRestore to restore from a select job
442 void JobList::preRestoreFromJob()
444 new prerestorePage(m_currentJob, R_JOBIDLIST);
448 * Subroutine to call preRestore to restore from a select job
450 void JobList::preRestoreFromTime()
452 new prerestorePage(m_currentJob, R_JOBDATETIME);
456 * Subroutine to call class to show the log in the database from that job
458 void JobList::showLogForJob()
460 QTreeWidgetItem* pageSelectorTreeWidgetItem = mainWin->getFromHash(this);
461 new JobLog(m_currentJob, pageSelectorTreeWidgetItem);