2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 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 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.
30 * Version $Id: medialist.cpp 4230 2007-02-21 20:07:37Z kerns $
34 * Dirk Bartley, March 2007
38 #include <QAbstractEventDispatcher>
41 #include "medialist.h"
42 #include "mediaedit/mediaedit.h"
43 #include "joblist/joblist.h"
44 #include "relabel/relabel.h"
47 MediaList::MediaList()
52 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
53 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.svg")));
55 /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
57 m_checkcurwidget = true;
59 /* add context sensitive menu items specific to this classto the page
60 * selector tree. m_contextActions is QList of QActions */
61 m_contextActions.append(actionRefreshMediaList);
65 MediaList::~MediaList()
70 * The main meat of the class!! The function that querries the director and
71 * creates the widgets with appropriate values.
73 void MediaList::populateTree()
75 QTreeWidgetItem *mediatreeitem, *pooltreeitem, *topItem;
77 if (!m_console->preventInUseConnect())
80 QStringList headerlist = (QStringList()
81 << "Volume Name" << "Id" << "Status" << "Enabled" << "Bytes" << "Files"
82 << "Jobs" << "Retention" << "Media Type" << "Slot" << "Use Duration"
83 << "Max Jobs" << "Max Files" << "Max Bytes" << "Recycle" << "Enabled"
84 << "RecyclePool" << "Last Written");
85 int statusIndex = headerlist.indexOf("Status");
87 m_checkcurwidget = false;
88 mp_treeWidget->clear();
89 m_checkcurwidget = true;
90 mp_treeWidget->setColumnCount(headerlist.count());
91 topItem = new QTreeWidgetItem(mp_treeWidget);
92 topItem->setText(0, "Pools");
93 topItem->setData(0, Qt::UserRole, 0);
94 topItem->setExpanded(true);
96 mp_treeWidget->setHeaderLabels(headerlist);
100 foreach (QString pool_listItem, m_console->pool_list) {
101 pooltreeitem = new QTreeWidgetItem(topItem);
102 pooltreeitem->setText(0, pool_listItem);
103 pooltreeitem->setData(0, Qt::UserRole, 1);
104 pooltreeitem->setExpanded(true);
106 query = "SELECT Media.VolumeName AS Media, "
107 " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
108 " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
109 " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
110 " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
111 " Media.Slot AS Slot, Media.VolUseDuration AS UseDuration,"
112 " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
113 " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
114 " Media.Enabled AS enabled, Pol.Name AS RecyclePool,"
115 " Media.LastWritten AS LastWritten"
117 " LEFT OUTER JOIN Pool ON (Media.PoolId=Pool.PoolId)"
118 " LEFT OUTER JOIN Pool AS Pol ON (Media.recyclepoolid=Pol.PoolId)"
120 query += " Pool.Name='" + pool_listItem + "'";
121 query += " ORDER BY Media";
123 if (mainWin->m_sqlDebug) {
124 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
127 if (m_console->sql_cmd(query, results)) {
129 QStringList fieldlist;
131 /* Iterate through the lines of results. */
132 foreach (QString resultline, results) {
133 fieldlist = resultline.split("\t");
135 mediatreeitem = new QTreeWidgetItem(pooltreeitem);
137 /* Iterate through fields in the record */
138 foreach (field, fieldlist) {
139 field = field.trimmed(); /* strip leading & trailing spaces */
141 mediatreeitem->setData(index, Qt::UserRole, 2);
142 mediatreeitem->setData(index, Qt::UserRole, 2);
143 mediatreeitem->setText(index, field);
144 if (index == statusIndex) {
145 setStatusColor(mediatreeitem, field, index);
149 } /* foreach field */
150 } /* foreach resultline */
151 } /* if results from query */
152 } /* foreach pool_listItem */
153 /* Resize the columns */
154 for(int cnter=0; cnter<headerlist.count(); cnter++) {
155 mp_treeWidget->resizeColumnToContents(cnter);
159 void MediaList::setStatusColor(QTreeWidgetItem *item, QString &field, int &index)
161 if (field == "Append" ) {
162 item->setBackground(index, Qt::green);
163 } else if (field == "Error") {
164 item->setBackground(index, Qt::red);
165 } else if ((field == "Used") || ("Full")){
166 item->setBackground(index, Qt::yellow);
171 * Called from the signal of the context sensitive menu!
173 void MediaList::editVolume()
175 MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
176 connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
180 * Called from the signal of the context sensitive menu!
182 void MediaList::showJobs()
184 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
185 mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
189 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
190 * The tree has been populated.
192 void MediaList::PgSeltreeWidgetClicked()
202 * Added to set the context menu policy based on currently active treeWidgetItem
203 * signaled by currentItemChanged
205 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
207 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
208 if (m_checkcurwidget) {
209 /* The Previous item */
210 if (previouswidgetitem) { /* avoid a segfault if first time */
211 mp_treeWidget->removeAction(actionEditVolume);
212 mp_treeWidget->removeAction(actionListJobsOnVolume);
213 mp_treeWidget->removeAction(actionDeleteVolume);
214 mp_treeWidget->removeAction(actionPruneVolume);
215 mp_treeWidget->removeAction(actionPurgeVolume);
216 mp_treeWidget->removeAction(actionRelabelVolume);
217 mp_treeWidget->removeAction(actionAllVolumesFromPool);
218 mp_treeWidget->removeAction(actionVolumeFromPool);
221 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
222 m_currentVolumeName=currentwidgetitem->text(0);
224 m_currentVolumeId=currentwidgetitem->text(1);
225 mp_treeWidget->addAction(actionEditVolume);
226 mp_treeWidget->addAction(actionListJobsOnVolume);
227 mp_treeWidget->addAction(actionDeleteVolume);
228 mp_treeWidget->addAction(actionPruneVolume);
229 mp_treeWidget->addAction(actionPurgeVolume);
230 mp_treeWidget->addAction(actionRelabelVolume);
231 mp_treeWidget->addAction(actionVolumeFromPool);
232 } else if (treedepth == 1) {
233 /* *******FIXME******
234 * I can't seem to get "All volumes from pool" or "Volume from pool" to work
235 * in one sentence command. Works when you do it one step at a time vi console
236 mp_treeWidget->addAction(actionAllVolumesFromPool);
243 * Setup a context menu
244 * Made separate from populate so that it would not create context menu over and
245 * over as the tree is repopulated.
247 void MediaList::createContextMenu()
249 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
250 mp_treeWidget->addAction(actionRefreshMediaList);
251 connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
252 connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
253 connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
254 connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
255 connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
256 connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
257 connect(mp_treeWidget, SIGNAL(
258 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
259 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
260 /* connect to the action specific to this pages class */
261 connect(actionRefreshMediaList, SIGNAL(triggered()), this,
262 SLOT(populateTree()));
263 connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
264 connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
268 * Virtual function which is called when this page is visible on the stack
270 void MediaList::currentStackItem()
274 /* Create the context menu for the medialist tree */
281 * Called from the signal of the context sensitive menu to delete a volume!
283 void MediaList::deleteVolume()
285 if (QMessageBox::warning(this, tr("Bat"),
286 tr("Are you sure you want to delete?? !!!.\n"
287 "This delete command is used to delete a Volume record and all associated catalog"
288 " records that were created. This command operates only on the Catalog"
289 " database and has no effect on the actual data written to a Volume. This"
290 " command can be dangerous and we strongly recommend that you do not use"
291 " it unless you know what you are doing. All Jobs and all associated"
292 " records (File and JobMedia) will be deleted from the catalog."
293 "Press OK to proceed with delete operation.?"),
294 QMessageBox::Ok | QMessageBox::Cancel)
295 == QMessageBox::Cancel) { return; }
297 QString cmd("delete volume=");
298 cmd += m_currentVolumeName;
303 * Called from the signal of the context sensitive menu to purge!
305 void MediaList::purgeVolume()
307 if (QMessageBox::warning(this, tr("Bat"),
308 tr("Are you sure you want to purge ?? !!!.\n"
309 "The Purge command will delete associated Catalog database records from Jobs and"
310 " Volumes without considering the retention period. Purge works only on the"
311 " Catalog database and does not affect data written to Volumes. This command can"
312 " be dangerous because you can delete catalog records associated with current"
313 " backups of files, and we recommend that you do not use it unless you know what"
315 "Press OK to proceed with the purge operation?"),
316 QMessageBox::Ok | QMessageBox::Cancel)
317 == QMessageBox::Cancel) { return; }
319 QString cmd("purge volume=");
320 cmd += m_currentVolumeName;
326 * Called from the signal of the context sensitive menu to prune!
328 void MediaList::pruneVolume()
330 new prunePage(m_currentVolumeName, "");
334 * Called from the signal of the context sensitive menu to relabel!
336 void MediaList::relabelVolume()
339 new relabelDialog(m_console, m_currentVolumeName);
343 * Called from the signal of the context sensitive menu to purge!
345 void MediaList::allVolumesFromPool()
347 QString cmd("update pool=");
348 cmd += m_currentVolumeName + " All Volumes From Pool";
354 * Called from the signal of the context sensitive menu to purge!
356 void MediaList::volumeFromPool()
358 QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
359 QTreeWidgetItem *parent = currentItem->parent();
360 QString pool = parent->text(0);
362 cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;