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