2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-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.
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.png")));
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()
68 writeExpandedSettings();
72 * The main meat of the class!! The function that querries the director and
73 * creates the widgets with appropriate values.
75 void MediaList::populateTree()
77 QTreeWidgetItem *mediatreeitem, *pooltreeitem;
79 if (!m_console->preventInUseConnect())
82 QStringList headerlist = (QStringList()
83 << "Volume Name" << "Id" << "Status" << "Enabled" << "Bytes" << "Files"
84 << "Jobs" << "Retention" << "Media Type" << "Slot" << "Use Duration"
85 << "Max Jobs" << "Max Files" << "Max Bytes" << "Recycle" << "Enabled"
86 << "RecyclePool" << "Last Written");
87 int statusIndex = headerlist.indexOf("Status");
88 QStringList flaglist = (QStringList()
89 << "L" << "R" << "L" << "R" << "BR" << "R"
90 << "R" << "RS" << "L" << "R" << "RS"
91 << "R" << "R" << "BR" << "R" << "R"
94 m_checkcurwidget = false;
96 writeExpandedSettings();
97 mp_treeWidget->clear();
98 m_checkcurwidget = true;
99 mp_treeWidget->setColumnCount(headerlist.count());
100 m_topItem = new QTreeWidgetItem(mp_treeWidget);
101 m_topItem->setText(0, "Pools");
102 m_topItem->setData(0, Qt::UserRole, 0);
103 m_topItem->setExpanded(true);
105 mp_treeWidget->setHeaderLabels(headerlist);
107 QSettings settings(m_console->m_dir->name(), "bat");
108 settings.beginGroup("MediaListTreeExpanded");
111 foreach (QString pool_listItem, m_console->pool_list) {
112 pooltreeitem = new QTreeWidgetItem(m_topItem);
113 pooltreeitem->setText(0, pool_listItem);
114 pooltreeitem->setData(0, Qt::UserRole, 1);
115 if(settings.contains(pool_listItem)) {
116 pooltreeitem->setExpanded(settings.value(pool_listItem).toBool());
118 pooltreeitem->setExpanded(true);
121 query = "SELECT Media.VolumeName AS Media, "
122 " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
123 " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
124 " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
125 " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
126 " Media.Slot AS Slot, Media.VolUseDuration AS UseDuration,"
127 " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
128 " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
129 " Media.Enabled AS enabled, Pol.Name AS RecyclePool,"
130 " Media.LastWritten AS LastWritten"
132 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
133 " LEFT OUTER JOIN Pool AS Pol ON (Media.RecyclePoolId=Pol.PoolId)"
135 query += " Pool.Name='" + pool_listItem + "'";
136 query += " ORDER BY Media";
138 if (mainWin->m_sqlDebug) {
139 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
142 if (m_console->sql_cmd(query, results)) {
144 QStringList fieldlist;
146 /* Iterate through the lines of results. */
147 foreach (QString resultline, results) {
148 fieldlist = resultline.split("\t");
150 mediatreeitem = new QTreeWidgetItem(pooltreeitem);
152 /* Iterate through fields in the record */
153 foreach (field, fieldlist) {
154 field = field.trimmed(); /* strip leading & trailing spaces */
156 mediatreeitem->setData(index, Qt::UserRole, 2);
157 mediatreeitem->setData(index, Qt::UserRole, 2);
158 mediatreeitem->setText(index, field);
159 if (index == statusIndex) {
160 setStatusColor(mediatreeitem, field, index);
162 if (flaglist[index].contains("B")) {
165 qlonglong bytes = field.toULongLong(&okay);
167 QString test = QString("%1").arg(bytes);
168 mainWin->hrConvert(text, bytes);
169 mediatreeitem->setText(index, text);
170 } else { Pmsg1(000, "conversion error %s\n", field.toUtf8().data()); }
172 if (flaglist[index].contains("S")) {
175 qlonglong seconds = field.toULongLong(&okay);
177 QString test = QString("%1").arg(seconds);
178 mainWin->hrConvertSeconds(text, seconds);
179 mediatreeitem->setText(index, text);
180 } else { Pmsg1(000, "conversion error %s\n", field.toUtf8().data()); }
182 if (flaglist[index].contains("R"))
183 mediatreeitem->setTextAlignment(index, Qt::AlignRight);
186 } /* foreach field */
187 } /* foreach resultline */
188 } /* if results from query */
189 } /* foreach pool_listItem */
191 /* Resize the columns */
192 for(int cnter=0; cnter<headerlist.count(); cnter++) {
193 mp_treeWidget->resizeColumnToContents(cnter);
197 void MediaList::setStatusColor(QTreeWidgetItem *item, QString &field, int &index)
199 if (field == "Append" ) {
200 item->setBackground(index, Qt::green);
201 } else if (field == "Error") {
202 item->setBackground(index, Qt::red);
203 } else if ((field == "Used") || ("Full")){
204 item->setBackground(index, Qt::yellow);
209 * Called from the signal of the context sensitive menu!
211 void MediaList::editVolume()
213 MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
214 connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
218 * Called from the signal of the context sensitive menu!
220 void MediaList::showJobs()
222 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
223 mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
227 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
228 * The tree has been populated.
230 void MediaList::PgSeltreeWidgetClicked()
240 * Added to set the context menu policy based on currently active treeWidgetItem
241 * signaled by currentItemChanged
243 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
245 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
246 if (m_checkcurwidget) {
247 /* The Previous item */
248 if (previouswidgetitem) { /* avoid a segfault if first time */
249 foreach(QAction* mediaAction, mp_treeWidget->actions()) {
250 mp_treeWidget->removeAction(mediaAction);
254 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
255 m_currentVolumeName=currentwidgetitem->text(0);
256 mp_treeWidget->addAction(actionRefreshMediaList);
258 m_currentVolumeId=currentwidgetitem->text(1);
259 mp_treeWidget->addAction(actionEditVolume);
260 mp_treeWidget->addAction(actionListJobsOnVolume);
261 mp_treeWidget->addAction(actionDeleteVolume);
262 mp_treeWidget->addAction(actionPruneVolume);
263 mp_treeWidget->addAction(actionPurgeVolume);
264 mp_treeWidget->addAction(actionRelabelVolume);
265 mp_treeWidget->addAction(actionVolumeFromPool);
266 } else if (treedepth == 1) {
267 mp_treeWidget->addAction(actionAllVolumesFromPool);
273 * Setup a context menu
274 * Made separate from populate so that it would not create context menu over and
275 * over as the tree is repopulated.
277 void MediaList::createContextMenu()
279 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
280 connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
281 connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
282 connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
283 connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
284 connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
285 connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
286 connect(mp_treeWidget, SIGNAL(
287 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
288 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
289 /* connect to the action specific to this pages class */
290 connect(actionRefreshMediaList, SIGNAL(triggered()), this,
291 SLOT(populateTree()));
292 connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
293 connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
294 connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
298 * Virtual function which is called when this page is visible on the stack
300 void MediaList::currentStackItem()
304 /* Create the context menu for the medialist tree */
311 * Called from the signal of the context sensitive menu to delete a volume!
313 void MediaList::deleteVolume()
315 if (QMessageBox::warning(this, tr("Bat"),
316 tr("Are you sure you want to delete?? !!!.\n"
317 "This delete command is used to delete a Volume record and all associated catalog"
318 " records that were created. This command operates only on the Catalog"
319 " database and has no effect on the actual data written to a Volume. This"
320 " command can be dangerous and we strongly recommend that you do not use"
321 " it unless you know what you are doing. All Jobs and all associated"
322 " records (File and JobMedia) will be deleted from the catalog."
323 "Press OK to proceed with delete operation.?"),
324 QMessageBox::Ok | QMessageBox::Cancel)
325 == QMessageBox::Cancel) { return; }
327 QString cmd("delete volume=");
328 cmd += m_currentVolumeName;
333 * Called from the signal of the context sensitive menu to purge!
335 void MediaList::purgeVolume()
337 if (QMessageBox::warning(this, tr("Bat"),
338 tr("Are you sure you want to purge ?? !!!.\n"
339 "The Purge command will delete associated Catalog database records from Jobs and"
340 " Volumes without considering the retention period. Purge works only on the"
341 " Catalog database and does not affect data written to Volumes. This command can"
342 " be dangerous because you can delete catalog records associated with current"
343 " backups of files, and we recommend that you do not use it unless you know what"
345 "Press OK to proceed with the purge operation?"),
346 QMessageBox::Ok | QMessageBox::Cancel)
347 == QMessageBox::Cancel) { return; }
349 QString cmd("purge volume=");
350 cmd += m_currentVolumeName;
356 * Called from the signal of the context sensitive menu to prune!
358 void MediaList::pruneVolume()
360 new prunePage(m_currentVolumeName, "");
364 * Called from the signal of the context sensitive menu to relabel!
366 void MediaList::relabelVolume()
369 new relabelDialog(m_console, m_currentVolumeName);
373 * Called from the signal of the context sensitive menu to purge!
375 void MediaList::allVolumesFromPool()
377 QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
382 void MediaList::allVolumes()
384 QString cmd = "update volume allfrompools";
390 * Called from the signal of the context sensitive menu to purge!
392 void MediaList::volumeFromPool()
394 QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
395 QTreeWidgetItem *parent = currentItem->parent();
396 QString pool = parent->text(0);
398 cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
404 * Write settings to save expanded states of the pools
406 void MediaList::writeExpandedSettings()
408 QSettings settings(m_console->m_dir->name(), "bat");
409 settings.beginGroup("MediaListTreeExpanded");
410 int childcount = m_topItem->childCount();
411 for (int cnt=0; cnt<childcount; cnt++) {
412 QTreeWidgetItem *poolitem = m_topItem->child(cnt);
413 settings.setValue(poolitem->text(0), poolitem->isExpanded());