2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
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.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
23 * Dirk Bartley, March 2007
28 #include <QAbstractEventDispatcher>
30 #include "clients/clients.h"
32 #include "status/clientstat.h"
33 #include "util/fmtwidgetitem.h"
35 Clients::Clients() : Pages()
38 m_name = tr("Clients");
40 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
41 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/network-server.png")));
43 /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
45 m_checkcurwidget = true;
47 m_firstpopulation = true;
48 /* add context sensitive menu items specific to this classto the page
49 * selector tree. m_contextActions is QList of QActions */
50 m_contextActions.append(actionRefreshClients);
59 * The main meat of the class!! The function that queries the director and
60 * creates the widgets with appropriate values.
62 void Clients::populateTable()
66 Freeze frz(*tableWidget); /* disable updating*/
68 QStringList headerlist = (QStringList() << tr("Client Name") << tr("File Retention")
69 << tr("Job Retention") << tr("AutoPrune") << tr("ClientId") << tr("Uname") );
71 int sortcol = headerlist.indexOf(tr("Client Name"));
72 Qt::SortOrder sortord = Qt::AscendingOrder;
73 if (tableWidget->rowCount()) {
74 sortcol = tableWidget->horizontalHeader()->sortIndicatorSection();
75 sortord = tableWidget->horizontalHeader()->sortIndicatorOrder();
78 m_checkcurwidget = false;
80 m_checkcurwidget = true;
82 tableWidget->setColumnCount(headerlist.count());
83 tableWidget->setHorizontalHeaderLabels(headerlist);
84 tableWidget->horizontalHeader()->setHighlightSections(false);
85 tableWidget->setRowCount(m_console->client_list.count());
86 tableWidget->verticalHeader()->hide();
87 tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
88 tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
89 tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
91 QString client_comsep("");
92 foreach (QString clientName, m_console->client_list){
94 client_comsep += "'" + clientName + "'";
98 client_comsep += ",'" + clientName + "'";
101 if (client_comsep != "") {
103 query += "SELECT Name, FileRetention, JobRetention, AutoPrune, ClientId, Uname"
105 " WHERE ClientId IN (SELECT MAX(ClientId) FROM Client WHERE";
106 query += " Name IN (" + client_comsep + ")";
107 query += " GROUP BY Name) ORDER BY Name";
110 if (mainWin->m_sqlDebug)
111 Pmsg1(000, "Clients query cmd : %s\n",query.toUtf8().data());
112 if (m_console->sql_cmd(query, results)) {
115 /* Iterate through the record returned from the query */
116 foreach (QString resultline, results) {
117 QStringList fieldlist = resultline.split("\t");
119 if (fieldlist.size() < 5) { // Uname is checked after
120 Pmsg1(0, "Unexpected line %s\n", resultline.toUtf8().data());
123 if (m_firstpopulation) {
124 settingsOpenStatus(fieldlist[0]);
127 TableItemFormatter item(*tableWidget, row);
129 /* Iterate through fields in the record */
130 QStringListIterator fld(fieldlist);
134 item.setTextFld(col++, fld.next());
137 item.setDurationFld(col++, fld.next());
140 item.setDurationFld(col++, fld.next());
143 item.setBoolFld(col++, fld.next());
146 item.setNumericFld(col++, fld.next());
150 item.setTextFld(col++, fld.next());
152 item.setTextFld(col++, "");
159 /* set default sorting */
160 tableWidget->sortByColumn(sortcol, sortord);
161 tableWidget->setSortingEnabled(true);
163 /* Resize rows and columns */
164 tableWidget->resizeColumnsToContents();
165 tableWidget->resizeRowsToContents();
168 int rcnt = tableWidget->rowCount();
169 int ccnt = tableWidget->columnCount();
170 for(int r=0; r < rcnt; r++) {
171 for(int c=0; c < ccnt; c++) {
172 QTableWidgetItem* item = tableWidget->item(r, c);
174 item->setFlags(Qt::ItemFlags(item->flags() & (~Qt::ItemIsEditable)));
178 m_firstpopulation = false;
182 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
183 * The tree has been populated.
185 void Clients::PgSeltreeWidgetClicked()
190 if (!isOnceDocked()) {
196 * Added to set the context menu policy based on currently active treeWidgetItem
197 * signaled by currentItemChanged
199 void Clients::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
201 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
202 if (m_checkcurwidget) {
203 int currentRow = currentwidgetitem->row();
204 QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
205 m_currentlyselected = currentrowzeroitem->text();
207 /* The Previous item */
208 if (previouswidgetitem) { /* avoid a segfault if first time */
209 tableWidget->removeAction(actionListJobsofClient);
210 tableWidget->removeAction(actionStatusClientWindow);
211 tableWidget->removeAction(actionPurgeJobs);
212 tableWidget->removeAction(actionPrune);
215 if (m_currentlyselected.length() != 0) {
216 /* set a hold variable to the client name in case the context sensitive
218 tableWidget->addAction(actionListJobsofClient);
219 tableWidget->addAction(actionStatusClientWindow);
220 tableWidget->addAction(actionPurgeJobs);
221 tableWidget->addAction(actionPrune);
227 * Setup a context menu
228 * Made separate from populate so that it would not create context menu over and
229 * over as the tree is repopulated.
231 void Clients::createContextMenu()
233 tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
234 tableWidget->addAction(actionRefreshClients);
235 /* for the tableItemChanged to maintain m_currentJob */
236 connect(tableWidget, SIGNAL(
237 currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
238 this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
240 /* connect to the action specific to this pages class */
241 connect(actionRefreshClients, SIGNAL(triggered()), this,
242 SLOT(populateTable()));
243 connect(actionListJobsofClient, SIGNAL(triggered()), this,
245 connect(actionStatusClientWindow, SIGNAL(triggered()), this,
246 SLOT(statusClientWindow()));
247 connect(actionPurgeJobs, SIGNAL(triggered()), this,
248 SLOT(consolePurgeJobs()));
249 connect(actionPrune, SIGNAL(triggered()), this,
254 * Function responding to actionListJobsofClient which calls mainwin function
255 * to create a window of a list of jobs of this client.
257 void Clients::showJobs()
259 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
260 mainWin->createPageJobList("", m_currentlyselected, "", "", parentItem);
264 * Function responding to actionListJobsofClient which calls mainwin function
265 * to create a window of a list of jobs of this client.
267 void Clients::consoleStatusClient()
269 QString cmd("status client=");
270 cmd += m_currentlyselected;
275 * Virtual function which is called when this page is visible on the stack
277 void Clients::currentStackItem()
281 /* Create the context menu for the client table */
286 * Function responding to actionPurgeJobs
288 void Clients::consolePurgeJobs()
290 if (QMessageBox::warning(this, "Bat",
291 tr("Are you sure you want to purge all jobs of client \"%1\" ?\n"
292 "The Purge command will delete associated Catalog database records from Jobs and"
293 " Volumes without considering the retention period. Purge works only on the"
294 " Catalog database and does not affect data written to Volumes. This command can"
295 " be dangerous because you can delete catalog records associated with current"
296 " backups of files, and we recommend that you do not use it unless you know what"
297 " you are doing.\n\n"
298 " Is there any way I can get you to click Cancel here? You really don't want to do"
300 "Press OK to proceed with the purge operation?").arg(m_currentlyselected),
301 QMessageBox::Ok | QMessageBox::Cancel,
303 == QMessageBox::Cancel) { return; }
305 QString cmd("purge jobs client=");
306 cmd += m_currentlyselected;
311 * Function responding to actionPrune
313 void Clients::prune()
315 new prunePage("", m_currentlyselected);
319 * Function responding to action to create new client status window
321 void Clients::statusClientWindow()
323 /* if one exists, then just set it current */
325 foreach(Pages *page, mainWin->m_pagehash) {
326 if (mainWin->currentConsole() == page->console()) {
327 if (page->name() == tr("Client Status %1").arg(m_currentlyselected)) {
334 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
335 new ClientStat(m_currentlyselected, parentItem);
340 * If first time, then check to see if there were status pages open the last time closed
343 void Clients::settingsOpenStatus(QString &client)
345 QSettings settings(m_console->m_dir->name(), "bat");
347 settings.beginGroup("OpenOnExit");
348 QString toRead = "ClientStatus_" + client;
349 if (settings.value(toRead) == 1) {
350 new ClientStat(client, mainWin->getFromHash(this));
352 mainWin->getFromHash(this)->setExpanded(true);