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