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