]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/status/clientstat.cpp
This version fixes an issue where the console window would start out not
[bacula/bacula] / bacula / src / qt-console / status / clientstat.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: clientstat.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 "clientstat.h"
38
39 /*
40  * Constructor for the class
41  */
42 ClientStat::ClientStat(QString &client, QTreeWidgetItem *parentTreeWidgetItem)
43 {
44    m_client = client;
45    setupUi(this);
46    pgInitialize(tr("Client Status %1").arg(m_client), parentTreeWidgetItem);
47    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
48    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/status.png")));
49    m_cursor = new QTextCursor(textEditHeader->document());
50
51    readSettings();
52    dockPage();
53    m_timer = new QTimer(this);
54
55    createConnections();
56    m_timer->start(1000);
57    setCurrent();
58 }
59
60 void ClientStat::getFont()
61 {
62    QFont font = textEditHeader->font();
63
64    QString dirname;
65    m_console->getDirResName(dirname);
66    QSettings settings(dirname, "bat");
67    settings.beginGroup("Console");
68    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
69    font.setPointSize(settings.value("consolePointSize", 10).toInt());
70    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
71    settings.endGroup();
72    textEditHeader->setFont(font);
73 }
74
75 /*
76  * Write the m_splitter settings in the destructor
77  */
78 ClientStat::~ClientStat()
79 {
80    writeSettings();
81 }
82
83 /*
84  * Populate all tables and header widgets
85  */
86 void ClientStat::populateAll()
87 {
88    populateTerminated();
89    populateCurrentTab(tabWidget->currentIndex());
90 }
91
92 /*
93  *  Timer is triggered, see if is current and repopulate.
94  */
95 void ClientStat::timerTriggered()
96 {
97    double value = timerDisplay->value();
98    value -= 1;
99    if (value == 0) {
100       value = spinBox->value();
101       bool iscurrent = mainWin->tabWidget->currentIndex() == mainWin->tabWidget->indexOf(this);
102       if (((isDocked() && iscurrent) || (!isDocked())) && (checkBox->checkState() == Qt::Checked)) {
103          populateAll();
104       }
105    }
106    timerDisplay->display(value);
107 }
108
109
110 void ClientStat::populateCurrentTab(int index)
111 {
112    if (index == 0)
113       populateRunning();
114    if (index == 1)
115       populateHeader();
116 }
117
118 /*
119  * Populate header text widget
120  */
121 void ClientStat::populateHeader()
122 {
123    QString command = QString(".status client=\"" + m_client + "\" header");
124    if (mainWin->m_commandDebug)
125       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
126    QStringList results;
127    textEditHeader->clear();
128
129    if (m_console->dir_cmd(command, results)) {
130       foreach (QString line, results) {
131          line += "\n";
132          textEditHeader->insertPlainText(line);
133       }
134    }
135 }
136
137 /*
138  * Populate teminated table
139  */
140 void ClientStat::populateTerminated()
141 {
142    QString command = QString(".status client=\"" + m_client + "\" terminated");
143    if (mainWin->m_commandDebug)
144       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
145    QStringList results;
146    QBrush blackBrush(Qt::black);
147
148    terminatedTable->clear();
149    QStringList headerlist = (QStringList()
150       << tr("Job Id") << tr("Job Level") << tr("Job Files")
151       << tr("Job Bytes") << tr("Job Status") << tr("Job Time")
152       << tr("Job Name"));
153    QStringList flaglist = (QStringList()
154       << "R" << "L" << "R" << "R" << "LC"
155       << "L" << "L");
156
157    terminatedTable->setColumnCount(headerlist.size());
158    terminatedTable->setHorizontalHeaderLabels(headerlist);
159
160    if (m_console->dir_cmd(command, results)) {
161       int row = 0;
162       QTableWidgetItem* p_tableitem;
163       terminatedTable->setRowCount(results.size());
164       foreach (QString line, results) {
165          /* Iterate through the record returned from the query */
166          QStringList fieldlist = line.split("\t");
167          int column = 0;
168          QString statusCode("");
169          /* Iterate through fields in the record */
170          foreach (QString field, fieldlist) {
171             field = field.trimmed();  /* strip leading & trailing spaces */
172             p_tableitem = new QTableWidgetItem(field, 1);
173             p_tableitem->setForeground(blackBrush);
174             p_tableitem->setFlags(0);
175             if (flaglist[column].contains("R"))
176                p_tableitem->setTextAlignment(Qt::AlignRight);
177             if (flaglist[column].contains("C")) {
178                if (field == "OK")
179                   p_tableitem->setBackground(Qt::green);
180                else
181                  p_tableitem->setBackground(Qt::red);
182             }
183             terminatedTable->setItem(results.size() - row - 1, column, p_tableitem);
184             column += 1;
185          }
186          row += 1;
187       }
188    }
189    terminatedTable->resizeColumnsToContents();
190    terminatedTable->resizeRowsToContents();
191    terminatedTable->verticalHeader()->hide();
192 }
193
194 /*
195  * Populate running text
196  */
197 void ClientStat::populateRunning()
198 {
199    QString command = QString(".status client=\"" + m_client + "\" running");
200    if (mainWin->m_commandDebug)
201       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
202    QStringList results;
203    textEditRunning->clear();
204
205    if (m_console->dir_cmd(command, results)) {
206       foreach (QString line, results) {
207          line += "\n";
208          textEditRunning->insertPlainText(line);
209       }
210    }
211 }
212
213 /*
214  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
215  * The tree has been populated.
216  */
217 void ClientStat::PgSeltreeWidgetClicked()
218 {
219    if (!m_populated) {
220       populateAll();
221       m_populated=true;
222    }
223 }
224
225 /*
226  *  Virtual function override of pages function which is called when this page
227  *  is visible on the stack
228  */
229 void ClientStat::currentStackItem()
230 {
231    populateAll();
232    timerDisplay->display(spinBox->value());
233
234    if (!m_populated) {
235       m_populated=true;
236    }
237 }
238
239 /*
240  * Function to create connections for context sensitive menu for this and
241  * the page selector
242  */
243 void ClientStat::createConnections()
244 {
245    connect(actionRefresh, SIGNAL(triggered()), this, SLOT(populateAll()));
246    connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(populateCurrentTab(int)));
247    connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTriggered()));
248    terminatedTable->setContextMenuPolicy(Qt::ActionsContextMenu);
249    terminatedTable->addAction(actionRefresh);
250 }
251
252 /*
253  * Save user settings associated with this page
254  */
255 void ClientStat::writeSettings()
256 {
257    QSettings settings(m_console->m_dir->name(), "bat");
258    settings.beginGroup(m_groupText);
259    settings.setValue(m_splitText, splitter->saveState());
260    settings.setValue("refreshInterval", spinBox->value());
261    settings.setValue("refreshCheck", checkBox->checkState());
262    settings.endGroup();
263
264    settings.beginGroup("OpenOnExit");
265    QString toWrite = "ClientStatus_" + m_client;
266    settings.setValue(toWrite, 1);
267    settings.endGroup();
268 }
269
270 /*
271  * Read and restore user settings associated with this page
272  */
273 void ClientStat::readSettings()
274 {
275    m_groupText = "ClientStatPage";
276    m_splitText = "splitterSizes_1";
277    QSettings settings(m_console->m_dir->name(), "bat");
278    settings.beginGroup(m_groupText);
279    if (settings.contains(m_splitText)) { splitter->restoreState(settings.value(m_splitText).toByteArray()); }
280    spinBox->setValue(settings.value("refreshInterval", 28).toInt());
281    checkBox->setCheckState((Qt::CheckState)settings.value("refreshCheck", Qt::Checked).toInt());
282    settings.endGroup();
283
284    timerDisplay->display(spinBox->value());
285 }