]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/medialist/medialist.cpp
Add translation strings
[bacula/bacula] / bacula / src / qt-console / medialist / medialist.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 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$
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 = tr("Media");
51    pgInitialize();
52    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
53    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
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    dockPage();
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 *mediatreeitem, *pooltreeitem;
78
79    if (!m_console->preventInUseConnect())
80        return;
81
82    QStringList headerlist = (QStringList()
83       << tr("Volume Name") << tr("Id") << tr("Status") << tr("Enabled") << tr("Bytes") << tr("Files")
84       << tr("Jobs") << tr("Retention") << tr("Media Type") << tr("Slot") << tr("Use Duration")
85       << tr("Max Jobs") << tr("Max Files") << tr("Max Bytes") << tr("Recycle") << tr("Enabled")
86       << tr("RecyclePool") << tr("Last Written"));
87    int statusIndex = headerlist.indexOf("Status");
88    QStringList flaglist = (QStringList()
89       << "L" << "R" << "L" << "R" << "BR" << "R"
90       << "R" << "RS" << "L" << "R" << "RS"
91       << "R" << "R" << "BR" << "R" << "R"
92       << "L" << "L");
93
94    m_checkcurwidget = false;
95    if (m_populated)
96       writeExpandedSettings();
97    mp_treeWidget->clear();
98    m_checkcurwidget = true;
99    mp_treeWidget->setColumnCount(headerlist.count());
100    m_topItem = new QTreeWidgetItem(mp_treeWidget);
101    m_topItem->setText(0, tr("Pools"));
102    m_topItem->setData(0, Qt::UserRole, 0);
103    m_topItem->setExpanded(true);
104    
105    mp_treeWidget->setHeaderLabels(headerlist);
106
107    QSettings settings(m_console->m_dir->name(), "bat");
108    settings.beginGroup("MediaListTreeExpanded");
109    QString query;
110
111    foreach (QString pool_listItem, m_console->pool_list) {
112       pooltreeitem = new QTreeWidgetItem(m_topItem);
113       pooltreeitem->setText(0, pool_listItem);
114       pooltreeitem->setData(0, Qt::UserRole, 1);
115       if(settings.contains(pool_listItem)) {
116          pooltreeitem->setExpanded(settings.value(pool_listItem).toBool());
117       } else {
118          pooltreeitem->setExpanded(true);
119       }
120
121       query =  "SELECT 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.Slot AS Slot, Media.VolUseDuration AS UseDuration,"
127          " Media.MaxVolJobs AS MaxJobs, Media.MaxVolFiles AS MaxFiles,"
128          " Media.MaxVolBytes AS MaxBytes, Media.Recycle AS Recycle,"
129          " Media.Enabled AS enabled, Pol.Name AS RecyclePool,"
130          " Media.LastWritten AS LastWritten"
131          " FROM Media"
132          " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
133          " LEFT OUTER JOIN Pool AS Pol ON (Media.RecyclePoolId=Pol.PoolId)"
134          " WHERE";
135       query += " Pool.Name='" + pool_listItem + "'";
136       query += " ORDER BY Media";
137    
138       if (mainWin->m_sqlDebug) {
139          Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
140       }
141       QStringList results;
142       if (m_console->sql_cmd(query, results)) {
143          QString field;
144          QStringList fieldlist;
145
146          /* Iterate through the lines of results. */
147          foreach (QString resultline, results) {
148             fieldlist = resultline.split("\t");
149             int index = 0;
150             mediatreeitem = new QTreeWidgetItem(pooltreeitem);
151
152             /* Iterate through fields in the record */
153             foreach (field, fieldlist) {
154                field = field.trimmed();  /* strip leading & trailing spaces */
155                if (field != "") {
156                   mediatreeitem->setData(index, Qt::UserRole, 2);
157                   mediatreeitem->setData(index, Qt::UserRole, 2);
158                   mediatreeitem->setText(index, field);
159                   if (index == statusIndex) {
160                      setStatusColor(mediatreeitem, field, index);
161                   } 
162                   if (flaglist[index].contains("B")) {
163                      QString text;
164                      bool okay;
165                      qlonglong bytes = field.toULongLong(&okay);
166                      if (okay){
167                         QString test =  QString("%1").arg(bytes);
168                         mainWin->hrConvert(text, bytes);
169                         mediatreeitem->setText(index, text);
170                      } else { Pmsg1(000, "conversion error %s\n", field.toUtf8().data()); }
171                   }
172                   if (flaglist[index].contains("S")) {
173                      QString text;
174                      bool okay;
175                      qlonglong seconds = field.toULongLong(&okay);
176                      if (okay){
177                         QString test =  QString("%1").arg(seconds);
178                         mainWin->hrConvertSeconds(text, seconds);
179                         mediatreeitem->setText(index, text);
180                      } else { Pmsg1(000, "conversion error %s\n", field.toUtf8().data()); }
181                   }
182                   if (flaglist[index].contains("R"))
183                      mediatreeitem->setTextAlignment(index, Qt::AlignRight);
184                }
185                index++;
186             } /* foreach field */
187          } /* foreach resultline */
188       } /* if results from query */
189    } /* foreach pool_listItem */
190    settings.endGroup();
191    /* Resize the columns */
192    for(int cnter=0; cnter<headerlist.count(); cnter++) {
193       mp_treeWidget->resizeColumnToContents(cnter);
194    }
195 }
196
197 void MediaList::setStatusColor(QTreeWidgetItem *item, QString &field, int &index)
198 {
199    if (field == "Append" ) {
200       item->setBackground(index, Qt::green);
201    } else if (field == "Error") {
202       item->setBackground(index, Qt::red);
203    } else if ((field == "Used") || ("Full")){
204       item->setBackground(index, Qt::yellow);
205    }
206 }
207
208 /*
209  * Called from the signal of the context sensitive menu!
210  */
211 void MediaList::editVolume()
212 {
213    MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
214    connect(edit, SIGNAL(destroyed()), this, SLOT(populateTree()));
215 }
216
217 /*
218  * Called from the signal of the context sensitive menu!
219  */
220 void MediaList::showJobs()
221 {
222    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
223    mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
224 }
225
226 /*
227  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
228  * The tree has been populated.
229  */
230 void MediaList::PgSeltreeWidgetClicked()
231 {
232    if (!m_populated) {
233       populateTree();
234       createContextMenu();
235       m_populated=true;
236    }
237 }
238
239 /*
240  * Added to set the context menu policy based on currently active treeWidgetItem
241  * signaled by currentItemChanged
242  */
243 void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
244 {
245    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
246    if (m_checkcurwidget) {
247       /* The Previous item */
248       if (previouswidgetitem) { /* avoid a segfault if first time */
249          foreach(QAction* mediaAction, mp_treeWidget->actions()) {
250             mp_treeWidget->removeAction(mediaAction);
251          }
252       }
253
254       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
255       m_currentVolumeName=currentwidgetitem->text(0);
256       mp_treeWidget->addAction(actionRefreshMediaList);
257       if (treedepth == 2){
258          m_currentVolumeId=currentwidgetitem->text(1);
259          mp_treeWidget->addAction(actionEditVolume);
260          mp_treeWidget->addAction(actionListJobsOnVolume);
261          mp_treeWidget->addAction(actionDeleteVolume);
262          mp_treeWidget->addAction(actionPruneVolume);
263          mp_treeWidget->addAction(actionPurgeVolume);
264          mp_treeWidget->addAction(actionRelabelVolume);
265          mp_treeWidget->addAction(actionVolumeFromPool);
266       } else if (treedepth == 1) {
267          mp_treeWidget->addAction(actionAllVolumesFromPool);
268       }
269    }
270 }
271
272 /* 
273  * Setup a context menu 
274  * Made separate from populate so that it would not create context menu over and
275  * over as the tree is repopulated.
276  */
277 void MediaList::createContextMenu()
278 {
279    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
280    connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editVolume()));
281    connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
282    connect(actionDeleteVolume, SIGNAL(triggered()), this, SLOT(deleteVolume()));
283    connect(actionPurgeVolume, SIGNAL(triggered()), this, SLOT(purgeVolume()));
284    connect(actionPruneVolume, SIGNAL(triggered()), this, SLOT(pruneVolume()));
285    connect(actionRelabelVolume, SIGNAL(triggered()), this, SLOT(relabelVolume()));
286    connect(mp_treeWidget, SIGNAL(
287            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
288            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
289    /* connect to the action specific to this pages class */
290    connect(actionRefreshMediaList, SIGNAL(triggered()), this,
291                 SLOT(populateTree()));
292    connect(actionAllVolumes, SIGNAL(triggered()), this, SLOT(allVolumes()));
293    connect(actionAllVolumesFromPool, SIGNAL(triggered()), this, SLOT(allVolumesFromPool()));
294    connect(actionVolumeFromPool, SIGNAL(triggered()), this, SLOT(volumeFromPool()));
295 }
296
297 /*
298  * Virtual function which is called when this page is visible on the stack
299  */
300 void MediaList::currentStackItem()
301 {
302    if(!m_populated) {
303       populateTree();
304       /* Create the context menu for the medialist tree */
305       createContextMenu();
306       m_populated=true;
307    }
308 }
309
310 /*
311  * Called from the signal of the context sensitive menu to delete a volume!
312  */
313 void MediaList::deleteVolume()
314 {
315    if (QMessageBox::warning(this, "Bat",
316       tr("Are you sure you want to delete??  !!!.\n"
317 "This delete command is used to delete a Volume record and all associated catalog"
318 " records that were created. This command operates only on the Catalog"
319 " database and has no effect on the actual data written to a Volume. This"
320 " command can be dangerous and we strongly recommend that you do not use"
321 " it unless you know what you are doing.  All Jobs and all associated"
322 " records (File and JobMedia) will be deleted from the catalog."
323       "Press OK to proceed with delete operation.?"),
324       QMessageBox::Ok | QMessageBox::Cancel)
325       == QMessageBox::Cancel) { return; }
326
327    QString cmd("delete volume=");
328    cmd += m_currentVolumeName;
329    consoleCommand(cmd);
330 }
331
332 /*
333  * Called from the signal of the context sensitive menu to purge!
334  */
335 void MediaList::purgeVolume()
336 {
337    if (QMessageBox::warning(this, "Bat",
338       tr("Are you sure you want to purge ??  !!!.\n"
339 "The Purge command will delete associated Catalog database records from Jobs and"
340 " Volumes without considering the retention period. Purge  works only on the"
341 " Catalog database and does not affect data written to Volumes. This command can"
342 " be dangerous because you can delete catalog records associated with current"
343 " backups of files, and we recommend that you do not use it unless you know what"
344 " you are doing.\n"
345       "Press OK to proceed with the purge operation?"),
346       QMessageBox::Ok | QMessageBox::Cancel)
347       == QMessageBox::Cancel) { return; }
348
349    QString cmd("purge volume=");
350    cmd += m_currentVolumeName;
351    consoleCommand(cmd);
352    populateTree();
353 }
354
355 /*
356  * Called from the signal of the context sensitive menu to prune!
357  */
358 void MediaList::pruneVolume()
359 {
360    new prunePage(m_currentVolumeName, "");
361 }
362
363 /*
364  * Called from the signal of the context sensitive menu to relabel!
365  */
366 void MediaList::relabelVolume()
367 {
368    setConsoleCurrent();
369    new relabelDialog(m_console, m_currentVolumeName);
370 }
371
372 /*
373  * Called from the signal of the context sensitive menu to purge!
374  */
375 void MediaList::allVolumesFromPool()
376 {
377    QString cmd = "update volume AllFromPool=" + m_currentVolumeName;
378    consoleCommand(cmd);
379    populateTree();
380 }
381
382 void MediaList::allVolumes()
383 {
384    QString cmd = "update volume allfrompools";
385    consoleCommand(cmd);
386    populateTree();
387 }
388
389 /*
390  * Called from the signal of the context sensitive menu to purge!
391  */
392 void MediaList::volumeFromPool()
393 {
394    QTreeWidgetItem *currentItem = mp_treeWidget->currentItem();
395    QTreeWidgetItem *parent = currentItem->parent();
396    QString pool = parent->text(0);
397    QString cmd;
398    cmd = "update volume=" + m_currentVolumeName + " frompool=" + pool;
399    consoleCommand(cmd);
400    populateTree();
401 }
402
403 /*
404  * Write settings to save expanded states of the pools
405  */
406 void MediaList::writeExpandedSettings()
407 {
408    QSettings settings(m_console->m_dir->name(), "bat");
409    settings.beginGroup("MediaListTreeExpanded");
410    int childcount = m_topItem->childCount();
411    for (int cnt=0; cnt<childcount; cnt++) {
412       QTreeWidgetItem *poolitem = m_topItem->child(cnt);
413       settings.setValue(poolitem->text(0), poolitem->isExpanded());
414    }
415    settings.endGroup();
416 }