]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/status/dirstat.cpp
8a9bd6049ff401940e35afd4da58d3f76ee72dae
[bacula/bacula] / bacula / src / qt-console / status / dirstat.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  *   Version $Id: dirstat.cpp 5880 2007-11-09 01:20:40Z bartleyd2 $
30  *
31  *   Dirk Bartley, March 2007
32  */
33  
34 #include "bat.h"
35 #include <QAbstractEventDispatcher>
36 #include <QTableWidgetItem>
37 #include "dirstat.h"
38
39 /*
40  * Constructor for the class
41  */
42 DirStat::DirStat()
43 {
44    setupUi(this);
45    m_name = tr("Director Status");
46    pgInitialize();
47    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
48    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/status.png")));
49    m_cursor = new QTextCursor(textEdit->document());
50
51    m_timer = new QTimer(this);
52    readSettings();
53    m_timer->start(1000);
54
55    createConnections();
56    setCurrent();
57 }
58
59 void DirStat::getFont()
60 {
61    QFont font = textEdit->font();
62
63    QString dirname;
64    m_console->getDirResName(dirname);
65    QSettings settings(dirname, "bat");
66    settings.beginGroup("Console");
67    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
68    font.setPointSize(settings.value("consolePointSize", 10).toInt());
69    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
70    settings.endGroup();
71    textEdit->setFont(font);
72 }
73
74 /*
75  * Write the m_splitter settings in the destructor
76  */
77 DirStat::~DirStat()
78 {
79    writeSettings();
80 }
81
82 /*
83  * Populate all tables and header widgets
84  */
85 void DirStat::populateAll()
86 {
87    populateHeader();
88    populateTerminated();
89    populateScheduled();
90    populateRunning();
91 }
92
93 /*
94  *  Timer is triggered, see if is current and repopulate.
95  */
96 void DirStat::timerTriggered()
97 {
98    double value = timerDisplay->value();
99    value -= 1;
100    if (value == 0) {
101       value = spinBox->value();
102       bool iscurrent = mainWin->tabWidget->currentIndex() == mainWin->tabWidget->indexOf(this);
103       if (((isDocked() && iscurrent) || ((!isDocked()) && isOnceDocked())) && (checkBox->checkState() == Qt::Checked)) {
104          populateAll();
105       }
106    }
107    timerDisplay->display(value);
108 }
109
110 /*
111  * Populate header text widget
112  */
113 void DirStat::populateHeader()
114 {
115    QString command = QString(".status dir header");
116    if (mainWin->m_commandDebug)
117       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
118    QStringList results;
119    textEdit->clear();
120
121    if (m_console->dir_cmd(command, results)) {
122       foreach (QString line, results) {
123          line += "\n";
124          textEdit->insertPlainText(line);
125       }
126    }
127 }
128
129 /*
130  * Populate teminated table
131  */
132 void DirStat::populateTerminated()
133 {
134    QString command = QString(".status dir terminated");
135    if (mainWin->m_commandDebug)
136       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
137    QStringList results;
138    QBrush blackBrush(Qt::black);
139
140    terminatedTable->clear();
141    QStringList headerlist = (QStringList()
142       << tr("Job Id") << tr("Job Level") << tr("Job Files")
143       << tr("Job Bytes") << tr("Job Status") << tr("Job Time") 
144       << tr("Job Name"));
145    QStringList flaglist = (QStringList()
146       << "R" << "L" << "R" << "R" << "LC" 
147       << "L" << "L");
148
149    terminatedTable->setColumnCount(headerlist.size());
150    terminatedTable->setHorizontalHeaderLabels(headerlist);
151
152    if (m_console->dir_cmd(command, results)) {
153       int row = 0;
154       QTableWidgetItem* p_tableitem;
155       terminatedTable->setRowCount(results.size());
156       foreach (QString line, results) {
157          /* Iterate through the record returned from the query */
158          QStringList fieldlist = line.split("\t");
159          int column = 0;
160          QString statusCode("");
161          /* Iterate through fields in the record */
162          foreach (QString field, fieldlist) {
163             field = field.trimmed();  /* strip leading & trailing spaces */
164             p_tableitem = new QTableWidgetItem(field, 1);
165             p_tableitem->setForeground(blackBrush);
166             p_tableitem->setFlags(0);
167             if (flaglist[column].contains("R"))
168                p_tableitem->setTextAlignment(Qt::AlignRight);
169             if (flaglist[column].contains("C")) {
170                if (field == "OK")
171                   p_tableitem->setBackground(Qt::green);
172                else
173                   p_tableitem->setBackground(Qt::red);
174             }
175             terminatedTable->setItem(results.size() - row - 1, column, p_tableitem);
176             column += 1;
177          }
178          row += 1;
179       }
180    }
181    terminatedTable->resizeColumnsToContents();
182    terminatedTable->resizeRowsToContents();
183    terminatedTable->verticalHeader()->hide();
184 }
185
186 /*
187  * Populate scheduled table
188  */
189 void DirStat::populateScheduled()
190 {
191    QString command = QString(".status dir scheduled");
192    if (mainWin->m_commandDebug)
193       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
194    QStringList results;
195    QBrush blackBrush(Qt::black);
196
197    scheduledTable->clear();
198    QStringList headerlist = (QStringList()
199       << tr("Job Level") << tr("Job Type") << tr("Priority") << tr("Job Time") 
200       << tr("Job Name") << tr("Volume"));
201    QStringList flaglist = (QStringList()
202       << "L" << "L" << "R" << "L" << "L" << "L");
203
204    scheduledTable->setColumnCount(headerlist.size());
205    scheduledTable->setHorizontalHeaderLabels(headerlist);
206    scheduledTable->setSelectionBehavior(QAbstractItemView::SelectRows);
207    scheduledTable->setSelectionMode(QAbstractItemView::SingleSelection);
208
209    if (m_console->dir_cmd(command, results)) {
210       int row = 0;
211       QTableWidgetItem* p_tableitem;
212       scheduledTable->setRowCount(results.size());
213       foreach (QString line, results) {
214          /* Iterate through the record returned from the query */
215          QStringList fieldlist = line.split("\t");
216          int column = 0;
217          QString statusCode("");
218          /* Iterate through fields in the record */
219          foreach (QString field, fieldlist) {
220             field = field.trimmed();  /* strip leading & trailing spaces */
221             p_tableitem = new QTableWidgetItem(field, 1);
222             p_tableitem->setForeground(blackBrush);
223             scheduledTable->setItem(row, column, p_tableitem);
224             column += 1;
225          }
226          row += 1;
227       }
228    }
229    scheduledTable->resizeColumnsToContents();
230    scheduledTable->resizeRowsToContents();
231    scheduledTable->verticalHeader()->hide();
232 }
233
234 /*
235  * Populate running table
236  */
237 void DirStat::populateRunning()
238 {
239    QString command = QString(".status dir running");
240    if (mainWin->m_commandDebug)
241       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
242    QStringList results;
243    QBrush blackBrush(Qt::black);
244
245    runningTable->clear();
246    QStringList headerlist = (QStringList()
247       << tr("Job Id") << tr("Job Level") << tr("Job Data") << tr("Job Info"));
248
249    runningTable->setColumnCount(headerlist.size());
250    runningTable->setHorizontalHeaderLabels(headerlist);
251    runningTable->setSelectionBehavior(QAbstractItemView::SelectRows);
252
253    if (m_console->dir_cmd(command, results)) {
254       int row = 0;
255       QTableWidgetItem* p_tableitem;
256       runningTable->setRowCount(results.size());
257       foreach (QString line, results) {
258          /* Iterate through the record returned from the query */
259          QStringList fieldlist = line.split("\t");
260          int column = 0;
261          QString statusCode("");
262          /* Iterate through fields in the record */
263          foreach (QString field, fieldlist) {
264             field = field.trimmed();  /* strip leading & trailing spaces */
265             p_tableitem = new QTableWidgetItem(field, 1);
266             p_tableitem->setForeground(blackBrush);
267             runningTable->setItem(row, column, p_tableitem);
268             column += 1;
269          }
270          row += 1;
271       }
272    }
273    runningTable->resizeColumnsToContents();
274    runningTable->resizeRowsToContents();
275    runningTable->verticalHeader()->hide();
276 }
277
278 /*
279  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
280  * The tree has been populated.
281  */
282 void DirStat::PgSeltreeWidgetClicked()
283 {
284    if (!m_populated) {
285       populateAll();
286       m_populated=true;
287    }
288    if (!isOnceDocked()) {
289       dockPage();
290    }
291 }
292
293 /*
294  *  Virtual function override of pages function which is called when this page
295  *  is visible on the stack
296  */
297 void DirStat::currentStackItem()
298 {
299    populateAll();
300    timerDisplay->display(spinBox->value());
301    if (!m_populated) {
302       m_populated=true;
303    }
304 }
305
306 /*
307  * Function to create connections for context sensitive menu for this and
308  * the page selector
309  */
310 void DirStat::createConnections()
311 {
312    connect(actionRefresh, SIGNAL(triggered()), this, SLOT(populateAll()));
313    connect(actionCancelRunning, SIGNAL(triggered()), this, SLOT(consoleCancelJob()));
314    connect(actionDisableScheduledJob, SIGNAL(triggered()), this, SLOT(consoleDisableJob()));
315    connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTriggered()));
316
317    scheduledTable->setContextMenuPolicy(Qt::ActionsContextMenu);
318    scheduledTable->addAction(actionRefresh);
319    scheduledTable->addAction(actionDisableScheduledJob);
320    terminatedTable->setContextMenuPolicy(Qt::ActionsContextMenu);
321    terminatedTable->addAction(actionRefresh);
322    runningTable->setContextMenuPolicy(Qt::ActionsContextMenu);
323    runningTable->addAction(actionRefresh);
324    runningTable->addAction(actionCancelRunning);
325 }
326
327 /*
328  * Save user settings associated with this page
329  */
330 void DirStat::writeSettings()
331 {
332    QSettings settings(m_console->m_dir->name(), "bat");
333    settings.beginGroup(m_groupText);
334    settings.setValue(m_splitText, splitter->saveState());
335    settings.setValue("refreshInterval", spinBox->value());
336    settings.setValue("refreshCheck", checkBox->checkState());
337    settings.endGroup();
338 }
339
340 /*
341  * Read and restore user settings associated with this page
342  */
343 void DirStat::readSettings()
344 {
345    m_groupText = "DirStatPage";
346    m_splitText = "splitterSizes_0";
347    QSettings settings(m_console->m_dir->name(), "bat");
348    settings.beginGroup(m_groupText);
349    if (settings.contains(m_splitText)) { splitter->restoreState(settings.value(m_splitText).toByteArray()); }
350    spinBox->setValue(settings.value("refreshInterval", 28).toInt());
351    checkBox->setCheckState((Qt::CheckState)settings.value("refreshCheck", Qt::Checked).toInt());
352    settings.endGroup();
353
354    timerDisplay->display(spinBox->value());
355 }
356
357 /*
358  * Cancel a running job
359  */
360 void DirStat::consoleCancelJob()
361 {
362    QList<int> rowList;
363    QList<QTableWidgetItem *> sitems = runningTable->selectedItems();
364    foreach (QTableWidgetItem *sitem, sitems) {
365       int row = sitem->row();
366       if (!rowList.contains(row)) {
367          rowList.append(row);
368       }
369    }
370
371    QStringList selectedJobsList;
372    foreach(int row, rowList) {
373       QTableWidgetItem * sitem = runningTable->item(row, 0);
374       selectedJobsList.append(sitem->text());
375    }
376    foreach( QString job, selectedJobsList )
377    {
378       QString cmd("cancel jobid=");
379       cmd += job;
380       consoleCommand(cmd);
381    }
382 }
383
384 /*
385  * Disable a scheduled Job
386  */
387 void DirStat::consoleDisableJob()
388 {
389    int currentrow = scheduledTable->currentRow();
390    QTableWidgetItem *item = scheduledTable->item(currentrow, 4);
391    if (item) {
392       QString text = item->text();
393       QString cmd("disable job=\"");
394       cmd += text + '"';
395       consoleCommand(cmd);
396    }
397 }