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