]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/clients/clients.cpp
This implements dynamic pages created on the fly being nested on the tree
[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    pgInitialize();
47
48    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_client.h */
49    m_populated = false;
50    m_checkcurwidget = true;
51    m_closeable = false;
52 }
53
54 Clients::~Clients()
55 {
56 }
57
58 /*
59  * The main meat of the class!!  The function that querries the director and 
60  * creates the widgets with appropriate values.
61  */
62 void Clients::populateTree()
63 {
64    QTreeWidgetItem *clientItem, *topItem;
65
66    m_checkcurwidget = false;
67    mp_treeWidget->clear();
68    m_checkcurwidget = true;
69
70    QStringList headerlist = (QStringList() << "Client Name" << "File Retention"
71        << "Job Retention" << "AutoPrune" << "ClientId" << "Uname" );
72
73    topItem = new QTreeWidgetItem(mp_treeWidget);
74    topItem->setText(0, "Clients");
75    topItem->setData(0, Qt::UserRole, 0);
76    topItem->setExpanded(true);
77
78    mp_treeWidget->setColumnCount(headerlist.count());
79    mp_treeWidget->setHeaderLabels(headerlist);
80    /* This could be a log item */
81    //printf("In Clients::populateTree()\n");
82
83    foreach(QString clientName, m_console->client_list){
84       clientItem = new QTreeWidgetItem(topItem);
85       clientItem->setText(0, clientName);
86       clientItem->setData(0, Qt::UserRole, 1);
87       clientItem->setExpanded(true);
88
89       /* Set up query QString and header QStringList */
90       QString query("");
91       query += "SELECT FileRetention, JobRetention, AutoPrune, ClientId, Uname"
92            " FROM Client"
93            " WHERE ";
94       query += " Name='" + clientName + "'";
95       query += " ORDER BY Name";
96
97       QStringList results;
98       /* This could be a log item */
99       //printf("Clients query cmd : %s\n",query.toUtf8().data());
100       if (m_console->sql_cmd(query, results)) {
101          int resultCount = results.count();
102          if (resultCount == 1){
103             QString resultline;
104             QString field;
105             QStringList fieldlist;
106             /* there will only be one of these */
107             foreach (resultline, results) {
108                fieldlist = resultline.split("\t");
109                int index = 0;
110                /* Iterate through fields in the record */
111                foreach (field, fieldlist) {
112                   field = field.trimmed();  /* strip leading & trailing spaces */
113                   clientItem->setData(index+1, Qt::UserRole, 1);
114                   /* Put media fields under the pool tree item */
115                   clientItem->setData(index+1, Qt::UserRole, 1);
116                   clientItem->setText(index+1, field);
117                   index++;
118                }
119             }
120          }
121       }
122    }
123 }
124
125 /*
126  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
127  * The tree has been populated.
128  */
129 void Clients::PgSeltreeWidgetClicked()
130 {
131    if(!m_populated) {
132       populateTree();
133       createContextMenu();
134       m_populated=true;
135    }
136 }
137
138 /*
139  * Added to set the context menu policy based on currently active treeWidgetItem
140  * signaled by currentItemChanged
141  */
142 void Clients::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
143 {
144    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
145    if (m_checkcurwidget) {
146       /* The Previous item */
147       if (previouswidgetitem) { /* avoid a segfault if first time */
148          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
149          if (treedepth == 1){
150             mp_treeWidget->removeAction(actionListJobsofClient);
151          }
152       }
153
154       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
155       if (treedepth == 1){
156          /* set a hold variable to the client name in case the context sensitive
157           * menu is used */
158          m_currentlyselected=currentwidgetitem->text(0);
159          mp_treeWidget->addAction(actionListJobsofClient);
160       }
161    }
162 }
163
164 /* 
165  * Setup a context menu 
166  * Made separate from populate so that it would not create context menu over and
167  * over as the tree is repopulated.
168  */
169 void Clients::createContextMenu()
170 {
171    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
172    mp_treeWidget->addAction(actionRefreshClients);
173    connect(mp_treeWidget, SIGNAL(
174            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
175            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
176    /* connect to the action specific to this pages class */
177    connect(actionRefreshClients, SIGNAL(triggered()), this,
178                 SLOT(populateTree()));
179    connect(actionListJobsofClient, SIGNAL(triggered()), this,
180                 SLOT(showJobs()));
181 }
182
183 /*
184  * Function responding to actionListJobsofClient which calls mainwin function
185  * to create a window of a list of jobs of this client.
186  */
187 void Clients::showJobs()
188 {
189    QString emptymedia("");
190    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
191    mainWin->createPageJobList(emptymedia, m_currentlyselected, parentItem);
192 }
193
194 /*
195  * Virtual function which is called when this page is visible on the stack
196  */
197 void Clients::currentStackItem()
198 {
199    if(!m_populated) {
200       populateTree();
201       /* add context sensitive menu items specific to this classto the page
202        * selector tree. m_m_contextActions is QList of QActions, so this is 
203        * only done once with the first population. */
204       m_contextActions.append(actionRefreshClients);
205       /* Create the context menu for the client tree */
206       createContextMenu();
207       m_populated=true;
208    }
209 }
210
211 /*
212  * Virtual Function to return the name for the medialist tree widget
213  */
214 void Clients::treeWidgetName(QString &name)
215 {
216    name = "Clients";
217 }
218