]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/joblist/joblist.cpp
A nicer way to resize the columns and rows in joblist. Edit a few comments
[bacula/bacula] / bacula / src / qt-console / joblist / joblist.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  *   Version $Id: joblist.h 4230 2007-02-21 20:07:37Z kerns $
30  *
31  *   Dirk Bartley, March 2007
32  */
33  
34 #include <QAbstractEventDispatcher>
35 #include <QTableWidgetItem>
36 #include "bat.h"
37 #include "joblist.h"
38
39
40 /*
41  * Constructor for the class
42  */
43 JobList::JobList(QString &mediaName, QString &clientname,
44          QTreeWidgetItem *parentTreeWidgetItem)
45 {
46    setupUi(this);
47    m_name = "Clients";
48    m_mediaName = mediaName;
49    m_clientName = clientname;
50    pgInitialize(parentTreeWidgetItem);
51    m_resultCount = 0;
52    m_populated = false;
53    m_closeable = false;
54    m_checkCurrentWidget = true;
55    createConnections();
56    setTitle();
57 }
58
59 /*
60  * The Meat of the class.
61  * This function will populate the QTableWidget, mp_tablewidget, with
62  * QTableWidgetItems representing the results of a query for what jobs exist on
63  * the media name passed from the constructor stored in m_mediaName.
64  */
65 void JobList::populateTable()
66 {
67    QStringList results;
68    QString resultline;
69    QBrush blackBrush(Qt::black);
70
71    /* Set up query QString and header QStringList */
72    QString query("");
73    query += "SELECT Job.Jobid AS Id, Job.Name AS JobName, Client.Name AS Client,"
74             " Job.Starttime AS JobStart, Job.Type AS JobType,"
75             " Job.Level AS BackupLevel, Job.Jobfiles AS FileCount,"
76             " Job.JobBytes AS Bytes, Job.JobStatus AS Status"
77             " FROM Job, JobMedia, Media, Client"
78             " WHERE JobMedia.JobId=Job.JobId and JobMedia.MediaId=Media.MediaId"
79             " and Client.ClientId=Job.ClientId";
80    if (m_mediaName != "") {
81       query += " and Media.VolumeName='" + m_mediaName + "'";
82       m_closeable=true;
83    }
84    if (m_clientName != "") {
85       query += " and Client.Name='" + m_clientName + "'";
86       m_closeable=true;
87    }
88    query += " ORDER BY Job.Starttime";
89    QStringList headerlist = (QStringList()
90       << "Job Id" << "Job Name" << "Client" << "Job Starttime" << "Job Type" 
91       << "Job Level" << "Job Files" << "Job Bytes" << "Job Status"  );
92
93    /* Initialize the QTableWidget */
94    m_checkCurrentWidget = false;
95    mp_tableWidget->clear();
96    m_checkCurrentWidget = true;
97    mp_tableWidget->setColumnCount(headerlist.size());
98    mp_tableWidget->setHorizontalHeaderLabels(headerlist);
99
100    /*  This could be a user preference debug message?? */
101    //printf("Query cmd : %s\n",query.toUtf8().data());
102    if (m_console->sql_cmd(query, results)) {
103       m_resultCount = results.count();
104
105       QTableWidgetItem* p_tableitem;
106       QString field;
107       QStringList fieldlist;
108       mp_tableWidget->setRowCount(results.size());
109
110       int row = 0;
111       /* Iterate through the record returned from the query */
112       foreach (resultline, results) {
113          fieldlist = resultline.split("\t");
114          int column = 0;
115          /* Iterate through fields in the record */
116          foreach (field, fieldlist) {
117             field = field.trimmed();  /* strip leading & trailing spaces */
118             p_tableitem = new QTableWidgetItem(field,1);
119             p_tableitem->setFlags(0);
120             p_tableitem->setForeground(blackBrush);
121             mp_tableWidget->setItem(row, column, p_tableitem);
122             column++;
123          }
124          row++;
125       }
126    } 
127    /* Resize the columns */
128    mp_tableWidget->resizeColumnsToContents();
129    mp_tableWidget->resizeRowsToContents();
130    mp_tableWidget->verticalHeader()->hide();
131    if ((m_mediaName != "") && (m_resultCount == 0)){
132       /* for context sensitive searches, let the user know if there were no
133        * results */
134       QMessageBox::warning(this, tr("Bat"),
135           tr("The Jobs query returned no results.\n"
136          "Press OK to continue?"), QMessageBox::Ok );
137    }
138 }
139
140 /*
141  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
142  * The tree has been populated.
143  */
144 void JobList::PgSeltreeWidgetClicked()
145 {
146    if (!m_populated) {
147       populateTable();
148       m_populated=true;
149    }
150 }
151
152 /*
153  *  Virtual function override of pages function which is called when this page
154  *  is visible on the stack
155  */
156 void JobList::currentStackItem()
157 {
158    if (!m_populated) {
159       populateTable();
160       m_contextActions.append(actionRefreshJobList);
161       m_populated=true;
162    }
163 }
164
165 /*
166  * Virtual Function to return the name for the medialist tree widget
167  */
168 void JobList::treeWidgetName(QString &desc)
169 {
170    if ((m_mediaName == "") && (m_clientName == "")) {
171       desc = "All Jobs";
172    } else {
173       desc = "Jobs ";
174       if (m_mediaName != "" ) {
175          desc += "on Volume " + m_mediaName;
176       }
177       if (m_clientName != "" ) {
178          desc += "of Client " + m_clientName;
179       }
180    }
181 }
182
183 /*
184  * This functions much line tableItemChanged for trees like the page selector,
185  * but I will do much less here
186  */
187 void JobList::tableItemChanged(QTableWidgetItem *currentItem, QTableWidgetItem * /*previousItem*/)
188 {
189    if (m_checkCurrentWidget) {
190       int row = currentItem->row();
191       QTableWidgetItem* jobitem = mp_tableWidget->item(row, 0);
192       m_currentJob = jobitem->text();
193    }
194 }
195
196 /*
197  * Function to create connections for context sensitive menu for this and
198  * the page selector
199  */
200 void JobList::createConnections()
201 {
202    /* connect to the action specific to this pages class that shows up in the 
203     * page selector tree */
204    connect(actionRefreshJobList, SIGNAL(triggered()), this,
205                 SLOT(populateTable()));
206    /* for the tableItemChanged to maintain m_currentJob */
207    connect(mp_tableWidget, SIGNAL(
208            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
209            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
210
211    /* Do what is required for the local context sensitive menu */
212
213
214    /* setContextMenuPolicy is required */
215    mp_tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
216
217    /* Add Actions */
218    mp_tableWidget->addAction(actionRefreshJobList);
219    mp_tableWidget->addAction(actionLongListJob);
220    mp_tableWidget->addAction(actionListJobid);
221    mp_tableWidget->addAction(actionListFilesOnJob);
222    mp_tableWidget->addAction(actionListJobMedia);
223    mp_tableWidget->addAction(actionListVolumes);
224    mp_tableWidget->addAction(actionDeleteJob);
225    mp_tableWidget->addAction(actionPurgeFiles);
226
227    /* Make Connections */
228    connect(actionLongListJob, SIGNAL(triggered()), this,
229                 SLOT(consoleLongListJob()));
230    connect(actionListJobid, SIGNAL(triggered()), this,
231                 SLOT(consoleListJobid()));
232    connect(actionListFilesOnJob, SIGNAL(triggered()), this,
233                 SLOT(consoleListFilesOnJob()));
234    connect(actionListJobMedia, SIGNAL(triggered()), this,
235                 SLOT(consoleListJobMedia()));
236    connect(actionListVolumes, SIGNAL(triggered()), this,
237                 SLOT(consoleListVolumes()));
238    connect(actionDeleteJob, SIGNAL(triggered()), this,
239                 SLOT(consoleDeleteJob()));
240    connect(actionPurgeFiles, SIGNAL(triggered()), this,
241                 SLOT(consolePurgeFiles()));
242 }
243
244 /*
245  * Functions to respond to local context sensitive menu sending console commands
246  * If I could figure out how to make these one function passing a string, Yaaaaaa
247  */
248 void JobList::consoleLongListJob()
249 {
250    QString cmd("llist jobid=");
251    cmd += m_currentJob;
252    consoleCommand(cmd);
253 }
254 void JobList::consoleListJobid()
255 {
256    QString cmd("list jobid=");
257    cmd += m_currentJob;
258    consoleCommand(cmd);
259 }
260 void JobList::consoleListFilesOnJob()
261 {
262    QString cmd("list files jobid=");
263    cmd += m_currentJob;
264    consoleCommand(cmd);
265 }
266 void JobList::consoleListJobMedia()
267 {
268    QString cmd("list jobmedia jobid=");
269    cmd += m_currentJob;
270    consoleCommand(cmd);
271 }
272 void JobList::consoleListVolumes()
273 {
274    QString cmd("list volumes jobid=");
275    cmd += m_currentJob;
276    consoleCommand(cmd);
277 }
278 void JobList::consoleDeleteJob()
279 {
280    QString cmd("delete job jobid=");
281    cmd += m_currentJob;
282    consoleCommand(cmd);
283 }
284 void JobList::consolePurgeFiles()
285 {
286    QString cmd("purge files jobid=");
287    cmd += m_currentJob;
288    consoleCommand(cmd);
289 }