From 493e98885e072d133ec2dbd563a8bd0da4eaa169 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Mon, 29 Jan 2007 10:34:57 +0000 Subject: [PATCH] Basic console text display now works git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4072 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/qt-console/Makefile | 20 ++-- bacula/src/qt-console/bat.pro | 11 +- .../src/qt-console/console/authenticate.cpp | 110 ++++++++++++++++++ bacula/src/qt-console/console/console.cpp | 75 ++++++------ bacula/src/qt-console/console/console.h | 9 +- bacula/src/qt-console/main.cpp | 3 - bacula/src/qt-console/main.ui | 16 ++- bacula/src/qt-console/mainwin.cpp | 45 +++---- bacula/src/qt-console/mainwin.h | 3 + 9 files changed, 202 insertions(+), 90 deletions(-) create mode 100644 bacula/src/qt-console/console/authenticate.cpp diff --git a/bacula/src/qt-console/Makefile b/bacula/src/qt-console/Makefile index b7b1bbcf3d..0a9d204bc7 100644 --- a/bacula/src/qt-console/Makefile +++ b/bacula/src/qt-console/Makefile @@ -1,6 +1,6 @@ ############################################################################# # Makefile for building: bat -# Generated by qmake (2.01a) (Qt 4.2.1) on: Sun Jan 28 22:55:01 2007 +# Generated by qmake (2.01a) (Qt 4.2.1) on: Mon Jan 29 10:12:52 2007 # Project: bat.pro # Template: app # Command: /usr/bin/qmake -unix -o Makefile bat.pro @@ -46,16 +46,16 @@ OBJECTS_DIR = ./ ####### Files SOURCES = main.cpp \ - authenticate.cpp \ bat_conf.cpp \ mainwin.cpp \ + console/authenticate.cpp \ console/console.cpp moc_mainwin.cpp \ moc_console.cpp \ qrc_main.cpp OBJECTS = main.o \ - authenticate.o \ bat_conf.o \ mainwin.o \ + authenticate.o \ console.o \ moc_mainwin.o \ moc_console.o \ @@ -150,7 +150,7 @@ qmake: FORCE dist: @$(CHK_DIR_EXISTS) .tmp/bat1.0.0 || $(MKDIR) .tmp/bat1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/bat1.0.0/ && $(COPY_FILE) --parents mainwin.h bat.h bat_conf.h console/console.h .tmp/bat1.0.0/ && $(COPY_FILE) --parents main.qrc .tmp/bat1.0.0/ && $(COPY_FILE) --parents main.cpp authenticate.cpp bat_conf.cpp mainwin.cpp console/console.cpp .tmp/bat1.0.0/ && $(COPY_FILE) --parents main.ui .tmp/bat1.0.0/ && (cd `dirname .tmp/bat1.0.0` && $(TAR) bat1.0.0.tar bat1.0.0 && $(COMPRESS) bat1.0.0.tar) && $(MOVE) `dirname .tmp/bat1.0.0`/bat1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/bat1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/bat1.0.0/ && $(COPY_FILE) --parents mainwin.h bat.h bat_conf.h console/console.h .tmp/bat1.0.0/ && $(COPY_FILE) --parents main.qrc .tmp/bat1.0.0/ && $(COPY_FILE) --parents main.cpp bat_conf.cpp mainwin.cpp console/authenticate.cpp console/console.cpp .tmp/bat1.0.0/ && $(COPY_FILE) --parents main.ui .tmp/bat1.0.0/ && (cd `dirname .tmp/bat1.0.0` && $(TAR) bat1.0.0.tar bat1.0.0 && $(COMPRESS) bat1.0.0.tar) && $(MOVE) `dirname .tmp/bat1.0.0`/bat1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/bat1.0.0 yaccclean: @@ -227,12 +227,6 @@ main.o: main.cpp bat.h \ bat_conf.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp -authenticate.o: authenticate.cpp bat.h \ - mainwin.h \ - ui_main.h \ - bat_conf.h - $(CXX) -c $(CXXFLAGS) $(INCPATH) -o authenticate.o authenticate.cpp - bat_conf.o: bat_conf.cpp bat_conf.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o bat_conf.o bat_conf.cpp @@ -242,6 +236,12 @@ mainwin.o: mainwin.cpp bat.h \ bat_conf.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o mainwin.o mainwin.cpp +authenticate.o: console/authenticate.cpp bat.h \ + mainwin.h \ + ui_main.h \ + bat_conf.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o authenticate.o console/authenticate.cpp + console.o: console/console.cpp bat.h \ mainwin.h \ ui_main.h \ diff --git a/bacula/src/qt-console/bat.pro b/bacula/src/qt-console/bat.pro index 1d537f90fa..8cc59c89f3 100644 --- a/bacula/src/qt-console/bat.pro +++ b/bacula/src/qt-console/bat.pro @@ -12,8 +12,11 @@ LIBS += -lbac LIBS += -lssl -lcrypto RESOURCES = main.qrc -# Input +# Main directory FORMS += main.ui -HEADERS += mainwin.h bat.h bat_conf.h console/console.h -SOURCES += main.cpp authenticate.cpp bat_conf.cpp -SOURCES += mainwin.cpp console/console.cpp +HEADERS += mainwin.h bat.h bat_conf.h +SOURCES += main.cpp bat_conf.cpp mainwin.cpp + +# Console +HEADERS += console/console.h +SOURCES += console/authenticate.cpp console/console.cpp diff --git a/bacula/src/qt-console/console/authenticate.cpp b/bacula/src/qt-console/console/authenticate.cpp new file mode 100644 index 0000000000..d1ff85be97 --- /dev/null +++ b/bacula/src/qt-console/console/authenticate.cpp @@ -0,0 +1,110 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2001-2007 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. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of John Walker. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ + +/* + * + * Bacula UA authentication. Provides authentication with + * the Director. + * + * Kern Sibbald, June MMI adapted to bat, Jan MMVI + * + * Version $Id$ + * + */ + + +#include "bat.h" + + +/* Commands sent to Director */ +static char hello[] = "Hello %s calling\n"; + +/* Response from Director */ +static char OKhello[] = "1000 OK:"; + +/* Forward referenced functions */ + +/* + * Authenticate Director + */ +bool Console::authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons) +{ + BSOCK *dir = jcr->dir_bsock; + int tls_local_need = BNET_TLS_NONE; + int tls_remote_need = BNET_TLS_NONE; + int compatible = true; + char bashed_name[MAX_NAME_LENGTH]; + char *password; + + /* + * Send my name to the Director then do authentication + */ + if (cons) { + bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name)); + bash_spaces(bashed_name); + password = cons->password; + } else { + bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name)); + password = director->password; + } + /* Timeout Hello after 5 mins */ + btimer_t *tid = start_bsock_timer(dir, 60 * 5); + bnet_fsend(dir, hello, bashed_name); + + /* respond to Dir challenge */ + if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) || + /* Now challenge dir */ + !cram_md5_challenge(dir, password, tls_local_need, compatible)) { + stop_bsock_timer(tid); + printf(_("%s: Director authorization problem.\n"), my_name); + set_text(_("Director authorization problem.\n")); + set_text(_( + "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n")); + return false; + } + + Dmsg1(6, ">dird: %s", dir->msg); + if (bnet_recv(dir) <= 0) { + stop_bsock_timer(tid); + set_textf(_("Bad response to Hello command: ERR=%s\n"), + bnet_strerror(dir)); + printf(_("%s: Bad response to Hello command: ERR=%s\n"), + my_name, bnet_strerror(dir)); + set_text(_("The Director is probably not running.\n")); + return false; + } + stop_bsock_timer(tid); + Dmsg1(10, "msg); + if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) { + set_text(_("Director rejected Hello command\n")); + return false; + } else { + set_text(dir->msg); + } + return true; +} diff --git a/bacula/src/qt-console/console/console.cpp b/bacula/src/qt-console/console/console.cpp index d68ae1f880..ba6fa7d002 100644 --- a/bacula/src/qt-console/console/console.cpp +++ b/bacula/src/qt-console/console/console.cpp @@ -39,12 +39,20 @@ Console::Console() { + QFont font; QTreeWidgetItem *item, *topItem; QTreeWidget *treeWidget = mainWin->treeWidget; m_sock = NULL; m_at_prompt = false; m_textEdit = mainWin->textEdit; /* our console screen */ + m_cursor = new QTextCursor(m_textEdit->document()); + + /* ***FIXME*** make this configurable */ + font.setFamily("Courier"); + font.setFixedPitch(true); + font.setPointSize(10); + m_textEdit->setFont(font); /* Just take the first Director */ LockRes(); @@ -77,17 +85,17 @@ void Console::connect() m_textEdit = mainWin->textEdit; /* our console screen */ if (!m_dir) { - set_text("No Director to connect to.\n"); + mainWin->set_status("No Director found."); return; } if (m_sock) { - set_text("Already connected.\n"); + mainWin->set_status("Already connected."); return; } memset(&jcr, 0, sizeof(jcr)); - set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport); + mainWin->set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport); set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport); /* Give GUI a chance */ @@ -101,7 +109,7 @@ void Console::connect() m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address, NULL, m_dir->DIRport, 0); if (m_sock == NULL) { - set_text("Connection failed\n"); + mainWin->set_status("Connection failed"); return; } @@ -115,7 +123,7 @@ void Console::connect() /* Give GUI a chance */ app->processEvents(); - set_status(_(" Initializing ...")); + mainWin->set_status(_(" Initializing ...")); bnet_fsend(m_sock, "autodisplay on"); @@ -131,9 +139,7 @@ void Console::connect() * .pools, .storage, .types, .levels, ... */ - set_status(_(" Connected")); - set_text("Connected\n"); - + mainWin->set_status(_(" Connected")); return; } #ifdef xxx @@ -152,36 +158,24 @@ void Console::set_textf(const char *fmt, ...) va_start(arg_ptr, fmt); len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr); va_end(arg_ptr); - m_textEdit->append(buf); -} - -void Console::set_text(const char *buf) -{ - m_textEdit->append(buf); + set_text(buf); } -void Console::set_statusf(const char *fmt, ...) +void Console::set_text(const QString buf) { - va_list arg_ptr; - char buf[1000]; - int len; - va_start(arg_ptr, fmt); - len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr); - va_end(arg_ptr); - set_status(buf); + m_cursor->movePosition(QTextCursor::End); + m_cursor->insertText(buf); + m_textEdit->moveCursor(QTextCursor::End); + m_textEdit->ensureCursorVisible(); } -void Console::set_status_ready() -{ - mainWin->statusBar()->showMessage("Ready"); -// ready = true; -} -void Console::set_status(const char *buf) +void Console::set_text(const char *buf) { - mainWin->statusBar()->showMessage(buf); -// set_text(buf); -// ready = false; + m_cursor->movePosition(QTextCursor::End); + m_cursor->insertText(buf); + m_textEdit->moveCursor(QTextCursor::End); + m_textEdit->ensureCursorVisible(); } @@ -189,13 +183,13 @@ void Console::write_dir(const char *msg) { if (m_sock) { m_at_prompt = false; - set_status(_(" Processing command ...")); + mainWin->set_status(_(" Processing command ...")); + QApplication::setOverrideCursor(Qt::WaitCursor); m_sock->msglen = strlen(msg); pm_strcpy(&m_sock->msg, msg); bnet_send(m_sock); - } - if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) { - app->closeAllWindows(); + } else { + mainWin->set_status(" Director not connected. Click on connect button."); } } @@ -219,15 +213,22 @@ void Console::read_dir(int fd) if (is_bnet_stop(m_sock)) { /* error or term request */ bnet_close(m_sock); m_sock = NULL; + m_notifier->setEnabled(false); + delete m_notifier; + m_notifier = NULL; + mainWin->set_status(_(" Director disconnected.")); + QApplication::restoreOverrideCursor(); return; } /* Must be a signal -- either do something or ignore it */ if (m_sock->msglen == BNET_PROMPT) { m_at_prompt = true; - set_status(_(" At prompt waiting for input ...")); + mainWin->set_status(_(" At prompt waiting for input ...")); + QApplication::restoreOverrideCursor(); } if (m_sock->msglen == BNET_EOD) { - set_status_ready(); + mainWin->set_status_ready(); + QApplication::restoreOverrideCursor(); } return; } diff --git a/bacula/src/qt-console/console/console.h b/bacula/src/qt-console/console/console.h index cd2340cbe3..394f33c1bb 100644 --- a/bacula/src/qt-console/console/console.h +++ b/bacula/src/qt-console/console/console.h @@ -16,11 +16,11 @@ class Console : public QWidget public: Console(); void set_text(const char *buf); + void set_text(const QString buf); void set_textf(const char *fmt, ...); - void set_statusf(const char *fmt, ...); - void set_status_ready(); - void set_status(const char *buf); void write_dir(const char *buf); + bool authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons); + bool is_connected() { return m_sock != NULL; }; public slots: void connect(); @@ -32,8 +32,7 @@ private: BSOCK *m_sock; bool m_at_prompt; QSocketNotifier *m_notifier; + QTextCursor *m_cursor; }; -extern int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons); - #endif /* _CONSOLE_H_ */ diff --git a/bacula/src/qt-console/main.cpp b/bacula/src/qt-console/main.cpp index 502d303cbf..cc99377062 100644 --- a/bacula/src/qt-console/main.cpp +++ b/bacula/src/qt-console/main.cpp @@ -38,9 +38,6 @@ #include "bat.h" -/* Imported functions */ -int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons); - /* Forward referenced functions */ void terminate_console(int sig); static void usage(); diff --git a/bacula/src/qt-console/main.ui b/bacula/src/qt-console/main.ui index 26895f12bc..bf78925309 100644 --- a/bacula/src/qt-console/main.ui +++ b/bacula/src/qt-console/main.ui @@ -6,7 +6,7 @@ 0 0 - 889 + 1038 802 @@ -80,6 +80,9 @@ 0 + + Selects right panel + true @@ -136,6 +139,12 @@ 0 + + Qt::NoFocus + + + false + @@ -148,6 +157,9 @@ + + QTextEdit::NoWrap + true @@ -212,7 +224,7 @@ 0 0 - 889 + 1038 33 diff --git a/bacula/src/qt-console/mainwin.cpp b/bacula/src/qt-console/mainwin.cpp index 7df2cb222b..ebe975d103 100644 --- a/bacula/src/qt-console/mainwin.cpp +++ b/bacula/src/qt-console/mainwin.cpp @@ -41,13 +41,12 @@ MainWin::MainWin(QWidget *parent) : QMainWindow(parent) setupUi(this); /* Setup UI defined by main.ui (designer) */ stackedWidget->setCurrentIndex(0); + statusBar()->showMessage("Director not connected. Click on connect button."); + m_console = new Console(); - /* Dummy message ***FIXME*** remove a bit later */ - textEdit->setPlainText("Hello Baculites\nThis is the main console window."); lineEdit->setFocus(); - /* Connect signals to slots */ connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line())); connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about())); @@ -80,9 +79,15 @@ void MainWin::input_line() { QString cmdStr = lineEdit->text(); /* Get the text */ lineEdit->clear(); /* clear the lineEdit box */ - textEdit->append(cmdStr); /* append text on screen */ - m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */ + if (m_console->is_connected()) { + m_console->set_text(cmdStr + "\n"); + m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */ + } else { + set_status("Director not connected. Click on connect button."); + } } + + void MainWin::about() { QMessageBox::about(this, tr("About bat"), @@ -92,23 +97,7 @@ void MainWin::about() " interface to the Director.")); } -void set_textf(const char *fmt, ...) -{ - va_list arg_ptr; - char buf[1000]; - int len; - va_start(arg_ptr, fmt); - len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr); - va_end(arg_ptr); - mainWin->textEdit->append(buf); -} - -void set_text(const char *buf) -{ - mainWin->textEdit->append(buf); -} - -void set_statusf(const char *fmt, ...) +void MainWin::set_statusf(const char *fmt, ...) { va_list arg_ptr; char buf[1000]; @@ -116,20 +105,18 @@ void set_statusf(const char *fmt, ...) va_start(arg_ptr, fmt); len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr); va_end(arg_ptr); -// gtk_label_set_text(GTK_LABEL(status1), buf); + set_status(buf); // set_scroll_bar_to_end(); -// ready = false; } -void set_status_ready() +void MainWin::set_status_ready() { - mainWin->statusBar()->showMessage("Ready"); -// ready = true; + set_status("Ready"); // set_scroll_bar_to_end(); } -void set_status(const char *buf) +void MainWin::set_status(const char *buf) { - mainWin->statusBar()->showMessage(buf); + statusBar()->showMessage(buf); // ready = false; } diff --git a/bacula/src/qt-console/mainwin.h b/bacula/src/qt-console/mainwin.h index 6176d2058a..67d7b81acf 100644 --- a/bacula/src/qt-console/mainwin.h +++ b/bacula/src/qt-console/mainwin.h @@ -46,6 +46,9 @@ class MainWin : public QMainWindow, public Ui::MainForm public: MainWin(QWidget *parent = 0); + void set_statusf(const char *fmt, ...); + void set_status_ready(); + void set_status(const char *buf); public slots: void input_line(); -- 2.39.5