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