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