2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2009 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
39 #include <QAbstractEventDispatcher>
42 #include "medialist.h"
43 #include "mediaedit/mediaedit.h"
44 #include "mediainfo/mediainfo.h"
45 #include "joblist/joblist.h"
46 #include "relabel/relabel.h"
48 #include "util/fmtwidgetitem.h"
50 MediaList::MediaList()
55 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
56 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
58 /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
60 m_checkcurwidget = true;
62 /* add context sensitive menu items specific to this classto the page
63 * selector tree. m_contextActions is QList of QActions */
64 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 Freeze frz(*mp_treeWidget); /* disable updating*/
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"));
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);
101 mp_treeWidget->setHeaderLabels(headerlist);
103 QSettings settings(m_console->m_dir->name(), "bat");
104 settings.beginGroup("MediaListTreeExpanded");
107 QTreeWidgetItem *pooltreeitem;
109 /* Comma separated list of pools first */
111 QString pool_comsep("");
112 foreach (QString pool_listItem, m_console->pool_list) {
114 pool_comsep += "'" + pool_listItem + "'";
118 pool_comsep += ",'" + pool_listItem + "'";
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"
138 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
139 " LEFT OUTER JOIN Pool AS RecPool ON (Media.RecyclePoolId=RecPool.PoolId)"
141 query += " Pool.Name IN (" + pool_comsep + ")";
142 query += " ORDER BY Pool.Name, Media";
144 if (mainWin->m_sqlDebug) {
145 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
149 if (m_console->sql_cmd(query, results)) {
150 QStringList fieldlist;
151 QString prev_pool("");
152 QString this_pool("");
154 /* Iterate through the lines of results. */
155 foreach (QString resultline, results) {
156 fieldlist = resultline.split("\t");
157 this_pool = fieldlist.takeFirst();
158 if (prev_pool != this_pool) {
159 prev_pool = this_pool;
160 pooltreeitem = new QTreeWidgetItem(m_topItem);
161 pooltreeitem->setText(0, this_pool);
162 pooltreeitem->setData(0, Qt::UserRole, 1);
164 if(settings.contains(this_pool)) {
165 pooltreeitem->setExpanded(settings.value(this_pool).toBool());
167 pooltreeitem->setExpanded(true);
170 if (fieldlist.size() < 18)
171 continue; // some fields missing, ignore row
174 TreeItemFormatter mediaitem(*pooltreeitem, 2);
176 /* Iterate through fields in the record */
177 QStringListIterator fld(fieldlist);
180 mediaitem.setTextFld(index++, fld.next());
183 mediaitem.setNumericFld(index++, fld.next());
186 mediaitem.setVolStatusFld(index++, fld.next());
189 mediaitem.setBoolFld(index++, fld.next());
192 mediaitem.setBytesFld(index++, fld.next());
195 mediaitem.setNumericFld(index++, fld.next());
198 mediaitem.setNumericFld(index++, fld.next());
201 mediaitem.setDurationFld(index++, fld.next());
204 mediaitem.setTextFld(index++, fld.next());
206 /* inchanger + slot */
207 int inchanger = fld.next().toInt();
209 mediaitem.setNumericFld(index++, fld.next());
212 /* volume not in changer, show blank slot */
213 mediaitem.setNumericFld(index++, "");
218 mediaitem.setDurationFld(index++, fld.next());
221 mediaitem.setNumericFld(index++, fld.next());
224 mediaitem.setNumericFld(index++, fld.next());
227 mediaitem.setBytesFld(index++, fld.next());
230 mediaitem.setBoolFld(index++, fld.next());
233 mediaitem.setTextFld(index++, fld.next());
236 mediaitem.setTextFld(index++, fld.next());
239 mediaitem.setDurationFld(index++, fld.next());
242 mediaitem.setDurationFld(index++, fld.next());
245 mediaitem.setNumericFld(index++, fld.next());
248 mediaitem.setTextFld(index++, fld.next());
250 } /* foreach resultline */
252 } /* if results from query */
253 } /* foreach pool_listItem */
255 /* Resize the columns */
256 for(int cnter=0; cnter<headerlist.count(); cnter++) {
257 mp_treeWidget->resizeColumnToContents(cnter);
262 * Called from the signal of the context sensitive menu!
264 void MediaList::editVolume()
266 MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
267 connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
271 * Called from the signal of the context sensitive menu!
273 void MediaList::showJobs()
275 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
276 mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
280 * Called from the signal of the context sensitive menu!
282 void MediaList::viewVolume()
284 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
285 MediaInfo* view = new MediaInfo(parentItem, m_currentVolumeName);
286 connect(view, SIGNAL(destroyed()), this, SLOT(populateTree()));
291 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
292 * The tree has been populated.
294 void MediaList::PgSeltreeWidgetClicked()
300 if (!isOnceDocked()) {
306 * Added to set the context menu policy based on currently active treeWidgetItem
307 * signaled by currentItemChanged
309 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
311 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
312 if (m_checkcurwidget) {
313 /* The Previous item */
314 if (previouswidgetitem) { /* avoid a segfault if first time */
315 foreach(QAction* mediaAction, mp_treeWidget->actions()) {
316 mp_treeWidget->removeAction(mediaAction);
320 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
321 m_currentVolumeName=currentwidgetitem->text(0);
322 mp_treeWidget->addAction(actionRefreshMediaList);
324 m_currentVolumeId=currentwidgetitem->text(1);
325 mp_treeWidget->addAction(actionEditVolume);
326 mp_treeWidget->addAction(actionListJobsOnVolume);
327 mp_treeWidget->addAction(actionDeleteVolume);
328 mp_treeWidget->addAction(actionPruneVolume);
329 mp_treeWidget->addAction(actionPurgeVolume);
330 mp_treeWidget->addAction(actionRelabelVolume);
331 mp_treeWidget->addAction(actionVolumeFromPool);
332 } else if (treedepth == 1) {
333 mp_treeWidget->addAction(actionAllVolumesFromPool);
339 * Setup a context menu
340 * Made separate from populate so that it would not create context menu over and
341 * over as the tree is repopulated.
343 void MediaList::createContextMenu()
345 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
346 connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(viewVolume()));
347 connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
348 connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
349 connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
350 connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
351 connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
352 connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
353 connect(mp_treeWidget, SIGNAL(
354 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
355 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
356 /* connect to the action specific to this pages class */
357 connect(actionRefreshMediaList, SIGNAL(triggered()), this,
358 SLOT(populateTree()));
359 connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
360 connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
361 connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
365 * Virtual function which is called when this page is visible on the stack
367 void MediaList::currentStackItem()
371 /* Create the context menu for the medialist tree */
377 * Called from the signal of the context sensitive menu to delete a volume!
379 void MediaList::deleteVolume()
381 if (QMessageBox::warning(this, "Bat",
382 tr("Are you sure you want to delete?? !!!.\n"
383 "This delete command is used to delete a Volume record and all associated catalog"
384 " records that were created. This command operates only on the Catalog"
385 " database and has no effect on the actual data written to a Volume. This"
386 " command can be dangerous and we strongly recommend that you do not use"
387 " it unless you know what you are doing. All Jobs and all associated"
388 " records (File and JobMedia) will be deleted from the catalog."
389 "Press OK to proceed with delete operation.?"),
390 QMessageBox::Ok | QMessageBox::Cancel)
391 == QMessageBox::Cancel) { return; }
393 QString cmd("delete volume=");
394 cmd += m_currentVolumeName;
399 * Called from the signal of the context sensitive menu to purge!
401 void MediaList::purgeVolume()
403 if (QMessageBox::warning(this, "Bat",
404 tr("Are you sure you want to purge ?? !!!.\n"
405 "The Purge command will delete associated Catalog database records from Jobs and"
406 " Volumes without considering the retention period. Purge works only on the"
407 " Catalog database and does not affect data written to Volumes. This command can"
408 " be dangerous because you can delete catalog records associated with current"
409 " backups of files, and we recommend that you do not use it unless you know what"
411 "Press OK to proceed with the purge operation?"),
412 QMessageBox::Ok | QMessageBox::Cancel)
413 == QMessageBox::Cancel) { return; }
415 QString cmd("purge volume=");
416 cmd += m_currentVolumeName;
422 * Called from the signal of the context sensitive menu to prune!
424 void MediaList::pruneVolume()
426 new prunePage(m_currentVolumeName, "");
430 * Called from the signal of the context sensitive menu to relabel!
432 void MediaList::relabelVolume()
435 new relabelDialog(m_console, m_currentVolumeName);
439 * Called from the signal of the context sensitive menu to purge!
441 void MediaList::allVolumesFromPool()
443 QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
448 void MediaList::allVolumes()
450 QString cmd = "update volume allfrompools";
456 * Called from the signal of the context sensitive menu to purge!
458 void MediaList::volumeFromPool()
460 QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
461 QTreeWidgetItem *parent = currentItem->parent();
462 QString pool = parent->text(0);
464 cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
470 * Write settings to save expanded states of the pools
472 void MediaList::writeExpandedSettings()
475 QSettings settings(m_console->m_dir->name(), "bat");
476 settings.beginGroup("MediaListTreeExpanded");
477 int childcount = m_topItem->childCount();
478 for (int cnt=0; cnt<childcount; cnt++) {
479 QTreeWidgetItem *poolitem = m_topItem->child(cnt);
480 settings.setValue(poolitem->text(0), poolitem->isExpanded());