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 plus additions
11 that are listed in the file LICENSE.
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);
64 MediaList::~MediaList()
69 * The main meat of the class!! The function that querries the director and
70 * creates the widgets with appropriate values.
72 void MediaList::populateTree()
74 QTreeWidgetItem *mediatreeitem, *pooltreeitem, *topItem;
76 if (!m_console->preventInUseConnect())
79 QStringList headerlist = (QStringList()
80 << "Volume Name" << "Id" << "Status" << "Enabled"
81 << "Bytes" << "Files" << "Jobs" << "Retention"
82 << "Media Type" << "Slot" << "Last Written");
83 int statusIndex = headerlist.indexOf("Status");
85 m_checkcurwidget = false;
86 mp_treeWidget->clear();
87 m_checkcurwidget = true;
88 mp_treeWidget->setColumnCount(headerlist.count());
89 topItem = new QTreeWidgetItem(mp_treeWidget);
90 topItem->setText(0, "Pools");
91 topItem->setData(0, Qt::UserRole, 0);
92 topItem->setExpanded(true);
94 mp_treeWidget->setHeaderLabels(headerlist);
98 foreach (QString pool_listItem, m_console->pool_list) {
99 pooltreeitem = new QTreeWidgetItem(topItem);
100 pooltreeitem->setText(0, pool_listItem);
101 pooltreeitem->setData(0, Qt::UserRole, 1);
102 pooltreeitem->setExpanded(true);
104 query = "SELECT Media.VolumeName AS Media, "
105 " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
106 " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
107 " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
108 " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
109 " Media.Slot AS Slot, Media.LastWritten AS LastWritten"
111 " WHERE Media.PoolId=Pool.PoolId";
112 query += " AND Pool.Name='" + pool_listItem + "'";
113 query += " ORDER BY Media";
115 if (mainWin->m_sqlDebug) {
116 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
119 if (m_console->sql_cmd(query, results)) {
121 QStringList fieldlist;
123 /* Iterate through the lines of results. */
124 foreach (QString resultline, results) {
125 fieldlist = resultline.split("\t");
127 mediatreeitem = new QTreeWidgetItem(pooltreeitem);
129 /* Iterate through fields in the record */
130 foreach (field, fieldlist) {
131 field = field.trimmed(); /* strip leading & trailing spaces */
133 mediatreeitem->setData(index, Qt::UserRole, 2);
134 mediatreeitem->setData(index, Qt::UserRole, 2);
135 mediatreeitem->setText(index, field);
136 if (index == statusIndex) {
137 setStatusColor(mediatreeitem, field, index);
141 } /* foreach field */
142 } /* foreach resultline */
143 } /* if results from query */
144 } /* foreach pool_listItem */
145 /* Resize the columns */
146 for(int cnter=0; cnter<headerlist.count(); cnter++) {
147 mp_treeWidget->resizeColumnToContents(cnter);
151 void MediaList::setStatusColor(QTreeWidgetItem *item, QString &field, int &index)
153 if (field == "Append" ) {
154 item->setBackground(index, Qt::green);
155 } else if (field == "Error") {
156 item->setBackground(index, Qt::red);
157 } else if ((field == "Used") || ("Full")){
158 item->setBackground(index, Qt::yellow);
163 * Called from the signal of the context sensitive menu!
165 void MediaList::editVolume()
167 MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
168 connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
172 * Called from the signal of the context sensitive menu!
174 void MediaList::showJobs()
176 QString emptyclient("");
177 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
178 mainWin->createPageJobList(m_currentVolumeName, emptyclient, parentItem);
182 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
183 * The tree has been populated.
185 void MediaList::PgSeltreeWidgetClicked()
195 * Added to set the context menu policy based on currently active treeWidgetItem
196 * signaled by currentItemChanged
198 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
200 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
201 if (m_checkcurwidget) {
202 /* The Previous item */
203 if (previouswidgetitem) { /* avoid a segfault if first time */
204 int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
206 mp_treeWidget->removeAction(actionEditVolume);
207 mp_treeWidget->removeAction(actionListJobsOnVolume);
208 mp_treeWidget->removeAction(actionDeleteVolume);
209 mp_treeWidget->removeAction(actionPruneVolume);
210 mp_treeWidget->removeAction(actionPurgeVolume);
211 mp_treeWidget->removeAction(actionRelabelVolume);
215 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
217 m_currentVolumeName=currentwidgetitem->text(0);
218 m_currentVolumeId=currentwidgetitem->text(1);
219 mp_treeWidget->addAction(actionEditVolume);
220 mp_treeWidget->addAction(actionListJobsOnVolume);
221 mp_treeWidget->addAction(actionDeleteVolume);
222 mp_treeWidget->addAction(actionPruneVolume);
223 mp_treeWidget->addAction(actionPurgeVolume);
224 mp_treeWidget->addAction(actionRelabelVolume);
230 * Setup a context menu
231 * Made separate from populate so that it would not create context menu over and
232 * over as the tree is repopulated.
234 void MediaList::createContextMenu()
236 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
237 mp_treeWidget->addAction(actionRefreshMediaList);
238 connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
239 connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
240 connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
241 connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
242 connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
243 connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
244 connect(mp_treeWidget, SIGNAL(
245 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
246 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
247 /* connect to the action specific to this pages class */
248 connect(actionRefreshMediaList, SIGNAL(triggered()), this,
249 SLOT(populateTree()));
253 * Virtual function which is called when this page is visible on the stack
255 void MediaList::currentStackItem()
259 /* Create the context menu for the medialist tree */
266 * Called from the signal of the context sensitive menu to delete a volume!
268 void MediaList::deleteVolume()
270 if (QMessageBox::warning(this, tr("Bat"),
271 tr("Are you sure you want to delete?? !!!.\n"
272 "This delete command is used to delete a Volume record and all associated catalog"
273 " records that were created. This command operates only on the Catalog"
274 " database and has no effect on the actual data written to a Volume. This"
275 " command can be dangerous and we strongly recommend that you do not use"
276 " it unless you know what you are doing. All Jobs and all associated"
277 " records (File and JobMedia) will be deleted from the catalog."
278 "Press OK to proceed with delete operation.?"),
279 QMessageBox::Ok | QMessageBox::Cancel)
280 == QMessageBox::Cancel) { return; }
282 QString cmd("delete volume=");
283 cmd += m_currentVolumeName;
288 * Called from the signal of the context sensitive menu to purge!
290 void MediaList::purgeVolume()
292 if (QMessageBox::warning(this, tr("Bat"),
293 tr("Are you sure you want to purge ?? !!!.\n"
294 "The Purge command will delete associated Catalog database records from Jobs and"
295 " Volumes without considering the retention period. Purge works only on the"
296 " Catalog database and does not affect data written to Volumes. This command can"
297 " be dangerous because you can delete catalog records associated with current"
298 " backups of files, and we recommend that you do not use it unless you know what"
300 "Press OK to proceed with the purge operation?"),
301 QMessageBox::Ok | QMessageBox::Cancel)
302 == QMessageBox::Cancel) { return; }
304 QString cmd("purge volume=");
305 cmd += m_currentVolumeName;
311 * Called from the signal of the context sensitive menu to prune!
313 void MediaList::pruneVolume()
315 new prunePage(m_currentVolumeName, "");
319 * Called from the signal of the context sensitive menu to relabel!
321 void MediaList::relabelVolume()
324 new relabelDialog(m_console, m_currentVolumeName);