]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/storage/storage.cpp
tweak autochanger content view
[bacula/bacula] / bacula / src / qt-console / storage / storage.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
5
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
11    in the file LICENSE.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28  
29 /*
30  *   Version $Id$
31  *
32  *  Storage Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 #include "bat.h"
39 #include <QAbstractEventDispatcher>
40 #include <QMenu>
41 #include "storage.h"
42 #include "content.h"
43 #include "label/label.h"
44 #include "mount/mount.h"
45 #include "status/storstat.h"
46 #include "util/fmtwidgetitem.h"
47
48 Storage::Storage()
49 {
50    setupUi(this);
51    pgInitialize(tr("Storage"));
52    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
53    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/package-x-generic.png")));
54
55    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_storage.h */
56    m_populated = false;
57    m_firstpopulation = true;
58    m_checkcurwidget = true;
59    m_closeable = false;
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);
64 }
65
66 Storage::~Storage()
67 {
68    if (m_populated)
69       writeExpandedSettings();
70 }
71
72 /*
73  * The main meat of the class!!  The function that querries the director and 
74  * creates the widgets with appropriate values.
75  */
76 void Storage::populateTree()
77 {
78    if (m_populated)
79       writeExpandedSettings();
80    m_populated = true;
81
82    Freeze frz(*mp_treeWidget); /* disable updating */
83
84    m_checkcurwidget = false;
85    mp_treeWidget->clear();
86    m_checkcurwidget = true;
87
88    QStringList headerlist = (QStringList() << tr("Name") << tr("Id")
89         << tr("Changer") << tr("Slot") << tr("Status") << tr("Enabled") << tr("Pool") 
90         << tr("Media Type") );
91
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);
96
97    mp_treeWidget->setColumnCount(headerlist.count());
98    mp_treeWidget->setHeaderLabels(headerlist);
99
100    QSettings settings(m_console->m_dir->name(), "bat");
101    settings.beginGroup("StorageTreeExpanded");
102
103    bool first = true;
104    QString storage_comsep("");
105    QString storageName;
106    foreach(storageName, m_console->storage_list){
107       if (first) {
108          storage_comsep += "'" + storageName + "'";
109          first = false;
110       }
111       else
112          storage_comsep += ",'" + storageName + "'";
113    }
114    if (storage_comsep != "") {
115
116       /* Set up query QString and header QStringList */
117       QString query("SELECT"
118                " Name AS StorageName,"
119                " StorageId AS ID, AutoChanger AS Changer"
120                " FROM Storage "
121                " WHERE StorageId IN (SELECT MAX(StorageId) FROM Storage WHERE");
122       query += " Name IN (" + storage_comsep + ")";
123       query += " GROUP BY Name) ORDER BY Name";
124
125       QStringList results;
126       /* This could be a log item */
127       if (mainWin->m_sqlDebug) {
128          Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
129       }
130       if (m_console->sql_cmd(query, results)) {
131
132          QStringList fieldlist;
133          foreach (QString resultline, results) {
134             fieldlist = resultline.split("\t");
135             storageName = fieldlist.takeFirst();
136             if (m_firstpopulation) {
137                settingsOpenStatus(storageName);
138             }
139             TreeItemFormatter storageItem(*m_topItem, 1);
140             storageItem.setTextFld(0, storageName);
141             if(settings.contains(storageName))
142                storageItem.widget()->setExpanded(settings.value(storageName).toBool());
143             else
144                storageItem.widget()->setExpanded(true);
145
146             int index = 1;
147             QStringListIterator fld(fieldlist);
148  
149             /* storage id */
150             storageItem.setNumericFld(index++, fld.next() );
151
152             /* changer */
153             QString changer = fld.next();
154             storageItem.setBoolFld(index++, changer);
155
156             if (changer == "1")
157                mediaList(storageItem.widget(), fieldlist.first());
158          }
159       }
160    }
161    /* Resize the columns */
162    for(int cnter=0; cnter<headerlist.size(); cnter++) {
163       mp_treeWidget->resizeColumnToContents(cnter);
164    }
165    m_firstpopulation = false;
166 }
167
168 /*
169  * For autochangers    A query to show the tapes in the changer.
170  */
171 void Storage::mediaList(QTreeWidgetItem *parent, const QString &storageID)
172 {
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" 
176                  " From Media"
177                  " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
178                  " WHERE Media.StorageId='" + storageID + "'"
179                  " AND Media.InChanger<>0"
180                  " ORDER BY Media.Slot");
181
182    QStringList results;
183    /* This could be a log item */
184    if (mainWin->m_sqlDebug) {
185       Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
186    }
187    if (m_console->sql_cmd(query, results)) {
188       QString resultline;
189       QString field;
190       QStringList fieldlist;
191  
192       foreach (resultline, results) {
193          fieldlist = resultline.split("\t");
194          if (fieldlist.size() < 6)
195              continue; 
196
197          /* Iterate through fields in the record */
198          QStringListIterator fld(fieldlist);
199          int index = 0;
200          TreeItemFormatter fmt(*parent, 2);
201
202          /* volname */
203          fmt.setTextFld(index++, fld.next()); 
204  
205          /* skip the next two columns, unused by media */
206          index += 2;
207
208          /* slot */
209          fmt.setNumericFld(index++, fld.next());
210
211          /* status */
212          fmt.setVolStatusFld(index++, fld.next());
213
214          /* enabled */
215          fmt.setBoolFld(index++, fld.next()); 
216
217          /* pool */
218          fmt.setTextFld(index++, fld.next()); 
219
220          /* media type */
221          fmt.setTextFld(index++, fld.next()); 
222
223       }
224    }
225 }
226
227 /*
228  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
229  * The tree has been populated.
230  */
231 void Storage::PgSeltreeWidgetClicked()
232 {
233    if(!m_populated) {
234       populateTree();
235       createContextMenu();
236    }
237    dockPage();
238 }
239
240 /*
241  * Added to set the context menu policy based on currently active treeWidgetItem
242  * signaled by currentItemChanged
243  */
244 void Storage::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
245 {
246    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
247    if (m_checkcurwidget) {
248       /* The Previous item */
249       if (previouswidgetitem) { /* avoid a segfault if first time */
250          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
251          if (treedepth == 1){
252             mp_treeWidget->removeAction(actionStatusStorageInConsole);
253             mp_treeWidget->removeAction(actionStatusStorageWindow);
254             mp_treeWidget->removeAction(actionLabelStorage);
255             mp_treeWidget->removeAction(actionMountStorage);
256             mp_treeWidget->removeAction(actionUnMountStorage);
257             mp_treeWidget->removeAction(actionUpdateSlots);
258             mp_treeWidget->removeAction(actionUpdateSlotsScan);
259             mp_treeWidget->removeAction(actionRelease);
260          }
261       }
262
263       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
264       if (treedepth == 1){
265          /* set a hold variable to the storage name in case the context sensitive
266           * menu is used */
267          m_currentStorage = currentwidgetitem->text(0);
268          m_currentAutoChanger = currentwidgetitem->text(2) == tr("Yes");
269          mp_treeWidget->addAction(actionStatusStorageInConsole);
270          mp_treeWidget->addAction(actionStatusStorageWindow);
271          mp_treeWidget->addAction(actionLabelStorage);
272          mp_treeWidget->addAction(actionMountStorage);
273          mp_treeWidget->addAction(actionUnMountStorage);
274          mp_treeWidget->addAction(actionRelease);
275          QString text;
276          text = tr("Status Storage \"%1\"").arg(m_currentStorage);;
277          actionStatusStorageInConsole->setText(text);
278          text = tr("Status Storage \"%1\" in Window").arg(m_currentStorage);;
279          actionStatusStorageWindow->setText(text);
280          text = tr("Label media in Storage \"%1\"").arg(m_currentStorage);
281          actionLabelStorage->setText(text);
282          text = tr("Mount media in Storage \"%1\"").arg(m_currentStorage);
283          actionMountStorage->setText(text);
284          text = tr("\"UN\" Mount media in Storage \"%1\"").arg(m_currentStorage);
285          text = tr("Release media in Storage \"%1\"").arg(m_currentStorage);
286          actionRelease->setText(text);
287          if (m_currentAutoChanger) {
288             mp_treeWidget->addAction(actionUpdateSlots);
289             mp_treeWidget->addAction(actionUpdateSlotsScan);
290             text = tr("Barcode Scan media in Storage \"%1\"").arg(m_currentStorage);
291             actionUpdateSlots->setText(text);
292             text = tr("Read scan media in Storage \"%1\"").arg( m_currentStorage);
293             actionUpdateSlotsScan->setText(text);
294          }
295       }
296    }
297 }
298
299 /* 
300  * Setup a context menu 
301  * Made separate from populate so that it would not create context menu over and
302  * over as the tree is repopulated.
303  */
304 void Storage::createContextMenu()
305 {
306    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
307    mp_treeWidget->addAction(actionRefreshStorage);
308    connect(mp_treeWidget, SIGNAL(
309            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
310            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
311    /* connect to the action specific to this pages class */
312    connect(actionRefreshStorage, SIGNAL(triggered()), this,
313                 SLOT(populateTree()));
314    connect(actionStatusStorageInConsole, SIGNAL(triggered()), this,
315                 SLOT(consoleStatusStorage()));
316    connect(actionLabelStorage, SIGNAL(triggered()), this,
317                 SLOT(consoleLabelStorage()));
318    connect(actionMountStorage, SIGNAL(triggered()), this,
319                 SLOT(consoleMountStorage()));
320    connect(actionUnMountStorage, SIGNAL(triggered()), this,
321                 SLOT(consoleUnMountStorage()));
322    connect(actionUpdateSlots, SIGNAL(triggered()), this,
323                 SLOT(consoleUpdateSlots()));
324    connect(actionUpdateSlotsScan, SIGNAL(triggered()), this,
325                 SLOT(consoleUpdateSlotsScan()));
326    connect(actionRelease, SIGNAL(triggered()), this,
327                 SLOT(consoleRelease()));
328    connect(actionStatusStorageWindow, SIGNAL(triggered()), this,
329                 SLOT(statusStorageWindow()));
330    connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
331            this, SLOT(contentWindow()));
332
333 }
334
335 void Storage::contentWindow()
336 {
337    if (m_currentStorage != "" && m_currentAutoChanger) { 
338       QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
339       new Content(m_currentStorage, parentItem);
340    }
341 }
342
343 /*
344  * Virtual function which is called when this page is visible on the stack
345  */
346 void Storage::currentStackItem()
347 {
348    if(!m_populated) {
349       populateTree();
350       /* Create the context menu for the storage tree */
351       createContextMenu();
352    }
353 }
354
355 /*
356  *  Functions to respond to local context sensitive menu sending console commands
357  *  If I could figure out how to make these one function passing a string, Yaaaaaa
358  */
359 void Storage::consoleStatusStorage()
360 {
361    QString cmd("status storage=");
362    cmd += m_currentStorage;
363    consoleCommand(cmd);
364 }
365
366 /* Label Media populating current storage by default */
367 void Storage::consoleLabelStorage()
368 {
369    new labelPage(m_currentStorage);
370 }
371
372 /* Mount currently selected storage */
373 void Storage::consoleMountStorage()
374 {
375    if (m_currentAutoChanger == 0){
376       /* no autochanger, just execute the command in the console */
377       QString cmd("mount storage=");
378       cmd += m_currentStorage;
379       consoleCommand(cmd);
380    } else {
381       setConsoleCurrent();
382       /* if this storage is an autochanger, lets ask for the slot */
383       new mountDialog(m_console, m_currentStorage);
384    }
385 }
386
387 /* Unmount Currently selected storage */
388 void Storage::consoleUnMountStorage()
389 {
390    QString cmd("umount storage=");
391    cmd += m_currentStorage;
392    consoleCommand(cmd);
393 }
394
395 /* Update Slots */
396 void Storage::consoleUpdateSlots()
397 {
398    QString cmd("update slots storage=");
399    cmd += m_currentStorage;
400    consoleCommand(cmd);
401 }
402
403 /* Update Slots Scan*/
404 void Storage::consoleUpdateSlotsScan()
405 {
406    QString cmd("update slots scan storage=");
407    cmd += m_currentStorage;
408    consoleCommand(cmd);
409 }
410
411 /* Release a tape in the drive */
412 void Storage::consoleRelease()
413 {
414    QString cmd("release storage=");
415    cmd += m_currentStorage;
416    consoleCommand(cmd);
417 }
418
419 /*
420  *  Open a status storage window
421  */
422 void Storage::statusStorageWindow()
423 {
424    /* if one exists, then just set it current */
425    bool found = false;
426    foreach(Pages *page, mainWin->m_pagehash) {
427       if (mainWin->currentConsole() == page->console()) {
428          if (page->name() == tr("Storage Status %1").arg(m_currentStorage)) {
429             found = true;
430             page->setCurrent();
431          }
432       }
433    }
434    if (!found) {
435       QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
436       new StorStat(m_currentStorage, parentItem);
437    }
438 }
439
440 /*
441  * Write settings to save expanded states of the pools
442  */
443 void Storage::writeExpandedSettings()
444 {
445    QSettings settings(m_console->m_dir->name(), "bat");
446    settings.beginGroup("StorageTreeExpanded");
447    int childcount = m_topItem->childCount();
448    for (int cnt=0; cnt<childcount; cnt++) {
449       QTreeWidgetItem *item = m_topItem->child(cnt);
450       settings.setValue(item->text(0), item->isExpanded());
451    }
452    settings.endGroup();
453 }
454
455 /*
456  * If first time, then check to see if there were status pages open the last time closed
457  * if so open
458  */
459 void Storage::settingsOpenStatus(QString &storage)
460 {
461    QSettings settings(m_console->m_dir->name(), "bat");
462
463    settings.beginGroup("OpenOnExit");
464    QString toRead = "StorageStatus_" + storage;
465    if (settings.value(toRead) == 1) {
466       if (mainWin->m_sqlDebug) {
467          Pmsg1(000, "Do open Storage Status window for : %s\n", storage.toUtf8().data());
468       }
469       new StorStat(storage, mainWin->getFromHash(this));
470       setCurrent();
471       mainWin->getFromHash(this)->setExpanded(true);
472    } else {
473       if (mainWin->m_sqlDebug) {
474          Pmsg1(000, "Do NOT open Storage Status window for : %s\n", storage.toUtf8().data());
475       }
476    }
477    settings.endGroup();
478 }