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.
32 * Kern Sibbald, January MMVI
36 #include <QAbstractEventDispatcher>
41 Console::Console(QStackedWidget *parent)
44 QTreeWidgetItem *item, *topItem;
45 QTreeWidget *treeWidget = mainWin->treeWidget;
48 parent->addWidget(this);
51 m_textEdit = textEdit; /* our console screen */
52 m_cursor = new QTextCursor(m_textEdit->document());
53 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
55 bRestore *restore = new bRestore(parent);
56 restore->setupUi(restore);
57 parent->addWidget(restore);
59 /* Just take the first Director */
61 m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
64 /* Dummy setup of treeWidget */
66 treeWidget->setColumnCount(1);
67 treeWidget->setHeaderLabel("Selection");
68 topItem = new QTreeWidgetItem(treeWidget);
69 topItem->setText(0, m_dir->name());
70 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
71 item = new QTreeWidgetItem(topItem);
73 item->setText(0, "Console");
74 item->setText(1, "0");
75 QBrush redBrush(Qt::red);
76 item->setForeground(0, redBrush);
77 item = new QTreeWidgetItem(topItem);
78 item->setText(0, "Restore");
79 item->setText(1, "1");
80 treeWidget->expandItem(topItem);
87 * Connect to Director. If there are more than one, put up
88 * a modal dialog so that the user chooses one.
90 void Console::connect()
94 m_textEdit = textEdit; /* our console screen */
97 mainWin->set_status("No Director found.");
101 mainWin->set_status("Already connected.");
105 memset(&jcr, 0, sizeof(jcr));
107 mainWin->set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport);
108 set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport);
110 /* Give GUI a chance */
111 app->processEvents();
114 /* If cons==NULL, default console will be used */
115 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
118 m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address,
119 NULL, m_dir->DIRport, 0);
120 if (m_sock == NULL) {
121 mainWin->set_status("Connection failed");
124 /* Update page selector to green to indicate that Console is connected */
125 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/connected.png")));
126 QBrush greenBrush(Qt::green);
127 m_consoleItem->setForeground(0, greenBrush);
130 jcr.dir_bsock = m_sock;
132 if (!authenticate_director(&jcr, m_dir, cons)) {
133 set_text(m_sock->msg);
137 /* Give GUI a chance */
138 app->processEvents();
140 mainWin->set_status(_(" Initializing ..."));
142 bnet_fsend(m_sock, "autodisplay on");
144 /* Set up input notifier */
145 m_notifier = new QSocketNotifier(m_sock->fd, QSocketNotifier::Read, 0);
146 QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(read_dir(int)));
148 /* Give GUI a chance */
149 app->processEvents();
152 * Query Directory for .jobs, .clients, .filesets, .msgs,
153 * .pools, .storage, .types, .levels, ...
156 mainWin->set_status(_(" Connected"));
161 void Console::writeSettings()
163 QFont font = get_font();
165 QSettings settings("bacula.org", "bat");
166 settings.beginGroup("Console");
167 settings.setValue("consoleFont", font.family());
168 settings.setValue("consolePointSize", font.pointSize());
169 settings.setValue("consoleFixedPitch", font.fixedPitch());
173 void Console::readSettings()
175 QFont font = get_font();
177 QSettings settings("bacula.org", "bat");
178 settings.beginGroup("Console");
179 font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
180 font.setPointSize(settings.value("consolePointSize", 10).toInt());
181 font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
183 m_textEdit->setFont(font);
186 void Console::set_font()
189 QFont font = QFontDialog::getFont(&ok, QFont(m_textEdit->font()), this);
191 m_textEdit->setFont(font);
195 const QFont Console::get_font()
197 return m_textEdit->font();
201 void Console::status_dir()
203 write_dir("status dir\n");
206 void Console::set_textf(const char *fmt, ...)
211 va_start(arg_ptr, fmt);
212 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
217 void Console::set_text(const QString buf)
219 m_cursor->movePosition(QTextCursor::End);
220 m_cursor->insertText(buf);
224 void Console::set_text(const char *buf)
226 m_cursor->movePosition(QTextCursor::End);
227 m_cursor->insertText(buf);
230 /* Position cursor to end of screen */
231 void Console::update_cursor()
233 QApplication::restoreOverrideCursor();
234 m_textEdit->moveCursor(QTextCursor::End);
235 m_textEdit->ensureCursorVisible();
239 /* Send a command to the Director */
240 void Console::write_dir(const char *msg)
244 mainWin->set_status(_(" Processing command ..."));
245 QApplication::setOverrideCursor(Qt::WaitCursor);
246 m_sock->msglen = strlen(msg);
247 pm_strcpy(&m_sock->msg, msg);
250 mainWin->set_status(" Director not connected. Click on connect button.");
251 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
252 QBrush redBrush(Qt::red);
253 m_consoleItem->setForeground(0, redBrush);
257 /* Called by signal when the Director has output for us */
258 void Console::read_dir(int fd)
266 stat = bnet_recv(m_sock);
272 set_text(m_sock->msg);
275 if (is_bnet_stop(m_sock)) { /* error or term request */
278 mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
279 QBrush redBrush(Qt::red);
280 m_consoleItem->setForeground(0, redBrush);
281 m_notifier->setEnabled(false);
284 mainWin->set_status(_(" Director disconnected."));
285 QApplication::restoreOverrideCursor();
288 /* Must be a signal -- either do something or ignore it */
289 if (m_sock->msglen == BNET_PROMPT) {
291 mainWin->set_status(_(" At prompt waiting for input ..."));
294 if (m_sock->msglen == BNET_EOD) {
295 mainWin->set_status_ready();
305 void start_director_reader(gpointer data)
308 if (director_reader_running || !UA_sock) {
311 tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
312 director_reader_running = true;
315 void stop_director_reader(gpointer data)
317 if (!director_reader_running) {
320 gdk_input_remove(tag);
321 director_reader_running = false;