]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/status/clientstat.cpp
Commit of patch by Riccardo.
[bacula/bacula] / bacula / src / qt-console / status / clientstat.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: clientstat.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 "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    QWidget::connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTriggered()));
56    m_timer->start(mainWin->m_refreshStatusDirInterval*1000);
57
58    createConnections();
59    setCurrent();
60 }
61
62 void ClientStat::getFont()
63 {
64    QFont font = textEditHeader->font();
65
66    QString dirname;
67    m_console->getDirResName(dirname);
68    QSettings settings(dirname, "bat");
69    settings.beginGroup("Console");
70    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
71    font.setPointSize(settings.value("consolePointSize", 10).toInt());
72    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
73    settings.endGroup();
74    textEditHeader->setFont(font);
75 }
76
77 /*
78  * Write the m_splitter settings in the destructor
79  */
80 ClientStat::~ClientStat()
81 {
82    writeSettings();
83 }
84
85 /*
86  * Populate all tables and header widgets
87  */
88 void ClientStat::populateAll()
89 {
90    if (!m_console->preventInUseConnect())
91        return;
92    populateHeader();
93    populateTerminated();
94    populateRunning();
95 }
96
97 /*
98  *  Timer is triggered, see if is current and repopulate.
99  */
100 void ClientStat::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 ClientStat::populateHeader()
113 {
114    QString command = QString(".status client=\"" + m_client + "\" header");
115    if (mainWin->m_commandDebug)
116       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
117    QStringList results;
118    textEditHeader->clear();
119
120    if (m_console->dir_cmd(command, results)) {
121       foreach (QString line, results) {
122          line += "\n";
123          textEditHeader->insertPlainText(line);
124       }
125    }
126 }
127
128 /*
129  * Populate teminated table
130  */
131 void ClientStat::populateTerminated()
132 {
133    QString command = QString(".status client=\"" + m_client + "\" terminated");
134    if (mainWin->m_commandDebug)
135       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
136    QStringList results;
137    QBrush blackBrush(Qt::black);
138
139    terminatedTable->clear();
140    QStringList headerlist = (QStringList()
141       << tr("Job Id") << tr("Job Level") << tr("Job Files")
142       << tr("Job Bytes") << tr("Job Status") << tr("Job Time")
143       << tr("Job Name"));
144    QStringList flaglist = (QStringList()
145       << "R" << "L" << "R" << "R" << "LC"
146       << "L" << "L");
147
148    terminatedTable->setColumnCount(headerlist.size());
149    terminatedTable->setHorizontalHeaderLabels(headerlist);
150
151    if (m_console->dir_cmd(command, results)) {
152       int row = 0;
153       QTableWidgetItem* p_tableitem;
154       terminatedTable->setRowCount(results.size());
155       foreach (QString line, results) {
156          /* Iterate through the record returned from the query */
157          QStringList fieldlist = line.split("\t");
158          int column = 0;
159          QString statusCode("");
160          /* Iterate through fields in the record */
161          foreach (QString field, fieldlist) {
162             field = field.trimmed();  /* strip leading & trailing spaces */
163             p_tableitem = new QTableWidgetItem(field, 1);
164             p_tableitem->setForeground(blackBrush);
165             p_tableitem->setFlags(0);
166             if (flaglist[column].contains("R"))
167                p_tableitem->setTextAlignment(Qt::AlignRight);
168             if (flaglist[column].contains("C")) {
169                if (field == "OK")
170                   p_tableitem->setBackground(Qt::green);
171                else
172                  p_tableitem->setBackground(Qt::red);
173             }
174             terminatedTable->setItem(results.size() - row - 1, column, p_tableitem);
175             column += 1;
176          }
177          row += 1;
178       }
179    }
180    terminatedTable->resizeColumnsToContents();
181    terminatedTable->resizeRowsToContents();
182    terminatedTable->verticalHeader()->hide();
183 }
184
185 /*
186  * Populate running text
187  */
188 void ClientStat::populateRunning()
189 {
190    QString command = QString(".status client=\"" + m_client + "\" running");
191    if (mainWin->m_commandDebug)
192       Pmsg1(000, "sending command : %s\n",command.toUtf8().data());
193    QStringList results;
194    textEditRunning->clear();
195
196    if (m_console->dir_cmd(command, results)) {
197       foreach (QString line, results) {
198          line += "\n";
199          textEditRunning->insertPlainText(line);
200       }
201    }
202 }
203
204 /*
205  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
206  * The tree has been populated.
207  */
208 void ClientStat::PgSeltreeWidgetClicked()
209 {
210    if (!m_populated) {
211       populateAll();
212       m_populated=true;
213    }
214 }
215
216 /*
217  *  Virtual function override of pages function which is called when this page
218  *  is visible on the stack
219  */
220 void ClientStat::currentStackItem()
221 {
222    populateAll();
223    if (!m_populated) {
224       m_populated=true;
225    }
226 }
227
228 /*
229  * Function to create connections for context sensitive menu for this and
230  * the page selector
231  */
232 void ClientStat::createConnections()
233 {
234    connect(actionRefresh, SIGNAL(triggered()), this,
235                    SLOT(populateAll()));
236 }
237
238 /*
239  * Save user settings associated with this page
240  */
241 void ClientStat::writeSettings()
242 {
243    QSettings settings(m_console->m_dir->name(), "bat");
244    settings.beginGroup(m_groupText);
245    settings.setValue(m_splitText, splitter->saveState());
246    settings.endGroup();
247 }
248
249 /*
250  * Read and restore user settings associated with this page
251  */
252 void ClientStat::readSettings()
253 {
254    m_groupText = "ClientStatPage";
255    m_splitText = "splitterSizes_1";
256    QSettings settings(m_console->m_dir->name(), "bat");
257    settings.beginGroup(m_groupText);
258    splitter->restoreState(settings.value(m_splitText).toByteArray());
259    settings.endGroup();
260 }