]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/jobs/jobs.cpp
Make the jobs interface a table instead of a tree.
[bacula/bacula] / bacula / src / qt-console / jobs / jobs.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 and included
11    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$
31  *
32  *  Jobs Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 #include "bat.h"
39 #include "jobs/jobs.h"
40 #include "run/run.h"
41
42 Jobs::Jobs()
43 {
44    setupUi(this);
45    m_name = tr("Jobs");
46    pgInitialize();
47    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
48    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/run.png")));
49
50    /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
51    m_populated = false;
52    m_checkcurwidget = true;
53    m_closeable = false;
54    /* add context sensitive menu items specific to this classto the page
55     * selector tree. m_contextActions is QList of QActions */
56    m_contextActions.append(actionRefreshJobs);
57    createContextMenu();
58    dockPage();
59 }
60
61 Jobs::~Jobs()
62 {
63 }
64
65 /*
66  * The main meat of the class!!  The function that querries the director and 
67  * creates the widgets with appropriate values.
68  */
69 void Jobs::populateTable()
70 {
71    QTableWidgetItem *tableItem;
72    QBrush blackBrush(Qt::black);
73
74    if (!m_console->preventInUseConnect())
75       return;
76    m_checkcurwidget = false;
77    tableWidget->clear();
78    m_checkcurwidget = true;
79    QStringList headerlist = (QStringList() << tr("Job Name") << tr("Pool") << tr("Messages")
80       << tr("Client") << tr("Storage") << tr("Where") << tr("Level") << tr("Type") 
81       << tr("FileSet") 
82       << tr("Catalog") << tr("Enabled"));
83
84    m_typeIndex = headerlist.indexOf(tr("Type"));
85
86    tableWidget->setColumnCount(headerlist.count());
87    tableWidget->setHorizontalHeaderLabels(headerlist);
88    tableWidget->setRowCount(m_console->job_list.count());
89    tableWidget->verticalHeader()->hide();
90    int row = 0;
91
92
93    foreach (QString jobName, m_console->job_list){
94       job_defaults job_defs;
95       job_defs.job_name = jobName;
96       if (m_console->get_job_defaults(job_defs)) {
97
98         for (int column=0; column<headerlist.count(); column++) {
99             tableItem = new QTableWidgetItem(1);
100             if (column == 0) 
101                tableItem->setText(job_defs.job_name);
102             if (column == 1) 
103                tableItem->setText(job_defs.pool_name);
104             if (column == 2) 
105                tableItem->setText(job_defs.messages_name);
106             if (column == 3) 
107                tableItem->setText(job_defs.client_name);
108             if (column == 4) 
109                tableItem->setText(job_defs.store_name);
110             if (column == 5) 
111                tableItem->setText(job_defs.where);
112             if (column == 6) 
113                tableItem->setText(job_defs.level);
114             if (column == 7) 
115                tableItem->setText(job_defs.type);
116             if (column == 8) 
117                tableItem->setText(job_defs.fileset_name);
118             if (column == 9) 
119                tableItem->setText(job_defs.catalog_name);
120             if (column == 10) 
121                if (job_defs.enabled)
122                   tableItem->setText("Yes");
123                else
124                   tableItem->setText("No");
125             /* tableItem->setFlags(Qt::ItemIsSelectable); */
126             tableItem->setForeground(blackBrush);
127             tableWidget->setItem(row, column, tableItem);
128          }
129       }
130       row++;
131    }
132    /* Resize the columns */
133    for(int cnter=0; cnter<headerlist.size(); cnter++) {
134       tableWidget->resizeColumnToContents(cnter);
135    }
136 }
137
138 /*
139  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
140  * The tree has been populated.
141  */
142 void Jobs::PgSeltreeWidgetClicked()
143 {
144    if(!m_populated) {
145       populateTable();
146       m_populated=true;
147    }
148 }
149
150 /*
151  * Added to set the context menu policy based on currently active tableWidgetItem
152  * signaled by currentItemChanged
153  */
154 void Jobs::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
155 {
156    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
157    if (m_checkcurwidget) {
158       /* The Previous item */
159       if (previouswidgetitem) { /* avoid a segfault if first time */
160          foreach(QAction* jobAction, tableWidget->actions()) {
161             tableWidget->removeAction(jobAction);
162          }
163       }
164       int currentRow = currentwidgetitem->row();
165       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
166       m_currentlyselected = currentrowzeroitem->text();
167       QTableWidgetItem *currenttypeitem = tableWidget->item(currentRow, m_typeIndex);
168       QString type = currenttypeitem->text();
169
170       if (m_currentlyselected.length() != 0) {
171          /* set a hold variable to the client name in case the context sensitive
172           * menu is used */
173          tableWidget->addAction(actionConsoleListFiles);
174          tableWidget->addAction(actionConsoleListVolumes);
175          tableWidget->addAction(actionConsoleListNextVolume);
176          tableWidget->addAction(actionConsoleEnableJob);
177          tableWidget->addAction(actionConsoleDisableJob);
178          tableWidget->addAction(actionConsoleCancel);
179          tableWidget->addAction(actionJobListQuery);
180          if (type == tr("Backup"))
181             tableWidget->addAction(actionRunJob);
182       }
183    }
184 }
185
186 /* 
187  * Setup a context menu 
188  * Made separate from populate so that it would not create context menu over and
189  * over as the table is repopulated.
190  */
191 void Jobs::createContextMenu()
192 {
193    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
194    tableWidget->addAction(actionRefreshJobs);
195    connect(tableWidget, SIGNAL(
196            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
197            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
198    /* connect to the action specific to this pages class */
199    connect(actionRefreshJobs, SIGNAL(triggered()), this,
200                 SLOT(populateTable()));
201    connect(actionConsoleListFiles, SIGNAL(triggered()), this, SLOT(consoleListFiles()));
202    connect(actionConsoleListVolumes, SIGNAL(triggered()), this, SLOT(consoleListVolume()));
203    connect(actionConsoleListNextVolume, SIGNAL(triggered()), this, SLOT(consoleListNextVolume()));
204    connect(actionConsoleEnableJob, SIGNAL(triggered()), this, SLOT(consoleEnable()));
205    connect(actionConsoleDisableJob, SIGNAL(triggered()), this, SLOT(consoleDisable()));
206    connect(actionConsoleCancel, SIGNAL(triggered()), this, SLOT(consoleCancel()));
207    connect(actionJobListQuery, SIGNAL(triggered()), this, SLOT(listJobs()));
208    connect(actionRunJob, SIGNAL(triggered()), this, SLOT(runJob()));
209 }
210
211 /*
212  * Virtual function which is called when this page is visible on the stack
213  */
214 void Jobs::currentStackItem()
215 {
216    populateTable();
217    if(!m_populated) {
218       /* Create the context menu for the client table */
219       m_populated=true;
220    }
221 }
222
223 /*
224  * The following functions are slots responding to users clicking on the context
225  * sensitive menu
226  */
227
228 void Jobs::consoleListFiles()
229 {
230    QString cmd = "list files job=\"" + m_currentlyselected + "\"";
231    if (mainWin->m_longList) { cmd.prepend("l"); }
232    consoleCommand(cmd);
233 }
234
235 void Jobs::consoleListVolume()
236 {
237    QString cmd = "list volumes job=\"" + m_currentlyselected + "\"";
238    if (mainWin->m_longList) { cmd.prepend("l"); }
239    consoleCommand(cmd);
240 }
241
242 void Jobs::consoleListNextVolume()
243 {
244    QString cmd = "list nextvolume job=\"" + m_currentlyselected + "\"";
245    if (mainWin->m_longList) { cmd.prepend("l"); }
246    consoleCommand(cmd);
247 }
248
249 void Jobs::consoleEnable()
250 {
251    QString cmd = "enable job=\"" + m_currentlyselected + "\"";
252    consoleCommand(cmd);
253 }
254
255 void Jobs::consoleDisable()
256 {
257    QString cmd = "disable job=\"" + m_currentlyselected + "\"";
258    consoleCommand(cmd);
259 }
260
261 void Jobs::consoleCancel()
262 {
263    QString cmd = "cancel job=\"" + m_currentlyselected + "\"";
264    consoleCommand(cmd);
265 }
266
267 void Jobs::listJobs()
268 {
269    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
270    mainWin->createPageJobList("", "", m_currentlyselected, "", parentItem);
271 }
272
273 /*
274  * Open a new job run page with the currentley selected "Backup" job 
275  * defaulted In
276  */
277 void Jobs::runJob()
278 {
279    new runPage(m_currentlyselected);
280 }