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