]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/jobs/jobs.cpp
7f13564ccd9ccfee4688e22a30c8e9fb36dde1ad
[bacula/bacula] / bacula / src / qt-console / jobs / jobs.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 and included
11    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  *  Jobs Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 //#include <QAbstractEventDispatcher>
39 //#include <QMenu>
40 #include "bat.h"
41 #include "jobs/jobs.h"
42
43 Jobs::Jobs()
44 {
45    setupUi(this);
46    m_name = "Jobs";
47    pgInitialize();
48    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
49    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/run.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    /* add context sensitive menu items specific to this classto the page
56     * selector tree. m_contextActions is QList of QActions */
57    m_contextActions.append(actionRefreshJobs);
58    createContextMenu();
59    dockPage();
60 }
61
62 Jobs::~Jobs()
63 {
64 }
65
66 /*
67  * The main meat of the class!!  The function that querries the director and 
68  * creates the widgets with appropriate values.
69  */
70 void Jobs::populateTree()
71 {
72    QTreeWidgetItem *jobsItem, *topItem;
73
74    if (!m_console->preventInUseConnect())
75       return;
76    m_checkcurwidget = false;
77    mp_treeWidget->clear();
78    m_checkcurwidget = true;
79    QStringList headerlist = (QStringList() << "Job Name" << "Pool" << "Messages"
80       << "Client" << "Storage" << "Where" << "Level" << "Type" << "FileSet" 
81       << "Catalog" << "Enabled");
82
83    topItem = new QTreeWidgetItem(mp_treeWidget);
84    topItem->setText(0, "Jobs");
85    topItem->setData(0, Qt::UserRole, 0);
86    topItem->setExpanded(true);
87
88    mp_treeWidget->setColumnCount(headerlist.count());
89    mp_treeWidget->setHeaderLabels(headerlist);
90
91    foreach (QString jobName, m_console->job_list){
92       jobsItem = new QTreeWidgetItem(topItem);
93       jobsItem->setText(0, jobName);
94       //jobsItem->setExpanded(true);
95
96       for (int i=0; i<headerlist.count(); i++)
97          jobsItem->setData(i, Qt::UserRole, 1);
98
99       job_defaults job_defs;
100       job_defs.job_name = jobName;
101       if (m_console->get_job_defaults(job_defs)) {
102          int col = 1;
103          jobsItem->setText(col++, job_defs.pool_name);
104          jobsItem->setText(col++, job_defs.messages_name);
105          jobsItem->setText(col++, job_defs.client_name);
106          jobsItem->setText(col++, job_defs.store_name);
107          jobsItem->setText(col++, job_defs.where);
108          jobsItem->setText(col++, job_defs.level);
109          jobsItem->setText(col++, job_defs.type);
110          jobsItem->setText(col++, job_defs.fileset_name);
111          jobsItem->setText(col++, job_defs.catalog_name);
112          if (job_defs.enabled) {
113             jobsItem->setText(col++, "Yes");
114          } else {
115             jobsItem->setText(col++, "No");
116          }
117       }
118    }
119    /* Resize the columns */
120    for(int cnter=0; cnter<headerlist.size(); cnter++) {
121       mp_treeWidget->resizeColumnToContents(cnter);
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 Jobs::PgSeltreeWidgetClicked()
130 {
131    if(!m_populated) {
132       populateTree();
133       m_populated=true;
134    }
135 }
136
137 /*
138  * Added to set the context menu policy based on currently active treeWidgetItem
139  * signaled by currentItemChanged
140  */
141 void Jobs::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
142 {
143    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
144    if (m_checkcurwidget) {
145       /* The Previous item */
146       if (previouswidgetitem) { /* avoid a segfault if first time */
147          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
148          if (treedepth == 1){
149             mp_treeWidget->removeAction(actionConsoleListFiles);
150             mp_treeWidget->removeAction(actionConsoleListVolumes);
151             mp_treeWidget->removeAction(actionConsoleListNextVolume);
152             mp_treeWidget->removeAction(actionConsoleEnableJob);
153             mp_treeWidget->removeAction(actionConsoleDisableJob);
154             mp_treeWidget->removeAction(actionConsoleCancel);
155             mp_treeWidget->removeAction(actionJobListQuery);
156          }
157       }
158
159       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
160       if (treedepth == 1){
161          /* set a hold variable to the client name in case the context sensitive
162           * menu is used */
163          m_currentlyselected=currentwidgetitem->text(0);
164          mp_treeWidget->addAction(actionConsoleListFiles);
165          mp_treeWidget->addAction(actionConsoleListVolumes);
166          mp_treeWidget->addAction(actionConsoleListNextVolume);
167          mp_treeWidget->addAction(actionConsoleEnableJob);
168          mp_treeWidget->addAction(actionConsoleDisableJob);
169          mp_treeWidget->addAction(actionConsoleCancel);
170          mp_treeWidget->addAction(actionJobListQuery);
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 Jobs::createContextMenu()
181 {
182    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
183    mp_treeWidget->addAction(actionRefreshJobs);
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(actionRefreshJobs, SIGNAL(triggered()), this,
189                 SLOT(populateTree()));
190    connect(actionConsoleListFiles, SIGNAL(triggered()), this, SLOT(consoleListFiles()));
191    connect(actionConsoleListVolumes, SIGNAL(triggered()), this, SLOT(consoleListVolume()));
192    connect(actionConsoleListNextVolume, SIGNAL(triggered()), this, SLOT(consoleListNextVolume()));
193    connect(actionConsoleEnableJob, SIGNAL(triggered()), this, SLOT(consoleEnable()));
194    connect(actionConsoleDisableJob, SIGNAL(triggered()), this, SLOT(consoleDisable()));
195    connect(actionConsoleCancel, SIGNAL(triggered()), this, SLOT(consoleCancel()));
196    connect(actionJobListQuery, SIGNAL(triggered()), this, SLOT(listJobs()));
197 }
198
199 /*
200  * Virtual function which is called when this page is visible on the stack
201  */
202 void Jobs::currentStackItem()
203 {
204    populateTree();
205    if(!m_populated) {
206       /* Create the context menu for the client tree */
207       m_populated=true;
208    }
209 }
210
211 /*
212  * The following functions are slots responding to users clicking on the context
213  * sensitive menu
214  */
215
216 void Jobs::consoleListFiles()
217 {
218    QString cmd("list files job=");
219    cmd += m_currentlyselected;
220    consoleCommand(cmd);
221 }
222
223 void Jobs::consoleListVolume()
224 {
225    QString cmd("list volumes job=");
226    cmd += m_currentlyselected;
227    consoleCommand(cmd);
228 }
229
230 void Jobs::consoleListNextVolume()
231 {
232    QString cmd("list nextvolume job=");
233    cmd += m_currentlyselected;
234    consoleCommand(cmd);
235 }
236
237 void Jobs::consoleEnable()
238 {
239    QString cmd("enable job=");
240    cmd += m_currentlyselected;
241    consoleCommand(cmd);
242 }
243
244 void Jobs::consoleDisable()
245 {
246    QString cmd("disable job=");
247    cmd += m_currentlyselected;
248    consoleCommand(cmd);
249 }
250
251 void Jobs::consoleCancel()
252 {
253    QString cmd("cancel job=");
254    cmd += m_currentlyselected;
255    consoleCommand(cmd);
256 }
257
258 void Jobs::listJobs()
259 {
260    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
261    mainWin->createPageJobList("", "", m_currentlyselected, "", parentItem);
262 }