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