+
+/* 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);
+}
+
+/* For suppressing .messages
+ * This may be rendered not needed if the multiple connections feature gets working */
+bool Console::hasFocus()
+{
+ if (mainWin->tabWidget->currentIndex() == mainWin->tabWidget->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();
+ }
+ m_console->stopTimer();
+}
+
+/* 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;
+ }
+}
+
+int Console::read(int conn)
+{
+ DirComm *dircomm = m_dircommHash.value(conn);
+ return dircomm->read();
+}
+
+char *Console::msg(int conn)
+{
+ DirComm *dircomm = m_dircommHash.value(conn);
+ return dircomm->msg();
+}
+
+int Console::write(int conn, const QString msg)
+{
+ DirComm *dircomm = m_dircommHash.value(conn);
+ mainWin->waitEnter();
+ int ret = dircomm->write(msg);
+ mainWin->waitExit();
+ return ret;
+}
+
+int Console::write(int conn, const char *msg)
+{
+ DirComm *dircomm = m_dircommHash.value(conn);
+ mainWin->waitEnter();
+ int ret = dircomm->write(msg);
+ mainWin->waitExit();
+ return ret;
+}
+
+/* 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 a connection. Check existing connections or create one
+ */
+bool Console::getDirComm(int &conn)
+{
+ if (findDirComm(conn)) {
+ return true;
+ }
+ if (mainWin->m_connDebug) Pmsg0(000, "call newDirComm\n");
+ return newDirComm(conn);
+}
+
+
+/*
+ * Try to find a free (unused but established) connection
+ * KES: Note, I think there is a problem here because for
+ * some reason, the notifier is often turned off on file
+ * descriptors that seem to me to be available. That means
+ * that we do not use a free descriptor and thus we will create
+ * a new connection that is maybe not necessary. Someone needs
+ * to look into whether or not notify() is correctly turned on
+ * when we are back at the command prompt and idle.
+ *
+ */
+bool Console::findDirComm(int &conn)
+{
+ int i = 1;
+ 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()) {
+ conn = dircomm->m_conn;
+ return true;
+ }
+ if (mainWin->m_connDebug) {
+ Pmsg4(000, "currentDirComm=%d at_prompt=%d at_main=%d && notify=%d\n",
+ i, dircomm->m_at_prompt, dircomm->m_at_main_prompt, dircomm->is_notify_enabled());
+ i++;
+ }
+ ++iter;
+ }
+ return false;
+}
+
+/*
+ * Create a new connection
+ */
+bool Console::newDirComm(int &conn)
+{
+ m_dircommCounter++;
+ if (mainWin->m_connDebug) {
+ Pmsg2(000, "newDirComm=%i to: %s\n", m_dircommCounter, m_dir->name());
+ }
+ DirComm *dircomm = new DirComm(this, m_dircommCounter);
+ m_dircommHash.insert(m_dircommCounter, dircomm);
+ bool success = dircomm->connect_dir();
+ if (mainWin->m_connDebug) {
+ if (success) {
+ Pmsg2(000, "newDirComm=%i Connected %s\n", m_dircommCounter, m_dir->name());
+ } else {
+ Emsg2(M_ERROR, 0, "DirComm=%i. Unable to connect to %s\n",
+ m_dircommCounter, m_dir->name());
+ }
+ }
+ if (!success) {
+ m_dircommHash.remove(m_dircommCounter);
+ delete dircomm;
+ m_dircommCounter--;
+ }
+ conn = m_dircommCounter;
+ return success;
+}