2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation plus additions
11 that are listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
34 * Kern Sibbald, January MMVI
38 #include <QAbstractEventDispatcher>
42 Console::Console(QStackedWidget *parent)
45 QTreeWidgetItem *item, *topItem;
46 QTreeWidget *treeWidget = mainWin->treeWidget;
49 parent->addWidget(this);
52 m_textEdit = textEdit; /* our console screen */
53 m_cursor = new QTextCursor(m_textEdit->document());
54 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
56 bRestore *brestore = new bRestore(parent);
57 brestore->setupUi(brestore);
58 parent->addWidget(brestore);
60 /* Just take the first Director */
62 m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
65 /* Dummy setup of treeWidget */
67 treeWidget->setColumnCount(1);
68 treeWidget->setHeaderLabel("Selection");
69 topItem = new QTreeWidgetItem(treeWidget);
70 topItem->setText(0, m_dir->name());
71 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
72 item = new QTreeWidgetItem(topItem);
74 item->setText(0, "Console");
75 item->setText(1, "0");
76 QBrush redBrush(Qt::red);
77 item->setForeground(0, redBrush);
78 item = new QTreeWidgetItem(topItem);
79 item->setText(0, "brestore");
80 item->setText(1, "1");
81 treeWidget->expandItem(topItem);
88 * Connect to Director. If there are more than one, put up
89 * a modal dialog so that the user chooses one.
91 void Console::connect()
95 m_textEdit = textEdit; /* our console screen */
98 mainWin->set_status(" No Director found.");
102 mainWin->set_status(" Already connected.");
106 memset(&jcr, 0, sizeof(jcr));
108 mainWin->set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport);
109 set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport);
111 /* Give GUI a chance */
112 app->processEvents();
115 /* If cons==NULL, default console will be used */
116 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
119 m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address,
120 NULL, m_dir->DIRport, 0);
121 if (m_sock == NULL) {
122 mainWin->set_status("Connection failed");
125 /* Update page selector to green to indicate that Console is connected */
126 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/connected.png")));
127 QBrush greenBrush(Qt::green);
128 m_consoleItem->setForeground(0, greenBrush);
131 jcr.dir_bsock = m_sock;
133 if (!authenticate_director(&jcr, m_dir, cons)) {
134 set_text(m_sock->msg);
138 /* Give GUI a chance */
139 app->processEvents();
141 mainWin->set_status(_(" Initializing ..."));
143 /* Set up input notifier */
144 m_notifier = new QSocketNotifier(m_sock->fd, QSocketNotifier::Read, 0);
145 QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(read_dir(int)));
147 job_list = get_list(".jobs");
148 client_list = get_list(".clients");
149 fileset_list = get_list(".filesets");
150 messages_list = get_list(".messages");
151 pool_list = get_list(".pools");
152 storage_list = get_list(".storage");
153 type_list = get_list(".types");
154 level_list = get_list(".levels");
156 mainWin->set_status(_(" Connected"));
162 * Send a command to the director, and read all the resulting
163 * output into a list.
165 QStringList Console::get_list(char *cmd)
172 while ((stat = read()) > 0) {
173 strip_trailing_junk(msg());
182 * Send a job name to the director, and read all the resulting
185 bool Console::get_job_defaults(char *job_name, struct job_defaults &job_defs)
191 bsnprintf(cmd, sizeof(cmd), ".defaults job=\"%s\"", job_name);
193 while ((stat = read()) > 0) {
194 strip_trailing_junk(msg());
205 void Console::writeSettings()
207 QFont font = get_font();
209 QSettings settings("bacula.org", "bat");
210 settings.beginGroup("Console");
211 settings.setValue("consoleFont", font.family());
212 settings.setValue("consolePointSize", font.pointSize());
213 settings.setValue("consoleFixedPitch", font.fixedPitch());
218 * Read and restore user settings
220 void Console::readSettings()
222 QFont font = get_font();
224 QSettings settings("bacula.org", "bat");
225 settings.beginGroup("Console");
226 font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
227 font.setPointSize(settings.value("consolePointSize", 10).toInt());
228 font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
230 m_textEdit->setFont(font);
234 * Set the console textEdit font
236 void Console::set_font()
239 QFont font = QFontDialog::getFont(&ok, QFont(m_textEdit->font()), this);
241 m_textEdit->setFont(font);
246 * Get the console text edit font
248 const QFont Console::get_font()
250 return m_textEdit->font();
254 void Console::status_dir()
256 write_dir("status dir\n");
260 * Put text into the console window
262 void Console::set_textf(const char *fmt, ...)
267 va_start(arg_ptr, fmt);
268 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
273 void Console::set_text(const QString buf)
275 m_cursor->movePosition(QTextCursor::End);
276 m_cursor->insertText(buf);
280 void Console::set_text(const char *buf)
282 m_cursor->movePosition(QTextCursor::End);
283 m_cursor->insertText(buf);
286 /* Position cursor to end of screen */
287 void Console::update_cursor()
289 QApplication::restoreOverrideCursor();
290 m_textEdit->moveCursor(QTextCursor::End);
291 m_textEdit->ensureCursorVisible();
295 * This should be moved into a bSocket class
305 /* Send a command to the Director */
306 void Console::write_dir(const char *msg)
310 mainWin->set_status(_(" Processing command ..."));
311 QApplication::setOverrideCursor(Qt::WaitCursor);
312 m_sock->msglen = strlen(msg);
313 pm_strcpy(&m_sock->msg, msg);
316 mainWin->set_status(" Director not connected. Click on connect button.");
317 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
318 QBrush redBrush(Qt::red);
319 m_consoleItem->setForeground(0, redBrush);
323 int Console::write(const char *msg)
325 m_sock->msglen = strlen(msg);
326 pm_strcpy(&m_sock->msg, msg);
327 return bnet_send(m_sock);
331 * Blocking read from director */
337 stat = bnet_wait_data_intr(m_sock, 1);
341 app->processEvents();
346 return bnet_recv(m_sock);
351 /* Called by signal when the Director has output for us */
352 void Console::read_dir(int fd)
360 stat = bnet_recv(m_sock);
366 set_text(m_sock->msg);
369 if (is_bnet_stop(m_sock)) { /* error or term request */
372 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
373 QBrush redBrush(Qt::red);
374 m_consoleItem->setForeground(0, redBrush);
375 m_notifier->setEnabled(false);
378 mainWin->set_status(_(" Director disconnected."));
379 QApplication::restoreOverrideCursor();
382 /* Must be a signal -- either do something or ignore it */
383 if (m_sock->msglen == BNET_PROMPT) {
385 mainWin->set_status(_(" At prompt waiting for input ..."));
388 if (m_sock->msglen == BNET_EOD) {
389 mainWin->set_status_ready();