]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/clients/clients.cpp
Apply pane freezing during updates patch from Riccardo Ghetta.
[bacula/bacula] / bacula / src / qt-console / clients / clients.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28  
29 /*
30  *   Version $Id$
31  *
32  *  Clients Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 #include <QAbstractEventDispatcher>
39 #include <QMenu>
40 #include "bat.h"
41 #include "clients/clients.h"
42 #include "run/run.h"
43 #include "status/clientstat.h"
44 #include "util/fmtwidgetitem.h"
45
46 Clients::Clients()
47 {
48    setupUi(this);
49    m_name = tr("Clients");
50    pgInitialize();
51    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
52    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/network-server.png")));
53
54    /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
55    m_populated = false;
56    m_checkcurwidget = true;
57    m_closeable = false;
58    /* add context sensitive menu items specific to this classto the page
59     * selector tree. m_contextActions is QList of QActions */
60    m_contextActions.append(actionRefreshClients);
61    createContextMenu();
62    dockPage();
63 }
64
65 Clients::~Clients()
66 {
67 }
68
69 /*
70  * The main meat of the class!!  The function that querries the director and 
71  * creates the widgets with appropriate values.
72  */
73 void Clients::populateTable()
74 {
75    if (!m_console->preventInUseConnect())
76       return;
77    m_populated = true;
78
79    Freeze frz(*tableWidget); /* disable updating*/
80
81    QStringList headerlist = (QStringList() << tr("Client Name") << tr("File Retention")
82        << tr("Job Retention") << tr("AutoPrune") << tr("ClientId") << tr("Uname") );
83
84    int sortcol = headerlist.indexOf(tr("Client Name"));
85    Qt::SortOrder sortord = Qt::AscendingOrder;
86    if (tableWidget->rowCount()) {
87       sortcol = tableWidget->horizontalHeader()->sortIndicatorSection();
88       sortord = tableWidget->horizontalHeader()->sortIndicatorOrder();
89    }
90
91    m_checkcurwidget = false;
92    tableWidget->clear();
93    m_checkcurwidget = true;
94
95    tableWidget->setColumnCount(headerlist.count());
96    tableWidget->setHorizontalHeaderLabels(headerlist);
97    tableWidget->horizontalHeader()->setHighlightSections(false);
98    tableWidget->setRowCount(m_console->client_list.count());
99    tableWidget->verticalHeader()->hide();
100    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
101    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
102    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
103    int row = 0;
104
105    foreach (QString clientName, m_console->client_list){
106       /* Set up query QString and header QStringList */
107       QString query("");
108       query += "SELECT Name, FileRetention, JobRetention, AutoPrune, ClientId, Uname"
109            " FROM Client"
110            " WHERE ";
111       query += " Name='" + clientName + "'";
112       query += " ORDER BY ClientId LIMIT 1";
113
114       QStringList results;
115       /* This could be a log item */
116       if (mainWin->m_sqlDebug) {
117          Pmsg1(000, "Clients query cmd : %s\n",query.toUtf8().data());
118       }
119       if (m_console->sql_cmd(query, results)) {
120          int resultCount = results.count();
121          if (resultCount){
122             /* only use the last one */
123             QString resultline = results[resultCount - 1];
124             QStringList fieldlist = resultline.split("\t");
125
126             TableItemFormatter item(*tableWidget, row);
127
128             /* Iterate through fields in the record */
129             QStringListIterator fld(fieldlist);
130             int col = 0;
131
132             /* name */
133             item.setTextFld(col++, fld.next());
134
135             /* file retention */
136             item.setDurationFld(col++, fld.next());
137
138             /* job retention */
139             item.setDurationFld(col++, fld.next());
140
141             /* autoprune */
142             item.setBoolFld(col++, fld.next());
143
144             /* client id */
145             item.setNumericFld(col++, fld.next());
146
147             /* uname */
148             item.setTextFld(col++, fld.next());
149
150          }
151       }
152       row ++;
153    }
154    /* set default sorting */
155    tableWidget->sortByColumn(sortcol, sortord);
156    tableWidget->setSortingEnabled(true);
157    
158    /* Resize rows and columns */
159    tableWidget->resizeColumnsToContents();
160    tableWidget->resizeRowsToContents();
161 }
162
163 /*
164  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
165  * The tree has been populated.
166  */
167 void Clients::PgSeltreeWidgetClicked()
168 {
169    if(!m_populated) {
170       populateTable();
171    }
172 }
173
174 /*
175  * Added to set the context menu policy based on currently active treeWidgetItem
176  * signaled by currentItemChanged
177  */
178 void Clients::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
179 {
180    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
181    if (m_checkcurwidget) {
182       int currentRow = currentwidgetitem->row();
183       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
184       m_currentlyselected = currentrowzeroitem->text();
185
186       /* The Previous item */
187       if (previouswidgetitem) { /* avoid a segfault if first time */
188          tableWidget->removeAction(actionListJobsofClient);
189          tableWidget->removeAction(actionStatusClientInConsole);
190          tableWidget->removeAction(actionStatusClientWindow);
191          tableWidget->removeAction(actionPurgeJobs);
192          tableWidget->removeAction(actionPrune);
193       }
194
195       if (m_currentlyselected.length() != 0) {
196          /* set a hold variable to the client name in case the context sensitive
197           * menu is used */
198          tableWidget->addAction(actionListJobsofClient);
199          tableWidget->addAction(actionStatusClientInConsole);
200          tableWidget->addAction(actionStatusClientWindow);
201          tableWidget->addAction(actionPurgeJobs);
202          tableWidget->addAction(actionPrune);
203       }
204    }
205 }
206
207 /* 
208  * Setup a context menu 
209  * Made separate from populate so that it would not create context menu over and
210  * over as the tree is repopulated.
211  */
212 void Clients::createContextMenu()
213 {
214    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
215    tableWidget->addAction(actionRefreshClients);
216    /* for the tableItemChanged to maintain m_currentJob */
217    connect(tableWidget, SIGNAL(
218            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
219            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
220
221    /* connect to the action specific to this pages class */
222    connect(actionRefreshClients, SIGNAL(triggered()), this,
223                 SLOT(populateTable()));
224    connect(actionListJobsofClient, SIGNAL(triggered()), this,
225                 SLOT(showJobs()));
226    connect(actionStatusClientInConsole, SIGNAL(triggered()), this,
227                 SLOT(consoleStatusClient()));
228    connect(actionStatusClientWindow, SIGNAL(triggered()), this,
229                 SLOT(statusClientWindow()));
230    connect(actionPurgeJobs, SIGNAL(triggered()), this,
231                 SLOT(consolePurgeJobs()));
232    connect(actionPrune, SIGNAL(triggered()), this,
233                 SLOT(prune()));
234 }
235
236 /*
237  * Function responding to actionListJobsofClient which calls mainwin function
238  * to create a window of a list of jobs of this client.
239  */
240 void Clients::showJobs()
241 {
242    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
243    mainWin->createPageJobList("", m_currentlyselected, "", "", parentItem);
244 }
245
246 /*
247  * Function responding to actionListJobsofClient which calls mainwin function
248  * to create a window of a list of jobs of this client.
249  */
250 void Clients::consoleStatusClient()
251 {
252    QString cmd("status client=");
253    cmd += m_currentlyselected;
254    consoleCommand(cmd);
255 }
256
257 /*
258  * Virtual function which is called when this page is visible on the stack
259  */
260 void Clients::currentStackItem()
261 {
262    if(!m_populated) {
263       populateTable();
264       /* Create the context menu for the client table */
265    }
266 }
267
268 /*
269  * Function responding to actionPurgeJobs 
270  */
271 void Clients::consolePurgeJobs()
272 {
273    if (QMessageBox::warning(this, "Bat",
274       tr("Are you sure you want to purge all jobs of client \"%1\" ?\n"
275 "The Purge command will delete associated Catalog database records from Jobs and"
276 " Volumes without considering the retention period. Purge  works only on the"
277 " Catalog database and does not affect data written to Volumes. This command can"
278 " be dangerous because you can delete catalog records associated with current"
279 " backups of files, and we recommend that you do not use it unless you know what"
280 " you are doing.\n\n"
281 " Is there any way I can get you to click Cancel here?  You really don't want to do"
282 " this\n\n"
283          "Press OK to proceed with the purge operation?").arg(m_currentlyselected),
284          QMessageBox::Ok | QMessageBox::Cancel,
285          QMessageBox::Cancel)
286       == QMessageBox::Cancel) { return; }
287
288    QString cmd("purge jobs client=");
289    cmd += m_currentlyselected;
290    consoleCommand(cmd);
291 }
292
293 /*
294  * Function responding to actionPrune
295  */
296 void Clients::prune()
297 {
298    new prunePage("", m_currentlyselected);
299 }
300
301 /*
302  * Function responding to action to create new client status window
303  */
304 void Clients::statusClientWindow()
305 {
306    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
307    new ClientStat(m_currentlyselected, parentItem);
308 }
309