]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/status/dirstat.cpp
1747f4cb7b7ea804bb95a0705ee6bf5c22a73b27
[bacula/bacula] / bacula / src / qt-console / status / dirstat.cpp
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *
22  *   Dirk Bartley, March 2007
23  */
24  
25 #include "bat.h"
26 #include <QAbstractEventDispatcher>
27 #include <QTableWidgetItem>
28 #include "dirstat.h"
29
30 static bool working = false;         /* prevent timer recursion */
31
32 /*
33  * Constructor for the class
34  */
35 DirStat::DirStat() : Pages()
36 {
37    setupUi(this);
38    m_name = tr("Director Status");
39    pgInitialize();
40    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
41    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/status.png")));
42    m_cursor = new QTextCursor(textEdit->document());
43
44    m_timer = new QTimer(this);
45    readSettings();
46    m_timer->start(1000);
47
48    createConnections();
49    setCurrent();
50 }
51
52 void DirStat::getFont()
53 {
54    QFont font = textEdit->font();
55
56    QString dirname;
57    m_console->getDirResName(dirname);
58    QSettings settings(dirname, "bat");
59    settings.beginGroup("Console");
60    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
61    font.setPointSize(settings.value("consolePointSize", 10).toInt());
62    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
63    settings.endGroup();
64    textEdit->setFont(font);
65 }
66
67 /*
68  * Write the m_splitter settings in the destructor
69  */
70 DirStat::~DirStat()
71 {
72    writeSettings();
73 }
74
75 /*
76  * Populate all tables and header widgets
77  */
78 void DirStat::populateAll()
79 {
80    populateHeader();
81    populateTerminated();
82    populateScheduled();
83    populateRunning();
84 }
85
86 /*
87  *  Timer is triggered, see if is current and repopulate.
88  */
89 void DirStat::timerTriggered()
90 {
91    double value = timerDisplay->value();
92    value -= 1;
93    if (value <= 0 && !working) {
94       working = true;
95       value = spinBox->value();
96       bool iscurrent = mainWin->tabWidget->currentIndex() == mainWin->tabWidget->indexOf(this);
97       if (((isDocked() && iscurrent) || ((!isDocked()) && isOnceDocked())) && (checkBox->checkState() == Qt::Checked)) {
98          populateAll();
99       }
100       working = false;
101    }
102    timerDisplay->display(value);
103 }
104
105 /*
106  * Populate header text widget
107  */
108 void DirStat::populateHeader()
109 {
110    QString command = QString(".status dir header");
111    if (mainWin->m_commandDebug)
112       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
113    QStringList results;
114    textEdit->clear();
115
116    if (m_console->dir_cmd(command, results)) {
117       foreach (QString line, results) {
118          line += "\n";
119          textEdit->insertPlainText(line);
120       }
121    }
122 }
123
124 /*
125  * Populate teminated table
126  */
127 void DirStat::populateTerminated()
128 {
129    QString command = QString(".status dir terminated");
130    if (mainWin->m_commandDebug)
131       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
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             }
170             terminatedTable->setItem(results.size() - row - 1, column, p_tableitem);
171             column += 1;
172          }
173          row += 1;
174       }
175    }
176    terminatedTable->resizeColumnsToContents();
177    terminatedTable->resizeRowsToContents();
178    terminatedTable->verticalHeader()->hide();
179 }
180
181 /*
182  * Populate scheduled table
183  */
184 void DirStat::populateScheduled()
185 {
186    QString command = QString(".status dir scheduled");
187    if (mainWin->m_commandDebug)
188       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
189    QStringList results;
190    QBrush blackBrush(Qt::black);
191
192    scheduledTable->clear();
193    QStringList headerlist = (QStringList()
194       << tr("Job Level") << tr("Job Type") << tr("Priority") << tr("Job Time") 
195       << tr("Job Name") << tr("Volume"));
196    QStringList flaglist = (QStringList()
197       << "L" << "L" << "R" << "L" << "L" << "L");
198
199    scheduledTable->setColumnCount(headerlist.size());
200    scheduledTable->setHorizontalHeaderLabels(headerlist);
201    scheduledTable->setSelectionBehavior(QAbstractItemView::SelectRows);
202    scheduledTable->setSelectionMode(QAbstractItemView::SingleSelection);
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             scheduledTable->setItem(row, column, p_tableitem);
219             column += 1;
220          }
221          row += 1;
222       }
223    }
224    scheduledTable->resizeColumnsToContents();
225    scheduledTable->resizeRowsToContents();
226    scheduledTable->verticalHeader()->hide();
227 }
228
229 /*
230  * Populate running table
231  */
232 void DirStat::populateRunning()
233 {
234    QString command = QString(".status dir running");
235    if (mainWin->m_commandDebug)
236       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
237    QStringList results;
238    QBrush blackBrush(Qt::black);
239
240    runningTable->clear();
241    QStringList headerlist = (QStringList()
242       << tr("Job Id") << tr("Job Level") << tr("Job Data") << tr("Job Info"));
243
244    runningTable->setColumnCount(headerlist.size());
245    runningTable->setHorizontalHeaderLabels(headerlist);
246    runningTable->setSelectionBehavior(QAbstractItemView::SelectRows);
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             runningTable->setItem(row, column, p_tableitem);
263             column += 1;
264          }
265          row += 1;
266       }
267    }
268    runningTable->resizeColumnsToContents();
269    runningTable->resizeRowsToContents();
270    runningTable->verticalHeader()->hide();
271 }
272
273 /*
274  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
275  * The tree has been populated.
276  */
277 void DirStat::PgSeltreeWidgetClicked()
278 {
279    if (!m_populated) {
280       populateAll();
281       m_populated=true;
282    }
283    if (!isOnceDocked()) {
284       dockPage();
285    }
286 }
287
288 /*
289  *  Virtual function override of pages function which is called when this page
290  *  is visible on the stack
291  */
292 void DirStat::currentStackItem()
293 {
294    populateAll();
295    timerDisplay->display(spinBox->value());
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, SLOT(populateAll()));
308    connect(actionCancelRunning, SIGNAL(triggered()), this, SLOT(consoleCancelJob()));
309    connect(actionDisableScheduledJob, SIGNAL(triggered()), this, SLOT(consoleDisableJob()));
310    connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTriggered()));
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.setValue("refreshInterval", spinBox->value());
331    settings.setValue("refreshCheck", checkBox->checkState());
332    settings.endGroup();
333 }
334
335 /*
336  * Read and restore user settings associated with this page
337  */
338 void DirStat::readSettings()
339 {
340    m_groupText = "DirStatPage";
341    m_splitText = "splitterSizes_0";
342    QSettings settings(m_console->m_dir->name(), "bat");
343    settings.beginGroup(m_groupText);
344    if (settings.contains(m_splitText)) { splitter->restoreState(settings.value(m_splitText).toByteArray()); }
345    spinBox->setValue(settings.value("refreshInterval", 28).toInt());
346    checkBox->setCheckState((Qt::CheckState)settings.value("refreshCheck", Qt::Checked).toInt());
347    settings.endGroup();
348
349    timerDisplay->display(spinBox->value());
350 }
351
352 /*
353  * Cancel a running job
354  */
355 void DirStat::consoleCancelJob()
356 {
357    QList<int> rowList;
358    QList<QTableWidgetItem *> sitems = runningTable->selectedItems();
359    foreach (QTableWidgetItem *sitem, sitems) {
360       int row = sitem->row();
361       if (!rowList.contains(row)) {
362          rowList.append(row);
363       }
364    }
365
366    QStringList selectedJobsList;
367    foreach(int row, rowList) {
368       QTableWidgetItem * sitem = runningTable->item(row, 0);
369       selectedJobsList.append(sitem->text());
370    }
371    foreach( QString job, selectedJobsList )
372    {
373       QString cmd("cancel jobid=");
374       cmd += job;
375       consoleCommand(cmd);
376    }
377 }
378
379 /*
380  * Disable a scheduled Job
381  */
382 void DirStat::consoleDisableJob()
383 {
384    int currentrow = scheduledTable->currentRow();
385    QTableWidgetItem *item = scheduledTable->item(currentrow, 4);
386    if (item) {
387       QString text = item->text();
388       QString cmd("disable job=\"");
389       cmd += text + '"';
390       consoleCommand(cmd);
391    }
392 }