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