]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/medialist/medialist.cpp
Tweak some comments and formatting.
[bacula/bacula] / bacula / src / qt-console / medialist / medialist.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2010 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 three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    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 Affero 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 Kern Sibbald.
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  *  MediaList Class
31  *
32  *   Dirk Bartley, March 2007
33  *
34  */ 
35
36 #include "bat.h"
37 #include <QAbstractEventDispatcher>
38 #include <QMenu>
39 #include <math.h>
40 #include "medialist.h"
41 #include "mediaedit/mediaedit.h"
42 #include "mediainfo/mediainfo.h"
43 #include "joblist/joblist.h"
44 #include "relabel/relabel.h"
45 #include "run/run.h"
46 #include "util/fmtwidgetitem.h"
47
48 MediaList::MediaList() : Pages()
49 {
50    setupUi(this);
51    m_name = tr("Pools");
52    pgInitialize();
53    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
54    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
55
56    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
57    m_populated = false;
58    m_checkcurwidget = true;
59    m_closeable = false;
60    /* add context sensitive menu items specific to this classto the page
61     * selector tree. m_contextActions is QList of QActions */
62    m_contextActions.append(actionRefreshMediaList);
63 }
64
65 MediaList::~MediaList()
66 {
67    if (m_populated)
68       writeExpandedSettings();
69 }
70
71 /*
72  * The main meat of the class!!  The function that querries the director and 
73  * creates the widgets with appropriate values.
74  */
75 void MediaList::populateTree()
76 {
77    QTreeWidgetItem *pooltreeitem = NULL;
78
79    if (m_populated) {
80       writeExpandedSettings();
81    }
82    m_populated = true;
83
84    Freeze frz(*mp_treeWidget); /* disable updating*/
85
86    QStringList headerlist = (QStringList()
87       << tr("Volume Name") << tr("Id") << tr("Status") << tr("Enabled") << tr("Bytes") << tr("Files")
88       << tr("Jobs") << tr("Retention") << tr("Media Type") << tr("Slot") << tr("Use Duration")
89       << tr("Max Jobs") << tr("Max Files") << tr("Max Bytes") << tr("Recycle")
90       << tr("Last Written") << tr("First Written") << tr("Read Time")
91       << tr("Write Time") << tr("Recycle Count") << tr("Recycle Pool"));
92
93    m_checkcurwidget = false;
94    mp_treeWidget->clear();
95    m_checkcurwidget = true;
96    mp_treeWidget->setColumnCount(headerlist.count());
97    m_topItem = new QTreeWidgetItem(mp_treeWidget);
98    m_topItem->setText(0, tr("Pools"));
99    m_topItem->setData(0, Qt::UserRole, 0);
100    m_topItem->setExpanded(true);
101    
102    mp_treeWidget->setHeaderLabels(headerlist);
103
104    QSettings settings(m_console->m_dir->name(), "bat");
105    settings.beginGroup("MediaListTreeExpanded");
106    QString query;
107
108
109    /* Comma separated list of pools first */
110    bool first = true;
111    QString pool_comsep("");
112    foreach (QString pool_listItem, m_console->pool_list) {
113       if (first) {
114          pool_comsep += "'" + pool_listItem + "'";
115          first = false;
116       }
117       else
118          pool_comsep += ",'" + pool_listItem + "'";
119    }
120    /* Now use pool_comsep list to perform just one query */
121    if (pool_comsep != "") {
122       query =  "SELECT Pool.Name AS pul,"
123          " Media.VolumeName AS Media, "
124          " Media.MediaId AS Id, Media.VolStatus AS VolStatus,"
125          " Media.Enabled AS Enabled, Media.VolBytes AS Bytes,"
126          " Media.VolFiles AS FileCount, Media.VolJobs AS JobCount,"
127          " Media.VolRetention AS VolumeRetention, Media.MediaType AS MediaType,"
128          " Media.InChanger AS InChanger, Media.Slot AS Slot, "
129          " Media.VolUseDuration AS UseDuration,"
130          " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
131          " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
132          " Media.LastWritten AS LastWritten,"
133          " Media.FirstWritten AS FirstWritten,"
134          " (VolReadTime/1000000) AS ReadTime, (VolWriteTime/1000000) AS WriteTime,"
135          " RecycleCount AS ReCyCount,"
136          " RecPool.Name AS RecyclePool"
137          " FROM Media"
138          " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
139          " LEFT OUTER JOIN Pool AS RecPool ON (Media.RecyclePoolId=RecPool.PoolId)"
140          " WHERE ";
141       query += " Pool.Name IN (" + pool_comsep + ")";
142       query += " ORDER BY Pool.Name, Media";
143
144       if (mainWin->m_sqlDebug) {
145          Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
146       }
147       QStringList results;
148       int counter = 0;
149       if (m_console->sql_cmd(query, results)) {
150          QStringList fieldlist;
151          QString prev_pool("");
152          QString this_pool("");
153
154          /* Iterate through the lines of results. */
155          foreach (QString resultline, results) {
156             fieldlist = resultline.split("\t");
157             this_pool = fieldlist.takeFirst();
158             if (prev_pool != this_pool) {
159                prev_pool = this_pool;
160                pooltreeitem = new QTreeWidgetItem(m_topItem);
161                pooltreeitem->setText(0, this_pool);
162                pooltreeitem->setData(0, Qt::UserRole, 1);
163             }
164             if(settings.contains(this_pool)) {
165                pooltreeitem->setExpanded(settings.value(this_pool).toBool());
166             } else {
167                pooltreeitem->setExpanded(true);
168             }
169
170             if (fieldlist.size() < 18)
171                continue; // some fields missing, ignore row
172
173             int index = 0;
174             TreeItemFormatter mediaitem(*pooltreeitem, 2);
175   
176             /* Iterate through fields in the record */
177             QStringListIterator fld(fieldlist);
178
179             /* volname */
180             mediaitem.setTextFld(index++, fld.next()); 
181
182             /* id */
183             mediaitem.setNumericFld(index++, fld.next()); 
184
185             /* status */
186             mediaitem.setVolStatusFld(index++, fld.next());
187
188             /* enabled */
189             mediaitem.setBoolFld(index++, fld.next());
190
191             /* bytes */
192             mediaitem.setBytesFld(index++, fld.next());
193
194             /* files */
195             mediaitem.setNumericFld(index++, fld.next()); 
196
197             /* jobs */
198             mediaitem.setNumericFld(index++, fld.next()); 
199
200             /* retention */
201             mediaitem.setDurationFld(index++, fld.next());
202
203             /* media type */
204             mediaitem.setTextFld(index++, fld.next()); 
205
206             /* inchanger + slot */
207             int inchanger = fld.next().toInt();
208             if (inchanger) {
209                mediaitem.setNumericFld(index++, fld.next()); 
210             } else {
211                /* volume not in changer, show blank slot */
212                mediaitem.setNumericFld(index++, ""); 
213                fld.next();
214             }
215
216             /* use duration */
217             mediaitem.setDurationFld(index++, fld.next());
218
219             /* max jobs */
220             mediaitem.setNumericFld(index++, fld.next()); 
221
222             /* max files */
223             mediaitem.setNumericFld(index++, fld.next()); 
224
225             /* max bytes */
226             mediaitem.setBytesFld(index++, fld.next());
227
228             /* recycle */
229             mediaitem.setBoolFld(index++, fld.next());
230
231             /* last written */
232             mediaitem.setTextFld(index++, fld.next()); 
233
234             /* first written */
235             mediaitem.setTextFld(index++, fld.next()); 
236
237             /* read time */
238             mediaitem.setDurationFld(index++, fld.next());
239
240             /* write time */
241             mediaitem.setDurationFld(index++, fld.next());
242
243             /* Recycle Count */
244             mediaitem.setNumericFld(index++, fld.next()); 
245
246             /* recycle pool */
247             mediaitem.setTextFld(index++, fld.next()); 
248
249          } /* foreach resultline */
250          counter += 1;
251       } /* if results from query */
252    } /* foreach pool_listItem */
253    settings.endGroup();
254    /* Resize the columns */
255    for(int cnter=0; cnter<headerlist.count(); cnter++) {
256       mp_treeWidget->resizeColumnToContents(cnter);
257    }
258 }
259
260 /*
261  * Called from the signal of the context sensitive menu!
262  */
263 void MediaList::editVolume()
264 {
265    MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
266    connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
267 }
268
269 /*
270  * Called from the signal of the context sensitive menu!
271  */
272 void MediaList::showJobs()
273 {
274    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
275    mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
276 }
277
278 /*
279  * Called from the signal of the context sensitive menu!
280  */
281 void MediaList::viewVolume()
282 {
283    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
284    MediaInfo* view = new MediaInfo(parentItem, m_currentVolumeName);
285    connect(view, SIGNAL(destroyed()), this, SLOT(populateTree()));
286
287 }
288
289 /*
290  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
291  * The tree has been populated.
292  */
293 void MediaList::PgSeltreeWidgetClicked()
294 {
295    if (!m_populated) {
296       populateTree();
297       createContextMenu();
298    }
299    if (!isOnceDocked()) {
300       dockPage();
301    }
302 }
303
304 /*
305  * Added to set the context menu policy based on currently active treeWidgetItem
306  * signaled by currentItemChanged
307  */
308 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
309 {
310    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
311    if (m_checkcurwidget) {
312       /* The Previous item */
313       if (previouswidgetitem) { /* avoid a segfault if first time */
314          foreach(QAction* mediaAction, mp_treeWidget->actions()) {
315             mp_treeWidget->removeAction(mediaAction);
316          }
317       }
318
319       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
320       m_currentVolumeName=currentwidgetitem->text(0);
321       mp_treeWidget->addAction(actionRefreshMediaList);
322       if (treedepth == 2){
323          m_currentVolumeId=currentwidgetitem->text(1);
324          mp_treeWidget->addAction(actionEditVolume);
325          mp_treeWidget->addAction(actionListJobsOnVolume);
326          mp_treeWidget->addAction(actionDeleteVolume);
327          mp_treeWidget->addAction(actionPruneVolume);
328          mp_treeWidget->addAction(actionPurgeVolume);
329          mp_treeWidget->addAction(actionRelabelVolume);
330          mp_treeWidget->addAction(actionVolumeFromPool);
331       } else if (treedepth == 1) {
332          mp_treeWidget->addAction(actionAllVolumesFromPool);
333       }
334    }
335 }
336
337 /* 
338  * Setup a context menu 
339  * Made separate from populate so that it would not create context menu over and
340  * over as the tree is repopulated.
341  */
342 void MediaList::createContextMenu()
343 {
344    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
345    connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(viewVolume()));
346    connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
347    connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
348    connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
349    connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
350    connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
351    connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
352    connect(mp_treeWidget, SIGNAL(
353            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
354            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
355    /* connect to the action specific to this pages class */
356    connect(actionRefreshMediaList, SIGNAL(triggered()), this,
357                 SLOT(populateTree()));
358    connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
359    connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
360    connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
361 }
362
363 /*
364  * Virtual function which is called when this page is visible on the stack
365  */
366 void MediaList::currentStackItem()
367 {
368    if(!m_populated) {
369       populateTree();
370       /* Create the context menu for the medialist tree */
371       createContextMenu();
372    }
373 }
374
375 /*
376  * Called from the signal of the context sensitive menu to delete a volume!
377  */
378 void MediaList::deleteVolume()
379 {
380    if (QMessageBox::warning(this, "Bat",
381       tr("Are you sure you want to delete??  !!!.\n"
382 "This delete command is used to delete a Volume record and all associated catalog"
383 " records that were created. This command operates only on the Catalog"
384 " database and has no effect on the actual data written to a Volume. This"
385 " command can be dangerous and we strongly recommend that you do not use"
386 " it unless you know what you are doing.  All Jobs and all associated"
387 " records (File and JobMedia) will be deleted from the catalog."
388       "Press OK to proceed with delete operation.?"),
389       QMessageBox::Ok | QMessageBox::Cancel)
390       == QMessageBox::Cancel) { return; }
391
392    QString cmd("delete volume=");
393    cmd += m_currentVolumeName;
394    consoleCommand(cmd);
395 }
396
397 /*
398  * Called from the signal of the context sensitive menu to purge!
399  */
400 void MediaList::purgeVolume()
401 {
402    if (QMessageBox::warning(this, "Bat",
403       tr("Are you sure you want to purge ??  !!!.\n"
404 "The Purge command will delete associated Catalog database records from Jobs and"
405 " Volumes without considering the retention period. Purge  works only on the"
406 " Catalog database and does not affect data written to Volumes. This command can"
407 " be dangerous because you can delete catalog records associated with current"
408 " backups of files, and we recommend that you do not use it unless you know what"
409 " you are doing.\n"
410       "Press OK to proceed with the purge operation?"),
411       QMessageBox::Ok | QMessageBox::Cancel)
412       == QMessageBox::Cancel) { return; }
413
414    QString cmd("purge volume=");
415    cmd += m_currentVolumeName;
416    consoleCommand(cmd);
417    populateTree();
418 }
419
420 /*
421  * Called from the signal of the context sensitive menu to prune!
422  */
423 void MediaList::pruneVolume()
424 {
425    new prunePage(m_currentVolumeName, "");
426 }
427
428 /*
429  * Called from the signal of the context sensitive menu to relabel!
430  */
431 void MediaList::relabelVolume()
432 {
433    setConsoleCurrent();
434    new relabelDialog(m_console, m_currentVolumeName);
435 }
436
437 /*
438  * Called from the signal of the context sensitive menu to purge!
439  */
440 void MediaList::allVolumesFromPool()
441 {
442    QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
443    consoleCommand(cmd);
444    populateTree();
445 }
446
447 void MediaList::allVolumes()
448 {
449    QString cmd = "update volume allfrompools";
450    consoleCommand(cmd);
451    populateTree();
452 }
453
454 /*
455  * Called from the signal of the context sensitive menu to purge!
456  */
457 void MediaList::volumeFromPool()
458 {
459    QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
460    QTreeWidgetItem *parent = currentItem->parent();
461    QString pool = parent->text(0);
462    QString cmd;
463    cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
464    consoleCommand(cmd);
465    populateTree();
466 }
467
468 /*
469  * Write settings to save expanded states of the pools
470  */
471 void MediaList::writeExpandedSettings()
472 {
473    if (m_topItem) {
474       QSettings settings(m_console->m_dir->name(), "bat");
475       settings.beginGroup("MediaListTreeExpanded");
476       int childcount = m_topItem->childCount();
477       for (int cnt=0; cnt<childcount; cnt++) {
478          QTreeWidgetItem *poolitem = m_topItem->child(cnt);
479          settings.setValue(poolitem->text(0), poolitem->isExpanded());
480       }
481       settings.endGroup();
482    }
483 }