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