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