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