]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/console/console.cpp
Attempt to correct timing problems with starting bat and obtaining
[bacula/bacula] / bacula / src / qt-console / console / console.cpp
index b0bde34df22f1b80cdcf558e37aec1eb4a9e5b4d..f17a735857e5bd27cffb46615b263d2da3eba872 100644 (file)
@@ -1,14 +1,14 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
    modify it under the terms of version two of the GNU General Public
-   License as published by the Free Software Foundation plus additions
-   that are listed in the file LICENSE.
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
 
    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,7 +20,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula® is a registered trademark of John Walker.
+   Bacula® is a registered trademark of Kern Sibbald.
    The licensor of Bacula is the Free Software Foundation Europe
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
  *
  */ 
 
-#include <QAbstractEventDispatcher>
 #include "bat.h"
 #include "console.h"
+#include "restore.h"
+#include "select.h"
+#include "run/run.h"
 
 Console::Console(QStackedWidget *parent)
 {
    QFont font;
-   QTreeWidgetItem *item, *topItem;
-   QTreeWidget *treeWidget = mainWin->treeWidget;
+   m_messages_pending = false;
+   m_parent = parent;
+   m_closeable = false;
+   m_console = this;
+   m_dircommCounter = 0;
+   m_dircommHash.insert(m_dircommCounter, new DirComm(this, m_dircommCounter));
 
    setupUi(this);
-   parent->addWidget(this);
-   m_sock = NULL;
-   m_at_prompt = false;
    m_textEdit = textEdit;   /* our console screen */
    m_cursor = new QTextCursor(m_textEdit->document());
-   mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
-
-   bRestore *brestore = new bRestore(parent);
-   brestore->setupUi(brestore);
-   parent->addWidget(brestore);
-
-   /* Just take the first Director */
-   LockRes();
-   m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
-   UnlockRes();
-
-   /* ***FIXME*** Dummy setup of treeWidget */
-   treeWidget->clear();
-   treeWidget->setColumnCount(1);
-   treeWidget->setHeaderLabel("Selection");
-   topItem = new QTreeWidgetItem(treeWidget);
-   topItem->setText(0, m_dir->name());
-   topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
-   item = new QTreeWidgetItem(topItem);
-   m_consoleItem = item;
-   item->setText(0, "Console");
-   item->setText(1, "0");
-   QBrush redBrush(Qt::red);
-   item->setForeground(0, redBrush);
-   item = new QTreeWidgetItem(topItem);
-   item->setText(0, "brestore");
-   item->setText(1, "1");
-   treeWidget->expandItem(topItem);
-
-   readSettings();
-   /* Check for messages every 5 seconds */
-// m_timer = new QTimer(this);
-// QWidget::connect(m_timer, SIGNAL(timeout()), this, SLOT(poll_messages()));
-// m_timer->start(5000);
+   mainWin->actionConnect->setIcon(QIcon(":images/disconnected.png"));
+
+   m_timer = NULL;
+   m_contextActions.append(actionStatusDir);
+   m_contextActions.append(actionConsoleHelp);
+   m_contextActions.append(actionRequestMessages);
+   m_contextActions.append(actionConsoleReload);
+   connect(actionStatusDir, SIGNAL(triggered()), this, SLOT(status_dir()));
+   connect(actionConsoleHelp, SIGNAL(triggered()), this, SLOT(consoleHelp()));
+   connect(actionConsoleReload, SIGNAL(triggered()), this, SLOT(consoleReload()));
+   connect(actionRequestMessages, SIGNAL(triggered()), this, SLOT(messages()));
+}
 
+Console::~Console()
+{
 }
 
-void Console::poll_messages()
+void Console::startTimer()
 {
-   m_messages_pending = true;
+   m_timer = new QTimer(this);
+   QWidget::connect(m_timer, SIGNAL(timeout()), this, SLOT(poll_messages()));
+   m_timer->start(mainWin->m_checkMessagesInterval*1000);
 }
 
