]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/medialist/mediaview.cpp
bat: cleanup MediaView
[bacula/bacula] / bacula / src / qt-console / medialist / mediaview.cpp
index 19d36fe8b3ed9ce0d65b8b21ebd4a8b1a61a38e1..f470a0020739fd9daf22026671c95851175c0329 100644 (file)
@@ -45,6 +45,13 @@ MediaView::MediaView()
    pgInitialize();
    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge.png")));
+   connect(m_pbApply, SIGNAL(pressed()), this, SLOT(applyPushed()));
+   connect(m_pbEdit, SIGNAL(pressed()), this, SLOT(editPushed()));
+   connect(m_pbPurge, SIGNAL(pressed()), this, SLOT(purgePushed()));
+   connect(m_pbDelete, SIGNAL(pressed()), this, SLOT(deletePushed()));
+   connect(m_pbPrune, SIGNAL(pressed()), this, SLOT(prunePushed()));
+   connect(m_tableMedia, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), 
+           this, SLOT(showInfoForMedia(QTableWidgetItem *)));
 
    /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
    m_populated = false;
@@ -52,77 +59,94 @@ MediaView::MediaView()
    m_closeable = false;
 }
 
-MediaView::~MediaView()
+void MediaView::showInfoForMedia(QTableWidgetItem * item)
 {
+   QTreeWidgetItem* pageSelectorTreeWidgetItem = mainWin->getFromHash(this);
+   int row = item->row();
+   QString vol = m_tableMedia->item(row, 0)->text();
+   new MediaInfo(pageSelectorTreeWidgetItem, vol);
+//   connect(j, SIGNAL(destroyed()), this, SLOT(populateTree()));
 }
 
-/*
- * The main meat of the class!!  The function that querries the director and 
- * creates the widgets with appropriate values.
- */
-void MediaView::populateTable()
+MediaView::~MediaView()
 {
-   m_populated = true;
-
-   Freeze frz(*m_tableMedia); /* disable updating*/
-
 }
 
-/*
- * Called from the signal of the context sensitive menu!
- */
-void MediaView::editVolume()
+void MediaView::applyPushed()
 {
-   MediaEdit* edit = new MediaEdit(mainWin->getFromHash(this), m_currentVolumeId);
-   connect(edit, SIGNAL(destroyed()), this, SLOT(populateTable()));
+   populateTable();
 }
 
-/*
- * Called from the signal of the context sensitive menu!
- */
-void MediaView::showJobs()
+void MediaView::editPushed()
 {
-   QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
-   mainWin->createPageJobList(m_currentVolumeName, "", "", "", parentItem);
+   QStringList sel;
+   QString cmd;
+   getSelection(sel);
+   
+   for(int i=0; i<sel.count(); i++) {
+      cmd = sel.at(i);
+      new MediaEdit(mainWin->getFromHash(this), cmd);
+   }
 }
 
-/*
- * Called from the signal of the context sensitive menu!
- */
-void MediaView::viewVolume()
+void MediaView::purgePushed()
 {
-   QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
-   MediaInfo* view = new MediaInfo(parentItem, m_currentVolumeName);
-   connect(view, SIGNAL(destroyed()), this, SLOT(populateTable()));
+   if (QMessageBox::warning(this, "Bat",
+      tr("Are you sure you want to purge ??  !!!.\n"
+"The Purge command will delete associated Catalog database records from Jobs and"
+" Volumes without considering the retention period. Purge  works only on the"
+" Catalog database and does not affect data written to Volumes. This command can"
+" be dangerous because you can delete catalog records associated with current"
+" backups of files, and we recommend that you do not use it unless you know what"
+" you are doing.\n"
+      "Press OK to proceed with the purge operation?"),
+      QMessageBox::Ok | QMessageBox::Cancel)
+      == QMessageBox::Cancel) { return; }
 
+   QStringList lst;
+   QString cmd;
+   getSelection(lst);
+   for(int i=0; i<lst.count(); i++) {
+      cmd = "purge volume=" + lst.at(i);
+      consoleCommand(cmd);
+   }
+   populateTable();
 }
 
