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