]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/pages.cpp
Fix bat bug that consumes connections + add braces on ifs + rename subroutines
[bacula/bacula] / bacula / src / qt-console / pages.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
5
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 and included
11    in the file LICENSE.
12
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.
17
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
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
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.
27 */
28 /*
29  *   Version $Id$
30  *
31  *   Dirk Bartley, March 2007
32  */
33
34 #include "bat.h"
35 #include "pages.h"
36
37 /* A global function */
38 bool isWin32Path(QString &fullPath) 
39 {
40    if (fullPath.size()<2) {
41       return false;
42    }
43
44    bool toret = fullPath[1].toAscii() == ':' && fullPath[0].isLetter();
45    if (mainWin->m_miscDebug) {
46       if (toret)
47          Pmsg1(000, "returning from isWin32Path true %s\n", fullPath.toUtf8().data());
48       else
49          Pmsg1(000, "returning from isWin32Path false %s\n", fullPath.toUtf8().data());
50    }
51    return toret;
52 }
53
54 /*
55  * dockPage
56  * This function is intended to be called from within the Pages class to pull
57  * a window from floating to in the stack widget.
58  */
59
60 void Pages::dockPage()
61 {
62    if (isDocked()) {
63       return;
64    }
65
66    /* These two lines are for making sure if it is being changed from a window
67     * that it has the proper window flag and parent.
68     */
69    setWindowFlags(Qt::Widget);
70
71    /* calculate the index that the tab should be inserted into */
72    int tabPos = 0;
73    QTreeWidgetItemIterator it(mainWin->treeWidget);
74    while (*it) {
75       Pages *somepage = mainWin->getFromHash(*it);
76       if (this == somepage) {
77          tabPos += 1;
78          break;
79       }
80       int pageindex = mainWin->tabWidget->indexOf(somepage);
81       if (pageindex != -1) { tabPos = pageindex; }
82       ++it;
83    }
84
85    /* This was being done already */
86    m_parent->insertTab(tabPos, this, m_name);
87
88    /* Set docked flag */
89    m_docked = true;
90    m_onceDocked = true;
91    mainWin->tabWidget->setCurrentWidget(this);
92    /* lets set the page selectors action for docking or undocking */
93    setContextMenuDockText();
94 }
95
96 /*
97  * undockPage
98  * This function is intended to be called from within the Pages class to put
99  * a window from the stack widget to a floating window.
100  */
101
102 void Pages::undockPage()
103 {
104    if (!isDocked()) {
105       return;
106    }
107
108    /* Change from a stacked widget to a normal window */
109    m_parent->removeTab(m_parent->indexOf(this));
110    setWindowFlags(Qt::Window);
111    show();
112    /* Clear docked flag */
113    m_docked = false;
114    /* The window has been undocked, lets change the context menu */
115    setContextMenuDockText();
116 }
117
118 /*
119  * This function is intended to be called with the subclasses.  When it is 
120  * called the specific sublclass does not have to be known to Pages.  When it 
121  * is called this function will change the page from it's current state of being
122  * docked or undocked and change it to the other.
123  */
124
125 void Pages::togglePageDocking()
126 {
127    if (m_docked) {
128       undockPage();
129    } else {
130       dockPage();
131    }
132 }
133
134 /*
135  * This function is because I wanted for some reason to keep it protected but still 
136  * give any subclasses the ability to find out if it is currently stacked or not.
137  */
138 bool Pages::isDocked()
139 {
140    return m_docked;
141 }
142
143 /*
144  * This function is because after the tabbed widget was added I could not tell
145  * from is docked if it had been docked yet.  To prevent status pages from requesting
146  * status from the director
147  */
148 bool Pages::isOnceDocked()
149 {
150    return m_onceDocked;
151 }
152
153
154 /*
155  * To keep m_closeable protected as well
156  */
157 bool Pages::isCloseable()
158 {
159    return m_closeable;
160 }
161
162 /*
163  * When a window is closed, this slot is called.  The idea is to put it back in the
164  * stack here, and it works.  I wanted to get it to the top of the stack so that the
165  * user immediately sees where his window went.  Also, if he undocks the window, then
166  * closes it with the tree item highlighted, it may be confusing that the highlighted
167  * treewidgetitem is not the stack item in the front.
168  */
169
170 void Pages::closeEvent(QCloseEvent* event)
171 {
172    /* A Widget was closed, lets toggle it back into the window, and set it in front. */
173    dockPage();
174
175    /* this fixes my woes of getting the widget to show up on top when closed */
176    event->ignore();
177
178    /* Set the current tree widget item in the Page Selector window to the item 
179     * which represents "this" 
180     * Which will also bring "this" to the top of the stacked widget */
181    setCurrent();
182 }
183
184 /*
185  * The next three are virtual functions.  The idea here is that each subclass will have the
186  * built in virtual function to override if the programmer wants to populate the window
187  * when it it is first clicked.
188  */
189 void Pages::PgSeltreeWidgetClicked()
190 {
191 }
192
193 /*
194  *  Virtual function which is called when this page is visible on the stack.
195  *  This will be overridden by classes that want to populate if they are on the
196  *  top.
197  */
198 void Pages::currentStackItem()
199 {
200 }
201
202 /*
203  * Function to close the stacked page and remove the widget from the
204  * Page selector window
205  */
206 void Pages::closeStackPage()
207 {
208    /* First get the tree widget item and destroy it */
209    QTreeWidgetItem *item=mainWin->getFromHash(this);
210    /* remove the QTreeWidgetItem <-> page from the hash */
211    mainWin->hashRemove(item, this);
212    /* remove the item from the page selector by destroying it */
213    delete item;
214    /* remove this */
215    delete this;
216 }
217
218 /*
219  * Function to set members from the external mainwin and it's overload being
220  * passed a specific QTreeWidgetItem to be it's parent on the tree
221  */
222 void Pages::pgInitialize()
223 {
224    pgInitialize(QString(), NULL);
225 }
226
227 void Pages::pgInitialize(const QString &name)
228 {
229    pgInitialize(name, NULL);
230 }
231
232 void Pages::pgInitialize(const QString &tname, QTreeWidgetItem *parentTreeWidgetItem)
233 {
234    m_docked = false;
235    m_onceDocked = false;
236    if (tname.size()) {
237       m_name = tname;
238    }
239    m_parent = mainWin->tabWidget;
240    m_console = mainWin->currentConsole();
241
242    if (!parentTreeWidgetItem) {
243       parentTreeWidgetItem = m_console->directorTreeItem();
244    }
245
246    QTreeWidgetItem *item = new QTreeWidgetItem(parentTreeWidgetItem);
247    QString name; 
248    treeWidgetName(name);
249    item->setText(0, name);
250    mainWin->hashInsert(item, this);
251    setTitle();
252 }
253
254 /*
255  * Virtual Function to return a name
256  * All subclasses should override this function
257  */
258 void Pages::treeWidgetName(QString &name)
259 {
260    name = m_name;
261 }
262
263 /*
264  * Function to simplify executing a console command and bringing the
265  * console to the front of the stack
266  */
267 void Pages::consoleCommand(QString &command)
268 {
269    consoleCommand(command, true);
270 }
271 void Pages::consoleCommand(QString &command, bool setCurrent)
272 {
273    int conn;
274    bool donotify = false;
275    if (m_console->getDirComm(conn))  {
276       if (m_console->is_notify_enabled(conn)) {
277          donotify = true;
278          m_console->notify(conn, false);
279       }
280       consoleCommand(command, conn, setCurrent);
281       if (donotify) { m_console->notify(conn, true); }
282    }
283 }
284 void Pages::consoleCommand(QString &command, int conn)
285 {
286    consoleCommand(command, conn, true);
287 }
288 void Pages::consoleCommand(QString &command, int conn, bool setCurrent)
289 {
290    /* Bring this director's console to the front of the stack */
291    if (setCurrent) { setConsoleCurrent(); }
292    QString displayhtml("<font color=\"blue\">");
293    displayhtml += command + "</font>\n";
294    m_console->display_html(displayhtml);
295    m_console->display_text("\n");
296    mainWin->waitEnter();
297    m_console->write_dir(conn, command.toUtf8().data(), false);
298    m_console->displayToPrompt(conn);
299    mainWin->waitExit();
300 }
301
302 /*
303  * Function for handling undocked windows becoming active.
304  * Change the currently selected item in the page selector window to the now
305  * active undocked window.  This will also make the console for the undocked
306  * window m_currentConsole.
307  */
308 void Pages::changeEvent(QEvent *event)
309 {
310    if ((event->type() ==  QEvent::ActivationChange) && (isActiveWindow())) {
311       setCurrent();
312    }
313 }
314
315 /*
316  * Function to simplify getting the name of the class and the director into
317  * the caption or title of the window
318  */
319 void Pages::setTitle()
320 {
321    QString wdgname, director;
322    treeWidgetName(wdgname);
323    m_console->getDirResName(director);
324    QString title = tr("%1 of Director %2").arg(wdgname).arg(director);
325    setWindowTitle(title);
326 }
327
328 /*
329  * Bring the current directors console window to the top of the stack.
330  */
331 void Pages::setConsoleCurrent()
332 {
333    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(m_console));
334 }
335
336 /*
337  * Bring this window to the top of the stack.
338  */
339 void Pages::setCurrent()
340 {
341    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(this));
342 }
343
344 /*
345  * Function to set the text of the toggle dock context menu when page and
346  * widget item are NOT known.  
347  */
348 void Pages::setContextMenuDockText()
349 {
350    QTreeWidgetItem *item = mainWin->getFromHash(this);
351    QString docktext;
352    if (isDocked()) {
353       docktext = tr("UnDock %1 Window").arg(item->text(0));
354    } else {
355       docktext = tr("ReDock %1 Window").arg(item->text(0));
356    }
357       
358    mainWin->actionToggleDock->setText(docktext);
359    setTreeWidgetItemDockColor();
360 }
361
362 /*
363  * Function to set the color of the tree widget item based on whether it is
364  * docked or not.
365  */
366 void Pages::setTreeWidgetItemDockColor()
367 {
368    QTreeWidgetItem* item = mainWin->getFromHash(this);
369    if (item) {
370       if (item->text(0) != tr("Console")) {
371          if (isDocked()) {
372          /* Set the brush to blue if undocked */
373             QBrush blackBrush(Qt::black);
374             item->setForeground(0, blackBrush);
375          } else {
376          /* Set the brush back to black if docked */
377             QBrush blueBrush(Qt::blue);
378             item->setForeground(0, blueBrush);
379          }
380       }
381    }
382 }
383
384 /* Function to get a list of volumes */
385 void Pages::getVolumeList(QStringList &volumeList)
386 {
387    QString query("SELECT VolumeName AS Media FROM Media ORDER BY Media");
388    if (mainWin->m_sqlDebug) {
389       Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
390    }
391    QStringList results;
392    if (m_console->sql_cmd(query, results)) {
393       QString field;
394       QStringList fieldlist;
395       /* Iterate through the lines of results. */
396       foreach (QString resultline, results) {
397          fieldlist = resultline.split("\t");
398          volumeList.append(fieldlist[0]);
399       } /* foreach resultline */
400    } /* if results from query */
401 }
402
403 /* Function to get a list of volumes */
404 void Pages::getStatusList(QStringList &statusLongList)
405 {
406    QString statusQuery("SELECT JobStatusLong FROM Status");
407    if (mainWin->m_sqlDebug) {
408       Pmsg1(000, "Query cmd : %s\n",statusQuery.toUtf8().data());
409    }
410    QStringList statusResults;
411    if (m_console->sql_cmd(statusQuery, statusResults)) {
412       QString field;
413       QStringList fieldlist;
414       /* Iterate through the lines of results. */
415       foreach (QString resultline, statusResults) {
416          fieldlist = resultline.split("\t");
417          statusLongList.append(fieldlist[0]);
418       } /* foreach resultline */
419    } /* if results from statusquery */
420 }