]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/status/dirstat.cpp
Make undocking work
[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 two of the GNU 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 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    m_closeable = true;
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) {
102       value = spinBox->value();
103       bool iscurrent = mainWin->tabWidget->currentIndex() == mainWin->tabWidget->indexOf(this);
104       if (((isDocked() && iscurrent) || ((!isDocked()) && isOnceDocked())) && (checkBox->checkState() == Qt::Checked)) {
105          populateAll();
106       }
107    }
108    timerDisplay->display(value);
109 }
110
111 /*
112  * Populate header text widget
113  */
114 void DirStat::populateHeader()
115 {
116    QString command = QString(".status dir header");
117    if (mainWin->m_commandDebug)
118       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
119    QStringList results;
120    textEdit->clear();
121
122    if (m_console->dir_cmd(command, results)) {
123       foreach (QString line, results) {
124          line += "\n";
125          textEdit->insertPlainText(line);
126       }
127    }
128 }
129
130 /*
131  * Populate teminated table
132  */
133 void DirStat::populateTerminated()
134 {
135    QString command = QString(".status dir terminated");
136    if (mainWin->m_commandDebug)
137       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
138    QStringList results;
139    QBrush blackBrush(Qt::black);
140
141    terminatedTable->clear();
142    QStringList headerlist = (QStringList()
143       << tr("Job Id") << tr("Job Level") << tr("Job Files")
144       << tr("Job Bytes") << tr("Job Status") << tr("Job Time") 
145       << tr("Job Name"));
146    QStringList flaglist = (QStringList()
147       << "R" << "L" << "R" << "R" << "LC" 
148       << "L" << "L");
149
150    terminatedTable->setColumnCount(headerlist.size());
151    terminatedTable->setHorizontalHeaderLabels(headerlist);
152
153    if (m_console->dir_cmd(command, results)) {
154       int row = 0;
155       QTableWidgetItem* p_tableitem;
156       terminatedTable->setRowCount(results.size());
157       foreach (QString line, results) {
158          /* Iterate through the record returned from the query */
159          QStringList fieldlist = line.split("\t");
160          int column = 0;
161          QString statusCode("");
162          /* Iterate through fields in the record */
163          foreach (QString field, fieldlist) {
164             field = field.trimmed();  /* strip leading & trailing spaces */
165             p_tableitem = new QTableWidgetItem(field, 1);
166             p_tableitem->setForeground(blackBrush);
167             p_tableitem->setFlags(0);
168             if (flaglist[column].contains("R"))
169                p_tableitem->setTextAlignment(Qt::AlignRight);
170             if (flaglist[column].contains("C")) {
171                if (field == "OK")
172                   p_tableitem->setBackground(Qt::green);
173                else
174                   p_tableitem->setBackground(Qt::red);
175             }
176             terminatedTable->setItem(results.size() - row - 1, column, p_tableitem);
177             column += 1;
178          }
179          row += 1;
180       }
181    }
182    terminatedTable->resizeColumnsToContents();
183    terminatedTable->resizeRowsToContents();
184    terminatedTable->verticalHeader()->hide();
185 }
186
187 /*
188  * Populate scheduled table
189  */
190 void DirStat::populateScheduled()
191 {
192    QString command = QString(".status dir scheduled");
193    if (mainWin->m_commandDebug)
194       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
195    QStringList results;
196    QBrush blackBrush(Qt::black);
197
198    scheduledTable->clear();
199    QStringList headerlist = (QStringList()
200       << tr("Job Level") << tr("Job Type") << tr("Priority") << tr("Job Time") 
201       << tr("Job Name") << tr("Volume"));
202    QStringList flaglist = (QStringList()
203       << "L" << "L" << "R" << "L" << "L" << "L");
204
205    scheduledTable->setColumnCount(headerlist.size());
206    scheduledTable->setHorizontalHeaderLabels(headerlist);
207    scheduledTable->setSelectionBehavior(QAbstractItemView::SelectRows);
208    scheduledTable->setSelectionMode(QAbstractItemView::SingleSelection);
209
210    if (m_console->dir_cmd(command, results)) {
211       int row = 0;
212       QTableWidgetItem* p_tableitem;
213       scheduledTable->setRowCount(results.size());
214       foreach (QString line, results) {
215          /* Iterate through the record returned from the query */
216          QStringList fieldlist = line.split("\t");
217          int column = 0;
218          QString statusCode("");
219          /* Iterate through fields in the record */
220          foreach (QString field, fieldlist) {
221             field = field.trimmed();  /* strip leading & trailing spaces */
222             p_tableitem = new QTableWidgetItem(field, 1);
223             p_tableitem->setForeground(blackBrush);
224             scheduledTable->setItem(row, column, p_tableitem);
225             column += 1;
226          }
227          row += 1;
228       }
229    }
230    scheduledTable->resizeColumnsToContents();
231    scheduledTable->resizeRowsToContents();
232    scheduledTable->verticalHeader()->hide();
233 }
234
235 /*
236  * Populate running table
237  */
238 void DirStat::populateRunning()
239 {
240    QString command = QString(".status dir running");
241    if (mainWin->m_commandDebug)
242       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
243    QStringList results;
244    QBrush blackBrush(Qt::black);
245
246    runningTable->clear();
247    QStringList headerlist = (QStringList()
248       << tr("Job Id") << tr("Job Level") << tr("Job Data") << tr("Job Info"));
249
250    runningTable->setColumnCount(headerlist.size());
251    runningTable->setHorizontalHeaderLabels(headerlist);
252    runningTable->setSelectionBehavior(QAbstractItemView::SelectRows);
253
254    if (m_console->dir_cmd(command, results)) {
255       int row = 0;
256       QTableWidgetItem* p_tableitem;
257       runningTable->setRowCount(results.size());
258       foreach (QString line, results) {
259          /* Iterate through the record returned from the query */
260          QStringList fieldlist = line.split("\t");
261          int column = 0;
262          QString statusCode("");
263          /* Iterate through fields in the record */
264          foreach (QString field, fieldlist) {
265             field = field.trimmed();  /* strip leading & trailing spaces */
266             p_tableitem = new QTableWidgetItem(field, 1);
267             p_tableitem->setForeground(blackBrush);
268             runningTable->setItem(row, column, p_tableitem);
269             column += 1;
270          }
271          row += 1;
272       }
273    }
274    runningTable->resizeColumnsToContents();
275    runningTable->resizeRowsToContents();
276    runningTable->verticalHeader()->hide();
277 }
278
279 /*
280  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
281  * The tree has been populated.
282  */
283 void DirStat::PgSeltreeWidgetClicked()
284 {
285    if (!m_populated) {
286       populateAll();
287       m_populated=true;
288    }
289    if (!isOnceDocked()) {
290       dockPage();
291    }
292 }
293
294 /*
295  *  Virtual function override of pages function which is called when this page
296  *  is visible on the stack
297  */
298 void DirStat::currentStackItem()
299 {
300    populateAll();
301    timerDisplay->display(spinBox->value());
302    if (!m_populated) {
303       m_populated=true;
304    }
305 }
306
307 /*
308  * Function to create connections for context sensitive menu for this and
309  * the page selector
310  */
311 void DirStat::createConnections()
312 {
313    connect(actionRefresh, SIGNAL(triggered()), this, SLOT(populateAll()));
314    connect(actionCancelRunning, SIGNAL(triggered()), this, SLOT(consoleCancelJob()));
315    connect(actionDisableScheduledJob, SIGNAL(triggered()), this, SLOT(consoleDisableJob()));
316    connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTriggered()));
317
318    scheduledTable->setContextMenuPolicy(Qt::ActionsContextMenu);
319    scheduledTable->addAction(actionRefresh);
320    scheduledTable->addAction(actionDisableScheduledJob);
321    terminatedTable->setContextMenuPolicy(Qt::ActionsContextMenu);
322    terminatedTable->addAction(actionRefresh);
323    runningTable->setContextMenuPolicy(Qt::ActionsContextMenu);
324    runningTable->addAction(actionRefresh);
325    runningTable->addAction(actionCancelRunning);
326 }
327
328 /*
329  * Save user settings associated with this page
330  */
331 void DirStat::writeSettings()
332 {
333    QSettings settings(m_console->m_dir->name(), "bat");
334    settings.beginGroup(m_groupText);
335    settings.setValue(m_splitText, splitter->saveState());
336    settings.setValue("refreshInterval", spinBox->value());
337    settings.setValue("refreshCheck", checkBox->checkState());
338    settings.endGroup();
339 }
340
341 /*
342  * Read and restore user settings associated with this page
343  */
344 void DirStat::readSettings()
345 {
346    m_groupText = "DirStatPage";
347    m_splitText = "splitterSizes_0";
348    QSettings settings(m_console->m_dir->name(), "bat");
349    settings.beginGroup(m_groupText);
350    if (settings.contains(m_splitText)) { splitter->restoreState(settings.value(m_splitText).toByteArray()); }
351    spinBox->setValue(settings.value("refreshInterval", 28).toInt());
352    checkBox->setCheckState((Qt::CheckState)settings.value("refreshCheck", Qt::Checked).toInt());
353    settings.endGroup();
354
355    timerDisplay->display(spinBox->value());
356 }
357
358 /*
359  * Cancel a running job
360  */
361 void DirStat::consoleCancelJob()
362 {
363    QList<int> rowList;
364    QList<QTableWidgetItem *> sitems = runningTable->selectedItems();
365    foreach (QTableWidgetItem *sitem, sitems) {
366       int row = sitem->row();
367       if (!rowList.contains(row)) {
368          rowList.append(row);
369       }
370    }
371
372    QStringList selectedJobsList;
373    foreach(int row, rowList) {
374       QTableWidgetItem * sitem = runningTable->item(row, 0);
375       selectedJobsList.append(sitem->text());
376    }
377    foreach( QString job, selectedJobsList )
378    {
379       QString cmd("cancel jobid=");
380       cmd += job;
381       consoleCommand(cmd);
382    }
383 }
384
385 /*
386  * Disable a scheduled Job
387  */
388 void DirStat::consoleDisableJob()
389 {
390    int currentrow = scheduledTable->currentRow();
391    QTableWidgetItem *item = scheduledTable->item(currentrow, 4);
392    if (item) {
393       QString text = item->text();
394       QString cmd("disable job=\"");
395       cmd += text + '"';
396       consoleCommand(cmd);
397    }
398 }