2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
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
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.
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
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.
34 * Dirk Bartley, March 2007
38 #include <QAbstractEventDispatcher>
42 #include "medialist.h"
43 #include "mediaedit/mediaedit.h"
44 #include "joblist/joblist.h"
45 #include "relabel/relabel.h"
47 #include "util/fmtwidgetitem.h"
49 MediaList::MediaList()
54 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
55 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
57 /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
59 m_checkcurwidget = true;
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);
67 MediaList::~MediaList()
70 writeExpandedSettings();
74 * The main meat of the class!! The function that querries the director and
75 * creates the widgets with appropriate values.
77 void MediaList::populateTree()
80 writeExpandedSettings();
83 if (!m_console->preventInUseConnect())
86 Freeze frz(*mp_treeWidget); /* disable updating*/
88 QStringList headerlist = (QStringList()
89 << tr("Volume Name") << tr("Id") << tr("Status") << tr("Enabled") << tr("Bytes") << tr("Files")
90 << tr("Jobs") << tr("Retention") << tr("Media Type") << tr("Slot") << tr("Use Duration")
91 << tr("Max Jobs") << tr("Max Files") << tr("Max Bytes") << tr("Recycle")
92 << tr("RecyclePool") << tr("Last Written"));
94 m_checkcurwidget = false;
95 mp_treeWidget->clear();
96 m_checkcurwidget = true;
97 mp_treeWidget->setColumnCount(headerlist.count());
98 m_topItem = new QTreeWidgetItem(mp_treeWidget);
99 m_topItem->setText(0, tr("Pools"));
100 m_topItem->setData(0, Qt::UserRole, 0);
101 m_topItem->setExpanded(true);
103 mp_treeWidget->setHeaderLabels(headerlist);
105 QSettings settings(m_console->m_dir->name(), "bat");
106 settings.beginGroup("MediaListTreeExpanded");
109 QTreeWidgetItem *pooltreeitem;
110 foreach (QString pool_listItem, m_console->pool_list) {
111 pooltreeitem = new QTreeWidgetItem(m_topItem);
112 pooltreeitem->setText(0, pool_listItem);
113 pooltreeitem->setData(0, Qt::UserRole, 1);
114 if(settings.contains(pool_listItem)) {
115 pooltreeitem->setExpanded(settings.value(pool_listItem).toBool());
117 pooltreeitem->setExpanded(true);
120 query = "SELECT Media.VolumeName AS Media, "
121 " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
122 " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
123 " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
124 " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
125 " Media.InChanger AS InChanger, Media.Slot AS Slot, "
126 " Media.VolUseDuration AS UseDuration,"
127 " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
128 " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
129 " Pol.Name AS RecyclePool, Media.LastWritten AS LastWritten"
131 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
132 " LEFT OUTER JOIN Pool AS Pol ON (Media.RecyclePoolId=Pol.PoolId)"
134 query += " Pool.Name='" + pool_listItem + "'";
135 query += " ORDER BY Media";
137 if (mainWin->m_sqlDebug) {
138 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
141 if (m_console->sql_cmd(query, results)) {
142 QStringList fieldlist;
144 /* Iterate through the lines of results. */
145 foreach (QString resultline, results) {
146 fieldlist = resultline.split("\t");
148 if (fieldlist.size() < 18)
149 continue; // some fields missing, ignore row
152 TreeItemFormatter mediaitem(*pooltreeitem, 2);
154 /* Iterate through fields in the record */
155 QStringListIterator fld(fieldlist);
158 mediaitem.setTextFld(index++, fld.next());
161 mediaitem.setNumericFld(index++, fld.next());
164 mediaitem.setVolStatusFld(index++, fld.next());
167 mediaitem.setBoolFld(index++, fld.next());
170 mediaitem.setBytesFld(index++, fld.next());
173 mediaitem.setNumericFld(index++, fld.next());
176 mediaitem.setNumericFld(index++, fld.next());
179 mediaitem.setDurationFld(index++, fld.next());
182 mediaitem.setTextFld(index++, fld.next());
184 /* inchanger + slot */
185 int inchanger = fld.next().toInt();
187 mediaitem.setNumericFld(index++, fld.next());
190 /* volume not in changer, show blank slot */
191 mediaitem.setNumericFld(index++, "");
196 mediaitem.setDurationFld(index++, fld.next());
199 mediaitem.setNumericFld(index++, fld.next());
202 mediaitem.setNumericFld(index++, fld.next());
205 mediaitem.setBytesFld(index++, fld.next());
208 mediaitem.setBoolFld(index++, fld.next());
211 mediaitem.setTextFld(index++, fld.next());
214 mediaitem.setTextFld(index++, fld.next());
216 } /* foreach resultline */
217 } /* if results from query */
218 } /* foreach pool_listItem */
220 /* Resize the columns */
221 for(int cnter=0; cnter<headerlist.count(); cnter++) {
222 mp_treeWidget->resizeColumnToContents(cnter);
227 * Called from the signal of the context sensitive menu!
229 void MediaList::editVolume()
231 MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
232 connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
236 * Called from the signal of the context sensitive menu!
238 void MediaList::showJobs()
240 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
241 mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
245 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
246 * The tree has been populated.
248 void MediaList::PgSeltreeWidgetClicked()
257 * Added to set the context menu policy based on currently active treeWidgetItem
258 * signaled by currentItemChanged
260 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
262 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
263 if (m_checkcurwidget) {
264 /* The Previous item */
265 if (previouswidgetitem) { /* avoid a segfault if first time */
266 foreach(QAction* mediaAction, mp_treeWidget->actions()) {
267 mp_treeWidget->removeAction(mediaAction);
271 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
272 m_currentVolumeName=currentwidgetitem->text(0);
273 mp_treeWidget->addAction(actionRefreshMediaList);
275 m_currentVolumeId=currentwidgetitem->text(1);
276 mp_treeWidget->addAction(actionEditVolume);
277 mp_treeWidget->addAction(actionListJobsOnVolume);
278 mp_treeWidget->addAction(actionDeleteVolume);
279 mp_treeWidget->addAction(actionPruneVolume);
280 mp_treeWidget->addAction(actionPurgeVolume);
281 mp_treeWidget->addAction(actionRelabelVolume);
282 mp_treeWidget->addAction(actionVolumeFromPool);
283 } else if (treedepth == 1) {
284 mp_treeWidget->addAction(actionAllVolumesFromPool);
290 * Setup a context menu
291 * Made separate from populate so that it would not create context menu over and
292 * over as the tree is repopulated.
294 void MediaList::createContextMenu()
296 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
297 connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
298 connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
299 connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
300 connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
301 connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
302 connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
303 connect(mp_treeWidget, SIGNAL(
304 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
305 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
306 /* connect to the action specific to this pages class */
307 connect(actionRefreshMediaList, SIGNAL(triggered()), this,
308 SLOT(populateTree()));
309 connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
310 connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
311 connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
315 * Virtual function which is called when this page is visible on the stack
317 void MediaList::currentStackItem()
321 /* Create the context menu for the medialist tree */
327 * Called from the signal of the context sensitive menu to delete a volume!
329 void MediaList::deleteVolume()
331 if (QMessageBox::warning(this, "Bat",
332 tr("Are you sure you want to delete?? !!!.\n"
333 "This delete command is used to delete a Volume record and all associated catalog"
334 " records that were created. This command operates only on the Catalog"
335 " database and has no effect on the actual data written to a Volume. This"
336 " command can be dangerous and we strongly recommend that you do not use"
337 " it unless you know what you are doing. All Jobs and all associated"
338 " records (File and JobMedia) will be deleted from the catalog."
339 "Press OK to proceed with delete operation.?"),
340 QMessageBox::Ok | QMessageBox::Cancel)
341 == QMessageBox::Cancel) { return; }
343 QString cmd("delete volume=");
344 cmd += m_currentVolumeName;
349 * Called from the signal of the context sensitive menu to purge!
351 void MediaList::purgeVolume()
353 if (QMessageBox::warning(this, "Bat",
354 tr("Are you sure you want to purge ?? !!!.\n"
355 "The Purge command will delete associated Catalog database records from Jobs and"
356 " Volumes without considering the retention period. Purge works only on the"
357 " Catalog database and does not affect data written to Volumes. This command can"
358 " be dangerous because you can delete catalog records associated with current"
359 " backups of files, and we recommend that you do not use it unless you know what"
361 "Press OK to proceed with the purge operation?"),
362 QMessageBox::Ok | QMessageBox::Cancel)
363 == QMessageBox::Cancel) { return; }
365 QString cmd("purge volume=");
366 cmd += m_currentVolumeName;
372 * Called from the signal of the context sensitive menu to prune!
374 void MediaList::pruneVolume()
376 new prunePage(m_currentVolumeName, "");
380 * Called from the signal of the context sensitive menu to relabel!
382 void MediaList::relabelVolume()
385 new relabelDialog(m_console, m_currentVolumeName);
389 * Called from the signal of the context sensitive menu to purge!
391 void MediaList::allVolumesFromPool()
393 QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
398 void MediaList::allVolumes()
400 QString cmd = "update volume allfrompools";
406 * Called from the signal of the context sensitive menu to purge!
408 void MediaList::volumeFromPool()
410 QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
411 QTreeWidgetItem *parent = currentItem->parent();
412 QString pool = parent->text(0);
414 cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
420 * Write settings to save expanded states of the pools
422 void MediaList::writeExpandedSettings()
424 QSettings settings(m_console->m_dir->name(), "bat");
425 settings.beginGroup("MediaListTreeExpanded");
426 int childcount = m_topItem->childCount();
427 for (int cnt=0; cnt<childcount; cnt++) {
428 QTreeWidgetItem *poolitem = m_topItem->child(cnt);
429 settings.setValue(poolitem->text(0), poolitem->isExpanded());