]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/medialist/medialist.cpp
Riccardo' patch for formatting text.
[bacula/bacula] / bacula / src / qt-console / medialist / medialist.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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$
31  *
32  *  MediaList Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 #include <QAbstractEventDispatcher>
39 #include <QMenu>
40 #include <math.h>
41 #include "bat.h"
42 #include "medialist.h"
43 #include "mediaedit/mediaedit.h"
44 #include "joblist/joblist.h"
45 #include "relabel/relabel.h"
46 #include "run/run.h"
47 #include "util/fmtwidgetitem.h"
48
49 MediaList::MediaList()
50 {
51    setupUi(this);
52    m_name = tr("Media");
53    pgInitialize();
54    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
55    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
56
57    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
58    m_populated = false;
59    m_checkcurwidget = true;
60    m_closeable = false;
61    /* add context sensitive menu items specific to this classto the page
62     * selector tree. m_contextActions is QList of QActions */
63    m_contextActions.append(actionRefreshMediaList);
64    dockPage();
65 }
66
67 MediaList::~MediaList()
68 {
69    if (m_populated)
70       writeExpandedSettings();
71 }
72
73 /*
74  * The main meat of the class!!  The function that querries the director and 
75  * creates the widgets with appropriate values.
76  */
77 void MediaList::populateTree()
78 {
79    QTreeWidgetItem *pooltreeitem;
80
81    if (!m_console->preventInUseConnect())
82        return;
83
84    QStringList headerlist = (QStringList()
85       << tr("Volume Name") << tr("Id") << tr("Status") << tr("Enabled") << tr("Bytes") << tr("Files")
86       << tr("Jobs") << tr("Retention") << tr("Media Type") << tr("Slot") << tr("Use Duration")
87       << tr("Max Jobs") << tr("Max Files") << tr("Max Bytes") << tr("Recycle")
88       << tr("RecyclePool") << tr("Last Written"));
89
90    m_checkcurwidget = false;
91    if (m_populated)
92       writeExpandedSettings();
93    mp_treeWidget->clear();
94    m_checkcurwidget = true;
95    mp_treeWidget->setColumnCount(headerlist.count());
96    m_topItem = new QTreeWidgetItem(mp_treeWidget);
97    m_topItem->setText(0, tr("Pools"));
98    m_topItem->setData(0, Qt::UserRole, 0);
99    m_topItem->setExpanded(true);
100    
101    mp_treeWidget->setHeaderLabels(headerlist);
102
103    QSettings settings(m_console->m_dir->name(), "bat");
104    settings.beginGroup("MediaListTreeExpanded");
105    QString query;
106
107    foreach (QString pool_listItem, m_console->pool_list) {
108       pooltreeitem = new QTreeWidgetItem(m_topItem);
109       pooltreeitem->setText(0, pool_listItem);
110       pooltreeitem->setData(0, Qt::UserRole, 1);
111       if(settings.contains(pool_listItem)) {
112          pooltreeitem->setExpanded(settings.value(pool_listItem).toBool());
113       } else {
114          pooltreeitem->setExpanded(true);
115       }
116
117       query =  "SELECT Media.VolumeName AS Media, "
118          " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
119          " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
120          " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
121          " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
122          " Media.InChanger AS InChanger, Media.Slot AS Slot, "
123          " Media.VolUseDuration AS UseDuration,"
124          " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
125          " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
126          " Pol.Name AS RecyclePool, Media.LastWritten AS LastWritten"
127          " FROM Media"
128          " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
129          " LEFT OUTER JOIN Pool AS Pol ON (Media.RecyclePoolId=Pol.PoolId)"
130          " WHERE";
131       query += " Pool.Name='" + pool_listItem + "'";
132       query += " ORDER BY Media";
133    
134       if (mainWin->m_sqlDebug) {
135          Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
136       }
137       QStringList results;
138       if (m_console->sql_cmd(query, results)) {
139          QStringList fieldlist;
140
141          /* Iterate through the lines of results. */
142          foreach (QString resultline, results) {
143             fieldlist = resultline.split("\t");
144
145             if (fieldlist.size() < 18)
146                continue; // some fields missing, ignore row
147
148             int index = 0;
149             TreeItemFormatter mediaitem(*pooltreeitem, 2);
150   
151             /* Iterate through fields in the record */
152             QStringListIterator fld(fieldlist);
153
154             /* volname */
155             mediaitem.setTextFld(index++, fld.next()); 
156
157             /* id */
158             mediaitem.setNumericFld(index++, fld.next()); 
159
160             /* status */
161             mediaitem.setVolStatusFld(index++, fld.next());
162
163             /* enabled */
164             mediaitem.setBoolFld(index++, fld.next());
165
166             /* bytes */
167             mediaitem.setBytesFld(index++, fld.next());
168
169             /* files */
170             mediaitem.setNumericFld(index++, fld.next()); 
171
172             /* jobs */
173             mediaitem.setNumericFld(index++, fld.next()); 
174
175             /* retention */
176             mediaitem.setDurationFld(index++, fld.next());
177
178             /* media type */
179             mediaitem.setTextFld(index++, fld.next()); 
180
181             /* inchanger + slot */
182             int inchanger = fld.next().toInt();
183             if (inchanger) {
184                mediaitem.setNumericFld(index++, fld.next()); 
185             }
186             else {
187                /* volume not in changer, show blank slot */
188                mediaitem.setNumericFld(index++, ""); 
189                fld.next();
190             }
191
192             /* use duration */
193             mediaitem.setDurationFld(index++, fld.next());
194
195             /* max jobs */
196             mediaitem.setNumericFld(index++, fld.next()); 
197
198             /* max files */
199             mediaitem.setNumericFld(index++, fld.next()); 
200
201             /* max bytes */
202             mediaitem.setBytesFld(index++, fld.next());
203
204             /* recycle */
205             mediaitem.setBoolFld(index++, fld.next());
206
207             /* recycle pool */
208             mediaitem.setTextFld(index++, fld.next()); 
209
210             /* last written */
211             mediaitem.setTextFld(index++, fld.next()); 
212
213          } /* foreach resultline */
214       } /* if results from query */
215    } /* foreach pool_listItem */
216    settings.endGroup();
217    /* Resize the columns */
218    for(int cnter=0; cnter<headerlist.count(); cnter++) {
219       mp_treeWidget->resizeColumnToContents(cnter);
220    }
221 }
222
223 /*
224  * Called from the signal of the context sensitive menu!
225  */
226 void MediaList::editVolume()
227 {
228    MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
229    connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
230 }
231
232 /*
233  * Called from the signal of the context sensitive menu!
234  */
235 void MediaList::showJobs()
236 {
237    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
238    mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
239 }
240
241 /*
242  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
243  * The tree has been populated.
244  */
245 void MediaList::PgSeltreeWidgetClicked()
246 {
247    if (!m_populated) {
248       populateTree();
249       createContextMenu();
250       m_populated=true;
251    }
252 }
253
254 /*
255  * Added to set the context menu policy based on currently active treeWidgetItem
256  * signaled by currentItemChanged
257  */
258 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
259 {
260    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
261    if (m_checkcurwidget) {
262       /* The Previous item */
263       if (previouswidgetitem) { /* avoid a segfault if first time */
264          foreach(QAction* mediaAction, mp_treeWidget->actions()) {
265             mp_treeWidget->removeAction(mediaAction);
266          }
267       }
268
269       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
270       m_currentVolumeName=currentwidgetitem->text(0);
271       mp_treeWidget->addAction(actionRefreshMediaList);
272       if (treedepth == 2){
273          m_currentVolumeId=currentwidgetitem->text(1);
274          mp_treeWidget->addAction(actionEditVolume);
275          mp_treeWidget->addAction(actionListJobsOnVolume);
276          mp_treeWidget->addAction(actionDeleteVolume);
277          mp_treeWidget->addAction(actionPruneVolume);
278          mp_treeWidget->addAction(actionPurgeVolume);
279          mp_treeWidget->addAction(actionRelabelVolume);
280          mp_treeWidget->addAction(actionVolumeFromPool);
281       } else if (treedepth == 1) {
282          mp_treeWidget->addAction(actionAllVolumesFromPool);
283       }
284    }
285 }
286
287 /* 
288  * Setup a context menu 
289  * Made separate from populate so that it would not create context menu over and
290  * over as the tree is repopulated.
291  */
292 void MediaList::createContextMenu()
293 {
294    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
295    connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
296    connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
297    connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
298    connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
299    connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
300    connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
301    connect(mp_treeWidget, SIGNAL(
302            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
303            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
304    /* connect to the action specific to this pages class */
305    connect(actionRefreshMediaList, SIGNAL(triggered()), this,
306                 SLOT(populateTree()));
307    connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
308    connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
309    connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
310 }
311
312 /*
313  * Virtual function which is called when this page is visible on the stack
314  */
315 void MediaList::currentStackItem()
316 {
317    if(!m_populated) {
318       populateTree();
319       /* Create the context menu for the medialist tree */
320       createContextMenu();
321       m_populated=true;
322    }
323 }
324
325 /*
326  * Called from the signal of the context sensitive menu to delete a volume!
327  */
328 void MediaList::deleteVolume()
329 {
330    if (QMessageBox::warning(this, "Bat",
331       tr("Are you sure you want to delete??  !!!.\n"
332 "This delete command is used to delete a Volume record and all associated catalog"
333 " records that were created. This command operates only on the Catalog"
334 " database and has no effect on the actual data written to a Volume. This"
335 " command can be dangerous and we strongly recommend that you do not use"
336 " it unless you know what you are doing.  All Jobs and all associated"
337 " records (File and JobMedia) will be deleted from the catalog."
338       "Press OK to proceed with delete operation.?"),
339       QMessageBox::Ok | QMessageBox::Cancel)
340       == QMessageBox::Cancel) { return; }
341
342    QString cmd("delete volume=");
343    cmd += m_currentVolumeName;
344    consoleCommand(cmd);
345 }
346
347 /*
348  * Called from the signal of the context sensitive menu to purge!
349  */
350 void MediaList::purgeVolume()
351 {
352    if (QMessageBox::warning(this, "Bat",
353       tr("Are you sure you want to purge ??  !!!.\n"
354 "The Purge command will delete associated Catalog database records from Jobs and"
355 " Volumes without considering the retention period. Purge  works only on the"
356 " Catalog database and does not affect data written to Volumes. This command can"
357 " be dangerous because you can delete catalog records associated with current"
358 " backups of files, and we recommend that you do not use it unless you know what"
359 " you are doing.\n"
360       "Press OK to proceed with the purge operation?"),
361       QMessageBox::Ok | QMessageBox::Cancel)
362       == QMessageBox::Cancel) { return; }
363
364    QString cmd("purge volume=");
365    cmd += m_currentVolumeName;
366    consoleCommand(cmd);
367    populateTree();
368 }
369
370 /*
371  * Called from the signal of the context sensitive menu to prune!
372  */
373 void MediaList::pruneVolume()
374 {
375    new prunePage(m_currentVolumeName, "");
376 }
377
378 /*
379  * Called from the signal of the context sensitive menu to relabel!
380  */
381 void MediaList::relabelVolume()
382 {
383    setConsoleCurrent();
384    new relabelDialog(m_console, m_currentVolumeName);
385 }
386
387 /*
388  * Called from the signal of the context sensitive menu to purge!
389  */
390 void MediaList::allVolumesFromPool()
391 {
392    QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
393    consoleCommand(cmd);
394    populateTree();
395 }
396
397 void MediaList::allVolumes()
398 {
399    QString cmd = "update volume allfrompools";
400    consoleCommand(cmd);
401    populateTree();
402 }
403
404 /*
405  * Called from the signal of the context sensitive menu to purge!
406  */
407 void MediaList::volumeFromPool()
408 {
409    QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
410    QTreeWidgetItem *parent = currentItem->parent();
411    QString pool = parent->text(0);
412    QString cmd;
413    cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
414    consoleCommand(cmd);
415    populateTree();
416 }
417
418 /*
419  * Write settings to save expanded states of the pools
420  */
421 void MediaList::writeExpandedSettings()
422 {
423    QSettings settings(m_console->m_dir->name(), "bat");
424    settings.beginGroup("MediaListTreeExpanded");
425    int childcount = m_topItem->childCount();
426    for (int cnt=0; cnt<childcount; cnt++) {
427       QTreeWidgetItem *poolitem = m_topItem->child(cnt);
428       settings.setValue(poolitem->text(0), poolitem->isExpanded());
429    }
430    settings.endGroup();
431 }