]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/clients/clients.cpp
The fix that I did last night seems to be occuring in just about all of the
[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_populating = false;
57    m_checkcurwidget = true;
58    m_closeable = false;
59    /* add context sensitive menu items specific to this classto the page
60     * selector tree. m_contextActions is QList of QActions */
61    m_contextActions.append(actionRefreshClients);
62    createContextMenu();
63    dockPage();
64 }
65
66 Clients::~Clients()
67 {
68 }
69
70 /*
71  * The main meat of the class!!  The function that querries the director and 
72  * creates the widgets with appropriate values.
73  */
74 void Clients::populateTable()
75 {
76    if (m_populating)
77       return;
78    m_populating = true;
79
80    if (!m_console->preventInUseConnect())
81       return;
82
83    QBrush blackBrush(Qt::black);
84
85    QStringList headerlist = (QStringList() << tr("Client Name") << tr("File Retention")
86        << tr("Job Retention") << tr("AutoPrune") << tr("ClientId") << tr("Uname") );
87
88    int sortcol = headerlist.indexOf(tr("Client Name"));
89    Qt::SortOrder sortord = Qt::AscendingOrder;
90    if (tableWidget->rowCount()) {
91       sortcol = tableWidget->horizontalHeader()->sortIndicatorSection();
92       sortord = tableWidget->horizontalHeader()->sortIndicatorOrder();
93    }
94
95    m_checkcurwidget = false;
96    tableWidget->clear();
97    m_checkcurwidget = true;
98
99    tableWidget->setColumnCount(headerlist.count());
100    tableWidget->setHorizontalHeaderLabels(headerlist);
101    tableWidget->horizontalHeader()->setHighlightSections(false);
102    tableWidget->setRowCount(m_console->client_list.count());
103    tableWidget->verticalHeader()->hide();
104    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
105    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
106    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
107    int row = 0;
108
109    foreach (QString clientName, m_console->client_list){
110       /* Set up query QString and header QStringList */
111       QString query("");
112       query += "SELECT Name, FileRetention, JobRetention, AutoPrune, ClientId, Uname"
113            " FROM Client"
114            " WHERE ";
115       query += " Name='" + clientName + "'";
116       query += " ORDER BY ClientId LIMIT 1";
117
118       QStringList results;
119       /* This could be a log item */
120       if (mainWin->m_sqlDebug) {
121          Pmsg1(000, "Clients query cmd : %s\n",query.toUtf8().data());
122       }
123       if (m_console->sql_cmd(query, results)) {
124          int resultCount = results.count();
125          if (resultCount){
126             /* only use the last one */
127             QString resultline = results[resultCount - 1];
128             QStringList fieldlist = resultline.split("\t");
129
130             TableItemFormatter item(*tableWidget, row);
131
132             /* Iterate through fields in the record */
133             QStringListIterator fld(fieldlist);
134             int col = 0;
135
136             /* name */
137             item.setTextFld(col++, fld.next());
138
139             /* file retention */
140             item.setDurationFld(col++, fld.next());
141
142             /* job retention */
143             item.setDurationFld(col++, fld.next());
144
145             /* autoprune */
146             item.setBoolFld(col++, fld.next());
147
148             /* client id */
149             item.setNumericFld(col++, fld.next());
150
151             /* uname */
152             item.setTextFld(col++, fld.next());
153
154          }
155       }
156       row ++;
157    }
158    /* set default sorting */
159    tableWidget->sortByColumn(sortcol, sortord);
160    tableWidget->setSortingEnabled(true);
161    
162    /* Resize rows and columns */
163    tableWidget->resizeColumnsToContents();
164    tableWidget->resizeRowsToContents();
165    m_populating = false;
166 }
167
168 /*
169  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
170  * The tree has been populated.
171  */
172 void Clients::PgSeltreeWidgetClicked()
173 {
174    if(!m_populated) {
175       populateTable();
176       m_populated=true;
177    }
178 }
179
180 /*
181  * Added to set the context menu policy based on currently active treeWidgetItem
182  * signaled by currentItemChanged
183  */
184 void Clients::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
185 {
186    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
187    if (m_checkcurwidget) {
188       int currentRow = currentwidgetitem->row();
189       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
190       m_currentlyselected = currentrowzeroitem->text();
191
192       /* The Previous item */
193       if (previouswidgetitem) { /* avoid a segfault if first time */
194          tableWidget->removeAction(actionListJobsofClient);
195          tableWidget->removeAction(actionStatusClientInConsole);
196          tableWidget->removeAction(actionStatusClientWindow);
197          tableWidget->removeAction(actionPurgeJobs);
198          tableWidget->removeAction(actionPrune);
199       }
200
201       if (m_currentlyselected.length() != 0) {
202          /* set a hold variable to the client name in case the context sensitive
203           * menu is used */
204          tableWidget->addAction(actionListJobsofClient);
205          tableWidget->addAction(actionStatusClientInConsole);
206          tableWidget->addAction(actionStatusClientWindow);
207          tableWidget->addAction(actionPurgeJobs);
208          tableWidget->addAction(actionPrune);
209       }
210    }
211 }
212
213 /* 
214  * Setup a context menu 
215  * Made separate from populate so that it would not create context menu over and
216  * over as the tree is repopulated.
217  */
218 void Clients::createContextMenu()
219 {
220    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
221    tableWidget->addAction(actionRefreshClients);
222    /* for the tableItemChanged to maintain m_currentJob */
223    connect(tableWidget, SIGNAL(
224            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
225            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
226
227    /* connect to the action specific to this pages class */
228    connect(actionRefreshClients, SIGNAL(triggered()), this,
229                 SLOT(populateTable()));
230    connect(actionListJobsofClient, SIGNAL(triggered()), this,
231                 SLOT(showJobs()));
232    connect(actionStatusClientInConsole, SIGNAL(triggered()), this,
233                 SLOT(consoleStatusClient()));
234    connect(actionStatusClientWindow, SIGNAL(triggered()), this,
235                 SLOT(statusClientWindow()));
236    connect(actionPurgeJobs, SIGNAL(triggered()), this,
237                 SLOT(consolePurgeJobs()));
238    connect(actionPrune, SIGNAL(triggered()), this,
239                 SLOT(prune()));
240 }
241
242 /*
243  * Function responding to actionListJobsofClient which calls mainwin function
244  * to create a window of a list of jobs of this client.
245  */
246 void Clients::showJobs()
247 {
248    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
249    mainWin->createPageJobList("", m_currentlyselected, "", "", parentItem);
250 }
251
252 /*
253  * Function responding to actionListJobsofClient which calls mainwin function
254  * to create a window of a list of jobs of this client.
255  */
256 void Clients::consoleStatusClient()
257 {
258    QString cmd("status client=");
259    cmd += m_currentlyselected;
260    consoleCommand(cmd);
261 }
262
263 /*
264  * Virtual function which is called when this page is visible on the stack
265  */
266 void Clients::currentStackItem()
267 {
268    if(!m_populated) {
269       populateTable();
270       /* Create the context menu for the client table */
271       m_populated=true;
272    }
273 }
274
275 /*
276  * Function responding to actionPurgeJobs 
277  */
278 void Clients::consolePurgeJobs()
279 {
280    if (QMessageBox::warning(this, "Bat",
281       tr("Are you sure you want to purge all jobs of client \"%1\" ?\n"
282 "The Purge command will delete associated Catalog database records from Jobs and"
283 " Volumes without considering the retention period. Purge  works only on the"
284 " Catalog database and does not affect data written to Volumes. This command can"
285 " be dangerous because you can delete catalog records associated with current"
286 " backups of files, and we recommend that you do not use it unless you know what"
287 " you are doing.\n\n"
288 " Is there any way I can get you to click Cancel here?  You really don't want to do"
289 " this\n\n"
290          "Press OK to proceed with the purge operation?").arg(m_currentlyselected),
291          QMessageBox::Ok | QMessageBox::Cancel,
292          QMessageBox::Cancel)
293       == QMessageBox::Cancel) { return; }
294
295    QString cmd("purge jobs client=");
296    cmd += m_currentlyselected;
297    consoleCommand(cmd);
298 }
299
300 /*
301  * Function responding to actionPrune
302  */
303 void Clients::prune()
304 {
305    new prunePage("", m_currentlyselected);
306 }
307
308 /*
309  * Function responding to action to create new client status window
310  */
311 void Clients::statusClientWindow()
312 {
313    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
314    new ClientStat(m_currentlyselected, parentItem);
315 }
316