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