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