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