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