]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/medialist/medialist.cpp
Prevent connecting with the Console::m_at_main_prompt member.
[bacula/bacula] / bacula / src / qt-console / medialist / medialist.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: medialist.cpp 4230 2007-02-21 20:07:37Z kerns $
31  *
32  *  MediaList Class
33  *
34  *   Dirk Bartley, March 2007
35  *
36  */ 
37
38 #include <QAbstractEventDispatcher>
39 #include <QMenu>
40 #include "bat.h"
41 #include "medialist.h"
42 #include "mediaedit/mediaedit.h"
43 #include "joblist/joblist.h"
44 #include "relabel/relabel.h"
45
46 MediaList::MediaList()
47 {
48    setupUi(this);
49    m_name = "Media";
50    pgInitialize();
51
52    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
53    m_populated = false;
54    m_checkcurwidget = true;
55    m_closeable = false;
56 }
57
58 MediaList::~MediaList()
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 MediaList::populateTree()
67 {
68    QTreeWidgetItem *mediatreeitem, *pooltreeitem, *topItem;
69
70    if (!m_console->preventInUseConnect())
71        return;
72
73    QStringList headerlist = (QStringList()
74       << "Volume Name" << "Id" << "Status" << "Enabled"
75       << "Bytes" << "Files" << "Jobs" << "Retention" 
76       << "Media Type" << "Slot" << "Last Written");
77    int statusIndex = headerlist.indexOf("Status");
78
79    m_checkcurwidget = false;
80    mp_treeWidget->clear();
81    m_checkcurwidget = true;
82    mp_treeWidget->setColumnCount(headerlist.count());
83    topItem = new QTreeWidgetItem(mp_treeWidget);
84    topItem->setText(0, "Pools");
85    topItem->setData(0, Qt::UserRole, 0);
86    topItem->setExpanded(true);
87    
88    mp_treeWidget->setHeaderLabels(headerlist);
89
90    QString query;
91
92    foreach (QString pool_listItem, m_console->pool_list) {
93       pooltreeitem = new QTreeWidgetItem(topItem);
94       pooltreeitem->setText(0, pool_listItem);
95       pooltreeitem->setData(0, Qt::UserRole, 1);
96       pooltreeitem->setExpanded(true);
97
98       query =  "SELECT Media.VolumeName AS Media, "
99          " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
100          " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
101          " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
102          " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
103          " Media.Slot AS Slot, Media.LastWritten AS LastWritten"
104          " FROM Media, Pool"
105          " WHERE Media.PoolId=Pool.PoolId";
106       query += " AND Pool.Name='" + pool_listItem + "'";
107       query += " ORDER BY Media";
108    
109       /* FIXME Make this a user configurable loggin action and dont use printf */
110       //printf("MediaList query cmd : %s\n",query.toUtf8().data());
111       QStringList results;
112       if (m_console->sql_cmd(query, results)) {
113          QString field;
114          QStringList fieldlist;
115
116          /* Iterate through the lines of results. */
117          foreach (QString resultline, results) {
118             fieldlist = resultline.split("\t");
119             int index = 0;
120             mediatreeitem = new QTreeWidgetItem(pooltreeitem);
121
122             /* Iterate through fields in the record */
123             foreach (field, fieldlist) {
124                field = field.trimmed();  /* strip leading & trailing spaces */
125                if (field != "") {
126                   mediatreeitem->setData(index, Qt::UserRole, 2);
127                   mediatreeitem->setData(index, Qt::UserRole, 2);
128                   mediatreeitem->setText(index, field);
129                   if (index == statusIndex) {
130                      setStatusColor(mediatreeitem, field, index);
131                   }
132                }
133                index++;
134             } /* foreach field */
135          } /* foreach resultline */
136       } /* if results from query */
137    } /* foreach pool_listItem */
138    /* Resize the columns */
139    for(int cnter=0; cnter<headerlist.count(); cnter++) {
140       mp_treeWidget->resizeColumnToContents(cnter);
141    }
142 }
143
144 void MediaList::setStatusColor(QTreeWidgetItem *item, QString &field, int &index)
145 {
146    if (field == "Append" ) {
147       item->setBackground(index, Qt::green);
148    } else if (field == "Error") {
149       item->setBackground(index, Qt::red);
150    } else if ((field == "Used") || ("Full")){
151       item->setBackground(index, Qt::yellow);
152    }
153 }
154
155 /*
156  * Called from the signal of the context sensitive menu!
157  */
158 void MediaList::editVolume()
159 {
160    MediaEdit* edit = new MediaEdit(m_console, m_currentVolumeId);
161    connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
162 }
163
164 /*
165  * Called from the signal of the context sensitive menu!
166  */
167 void MediaList::showJobs()
168 {
169    QString emptyclient("");
170    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
171    mainWin->createPageJobList(m_currentVolumeName, emptyclient, parentItem);
172 }
173
174 /*
175  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
176  * The tree has been populated.
177  */
178 void MediaList::PgSeltreeWidgetClicked()
179 {
180    if (!m_populated) {
181       populateTree();
182       createContextMenu();
183       m_populated=true;
184    }
185 }
186
187 /*
188  * Added to set the context menu policy based on currently active treeWidgetItem
189  * signaled by currentItemChanged
190  */
191 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
192 {
193    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
194    if (m_checkcurwidget) {
195       /* The Previous item */
196       if (previouswidgetitem) { /* avoid a segfault if first time */
197          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
198          if (treedepth == 2){
199             mp_treeWidget->removeAction(actionEditVolume);
200             mp_treeWidget->removeAction(actionListJobsOnVolume);
201             mp_treeWidget->removeAction(actionDeleteVolume);
202             mp_treeWidget->removeAction(actionPurgeVolume);
203             mp_treeWidget->removeAction(actionRelabelVolume);
204          }
205       }
206
207       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
208       if (treedepth == 2){
209          m_currentVolumeName=currentwidgetitem->text(0);
210          m_currentVolumeId=currentwidgetitem->text(1);
211          mp_treeWidget->addAction(actionEditVolume);
212          mp_treeWidget->addAction(actionListJobsOnVolume);
213          mp_treeWidget->addAction(actionDeleteVolume);
214          mp_treeWidget->addAction(actionPurgeVolume);
215          mp_treeWidget->addAction(actionRelabelVolume);
216       }
217    }
218 }
219
220 /* 
221  * Setup a context menu 
222  * Made separate from populate so that it would not create context menu over and
223  * over as the tree is repopulated.
224  */
225 void MediaList::createContextMenu()
226 {
227    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
228    mp_treeWidget->addAction(actionRefreshMediaList);
229    connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
230    connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
231    connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
232    connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
233    connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
234    connect(mp_treeWidget, SIGNAL(
235            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
236            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
237    /* connect to the action specific to this pages class */
238    connect(actionRefreshMediaList, SIGNAL(triggered()), this,
239                 SLOT(populateTree()));
240 }
241
242 /*
243  * Virtual function which is called when this page is visible on the stack
244  */
245 void MediaList::currentStackItem()
246 {
247    if(!m_populated) {
248       populateTree();
249       /* add context sensitive menu items specific to this classto the page
250        * selector tree. m_m_contextActions is QList of QActions, so this is 
251        * only done once with the first population. */
252       m_contextActions.append(actionRefreshMediaList);
253       /* Create the context menu for the medialist tree */
254       createContextMenu();
255       m_populated=true;
256    }
257 }
258
259 /*
260  * Called from the signal of the context sensitive menu!
261  */
262 void MediaList::deleteVolume()
263 {
264    if (QMessageBox::warning(this, tr("Bat"),
265       tr("Are you sure you want to delete??  !!!.\n"
266 "This delete command is used to delete a Volume record and all associated catalog"
267 " records that were created. This command operates only on the Catalog"
268 " database and has no effect on the actual data written to a Volume. This"
269 " command can be dangerous and we strongly recommend that you do not use"
270 " it unless you know what you are doing.  All Jobs and all associated"
271 " records (File and JobMedia) will be deleted from the catalog."
272       "Press OK to proceed with delete operation.?"),
273       QMessageBox::Ok | QMessageBox::Cancel)
274       == QMessageBox::Cancel) { return; }
275
276    QString cmd("delete volume=");
277    cmd += m_currentVolumeName;
278    consoleCommand(cmd);
279 }
280 /*
281  * Called from the signal of the context sensitive menu!
282  */
283 void MediaList::purgeVolume()
284 {
285    if (QMessageBox::warning(this, tr("Bat"),
286       tr("Are you sure you want to purge ??  !!!.\n"
287 "The Purge command will delete associated Catalog database records from Jobs and"
288 " Volumes without considering the retention period. Purge  works only on the"
289 " Catalog database and does not affect data written to Volumes. This command can"
290 " be dangerous because you can delete catalog records associated with current"
291 " backups of files, and we recommend that you do not use it unless you know what"
292 " you are doing.\n"
293       "Press OK to proceed with the purge operation?"),
294       QMessageBox::Ok | QMessageBox::Cancel)
295       == QMessageBox::Cancel) { return; }
296
297    QString cmd("purge volume=");
298    cmd += m_currentVolumeName;
299    consoleCommand(cmd);
300    populateTree();
301 }
302 /*
303  * Called from the signal of the context sensitive menu!
304  */
305 void MediaList::relabelVolume()
306 {
307    setConsoleCurrent();
308    new relabelDialog(m_console, m_currentVolumeName);
309 }