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