]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/medialist/medialist.cpp
Correct Pol typo
[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 #include "run/run.h"
46
47 MediaList::MediaList()
48 {
49    setupUi(this);
50    m_name = "Media";
51    pgInitialize();
52    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
53    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.svg")));
54
55    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
56    m_populated = false;
57    m_checkcurwidget = true;
58    m_closeable = false;
59    /* add context sensitive menu items specific to this classto the page
60     * selector tree. m_contextActions is QList of QActions */
61    m_contextActions.append(actionRefreshMediaList);
62 }
63
64 MediaList::~MediaList()
65 {
66 }
67
68 /*
69  * The main meat of the class!!  The function that querries the director and 
70  * creates the widgets with appropriate values.
71  */
72 void MediaList::populateTree()
73 {
74    QTreeWidgetItem *mediatreeitem, *pooltreeitem, *topItem;
75
76    if (!m_console->preventInUseConnect())
77        return;
78
79    QStringList headerlist = (QStringList()
80       << "Volume Name" << "Id" << "Status" << "Enabled" << "Bytes" << "Files"
81       << "Jobs" << "Retention" << "Media Type" << "Slot" << "Use Duration"
82       << "Max Jobs" << "Max Files" << "Max Bytes" << "Recycle" << "Enabled"
83       << "RecyclePool" << "Last Written");
84    int statusIndex = headerlist.indexOf("Status");
85
86    m_checkcurwidget = false;
87    mp_treeWidget->clear();
88    m_checkcurwidget = true;
89    mp_treeWidget->setColumnCount(headerlist.count());
90    topItem = new QTreeWidgetItem(mp_treeWidget);
91    topItem->setText(0, "Pools");
92    topItem->setData(0, Qt::UserRole, 0);
93    topItem->setExpanded(true);
94    
95    mp_treeWidget->setHeaderLabels(headerlist);
96
97    QString query;
98
99    foreach (QString pool_listItem, m_console->pool_list) {
100       pooltreeitem = new QTreeWidgetItem(topItem);
101       pooltreeitem->setText(0, pool_listItem);
102       pooltreeitem->setData(0, Qt::UserRole, 1);
103       pooltreeitem->setExpanded(true);
104
105       query =  "SELECT Media.VolumeName AS Media, "
106          " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
107          " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
108          " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
109          " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
110          " Media.Slot AS Slot, Media.VolUseDuration AS UseDuration,"
111          " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
112          " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
113          " Media.Enabled AS enabled, Pol.Name AS RecyclePool,"
114          " Media.LastWritten AS LastWritten"
115          " FROM Media"
116          " LEFT OUTER JOIN Pool ON (Media.PoolId=Pool.PoolId)"
117          " LEFT OUTER JOIN Pool AS Pol ON (Media.recyclepoolid=Pol.PoolId)"
118          " WHERE";
119       query += " Pool.Name='" + pool_listItem + "'";
120       query += " ORDER BY Media";
121    
122       if (mainWin->m_sqlDebug) {
123          Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
124       }
125       QStringList results;
126       if (m_console->sql_cmd(query, results)) {
127          QString field;
128          QStringList fieldlist;
129
130          /* Iterate through the lines of results. */
131          foreach (QString resultline, results) {
132             fieldlist = resultline.split("\t");
133             int index = 0;
134             mediatreeitem = new QTreeWidgetItem(pooltreeitem);
135
136             /* Iterate through fields in the record */
137             foreach (field, fieldlist) {
138                field = field.trimmed();  /* strip leading & trailing spaces */
139                if (field != "") {
140                   mediatreeitem->setData(index, Qt::UserRole, 2);
141                   mediatreeitem->setData(index, Qt::UserRole, 2);
142                   mediatreeitem->setText(index, field);
143                   if (index == statusIndex) {
144                      setStatusColor(mediatreeitem, field, index);
145                   }
146                }
147                index++;
148             } /* foreach field */
149          } /* foreach resultline */
150       } /* if results from query */
151    } /* foreach pool_listItem */
152    /* Resize the columns */
153    for(int cnter=0; cnter<headerlist.count(); cnter++) {
154       mp_treeWidget->resizeColumnToContents(cnter);
155    }
156 }
157
158 void MediaList::setStatusColor(QTreeWidgetItem *item, QString &field, int &index)
159 {
160    if (field == "Append" ) {
161       item->setBackground(index, Qt::green);
162    } else if (field == "Error") {
163       item->setBackground(index, Qt::red);
164    } else if ((field == "Used") || ("Full")){
165       item->setBackground(index, Qt::yellow);
166    }
167 }
168
169 /*
170  * Called from the signal of the context sensitive menu!
171  */
172 void MediaList::editVolume()
173 {
174    MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
175    connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
176 }
177
178 /*
179  * Called from the signal of the context sensitive menu!
180  */
181 void MediaList::showJobs()
182 {
183    QString emptyclient("");
184    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
185    mainWin->createPageJobList(m_currentVolumeName, emptyclient, parentItem);
186 }
187
188 /*
189  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
190  * The tree has been populated.
191  */
192 void MediaList::PgSeltreeWidgetClicked()
193 {
194    if (!m_populated) {
195       populateTree();
196       createContextMenu();
197       m_populated=true;
198    }
199 }
200
201 /*
202  * Added to set the context menu policy based on currently active treeWidgetItem
203  * signaled by currentItemChanged
204  */
205 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
206 {
207    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
208    if (m_checkcurwidget) {
209       /* The Previous item */
210       if (previouswidgetitem) { /* avoid a segfault if first time */
211          mp_treeWidget->removeAction(actionEditVolume);
212          mp_treeWidget->removeAction(actionListJobsOnVolume);
213          mp_treeWidget->removeAction(actionDeleteVolume);
214          mp_treeWidget->removeAction(actionPruneVolume);
215          mp_treeWidget->removeAction(actionPurgeVolume);
216           mp_treeWidget->removeAction(actionRelabelVolume);
217          mp_treeWidget->removeAction(actionAllVolumesFromPool);
218       }
219
220       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
221       m_currentVolumeName=currentwidgetitem->text(0);
222       if (treedepth == 2){
223          m_currentVolumeId=currentwidgetitem->text(1);
224          mp_treeWidget->addAction(actionEditVolume);
225          mp_treeWidget->addAction(actionListJobsOnVolume);
226          mp_treeWidget->addAction(actionDeleteVolume);
227          mp_treeWidget->addAction(actionPruneVolume);
228          mp_treeWidget->addAction(actionPurgeVolume);
229          mp_treeWidget->addAction(actionRelabelVolume);
230       } else if (treedepth == 1) {
231 /*  *******FIXME******
232  *  I can't seem to get "All volumes from pool" or "Volume from pool" to work
233  *  in one sentence command.   Works when you do it one step at a time vi console
234          mp_treeWidget->addAction(actionAllVolumesFromPool);
235 */
236       }
237    }
238 }
239
240 /* 
241  * Setup a context menu 
242  * Made separate from populate so that it would not create context menu over and
243  * over as the tree is repopulated.
244  */
245 void MediaList::createContextMenu()
246 {
247    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
248    mp_treeWidget->addAction(actionRefreshMediaList);
249    connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
250    connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
251    connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
252    connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
253    connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
254    connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
255    connect(mp_treeWidget, SIGNAL(
256            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
257            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
258    /* connect to the action specific to this pages class */
259    connect(actionRefreshMediaList, SIGNAL(triggered()), this,
260                 SLOT(populateTree()));
261    connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
262 }
263
264 /*
265  * Virtual function which is called when this page is visible on the stack
266  */
267 void MediaList::currentStackItem()
268 {
269    if(!m_populated) {
270       populateTree();
271       /* Create the context menu for the medialist tree */
272       createContextMenu();
273       m_populated=true;
274    }
275 }
276
277 /*
278  * Called from the signal of the context sensitive menu to delete a volume!
279  */
280 void MediaList::deleteVolume()
281 {
282    if (QMessageBox::warning(this, tr("Bat"),
283       tr("Are you sure you want to delete??  !!!.\n"
284 "This delete command is used to delete a Volume record and all associated catalog"
285 " records that were created. This command operates only on the Catalog"
286 " database and has no effect on the actual data written to a Volume. This"
287 " command can be dangerous and we strongly recommend that you do not use"
288 " it unless you know what you are doing.  All Jobs and all associated"
289 " records (File and JobMedia) will be deleted from the catalog."
290       "Press OK to proceed with delete operation.?"),
291       QMessageBox::Ok | QMessageBox::Cancel)
292       == QMessageBox::Cancel) { return; }
293
294    QString cmd("delete volume=");
295    cmd += m_currentVolumeName;
296    consoleCommand(cmd);
297 }
298
299 /*
300  * Called from the signal of the context sensitive menu to purge!
301  */
302 void MediaList::purgeVolume()
303 {
304    if (QMessageBox::warning(this, tr("Bat"),
305       tr("Are you sure you want to purge ??  !!!.\n"
306 "The Purge command will delete associated Catalog database records from Jobs and"
307 " Volumes without considering the retention period. Purge  works only on the"
308 " Catalog database and does not affect data written to Volumes. This command can"
309 " be dangerous because you can delete catalog records associated with current"
310 " backups of files, and we recommend that you do not use it unless you know what"
311 " you are doing.\n"
312       "Press OK to proceed with the purge operation?"),
313       QMessageBox::Ok | QMessageBox::Cancel)
314       == QMessageBox::Cancel) { return; }
315
316    QString cmd("purge volume=");
317    cmd += m_currentVolumeName;
318    consoleCommand(cmd);
319    populateTree();
320 }
321
322 /*
323  * Called from the signal of the context sensitive menu to prune!
324  */
325 void MediaList::pruneVolume()
326 {
327    new prunePage(m_currentVolumeName, "");
328 }
329
330 /*
331  * Called from the signal of the context sensitive menu to relabel!
332  */
333 void MediaList::relabelVolume()
334 {
335    setConsoleCurrent();
336    new relabelDialog(m_console, m_currentVolumeName);
337 }
338
339 /*
340  * Called from the signal of the context sensitive menu to purge!
341  */
342 void MediaList::allVolumesFromPool()
343 {
344    QString cmd("update pool=");
345    cmd += m_currentVolumeName + " All Volumes From Pool";
346    consoleCommand(cmd);
347    populateTree();
348 }