*
* MediaList Class
*
- * Kern Sibbald, January MMVI
+ * Dirk Bartley, March 2007
*
*/
#include <QAbstractEventDispatcher>
+#include <QMenu>
#include "bat.h"
#include "medialist.h"
#include "mediaedit/mediaedit.h"
#include "joblist/joblist.h"
-#include <QMenu>
-//#include <QSize>
MediaList::MediaList(QStackedWidget *parent, Console *console)
{
setupUi(this);
- m_parent=parent;
-// AddTostack();
- m_poollist = new QStringList();
- m_treeWidget = treeWidget; /* our medialist screen */
- m_console = console;
+ /* mp_treeWidget, Storage Tree Tree Widget inherited from ui_medialist.h */
+ mp_console = console;
+ m_parent = parent;
createConnections();
- m_popupmedia="";
+ m_populated = false;
+ m_checkcurwidget = true;
+ m_closeable = false;
+}
+
+MediaList::~MediaList()
+{
}
+/*
+ * The main meat of the class!! The function that querries the director and
+ * creates the widgets with appropriate values.
+ */
void MediaList::populateTree()
{
- int stat;
- QTreeWidgetItem *mediatreeitem, *treeitem, *topItem;
+ QTreeWidgetItem *mediatreeitem, *pooltreeitem, *topItem;
+ QString currentpool("");
+ QString resultline;
+ QStringList results;
+ const char *query =
+ "SELECT p.Name,m.VolumeName,m.MediaId,m.VolStatus,m.Enabled,m.VolBytes,"
+ "m.VolFiles,m.VolJobs,m.VolRetention,m.MediaType,m.LastWritten"
+ " FROM Media m,Pool p"
+ " WHERE m.PoolId=p.PoolId"
+ " ORDER BY p.Name";
+ QStringList headerlist = (QStringList()
+ << "Pool Name" << "Volume Name" << "Media Id" << "Volume Status" << "Enabled"
+ << "Volume Bytes" << "Volume Files" << "Volume Jobs" << "Volume Retention"
+ << "Media Type" << "Last Written");
- m_treeWidget->clear();
- m_treeWidget->setColumnCount(3);
- topItem = new QTreeWidgetItem(m_treeWidget);
+ m_checkcurwidget = false;
+ mp_treeWidget->clear();
+ m_checkcurwidget = true;
+ mp_treeWidget->setColumnCount(headerlist.count());
+ topItem = new QTreeWidgetItem(mp_treeWidget);
topItem->setText(0, "Pools");
topItem->setData(0, Qt::UserRole, 0);
- topItem->setExpanded( true );
- //topItem->setSizeHint(0,QSize(1050,50));
-
- /* Start with a list of pools */
- m_poollist->clear();
- QString *scmd = new QString(".pools\n");
- m_console->write_dir(scmd->toUtf8().data());
- while ((stat=m_console->read()) > 0) {
- m_poollist->append(m_console->msg());
- }
- for ( QStringList::Iterator poolitem = m_poollist->begin(); poolitem != m_poollist->end(); ++poolitem ) {
- treeitem = new QTreeWidgetItem(topItem);
- m_console->setTreeItem(treeitem);
- poolitem->replace(QRegExp("\n"), "");
- treeitem->setText(0, poolitem->toUtf8().data());
- treeitem->setData(0, Qt::UserRole, 1);
- treeitem->setExpanded( true );
-
- /* iterate through the media in the pool */
- QString *mcmd = new QString("sqlquery\n");
- m_console->write_dir(mcmd->toUtf8().data());
- while ((stat=m_console->read()) > 0) { }
- QString *mcmd2 = new QString("select m.volumename, m.mediaid, m.mediatype from media m, pool p where p.name = '");
- mcmd2->append(*poolitem);
- mcmd2->append("';\n");
- m_console->write_dir(mcmd2->toUtf8().data());
- QString *mediaread = new QString();
- while ((stat=m_console->read()) > 0) {
- *mediaread += m_console->msg();
- }
- m_console->discardToPrompt();
- QStringList sqllinelist = mediaread->split("\n");
- /* a regex todetermine if mediareadline is a line of interest. */
- QRegExp regex("^\\|.*\\|$");
- int recordcnter=0;
- QStringList *headerlist = new QStringList();
- /* Iterate through lines retuned */
- for ( QStringList::Iterator mediareadline = sqllinelist.begin(); mediareadline != sqllinelist.end(); ++mediareadline ) {
- if ( regex.indexIn(*mediareadline) >= 0 ){
- QStringList recorditemlist = mediareadline->split("|");
- int recorditemcnter=0;
- /* Iterate through items in the record */
- for ( QStringList::Iterator mediarecorditem = recorditemlist.begin(); mediarecorditem != recorditemlist.end(); ++mediarecorditem ) {
- QString trimmeditem = mediarecorditem->trimmed();
- if( trimmeditem != "" ){
- if ( recordcnter == 0 ){
- headerlist->append(trimmeditem);
- } else {
- if ( recorditemcnter == 0 ){
- mediatreeitem = new QTreeWidgetItem(treeitem);
- }
- mediatreeitem->setData(recorditemcnter, Qt::UserRole, 2);
- mediatreeitem->setText(recorditemcnter, trimmeditem.toUtf8().data());
- }
- recorditemcnter+=1;
- }
- }
- recordcnter+=1;
- }
+ topItem->setExpanded(true);
+
+ mp_treeWidget->setHeaderLabels(headerlist);
+
+ //printf("MediaList query cmd : %s\n",query);
+ if (mp_console->sql_cmd(query, results)) {
+ QString field;
+ QStringList fieldlist;
+
+ foreach (resultline, results) {
+ fieldlist = resultline.split("\t");
+ int index = 0;
+ /* Iterate through fields in the record */
+ foreach (field, fieldlist) {
+ field = field.trimmed(); /* strip leading & trailing spaces */
+ if (field != "") {
+ /* The first field is the pool name */
+ if (index == 0) {
+ /* If new pool name, create new Pool item */
+ if (currentpool != field) {
+ currentpool = field;
+ pooltreeitem = new QTreeWidgetItem(topItem);
+ pooltreeitem->setText(0, field);
+ pooltreeitem->setData(0, Qt::UserRole, 1);
+ pooltreeitem->setExpanded(true);
+ }
+ mediatreeitem = new QTreeWidgetItem(pooltreeitem);
+ mediatreeitem->setData(index, Qt::UserRole, 2);
+ } else {
+ /* Put media fields under the pool tree item */
+ mediatreeitem->setData(index, Qt::UserRole, 2);
+ mediatreeitem->setText(index, field);
+ }
+ }
+ index++;
+ }
}
- m_treeWidget->setHeaderLabels(*headerlist);
}
}
+/*
+ * Not being used currently, Should this be kept for possible future use.
+ */
void MediaList::createConnections()
{
- connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
- SLOT(treeItemClicked(QTreeWidgetItem *, int)));
- connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
- SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
+ connect(mp_treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
+ SLOT(treeItemClicked(QTreeWidgetItem *, int)));
+ connect(mp_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
+ SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
}
-void MediaList::treeItemClicked(QTreeWidgetItem *item, int column)
+/*
+ * Not being used currently, Should this be kept for possible future use.
+ */
+void MediaList::treeItemClicked(QTreeWidgetItem * /*item*/, int /*column*/)
{
- int treedepth = item->data(column, Qt::UserRole).toInt();
- QString text = item->text(0);
- switch (treedepth){
- case 1:
- break;
- case 2:
- /* Can't figure out how to make a right button do this --- Qt::LeftButton, Qt::RightButton, Qt::MidButton */
- m_popupmedia = text;
- QMenu *popup = new QMenu( m_treeWidget );
- connect(popup->addAction("Edit Properties"), SIGNAL(triggered()), this, SLOT(editMedia()));
- connect(popup->addAction("Show Jobs On Media"), SIGNAL(triggered()), this, SLOT(showJobs()));
- popup->exec(QCursor::pos());
- }
}
-void MediaList::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
+/*
+ * Not being used currently, Should this be kept for possible future use.
+ */
+void MediaList::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
{
- int treedepth = item->data(column, Qt::UserRole).toInt();
- if (treedepth >= 0) {
- }
}
+/*
+ * Called from the signal of the context sensitive menu!
+ */
void MediaList::editMedia()
{
- MediaEdit* edit = new MediaEdit(m_console, m_popupmedia);
+ MediaEdit* edit = new MediaEdit(mp_console, m_currentlyselected);
edit->show();
}
+/*
+ * Called from the signal of the context sensitive menu!
+ */
void MediaList::showJobs()
{
- JobList* joblist = new JobList(m_console, m_popupmedia);
- joblist->show();
+ QString emptyclient("");
+ mainWin->createPageJobList(m_currentlyselected, emptyclient);
+}
+
+/*
+ * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
+ * The tree has been populated.
+ */
+void MediaList::PgSeltreeWidgetClicked()
+{
+ if(!m_populated) {
+ populateTree();
+ createContextMenu();
+ m_populated=true;
+ }
+}
+
+/*
+ * When the treeWidgetItem in the page selector tree is doubleclicked, Use that
+ * As a signal to repopulate from a query of the database.
+ * Should this be from a context sensitive menu in either or both of the page selector
+ * or This widnow ???
+ */
+void MediaList::PgSeltreeWidgetDoubleClicked()
+{
+ populateTree();
+}
+
+/*
+ * Added to set the context menu policy based on currently active treeWidgetItem
+ * signaled by currentItemChanged
+ */
+void MediaList::treeItemChanged(QTreeWidgetItem *currentwidgetitem, QTreeWidgetItem *previouswidgetitem )
+{
+ /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
+ if (m_checkcurwidget) {
+ /* The Previous item */
+ if (previouswidgetitem) { /* avoid a segfault if first time */
+ int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
+ if (treedepth == 2){
+ mp_treeWidget->removeAction(actionEditVolume);
+ mp_treeWidget->removeAction(actionListJobsOnVolume);
+ }
+ }
+
+ int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
+ if (treedepth == 2){
+ m_currentlyselected=currentwidgetitem->text(1);
+ mp_treeWidget->addAction(actionEditVolume);
+ mp_treeWidget->addAction(actionListJobsOnVolume);
+ }
+ }
+}
+
+/*
+ * Setup a context menu
+ * Made separate from populate so that it would not create context menu over and
+ * over as the tree is repopulated.
+ */
+void MediaList::createContextMenu()
+{
+ mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
+ /*mp_treeWidget->setContextMenuPolicy(Qt::NoContextMenu);*/
+ mp_treeWidget->addAction(actionRefreshMediaList);
+ connect(actionEditVolume, SIGNAL(triggered()), this, SLOT(editMedia()));
+ connect(actionListJobsOnVolume, SIGNAL(triggered()), this, SLOT(showJobs()));
+ mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
+ connect(mp_treeWidget, SIGNAL(
+ currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
+ this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
+ /* connect to the action specific to this pages class */
+ connect(actionRefreshMediaList, SIGNAL(triggered()), this,
+ SLOT(populateTree()));
+}
+
+/*
+ * Virtual function which is called when this page is visible on the stack
+ */
+void MediaList::currentStackItem()
+{
+ if(!m_populated) {
+ populateTree();
+ /* add context sensitive menu items specific to this classto the page
+ * selector tree. m_m_contextActions is QList of QActions, so this is
+ * only done once with the first population. */
+ m_contextActions.append(actionRefreshMediaList);
+ /* Create the context menu for the medialist tree */
+ createContextMenu();
+ m_populated=true;
+ }
}