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