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