From 3ad7bd4bdd5d685574c6c4985c5512ec5db6d4d3 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sat, 27 Jan 2007 20:58:35 +0000 Subject: [PATCH] Start on Console class git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4062 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/qt-console/Makefile | 2 +- bacula/src/qt-console/bat.h | 6 + bacula/src/qt-console/bat_conf.h | 6 +- bacula/src/qt-console/console.cpp | 265 +++++++++++++++++++++++++++ bacula/src/qt-console/console.h | 22 +++ bacula/src/qt-console/main.cpp | 2 + bacula/src/qt-console/mainwindow.cpp | 2 +- bacula/src/qt-console/mainwindow.h | 3 + 8 files changed, 303 insertions(+), 5 deletions(-) create mode 100644 bacula/src/qt-console/console.cpp diff --git a/bacula/src/qt-console/Makefile b/bacula/src/qt-console/Makefile index 856d690b3a..fe961c9a96 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: Sat Jan 27 19:24:48 2007 +# Generated by qmake (2.01a) (Qt 4.2.1) on: Sat Jan 27 21:58:01 2007 # Project: bat.pro # Template: app # Command: /usr/bin/qmake -unix -o Makefile bat.pro diff --git a/bacula/src/qt-console/bat.h b/bacula/src/qt-console/bat.h index 186b81ad62..906d931f9c 100644 --- a/bacula/src/qt-console/bat.h +++ b/bacula/src/qt-console/bat.h @@ -1,4 +1,6 @@ +#ifndef _BAT_H_ + #include "mainwindow.h" #include "config.h" #include "bacula.h" @@ -7,8 +9,12 @@ extern MainWindow *mainWin; extern QApplication *app; +extern BSOCK *UA_sock; + void set_textf(const char *fmt, ...); void set_text(const char *buf); int bvsnprintf(char *str, int32_t size, const char *format, va_list ap); + +#endif /* _BAT_H_ */ diff --git a/bacula/src/qt-console/bat_conf.h b/bacula/src/qt-console/bat_conf.h index 209734aff8..ec4c9431b9 100644 --- a/bacula/src/qt-console/bat_conf.h +++ b/bacula/src/qt-console/bat_conf.h @@ -33,8 +33,8 @@ Switzerland, email:ftf@fsfeurope.org. */ -#ifndef __CONSOLE_CONF_H_ -#define __CONSOLE_CONF_H_ +#ifndef _BAT_CONF_H_ +#define _BAT_CONF_H_ /* * Resource codes -- they must be sequential for indexing @@ -108,4 +108,4 @@ union u_res { typedef union u_res URES; -#endif +#endif /* _BAT_CONF_H_ */ diff --git a/bacula/src/qt-console/console.cpp b/bacula/src/qt-console/console.cpp new file mode 100644 index 0000000000..2f288439b2 --- /dev/null +++ b/bacula/src/qt-console/console.cpp @@ -0,0 +1,265 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2000-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. +*/ + +/* + * Connect to a Bacula daemon + * + * Kern Sibbald, January MMVI + * + */ + +#include "bat.h" +#include "console.h" + +/* + * Connect to Director. If there are more than one, put up + * a modal dialog so that the user chooses one. + */ +bool Console::connect(QWidget *textEdit) +{ + JCR jcr; + + m_textEdit = textEdit; + +#ifdef xxx + if (ndir > 1) { + LockRes(); + foreach_res(dir, R_DIRECTOR) { + dirs = g_list_append(dirs, dir->hdr.name); + } + UnlockRes(); + dir_dialog = create_SelectDirectorDialog(); + combo = lookup_widget(dir_dialog, "combo1"); + dir_select = lookup_widget(dir_dialog, "dirselect"); + if (dirs) { + gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs); + } + gtk_widget_show(dir_dialog); + gtk_main(); + + if (reply == OK) { + gchar *ecmd = gtk_editable_get_chars((GtkEditable *)dir_select, 0, -1); + dir = (DIRRES *)GetResWithName(R_DIRECTOR, ecmd); + if (ecmd) { + g_free(ecmd); /* release director name string */ + } + } + if (dirs) { + g_free(dirs); + } + gtk_widget_destroy(dir_dialog); + dir_dialog = NULL; + } else { +#endif + /* Just take the first Director */ + LockRes(); + m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL); + UnlockRes(); + + if (!m_dir) { + return false; + } + + memset(&jcr, 0, sizeof(jcr)); + + set_statusf(_(" Connecting to Director %s:%d"), m_dir->address,dir->DIRport); + set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address,dir->DIRport); + + + LockRes(); + /* If cons==NULL, default console will be used */ + CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL); + UnlockRes(); + +#ifdef xxx + char buf[1024]; + /* Initialize Console TLS context */ + if (cons && (cons->tls_enable || cons->tls_require)) { + /* Generate passphrase prompt */ + bsnprintf(buf, sizeof(buf), _("Passphrase for Console \"%s\" TLS private key: "), cons->hdr.name); + + /* Initialize TLS context: + * Args: CA certfile, CA certdir, Certfile, Keyfile, + * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */ + cons->tls_ctx = new_tls_context(cons->tls_ca_certfile, + cons->tls_ca_certdir, cons->tls_certfile, + cons->tls_keyfile, tls_pem_callback, &buf, NULL, true); + + if (!cons->tls_ctx) { + bsnprintf(buf, sizeof(buf), _("Failed to initialize TLS context for Console \"%s\".\n"), + dir->hdr.name); + set_text(buf, strlen(buf)); + terminate_console(0); + return true; + } + + } + + /* Initialize Director TLS context */ + if (dir->tls_enable || dir->tls_require) { + /* Generate passphrase prompt */ + bsnprintf(buf, sizeof(buf), _("Passphrase for Director \"%s\" TLS private key: "), dir->hdr.name); + + /* Initialize TLS context: + * Args: CA certfile, CA certdir, Certfile, Keyfile, + * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */ + dir->tls_ctx = new_tls_context(dir->tls_ca_certfile, + dir->tls_ca_certdir, dir->tls_certfile, + dir->tls_keyfile, tls_pem_callback, &buf, NULL, true); + + if (!dir->tls_ctx) { + bsnprintf(buf, sizeof(buf), _("Failed to initialize TLS context for Director \"%s\".\n"), + dir->hdr.name); + set_text(buf, strlen(buf)); + terminate_console(0); + return true; + } + } +#endif + + m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address, + NULL, m_dir->DIRport, 0); + if (m__sock == NULL) { + return false; + } + + jcr.dir_bsock = m_sock; + if (!authenticate_director(&jcr, m_dir, cons)) { + set_text(m_sock->msg); + return false; + } + + set_status(_(" Initializing ...")); + + bnet_fsend(m_sock, "autodisplay on"); + + /* Read and display all initial messages */ + while (bnet_recv(m_sock) > 0) { + set_text(UA_sock->msg); + } + + /* Give GUI a chance */ + app->processEvents(); + +#ifdef xxx + /* Fill the run_dialog combo boxes */ + job_list = get_and_fill_combo(run_dialog, "combo_job", ".jobs"); + client_list = get_and_fill_combo(run_dialog, "combo_client", ".clients"); + fileset_list = get_and_fill_combo(run_dialog, "combo_fileset", ".filesets"); + messages_list = get_and_fill_combo(run_dialog, "combo_messages", ".msgs"); + pool_list = get_and_fill_combo(run_dialog, "combo_pool", ".pools"); + storage_list = get_and_fill_combo(run_dialog, "combo_storage", ".storage"); + type_list = get_and_fill_combo(run_dialog, "combo_type", ".types"); + level_list = get_and_fill_combo(run_dialog, "combo_level", ".levels"); + + /* Fill the label dialog combo boxes */ + fill_combo(label_dialog, "label_combo_storage", storage_list); + fill_combo(label_dialog, "label_combo_pool", pool_list); + + + /* Fill the restore_dialog combo boxes */ + fill_combo(restore_dialog, "combo_restore_job", job_list); + fill_combo(restore_dialog, "combo_restore_client", client_list); + fill_combo(restore_dialog, "combo_restore_fileset", fileset_list); + fill_combo(restore_dialog, "combo_restore_pool", pool_list); + fill_combo(restore_dialog, "combo_restore_storage", storage_list); +#endif + + set_status(_(" Connected")); + return true; +} + +#ifdef xxx +void write_director(const gchar *msg) +{ + if (UA_sock) { + at_prompt = false; + set_status(_(" Processing command ...")); + UA_sock->msglen = strlen(msg); + pm_strcpy(&UA_sock->msg, msg); + bnet_send(UA_sock); + } + if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) { + disconnect_from_director((gpointer)NULL); + gtk_main_quit(); + } +} + +extern "C" +void read_director(gpointer data, gint fd, GdkInputCondition condition) +{ + int stat; + + if (!UA_sock || UA_sock->fd != fd) { + return; + } + stat = bnet_recv(UA_sock); + if (stat >= 0) { + if (at_prompt) { + set_text("\n", 1); + at_prompt = false; + } + set_text(UA_sock->msg, UA_sock->msglen); + return; + } + if (is_bnet_stop(UA_sock)) { /* error or term request */ + gtk_main_quit(); + return; + } + /* Must be a signal -- either do something or ignore it */ + if (UA_sock->msglen == BNET_PROMPT) { + at_prompt = true; + set_status(_(" At prompt waiting for input ...")); + } + if (UA_sock->msglen == BNET_EOD) { + set_status_ready(); + } + return; +} + +static gint tag; + +void start_director_reader(gpointer data) +{ + + if (director_reader_running || !UA_sock) { + return; + } + tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL); + director_reader_running = true; +} + +void stop_director_reader(gpointer data) +{ + if (!director_reader_running) { + return; + } + gdk_input_remove(tag); + director_reader_running = false; +} +#endif diff --git a/bacula/src/qt-console/console.h b/bacula/src/qt-console/console.h index 6ff13fc600..bf836b6b96 100644 --- a/bacula/src/qt-console/console.h +++ b/bacula/src/qt-console/console.h @@ -1,4 +1,7 @@ +#ifndef _CONSOLE_H_ +#define _CONSOLE_H_ + #include "mainwindow.h" #include "config.h" #include "bacula.h" @@ -8,8 +11,27 @@ extern MainWindow *mainWin; extern QApplication *app; +class Console(QWidget *textEdit) +{ +public: + Console(); + bool connect(); +// void setDirector() +// void write(); +// void read(); + void set_text(const char *buf); + void set_textf(const char *fmt, ...); +private: + QWidget *m_textEdit; + DIRRES *m_dir; + BSOCK *m_sock; +}; + + + void set_textf(const char *fmt, ...); void set_text(const char *buf); int bvsnprintf(char *str, int32_t size, const char *format, va_list ap); +#endif /* _CONSOLE_H_ */ diff --git a/bacula/src/qt-console/main.cpp b/bacula/src/qt-console/main.cpp index 91e0386a20..2da43403c9 100644 --- a/bacula/src/qt-console/main.cpp +++ b/bacula/src/qt-console/main.cpp @@ -53,6 +53,8 @@ static char *configfile = NULL; MainWindow *mainWin; QApplication *app; +BSOCK *UA_sock = NULL; + int main(int argc, char *argv[]) { diff --git a/bacula/src/qt-console/mainwindow.cpp b/bacula/src/qt-console/mainwindow.cpp index d5a45f4725..f12ccad826 100644 --- a/bacula/src/qt-console/mainwindow.cpp +++ b/bacula/src/qt-console/mainwindow.cpp @@ -27,7 +27,7 @@ */ /* - * Main Window control for qt-console + * Main Window control for bat (qt-console) * * Kern Sibbald, January MMVI * diff --git a/bacula/src/qt-console/mainwindow.h b/bacula/src/qt-console/mainwindow.h index 884a28355b..41f1f47f08 100644 --- a/bacula/src/qt-console/mainwindow.h +++ b/bacula/src/qt-console/mainwindow.h @@ -32,6 +32,8 @@ * Written by Kern Sibbald, January MMVI */ +#ifndef _MAINWINDOW_H_ +#define _MAINWINDOW_H_ #include #include "ui_main.h" @@ -52,3 +54,4 @@ private: QString m_UserInput; }; +#endif /* _MAINWINDOW_H_ */ -- 2.39.5