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