]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/storage/storage.cpp
Auto select console tree widget of current console when a command is entered in the
[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    setTitle();
54 }
55
56 Storage::~Storage()
57 {
58 }
59
60 /*
61  * The main meat of the class!!  The function that querries the director and 
62  * creates the widgets with appropriate values.
63  */
64 void Storage::populateTree()
65 {
66    QTreeWidgetItem *storageItem, *topItem;
67
68    m_checkcurwidget = false;
69    mp_treeWidget->clear();
70    m_checkcurwidget = true;
71
72    QStringList headerlist = (QStringList() << "Storage Name" << "Storage Id"
73        << "Auto Changer");
74
75    topItem = new QTreeWidgetItem(mp_treeWidget);
76    topItem->setText(0, "Storage");
77    topItem->setData(0, Qt::UserRole, 0);
78    topItem->setExpanded(true);
79
80    mp_treeWidget->setColumnCount(headerlist.count());
81    mp_treeWidget->setHeaderLabels(headerlist);
82    /* This could be a log item */
83    //printf("In Storage::populateTree()\n");
84
85    foreach(QString storageName, m_console->storage_list){
86       storageItem = new QTreeWidgetItem(topItem);
87       storageItem->setText(0, storageName);
88       storageItem->setData(0, Qt::UserRole, 1);
89       storageItem->setExpanded(true);
90
91       /* Set up query QString and header QStringList */
92       QString query("");
93       query += "SELECT Name AS StorageName, StorageId AS ID, AutoChanger"
94            " FROM Storage"
95            " WHERE ";
96       query += " Name='" + storageName + "'";
97       query += " ORDER BY Name";
98
99       QStringList results;
100       /* This could be a log item */
101       //printf("Storage query cmd : %s\n",query.toUtf8().data());
102       if (m_console->sql_cmd(query, results)) {
103          int resultCount = results.count();
104          if (resultCount == 1){
105             QString resultline;
106             QString field;
107             QStringList fieldlist;
108             /* there will only be one of these */
109             foreach (resultline, results) {
110                fieldlist = resultline.split("\t");
111                int index = 0;
112                /* Iterate through fields in the record */
113                foreach (field, fieldlist) {
114                   field = field.trimmed();  /* strip leading & trailing spaces */
115                   storageItem->setData(index+1, Qt::UserRole, 1);
116                   /* Put media fields under the pool tree item */
117                   storageItem->setData(index+1, Qt::UserRole, 1);
118                   storageItem->setText(index+1, field);
119                   index++;
120                }
121             }
122          }
123       }
124    }
125 }
126
127 /*
128  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
129  * The tree has been populated.
130  */
131 void Storage::PgSeltreeWidgetClicked()
132 {
133    if(!m_populated) {
134       populateTree();
135       createContextMenu();
136       m_populated=true;
137    }
138 }
139
140 /*
141  * Added to set the context menu policy based on currently active treeWidgetItem
142  * signaled by currentItemChanged
143  */
144 void Storage::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
145 {
146    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
147    if (m_checkcurwidget) {
148       /* The Previous item */
149       if (previouswidgetitem) { /* avoid a segfault if first time */
150          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
151          if (treedepth == 1){
152             mp_treeWidget->removeAction(actionStatusStorageInConsole);
153          }
154       }
155
156       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
157       if (treedepth == 1){
158          /* set a hold variable to the storage name in case the context sensitive
159           * menu is used */
160          m_currentlyselected=currentwidgetitem->text(0);
161          mp_treeWidget->addAction(actionStatusStorageInConsole);
162       }
163    }
164 }
165
166 /* 
167  * Setup a context menu 
168  * Made separate from populate so that it would not create context menu over and
169  * over as the tree is repopulated.
170  */
171 void Storage::createContextMenu()
172 {
173    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
174    mp_treeWidget->addAction(actionRefreshStorage);
175    connect(mp_treeWidget, SIGNAL(
176            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
177            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
178    /* connect to the action specific to this pages class */
179    connect(actionRefreshStorage, SIGNAL(triggered()), this,
180                 SLOT(populateTree()));
181    connect(actionStatusStorageInConsole, SIGNAL(triggered()), this,
182                 SLOT(consoleStatusStorage()));
183 }
184
185 /*
186  * Function responding to actionListJobsofStorage which calls mainwin function
187  * to create a window of a list of jobs of this storage.
188  */
189 void Storage::consoleStatusStorage()
190 {
191    QString cmd("status storage=");
192    cmd += m_currentlyselected;
193    consoleCommand(cmd);
194 //   m_console->write_dir(cmd.toUtf8().data());
195 //   m_console->displayToPrompt();
196    /* Bring this directors console to the front of the stack */
197 //   mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(m_console));
198 }
199
200 /*
201  * Virtual function which is called when this page is visible on the stack
202  */
203 void Storage::currentStackItem()
204 {
205    if(!m_populated) {
206       populateTree();
207       /* add context sensitive menu items specific to this classto the page
208        * selector tree. m_m_contextActions is QList of QActions, so this is 
209        * only done once with the first population. */
210       m_contextActions.append(actionRefreshStorage);
211       /* Create the context menu for the storage tree */
212       createContextMenu();
213       m_populated=true;
214    }
215 }