-/* Terminate any open socket */
-void Console::terminate()
+void Console::stopTimer()
+{
+   if (m_timer) {
+      QWidget::disconnect(m_timer, SIGNAL(timeout()), this, SLOT(poll_messages()));
+      m_timer->stop();
+      delete m_timer;
+      m_timer = NULL;
+   }
+}
+
+/* slot connected to the timer
+ * requires preferences of check messages and operates at interval */
+void Console::poll_messages()
 {
-   if (m_sock) {
-      m_sock->close();
-      m_sock = NULL;
+   int conn = availableDirComm();
+   DirComm *dircomm = m_dircommHash.value(conn);
+
+   if (mainWin->m_checkMessages && dircomm->m_at_main_prompt && hasFocus()){
+      messagesPending(true);
+      dircomm->write(".messages");
+      displayToPrompt(conn);
+      messagesPending(false);
    }
-   m_timer->stop();
 }
 
 /*
- * Connect to Director. If there are more than one, put up
- * a modal dialog so that the user chooses one.
+ * Connect to Director. 
  */
-void Console::connect()
+void Console::connect_dir()
 {
-   JCR jcr;
-
-   m_textEdit = textEdit;   /* our console screen */
+   DirComm *dircomm = m_dircommHash.value(0);
 
-   if (!m_dir) {          
-      mainWin->set_status("No Director found.");
-      return;
-   }
-   if (m_sock) {
-      mainWin->set_status("Already connected.");
+   if (!m_console->m_dir) {
+      mainWin->set_status( tr("No Director found."));
       return;
    }
 
-   memset(&jcr, 0, sizeof(jcr));
-
-   mainWin->set_statusf(_("Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport);
-   display_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport);
+   m_textEdit = textEdit;   /* our console screen */
 
-   /* Give GUI a chance */
-   app->processEvents();
+   if (dircomm->connect_dir()) {
+      if (mainWin->m_connDebug)
+         Pmsg0(000, "DirComm 0 Seems to have Connected\n");
+      beginNewCommand(0);
+      job_list.clear();
+      client_list.clear();
+      fileset_list.clear();
+      fileset_list.clear();
+      messages_list.clear();
+      pool_list.clear();
+      storage_list.clear();
+      type_list.clear();
+      level_list.clear();
+      int jl = 0;
+      bool done = false;
+      /* this was added because I was getting job lists with 0 count, but not consistently*/
+      while (!done) {
+        dir_cmd(".jobs", job_list);
+        jl++;
+        if ((jl > 10) || job_list.count() > 0) done = true;
+      }
+      dir_cmd(".clients", client_list);
+      dir_cmd(".filesets", fileset_list);  
+      dir_cmd(".msgs", messages_list);
+      dir_cmd(".pools", pool_list);
+      dir_cmd(".storage", storage_list);
+      dir_cmd(".types", type_list);
+      dir_cmd(".levels", level_list);
+
+      if (mainWin->m_connDebug) {
+         QString dbgmsg = QString("jobs=%1 clients=%2 filesets=%3 msgs=%4 pools=%5 storage=%6 types=%7 levels=%8\n")
+           .arg(job_list.count()).arg(client_list.count()).arg(fileset_list.count()).arg(messages_list.count())
+           .arg(pool_list.count()).arg(storage_list.count()).arg(type_list.count()).arg(level_list.count());
+         Pmsg1(000, "%s\n", dbgmsg.toUtf8().data());
+      }
    
-   LockRes();
-   /* If cons==NULL, default console will be used */
-   CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
-   UnlockRes();
-
-   m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address,
-                          NULL, m_dir->DIRport, 0);
-   if (m_sock == NULL) {
-      mainWin->set_status("Connection failed");
-      return;
-   } else {
-      /* Update page selector to green to indicate that Console is connected */
-      mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/connected.png")));
-      QBrush greenBrush(Qt::green);
-      m_consoleItem->setForeground(0, greenBrush);
+      mainWin->set_status(_("Connected"));
+      startTimer();                      /* start message timer */
    }
+}
 
-   jcr.dir_bsock = m_sock;
+bool Console::dir_cmd(QString &cmd, QStringList &results)
+{
+   return dir_cmd(cmd.toUtf8().data(), results);
+}
 
-   if (!authenticate_director(&jcr, m_dir, cons)) {
-      display_text(m_sock->msg);
-      return;
+/*
+ * Send a command to the Director, and return the
+ *  results in a QStringList.  
+ */
+bool Console::dir_cmd(const char *cmd, QStringList &results)
+{
+   int conn = availableDirComm();
+   int stat;
+   DirComm *dircomm = m_dircommHash.value(conn);
+
+   if (mainWin->m_connDebug)
+      Pmsg2(000, "dir_cmd conn %i %s\n", conn, cmd);
+   notify(conn, false);
+   dircomm->write(cmd);
+   while ((stat = dircomm->read()) > 0 && dircomm->is_in_command()) {
+      if (mainWin->m_displayAll) display_text(dircomm->msg());
+      strip_trailing_junk(dircomm->msg());
+      results << dircomm->msg();
    }
+   if (stat > 0 && mainWin->m_displayAll) display_text(dircomm->msg());
+   notify(conn, true);
+   discardToPrompt(conn);
+   return true;              /* ***FIXME*** return any command error */
+}
 
-   /* Give GUI a chance */
-   app->processEvents();
-
-   mainWin->set_status(_("Initializing ..."));
-
-   /* Set up input notifier */
-   m_notifier = new QSocketNotifier(m_sock->fd, QSocketNotifier::Read, 0);
-   QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(read_dir(int)));
+bool Console::sql_cmd(QString &query, QStringList &results)
+{
+   return sql_cmd(query.toUtf8().data(), results);
+}
 
-   write(".api");
-   discardToPrompt();
+/*
+ * Send an sql query to the Director, and return the
+ *  results in a QStringList.  
+ */
+bool Console::sql_cmd(const char *query, QStringList &results)
+{
+   int conn = availableDirComm();
+   DirComm *dircomm = m_dircommHash.value(conn);
+   int stat;
+   POOL_MEM cmd(PM_MESSAGE);
 
-   beginNewCommand();
-   job_list = get_list(".jobs");
-   client_list = get_list(".clients");
-   fileset_list = get_list(".filesets");
-   messages_list = get_list(".messages");
-   pool_list = get_list(".pools");
-   storage_list = get_list(".storage");
-   type_list = get_list(".types");
-   level_list = get_list(".levels");
+   if (!is_connectedGui()) {
+      return false;
+   }
 
-   mainWin->set_status(_("Connected"));
-   return;
+   if (mainWin->m_connDebug)
+      Pmsg2(000, "sql_cmd conn %i %s\n", conn, query);
+   notify(conn, false);
+   
+   pm_strcpy(cmd, ".sql query=\"");
+   pm_strcat(cmd, query);
+   pm_strcat(cmd, "\"");
+   dircomm->write(cmd.c_str());
+   while ((stat = dircomm->read()) > 0) {
+      bool first = true;
+      if (mainWin->m_displayAll) {
+         display_text(dircomm->msg());
+         display_text("\n");
+      }
+      strip_trailing_junk(dircomm->msg());
+      bool doappend = true;
+      if (first) {
+         QString dum = dircomm->msg();
+         if ((dum.left(6) == "*None*")) doappend = false;
+      }
+      if (doappend)
+         results << dircomm->msg();
+      first = false;
+   }
+   notify(conn, true);
+   discardToPrompt(conn);
+   return true;              /* ***FIXME*** return any command error */
 }
 
+/* Send a command to the Director */
+int Console::write_dir(const char *msg)
+{
+   int conn = availableDirComm();
+   write_dir(conn, msg);
+   return conn;
+}
 
-/*  
- * Send a command to the director, and read all the resulting
- *  output into a list.
- */
-QStringList Console::get_list(char *cmd)
+/* Send a command to the Director */
+void Console::write_dir(int conn, const char *msg)
 {
-   QStringList list;
-   int stat;
+   DirComm *dircomm = m_dircommHash.value(conn);
 
-   setEnabled(false);
-   write(cmd);
-   while ((stat = read()) > 0) {
-      strip_trailing_junk(msg());
-      list << msg();
+   if (dircomm->m_sock) {
+      mainWin->set_status(_("Processing command ..."));
+      QApplication::setOverrideCursor(Qt::WaitCursor);
+      dircomm->write(msg);
+   } else {
+      mainWin->set_status( tr(" Director not connected. Click on connect button."));
+      mainWin->actionConnect->setIcon(QIcon(":images/disconnected.png"));
+      QBrush redBrush(Qt::red);
+      QTreeWidgetItem *item = mainWin->getFromHash(this);
+      item->setForeground(0, redBrush);
+      dircomm->m_at_prompt = false;
+      dircomm->m_at_main_prompt = false;
    }
-   setEnabled(true);
-// list.sort();
-   return list;
 }
 
 /*  
@@ -207,16 +271,19 @@ QStringList Console::get_list(char *cmd)
 bool Console::get_job_defaults(struct job_defaults &job_defs)
 {
    QString scmd;
-   char cmd[1000];
    int stat;
    char *def;
 
-   setEnabled(false);
-   beginNewCommand();
+   int conn = notifyOff();
+   beginNewCommand(conn);
+   DirComm *dircomm = m_dircommHash.value(conn);
+   if (mainWin->m_connDebug)
+      Pmsg1(000, "job_defaults conn %i\n", conn);
    scmd = QString(".defaults job=\"%1\"").arg(job_defs.job_name);
-   write(scmd);
-   while ((stat = read()) > 0) {
-      def = strchr(msg(), '=');
+   dircomm->write(scmd);
+   while ((stat = dircomm->read()) > 0) {
+      if (mainWin->m_displayAll) display_text(dircomm->msg());
+      def = strchr(dircomm->msg(), '=');
       if (!def) {
          continue;
       }
@@ -224,68 +291,59 @@ bool Console::get_job_defaults(struct job_defaults &job_defs)
       *def++ = 0;
       strip_trailing_junk(def);
 
-      if (strcmp(msg(), "job") == 0) {
+      if (strcmp(dircomm->msg(), "job") == 0) {
          if (strcmp(def, job_defs.job_name.toUtf8().data()) != 0) {
             goto bail_out;
          }
          continue;
       }
-      if (strcmp(msg(), "pool") == 0) {
+      if (strcmp(dircomm->msg(), "pool") == 0) {
          job_defs.pool_name = def;
          continue;
       }
-      if (strcmp(msg(), "messages") == 0) {
+      if (strcmp(dircomm->msg(), "messages") == 0) {
          job_defs.messages_name = def;
          continue;
       }
-      if (strcmp(msg(), "client") == 0) {
+      if (strcmp(dircomm->msg(), "client") == 0) {
          job_defs.client_name = def;
          continue;
       }
-      if (strcmp(msg(), "storage") == 0) {
+      if (strcmp(dircomm->msg(), "storage") == 0) {
          job_defs.store_name = def;
          continue;
       }
-      if (strcmp(msg(), "where") == 0) {
+      if (strcmp(dircomm->msg(), "where") == 0) {
          job_defs.where = def;
          continue;
       }
-      if (strcmp(msg(), "level") == 0) {
+      if (strcmp(dircomm->msg(), "level") == 0) {
          job_defs.level = def;
          continue;
       }
-      if (strcmp(msg(), "type") == 0) {
+      if (strcmp(dircomm->msg(), "type") == 0) {
          job_defs.type = def;
          continue;
       }
-      if (strcmp(msg(), "fileset") == 0) {
+      if (strcmp(dircomm->msg(), "fileset") == 0) {
          job_defs.fileset_name = def;
          continue;
       }
-      if (strcmp(msg(), "catalog") == 0) {
+      if (strcmp(dircomm->msg(), "catalog") == 0) {
          job_defs.catalog_name = def;
          continue;
       }
-      if (strcmp(msg(), "enabled") == 0) {
+      if (strcmp(dircomm->msg(), "enabled") == 0) {
          job_defs.enabled = *def == '1' ? true : false;
          continue;
       }
    }
-   bsnprintf(cmd, sizeof(cmd), "job=%s pool=%s client=%s storage=%s where=%s\n"
-      "level=%s type=%s fileset=%s catalog=%s enabled=%d\n",
-      job_defs.job_name.toUtf8().data(), job_defs.pool_name.toUtf8().data(), 
-      job_defs.client_name.toUtf8().data(), 
-      job_defs.pool_name.toUtf8().data(), job_defs.messages_name.toUtf8().data(), 
-      job_defs.store_name.toUtf8().data(),
-      job_defs.where.toUtf8().data(), job_defs.level.toUtf8().data(), 
-      job_defs.type.toUtf8().data(), job_defs.fileset_name.toUtf8().data(),
-      job_defs.catalog_name.toUtf8().data(), job_defs.enabled);
-
-   setEnabled(true);
+
+   notify(conn, true);
    return true;
 
 bail_out:
-   setEnabled(true);
+   notify(conn, true);
    return false;
 }
 
@@ -297,8 +355,7 @@ void Console::writeSettings()
 {
    QFont font = get_font();
 
-   QSettings settings("bacula.org", "bat");
-   /* ***FIXME*** make console name unique */
+   QSettings settings(m_dir->name(), "bat");
    settings.beginGroup("Console");
    settings.setValue("consoleFont", font.family());
    settings.setValue("consolePointSize", font.pointSize());
@@ -313,7 +370,7 @@ void Console::readSettings()
 { 
    QFont font = get_font();
 
-   QSettings settings("bacula.org", "bat");
+   QSettings settings(m_dir->name(), "bat");
    settings.beginGroup("Console");
    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
    font.setPointSize(settings.value("consolePointSize", 10).toInt());
@@ -342,11 +399,24 @@ const QFont Console::get_font()
    return m_textEdit->font();
 }
 
-
+/*
+ * Slot for responding to status dir button on button bar
+ */
 void Console::status_dir()
 {
-   write_dir("status dir\n");
-   displayToPrompt();
+   QString cmd("status dir");
+   consoleCommand(cmd);
+}
+
+/*
+ * Slot for responding to messages button on button bar
+ * Here we want to bring the console to the front so use pages' consoleCommand
+ */
+void Console::messages()
+{
+   QString cmd(".messages");
+   consoleCommand(cmd);
+   messagesPending(false);
 }
 
 /*
@@ -365,15 +435,21 @@ void Console::display_textf(const char *fmt, ...)
 
 void Console::display_text(const QString buf)
 {
-   m_cursor->movePosition(QTextCursor::End);
    m_cursor->insertText(buf);
+   update_cursor();
 }
 
 
 void Console::display_text(const char *buf)
 {
-   m_cursor->movePosition(QTextCursor::End);
    m_cursor->insertText(buf);
+   update_cursor();
+}
+
+void Console::display_html(const QString buf)
+{
+   m_cursor->insertHtml(buf);
+   update_cursor();
 }
 
 /* Position cursor to end of screen */
@@ -384,178 +460,275 @@ void Console::update_cursor()
    m_textEdit->ensureCursorVisible();
 }
 
-/* 
- * This should be moved into a bSocket class 
- */
-char *Console::msg()
+void Console::beginNewCommand(int conn)
 {
-   if (m_sock) {
-      return m_sock->msg;
+   DirComm *dircomm = m_dircommHash.value(conn);
+
+   for (int i=0; i < 3; i++) {
+      dircomm->write(".");
+      while (dircomm->read() > 0) {
+         if (mainWin->m_displayAll) display_text(dircomm->msg());
+      }
+      if (dircomm->m_at_main_prompt) {
+         break;
+      }
+   }
+   display_text("\n");
+}
+
+void Console::displayToPrompt(int conn)
+{ 
+   DirComm *dircomm = m_dircommHash.value(conn);
+
+   int stat = 0;
+   QString buf;
+   if (mainWin->m_commDebug) Pmsg0(000, "DisplaytoPrompt\n");
+   while (!dircomm->m_at_prompt) {
+      if ((stat=dircomm->read()) > 0) {
+         buf += dircomm->msg();
+         if (buf.size() >= 8196 || m_messages_pending) {
+            display_text(buf);
+            buf.clear();
+            messagesPending(false);
+         }
+      }
    }
-   return NULL;
+   display_text(buf);
+   if (mainWin->m_commDebug) Pmsg1(000, "endDisplaytoPrompt=%d\n", stat);
 }
 
-/* Send a command to the Director */
-void Console::write_dir(const char *msg)
+void Console::discardToPrompt(int conn)
 {
-   if (m_sock) {
-      mainWin->set_status(_("Processing command ..."));
-      QApplication::setOverrideCursor(Qt::WaitCursor);
-      write(msg);
+   DirComm *dircomm = m_dircommHash.value(conn);
+
+   int stat = 0;
+   if (mainWin->m_commDebug) Pmsg0(000, "discardToPrompt\n");
+   if (mainWin->m_displayAll) {
+      displayToPrompt(conn);
    } else {
-      mainWin->set_status(" Director not connected. Click on connect button.");
-      mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
-      QBrush redBrush(Qt::red);
-      m_consoleItem->setForeground(0, redBrush);
+      while (!dircomm->m_at_prompt) {
+         stat=dircomm->read();
+      }
    }
+   if (mainWin->m_commDebug) Pmsg1(000, "endDiscardToPrompt=%d\n", stat);
+}
+
+/*
+ * When the notifier is enabled, read_dir() will automatically be
+ * called by the Qt event loop when ever there is any output 
+ * from the Directory, and read_dir() will then display it on
+ * the console.
+ *
+ * When we are in a bat dialog, we want to control *all* output
+ * from the Directory, so we set notify to off.
+ *    m_console->notifiy(false);
+ */
+
+/* dual purpose function to turn notify off and return an available connection */
+int Console::notifyOff()
+{ 
+   int conn = availableDirComm();
+   notify(conn, false);
+   return conn;
 }
 
-int Console::write(const QString msg)
+/* knowing a connection, turn notify off or on */
+bool Console::notify(int conn, bool enable)
+{ 
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->notify(enable);
+}
+
+/* knowing a connection, return notify state */
+bool Console::is_notify_enabled(int conn) const
 {
-   return write(msg.toUtf8().data());
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->is_notify_enabled();
 }
 
-int Console::write(const char *msg)
+void Console::setDirectorTreeItem(QTreeWidgetItem *item)
 {
-   m_sock->msglen = strlen(msg);
-   pm_strcpy(&m_sock->msg, msg);
-   m_at_prompt = false;
-   if (commDebug) Pmsg1(000, "send: %s\n", msg);
-   return m_sock->send();
+   m_directorTreeItem = item;
+}
+
+void Console::setDirRes(DIRRES *dir) 
+{ 
+   m_dir = dir;
 }
 
 /*
- * Get to main command prompt 
+ * To have the ability to get the name of the director resource.
  */
-void Console::beginNewCommand()
+void Console::getDirResName(QString &name_returned)
 {
-   write(".\n");
-   while (read() > 0) {
-   }
-   write(".\n");
-   while (read() > 0) {
+   name_returned = m_dir->name();
+}
+
+/* Slot for responding to page selectors status help command */
+void Console::consoleHelp()
+{
+   QString cmd("help");
+   consoleCommand(cmd);
+}
+
+/* Slot for responding to page selectors reload bacula-dir.conf */
+void Console::consoleReload()
+{
+   QString cmd("reload");
+   consoleCommand(cmd);
+}
+
+/* Function to get a list of volumes */
+void Console::getVolumeList(QStringList &volumeList)
+{
+   QString query("SELECT VolumeName AS Media FROM Media ORDER BY Media");
+   if (mainWin->m_sqlDebug) {
+      Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
    }
-   write(".\n");
-   while (read() > 0) {
+   QStringList results;
+   if (sql_cmd(query, results)) {
+      QString field;
+      QStringList fieldlist;
+      /* Iterate through the lines of results. */
+      foreach (QString resultline, results) {
+         fieldlist = resultline.split("\t");
+         volumeList.append(fieldlist[0]);
+      } /* foreach resultline */
+   } /* if results from query */
+}
+
+/* Function to get a list of volumes */
+void Console::getStatusList(QStringList &statusLongList)
+{
+   QString statusQuery("SELECT JobStatusLong FROM Status");
+   if (mainWin->m_sqlDebug) {
+      Pmsg1(000, "Query cmd : %s\n",statusQuery.toUtf8().data());
    }
-   display_text("\n");
+   QStringList statusResults;
+   if (sql_cmd(statusQuery, statusResults)) {
+      QString field;
+      QStringList fieldlist;
+      /* Iterate through the lines of results. */
+      foreach (QString resultline, statusResults) {
+         fieldlist = resultline.split("\t");
+         statusLongList.append(fieldlist[0]);
+      } /* foreach resultline */
+   } /* if results from statusquery */
 }
 
-void Console::displayToPrompt()
-{ 
-   int stat;
-   if (commDebug) Pmsg0(000, "DisplaytoPrompt\n");
-   m_notifier->setEnabled(false);
-   while ((stat = read()) > 0) {
-      display_text(msg());
+/* For suppressing .messages
+ * This may be rendered not needed if the multiple connections feature gets working */
+bool Console::hasFocus()
+{
+   if (mainWin->stackedWidget->currentIndex() == mainWin->stackedWidget->indexOf(this))
+      return true;
+   else
+      return false;
+}
+
+/* For adding feature to have the gui's messages button change when 
+ * messages are pending */
+bool Console::messagesPending(bool pend)
+{
+   bool prev = m_messages_pending;
+   m_messages_pending = pend;
+   mainWin->setMessageIcon();
+   return prev;
+}
+
+/* terminate all existing connections */
+void Console::terminate()
+{
+   foreach(DirComm* dircomm,  m_dircommHash) {
+      dircomm->terminate();
    }
-   if (commDebug) Pmsg1(000, "endDisplaytoPrompt=%d\n", stat);
-   m_notifier->setEnabled(true);
+   m_console->stopTimer();
 }
 
-void Console::discardToPrompt()
-{ 
-   int stat;
-   if (commDebug) Pmsg0(000, "discardToPrompt\n");
-   m_notifier->setEnabled(false);
-   while ((stat = read()) > 0) {
+/* Maybe this should be checking the list, for the moment lets check 0 which should be connected */
+bool Console::is_connectedGui()
+{
+   if (is_connected(0)) {
+      return true;
+   } else {
+      QString message = tr("Director is currently disconnected\nPlease reconnect!");
+      QMessageBox::warning(this, "Bat", message, QMessageBox::Ok );
+      return false;
    }
-   if (commDebug) Pmsg1(000, "endDisplayToPrompt=%d\n", stat);
-   m_notifier->setEnabled(true);
 }
 
+int Console::read(int conn)
+{
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->read();
+}
 
-/* 
- * Blocking read from director
- */
-int Console::read()
-{
-   int stat = BNET_HARDEOF;
-   while (m_sock) {
-      for (;;) {
-         stat = bnet_wait_data_intr(m_sock, 1);
-         if (stat > 0) {
-            break;
-         } 
-         app->processEvents();
-//       if (m_api_set && m_messages_pending) {
-//          write_dir(".messages");
-//          m_messages_pending = false;
-//       }
-      }
-      stat = m_sock->recv();
-      if (stat >= 0) {
-         if (m_at_prompt) {
-            display_text("\n");
-            m_at_prompt = false;
-         }
-         if (commDebug) Pmsg1(000, "got: %s", m_sock->msg);
+char *Console::msg(int conn)
+{
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->msg();
+}
 
-      }
-      switch (m_sock->msglen) {
-      case BNET_SERVER_READY:
-//       if (m_api_set && m_messages_pending) {
-//          write_dir(".messages");
-//          m_messages_pending = false;
-//       }
-         m_at_prompt = true;
-         continue;
-      case BNET_MESSAGES_PENDING:
-         m_messages_pending = true;
-         continue;
-      case BNET_CMD_BEGIN:
-         m_at_prompt = false;
-         continue;
-      case BNET_PROMPT:
-      case BNET_CMD_OK:
-         if (commDebug) Pmsg0(000, "CMD OK/PROMPT\n");
-         m_at_prompt = true;
-         mainWin->set_status(_("At prompt waiting for input ..."));
-         update_cursor();
-         QApplication::restoreOverrideCursor();
-         break;
-      case BNET_CMD_FAILED:
-         if (commDebug) Pmsg0(000, "CMD FAIL\n");
-         m_at_prompt = true;
-         mainWin->set_status(_("Command failed. At prompt waiting for input ..."));
-         update_cursor();
-         QApplication::restoreOverrideCursor();
-         break;
-      case BNET_EOD:
-         if (commDebug) Pmsg0(000, "EOD\n");
-         mainWin->set_status_ready();
-         update_cursor();
-         QApplication::restoreOverrideCursor();
-         if (!m_api_set) {
-            break;
-         }
-         continue;
-      }
-      if (is_bnet_stop(m_sock)) {         /* error or term request */
-         m_sock->close();
-         m_sock = NULL;
-         mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
-         QBrush redBrush(Qt::red);
-         m_consoleItem->setForeground(0, redBrush);
-         m_notifier->setEnabled(false);
-         delete m_notifier;
-         m_notifier = NULL;
-         mainWin->set_status(_("Director disconnected."));
-         QApplication::restoreOverrideCursor();
-      }
-      break;
-   } 
-   return stat;
+int Console::write(int conn, const QString msg)
+{
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->write(msg);
 }
 
-/* Called by signal when the Director has output for us */
-void Console::read_dir(int fd)
+int Console::write(int conn, const char *msg)
 {
-   int stat;
-   (void)fd;
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->write(msg);
+}
 
-   if (commDebug) Pmsg0(000, "read_dir\n");
-   while ((stat = read()) >= 0) {
-      display_text(msg());
+/* This checks to see if any is connected */
+bool Console::is_connected()
+{
+   bool connected = false;
+   foreach(DirComm* dircomm,  m_dircommHash) {
+      if (dircomm->is_connected())
+         return true;
    }
+   return connected;
+}
+
+/* knowing the connection id, is it connected */
+bool Console::is_connected(int conn)
+{
+   DirComm *dircomm = m_dircommHash.value(conn);
+   return dircomm->is_connected();
+}
+
+/*
+ * Need an available connection.  Check existing connections or create one
+ */
+int Console::availableDirComm()
+{
+   QHash<int, DirComm*>::const_iterator iter = m_dircommHash.constBegin();
+   while (iter != m_dircommHash.constEnd()) {
+      DirComm *dircomm = iter.value();
+      if (dircomm->m_at_prompt && dircomm->m_at_main_prompt && dircomm->is_notify_enabled())
+         return dircomm->m_conn;
+      ++iter;
+   }
+   return newDirComm();
+}
+
+/*
+ *  Create a new connection
+ */
+int Console::newDirComm()
+{
+   m_dircommCounter += 1;
+   if (mainWin->m_connDebug)
+      Pmsg1(000, "DirComm %i About to Create and Connect\n", m_dircommCounter);
+   DirComm *dircomm = new DirComm(this, m_dircommCounter);
+   m_dircommHash.insert(m_dircommCounter, dircomm);
+   bool success = dircomm->connect_dir();
+   if (mainWin->m_connDebug)
+      if (success)
+         Pmsg1(000, "DirComm %i Connected\n", m_dircommCounter);
+      else
+         Pmsg1(000, "DirComm %i NOT Connected\n", m_dircommCounter);
+   return m_dircommCounter;
 }