]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/clients/clients.cpp
Add a consoleCommand to Pages class
[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             mp_treeWidget->removeAction(actionStatusClientInConsole);
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          mp_treeWidget->addAction(actionStatusClientInConsole);
162       }
163    }
164 }
165
166 /* 
167  * Setup a context menu 
168  * Made separate from populate so that it would not create context menu over and
169  * over as the tree is repopulated.
170  */
171 void Clients::createContextMenu()
172 {
173    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
174    mp_treeWidget->addAction(actionRefreshClients);
175    connect(mp_treeWidget, SIGNAL(
176            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
177            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
178    /* connect to the action specific to this pages class */
179    connect(actionRefreshClients, SIGNAL(triggered()), this,
180                 SLOT(populateTree()));
181    connect(actionListJobsofClient, SIGNAL(triggered()), this,
182                 SLOT(showJobs()));
183    connect(actionStatusClientInConsole, SIGNAL(triggered()), this,
184                 SLOT(consoleStatusClient()));
185 }
186
187 /*
188  * Function responding to actionListJobsofClient which calls mainwin function
189  * to create a window of a list of jobs of this client.
190  */
191 void Clients::showJobs()
192 {
193    QString emptymedia("");
194    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
195    mainWin->createPageJobList(emptymedia, m_currentlyselected, parentItem);
196 }
197
198 /*
199  * Function responding to actionListJobsofClient which calls mainwin function
200  * to create a window of a list of jobs of this client.
201  */
202 void Clients::consoleStatusClient()
203 {
204    QString cmd("status client=");
205    cmd += m_currentlyselected += "\n";
206    consoleCommand(cmd);
207 }
208
209 /*
210  * Virtual function which is called when this page is visible on the stack
211  */
212 void Clients::currentStackItem()
213 {
214    if(!m_populated) {
215       populateTree();
216       /* add context sensitive menu items specific to this classto the page
217        * selector tree. m_m_contextActions is QList of QActions, so this is 
218        * only done once with the first population. */
219       m_contextActions.append(actionRefreshClients);
220       /* Create the context menu for the client tree */
221       createContextMenu();
222       m_populated=true;
223    }
224 }
225
226 /*
227  * Virtual Function to return the name for the medialist tree widget
228  */
229 void Clients::treeWidgetName(QString &name)
230 {
231    name = "Clients";
232 }
233