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