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