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>
43 #include "label/label.h"
44 #include "mount/mount.h"
45 #include "status/storstat.h"
46 #include "util/fmtwidgetitem.h"
51 pgInitialize(tr("Storage"));
52 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
53 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/package-x-generic.png")));
55 /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_storage.h */
57 m_firstpopulation = true;
58 m_checkcurwidget = true;
60 m_currentStorage = "";
61 /* add context sensitive menu items specific to this classto the page
62 * selector tree. m_contextActions is QList of QActions */
63 m_contextActions.append(actionRefreshStorage);
69 writeExpandedSettings();
73 * The main meat of the class!! The function that querries the director and
74 * creates the widgets with appropriate values.
76 void Storage::populateTree()
79 writeExpandedSettings();
82 Freeze frz(*mp_treeWidget); /* disable updating */
84 m_checkcurwidget = false;
85 mp_treeWidget->clear();
86 m_checkcurwidget = true;
88 QStringList headerlist = (QStringList() << tr("Name") << tr("Id")
89 << tr("Changer") << tr("Slot") << tr("Status") << tr("Enabled") << tr("Pool")
90 << tr("Media Type") );
92 m_topItem = new QTreeWidgetItem(mp_treeWidget);
93 m_topItem->setText(0, tr("Storage"));
94 m_topItem->setData(0, Qt::UserRole, 0);
95 m_topItem->setExpanded(true);
97 mp_treeWidget->setColumnCount(headerlist.count());
98 mp_treeWidget->setHeaderLabels(headerlist);
100 QSettings settings(m_console->m_dir->name(), "bat");
101 settings.beginGroup("StorageTreeExpanded");
104 QString storage_comsep("");
106 foreach(storageName, m_console->storage_list){
108 storage_comsep += "'" + storageName + "'";
112 storage_comsep += ",'" + storageName + "'";
114 if (storage_comsep != "") {
116 /* Set up query QString and header QStringList */
117 QString query("SELECT"
118 " Name AS StorageName,"
119 " StorageId AS ID, AutoChanger AS Changer"
121 " WHERE StorageId IN (SELECT MAX(StorageId) FROM Storage WHERE");
122 query += " Name IN (" + storage_comsep + ")";
123 query += " GROUP BY Name) ORDER BY Name";
126 /* This could be a log item */
127 if (mainWin->m_sqlDebug) {
128 Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
130 if (m_console->sql_cmd(query, results)) {
132 QStringList fieldlist;
133 foreach (QString resultline, results) {
134 fieldlist = resultline.split("\t");
135 storageName = fieldlist.takeFirst();
136 if (m_firstpopulation) {
137 settingsOpenStatus(storageName);
139 TreeItemFormatter storageItem(*m_topItem, 1);
140 storageItem.setTextFld(0, storageName);
141 if(settings.contains(storageName))
142 storageItem.widget()->setExpanded(settings.value(storageName).toBool());
144 storageItem.widget()->setExpanded(true);
147 QStringListIterator fld(fieldlist);
150 storageItem.setNumericFld(index++, fld.next() );
153 QString changer = fld.next();
154 storageItem.setBoolFld(index++, changer);
157 mediaList(storageItem.widget(), fieldlist.first());
161 /* Resize the columns */
162 for(int cnter=0; cnter<headerlist.size(); cnter++) {
163 mp_treeWidget->resizeColumnToContents(cnter);
165 m_firstpopulation = false;
169 * For autochangers A query to show the tapes in the changer.
171 void Storage::mediaList(QTreeWidgetItem *parent, const QString &storageID)
173 QString query("SELECT Media.VolumeName AS Media, Media.Slot AS Slot,"
174 " Media.VolStatus AS VolStatus, Media.Enabled AS Enabled,"
175 " Pool.Name AS MediaPool, Media.MediaType AS MediaType"
177 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
178 " WHERE Media.StorageId='" + storageID + "'"
179 " AND Media.InChanger<>0"
180 " ORDER BY Media.Slot");
183 /* This could be a log item */
184 if (mainWin->m_sqlDebug) {
185 Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
187 if (m_console->sql_cmd(query, results)) {
190 QStringList fieldlist;
192 foreach (resultline, results) {
193 fieldlist = resultline.split("\t");
194 if (fieldlist.size() < 6)
197 /* Iterate through fields in the record */
198 QStringListIterator fld(fieldlist);
200 TreeItemFormatter fmt(*parent, 2);
203 fmt.setTextFld(index++, fld.next());
205 /* skip the next two columns, unused by media */
209 fmt.setNumericFld(index++, fld.next());
212 fmt.setVolStatusFld(index++, fld.next());
215 fmt.setBoolFld(index++, fld.next());
218 fmt.setTextFld(index++, fld.next());
221 fmt.setTextFld(index++, fld.next());
228 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
229 * The tree has been populated.
231 void Storage::PgSeltreeWidgetClicked()
237 if (!isOnceDocked()) {
243 * Added to set the context menu policy based on currently active treeWidgetItem
244 * signaled by currentItemChanged
246 void Storage::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
248 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
249 if (m_checkcurwidget) {
250 /* The Previous item */
251 if (previouswidgetitem) { /* avoid a segfault if first time */
252 int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
254 mp_treeWidget->removeAction(actionStatusStorageInConsole);
255 mp_treeWidget->removeAction(actionStatusStorageWindow);
256 mp_treeWidget->removeAction(actionLabelStorage);
257 mp_treeWidget->removeAction(actionMountStorage);
258 mp_treeWidget->removeAction(actionUnMountStorage);
259 mp_treeWidget->removeAction(actionUpdateSlots);
260 mp_treeWidget->removeAction(actionUpdateSlotsScan);
261 mp_treeWidget->removeAction(actionRelease);
265 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
267 /* set a hold variable to the storage name in case the context sensitive
269 m_currentStorage = currentwidgetitem->text(0);
270 m_currentAutoChanger = currentwidgetitem->text(2) == tr("Yes");
271 mp_treeWidget->addAction(actionStatusStorageInConsole);
272 mp_treeWidget->addAction(actionStatusStorageWindow);
273 mp_treeWidget->addAction(actionLabelStorage);
274 mp_treeWidget->addAction(actionMountStorage);
275 mp_treeWidget->addAction(actionUnMountStorage);
276 mp_treeWidget->addAction(actionRelease);
278 text = tr("Status Storage \"%1\"").arg(m_currentStorage);;
279 actionStatusStorageInConsole->setText(text);
280 text = tr("Status Storage \"%1\" in Window").arg(m_currentStorage);;
281 actionStatusStorageWindow->setText(text);
282 text = tr("Label media in Storage \"%1\"").arg(m_currentStorage);
283 actionLabelStorage->setText(text);
284 text = tr("Mount media in Storage \"%1\"").arg(m_currentStorage);
285 actionMountStorage->setText(text);
286 text = tr("\"UN\" Mount media in Storage \"%1\"").arg(m_currentStorage);
287 text = tr("Release media in Storage \"%1\"").arg(m_currentStorage);
288 actionRelease->setText(text);
289 if (m_currentAutoChanger) {
290 mp_treeWidget->addAction(actionUpdateSlots);
291 mp_treeWidget->addAction(actionUpdateSlotsScan);
292 text = tr("Barcode Scan media in Storage \"%1\"").arg(m_currentStorage);
293 actionUpdateSlots->setText(text);
294 text = tr("Read scan media in Storage \"%1\"").arg( m_currentStorage);
295 actionUpdateSlotsScan->setText(text);
302 * Setup a context menu
303 * Made separate from populate so that it would not create context menu over and
304 * over as the tree is repopulated.
306 void Storage::createContextMenu()
308 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
309 mp_treeWidget->addAction(actionRefreshStorage);
310 connect(mp_treeWidget, SIGNAL(
311 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
312 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
313 /* connect to the action specific to this pages class */
314 connect(actionRefreshStorage, SIGNAL(triggered()), this,
315 SLOT(populateTree()));
316 connect(actionStatusStorageInConsole, SIGNAL(triggered()), this,
317 SLOT(consoleStatusStorage()));
318 connect(actionLabelStorage, SIGNAL(triggered()), this,
319 SLOT(consoleLabelStorage()));
320 connect(actionMountStorage, SIGNAL(triggered()), this,
321 SLOT(consoleMountStorage()));
322 connect(actionUnMountStorage, SIGNAL(triggered()), this,
323 SLOT(consoleUnMountStorage()));
324 connect(actionUpdateSlots, SIGNAL(triggered()), this,
325 SLOT(consoleUpdateSlots()));
326 connect(actionUpdateSlotsScan, SIGNAL(triggered()), this,
327 SLOT(consoleUpdateSlotsScan()));
328 connect(actionRelease, SIGNAL(triggered()), this,
329 SLOT(consoleRelease()));
330 connect(actionStatusStorageWindow, SIGNAL(triggered()), this,
331 SLOT(statusStorageWindow()));
332 connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
333 this, SLOT(contentWindow()));
337 void Storage::contentWindow()
339 if (m_currentStorage != "" && m_currentAutoChanger) {
340 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
341 new Content(m_currentStorage, parentItem);
346 * Virtual function which is called when this page is visible on the stack
348 void Storage::currentStackItem()
352 /* Create the context menu for the storage tree */
358 * Functions to respond to local context sensitive menu sending console commands
359 * If I could figure out how to make these one function passing a string, Yaaaaaa
361 void Storage::consoleStatusStorage()
363 QString cmd("status storage=");
364 cmd += m_currentStorage;
368 /* Label Media populating current storage by default */
369 void Storage::consoleLabelStorage()
371 new labelPage(m_currentStorage);
374 /* Mount currently selected storage */
375 void Storage::consoleMountStorage()
377 if (m_currentAutoChanger == 0){
378 /* no autochanger, just execute the command in the console */
379 QString cmd("mount storage=");
380 cmd += m_currentStorage;
384 /* if this storage is an autochanger, lets ask for the slot */
385 new mountDialog(m_console, m_currentStorage);
389 /* Unmount Currently selected storage */
390 void Storage::consoleUnMountStorage()
392 QString cmd("umount storage=");
393 cmd += m_currentStorage;
398 void Storage::consoleUpdateSlots()
400 QString cmd("update slots storage=");
401 cmd += m_currentStorage;
405 /* Update Slots Scan*/
406 void Storage::consoleUpdateSlotsScan()
408 QString cmd("update slots scan storage=");
409 cmd += m_currentStorage;
413 /* Release a tape in the drive */
414 void Storage::consoleRelease()
416 QString cmd("release storage=");
417 cmd += m_currentStorage;
422 * Open a status storage window
424 void Storage::statusStorageWindow()
426 /* if one exists, then just set it current */
428 foreach(Pages *page, mainWin->m_pagehash) {
429 if (mainWin->currentConsole() == page->console()) {
430 if (page->name() == tr("Storage Status %1").arg(m_currentStorage)) {
437 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
438 new StorStat(m_currentStorage, parentItem);
443 * Write settings to save expanded states of the pools
445 void Storage::writeExpandedSettings()
447 QSettings settings(m_console->m_dir->name(), "bat");
448 settings.beginGroup("StorageTreeExpanded");
449 int childcount = m_topItem->childCount();
450 for (int cnt=0; cnt<childcount; cnt++) {
451 QTreeWidgetItem *item = m_topItem->child(cnt);
452 settings.setValue(item->text(0), item->isExpanded());
458 * If first time, then check to see if there were status pages open the last time closed
461 void Storage::settingsOpenStatus(QString &storage)
463 QSettings settings(m_console->m_dir->name(), "bat");
465 settings.beginGroup("OpenOnExit");
466 QString toRead = "StorageStatus_" + storage;
467 if (settings.value(toRead) == 1) {
468 if (mainWin->m_sqlDebug) {
469 Pmsg1(000, "Do open Storage Status window for : %s\n", storage.toUtf8().data());
471 new StorStat(storage, mainWin->getFromHash(this));
473 mainWin->getFromHash(this)->setExpanded(true);
475 if (mainWin->m_sqlDebug) {
476 Pmsg1(000, "Do NOT open Storage Status window for : %s\n", storage.toUtf8().data());