]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/clients/clients.cpp
Change fileset and client windows from tree widgets to table widgets.
[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 John Walker.
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
45 Clients::Clients()
46 {
47    setupUi(this);
48    m_name = tr("Clients");
49    pgInitialize();
50    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
51    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/network-server.png")));
52
53    /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
54    m_populated = false;
55    m_checkcurwidget = true;
56    m_closeable = false;
57    /* add context sensitive menu items specific to this classto the page
58     * selector tree. m_contextActions is QList of QActions */
59    m_contextActions.append(actionRefreshClients);
60    createContextMenu();
61    dockPage();
62 }
63
64 Clients::~Clients()
65 {
66 }
67
68 /*
69  * The main meat of the class!!  The function that querries the director and 
70  * creates the widgets with appropriate values.
71  */
72 void Clients::populateTable()
73 {
74    QTableWidgetItem *tableItem;
75    QBrush blackBrush(Qt::black);
76
77    if (!m_console->preventInUseConnect())
78       return;
79    m_checkcurwidget = false;
80    tableWidget->clear();
81    m_checkcurwidget = true;
82
83    QStringList headerlist = (QStringList() << tr("Client Name") << tr("File Retention")
84        << tr("Job Retention") << tr("AutoPrune") << tr("ClientId") << tr("Uname") );
85
86    tableWidget->setColumnCount(headerlist.count());
87    tableWidget->setHorizontalHeaderLabels(headerlist);
88    tableWidget->setRowCount(m_console->client_list.count());
89    tableWidget->verticalHeader()->hide();
90    int row = 0;
91
92    foreach (QString clientName, m_console->client_list){
93       /* Set up query QString and header QStringList */
94       QString query("");
95       query += "SELECT Name, FileRetention, JobRetention, AutoPrune, ClientId, Uname"
96            " FROM Client"
97            " WHERE ";
98       query += " Name='" + clientName + "'";
99       query += " ORDER BY Name";
100
101       QStringList results;
102       /* This could be a log item */
103       if (mainWin->m_sqlDebug) {
104          Pmsg1(000, "Clients query cmd : %s\n",query.toUtf8().data());
105       }
106       if (m_console->sql_cmd(query, results)) {
107          int resultCount = results.count();
108          if (resultCount == 1){
109             QString resultline;
110             QString field;
111             QStringList fieldlist;
112             /* there will only be one of these */
113             foreach (resultline, results) {
114                fieldlist = resultline.split("\t");
115                int column = 0;
116                /* Iterate through fields in the record */
117                foreach (field, fieldlist) {
118                   field = field.trimmed();  /* strip leading & trailing spaces */
119                   tableItem = new QTableWidgetItem(field, 1);
120                   tableItem->setFlags(Qt::ItemIsSelectable);
121                   tableItem->setForeground(blackBrush);
122                   tableItem->setData(Qt::UserRole, 1);
123                   tableWidget->setItem(row, column, tableItem);
124                   column++;
125                }
126             }
127          }
128       }
129       row ++;
130    }
131    /* Resize the columns */
132    for(int cnter=0; cnter<headerlist.size(); cnter++) {
133       tableWidget->resizeColumnToContents(cnter);
134    }
135 }
136
137 /*
138  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
139  * The tree has been populated.
140  */
141 void Clients::PgSeltreeWidgetClicked()
142 {
143    if(!m_populated) {
144       populateTable();
145       m_populated=true;
146    }
147 }
148
149 /*
150  * Added to set the context menu policy based on currently active treeWidgetItem
151  * signaled by currentItemChanged
152  */
153 void Clients::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
154 {
155    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
156    if (m_checkcurwidget) {
157       int currentRow = currentwidgetitem->row();
158       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
159       m_currentlyselected = currentrowzeroitem->text();
160
161       /* The Previous item */
162       if (previouswidgetitem) { /* avoid a segfault if first time */
163          tableWidget->removeAction(actionListJobsofClient);
164          tableWidget->removeAction(actionStatusClientInConsole);
165          tableWidget->removeAction(actionStatusClientWindow);
166          tableWidget->removeAction(actionPurgeJobs);
167          tableWidget->removeAction(actionPrune);
168       }
169
170       if (m_currentlyselected.length() != 0) {
171          /* set a hold variable to the client name in case the context sensitive
172           * menu is used */
173          tableWidget->addAction(actionListJobsofClient);
174          tableWidget->addAction(actionStatusClientInConsole);
175          tableWidget->addAction(actionStatusClientWindow);
176          tableWidget->addAction(actionPurgeJobs);
177          tableWidget->addAction(actionPrune);
178       }
179    }
180 }
181
182 /* 
183  * Setup a context menu 
184  * Made separate from populate so that it would not create context menu over and
185  * over as the tree is repopulated.
186  */
187 void Clients::createContextMenu()
188 {
189    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
190    tableWidget->addAction(actionRefreshClients);
191    /* for the tableItemChanged to maintain m_currentJob */
192    connect(tableWidget, SIGNAL(
193            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
194            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
195
196    /* connect to the action specific to this pages class */
197    connect(actionRefreshClients, SIGNAL(triggered()), this,
198                 SLOT(populateTable()));
199    connect(actionListJobsofClient, SIGNAL(triggered()), this,
200                 SLOT(showJobs()));
201    connect(actionStatusClientInConsole, SIGNAL(triggered()), this,
202                 SLOT(consoleStatusClient()));
203    connect(actionStatusClientWindow, SIGNAL(triggered()), this,
204                 SLOT(statusClientWindow()));
205    connect(actionPurgeJobs, SIGNAL(triggered()), this,
206                 SLOT(consolePurgeJobs()));
207    connect(actionPrune, SIGNAL(triggered()), this,
208                 SLOT(prune()));
209 }
210
211 /*
212  * Function responding to actionListJobsofClient which calls mainwin function
213  * to create a window of a list of jobs of this client.
214  */
215 void Clients::showJobs()
216 {
217    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
218    mainWin->createPageJobList("", m_currentlyselected, "", "", parentItem);
219 }
220
221 /*
222  * Function responding to actionListJobsofClient which calls mainwin function
223  * to create a window of a list of jobs of this client.
224  */
225 void Clients::consoleStatusClient()
226 {
227    QString cmd("status client=");
228    cmd += m_currentlyselected;
229    consoleCommand(cmd);
230 }
231
232 /*
233  * Virtual function which is called when this page is visible on the stack
234  */
235 void Clients::currentStackItem()
236 {
237    if(!m_populated) {
238       populateTable();
239       /* Create the context menu for the client table */
240       m_populated=true;
241    }
242 }
243
244 /*
245  * Function responding to actionPurgeJobs 
246  */
247 void Clients::consolePurgeJobs()
248 {
249    if (QMessageBox::warning(this, "Bat",
250       tr("Are you sure you want to purge ??  !!!.\n"
251 "The Purge command will delete associated Catalog database records from Jobs and"
252 " Volumes without considering the retention period. Purge  works only on the"
253 " Catalog database and does not affect data written to Volumes. This command can"
254 " be dangerous because you can delete catalog records associated with current"
255 " backups of files, and we recommend that you do not use it unless you know what"
256 " you are doing.\n\n"
257 " Is there any way I can get you to click Cancel here?  You really don't want to do"
258 " this\n\n"
259       "Press OK to proceed with the purge operation?"),
260       QMessageBox::Ok | QMessageBox::Cancel)
261       == QMessageBox::Cancel) { return; }
262
263    QString cmd("purge jobs client=");
264    cmd += m_currentlyselected;
265    consoleCommand(cmd);
266 }
267
268 /*
269  * Function responding to actionPrune
270  */
271 void Clients::prune()
272 {
273    new prunePage("", m_currentlyselected);
274 }
275
276 /*
277  * Function responding to action to create new client status window
278  */
279 void Clients::statusClientWindow()
280 {
281    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
282    new ClientStat(m_currentlyselected, parentItem);
283 }