]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/storage/storage.cpp
Added Release in context of storage.
[bacula/bacula] / bacula / src / qt-console / storage / storage.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 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 plus additions
11    that are listed 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 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.
27 */
28  
29 /*
30  *   Version $Id$
31  *
32  *  Storage Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 #include <QAbstractEventDispatcher>
39 #include <QMenu>
40 #include "bat.h"
41 #include "storage.h"
42 #include "label/label.h"
43 #include "../mount/mount.h"
44
45 Storage::Storage()
46 {
47    setupUi(this);
48    m_name = "Storage";
49    pgInitialize();
50    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
51    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/package-x-generic.png")));
52
53    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_storage.h */
54    m_populated = false;
55    m_checkcurwidget = true;
56    m_closeable = false;
57    m_currentStorage = "";
58    /* add context sensitive menu items specific to this classto the page
59     * selector tree. m_contextActions is QList of QActions */
60    m_contextActions.append(actionRefreshStorage);
61 }
62
63 Storage::~Storage()
64 {
65 }
66
67 /*
68  * The main meat of the class!!  The function that querries the director and 
69  * creates the widgets with appropriate values.
70  */
71 void Storage::populateTree()
72 {
73    QTreeWidgetItem *storageItem, *topItem;
74
75    if (!m_console->preventInUseConnect())
76        return;
77
78    m_checkcurwidget = false;
79    mp_treeWidget->clear();
80    m_checkcurwidget = true;
81
82    QStringList headerlist = (QStringList() << "Storage Name" << "Storage Id"
83        << "Auto Changer");
84
85    topItem = new QTreeWidgetItem(mp_treeWidget);
86    topItem->setText(0, "Storage");
87    topItem->setData(0, Qt::UserRole, 0);
88    topItem->setExpanded(true);
89
90    mp_treeWidget->setColumnCount(headerlist.count());
91    mp_treeWidget->setHeaderLabels(headerlist);
92
93    foreach(QString storageName, m_console->storage_list){
94       storageItem = new QTreeWidgetItem(topItem);
95       storageItem->setText(0, storageName);
96       storageItem->setData(0, Qt::UserRole, 1);
97       storageItem->setExpanded(true);
98
99       /* Set up query QString and header QStringList */
100       QString query("SELECT StorageId AS ID, AutoChanger AS Changer"
101                " FROM Storage WHERE");
102       query += " Name='" + storageName + "'"
103                " ORDER BY Name";
104
105       QStringList results;
106       /* This could be a log item */
107       if (mainWin->m_sqlDebug) {
108          Pmsg1(000, "Storage query cmd : %s\n",query.toUtf8().data());
109       }
110       if (m_console->sql_cmd(query, results)) {
111          int resultCount = results.count();
112          if (resultCount == 1){
113             QString resultline;
114             QString field;
115             QStringList fieldlist;
116             /* there will only be one of these */
117             foreach (resultline, results) {
118                fieldlist = resultline.split("\t");
119                int index = 0;
120                /* Iterate through fields in the record */
121                foreach (field, fieldlist) {
122                   field = field.trimmed();  /* strip leading & trailing spaces */
123                   storageItem->setData(index+1, Qt::UserRole, 1);
124                   /* Put media fields under the pool tree item */
125                   storageItem->setData(index+1, Qt::UserRole, 1);
126                   storageItem->setText(index+1, field);
127                   index++;
128                }
129             }
130          }
131       }
132    }
133    /* Resize the columns */
134    for(int cnter=1; cnter<headerlist.size(); cnter++) {
135       mp_treeWidget->resizeColumnToContents(cnter);
136    }
137 }
138
139 /*
140  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
141  * The tree has been populated.
142  */
143 void Storage::PgSeltreeWidgetClicked()
144 {
145    if(!m_populated) {
146       populateTree();
147       createContextMenu();
148       m_populated=true;
149    }
150 }
151
152 /*
153  * Added to set the context menu policy based on currently active treeWidgetItem
154  * signaled by currentItemChanged
155  */
156 void Storage::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
157 {
158    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
159    if (m_checkcurwidget) {
160       /* The Previous item */
161       if (previouswidgetitem) { /* avoid a segfault if first time */
162          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
163          if (treedepth == 1){
164             mp_treeWidget->removeAction(actionStatusStorageInConsole);
165             mp_treeWidget->removeAction(actionLabelStorage);
166             mp_treeWidget->removeAction(actionMountStorage);
167             mp_treeWidget->removeAction(actionUnMountStorage);
168             mp_treeWidget->removeAction(actionUpdateSlots);
169             mp_treeWidget->removeAction(actionUpdateSlotsScan);
170             mp_treeWidget->removeAction(actionRelease);
171          }
172       }
173
174       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
175       if (treedepth == 1){
176          /* set a hold variable to the storage name in case the context sensitive
177           * menu is used */
178          m_currentStorage = currentwidgetitem->text(0);
179          m_currentAutoChanger = currentwidgetitem->text(2).toInt();
180          mp_treeWidget->addAction(actionStatusStorageInConsole);
181          mp_treeWidget->addAction(actionLabelStorage);
182          mp_treeWidget->addAction(actionMountStorage);
183          mp_treeWidget->addAction(actionUnMountStorage);
184          mp_treeWidget->addAction(actionRelease);
185          QString text;
186          text = "Status Storage \"" + m_currentStorage + "\"";
187          actionStatusStorageInConsole->setText(text);
188          text = "Label media in Storage \"" + m_currentStorage + "\"";
189          actionLabelStorage->setText(text);
190          text = "Mount media in Storage \"" + m_currentStorage + "\"";
191          actionMountStorage->setText(text);
192          text = "\"UN\" Mount media in Storage \"" + m_currentStorage + "\"";
193          actionUnMountStorage->setText(text);
194          text = "Release media in Storage \"" + m_currentStorage + "\"";
195          actionRelease->setText(text);
196          if (m_currentAutoChanger != 0) {
197             mp_treeWidget->addAction(actionUpdateSlots);
198             mp_treeWidget->addAction(actionUpdateSlotsScan);
199             text = "Barcode Scan media in Storage \"" + m_currentStorage + "\"";
200             actionUpdateSlots->setText(text);
201             text = "Mount and read scan media in Storage \"" + m_currentStorage + "\"";
202             actionUpdateSlotsScan->setText(text);
203          }
204       }
205    }
206 }
207
208 /* 
209  * Setup a context menu 
210  * Made separate from populate so that it would not create context menu over and
211  * over as the tree is repopulated.
212  */
213 void Storage::createContextMenu()
214 {
215    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
216    mp_treeWidget->addAction(actionRefreshStorage);
217    connect(mp_treeWidget, SIGNAL(
218            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
219            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
220    /* connect to the action specific to this pages class */
221    connect(actionRefreshStorage, SIGNAL(triggered()), this,
222                 SLOT(populateTree()));
223    connect(actionStatusStorageInConsole, SIGNAL(triggered()), this,
224                 SLOT(consoleStatusStorage()));
225    connect(actionLabelStorage, SIGNAL(triggered()), this,
226                 SLOT(consoleLabelStorage()));
227    connect(actionMountStorage, SIGNAL(triggered()), this,
228                 SLOT(consoleMountStorage()));
229    connect(actionUnMountStorage, SIGNAL(triggered()), this,
230                 SLOT(consoleUnMountStorage()));
231    connect(actionUpdateSlots, SIGNAL(triggered()), this,
232                 SLOT(consoleUpdateSlots()));
233    connect(actionUpdateSlotsScan, SIGNAL(triggered()), this,
234                 SLOT(consoleUpdateSlotsScan()));
235    connect(actionRelease, SIGNAL(triggered()), this,
236                 SLOT(consoleRelease()));
237 }
238
239 /*
240  * Virtual function which is called when this page is visible on the stack
241  */
242 void Storage::currentStackItem()
243 {
244    if(!m_populated) {
245       populateTree();
246       /* Create the context menu for the storage tree */
247       createContextMenu();
248       m_populated=true;
249    }
250 }
251
252 /*
253  *  Functions to respond to local context sensitive menu sending console commands
254  *  If I could figure out how to make these one function passing a string, Yaaaaaa
255  */
256 void Storage::consoleStatusStorage()
257 {
258    QString cmd("status storage=");
259    cmd += m_currentStorage;
260    consoleCommand(cmd);
261 }
262
263 /* Label Media populating current storage by default */
264 void Storage::consoleLabelStorage()
265 {
266    new labelPage(m_currentStorage);
267 }
268
269 /* Mount currently selected storage */
270 void Storage::consoleMountStorage()
271 {
272    if (m_currentAutoChanger == 0){
273       /* no autochanger, just execute the command in the console */
274       QString cmd("mount storage=");
275       cmd += m_currentStorage;
276       consoleCommand(cmd);
277    } else {
278       setConsoleCurrent();
279       /* if this storage is an autochanger, lets ask for the slot */
280       new mountDialog(m_console, m_currentStorage);
281    }
282 }
283
284 /* Unmount Currently selected storage */
285 void Storage::consoleUnMountStorage()
286 {
287    QString cmd("umount storage=");
288    cmd += m_currentStorage;
289    consoleCommand(cmd);
290 }
291
292 /* Update Slots */
293 void Storage::consoleUpdateSlots()
294 {
295    QString cmd("update slots storage=");
296    cmd += m_currentStorage;
297    consoleCommand(cmd);
298 }
299
300 /* Update Slots Scan*/
301 void Storage::consoleUpdateSlotsScan()
302 {
303    QString cmd("update slots scan storage=");
304    cmd += m_currentStorage;
305    consoleCommand(cmd);
306 }
307
308 /* Release a tape in the drive */
309 void Storage::consoleRelease()
310 {
311    QString cmd("release storage=");
312    cmd += m_currentStorage;
313    consoleCommand(cmd);
314 }