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