2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
23 * Dirk Bartley, March 2007
28 #include <QAbstractEventDispatcher>
31 #include "medialist.h"
32 #include "mediaedit/mediaedit.h"
33 #include "mediainfo/mediainfo.h"
34 #include "joblist/joblist.h"
35 #include "relabel/relabel.h"
37 #include "util/fmtwidgetitem.h"
39 MediaList::MediaList() : Pages()
44 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
45 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
47 /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
49 m_checkcurwidget = true;
51 /* add context sensitive menu items specific to this classto the page
52 * selector tree. m_contextActions is QList of QActions */
53 m_contextActions.append(actionRefreshMediaList);
56 MediaList::~MediaList()
59 writeExpandedSettings();
63 * The main meat of the class!! The function that querries the director and
64 * creates the widgets with appropriate values.
66 void MediaList::populateTree()
68 QTreeWidgetItem *pooltreeitem = NULL;
71 writeExpandedSettings();
75 Freeze frz(*mp_treeWidget); /* disable updating*/
77 QStringList headerlist = (QStringList()
78 << tr("Volume Name") << tr("Id") << tr("Status") << tr("Enabled") << tr("Bytes") << tr("Files")
79 << tr("Jobs") << tr("Retention") << tr("Media Type") << tr("Slot") << tr("Use Duration")
80 << tr("Max Jobs") << tr("Max Files") << tr("Max Bytes") << tr("Recycle")
81 << tr("Last Written") << tr("First Written") << tr("Read Time")
82 << tr("Write Time") << tr("Recycle Count") << tr("Recycle Pool"));
84 m_checkcurwidget = false;
85 mp_treeWidget->clear();
86 m_checkcurwidget = true;
87 mp_treeWidget->setColumnCount(headerlist.count());
88 m_topItem = new QTreeWidgetItem(mp_treeWidget);
89 m_topItem->setText(0, tr("Pools"));
90 m_topItem->setData(0, Qt::UserRole, 0);
91 m_topItem->setExpanded(true);
93 mp_treeWidget->setHeaderLabels(headerlist);
95 QSettings settings(m_console->m_dir->name(), "bat");
96 settings.beginGroup("MediaListTreeExpanded");
100 /* Comma separated list of pools first */
102 QString pool_comsep("");
103 foreach (QString pool_listItem, m_console->pool_list) {
105 pool_comsep += "'" + pool_listItem + "'";
109 pool_comsep += ",'" + pool_listItem + "'";
111 /* Now use pool_comsep list to perform just one query */
112 if (pool_comsep != "") {
113 query = "SELECT Pool.Name AS pul,"
114 " Media.VolumeName AS Media, "
115 " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
116 " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
117 " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
118 " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
119 " Media.InChanger AS InChanger, Media.Slot AS Slot, "
120 " Media.VolUseDuration AS UseDuration,"
121 " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
122 " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
123 " Media.LastWritten AS LastWritten,"
124 " Media.FirstWritten AS FirstWritten,"
125 " (VolReadTime/1000000) AS ReadTime, (VolWriteTime/1000000) AS WriteTime,"
126 " RecycleCount AS ReCyCount,"
127 " RecPool.Name AS RecyclePool"
129 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
130 " LEFT OUTER JOIN Pool AS RecPool ON (Media.RecyclePoolId=RecPool.PoolId)"
132 query += " Pool.Name IN (" + pool_comsep + ")";
133 query += " ORDER BY Pool.Name, Media";
135 if (mainWin->m_sqlDebug) {
136 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
140 if (m_console->sql_cmd(query, results)) {
141 QStringList fieldlist;
142 QString prev_pool("");
143 QString this_pool("");
145 /* Iterate through the lines of results. */
146 foreach (QString resultline, results) {
147 fieldlist = resultline.split("\t");
148 this_pool = fieldlist.takeFirst();
149 if (prev_pool != this_pool) {
150 prev_pool = this_pool;
151 pooltreeitem = new QTreeWidgetItem(m_topItem);
152 pooltreeitem->setText(0, this_pool);
153 pooltreeitem->setData(0, Qt::UserRole, 1);
155 if(settings.contains(this_pool)) {
156 pooltreeitem->setExpanded(settings.value(this_pool).toBool());
158 pooltreeitem->setExpanded(true);
161 if (fieldlist.size() < 21) { // Handle recyclepool specifically, and pool is already removed
162 Pmsg2(000, "Unexpected line %s %d", resultline.toUtf8().data(), fieldlist.size());
163 continue; // some fields missing, ignore row
166 TreeItemFormatter mediaitem(*pooltreeitem, 2);
168 /* Iterate through fields in the record */
169 QStringListIterator fld(fieldlist);
172 mediaitem.setTextFld(index++, fld.next());
175 mediaitem.setNumericFld(index++, fld.next());
178 mediaitem.setVolStatusFld(index++, fld.next());
181 mediaitem.setBoolFld(index++, fld.next());
184 mediaitem.setBytesFld(index++, fld.next());
187 mediaitem.setNumericFld(index++, fld.next());
190 mediaitem.setNumericFld(index++, fld.next());
193 mediaitem.setDurationFld(index++, fld.next());
196 mediaitem.setTextFld(index++, fld.next());
198 /* inchanger + slot */
199 int inchanger = fld.next().toInt();
201 mediaitem.setNumericFld(index++, fld.next());
204 /* volume not in changer, show blank slot */
205 mediaitem.setNumericFld(index++, "");
210 mediaitem.setDurationFld(index++, fld.next());
213 mediaitem.setNumericFld(index++, fld.next());
216 mediaitem.setNumericFld(index++, fld.next());
219 mediaitem.setBytesFld(index++, fld.next());
222 mediaitem.setBoolFld(index++, fld.next());
225 mediaitem.setTextFld(index++, fld.next());
228 mediaitem.setTextFld(index++, fld.next());
231 mediaitem.setDurationFld(index++, fld.next());
234 mediaitem.setDurationFld(index++, fld.next());
237 mediaitem.setNumericFld(index++, fld.next());
241 mediaitem.setTextFld(index++, fld.next());
243 mediaitem.setTextFld(index++, "");
246 } /* foreach resultline */
248 } /* if results from query */
249 } /* foreach pool_listItem */
251 /* Resize the columns */
252 for(int cnter=0; cnter<headerlist.count(); cnter++) {
253 mp_treeWidget->resizeColumnToContents(cnter);
258 * Called from the signal of the context sensitive menu!
260 void MediaList::editVolume()
262 MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
263 connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
267 * Called from the signal of the context sensitive menu!
269 void MediaList::showJobs()
271 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
272 mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
276 * Called from the signal of the context sensitive menu!
278 void MediaList::viewVolume()
280 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
281 MediaInfo* view = new MediaInfo(parentItem, m_currentVolumeName);
282 connect(view, SIGNAL(destroyed()), this, SLOT(populateTree()));
287 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
288 * The tree has been populated.
290 void MediaList::PgSeltreeWidgetClicked()
296 if (!isOnceDocked()) {
302 * Added to set the context menu policy based on currently active treeWidgetItem
303 * signaled by currentItemChanged
305 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
307 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
308 if (m_checkcurwidget) {
309 /* The Previous item */
310 if (previouswidgetitem) { /* avoid a segfault if first time */
311 foreach(QAction* mediaAction, mp_treeWidget->actions()) {
312 mp_treeWidget->removeAction(mediaAction);
316 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
317 m_currentVolumeName=currentwidgetitem->text(0);
318 mp_treeWidget->addAction(actionRefreshMediaList);
320 m_currentVolumeId=currentwidgetitem->text(1);
321 mp_treeWidget->addAction(actionEditVolume);
322 mp_treeWidget->addAction(actionListJobsOnVolume);
323 mp_treeWidget->addAction(actionDeleteVolume);
324 mp_treeWidget->addAction(actionPruneVolume);
325 mp_treeWidget->addAction(actionPurgeVolume);
326 mp_treeWidget->addAction(actionRelabelVolume);
327 mp_treeWidget->addAction(actionVolumeFromPool);
328 } else if (treedepth == 1) {
329 mp_treeWidget->addAction(actionAllVolumesFromPool);
335 * Setup a context menu
336 * Made separate from populate so that it would not create context menu over and
337 * over as the tree is repopulated.
339 void MediaList::createContextMenu()
341 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
342 connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(viewVolume()));
343 connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
344 connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
345 connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
346 connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
347 connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
348 connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
349 connect(mp_treeWidget, SIGNAL(
350 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
351 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
352 /* connect to the action specific to this pages class */
353 connect(actionRefreshMediaList, SIGNAL(triggered()), this,
354 SLOT(populateTree()));
355 connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
356 connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
357 connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
361 * Virtual function which is called when this page is visible on the stack
363 void MediaList::currentStackItem()
367 /* Create the context menu for the medialist tree */
373 * Called from the signal of the context sensitive menu to delete a volume!
375 void MediaList::deleteVolume()
377 if (QMessageBox::warning(this, "Bat",
378 tr("Are you sure you want to delete?? !!!.\n"
379 "This delete command is used to delete a Volume record and all associated catalog"
380 " records that were created. This command operates only on the Catalog"
381 " database and has no effect on the actual data written to a Volume. This"
382 " command can be dangerous and we strongly recommend that you do not use"
383 " it unless you know what you are doing. All Jobs and all associated"
384 " records (File and JobMedia) will be deleted from the catalog."
385 "Press OK to proceed with delete operation.?"),
386 QMessageBox::Ok | QMessageBox::Cancel)
387 == QMessageBox::Cancel) { return; }
389 QString cmd("delete volume=");
390 cmd += m_currentVolumeName;
395 * Called from the signal of the context sensitive menu to purge!
397 void MediaList::purgeVolume()
399 if (QMessageBox::warning(this, "Bat",
400 tr("Are you sure you want to purge ?? !!!.\n"
401 "The Purge command will delete associated Catalog database records from Jobs and"
402 " Volumes without considering the retention period. Purge works only on the"
403 " Catalog database and does not affect data written to Volumes. This command can"
404 " be dangerous because you can delete catalog records associated with current"
405 " backups of files, and we recommend that you do not use it unless you know what"
407 "Press OK to proceed with the purge operation?"),
408 QMessageBox::Ok | QMessageBox::Cancel)
409 == QMessageBox::Cancel) { return; }
411 QString cmd("purge volume=");
412 cmd += m_currentVolumeName;
418 * Called from the signal of the context sensitive menu to prune!
420 void MediaList::pruneVolume()
422 new prunePage(m_currentVolumeName, "");
426 * Called from the signal of the context sensitive menu to relabel!
428 void MediaList::relabelVolume()
431 new relabelDialog(m_console, m_currentVolumeName);
435 * Called from the signal of the context sensitive menu to purge!
437 void MediaList::allVolumesFromPool()
439 QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
444 void MediaList::allVolumes()
446 QString cmd = "update volume allfrompools";
452 * Called from the signal of the context sensitive menu to purge!
454 void MediaList::volumeFromPool()
456 QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
457 QTreeWidgetItem *parent = currentItem->parent();
458 QString pool = parent->text(0);
460 cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
466 * Write settings to save expanded states of the pools
468 void MediaList::writeExpandedSettings()
471 QSettings settings(m_console->m_dir->name(), "bat");
472 settings.beginGroup("MediaListTreeExpanded");
473 int childcount = m_topItem->childCount();
474 for (int cnt=0; cnt<childcount; cnt++) {
475 QTreeWidgetItem *poolitem = m_topItem->child(cnt);
476 settings.setValue(poolitem->text(0), poolitem->isExpanded());