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