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