]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/storage/storage.cpp
Add context sensitive options in storage for label, mount and umount.
[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: storage.cpp 4230 2007-02-21 20:07:37Z kerns $
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/storage.h"
42
43 Storage::Storage()
44 {
45    setupUi(this);
46    m_name = "Storage";
47    pgInitialize();
48
49    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_storage.h */
50    m_populated = false;
51    m_checkcurwidget = true;
52    m_closeable = false;
53    m_currentStorage = "";
54    setTitle();
55 }
56
57 Storage::~Storage()
58 {
59 }
60
61 /*
62  * The main meat of the class!!  The function that querries the director and 
63  * creates the widgets with appropriate values.
64  */
65 void Storage::populateTree()
66 {
67    QTreeWidgetItem *storageItem, *topItem;
68
69    m_checkcurwidget = false;
70    mp_treeWidget->clear();
71    m_checkcurwidget = true;
72
73    QStringList headerlist = (QStringList() << "Storage Name" << "Storage Id"
74        << "Auto Changer");
75
76    topItem = new QTreeWidgetItem(mp_treeWidget);
77    topItem->setText(0, "Storage");
78    topItem->setData(0, Qt::UserRole, 0);
79    topItem->setExpanded(true);
80
81    mp_treeWidget->setColumnCount(headerlist.count());
82    mp_treeWidget->setHeaderLabels(headerlist);
83    /* This could be a log item */
84    //printf("In Storage::populateTree()\n");
85
86    foreach(QString storageName, m_console->storage_list){
87       storageItem = new QTreeWidgetItem(topItem);
88       storageItem->setText(0, storageName);
89       storageItem->setData(0, Qt::UserRole, 1);
90       storageItem->setExpanded(true);
91
92       /* Set up query QString and header QStringList */
93       QString query("");
94       query += "SELECT Name AS StorageName, StorageId AS ID, AutoChanger"
95            " FROM Storage"
96            " WHERE ";
97       query += " Name='" + storageName + "'";
98       query += " ORDER BY Name";
99
100       QStringList results;
101       /* This could be a log item */
102       //printf("Storage query cmd : %s\n",query.toUtf8().data());
103       if (m_console->sql_cmd(query, results)) {
104          int resultCount = results.count();
105          if (resultCount == 1){
106             QString resultline;
107             QString field;
108             QStringList fieldlist;
109             /* there will only be one of these */
110             foreach (resultline, results) {
111                fieldlist = resultline.split("\t");
112                int index = 0;
113                /* Iterate through fields in the record */
114                foreach (field, fieldlist) {
115                   field = field.trimmed();  /* strip leading & trailing spaces */
116                   storageItem->setData(index+1, Qt::UserRole, 1);
117                   /* Put media fields under the pool tree item */
118                   storageItem->setData(index+1, Qt::UserRole, 1);
119                   storageItem->setText(index+1, field);
120                   index++;
121                }
122             }
123          }
124       }
125    }
126    /* Resize the columns */
127    for(int cnter=1; cnter<headerlist.size(); cnter++) {
128       mp_treeWidget->resizeColumnToContents(cnter);
129    }
130 }
131
132 /*
133  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
134  * The tree has been populated.
135  */
136 void Storage::PgSeltreeWidgetClicked()
137 {
138    if(!m_populated) {
139       populateTree();
140       createContextMenu();
141       m_populated=true;
142    }
143 }
144
145 /*
146  * Added to set the context menu policy based on currently active treeWidgetItem
147  * signaled by currentItemChanged
148  */
149 void Storage::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
150 {
151    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
152    if (m_checkcurwidget) {
153       /* The Previous item */
154       if (previouswidgetitem) { /* avoid a segfault if first time */
155          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
156          if (treedepth == 1){
157             mp_treeWidget->removeAction(actionStatusStorageInConsole);
158             mp_treeWidget->removeAction(actionLabelStorage);
159             mp_treeWidget->removeAction(actionMountStorage);
160             mp_treeWidget->removeAction(actionUnMountStorage);
161          }
162       }
163
164       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
165       if (treedepth == 1){
166          /* set a hold variable to the storage name in case the context sensitive
167           * menu is used */
168          m_currentStorage=currentwidgetitem->text(0);
169          mp_treeWidget->addAction(actionStatusStorageInConsole);
170          mp_treeWidget->addAction(actionLabelStorage);
171          mp_treeWidget->addAction(actionMountStorage);
172          mp_treeWidget->addAction(actionUnMountStorage);
173          QString text;
174          text = "Status Storage " + m_currentStorage;
175          actionStatusStorageInConsole->setText(text);
176          text = "Label media in Storage " + m_currentStorage;
177          actionLabelStorage->setText(text);
178          text = "Mount media in Storage " + m_currentStorage;
179          actionMountStorage->setText(text);
180          text = "\"UN\" Mount media in Storage " + m_currentStorage;
181          actionUnMountStorage->setText(text);
182       }
183    }
184 }
185
186 /* 
187  * Setup a context menu 
188  * Made separate from populate so that it would not create context menu over and
189  * over as the tree is repopulated.
190  */
191 void Storage::createContextMenu()
192 {
193    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
194    mp_treeWidget->addAction(actionRefreshStorage);
195    connect(mp_treeWidget, SIGNAL(
196            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
197            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
198    /* connect to the action specific to this pages class */
199    connect(actionRefreshStorage, SIGNAL(triggered()), this,
200                 SLOT(populateTree()));
201    connect(actionStatusStorageInConsole, SIGNAL(triggered()), this,
202                 SLOT(consoleStatusStorage()));
203    connect(actionLabelStorage, SIGNAL(triggered()), this,
204                 SLOT(consoleLabelStorage()));
205    connect(actionMountStorage, SIGNAL(triggered()), this,
206                 SLOT(consoleMountStorage()));
207    connect(actionUnMountStorage, SIGNAL(triggered()), this,
208                 SLOT(consoleUnMountStorage()));
209 }
210
211 /*
212  * Virtual function which is called when this page is visible on the stack
213  */
214 void Storage::currentStackItem()
215 {
216    if(!m_populated) {
217       populateTree();
218       /* add context sensitive menu items specific to this classto the page
219        * selector tree. m_m_contextActions is QList of QActions, so this is 
220        * only done once with the first population. */
221       m_contextActions.append(actionRefreshStorage);
222       /* Create the context menu for the storage tree */
223       createContextMenu();
224       m_populated=true;
225    }
226 }
227
228 /*
229  *  Functions to respond to local context sensitive menu sending console commands
230  *  If I could figure out how to make these one function passing a string, Yaaaaaa
231  */
232 void Storage::consoleStatusStorage()
233 {
234    QString cmd("status storage=");
235    cmd += m_currentStorage;
236    consoleCommand(cmd);
237 }
238 void Storage::consoleLabelStorage()
239 {
240    new labelDialog(m_console, m_currentStorage);
241 }
242 void Storage::consoleMountStorage()
243 {
244    QString cmd("mount storage=");
245    cmd += m_currentStorage;
246    consoleCommand(cmd);
247 }
248 void Storage::consoleUnMountStorage()
249 {
250    QString cmd("umount storage=");
251    cmd += m_currentStorage;
252    consoleCommand(cmd);
253 }