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