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