]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/status/dirstat.cpp
On vacation I am having a little fun !!
[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    dockPage();
55    m_timer->start(1000);
56
57    createConnections();
58    setCurrent();
59 }
60
61 void DirStat::getFont()
62 {
63    QFont font = textEdit->font();
64
65    QString dirname;
66    m_console->getDirResName(dirname);
67    QSettings settings(dirname, "bat");
68    settings.beginGroup("Console");
69    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
70    font.setPointSize(settings.value("consolePointSize", 10).toInt());
71    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
72    settings.endGroup();
73    textEdit->setFont(font);
74 }
75
76 /*
77  * Write the m_splitter settings in the destructor
78  */
79 DirStat::~DirStat()
80 {
81    writeSettings();
82 }
83
84 /*
85  * Populate all tables and header widgets
86  */
87 void DirStat::populateAll()
88 {
89    populateHeader();
90    populateTerminated();
91    populateScheduled();
92    populateRunning();
93 }
94
95 /*
96  *  Timer is triggered, see if is current and repopulate.
97  */
98 void DirStat::timerTriggered()
99 {
100    double value = timerDisplay->value();
101    value -= 1;
102    if (value == 0) {
103       value = spinBox->value();
104       bool iscurrent = mainWin->stackedWidget->currentIndex() == mainWin->stackedWidget->indexOf(this);
105       if (((isDocked() && iscurrent) || (!isDocked())) && (checkBox->checkState() == Qt::Checked)) {
106          populateAll();
107       }
108    }
109    timerDisplay->display(value);
110 }
111
112 /*
113  * Populate header text widget
114  */
115 void DirStat::populateHeader()
116 {
117    QString command = QString(".status dir header");
118    if (mainWin->m_commandDebug)
119       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
120    QStringList results;
121    textEdit->clear();
122
123    if (m_console->dir_cmd(command, results)) {
124       foreach (QString line, results) {
125          line += "\n";
126          textEdit->insertPlainText(line);
127       }
128    }
129 }
130
131 /*
132  * Populate teminated table
133  */
134 void DirStat::populateTerminated()
135 {
136    QString command = QString(".status dir terminated");
137    if (mainWin->m_commandDebug)
138       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
139    QStringList results;
140    QBrush blackBrush(Qt::black);
141
142    terminatedTable->clear();
143    QStringList headerlist = (QStringList()
144       << tr("Job Id") << tr("Job Level") << tr("Job Files")
145       << tr("Job Bytes") << tr("Job Status") << tr("Job Time") 
146       << tr("Job Name"));
147    QStringList flaglist = (QStringList()
148       << "R" << "L" << "R" << "R" << "LC" 
149       << "L" << "L");
150
151    terminatedTable->setColumnCount(headerlist.size());
152    terminatedTable->setHorizontalHeaderLabels(headerlist);
153
154    if (m_console->dir_cmd(command, results)) {
155       int row = 0;
156       QTableWidgetItem* p_tableitem;
157       terminatedTable->setRowCount(results.size());
158       foreach (QString line, results) {
159          /* Iterate through the record returned from the query */
160          QStringList fieldlist = line.split("\t");
161          int column = 0;
162          QString statusCode("");
163          /* Iterate through fields in the record */
164          foreach (QString field, fieldlist) {
165             field = field.trimmed();  /* strip leading & trailing spaces */
166             p_tableitem = new QTableWidgetItem(field, 1);
167             p_tableitem->setForeground(blackBrush);
168             p_tableitem->setFlags(0);
169             if (flaglist[column].contains("R"))
170                p_tableitem->setTextAlignment(Qt::AlignRight);
171             if (flaglist[column].contains("C")) {
172                if (field == "OK")
173                   p_tableitem->setBackground(Qt::green);
174                else
175                   p_tableitem->setBackground(Qt::red);
176             }
177             terminatedTable->setItem(results.size() - row - 1, column, p_tableitem);
178             column += 1;
179          }
180          row += 1;
181       }
182    }
183    terminatedTable->resizeColumnsToContents();
184    terminatedTable->resizeRowsToContents();
185    terminatedTable->verticalHeader()->hide();
186 }
187
188 /*
189  * Populate scheduled table
190  */
191 void DirStat::populateScheduled()
192 {
193    QString command = QString(".status dir scheduled");
194    if (mainWin->m_commandDebug)
195       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
196    QStringList results;
197    QBrush blackBrush(Qt::black);
198
199    scheduledTable->clear();
200    QStringList headerlist = (QStringList()
201       << tr("Job Level") << tr("Job Type") << tr("Priority") << tr("Job Time") 
202       << tr("Job Name") << tr("Volume"));
203    QStringList flaglist = (QStringList()
204       << "L" << "L" << "R" << "L" << "L" << "L");
205
206    scheduledTable->setColumnCount(headerlist.size());
207    scheduledTable->setHorizontalHeaderLabels(headerlist);
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             p_tableitem->setFlags(0);
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
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             p_tableitem->setFlags(Qt::ItemIsSelectable);
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 }
290
291 /*
292  *  Virtual function override of pages function which is called when this page
293  *  is visible on the stack
294  */
295 void DirStat::currentStackItem()
296 {
297    populateAll();
298    timerDisplay->display(spinBox->value());
299    if (!m_populated) {
300       m_populated=true;
301    }
302 }
303
304 /*
305  * Function to create connections for context sensitive menu for this and
306  * the page selector
307  */
308 void DirStat::createConnections()
309 {
310    connect(actionRefresh, SIGNAL(triggered()), this, SLOT(populateAll()));
311    connect(actionCancelRunning, SIGNAL(triggered()), this, SLOT(consoleCancelJob()));
312    connect(actionDisableScheduledJob, SIGNAL(triggered()), this, SLOT(consoleDisableJob()));
313    connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTriggered()));
314
315    scheduledTable->setContextMenuPolicy(Qt::ActionsContextMenu);
316    scheduledTable->addAction(actionRefresh);
317    scheduledTable->addAction(actionDisableScheduledJob);
318    terminatedTable->setContextMenuPolicy(Qt::ActionsContextMenu);
319    terminatedTable->addAction(actionRefresh);
320    runningTable->setContextMenuPolicy(Qt::ActionsContextMenu);
321    runningTable->addAction(actionRefresh);
322    runningTable->addAction(actionCancelRunning);
323 }
324
325 /*
326  * Save user settings associated with this page
327  */
328 void DirStat::writeSettings()
329 {
330    QSettings settings(m_console->m_dir->name(), "bat");
331    settings.beginGroup(m_groupText);
332    settings.setValue(m_splitText, splitter->saveState());
333    settings.setValue("refreshInterval", spinBox->value());
334    settings.setValue("refreshCheck", checkBox->checkState());
335    settings.endGroup();
336 }
337
338 /*
339  * Read and restore user settings associated with this page
340  */
341 void DirStat::readSettings()
342 {
343    m_groupText = "DirStatPage";
344    m_splitText = "splitterSizes_0";
345    QSettings settings(m_console->m_dir->name(), "bat");
346    settings.beginGroup(m_groupText);
347    splitter->restoreState(settings.value(m_splitText).toByteArray());
348    spinBox->setValue(settings.value("refreshInterval", 28).toInt());
349    checkBox->setCheckState((Qt::CheckState)settings.value("refreshCheck", Qt::Checked).toInt());
350    settings.endGroup();
351
352    timerDisplay->display(spinBox->value());
353 }
354
355 /*
356  * Cancel a running job
357  */
358 void DirStat::consoleCancelJob()
359 {
360    QList<int> rowList;
361    QList<QTableWidgetItem *> sitems = runningTable->selectedItems();
362    foreach (QTableWidgetItem *sitem, sitems) {
363       int row = sitem->row();
364       if (!rowList.contains(row)) {
365          rowList.append(row);
366       }
367    }
368
369    QStringList selectedJobsList;
370    foreach(int row, rowList) {
371       QTableWidgetItem * sitem = runningTable->item(row, 0);
372       selectedJobsList.append(sitem->text());
373    }
374    foreach( QString job, selectedJobsList )
375    {
376       QString cmd("cancel jobid=");
377       cmd += job;
378       consoleCommand(cmd);
379    }
380 }
381
382 /*
383  * Disable a scheduled Job
384  */
385 void DirStat::consoleDisableJob()
386 {
387    int currentrow = scheduledTable->currentRow();
388    QTableWidgetItem *item = scheduledTable->item(currentrow, 4);
389    if (item) {
390       QString text = item->text();
391       QString cmd("disable job=\"");
392       cmd += text + '"';
393       consoleCommand(cmd);
394    }
395 }