2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
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.
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 Bacula® is a registered trademark of Kern Sibbald.
22 * Dirk Bartley, March 2007
27 #include <QAbstractEventDispatcher>
29 #include "clients/clients.h"
31 #include "status/clientstat.h"
32 #include "util/fmtwidgetitem.h"
34 Clients::Clients() : Pages()
37 m_name = tr("Clients");
39 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
40 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/network-server.png")));
42 /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
44 m_checkcurwidget = true;
46 m_firstpopulation = true;
47 /* add context sensitive menu items specific to this classto the page
48 * selector tree. m_contextActions is QList of QActions */
49 m_contextActions.append(actionRefreshClients);
58 * The main meat of the class!! The function that queries the director and
59 * creates the widgets with appropriate values.
61 void Clients::populateTable()
65 Freeze frz(*tableWidget); /* disable updating*/
67 QStringList headerlist = (QStringList() << tr("Client Name") << tr("File Retention")
68 << tr("Job Retention") << tr("AutoPrune") << tr("ClientId") << tr("Uname") );
70 int sortcol = headerlist.indexOf(tr("Client Name"));
71 Qt::SortOrder sortord = Qt::AscendingOrder;
72 if (tableWidget->rowCount()) {
73 sortcol = tableWidget->horizontalHeader()->sortIndicatorSection();
74 sortord = tableWidget->horizontalHeader()->sortIndicatorOrder();
77 m_checkcurwidget = false;
79 m_checkcurwidget = true;
81 tableWidget->setColumnCount(headerlist.count());
82 tableWidget->setHorizontalHeaderLabels(headerlist);
83 tableWidget->horizontalHeader()->setHighlightSections(false);
84 tableWidget->setRowCount(m_console->client_list.count());
85 tableWidget->verticalHeader()->hide();
86 tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
87 tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
88 tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
90 QString client_comsep("");
91 foreach (QString clientName, m_console->client_list){
93 client_comsep += "'" + clientName + "'";
97 client_comsep += ",'" + clientName + "'";
100 if (client_comsep != "") {
102 query += "SELECT Name, FileRetention, JobRetention, AutoPrune, ClientId, Uname"
104 " WHERE ClientId IN (SELECT MAX(ClientId) FROM Client WHERE";
105 query += " Name IN (" + client_comsep + ")";
106 query += " GROUP BY Name) ORDER BY Name";
109 if (mainWin->m_sqlDebug)
110 Pmsg1(000, "Clients query cmd : %s\n",query.toUtf8().data());
111 if (m_console->sql_cmd(query, results)) {
114 /* Iterate through the record returned from the query */
115 foreach (QString resultline, results) {
116 QStringList fieldlist = resultline.split("\t");
118 if (m_firstpopulation) {
119 settingsOpenStatus(fieldlist[0]);
122 TableItemFormatter item(*tableWidget, row);
124 /* Iterate through fields in the record */
125 QStringListIterator fld(fieldlist);
129 item.setTextFld(col++, fld.next());
132 item.setDurationFld(col++, fld.next());
135 item.setDurationFld(col++, fld.next());
138 item.setBoolFld(col++, fld.next());
141 item.setNumericFld(col++, fld.next());
144 item.setTextFld(col++, fld.next());
150 /* set default sorting */
151 tableWidget->sortByColumn(sortcol, sortord);
152 tableWidget->setSortingEnabled(true);
154 /* Resize rows and columns */
155 tableWidget->resizeColumnsToContents();
156 tableWidget->resizeRowsToContents();
159 int rcnt = tableWidget->rowCount();
160 int ccnt = tableWidget->columnCount();
161 for(int r=0; r < rcnt; r++) {
162 for(int c=0; c < ccnt; c++) {
163 QTableWidgetItem* item = tableWidget->item(r, c);
165 item->setFlags(Qt::ItemFlags(item->flags() & (~Qt::ItemIsEditable)));
169 m_firstpopulation = false;
173 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
174 * The tree has been populated.
176 void Clients::PgSeltreeWidgetClicked()
181 if (!isOnceDocked()) {
187 * Added to set the context menu policy based on currently active treeWidgetItem
188 * signaled by currentItemChanged
190 void Clients::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
192 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
193 if (m_checkcurwidget) {
194 int currentRow = currentwidgetitem->row();
195 QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
196 m_currentlyselected = currentrowzeroitem->text();
198 /* The Previous item */
199 if (previouswidgetitem) { /* avoid a segfault if first time */
200 tableWidget->removeAction(actionListJobsofClient);
201 tableWidget->removeAction(actionStatusClientWindow);
202 tableWidget->removeAction(actionPurgeJobs);
203 tableWidget->removeAction(actionPrune);
206 if (m_currentlyselected.length() != 0) {
207 /* set a hold variable to the client name in case the context sensitive
209 tableWidget->addAction(actionListJobsofClient);
210 tableWidget->addAction(actionStatusClientWindow);
211 tableWidget->addAction(actionPurgeJobs);
212 tableWidget->addAction(actionPrune);
218 * Setup a context menu
219 * Made separate from populate so that it would not create context menu over and
220 * over as the tree is repopulated.
222 void Clients::createContextMenu()
224 tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
225 tableWidget->addAction(actionRefreshClients);
226 /* for the tableItemChanged to maintain m_currentJob */
227 connect(tableWidget, SIGNAL(
228 currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
229 this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
231 /* connect to the action specific to this pages class */
232 connect(actionRefreshClients, SIGNAL(triggered()), this,
233 SLOT(populateTable()));
234 connect(actionListJobsofClient, SIGNAL(triggered()), this,
236 connect(actionStatusClientWindow, SIGNAL(triggered()), this,
237 SLOT(statusClientWindow()));
238 connect(actionPurgeJobs, SIGNAL(triggered()), this,
239 SLOT(consolePurgeJobs()));
240 connect(actionPrune, SIGNAL(triggered()), this,
245 * Function responding to actionListJobsofClient which calls mainwin function
246 * to create a window of a list of jobs of this client.
248 void Clients::showJobs()
250 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
251 mainWin->createPageJobList("", m_currentlyselected, "", "", parentItem);
255 * Function responding to actionListJobsofClient which calls mainwin function
256 * to create a window of a list of jobs of this client.
258 void Clients::consoleStatusClient()
260 QString cmd("status client=");
261 cmd += m_currentlyselected;
266 * Virtual function which is called when this page is visible on the stack
268 void Clients::currentStackItem()
272 /* Create the context menu for the client table */
277 * Function responding to actionPurgeJobs
279 void Clients::consolePurgeJobs()
281 if (QMessageBox::warning(this, "Bat",
282 tr("Are you sure you want to purge all jobs of client \"%1\" ?\n"
283 "The Purge command will delete associated Catalog database records from Jobs and"
284 " Volumes without considering the retention period. Purge works only on the"
285 " Catalog database and does not affect data written to Volumes. This command can"
286 " be dangerous because you can delete catalog records associated with current"
287 " backups of files, and we recommend that you do not use it unless you know what"
288 " you are doing.\n\n"
289 " Is there any way I can get you to click Cancel here? You really don't want to do"
291 "Press OK to proceed with the purge operation?").arg(m_currentlyselected),
292 QMessageBox::Ok | QMessageBox::Cancel,
294 == QMessageBox::Cancel) { return; }
296 QString cmd("purge jobs client=");
297 cmd += m_currentlyselected;
302 * Function responding to actionPrune
304 void Clients::prune()
306 new prunePage("", m_currentlyselected);
310 * Function responding to action to create new client status window
312 void Clients::statusClientWindow()
314 /* if one exists, then just set it current */
316 foreach(Pages *page, mainWin->m_pagehash) {
317 if (mainWin->currentConsole() == page->console()) {
318 if (page->name() == tr("Client Status %1").arg(m_currentlyselected)) {
325 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
326 new ClientStat(m_currentlyselected, parentItem);
331 * If first time, then check to see if there were status pages open the last time closed
334 void Clients::settingsOpenStatus(QString &client)
336 QSettings settings(m_console->m_dir->name(), "bat");
338 settings.beginGroup("OpenOnExit");
339 QString toRead = "ClientStatus_" + client;
340 if (settings.value(toRead) == 1) {
341 new ClientStat(client, mainWin->getFromHash(this));
343 mainWin->getFromHash(this)->setExpanded(true);