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