]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/jobs/jobs.cpp
edb621788bc6774605ad97ee9af2f51929031e08
[bacula/bacula] / bacula / src / qt-console / jobs / jobs.cpp
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20  
21 /*
22  *  Jobs Class
23  *
24  *   Dirk Bartley, March 2007
25  */ 
26
27 #include "bat.h"
28 #include "jobs/jobs.h"
29 #include "run/run.h"
30 #include "util/fmtwidgetitem.h"
31
32 Jobs::Jobs() : Pages()
33 {
34    setupUi(this);
35    m_name = tr("Jobs");
36    pgInitialize();
37    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
38    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/run.png")));
39
40    /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
41    m_populated = false;
42    m_checkcurwidget = true;
43    m_closeable = false;
44    /* add context sensitive menu items specific to this classto the page
45     * selector tree. m_contextActions is QList of QActions */
46    m_contextActions.append(actionRefreshJobs);
47    createContextMenu();
48
49    connect(tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)),
50            this, SLOT(runJob()));
51 }
52
53 Jobs::~Jobs()
54 {
55 }
56
57 /*
58  * The main meat of the class!!  The function that querries the director and 
59  * creates the widgets with appropriate values.
60  */
61 void Jobs::populateTable()
62 {
63    m_populated = true;
64    mainWin->waitEnter();
65
66    Freeze frz(*tableWidget); /* disable updating*/
67
68    QBrush blackBrush(Qt::black);
69    m_checkcurwidget = false;
70    tableWidget->clear();
71    m_checkcurwidget = true;
72    QStringList headerlist = (QStringList() << tr("Job Name") 
73       << tr("Pool") << tr("Messages") << tr("Client") 
74       << tr("Storage") << tr("Level") << tr("Type") 
75       << tr("FileSet") << tr("Catalog") << tr("Enabled")
76       << tr("Where"));
77
78    m_typeIndex = headerlist.indexOf(tr("Type"));
79
80    tableWidget->setColumnCount(headerlist.count());
81    tableWidget->setHorizontalHeaderLabels(headerlist);
82    tableWidget->horizontalHeader()->setHighlightSections(false);
83    tableWidget->setRowCount(m_console->job_list.count());
84    tableWidget->verticalHeader()->hide();
85    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
86    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
87    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
88
89    int row = 0;
90    foreach (QString jobName, m_console->job_list){
91       job_defaults job_defs;
92       job_defs.job_name = jobName;
93       if (m_console->get_job_defaults(job_defs)) {
94          int col = 0;
95          TableItemFormatter jobsItem(*tableWidget, row);
96          jobsItem.setTextFld(col++, jobName); 
97          jobsItem.setTextFld(col++, job_defs.pool_name);
98          jobsItem.setTextFld(col++, job_defs.messages_name);
99          jobsItem.setTextFld(col++, job_defs.client_name);
100          jobsItem.setTextFld(col++, job_defs.store_name);
101          jobsItem.setTextFld(col++, job_defs.level);
102          jobsItem.setTextFld(col++, job_defs.type);
103          jobsItem.setTextFld(col++, job_defs.fileset_name);
104          jobsItem.setTextFld(col++, job_defs.catalog_name);
105          jobsItem.setBoolFld(col++, job_defs.enabled);
106          jobsItem.setTextFld(col++, job_defs.where);
107       }
108       row++;
109    }
110    /* set default sorting */
111    tableWidget->sortByColumn(headerlist.indexOf(tr("Job Name")), Qt::AscendingOrder);
112    tableWidget->setSortingEnabled(true);
113    
114    /* Resize rows and columns */
115    tableWidget->resizeColumnsToContents();
116    tableWidget->resizeRowsToContents();
117
118    /* make read only */
119    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
120
121    mainWin->waitExit();
122    dockPage();
123 }
124
125 /*
126  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
127  * The tree has been populated.
128  */
129 void Jobs::PgSeltreeWidgetClicked()
130 {
131    if(!m_populated) {
132       populateTable();
133    }
134 }
135
136 /*
137  * Added to set the context menu policy based on currently active tableWidgetItem
138  * signaled by currentItemChanged
139  */
140 void Jobs::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
141 {
142    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
143    if (m_checkcurwidget && currentwidgetitem) {
144       /* The Previous item */
145       if (previouswidgetitem) { /* avoid a segfault if first time */
146          foreach(QAction* jobAction, tableWidget->actions()) {
147             tableWidget->removeAction(jobAction);
148          }
149       }
150       int currentRow = currentwidgetitem->row();
151       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
152       m_currentlyselected = currentrowzeroitem->text();
153       QTableWidgetItem *currenttypeitem = tableWidget->item(currentRow, m_typeIndex);
154       QString type = currenttypeitem->text();
155
156       if (m_currentlyselected.length() != 0) {
157          /* set a hold variable to the client name in case the context sensitive
158           * menu is used */
159          tableWidget->addAction(actionRefreshJobs);
160          tableWidget->addAction(actionConsoleListFiles);
161          tableWidget->addAction(actionConsoleListVolumes);
162          tableWidget->addAction(actionConsoleListNextVolume);
163          tableWidget->addAction(actionConsoleEnableJob);
164          tableWidget->addAction(actionConsoleDisableJob);
165          tableWidget->addAction(actionConsoleCancel);
166          tableWidget->addAction(actionJobListQuery);
167          tableWidget->addAction(actionRunJob);
168       }
169    }
170 }
171
172 /* 
173  * Setup a context menu 
174  * Made separate from populate so that it would not create context menu over and
175  * over as the table is repopulated.
176  */
177 void Jobs::createContextMenu()
178 {
179    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
180    tableWidget->addAction(actionRefreshJobs);
181    connect(tableWidget, SIGNAL(
182            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
183            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
184    /* connect to the action specific to this pages class */
185    connect(actionRefreshJobs, SIGNAL(triggered()), this,
186                 SLOT(populateTable()));
187    connect(actionConsoleListFiles, SIGNAL(triggered()), this, SLOT(consoleListFiles()));
188    connect(actionConsoleListVolumes, SIGNAL(triggered()), this, SLOT(consoleListVolume()));
189    connect(actionConsoleListNextVolume, SIGNAL(triggered()), this, SLOT(consoleListNextVolume()));
190    connect(actionConsoleEnableJob, SIGNAL(triggered()), this, SLOT(consoleEnable()));
191    connect(actionConsoleDisableJob, SIGNAL(triggered()), this, SLOT(consoleDisable()));
192    connect(actionConsoleCancel, SIGNAL(triggered()), this, SLOT(consoleCancel()));
193    connect(actionJobListQuery, SIGNAL(triggered()), this, SLOT(listJobs()));
194    connect(actionRunJob, SIGNAL(triggered()), this, SLOT(runJob()));
195 }
196
197 /*
198  * Virtual function which is called when this page is visible on the stack
199  */
200 void Jobs::currentStackItem()
201 {
202    if(!m_populated) {
203       /* Create the context menu for the client table */
204       populateTable();
205    }
206 }
207
208 /*
209  * The following functions are slots responding to users clicking on the context
210  * sensitive menu
211  */
212
213 void Jobs::consoleListFiles()
214 {
215    QString cmd = "list files job=\"" + m_currentlyselected + "\"";
216    if (mainWin->m_longList) { cmd.prepend("l"); }
217    consoleCommand(cmd);
218 }
219
220 void Jobs::consoleListVolume()
221 {
222    QString cmd = "list volumes job=\"" + m_currentlyselected + "\"";
223    if (mainWin->m_longList) { cmd.prepend("l"); }
224    consoleCommand(cmd);
225 }
226
227 void Jobs::consoleListNextVolume()
228 {
229    QString cmd = "list nextvolume job=\"" + m_currentlyselected + "\"";
230    if (mainWin->m_longList) { cmd.prepend("l"); }
231    consoleCommand(cmd);
232 }
233
234 void Jobs::consoleEnable()
235 {
236    QString cmd = "enable job=\"" + m_currentlyselected + "\"";
237    consoleCommand(cmd);
238 }
239
240 void Jobs::consoleDisable()
241 {
242    QString cmd = "disable job=\"" + m_currentlyselected + "\"";
243    consoleCommand(cmd);
244 }
245
246 void Jobs::consoleCancel()
247 {
248    QString cmd = "cancel job=\"" + m_currentlyselected + "\"";
249    consoleCommand(cmd);
250 }
251
252 void Jobs::listJobs()
253 {
254    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
255    mainWin->createPageJobList("", "", m_currentlyselected, "", parentItem);
256 }
257
258 /*
259  * Open a new job run page with the currently selected job 
260  * defaulted In
261  */
262 void Jobs::runJob()
263 {
264    new runPage(m_currentlyselected);
265 }