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