2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2010 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 three of the GNU Affero 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 Affero 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.
33 * Dirk Bartley, March 2007
38 #include <QAbstractEventDispatcher>
42 #include "label/label.h"
43 #include "mount/mount.h"
44 #include "status/storstat.h"
45 #include "util/fmtwidgetitem.h"
47 Storage::Storage() : Pages()
50 pgInitialize(tr("Storage"));
51 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
52 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/package-x-generic.png")));
54 /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_storage.h */
56 m_firstpopulation = true;
57 m_checkcurwidget = true;
59 m_currentStorage = "";
60 /* add context sensitive menu items specific to this classto the page
61 * selector tree. m_contextActions is QList of QActions */
62 m_contextActions.append(actionRefreshStorage);
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 Storage::populateTree()
78 writeExpandedSettings();
81 Freeze frz(*mp_treeWidget); /* disable updating */
83 m_checkcurwidget = false;
84 mp_treeWidget->clear();
85 m_checkcurwidget = true;
87 QStringList headerlist = (QStringList() << tr("Name") << tr("Id")
88 << tr("Changer") << tr("Slot") << tr("Status") << tr("Enabled") << tr("Pool")
89 << tr("Media Type") );
91 m_topItem = new QTreeWidgetItem(mp_treeWidget);
92 m_topItem->setText(0, tr("Storage"));
93 m_topItem->setData(0, Qt::UserRole, 0);
94 m_topItem->setExpanded(true);
96 mp_treeWidget->setColumnCount(headerlist.count());
97 mp_treeWidget->setHeaderLabels(headerlist);
99 QSettings settings(m_console->m_dir->name(), "bat");
100 settings.beginGroup("StorageTreeExpanded");
103 QString storage_comsep("");
105 foreach(storageName, m_console->storage_list){
107 storage_comsep += "'" + storageName + "'";
111 storage_comsep += ",'" + storageName + "'";
113 if (storage_comsep != "") {
115 /* Set up query QString and header QStringList */
116 QString query("SELECT"
117 " Name AS StorageName,"
118 " StorageId AS ID, AutoChanger AS Changer"
120 " WHERE StorageId IN (SELECT MAX(StorageId) FROM Storage WHERE");
121 query += " Name IN (" + storage_comsep + ")";
122 query += " GROUP BY Name) ORDER BY Name";
125 /* This could be a log item */
126 if (mainWin->m_sqlDebug) {
127 Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
129 if (m_console->sql_cmd(query, results)) {
131 QStringList fieldlist;
132 foreach (QString resultline, results) {
133 fieldlist = resultline.split("\t");
134 storageName = fieldlist.takeFirst();
135 if (m_firstpopulation) {
136 settingsOpenStatus(storageName);
138 TreeItemFormatter storageItem(*m_topItem, 1);
139 storageItem.setTextFld(0, storageName);
140 if(settings.contains(storageName))
141 storageItem.widget()->setExpanded(settings.value(storageName).toBool());
143 storageItem.widget()->setExpanded(true);
146 QStringListIterator fld(fieldlist);
149 storageItem.setNumericFld(index++, fld.next());
152 QString changer = fld.next();
153 storageItem.setBoolFld(index++, changer);
156 mediaList(storageItem.widget(), fieldlist.first());
160 /* Resize the columns */
161 for(int cnter=0; cnter<headerlist.size(); cnter++) {
162 mp_treeWidget->resizeColumnToContents(cnter);
164 m_firstpopulation = false;
168 * For autochangers A query to show the tapes in the changer.
170 void Storage::mediaList(QTreeWidgetItem *parent, const QString &storageID)
172 QString query("SELECT Media.VolumeName AS Media, Media.Slot AS Slot,"
173 " Media.VolStatus AS VolStatus, Media.Enabled AS Enabled,"
174 " Pool.Name AS MediaPool, Media.MediaType AS MediaType"
176 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
177 " WHERE Media.StorageId='" + storageID + "'"
178 " AND Media.InChanger<>0"
179 " ORDER BY Media.Slot");
182 /* This could be a log item */
183 if (mainWin->m_sqlDebug) {
184 Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
186 if (m_console->sql_cmd(query, results)) {
189 QStringList fieldlist;
191 foreach (resultline, results) {
192 fieldlist = resultline.split("\t");
193 if (fieldlist.size() < 6)
196 /* Iterate through fields in the record */
197 QStringListIterator fld(fieldlist);
199 TreeItemFormatter fmt(*parent, 2);
202 fmt.setTextFld(index++, fld.next());
204 /* skip the next two columns, unused by media */
208 fmt.setNumericFld(index++, fld.next());
211 fmt.setVolStatusFld(index++, fld.next());
214 fmt.setBoolFld(index++, fld.next());
217 fmt.setTextFld(index++, fld.next());
220 fmt.setTextFld(index++, fld.next());
227 * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
228 * The tree has been populated.
230 void Storage::PgSeltreeWidgetClicked()
236 if (!isOnceDocked()) {
242 * Added to set the context menu policy based on currently active treeWidgetItem
243 * signaled by currentItemChanged
245 void Storage::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
247 /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
248 if (m_checkcurwidget) {
249 /* The Previous item */
250 if (previouswidgetitem) { /* avoid a segfault if first time */
251 int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
253 mp_treeWidget->removeAction(actionStatusStorageInConsole);
254 mp_treeWidget->removeAction(actionStatusStorageWindow);
255 mp_treeWidget->removeAction(actionLabelStorage);
256 mp_treeWidget->removeAction(actionMountStorage);
257 mp_treeWidget->removeAction(actionUnMountStorage);
258 mp_treeWidget->removeAction(actionUpdateSlots);
259 mp_treeWidget->removeAction(actionUpdateSlotsScan);
260 mp_treeWidget->removeAction(actionRelease);
264 int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
266 /* set a hold variable to the storage name in case the context sensitive
268 m_currentStorage = currentwidgetitem->text(0);
269 m_currentAutoChanger = currentwidgetitem->text(2) == tr("Yes");
270 mp_treeWidget->addAction(actionStatusStorageInConsole);
271 mp_treeWidget->addAction(actionStatusStorageWindow);
272 mp_treeWidget->addAction(actionLabelStorage);
273 mp_treeWidget->addAction(actionMountStorage);
274 mp_treeWidget->addAction(actionUnMountStorage);
275 mp_treeWidget->addAction(actionRelease);
277 text = tr("Status Storage \"%1\"").arg(m_currentStorage);;
278 actionStatusStorageInConsole->setText(text);
279 text = tr("Status Storage \"%1\" in Window").arg(m_currentStorage);;
280 actionStatusStorageWindow->setText(text);
281 text = tr("Label media in Storage \"%1\"").arg(m_currentStorage);
282 actionLabelStorage->setText(text);
283 text = tr("Mount media in Storage \"%1\"").arg(m_currentStorage);
284 actionMountStorage->setText(text);
285 text = tr("\"UN\" Mount media in Storage \"%1\"").arg(m_currentStorage);
286 text = tr("Release media in Storage \"%1\"").arg(m_currentStorage);
287 actionRelease->setText(text);
288 if (m_currentAutoChanger) {
289 mp_treeWidget->addAction(actionUpdateSlots);
290 mp_treeWidget->addAction(actionUpdateSlotsScan);
291 text = tr("Barcode Scan media in Storage \"%1\"").arg(m_currentStorage);
292 actionUpdateSlots->setText(text);
293 text = tr("Read scan media in Storage \"%1\"").arg( m_currentStorage);
294 actionUpdateSlotsScan->setText(text);
301 * Setup a context menu
302 * Made separate from populate so that it would not create context menu over and
303 * over as the tree is repopulated.
305 void Storage::createContextMenu()
307 mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
308 mp_treeWidget->addAction(actionRefreshStorage);
309 connect(mp_treeWidget, SIGNAL(
310 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
311 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
312 /* connect to the action specific to this pages class */
313 connect(actionRefreshStorage, SIGNAL(triggered()), this,
314 SLOT(populateTree()));
315 connect(actionStatusStorageInConsole, SIGNAL(triggered()), this,
316 SLOT(consoleStatusStorage()));
317 connect(actionLabelStorage, SIGNAL(triggered()), this,
318 SLOT(consoleLabelStorage()));
319 connect(actionMountStorage, SIGNAL(triggered()), this,
320 SLOT(consoleMountStorage()));
321 connect(actionUnMountStorage, SIGNAL(triggered()), this,
322 SLOT(consoleUnMountStorage()));
323 connect(actionUpdateSlots, SIGNAL(triggered()), this,
324 SLOT(consoleUpdateSlots()));
325 connect(actionUpdateSlotsScan, SIGNAL(triggered()), this,
326 SLOT(consoleUpdateSlotsScan()));
327 connect(actionRelease, SIGNAL(triggered()), this,
328 SLOT(consoleRelease()));
329 connect(actionStatusStorageWindow, SIGNAL(triggered()), this,
330 SLOT(statusStorageWindow()));
331 connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
332 this, SLOT(contentWindow()));
336 void Storage::contentWindow()
338 if (m_currentStorage != "" && m_currentAutoChanger) {
339 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
340 new Content(m_currentStorage, parentItem);
345 * Virtual function which is called when this page is visible on the stack
347 void Storage::currentStackItem()
351 /* Create the context menu for the storage tree */
357 * Functions to respond to local context sensitive menu sending console commands
358 * If I could figure out how to make these one function passing a string, Yaaaaaa
360 void Storage::consoleStatusStorage()
362 QString cmd("status storage=");
363 cmd += m_currentStorage;
367 /* Label Media populating current storage by default */
368 void Storage::consoleLabelStorage()
370 new labelPage(m_currentStorage);
373 /* Mount currently selected storage */
374 void Storage::consoleMountStorage()
376 if (m_currentAutoChanger == 0){
377 /* no autochanger, just execute the command in the console */
378 QString cmd("mount storage=");
379 cmd += m_currentStorage;
383 /* if this storage is an autochanger, lets ask for the slot */
384 new mountDialog(m_console, m_currentStorage);
388 /* Unmount Currently selected storage */
389 void Storage::consoleUnMountStorage()
391 QString cmd("umount storage=");
392 cmd += m_currentStorage;
397 void Storage::consoleUpdateSlots()
399 QString cmd("update slots storage=");
400 cmd += m_currentStorage;
404 /* Update Slots Scan*/
405 void Storage::consoleUpdateSlotsScan()
407 QString cmd("update slots scan storage=");
408 cmd += m_currentStorage;
412 /* Release a tape in the drive */
413 void Storage::consoleRelease()
415 QString cmd("release storage=");
416 cmd += m_currentStorage;
421 * Open a status storage window
423 void Storage::statusStorageWindow()
425 /* if one exists, then just set it current */
427 foreach(Pages *page, mainWin->m_pagehash) {
428 if (mainWin->currentConsole() == page->console()) {
429 if (page->name() == tr("Storage Status %1").arg(m_currentStorage)) {
436 QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
437 new StorStat(m_currentStorage, parentItem);
442 * Write settings to save expanded states of the pools
444 void Storage::writeExpandedSettings()
446 QSettings settings(m_console->m_dir->name(), "bat");
447 settings.beginGroup("StorageTreeExpanded");
448 int childcount = m_topItem->childCount();
449 for (int cnt=0; cnt<childcount; cnt++) {
450 QTreeWidgetItem *item = m_topItem->child(cnt);
451 settings.setValue(item->text(0), item->isExpanded());
457 * If first time, then check to see if there were status pages open the last time closed
460 void Storage::settingsOpenStatus(QString &storage)
462 QSettings settings(m_console->m_dir->name(), "bat");
464 settings.beginGroup("OpenOnExit");
465 QString toRead = "StorageStatus_" + storage;
466 if (settings.value(toRead) == 1) {
467 if (mainWin->m_sqlDebug) {
468 Pmsg1(000, "Do open Storage Status window for : %s\n", storage.toUtf8().data());
470 new StorStat(storage, mainWin->getFromHash(this));
472 mainWin->getFromHash(this)->setExpanded(true);
474 if (mainWin->m_sqlDebug) {
475 Pmsg1(000, "Do NOT open Storage Status window for : %s\n", storage.toUtf8().data());