]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/medialist/medialist.cpp
Add columns to the tree to show read/write time, scratch pool, recycle count.
[bacula/bacula] / bacula / src / qt-console / medialist / medialist.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 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 Kern Sibbald.
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 "bat.h"
39 #include <QAbstractEventDispatcher>
40 #include <QMenu>
41 #include <math.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    if (m_populated)
80       writeExpandedSettings();
81    m_populated = true;
82
83    Freeze frz(*mp_treeWidget); /* disable updating*/
84
85    QStringList headerlist = (QStringList()
86       << tr("Volume Name") << tr("Id") << tr("Status") << tr("Enabled") << tr("Bytes") << tr("Files")
87       << tr("Jobs") << tr("Retention") << tr("Media Type") << tr("Slot") << tr("Use Duration")
88       << tr("Max Jobs") << tr("Max Files") << tr("Max Bytes") << tr("Recycle")
89       << tr("Last Written") << tr("First Written") << tr("Read Time")
90       << tr("Write Time") << tr("Recycle Count") << tr("Recycle Pool") << tr("Scratch Pool"));
91
92    m_checkcurwidget = false;
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    QTreeWidgetItem *pooltreeitem;
108
109    /* Comma separated list of pools first */
110    bool first = true;
111    QString pool_comsep("");
112    foreach (QString pool_listItem, m_console->pool_list) {
113       if (first) {
114          pool_comsep += "'" + pool_listItem + "'";
115          first = false;
116       }
117       else
118          pool_comsep += ",'" + pool_listItem + "'";
119    }
120    /* Now use pool_comsep list to perform just one query */
121    if (pool_comsep != "") {
122       query =  "SELECT Pool.Name AS pul,"
123          " Media.VolumeName AS Media, "
124          " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
125          " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
126          " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
127          " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
128          " Media.InChanger AS InChanger, Media.Slot AS Slot, "
129          " Media.VolUseDuration AS UseDuration,"
130          " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
131          " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
132          " Media.LastWritten AS LastWritten,"
133          " Media.FirstWritten AS FirstWritten,"
134          " (VolReadTime/1000000) AS ReadTime, (VolWriteTime/1000000) AS WriteTime,"
135          " RecycleCount AS ReCyCount,"
136          " RecPool.Name AS RecyclePool, ScrPool.Name AS ScratchPool"
137          " FROM Media"
138          " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
139          " LEFT OUTER JOIN Pool AS RecPool ON (Media.RecyclePoolId=RecPool.PoolId)"
140          " LEFT OUTER JOIN Pool AS ScrPool ON (Media.ScratchPoolId=ScrPool.PoolId)"
141          " WHERE ";
142       query += " Pool.Name IN (" + pool_comsep + ")";
143       query += " ORDER BY Pool.Name, Media";
144
145       if (mainWin->m_sqlDebug) {
146          Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
147       }
148       QStringList results;
149       int counter = 0;
150       if (m_console->sql_cmd(query, results)) {
151          QStringList fieldlist;
152          QString prev_pool("");
153          QString this_pool("");
154
155          /* Iterate through the lines of results. */
156          foreach (QString resultline, results) {
157             fieldlist = resultline.split("\t");
158             this_pool = fieldlist.takeFirst();
159             if (prev_pool != this_pool) {
160                prev_pool = this_pool;
161                pooltreeitem = new QTreeWidgetItem(m_topItem);
162                pooltreeitem->setText(0, this_pool);
163                pooltreeitem->setData(0, Qt::UserRole, 1);
164             }
165             if(settings.contains(this_pool)) {
166                pooltreeitem->setExpanded(settings.value(this_pool).toBool());
167             } else {
168                pooltreeitem->setExpanded(true);
169             }
170
171             if (fieldlist.size() < 18)
172                continue; // some fields missing, ignore row
173
174             int index = 0;
175             TreeItemFormatter mediaitem(*pooltreeitem, 2);
176   
177             /* Iterate through fields in the record */
178             QStringListIterator fld(fieldlist);
179
180             /* volname */
181             mediaitem.setTextFld(index++, fld.next()); 
182
183             /* id */
184             mediaitem.setNumericFld(index++, fld.next()); 
185
186             /* status */
187             mediaitem.setVolStatusFld(index++, fld.next());
188
189             /* enabled */
190             mediaitem.setBoolFld(index++, fld.next());
191
192             /* bytes */
193             mediaitem.setBytesFld(index++, fld.next());
194
195             /* files */
196             mediaitem.setNumericFld(index++, fld.next()); 
197
198             /* jobs */
199             mediaitem.setNumericFld(index++, fld.next()); 
200
201             /* retention */
202             mediaitem.setDurationFld(index++, fld.next());
203
204             /* media type */
205             mediaitem.setTextFld(index++, fld.next()); 
206
207             /* inchanger + slot */
208             int inchanger = fld.next().toInt();
209             if (inchanger) {
210                mediaitem.setNumericFld(index++, fld.next()); 
211             }
212             else {
213                /* volume not in changer, show blank slot */
214                mediaitem.setNumericFld(index++, ""); 
215                fld.next();
216             }
217
218             /* use duration */
219             mediaitem.setDurationFld(index++, fld.next());
220
221             /* max jobs */
222             mediaitem.setNumericFld(index++, fld.next()); 
223
224             /* max files */
225             mediaitem.setNumericFld(index++, fld.next()); 
226
227             /* max bytes */
228             mediaitem.setBytesFld(index++, fld.next());
229
230             /* recycle */
231             mediaitem.setBoolFld(index++, fld.next());
232
233             /* last written */
234             mediaitem.setTextFld(index++, fld.next()); 
235
236             /* first written */
237             mediaitem.setTextFld(index++, fld.next()); 
238
239             /* read time */
240             mediaitem.setDurationFld(index++, fld.next());
241
242             /* write time */
243             mediaitem.setDurationFld(index++, fld.next());
244
245             /* Recycle Count */
246             mediaitem.setNumericFld(index++, fld.next()); 
247
248             /* recycle pool */
249             mediaitem.setTextFld(index++, fld.next()); 
250
251             /* Scratch pool */
252             mediaitem.setTextFld(index++, fld.next()); 
253
254          } /* foreach resultline */
255          counter += 1;
256       } /* if results from query */
257    } /* foreach pool_listItem */
258    settings.endGroup();
259    /* Resize the columns */
260    for(int cnter=0; cnter<headerlist.count(); cnter++) {
261       mp_treeWidget->resizeColumnToContents(cnter);
262    }
263 }
264
265 /*
266  * Called from the signal of the context sensitive menu!
267  */
268 void MediaList::editVolume()
269 {
270    MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
271    connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
272 }
273
274 /*
275  * Called from the signal of the context sensitive menu!
276  */
277 void MediaList::showJobs()
278 {
279    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
280    mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
281 }
282
283 /*
284  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
285  * The tree has been populated.
286  */
287 void MediaList::PgSeltreeWidgetClicked()
288 {
289    if (!m_populated) {
290       populateTree();
291       createContextMenu();
292    }
293 }
294
295 /*
296  * Added to set the context menu policy based on currently active treeWidgetItem
297  * signaled by currentItemChanged
298  */
299 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
300 {
301    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
302    if (m_checkcurwidget) {
303       /* The Previous item */
304       if (previouswidgetitem) { /* avoid a segfault if first time */
305          foreach(QAction* mediaAction, mp_treeWidget->actions()) {
306             mp_treeWidget->removeAction(mediaAction);
307          }
308       }
309
310       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
311       m_currentVolumeName=currentwidgetitem->text(0);
312       mp_treeWidget->addAction(actionRefreshMediaList);
313       if (treedepth == 2){
314          m_currentVolumeId=currentwidgetitem->text(1);
315          mp_treeWidget->addAction(actionEditVolume);
316          mp_treeWidget->addAction(actionListJobsOnVolume);
317          mp_treeWidget->addAction(actionDeleteVolume);
318          mp_treeWidget->addAction(actionPruneVolume);
319          mp_treeWidget->addAction(actionPurgeVolume);
320          mp_treeWidget->addAction(actionRelabelVolume);
321          mp_treeWidget->addAction(actionVolumeFromPool);
322       } else if (treedepth == 1) {
323          mp_treeWidget->addAction(actionAllVolumesFromPool);
324       }
325    }
326 }
327
328 /* 
329  * Setup a context menu 
330  * Made separate from populate so that it would not create context menu over and
331  * over as the tree is repopulated.
332  */
333 void MediaList::createContextMenu()
334 {
335    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
336    connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
337    connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
338    connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
339    connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
340    connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
341    connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
342    connect(mp_treeWidget, SIGNAL(
343            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
344            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
345    /* connect to the action specific to this pages class */
346    connect(actionRefreshMediaList, SIGNAL(triggered()), this,
347                 SLOT(populateTree()));
348    connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
349    connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
350    connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
351 }
352
353 /*
354  * Virtual function which is called when this page is visible on the stack
355  */
356 void MediaList::currentStackItem()
357 {
358    if(!m_populated) {
359       populateTree();
360       /* Create the context menu for the medialist tree */
361       createContextMenu();
362    }
363 }
364
365 /*
366  * Called from the signal of the context sensitive menu to delete a volume!
367  */
368 void MediaList::deleteVolume()
369 {
370    if (QMessageBox::warning(this, "Bat",
371       tr("Are you sure you want to delete??  !!!.\n"
372 "This delete command is used to delete a Volume record and all associated catalog"
373 " records that were created. This command operates only on the Catalog"
374 " database and has no effect on the actual data written to a Volume. This"
375 " command can be dangerous and we strongly recommend that you do not use"
376 " it unless you know what you are doing.  All Jobs and all associated"
377 " records (File and JobMedia) will be deleted from the catalog."
378       "Press OK to proceed with delete operation.?"),
379       QMessageBox::Ok | QMessageBox::Cancel)
380       == QMessageBox::Cancel) { return; }
381
382    QString cmd("delete volume=");
383    cmd += m_currentVolumeName;
384    consoleCommand(cmd);
385 }
386
387 /*
388  * Called from the signal of the context sensitive menu to purge!
389  */
390 void MediaList::purgeVolume()
391 {
392    if (QMessageBox::warning(this, "Bat",
393       tr("Are you sure you want to purge ??  !!!.\n"
394 "The Purge command will delete associated Catalog database records from Jobs and"
395 " Volumes without considering the retention period. Purge  works only on the"
396 " Catalog database and does not affect data written to Volumes. This command can"
397 " be dangerous because you can delete catalog records associated with current"
398 " backups of files, and we recommend that you do not use it unless you know what"
399 " you are doing.\n"
400       "Press OK to proceed with the purge operation?"),
401       QMessageBox::Ok | QMessageBox::Cancel)
402       == QMessageBox::Cancel) { return; }
403
404    QString cmd("purge volume=");
405    cmd += m_currentVolumeName;
406    consoleCommand(cmd);
407    populateTree();
408 }
409
410 /*
411  * Called from the signal of the context sensitive menu to prune!
412  */
413 void MediaList::pruneVolume()
414 {
415    new prunePage(m_currentVolumeName, "");
416 }
417
418 /*
419  * Called from the signal of the context sensitive menu to relabel!
420  */
421 void MediaList::relabelVolume()
422 {
423    setConsoleCurrent();
424    new relabelDialog(m_console, m_currentVolumeName);
425 }
426
427 /*
428  * Called from the signal of the context sensitive menu to purge!
429  */
430 void MediaList::allVolumesFromPool()
431 {
432    QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
433    consoleCommand(cmd);
434    populateTree();
435 }
436
437 void MediaList::allVolumes()
438 {
439    QString cmd = "update volume allfrompools";
440    consoleCommand(cmd);
441    populateTree();
442 }
443
444 /*
445  * Called from the signal of the context sensitive menu to purge!
446  */
447 void MediaList::volumeFromPool()
448 {
449    QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
450    QTreeWidgetItem *parent = currentItem->parent();
451    QString pool = parent->text(0);
452    QString cmd;
453    cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
454    consoleCommand(cmd);
455    populateTree();
456 }
457
458 /*
459  * Write settings to save expanded states of the pools
460  */
461 void MediaList::writeExpandedSettings()
462 {
463    if (m_topItem) {
464       QSettings settings(m_console->m_dir->name(), "bat");
465       settings.beginGroup("MediaListTreeExpanded");
466       int childcount = m_topItem->childCount();
467       for (int cnt=0; cnt<childcount; cnt++) {
468          QTreeWidgetItem *poolitem = m_topItem->child(cnt);
469          settings.setValue(poolitem->text(0), poolitem->isExpanded());
470       }
471       settings.endGroup();
472    }
473 }