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