-/*
- * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
- * The tree has been populated.
- */
-void MediaView::PgSeltreeWidgetClicked()
+bool MediaView::getSelection(QStringList &list)
 {
-   if (!m_populated) {
-      populateTable();
+   QList<QTableWidgetItem*> items = m_tableMedia->selectedItems();
+   QTableWidgetItem *it;
+   int row;
+   int nb = items.count();
+   int *tab = (int *) malloc (nb * sizeof(int));
+   memset(tab, 0, sizeof(int)*nb);
+   for (int i = 0; i < nb; ++i) {
+      row = items[i]->row();
+      if (!tab[row]) {
+         tab[row]=1;
+         it = m_tableMedia->item(row, 0);
+         list.append(it->text());
+      }
    }
-   dockPage();
+   free(tab);
+   return list.count() > 0;
 }
 
-/*
- * Virtual function which is called when this page is visible on the stack
- */
-void MediaView::currentStackItem()
+void MediaView::prunePushed()
 {
-   if(!m_populated) {
-      populateTable();
+   QStringList sel;
+   QString cmd;
+   getSelection(sel);
+
+   for(int i=0; i<sel.count(); i++) {
+      cmd = "prune volume=" + sel.at(i);
+      consoleCommand(cmd);
    }
 }
 
-/*
- * Called from the signal of the context sensitive menu to delete a volume!
- */
-void MediaView::deleteVolume()
+
+void MediaView::deletePushed()
 {
    if (QMessageBox::warning(this, "Bat",
       tr("Are you sure you want to delete??  !!!.\n"
@@ -136,40 +160,185 @@ void MediaView::deleteVolume()
       QMessageBox::Ok | QMessageBox::Cancel)
       == QMessageBox::Cancel) { return; }
 
-   QString cmd("delete volume=");
-   cmd += m_currentVolumeName;
-   consoleCommand(cmd);
+   QStringList lst;
+   QString cmd;
+   getSelection(lst);
+   for(int i=0; i<lst.count(); i++) {
+      cmd = "delete volume=" + lst.at(i);
+      consoleCommand(cmd);
+   }
+   populateTable();
+}
+
+void MediaView::populateForm()
+{
+   m_cbPool->clear();
+   m_cbPool->addItem("");
+   m_cbPool->addItems(m_console->pool_list);
+
+   m_cbStatus->clear();
+   m_cbStatus->addItem("");
+   m_cbStatus->addItems(m_console->volstatus_list);
+
+   m_cbMediaType->clear();
+   m_cbMediaType->addItem("");
+   m_cbMediaType->addItems(m_console->mediatype_list);
+
+   m_cbLocation->clear();
+   m_cbLocation->addItem("");
+   m_cbLocation->addItems(m_console->location_list);
 }
 
 /*
- * Called from the signal of the context sensitive menu to purge!
+ * The main meat of the class!!  The function that querries the director and 
+ * creates the widgets with appropriate values.
  */
-void MediaView::purgeVolume()
+void MediaView::populateTable()
 {
-   if (QMessageBox::warning(this, "Bat",
-      tr("Are you sure you want to purge ??  !!!.\n"
-"The Purge command will delete associated Catalog database records from Jobs and"
-" Volumes without considering the retention period. Purge  works only on the"
-" Catalog database and does not affect data written to Volumes. This command can"
-" be dangerous because you can delete catalog records associated with current"
-" backups of files, and we recommend that you do not use it unless you know what"
-" you are doing.\n"
-      "Press OK to proceed with the purge operation?"),
-      QMessageBox::Ok | QMessageBox::Cancel)
-      == QMessageBox::Cancel) { return; }
+   utime_t t;
+   time_t ttime;
+   QString stat, LastWritten;
+   char buf[256];
+   struct tm tm;
 
-   QString cmd("purge volume=");
-   cmd += m_currentVolumeName;
-   consoleCommand(cmd);
-   populateTable();
+   m_populated = true;
+
+   Freeze frz(*m_tableMedia); /* disable updating*/
+   QStringList where;
+   QString cmd;
+   if (m_cbPool->currentText() != "") {
+      cmd = " Pool.Name = '" + m_cbPool->currentText() + "'";
+      where.append(cmd);
+   } 
+
+   if (m_cbStatus->currentText() != "") {
+      cmd = " Media.VolStatus = '" + m_cbStatus->currentText() + "'";
+      where.append(cmd);
+   }
+
+   if (m_cbStatus->currentText() != "") {
+      cmd = " Media.VolStatus = '" + m_cbStatus->currentText() + "'";
+      where.append(cmd);
+   }
+
+   if (m_cbMediaType->currentText() != "") {
+      cmd = " Media.MediaType = '" + m_cbMediaType->currentText() + "'";
+      where.append(cmd);
+   }
+
+   if (m_cbLocation->currentText() != "") {
+      cmd = " Location.Location = '" + m_cbLocation->currentText() + "'";
+      where.append(cmd);
+   }
+
+   if (m_textName->text() != "") {
+      cmd = " Media.VolumeName like '%" + m_textName->text() + "%'";
+      where.append(cmd);
+   }
+
+   if (where.size() > 0) {
+      cmd = " WHERE " + where.join(" AND ");
+   } else {
+      cmd = "";
+   }
+
+   m_tableMedia->clearContents();
+
+   QString query = 
+      "SELECT MediaId, VolumeName, InChanger, Slot, VolBytes, VolStatus, "
+      "Pool.Name, "
+      "MediaType, LastWritten,"
+      "Media.VolRetention "
+      "FROM Media JOIN Pool USING (PoolId) "
+      "LEFT JOIN Location USING (LocationId) "
+      + cmd + 
+      " ORDER BY VolumeName LIMIT " + m_sbLimit->cleanText();
+
+//   Pmsg1(000, "MediaView query cmd : %s\n",query.toUtf8().data());
+
+   QStringList results;
+   if (m_console->sql_cmd(query, results)) {
+      QString resultline;
+      QStringList fieldlist;
+      m_tableMedia->setRowCount(results.size());
+      int row=0;
+      foreach (resultline, results) { // should have only one result
+         fieldlist = resultline.split("\t");
+         QStringListIterator fld(fieldlist);
+         int index=0;
+         TableItemFormatter mediaitem(*m_tableMedia, row);
+
+         fld.next();            // MediaId
+         
+         /* VolumeName */
+         mediaitem.setTextFld(index++, fld.next()); 
+         
+         /* Online */
+         mediaitem.setTextFld(index++, fld.next());
+         fld.next();            // Slot
+
+         /* Volume bytes */
+         mediaitem.setBytesFld(index++, fld.next());
+
+         /* Usage */
+         mediaitem.setTextFld(index++, "NYI");
+         
+         /* Volstatus */
+         mediaitem.setVolStatusFld(index++, fld.next());
+
+         /* Pool */
+         mediaitem.setTextFld(index++, fld.next());
+
+         /* MediaType */
+         mediaitem.setTextFld(index++, fld.next());
+
+         /* LastWritten */
+         LastWritten = fld.next();
+         mediaitem.setTextFld(index++, LastWritten);
+
+         stat = fld.next();
+         t = str_to_utime(LastWritten.toAscii().data());
+         t = t + stat.toULongLong();
+         ttime = t;
+         localtime_r(&ttime, &tm);         
+         strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
+
+         mediaitem.setTextFld(index++, buf);
+         
+         row++;
+      }
+   }
+
+   m_tableMedia->resizeColumnsToContents();
+   m_tableMedia->resizeRowsToContents();
+   m_tableMedia->verticalHeader()->hide();
+
+   /* make read only */
+   m_tableMedia->setEditTriggers(QAbstractItemView::NoEditTriggers);
+}
+
+/*
+ * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
+ * The tree has been populated.
+ */
+void MediaView::PgSeltreeWidgetClicked()
+{
+   if (!m_populated) {
+      populateForm();
+      populateTable();
+   }
+   dockPage();
 }
 
 /*
- * Called from the signal of the context sensitive menu to prune!
+ * Virtual function which is called when this page is visible on the stack
  */
-void MediaView::pruneVolume()
+void MediaView::currentStackItem()
 {
-   new prunePage(m_currentVolumeName, "");
+   if(!m_populated) {
+      populateForm();
+      populateTable();
+   }
 }
 
 // /*